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