]> git.sesse.net Git - vlc/blob - src/misc/modules.c
- Added vlc_mutex_destroy and vlc_cond_destroy function, for pthreads.
[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 CallSymbol( module_t * p_module, char * psz_name );
59
60 /*****************************************************************************
61  * module_CreateBank: create the module bank.
62  *****************************************************************************
63  * This function creates a module bank structure.
64  *****************************************************************************/
65 module_bank_t * module_CreateBank( void )
66 {
67     module_bank_t * p_bank;
68
69     p_bank = malloc( sizeof( module_bank_t ) );
70
71     return( p_bank );
72 }
73
74 /*****************************************************************************
75  * module_InitBank: create the module bank.
76  *****************************************************************************
77  * This function creates a module bank structure and fills it with the
78  * built-in modules, as well as all the dynamic modules it can find.
79  *****************************************************************************/
80 void module_InitBank( module_bank_t * p_bank )
81 {
82     static char * path[] = { ".", "lib", PLUGIN_PATH, NULL } ;
83
84     char **         ppsz_path = path;
85     char *          psz_file;
86 #ifdef SYS_BEOS
87     char *          psz_program_path = beos_GetProgramPath();
88     int             i_programlen = strlen( psz_program_path );
89 #endif
90     DIR *           dir;
91     struct dirent * file;
92
93     p_bank->first = NULL;
94     vlc_mutex_init( &p_bank->lock );
95
96     intf_Msg( "module: module bank initialized" );
97
98     for( ; *ppsz_path != NULL ; ppsz_path++ )
99     {
100         if( (dir = opendir( *ppsz_path )) )
101         {
102             /* Store strlen(*ppsz_path) for later use. */
103             int i_dirlen = strlen( *ppsz_path );
104
105             /* Parse the directory and try to load all files it contains. */
106             while( (file = readdir( dir )) )
107             {
108                 int i_filelen = strlen( file->d_name );
109
110                 /* We only load files ending with ".so" */
111                 if( i_filelen > 3
112                         && !strcmp( file->d_name + i_filelen - 3, ".so" ) )
113                 {
114 #ifdef SYS_BEOS
115                     /* Under BeOS, we need to add beos_GetProgramPath() to
116                      * access files under the current directory */
117                     if( memcmp( file->d_name, "/", 1 ) )
118                     {
119                         psz_file = malloc( i_programlen + i_dirlen
120                                                + i_filelen + 3 );
121                         if( psz_file == NULL )
122                         {
123                             continue;
124                         }
125                         sprintf( psz_file, "%s/%s/%s", psz_programlen,
126                                  *ppsz_path, file->d_name );
127                     }
128                     else
129 #endif
130                     {
131                         psz_file = malloc( i_dirlen + i_filelen + 2 );
132                         if( psz_file == NULL )
133                         {
134                             continue;
135                         }
136                         sprintf( psz_file, "%s/%s", *ppsz_path, file->d_name );
137                     }
138                     /* We created a nice filename -- now we just try to load
139                      * it as a dynamic module. */
140                     AllocateDynModule( p_bank, psz_file );
141
142                     /* We don't care if the allocation succeeded */
143                     free( psz_file );
144                 }
145             }
146         }
147     }
148
149     return;
150 }
151
152 /*****************************************************************************
153  * module_DestroyBank: destroy the module bank.
154  *****************************************************************************
155  * This function unloads all unused dynamic modules and removes the module
156  * bank in case of success.
157  *****************************************************************************/
158 void module_DestroyBank( module_bank_t * p_bank )
159 {
160     module_t * p_next;
161
162     while( p_bank->first != NULL )
163     {
164         if( FreeModule( p_bank, p_bank->first ) )
165         {
166             /* Module deletion failed */
167             intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
168                          p_bank->first->psz_name );
169
170             /* We just free the module by hand. Niahahahahaha. */
171             p_next = p_bank->first->next;
172             free(p_bank->first);
173             p_bank->first = p_next;
174         }
175     }
176
177     /* Destory the lock */
178     vlc_mutex_destroy( &p_bank->lock );
179     
180     /* We can free the module bank */
181     free( p_bank );
182
183     return;
184 }
185
186 /*****************************************************************************
187  * module_ResetBank: reset the module bank.
188  *****************************************************************************
189  * This function resets the module bank by unloading all unused dynamic
190  * modules.
191  *****************************************************************************/
192 void module_ResetBank( module_bank_t * p_bank )
193 {
194     intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
195     return;
196 }
197
198 /*****************************************************************************
199  * module_ManageBank: manage the module bank.
200  *****************************************************************************
201  * This function parses the module bank and hides modules that have been
202  * unused for a while.
203  *****************************************************************************/
204 void module_ManageBank( module_bank_t * p_bank )
205 {
206     module_t * p_module;
207
208     /* We take the global lock */
209     vlc_mutex_lock( &p_bank->lock );
210
211     /* Parse the module list to see if any modules need to be unloaded */
212     for( p_module = p_bank->first ;
213          p_module != NULL ;
214          p_module = p_module->next )
215     {
216         /* If the module is unused and if it is a dynamic module... */
217         if( p_module->i_usage == 0 && !p_module->b_builtin )
218         {
219             if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
220             {
221                 p_module->i_unused_delay++;
222             }
223             else
224             {
225                 intf_Msg( "module: hiding unused module `%s'",
226                           p_module->psz_name );
227                 HideModule( p_module );
228             }
229         }
230     }
231
232     /* We release the global lock */
233     vlc_mutex_unlock( &p_bank->lock );
234
235     return;
236 }
237
238 /*****************************************************************************
239  * module_Need: increase the usage count of a module and load it if needed.
240  *****************************************************************************
241  * This function has to be called before a thread starts using a module. If
242  * the module is already loaded, we just increase its usage count. If it isn't
243  * loaded, we have to dynamically open it and initialize it.
244  * If you successfully call module_Need() at any moment, be careful to call
245  * module_Unneed() when you don't need it anymore.
246  *****************************************************************************/
247 int module_Need( module_t * p_module )
248 {
249     if( p_module->i_usage >= 0 )
250     {
251         /* This module is already loaded and activated, we can return */
252         p_module->i_usage++;
253         return( 0 );
254     }
255
256     if( p_module->b_builtin )
257     {
258         /* A built-in module should always have a refcount >= 0 ! */
259         intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
260                      p_module->psz_name, p_module->i_usage );
261         return( -1 );
262     }
263
264     if( p_module->i_usage != -1 )
265     {
266         /* This shouldn't happen. Ever. We have serious problems here. */
267         intf_ErrMsg( "module error: dynamic module `%s' has refcount %i",
268                      p_module->psz_name, p_module->i_usage );
269         return( -1 );
270     }
271
272     /* i_usage == -1, which means that the module isn't in memory */
273     if( ! module_load( p_module->psz_filename, &p_module->handle ) )
274     {
275         /* The dynamic module couldn't be opened */
276         intf_ErrMsg( "module error: cannot open %s (%s)",
277                      p_module->psz_filename, module_error() );
278         return( -1 );
279     }
280
281     if( CallSymbol( p_module, "ActivateModule" ) != 0 )
282     {
283         /* We couldn't call ActivateModule() -- looks nasty, but
284          * we can't do much about it. Just try to unload module. */
285         module_unload( p_module->handle );
286         p_module->i_usage = -1;
287         return( -1 );
288     }
289
290     /* Everything worked fine ! The module is ready to be used */
291     p_module->i_usage = 1;
292
293     return( 0 );
294 }
295
296 /*****************************************************************************
297  * module_Unneed: decrease the usage count of a module.
298  *****************************************************************************
299  * This function has to be called before a thread starts using a module. If
300  * the module is already loaded, we just increase its usage count. If it isn't
301  * loaded, we have to dynamically open it and initialize it.
302  * If you successfully call module_Need() at any moment, be careful to call
303  * module_Unneed() when you don't need it anymore.
304  *****************************************************************************/
305 int module_Unneed( module_t * p_module )
306 {
307     if( p_module->i_usage <= 0 )
308     {
309         /* This shouldn't happen. Ever. We have serious problems here. */
310         intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
311                      " which isn't even in use", p_module->psz_name );
312         return( -1 );
313     }
314
315     /* This module is still in use, we can return */
316     p_module->i_usage--;
317     p_module->i_unused_delay = 0;
318
319     return( 0 );
320 }
321
322 /*****************************************************************************
323  * Following functions are local.
324  *****************************************************************************/
325
326 /*****************************************************************************
327  * AllocateDynModule: load a module into memory and initialize it.
328  *****************************************************************************
329  * This function loads a dynamically loadable module and allocates a structure
330  * for its information data. The module can then be handled by module_Need,
331  * module_Unneed and HideModule. It can be removed by FreeModule.
332  *****************************************************************************/
333 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename )
334 {
335     module_t * p_module;
336     module_handle_t handle;
337
338     /* Try to dynamically load the module. */
339     if( ! module_load( psz_filename, &handle ) )
340     {
341         /* The dynamic module couldn't be opened */
342         intf_DbgMsg( "module error: cannot open %s (%s)",
343                      psz_filename, module_error() );
344         return( -1 );
345     }
346
347     /* Now that we have successfully loaded the module, we can
348      * allocate a structure for it */ 
349     p_module = malloc( sizeof( module_t ) );
350     if( p_module == NULL )
351     {
352         intf_ErrMsg( "module error: can't allocate p_module" );
353         module_unload( handle );
354         return( -1 );
355     }
356
357     /* We need to fill these since they may be needed by CallSymbol() */
358     p_module->psz_filename = psz_filename;
359     p_module->handle = handle;
360
361     if( CallSymbol( p_module, "InitModule" ) != 0 )
362     {
363         /* We couldn't call InitModule() */
364         free( p_module );
365         module_unload( handle );
366         return( -1 );
367     }
368
369     if( CallSymbol( p_module, "ActivateModule" ) != 0 )
370     {
371         /* We couldn't call ActivateModule() */
372         free( p_module );
373         module_unload( handle );
374         return( -1 );
375     }
376
377     /* We strdup() these entries so that they are still valid when the
378      * module is unloaded. */
379     p_module->psz_filename = strdup( p_module->psz_filename );
380     p_module->psz_name = strdup( p_module->psz_name );
381     p_module->psz_longname = strdup( p_module->psz_longname );
382     p_module->psz_version = strdup( p_module->psz_version );
383     if( p_module->psz_filename == NULL 
384             || p_module->psz_name == NULL
385             || p_module->psz_longname == NULL
386             || p_module->psz_version == NULL )
387     {
388         intf_ErrMsg( "module error: can't duplicate strings" );
389         free( p_module->psz_filename );
390         free( p_module->psz_name );
391         free( p_module->psz_longname );
392         free( p_module->psz_version );
393         free( p_module );
394         module_unload( handle );
395         return( -1 );
396     }
397
398     /* Everything worked fine ! The module is ready to be added to the list. */
399     p_module->i_usage = 0;
400     p_module->i_unused_delay = 0;
401
402     p_module->b_builtin = 0;
403
404     /* Link module into the linked list */
405     if( p_bank->first != NULL )
406     {
407         p_bank->first->prev = p_module;
408     }
409     p_module->next = p_bank->first;
410     p_module->prev = NULL;
411     p_bank->first = p_module;
412
413     intf_Msg( "module: dynamic module `%s', %s",
414               p_module->psz_name, p_module->psz_longname );
415
416     return( 0 );
417 }
418
419 /*****************************************************************************
420  * HideModule: remove a module from memory but keep its structure.
421  *****************************************************************************
422  * This function can only be called if i_usage == 0. It will make a call
423  * to the module's inner DeactivateModule() symbol, and then unload it
424  * from memory. A call to module_Need() will automagically load it again.
425  *****************************************************************************/
426 static int HideModule( module_t * p_module )
427 {
428     if( p_module->b_builtin )
429     {
430         /* A built-in module should never be hidden. */
431         intf_ErrMsg( "module error: trying to hide built-in module `%s'",
432                      p_module->psz_name );
433         return( -1 );
434     }
435
436     if( p_module->i_usage >= 1 )
437     {
438         intf_ErrMsg( "module error: trying to hide module `%s' which is still"
439                      " in use", p_module->psz_name );
440         return( -1 );
441     }
442
443     if( p_module->i_usage <= -1 )
444     {
445         intf_ErrMsg( "module error: trying to hide module `%s' which is already"
446                      " hidden", p_module->psz_name );
447         return( -1 );
448     }
449
450     if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
451     {
452         /* We couldn't call DeactivateModule() -- looks nasty, but
453          * we can't do much about it. Just try to unload module anyway. */
454         module_unload( p_module->handle );
455         p_module->i_usage = -1;
456         return( -1 );
457     }
458
459     /* Everything worked fine, we can safely unload the module. */
460     module_unload( p_module->handle );
461     p_module->i_usage = -1;
462
463     return( 0 );
464 }
465
466 /*****************************************************************************
467  * FreeModule: delete a module and its structure.
468  *****************************************************************************
469  * This function can only be called if i_usage <= 0.
470  *****************************************************************************/
471 static int FreeModule( module_bank_t * p_bank, module_t * p_module )
472 {
473     /* If the module is not in use but is still in memory, we first have
474      * to hide it and remove it from memory before we can free the
475      * data structure. */
476     if( p_module->b_builtin )
477     {
478         if( p_module->i_usage != 0 )
479         {
480             intf_ErrMsg( "module error: trying to free builtin module `%s' with"
481                          " usage %i", p_module->psz_name, p_module->i_usage );
482             return( -1 );
483         }
484     }
485     else
486     {
487         if( p_module->i_usage >= 1 )
488         {
489             intf_ErrMsg( "module error: trying to free module `%s' which is"
490                          " still in use", p_module->psz_name );
491             return( -1 );
492         }
493
494         /* Two possibilities here: i_usage == -1 and the module is already
495          * unloaded, we can continue, or i_usage == 0, and we have to hide
496          * the module before going on. */
497         if( p_module->i_usage == 0 )
498         {
499             if( HideModule( p_module ) != 0 )
500             {
501                 return( -1 );
502             }
503         }
504     }
505
506     /* Unlink the module from the linked list. */
507     if( p_module == p_bank->first )
508     {
509         p_bank->first = p_module->next;
510     }
511
512     if( p_module->prev != NULL )
513     {
514         p_module->prev->next = p_module->next;
515     }
516
517     if( p_module->next != NULL )
518     {
519         p_module->next->prev = p_module->prev;
520     }
521
522     /* We free the structures that we strdup()ed in Allocate*Module(). */
523     free( p_module->psz_filename );
524     free( p_module->psz_name );
525     free( p_module->psz_longname );
526     free( p_module->psz_version );
527
528     free( p_module );
529
530     return( 0 );
531 }
532
533 /*****************************************************************************
534  * CallSymbol: calls a module symbol.
535  *****************************************************************************
536  * This function calls a symbol given its name and a module structure. The
537  * symbol MUST refer to a function returning int and taking a module_t* as
538  * an argument.
539  *****************************************************************************/
540 static int CallSymbol( module_t * p_module, char * psz_name )
541 {
542     typedef int ( symbol_t ) ( module_t * p_module );
543     symbol_t * p_symbol;
544
545     /* Try to resolve the symbol */
546     p_symbol = module_getsymbol( p_module->handle, psz_name );
547
548     if( !p_symbol )
549     {
550         /* We couldn't load the symbol */
551         intf_DbgMsg( "module warning: cannot find symbol %s in module %s (%s)",
552                      psz_name, p_module->psz_filename, module_error() );
553         return( -1 );
554     }
555
556     /* We can now try to call the symbol */
557     if( p_symbol( p_module ) != 0 )
558     {
559         /* With a well-written module we shouldn't have to print an
560          * additional error message here, but just make sure. */
561         intf_ErrMsg( "module error: failed calling symbol %s in module %s",
562                      psz_name, p_module->psz_filename );
563         return( -1 );
564     }
565
566     /* Everything worked fine, we can return */
567     return( 0 );
568 }
569