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