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