]> git.sesse.net Git - vlc/blob - src/misc/modules.c
* Sync MOSX dev/CVS tree : code support for MacOS X audio, video and
[vlc] / src / misc / modules.c
1 /*****************************************************************************
2  * modules.c : Built-in and dynamic modules management functions
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: modules.c,v 1.20 2001/04/06 18:18:10 massiot Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Ethan C. Baldridge <BaldridgeE@cadmus.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24 #include "defs.h"
25
26 #include "config.h"
27
28 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
29  * is set to 64. Don't try to be cleverer. */
30 #ifdef _FILE_OFFSET_BITS
31 #undef _FILE_OFFSET_BITS
32 #endif
33
34 #include <stdlib.h>                                      /* free(), strtol() */
35 #include <stdio.h>                                              /* sprintf() */
36 #include <string.h>                                              /* strdup() */
37 #include <dirent.h>
38
39 #if defined(HAVE_DLFCN_H)                                /* Linux, BSD, Hurd */
40 #include <dlfcn.h>                           /* dlopen(), dlsym(), dlclose() */
41
42 #elif defined(HAVE_IMAGE_H)                                          /* BeOS */
43 #include <image.h>
44
45 #else
46 /* FIXME: this isn't supposed to be an error */
47 #error no dynamic plugins available on your system !
48 #endif
49
50 #ifdef SYS_BEOS
51 #include "beos_specific.h"
52 #endif
53
54 #include "common.h"
55 #include "threads.h"
56
57 #ifdef SYS_DARWIN1_3
58 #include <sys/param.h>                                    /* for MAXPATHLEN */
59 #include "main.h"
60 extern main_t *p_main;
61 #endif
62
63 #include "intf_msg.h"
64 #include "modules.h"
65 #include "modules_core.h"
66
67 /* Local prototypes */
68 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename );
69 static int HideModule( module_t * p_module );
70 static int FreeModule( module_bank_t * p_bank, module_t * p_module );
71 static int LockModule( module_t * p_module );
72 static int UnlockModule( module_t * p_module );
73 static int CallSymbol( module_t * p_module, char * psz_name );
74
75 /*****************************************************************************
76  * module_CreateBank: create the module bank.
77  *****************************************************************************
78  * This function creates a module bank structure.
79  *****************************************************************************/
80 module_bank_t * module_CreateBank( void )
81 {
82     module_bank_t * p_bank;
83
84     p_bank = malloc( sizeof( module_bank_t ) );
85
86     return( p_bank );
87 }
88
89 /*****************************************************************************
90  * module_InitBank: create the module bank.
91  *****************************************************************************
92  * This function creates a module bank structure and fills it with the
93  * built-in modules, as well as all the dynamic modules it can find.
94  *****************************************************************************/
95 void module_InitBank( module_bank_t * p_bank )
96 {
97     static char * path[] = { ".", "lib", PLUGIN_PATH, NULL, NULL };
98
99     char **         ppsz_path = path;
100     char *          psz_fullpath;
101     char *          psz_file;
102 #ifdef SYS_BEOS
103     char *          psz_vlcpath = beos_GetProgramPath();
104     int             i_vlclen = strlen( psz_vlcpath );
105     boolean_t       b_notinroot;
106 #elif defined SYS_DARWIN1_3
107     static char     once = 0;
108     static char     app_path[ MAXPATHLEN ];
109     // HACK TO CUT OUT trailing 'vlc'
110     int             i_pathlen = strlen( p_main->ppsz_argv[0] ) - 3;
111 #endif
112     DIR *           dir;
113     struct dirent * file;
114
115     p_bank->first = NULL;
116     vlc_mutex_init( &p_bank->lock );
117
118     intf_WarnMsg( 1, "module: module bank initialized" );
119
120 #ifdef SYS_DARWIN1_3
121     if ( !once )
122     {
123         once = 1;
124         strncpy( app_path, p_main->ppsz_argv[ 0 ], i_pathlen );
125         intf_ErrMsg( "%s", p_main->ppsz_argv[ 0 ] );
126         strcat( app_path, "lib" );
127         path[ 3 ] = app_path ;
128         intf_ErrMsg( "%s", path[ 3 ] );
129     }
130 #endif
131
132     for( ; *ppsz_path != NULL ; ppsz_path++ )
133     {
134         /* Store strlen(*ppsz_path) for later use. */
135         int i_dirlen = strlen( *ppsz_path );
136
137 #ifdef SYS_BEOS
138         b_notinroot = 0;
139         /* Under BeOS, we need to add beos_GetProgramPath() to access
140          * files under the current directory */
141         if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
142         {
143             i_dirlen += i_vlclen + 2;
144             b_notinroot = 1;
145
146             psz_fullpath = malloc( i_dirlen );
147             if( psz_fullpath == NULL )
148             {
149                 continue;
150             }
151             sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
152         }
153         else
154 #endif
155         {
156             psz_fullpath = *ppsz_path;
157         }
158
159         intf_WarnMsgImm( 2, "module: browsing %s", psz_fullpath );
160
161         if( (dir = opendir( psz_fullpath )) )
162         {
163             /* Parse the directory and try to load all files it contains. */
164             while( (file = readdir( dir )) )
165             {
166                 int i_filelen = strlen( file->d_name );
167
168                 /* We only load files ending with ".so" */
169                 if( i_filelen > 3
170                         && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
171                 {
172                     psz_file = malloc( i_dirlen + i_filelen + 2 );
173                     if( psz_file == NULL )
174                     {
175                         continue;
176                     }
177                     sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
178
179                     /* We created a nice filename -- now we just try to load
180                      * it as a dynamic module. */
181                     AllocateDynModule( p_bank, psz_file );
182
183                     /* We don't care if the allocation succeeded */
184                     free( psz_file );
185                 }
186             }
187
188             /* Close the directory if successfully opened */
189             closedir( dir );
190         }
191
192 #ifdef SYS_BEOS
193         if( b_notinroot )
194         {
195             free( psz_fullpath );
196         }
197 #endif
198     }
199
200     return;
201 }
202
203 /*****************************************************************************
204  * module_DestroyBank: destroy the module bank.
205  *****************************************************************************
206  * This function unloads all unused dynamic modules and removes the module
207  * bank in case of success.
208  *****************************************************************************/
209 void module_DestroyBank( module_bank_t * p_bank )
210 {
211     module_t * p_next;
212
213     while( p_bank->first != NULL )
214     {
215         if( FreeModule( p_bank, p_bank->first ) )
216         {
217             /* Module deletion failed */
218             intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
219                          p_bank->first->psz_name );
220
221             /* We just free the module by hand. Niahahahahaha. */
222             p_next = p_bank->first->next;
223             free(p_bank->first);
224             p_bank->first = p_next;
225         }
226     }
227
228     /* Destroy the lock */
229     vlc_mutex_destroy( &p_bank->lock );
230     
231     /* We can free the module bank */
232     free( p_bank );
233
234     return;
235 }
236
237 /*****************************************************************************
238  * module_ResetBank: reset the module bank.
239  *****************************************************************************
240  * This function resets the module bank by unloading all unused dynamic
241  * modules.
242  *****************************************************************************/
243 void module_ResetBank( module_bank_t * p_bank )
244 {
245     intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
246     return;
247 }
248
249 /*****************************************************************************
250  * module_ManageBank: manage the module bank.
251  *****************************************************************************
252  * This function parses the module bank and hides modules that have been
253  * unused for a while.
254  *****************************************************************************/
255 void module_ManageBank( module_bank_t * p_bank )
256 {
257     module_t * p_module;
258
259     /* We take the global lock */
260     vlc_mutex_lock( &p_bank->lock );
261
262     /* Parse the module list to see if any modules need to be unloaded */
263     for( p_module = p_bank->first ;
264          p_module != NULL ;
265          p_module = p_module->next )
266     {
267         /* If the module is unused and if it is a dynamic module... */
268         if( p_module->i_usage == 0 && !p_module->b_builtin )
269         {
270             if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
271             {
272                 p_module->i_unused_delay++;
273             }
274             else
275             {
276                 intf_WarnMsg( 1, "module: hiding unused module `%s'",
277                               p_module->psz_name );
278                 HideModule( p_module );
279             }
280         }
281     }
282
283     /* We release the global lock */
284     vlc_mutex_unlock( &p_bank->lock );
285
286     return;
287 }
288
289 /*****************************************************************************
290  * module_Need: return the best module function, given a capability list.
291  *****************************************************************************
292  * This function returns the module that best fits the asked capabilities.
293  *****************************************************************************/
294 module_t * module_Need( module_bank_t *p_bank,
295                         int i_capabilities, void *p_data )
296 {
297     module_t * p_module;
298     module_t * p_bestmodule = NULL;
299     int i_score, i_totalscore, i_bestscore = 0;
300     int i_index;
301
302     /* We take the global lock */
303     vlc_mutex_lock( &p_bank->lock );
304
305     /* Parse the module list for capabilities and probe each of them */
306     for( p_module = p_bank->first ;
307          p_module != NULL ;
308          p_module = p_module->next )
309     {
310         /* Test that this module can do everything we need */
311         if( ( p_module->i_capabilities & i_capabilities ) == i_capabilities )
312         {
313             i_totalscore = 0;
314
315             LockModule( p_module );
316
317             /* Parse all the requested capabilities and test them */
318             for( i_index = 0 ; (1 << i_index) <= i_capabilities ; i_index++ )
319             {
320                 if( ( (1 << i_index) & i_capabilities ) )
321                 {
322                     i_score = ( (function_list_t *)p_module->p_functions)
323                                                   [i_index].pf_probe( p_data );
324
325                     if( i_score )
326                     {
327                         i_totalscore += i_score;
328                     }
329                     else
330                     {
331                         break;
332                     }
333                 }
334             }
335
336             /* If the high score was broken, we have a new champion */
337             if( i_totalscore > i_bestscore )
338             {
339                 /* Keep the current module locked, but release the previous */
340                 if( p_bestmodule != NULL )
341                 {
342                     UnlockModule( p_bestmodule );
343                 }
344
345                 /* This is the new best module */
346                 i_bestscore = i_totalscore;
347                 p_bestmodule = p_module;
348             }
349             else
350             {
351                 /* This module wasn't interesting, unlock it and forget it */
352                 UnlockModule( p_module );
353             }
354         }
355     }
356
357     /* We release the global lock */
358     vlc_mutex_unlock( &p_bank->lock );
359
360     if( p_bestmodule != NULL )
361     {
362         intf_WarnMsg( 1, "module: locking module `%s'",
363                       p_bestmodule->psz_name );
364     }
365
366     /* Don't forget that the module is still locked if bestmodule != NULL */
367     return( p_bestmodule );
368 }
369
370 /*****************************************************************************
371  * module_Unneed: decrease the usage count of a module.
372  *****************************************************************************
373  * This function must be called by the thread that called module_Need, to
374  * decrease the reference count and allow for hiding of modules.
375  *****************************************************************************/
376 void module_Unneed( module_bank_t * p_bank, module_t * p_module )
377 {
378     /* We take the global lock */
379     vlc_mutex_lock( &p_bank->lock );
380
381     /* Just unlock the module - we can't do anything if it fails,
382      * so there is no need to check the return value. */
383     UnlockModule( p_module );
384
385     intf_WarnMsg( 1, "module: unlocking module `%s'", p_module->psz_name );
386
387     /* We release the global lock */
388     vlc_mutex_unlock( &p_bank->lock );
389
390     return;
391 }
392
393 /*****************************************************************************
394  * Following functions are local.
395  *****************************************************************************/
396
397 /*****************************************************************************
398  * AllocateDynModule: load a module into memory and initialize it.
399  *****************************************************************************
400  * This function loads a dynamically loadable module and allocates a structure
401  * for its information data. The module can then be handled by module_Need,
402  * module_Unneed and HideModule. It can be removed by FreeModule.
403  *****************************************************************************/
404 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename )
405 {
406     module_t * p_module, * p_othermodule;
407     module_handle_t handle;
408
409     /* Try to dynamically load the module. */
410     if( module_load( psz_filename, &handle ) )
411     {
412         /* The dynamic module couldn't be opened */
413         intf_WarnMsgImm( 1, "module warning: cannot open %s (%s)",
414                          psz_filename, module_error() );
415         return( -1 );
416     }
417
418     /* Now that we have successfully loaded the module, we can
419      * allocate a structure for it */ 
420     p_module = malloc( sizeof( module_t ) );
421     if( p_module == NULL )
422     {
423         intf_ErrMsg( "module error: can't allocate p_module" );
424         module_unload( handle );
425         return( -1 );
426     }
427
428     /* We need to fill these since they may be needed by CallSymbol() */
429     p_module->psz_filename = psz_filename;
430     p_module->handle = handle;
431
432     /* Initialize the module : fill p_module->psz_name, etc. */
433     if( CallSymbol( p_module, "InitModule" ) != 0 )
434     {
435         /* We couldn't call InitModule() */
436         free( p_module );
437         module_unload( handle );
438         return( -1 );
439     }
440
441     /* Check that version numbers match */
442     if( strcmp( VERSION, p_module->psz_version ) )
443     {
444         free( p_module );
445         module_unload( handle );
446         return( -1 );
447     }
448
449     /* Check that we don't already have a module with this name */
450     for( p_othermodule = p_bank->first ;
451          p_othermodule != NULL ;
452          p_othermodule = p_othermodule->next )
453     {
454         if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
455         {
456             free( p_module );
457             module_unload( handle );
458             return( -1 );
459         }
460     }
461
462     /* Activate the module : fill the capability structure, etc. */
463     if( CallSymbol( p_module, "ActivateModule" ) != 0 )
464     {
465         /* We couldn't call ActivateModule() */
466         free( p_module );
467         module_unload( handle );
468         return( -1 );
469     }
470
471     /* We strdup() these entries so that they are still valid when the
472      * module is unloaded. */
473     p_module->psz_filename = strdup( p_module->psz_filename );
474     p_module->psz_name = strdup( p_module->psz_name );
475     p_module->psz_longname = strdup( p_module->psz_longname );
476     p_module->psz_version = strdup( p_module->psz_version );
477     if( p_module->psz_filename == NULL 
478             || p_module->psz_name == NULL
479             || p_module->psz_longname == NULL
480             || p_module->psz_version == NULL )
481     {
482         intf_ErrMsg( "module error: can't duplicate strings" );
483         free( p_module->psz_filename );
484         free( p_module->psz_name );
485         free( p_module->psz_longname );
486         free( p_module->psz_version );
487         free( p_module );
488         module_unload( handle );
489         return( -1 );
490     }
491
492     /* Everything worked fine ! The module is ready to be added to the list. */
493     p_module->i_usage = 0;
494     p_module->i_unused_delay = 0;
495
496     p_module->b_builtin = 0;
497
498     /* Link module into the linked list */
499     if( p_bank->first != NULL )
500     {
501         p_bank->first->prev = p_module;
502     }
503     p_module->next = p_bank->first;
504     p_module->prev = NULL;
505     p_bank->first = p_module;
506
507     /* Immediate message so that a slow module doesn't make the user wait */
508     intf_WarnMsgImm( 1, "module: dynamic module `%s', %s",
509                      p_module->psz_name, p_module->psz_longname );
510
511     return( 0 );
512 }
513
514 /*****************************************************************************
515  * HideModule: remove a module from memory but keep its structure.
516  *****************************************************************************
517  * This function can only be called if i_usage == 0. It will make a call
518  * to the module's inner DeactivateModule() symbol, and then unload it
519  * from memory. A call to module_Need() will automagically load it again.
520  *****************************************************************************/
521 static int HideModule( module_t * p_module )
522 {
523     if( p_module->b_builtin )
524     {
525         /* A built-in module should never be hidden. */
526         intf_ErrMsg( "module error: trying to hide built-in module `%s'",
527                      p_module->psz_name );
528         return( -1 );
529     }
530
531     if( p_module->i_usage >= 1 )
532     {
533         intf_ErrMsg( "module error: trying to hide module `%s' which is still"
534                      " in use", p_module->psz_name );
535         return( -1 );
536     }
537
538     if( p_module->i_usage <= -1 )
539     {
540         intf_ErrMsg( "module error: trying to hide module `%s' which is already"
541                      " hidden", p_module->psz_name );
542         return( -1 );
543     }
544
545     /* Deactivate the module : free the capability structure, etc. */
546     if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
547     {
548         /* We couldn't call DeactivateModule() -- looks nasty, but
549          * we can't do much about it. Just try to unload module anyway. */
550         module_unload( p_module->handle );
551         p_module->i_usage = -1;
552         return( -1 );
553     }
554
555     /* Everything worked fine, we can safely unload the module. */
556     module_unload( p_module->handle );
557     p_module->i_usage = -1;
558
559     return( 0 );
560 }
561
562 /*****************************************************************************
563  * FreeModule: delete a module and its structure.
564  *****************************************************************************
565  * This function can only be called if i_usage <= 0.
566  *****************************************************************************/
567 static int FreeModule( module_bank_t * p_bank, module_t * p_module )
568 {
569     /* If the module is not in use but is still in memory, we first have
570      * to hide it and remove it from memory before we can free the
571      * data structure. */
572     if( p_module->b_builtin )
573     {
574         if( p_module->i_usage != 0 )
575         {
576             intf_ErrMsg( "module error: trying to free builtin module `%s' with"
577                          " usage %i", p_module->psz_name, p_module->i_usage );
578             return( -1 );
579         }
580     }
581     else
582     {
583         if( p_module->i_usage >= 1 )
584         {
585             intf_ErrMsg( "module error: trying to free module `%s' which is"
586                          " still in use", p_module->psz_name );
587             return( -1 );
588         }
589
590         /* Two possibilities here: i_usage == -1 and the module is already
591          * unloaded, we can continue, or i_usage == 0, and we have to hide
592          * the module before going on. */
593         if( p_module->i_usage == 0 )
594         {
595             if( HideModule( p_module ) != 0 )
596             {
597                 return( -1 );
598             }
599         }
600     }
601
602     /* Unlink the module from the linked list. */
603     if( p_module == p_bank->first )
604     {
605         p_bank->first = p_module->next;
606     }
607
608     if( p_module->prev != NULL )
609     {
610         p_module->prev->next = p_module->next;
611     }
612
613     if( p_module->next != NULL )
614     {
615         p_module->next->prev = p_module->prev;
616     }
617
618     /* We free the structures that we strdup()ed in Allocate*Module(). */
619     free( p_module->psz_filename );
620     free( p_module->psz_name );
621     free( p_module->psz_longname );
622     free( p_module->psz_version );
623
624     free( p_module );
625
626     return( 0 );
627 }
628
629 /*****************************************************************************
630  * LockModule: increase the usage count of a module and load it if needed.
631  *****************************************************************************
632  * This function has to be called before a thread starts using a module. If
633  * the module is already loaded, we just increase its usage count. If it isn't
634  * loaded, we have to dynamically open it and initialize it.
635  * If you successfully call LockModule() at any moment, be careful to call
636  * UnlockModule() when you don't need it anymore.
637  *****************************************************************************/
638 static int LockModule( module_t * p_module )
639 {
640     if( p_module->i_usage >= 0 )
641     {
642         /* This module is already loaded and activated, we can return */
643         p_module->i_usage++;
644         return( 0 );
645     }
646
647     if( p_module->b_builtin )
648     {
649         /* A built-in module should always have a refcount >= 0 ! */
650         intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
651                      p_module->psz_name, p_module->i_usage );
652         return( -1 );
653     }
654
655     if( p_module->i_usage != -1 )
656     {
657         /* This shouldn't happen. Ever. We have serious problems here. */
658         intf_ErrMsg( "module error: dynamic module `%s' has refcount %i",
659                      p_module->psz_name, p_module->i_usage );
660         return( -1 );
661     }
662
663     /* i_usage == -1, which means that the module isn't in memory */
664     if( module_load( p_module->psz_filename, &p_module->handle ) )
665     {
666         /* The dynamic module couldn't be opened */
667         intf_ErrMsg( "module error: cannot open %s (%s)",
668                      p_module->psz_filename, module_error() );
669         return( -1 );
670     }
671
672     /* FIXME: what to do if the guy modified the plugin while it was
673      * unloaded ? It makes XMMS crash nastily, perhaps we should try
674      * to be a bit more clever here. */
675
676     /* Activate the module : fill the capability structure, etc. */
677     if( CallSymbol( p_module, "ActivateModule" ) != 0 )
678     {
679         /* We couldn't call ActivateModule() -- looks nasty, but
680          * we can't do much about it. Just try to unload module. */
681         module_unload( p_module->handle );
682         p_module->i_usage = -1;
683         return( -1 );
684     }
685
686     /* Everything worked fine ! The module is ready to be used */
687     p_module->i_usage = 1;
688
689     return( 0 );
690 }
691
692 /*****************************************************************************
693  * UnlockModule: decrease the usage count of a module.
694  *****************************************************************************
695  * We decrease the usage count of a module so that we know when a module
696  * becomes unused and can be hidden.
697  *****************************************************************************/
698 static int UnlockModule( module_t * p_module )
699 {
700     if( p_module->i_usage <= 0 )
701     {
702         /* This shouldn't happen. Ever. We have serious problems here. */
703         intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
704                      " which isn't even in use", p_module->psz_name );
705         return( -1 );
706     }
707
708     /* This module is still in use, we can return */
709     p_module->i_usage--;
710     p_module->i_unused_delay = 0;
711
712     return( 0 );
713 }
714
715 /*****************************************************************************
716  * CallSymbol: calls a module symbol.
717  *****************************************************************************
718  * This function calls a symbol given its name and a module structure. The
719  * symbol MUST refer to a function returning int and taking a module_t* as
720  * an argument.
721  *****************************************************************************/
722 static int CallSymbol( module_t * p_module, char * psz_name )
723 {
724     typedef int ( symbol_t ) ( module_t * p_module );
725     symbol_t * p_symbol;
726
727     /* Try to resolve the symbol */
728     p_symbol = module_getsymbol( p_module->handle, psz_name );
729
730     if( !p_symbol )
731     {
732         /* We couldn't load the symbol */
733         intf_WarnMsg( 1, "module warning: "
734                          "cannot find symbol %s in module %s (%s)",
735                          psz_name, p_module->psz_filename, module_error() );
736         return( -1 );
737     }
738
739     /* We can now try to call the symbol */
740     if( p_symbol( p_module ) != 0 )
741     {
742         /* With a well-written module we shouldn't have to print an
743          * additional error message here, but just make sure. */
744         intf_ErrMsg( "module error: failed calling symbol %s in module %s",
745                      psz_name, p_module->psz_filename );
746         return( -1 );
747     }
748
749     /* Everything worked fine, we can return */
750     return( 0 );
751 }
752