]> git.sesse.net Git - vlc/blob - src/misc/modules.c
. should compile & run on Solaris with ./configure --disable-dsp
[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     /* We can free the module bank */
178     free( p_bank );
179
180     return;
181 }
182
183 /*****************************************************************************
184  * module_ResetBank: reset the module bank.
185  *****************************************************************************
186  * This function resets the module bank by unloading all unused dynamic
187  * modules.
188  *****************************************************************************/
189 void module_ResetBank( module_bank_t * p_bank )
190 {
191     intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
192     return;
193 }
194
195 /*****************************************************************************
196  * module_ManageBank: manage the module bank.
197  *****************************************************************************
198  * This function parses the module bank and hides modules that have been
199  * unused for a while.
200  *****************************************************************************/
201 void module_ManageBank( module_bank_t * p_bank )
202 {
203     module_t * p_module;
204
205     /* We take the global lock */
206     vlc_mutex_lock( &p_bank->lock );
207
208     /* Parse the module list to see if any modules need to be unloaded */
209     for( p_module = p_bank->first ;
210          p_module != NULL ;
211          p_module = p_module->next )
212     {
213         /* If the module is unused and if it is a dynamic module... */
214         if( p_module->i_usage == 0 && !p_module->b_builtin )
215         {
216             if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
217             {
218                 p_module->i_unused_delay++;
219             }
220             else
221             {
222                 intf_Msg( "module: hiding unused module `%s'",
223                           p_module->psz_name );
224                 HideModule( p_module );
225             }
226         }
227     }
228
229     /* We release the global lock */
230     vlc_mutex_unlock( &p_bank->lock );
231
232     return;
233 }
234
235 /*****************************************************************************
236  * module_Need: increase the usage count of a module and load it if needed.
237  *****************************************************************************
238  * This function has to be called before a thread starts using a module. If
239  * the module is already loaded, we just increase its usage count. If it isn't
240  * loaded, we have to dynamically open it and initialize it.
241  * If you successfully call module_Need() at any moment, be careful to call
242  * module_Unneed() when you don't need it anymore.
243  *****************************************************************************/
244 int module_Need( module_t * p_module )
245 {
246     if( p_module->i_usage >= 0 )
247     {
248         /* This module is already loaded and activated, we can return */
249         p_module->i_usage++;
250         return( 0 );
251     }
252
253     if( p_module->b_builtin )
254     {
255         /* A built-in module should always have a refcount >= 0 ! */
256         intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
257                      p_module->psz_name, p_module->i_usage );
258         return( -1 );
259     }
260
261     if( p_module->i_usage != -1 )
262     {
263         /* This shouldn't happen. Ever. We have serious problems here. */
264         intf_ErrMsg( "module error: dynamic module `%s' has refcount %i",
265                      p_module->psz_name, p_module->i_usage );
266         return( -1 );
267     }
268
269     /* i_usage == -1, which means that the module isn't in memory */
270     if( ! module_load( p_module->psz_filename, &p_module->handle ) )
271     {
272         /* The dynamic module couldn't be opened */
273         intf_ErrMsg( "module error: cannot open %s (%s)",
274                      p_module->psz_filename, module_error() );
275         return( -1 );
276     }
277
278     if( CallSymbol( p_module, "ActivateModule" ) != 0 )
279     {
280         /* We couldn't call ActivateModule() -- looks nasty, but
281          * we can't do much about it. Just try to unload module. */
282         module_unload( p_module->handle );
283         p_module->i_usage = -1;
284         return( -1 );
285     }
286
287     /* Everything worked fine ! The module is ready to be used */
288     p_module->i_usage = 1;
289
290     return( 0 );
291 }
292
293 /*****************************************************************************
294  * module_Unneed: decrease the usage count of a module.
295  *****************************************************************************
296  * This function has to be called before a thread starts using a module. If
297  * the module is already loaded, we just increase its usage count. If it isn't
298  * loaded, we have to dynamically open it and initialize it.
299  * If you successfully call module_Need() at any moment, be careful to call
300  * module_Unneed() when you don't need it anymore.
301  *****************************************************************************/
302 int module_Unneed( module_t * p_module )
303 {
304     if( p_module->i_usage <= 0 )
305     {
306         /* This shouldn't happen. Ever. We have serious problems here. */
307         intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
308                      " which isn't even in use", p_module->psz_name );
309         return( -1 );
310     }
311
312     /* This module is still in use, we can return */
313     p_module->i_usage--;
314     p_module->i_unused_delay = 0;
315
316     return( 0 );
317 }
318
319 /*****************************************************************************
320  * Following functions are local.
321  *****************************************************************************/
322
323 /*****************************************************************************
324  * AllocateDynModule: load a module into memory and initialize it.
325  *****************************************************************************
326  * This function loads a dynamically loadable module and allocates a structure
327  * for its information data. The module can then be handled by module_Need,
328  * module_Unneed and HideModule. It can be removed by FreeModule.
329  *****************************************************************************/
330 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename )
331 {
332     module_t * p_module;
333     module_handle_t handle;
334
335     /* Try to dynamically load the module. */
336     if( ! module_load( psz_filename, &handle ) )
337     {
338         /* The dynamic module couldn't be opened */
339         intf_DbgMsg( "module error: cannot open %s (%s)",
340                      psz_filename, module_error() );
341         return( -1 );
342     }
343
344     /* Now that we have successfully loaded the module, we can
345      * allocate a structure for it */ 
346     p_module = malloc( sizeof( module_t ) );
347     if( p_module == NULL )
348     {
349         intf_ErrMsg( "module error: can't allocate p_module" );
350         module_unload( handle );
351         return( -1 );
352     }
353
354     /* We need to fill these since they may be needed by CallSymbol() */
355     p_module->psz_filename = psz_filename;
356     p_module->handle = handle;
357
358     if( CallSymbol( p_module, "InitModule" ) != 0 )
359     {
360         /* We couldn't call InitModule() */
361         free( p_module );
362         module_unload( handle );
363         return( -1 );
364     }
365
366     if( CallSymbol( p_module, "ActivateModule" ) != 0 )
367     {
368         /* We couldn't call ActivateModule() */
369         free( p_module );
370         module_unload( handle );
371         return( -1 );
372     }
373
374     /* We strdup() these entries so that they are still valid when the
375      * module is unloaded. */
376     p_module->psz_filename = strdup( p_module->psz_filename );
377     p_module->psz_name = strdup( p_module->psz_name );
378     p_module->psz_longname = strdup( p_module->psz_longname );
379     p_module->psz_version = strdup( p_module->psz_version );
380     if( p_module->psz_filename == NULL 
381             || p_module->psz_name == NULL
382             || p_module->psz_longname == NULL
383             || p_module->psz_version == NULL )
384     {
385         intf_ErrMsg( "module error: can't duplicate strings" );
386         free( p_module->psz_filename );
387         free( p_module->psz_name );
388         free( p_module->psz_longname );
389         free( p_module->psz_version );
390         free( p_module );
391         module_unload( handle );
392         return( -1 );
393     }
394
395     /* Everything worked fine ! The module is ready to be added to the list. */
396     p_module->i_usage = 0;
397     p_module->i_unused_delay = 0;
398
399     p_module->b_builtin = 0;
400
401     /* Link module into the linked list */
402     if( p_bank->first != NULL )
403     {
404         p_bank->first->prev = p_module;
405     }
406     p_module->next = p_bank->first;
407     p_module->prev = NULL;
408     p_bank->first = p_module;
409
410     intf_Msg( "module: dynamic module `%s', %s",
411               p_module->psz_name, p_module->psz_longname );
412
413     return( 0 );
414 }
415
416 /*****************************************************************************
417  * HideModule: remove a module from memory but keep its structure.
418  *****************************************************************************
419  * This function can only be called if i_usage == 0. It will make a call
420  * to the module's inner DeactivateModule() symbol, and then unload it
421  * from memory. A call to module_Need() will automagically load it again.
422  *****************************************************************************/
423 static int HideModule( module_t * p_module )
424 {
425     if( p_module->b_builtin )
426     {
427         /* A built-in module should never be hidden. */
428         intf_ErrMsg( "module error: trying to hide built-in module `%s'",
429                      p_module->psz_name );
430         return( -1 );
431     }
432
433     if( p_module->i_usage >= 1 )
434     {
435         intf_ErrMsg( "module error: trying to hide module `%s' which is still"
436                      " in use", p_module->psz_name );
437         return( -1 );
438     }
439
440     if( p_module->i_usage <= -1 )
441     {
442         intf_ErrMsg( "module error: trying to hide module `%s' which is already"
443                      " hidden", p_module->psz_name );
444         return( -1 );
445     }
446
447     if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
448     {
449         /* We couldn't call DeactivateModule() -- looks nasty, but
450          * we can't do much about it. Just try to unload module anyway. */
451         module_unload( p_module->handle );
452         p_module->i_usage = -1;
453         return( -1 );
454     }
455
456     /* Everything worked fine, we can safely unload the module. */
457     module_unload( p_module->handle );
458     p_module->i_usage = -1;
459
460     return( 0 );
461 }
462
463 /*****************************************************************************
464  * FreeModule: delete a module and its structure.
465  *****************************************************************************
466  * This function can only be called if i_usage <= 0.
467  *****************************************************************************/
468 static int FreeModule( module_bank_t * p_bank, module_t * p_module )
469 {
470     /* If the module is not in use but is still in memory, we first have
471      * to hide it and remove it from memory before we can free the
472      * data structure. */
473     if( p_module->b_builtin )
474     {
475         if( p_module->i_usage != 0 )
476         {
477             intf_ErrMsg( "module error: trying to free builtin module `%s' with"
478                          " usage %i", p_module->psz_name, p_module->i_usage );
479             return( -1 );
480         }
481     }
482     else
483     {
484         if( p_module->i_usage >= 1 )
485         {
486             intf_ErrMsg( "module error: trying to free module `%s' which is"
487                          " still in use", p_module->psz_name );
488             return( -1 );
489         }
490
491         /* Two possibilities here: i_usage == -1 and the module is already
492          * unloaded, we can continue, or i_usage == 0, and we have to hide
493          * the module before going on. */
494         if( p_module->i_usage == 0 )
495         {
496             if( HideModule( p_module ) != 0 )
497             {
498                 return( -1 );
499             }
500         }
501     }
502
503     /* Unlink the module from the linked list. */
504     if( p_module == p_bank->first )
505     {
506         p_bank->first = p_module->next;
507     }
508
509     if( p_module->prev != NULL )
510     {
511         p_module->prev->next = p_module->next;
512     }
513
514     if( p_module->next != NULL )
515     {
516         p_module->next->prev = p_module->prev;
517     }
518
519     /* We free the structures that we strdup()ed in Allocate*Module(). */
520     free( p_module->psz_filename );
521     free( p_module->psz_name );
522     free( p_module->psz_longname );
523     free( p_module->psz_version );
524
525     free( p_module );
526
527     return( 0 );
528 }
529
530 /*****************************************************************************
531  * CallSymbol: calls a module symbol.
532  *****************************************************************************
533  * This function calls a symbol given its name and a module structure. The
534  * symbol MUST refer to a function returning int and taking a module_t* as
535  * an argument.
536  *****************************************************************************/
537 static int CallSymbol( module_t * p_module, char * psz_name )
538 {
539     typedef int ( symbol_t ) ( module_t * p_module );
540     symbol_t * p_symbol;
541
542     /* Try to resolve the symbol */
543     p_symbol = module_getsymbol( p_module->handle, psz_name );
544
545     if( !p_symbol )
546     {
547         /* We couldn't load the symbol */
548         intf_DbgMsg( "module warning: cannot find symbol %s in module %s (%s)",
549                      psz_name, p_module->psz_filename, module_error() );
550         return( -1 );
551     }
552
553     /* We can now try to call the symbol */
554     if( p_symbol( p_module ) != 0 )
555     {
556         /* With a well-written module we shouldn't have to print an
557          * additional error message here, but just make sure. */
558         intf_ErrMsg( "module error: failed calling symbol %s in module %s",
559                      psz_name, p_module->psz_filename );
560         return( -1 );
561     }
562
563     /* Everything worked fine, we can return */
564     return( 0 );
565 }
566