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