]> git.sesse.net Git - vlc/blob - src/misc/modules.c
2bf9b94fcfb3636b30d0f24fb82d7c289d7de9f1
[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_DbgMsg( "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].pf_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     intf_Msg( "module: locking module `%s'", p_bestmodule->psz_name );
312
313     /* Don't forget that the module is still locked if bestmodule != NULL */
314     return( p_bestmodule );
315 }
316
317 /*****************************************************************************
318  * module_Unneed: decrease the usage count of a module.
319  *****************************************************************************
320  * This function must be called by the thread that called module_Need, to
321  * decrease the reference count and allow for hiding of modules.
322  *****************************************************************************/
323 void module_Unneed( module_bank_t * p_bank, module_t * p_module )
324 {
325     /* We take the global lock */
326     vlc_mutex_lock( &p_bank->lock );
327
328     /* Just unlock the module - we can't do anything if it fails,
329      * so there is no need to check the return value. */
330     UnlockModule( p_module );
331
332     intf_Msg( "module: unlocking module `%s'", p_module->psz_name );
333
334     /* We release the global lock */
335     vlc_mutex_unlock( &p_bank->lock );
336
337     return;
338 }
339
340 /*****************************************************************************
341  * Following functions are local.
342  *****************************************************************************/
343
344 /*****************************************************************************
345  * AllocateDynModule: load a module into memory and initialize it.
346  *****************************************************************************
347  * This function loads a dynamically loadable module and allocates a structure
348  * for its information data. The module can then be handled by module_Need,
349  * module_Unneed and HideModule. It can be removed by FreeModule.
350  *****************************************************************************/
351 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename )
352 {
353     module_t * p_module, * p_othermodule;
354     module_handle_t handle;
355
356     /* Try to dynamically load the module. */
357     if( module_load( psz_filename, &handle ) )
358     {
359         /* The dynamic module couldn't be opened */
360         intf_DbgMsg( "module warning: cannot open %s (%s)",
361                      psz_filename, module_error() );
362         return( -1 );
363     }
364
365     /* Now that we have successfully loaded the module, we can
366      * allocate a structure for it */ 
367     p_module = malloc( sizeof( module_t ) );
368     if( p_module == NULL )
369     {
370         intf_ErrMsg( "module error: can't allocate p_module" );
371         module_unload( handle );
372         return( -1 );
373     }
374
375     /* We need to fill these since they may be needed by CallSymbol() */
376     p_module->psz_filename = psz_filename;
377     p_module->handle = handle;
378
379     /* Initialize the module : fill p_module->psz_name, etc. */
380     if( CallSymbol( p_module, "InitModule" ) != 0 )
381     {
382         /* We couldn't call InitModule() */
383         free( p_module );
384         module_unload( handle );
385         return( -1 );
386     }
387
388     /* Check that version numbers match */
389     if( strcmp( VERSION, p_module->psz_version ) )
390     {
391         free( p_module );
392         module_unload( handle );
393         return( -1 );
394     }
395
396     /* Check that we don't already have a module with this name */
397     for( p_othermodule = p_bank->first ;
398          p_othermodule != NULL ;
399          p_othermodule = p_othermodule->next )
400     {
401         if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
402         {
403             free( p_module );
404             module_unload( handle );
405             return( -1 );
406         }
407     }
408
409     /* Activate the module : fill the capability structure, etc. */
410     if( CallSymbol( p_module, "ActivateModule" ) != 0 )
411     {
412         /* We couldn't call ActivateModule() */
413         free( p_module );
414         module_unload( handle );
415         return( -1 );
416     }
417
418     /* We strdup() these entries so that they are still valid when the
419      * module is unloaded. */
420     p_module->psz_filename = strdup( p_module->psz_filename );
421     p_module->psz_name = strdup( p_module->psz_name );
422     p_module->psz_longname = strdup( p_module->psz_longname );
423     p_module->psz_version = strdup( p_module->psz_version );
424     if( p_module->psz_filename == NULL 
425             || p_module->psz_name == NULL
426             || p_module->psz_longname == NULL
427             || p_module->psz_version == NULL )
428     {
429         intf_ErrMsg( "module error: can't duplicate strings" );
430         free( p_module->psz_filename );
431         free( p_module->psz_name );
432         free( p_module->psz_longname );
433         free( p_module->psz_version );
434         free( p_module );
435         module_unload( handle );
436         return( -1 );
437     }
438
439     /* Everything worked fine ! The module is ready to be added to the list. */
440     p_module->i_usage = 0;
441     p_module->i_unused_delay = 0;
442
443     p_module->b_builtin = 0;
444
445     /* Link module into the linked list */
446     if( p_bank->first != NULL )
447     {
448         p_bank->first->prev = p_module;
449     }
450     p_module->next = p_bank->first;
451     p_module->prev = NULL;
452     p_bank->first = p_module;
453
454     intf_Msg( "module: dynamic module `%s', %s",
455               p_module->psz_name, p_module->psz_longname );
456
457     return( 0 );
458 }
459
460 /*****************************************************************************
461  * HideModule: remove a module from memory but keep its structure.
462  *****************************************************************************
463  * This function can only be called if i_usage == 0. It will make a call
464  * to the module's inner DeactivateModule() symbol, and then unload it
465  * from memory. A call to module_Need() will automagically load it again.
466  *****************************************************************************/
467 static int HideModule( module_t * p_module )
468 {
469     if( p_module->b_builtin )
470     {
471         /* A built-in module should never be hidden. */
472         intf_ErrMsg( "module error: trying to hide built-in module `%s'",
473                      p_module->psz_name );
474         return( -1 );
475     }
476
477     if( p_module->i_usage >= 1 )
478     {
479         intf_ErrMsg( "module error: trying to hide module `%s' which is still"
480                      " in use", p_module->psz_name );
481         return( -1 );
482     }
483
484     if( p_module->i_usage <= -1 )
485     {
486         intf_ErrMsg( "module error: trying to hide module `%s' which is already"
487                      " hidden", p_module->psz_name );
488         return( -1 );
489     }
490
491     /* Deactivate the module : free the capability structure, etc. */
492     if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
493     {
494         /* We couldn't call DeactivateModule() -- looks nasty, but
495          * we can't do much about it. Just try to unload module anyway. */
496         module_unload( p_module->handle );
497         p_module->i_usage = -1;
498         return( -1 );
499     }
500
501     /* Everything worked fine, we can safely unload the module. */
502     module_unload( p_module->handle );
503     p_module->i_usage = -1;
504
505     return( 0 );
506 }
507
508 /*****************************************************************************
509  * FreeModule: delete a module and its structure.
510  *****************************************************************************
511  * This function can only be called if i_usage <= 0.
512  *****************************************************************************/
513 static int FreeModule( module_bank_t * p_bank, module_t * p_module )
514 {
515     /* If the module is not in use but is still in memory, we first have
516      * to hide it and remove it from memory before we can free the
517      * data structure. */
518     if( p_module->b_builtin )
519     {
520         if( p_module->i_usage != 0 )
521         {
522             intf_ErrMsg( "module error: trying to free builtin module `%s' with"
523                          " usage %i", p_module->psz_name, p_module->i_usage );
524             return( -1 );
525         }
526     }
527     else
528     {
529         if( p_module->i_usage >= 1 )
530         {
531             intf_ErrMsg( "module error: trying to free module `%s' which is"
532                          " still in use", p_module->psz_name );
533             return( -1 );
534         }
535
536         /* Two possibilities here: i_usage == -1 and the module is already
537          * unloaded, we can continue, or i_usage == 0, and we have to hide
538          * the module before going on. */
539         if( p_module->i_usage == 0 )
540         {
541             if( HideModule( p_module ) != 0 )
542             {
543                 return( -1 );
544             }
545         }
546     }
547
548     /* Unlink the module from the linked list. */
549     if( p_module == p_bank->first )
550     {
551         p_bank->first = p_module->next;
552     }
553
554     if( p_module->prev != NULL )
555     {
556         p_module->prev->next = p_module->next;
557     }
558
559     if( p_module->next != NULL )
560     {
561         p_module->next->prev = p_module->prev;
562     }
563
564     /* We free the structures that we strdup()ed in Allocate*Module(). */
565     free( p_module->psz_filename );
566     free( p_module->psz_name );
567     free( p_module->psz_longname );
568     free( p_module->psz_version );
569
570     free( p_module );
571
572     return( 0 );
573 }
574
575 /*****************************************************************************
576  * LockModule: increase the usage count of a module and load it if needed.
577  *****************************************************************************
578  * This function has to be called before a thread starts using a module. If
579  * the module is already loaded, we just increase its usage count. If it isn't
580  * loaded, we have to dynamically open it and initialize it.
581  * If you successfully call LockModule() at any moment, be careful to call
582  * UnlockModule() when you don't need it anymore.
583  *****************************************************************************/
584 static int LockModule( module_t * p_module )
585 {
586     if( p_module->i_usage >= 0 )
587     {
588         /* This module is already loaded and activated, we can return */
589         p_module->i_usage++;
590         return( 0 );
591     }
592
593     if( p_module->b_builtin )
594     {
595         /* A built-in module should always have a refcount >= 0 ! */
596         intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
597                      p_module->psz_name, p_module->i_usage );
598         return( -1 );
599     }
600
601     if( p_module->i_usage != -1 )
602     {
603         /* This shouldn't happen. Ever. We have serious problems here. */
604         intf_ErrMsg( "module error: dynamic module `%s' has refcount %i",
605                      p_module->psz_name, p_module->i_usage );
606         return( -1 );
607     }
608
609     /* i_usage == -1, which means that the module isn't in memory */
610     if( module_load( p_module->psz_filename, &p_module->handle ) )
611     {
612         /* The dynamic module couldn't be opened */
613         intf_ErrMsg( "module error: cannot open %s (%s)",
614                      p_module->psz_filename, module_error() );
615         return( -1 );
616     }
617
618     /* FIXME: what to do if the guy modified the plugin while it was
619      * unloaded ? It makes XMMS crash nastily, perhaps we should try
620      * to be a bit more clever here. */
621
622     /* Activate the module : fill the capability structure, etc. */
623     if( CallSymbol( p_module, "ActivateModule" ) != 0 )
624     {
625         /* We couldn't call ActivateModule() -- looks nasty, but
626          * we can't do much about it. Just try to unload module. */
627         module_unload( p_module->handle );
628         p_module->i_usage = -1;
629         return( -1 );
630     }
631
632     /* Everything worked fine ! The module is ready to be used */
633     p_module->i_usage = 1;
634
635     return( 0 );
636 }
637
638 /*****************************************************************************
639  * UnlockModule: decrease the usage count of a module.
640  *****************************************************************************
641  * We decrease the usage count of a module so that we know when a module
642  * becomes unused and can be hidden.
643  *****************************************************************************/
644 static int UnlockModule( module_t * p_module )
645 {
646     if( p_module->i_usage <= 0 )
647     {
648         /* This shouldn't happen. Ever. We have serious problems here. */
649         intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
650                      " which isn't even in use", p_module->psz_name );
651         return( -1 );
652     }
653
654     /* This module is still in use, we can return */
655     p_module->i_usage--;
656     p_module->i_unused_delay = 0;
657
658     return( 0 );
659 }
660
661 /*****************************************************************************
662  * CallSymbol: calls a module symbol.
663  *****************************************************************************
664  * This function calls a symbol given its name and a module structure. The
665  * symbol MUST refer to a function returning int and taking a module_t* as
666  * an argument.
667  *****************************************************************************/
668 static int CallSymbol( module_t * p_module, char * psz_name )
669 {
670     typedef int ( symbol_t ) ( module_t * p_module );
671     symbol_t * p_symbol;
672
673     /* Try to resolve the symbol */
674     p_symbol = module_getsymbol( p_module->handle, psz_name );
675
676     if( !p_symbol )
677     {
678         /* We couldn't load the symbol */
679         intf_DbgMsg( "module warning: cannot find symbol %s in module %s (%s)",
680                      psz_name, p_module->psz_filename, module_error() );
681         return( -1 );
682     }
683
684     /* We can now try to call the symbol */
685     if( p_symbol( p_module ) != 0 )
686     {
687         /* With a well-written module we shouldn't have to print an
688          * additional error message here, but just make sure. */
689         intf_ErrMsg( "module error: failed calling symbol %s in module %s",
690                      psz_name, p_module->psz_filename );
691         return( -1 );
692     }
693
694     /* Everything worked fine, we can return */
695     return( 0 );
696 }
697