1 /*****************************************************************************
2 * modules.c : Built-in and dynamic modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
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.
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.
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 *****************************************************************************/
27 #include <stdlib.h> /* free(), strtol() */
28 #include <stdio.h> /* sprintf() */
29 #include <string.h> /* strdup() */
32 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
33 #include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
35 #elif defined(HAVE_IMAGE_H) /* BeOS */
39 /* FIXME: this isn't supposed to be an error */
40 #error no dynamic plugins available on your system !
44 #include "beos_specific.h"
52 #include "modules_core.h"
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 LockModule( module_t * p_module );
59 static int UnlockModule( module_t * p_module );
60 static int CallSymbol( module_t * p_module, char * psz_name );
62 /*****************************************************************************
63 * module_CreateBank: create the module bank.
64 *****************************************************************************
65 * This function creates a module bank structure.
66 *****************************************************************************/
67 module_bank_t * module_CreateBank( void )
69 module_bank_t * p_bank;
71 p_bank = malloc( sizeof( module_bank_t ) );
76 /*****************************************************************************
77 * module_InitBank: create the module bank.
78 *****************************************************************************
79 * This function creates a module bank structure and fills it with the
80 * built-in modules, as well as all the dynamic modules it can find.
81 *****************************************************************************/
82 void module_InitBank( module_bank_t * p_bank )
84 static char * path[] = { ".", "lib", PLUGIN_PATH, NULL } ;
86 char ** ppsz_path = path;
89 char * psz_program_path = beos_GetProgramPath();
90 int i_programlen = strlen( psz_program_path );
96 vlc_mutex_init( &p_bank->lock );
98 intf_Msg( "module: module bank initialized" );
100 for( ; *ppsz_path != NULL ; ppsz_path++ )
102 if( (dir = opendir( *ppsz_path )) )
104 /* Store strlen(*ppsz_path) for later use. */
105 int i_dirlen = strlen( *ppsz_path );
107 /* Parse the directory and try to load all files it contains. */
108 while( (file = readdir( dir )) )
110 int i_filelen = strlen( file->d_name );
112 /* We only load files ending with ".so" */
114 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
117 /* Under BeOS, we need to add beos_GetProgramPath() to
118 * access files under the current directory */
119 if( strncmp( file->d_name, "/", 1 ) )
121 psz_file = malloc( i_programlen + i_dirlen
123 if( psz_file == NULL )
127 sprintf( psz_file, "%s/%s/%s", psz_programlen,
128 *ppsz_path, file->d_name );
133 psz_file = malloc( i_dirlen + i_filelen + 2 );
134 if( psz_file == NULL )
138 sprintf( psz_file, "%s/%s", *ppsz_path, file->d_name );
140 /* We created a nice filename -- now we just try to load
141 * it as a dynamic module. */
142 AllocateDynModule( p_bank, psz_file );
144 /* We don't care if the allocation succeeded */
154 /*****************************************************************************
155 * module_DestroyBank: destroy the module bank.
156 *****************************************************************************
157 * This function unloads all unused dynamic modules and removes the module
158 * bank in case of success.
159 *****************************************************************************/
160 void module_DestroyBank( module_bank_t * p_bank )
164 while( p_bank->first != NULL )
166 if( FreeModule( p_bank, p_bank->first ) )
168 /* Module deletion failed */
169 intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
170 p_bank->first->psz_name );
172 /* We just free the module by hand. Niahahahahaha. */
173 p_next = p_bank->first->next;
175 p_bank->first = p_next;
179 /* Destroy the lock */
180 vlc_mutex_destroy( &p_bank->lock );
182 /* We can free the module bank */
188 /*****************************************************************************
189 * module_ResetBank: reset the module bank.
190 *****************************************************************************
191 * This function resets the module bank by unloading all unused dynamic
193 *****************************************************************************/
194 void module_ResetBank( module_bank_t * p_bank )
196 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
200 /*****************************************************************************
201 * module_ManageBank: manage the module bank.
202 *****************************************************************************
203 * This function parses the module bank and hides modules that have been
204 * unused for a while.
205 *****************************************************************************/
206 void module_ManageBank( module_bank_t * p_bank )
210 /* We take the global lock */
211 vlc_mutex_lock( &p_bank->lock );
213 /* Parse the module list to see if any modules need to be unloaded */
214 for( p_module = p_bank->first ;
216 p_module = p_module->next )
218 /* If the module is unused and if it is a dynamic module... */
219 if( p_module->i_usage == 0 && !p_module->b_builtin )
221 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
223 p_module->i_unused_delay++;
227 intf_DbgMsg( "module: hiding unused module `%s'",
228 p_module->psz_name );
229 HideModule( p_module );
234 /* We release the global lock */
235 vlc_mutex_unlock( &p_bank->lock );
240 /*****************************************************************************
241 * module_Need: return the best module function, given a capability list.
242 *****************************************************************************
243 * This function returns the module that best fits the asked capabilities.
244 *****************************************************************************/
245 module_t * module_Need( module_bank_t *p_bank,
246 int i_capabilities, void *p_data )
249 module_t * p_bestmodule = NULL;
250 int i_score, i_totalscore, i_bestscore = 0;
253 /* We take the global lock */
254 vlc_mutex_lock( &p_bank->lock );
256 /* Parse the module list for capabilities and probe each of them */
257 for( p_module = p_bank->first ;
259 p_module = p_module->next )
261 /* Test that this module can do everything we need */
262 if( ( p_module->i_capabilities & i_capabilities ) == i_capabilities )
266 LockModule( p_module );
268 /* Parse all the requested capabilities and test them */
269 for( i_index = 0 ; (1 << i_index) <= i_capabilities ; i_index++ )
271 if( ( (1 << i_index) & i_capabilities ) )
273 i_score = ( (function_list_t *)p_module->p_functions)
274 [i_index].pf_probe( p_data );
278 i_totalscore += i_score;
287 /* If the high score was broken, we have a new champion */
288 if( i_totalscore > i_bestscore )
290 /* Keep the current module locked, but release the previous */
291 if( p_bestmodule != NULL )
293 UnlockModule( p_bestmodule );
296 /* This is the new best module */
297 i_bestscore = i_totalscore;
298 p_bestmodule = p_module;
302 /* This module wasn't interesting, unlock it and forget it */
303 UnlockModule( p_module );
308 /* We release the global lock */
309 vlc_mutex_unlock( &p_bank->lock );
311 intf_Msg( "module: locking module `%s'", p_bestmodule->psz_name );
313 /* Don't forget that the module is still locked if bestmodule != NULL */
314 return( p_bestmodule );
317 /*****************************************************************************
318 * module_Unneed: decrease the usage count of a module.
319 *****************************************************************************
320 * This function must be called by the thread that called module_Need, to
321 * decrease the reference count and allow for hiding of modules.
322 *****************************************************************************/
323 void module_Unneed( module_bank_t * p_bank, module_t * p_module )
325 /* We take the global lock */
326 vlc_mutex_lock( &p_bank->lock );
328 /* Just unlock the module - we can't do anything if it fails,
329 * so there is no need to check the return value. */
330 UnlockModule( p_module );
332 intf_Msg( "module: unlocking module `%s'", p_module->psz_name );
334 /* We release the global lock */
335 vlc_mutex_unlock( &p_bank->lock );
340 /*****************************************************************************
341 * Following functions are local.
342 *****************************************************************************/
344 /*****************************************************************************
345 * AllocateDynModule: load a module into memory and initialize it.
346 *****************************************************************************
347 * This function loads a dynamically loadable module and allocates a structure
348 * for its information data. The module can then be handled by module_Need,
349 * module_Unneed and HideModule. It can be removed by FreeModule.
350 *****************************************************************************/
351 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename )
353 module_t * p_module, * p_othermodule;
354 module_handle_t handle;
356 /* Try to dynamically load the module. */
357 if( module_load( psz_filename, &handle ) )
359 /* The dynamic module couldn't be opened */
360 intf_DbgMsg( "module warning: cannot open %s (%s)",
361 psz_filename, module_error() );
365 /* Now that we have successfully loaded the module, we can
366 * allocate a structure for it */
367 p_module = malloc( sizeof( module_t ) );
368 if( p_module == NULL )
370 intf_ErrMsg( "module error: can't allocate p_module" );
371 module_unload( handle );
375 /* We need to fill these since they may be needed by CallSymbol() */
376 p_module->psz_filename = psz_filename;
377 p_module->handle = handle;
379 /* Initialize the module : fill p_module->psz_name, etc. */
380 if( CallSymbol( p_module, "InitModule" ) != 0 )
382 /* We couldn't call InitModule() */
384 module_unload( handle );
388 /* Check that version numbers match */
389 if( strcmp( VERSION, p_module->psz_version ) )
392 module_unload( handle );
396 /* Check that we don't already have a module with this name */
397 for( p_othermodule = p_bank->first ;
398 p_othermodule != NULL ;
399 p_othermodule = p_othermodule->next )
401 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
404 module_unload( handle );
409 /* Activate the module : fill the capability structure, etc. */
410 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
412 /* We couldn't call ActivateModule() */
414 module_unload( handle );
418 /* We strdup() these entries so that they are still valid when the
419 * module is unloaded. */
420 p_module->psz_filename = strdup( p_module->psz_filename );
421 p_module->psz_name = strdup( p_module->psz_name );
422 p_module->psz_longname = strdup( p_module->psz_longname );
423 p_module->psz_version = strdup( p_module->psz_version );
424 if( p_module->psz_filename == NULL
425 || p_module->psz_name == NULL
426 || p_module->psz_longname == NULL
427 || p_module->psz_version == NULL )
429 intf_ErrMsg( "module error: can't duplicate strings" );
430 free( p_module->psz_filename );
431 free( p_module->psz_name );
432 free( p_module->psz_longname );
433 free( p_module->psz_version );
435 module_unload( handle );
439 /* Everything worked fine ! The module is ready to be added to the list. */
440 p_module->i_usage = 0;
441 p_module->i_unused_delay = 0;
443 p_module->b_builtin = 0;
445 /* Link module into the linked list */
446 if( p_bank->first != NULL )
448 p_bank->first->prev = p_module;
450 p_module->next = p_bank->first;
451 p_module->prev = NULL;
452 p_bank->first = p_module;
454 intf_Msg( "module: dynamic module `%s', %s",
455 p_module->psz_name, p_module->psz_longname );
460 /*****************************************************************************
461 * HideModule: remove a module from memory but keep its structure.
462 *****************************************************************************
463 * This function can only be called if i_usage == 0. It will make a call
464 * to the module's inner DeactivateModule() symbol, and then unload it
465 * from memory. A call to module_Need() will automagically load it again.
466 *****************************************************************************/
467 static int HideModule( module_t * p_module )
469 if( p_module->b_builtin )
471 /* A built-in module should never be hidden. */
472 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
473 p_module->psz_name );
477 if( p_module->i_usage >= 1 )
479 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
480 " in use", p_module->psz_name );
484 if( p_module->i_usage <= -1 )
486 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
487 " hidden", p_module->psz_name );
491 /* Deactivate the module : free the capability structure, etc. */
492 if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
494 /* We couldn't call DeactivateModule() -- looks nasty, but
495 * we can't do much about it. Just try to unload module anyway. */
496 module_unload( p_module->handle );
497 p_module->i_usage = -1;
501 /* Everything worked fine, we can safely unload the module. */
502 module_unload( p_module->handle );
503 p_module->i_usage = -1;
508 /*****************************************************************************
509 * FreeModule: delete a module and its structure.
510 *****************************************************************************
511 * This function can only be called if i_usage <= 0.
512 *****************************************************************************/
513 static int FreeModule( module_bank_t * p_bank, module_t * p_module )
515 /* If the module is not in use but is still in memory, we first have
516 * to hide it and remove it from memory before we can free the
518 if( p_module->b_builtin )
520 if( p_module->i_usage != 0 )
522 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
523 " usage %i", p_module->psz_name, p_module->i_usage );
529 if( p_module->i_usage >= 1 )
531 intf_ErrMsg( "module error: trying to free module `%s' which is"
532 " still in use", p_module->psz_name );
536 /* Two possibilities here: i_usage == -1 and the module is already
537 * unloaded, we can continue, or i_usage == 0, and we have to hide
538 * the module before going on. */
539 if( p_module->i_usage == 0 )
541 if( HideModule( p_module ) != 0 )
548 /* Unlink the module from the linked list. */
549 if( p_module == p_bank->first )
551 p_bank->first = p_module->next;
554 if( p_module->prev != NULL )
556 p_module->prev->next = p_module->next;
559 if( p_module->next != NULL )
561 p_module->next->prev = p_module->prev;
564 /* We free the structures that we strdup()ed in Allocate*Module(). */
565 free( p_module->psz_filename );
566 free( p_module->psz_name );
567 free( p_module->psz_longname );
568 free( p_module->psz_version );
575 /*****************************************************************************
576 * LockModule: increase the usage count of a module and load it if needed.
577 *****************************************************************************
578 * This function has to be called before a thread starts using a module. If
579 * the module is already loaded, we just increase its usage count. If it isn't
580 * loaded, we have to dynamically open it and initialize it.
581 * If you successfully call LockModule() at any moment, be careful to call
582 * UnlockModule() when you don't need it anymore.
583 *****************************************************************************/
584 static int LockModule( module_t * p_module )
586 if( p_module->i_usage >= 0 )
588 /* This module is already loaded and activated, we can return */
593 if( p_module->b_builtin )
595 /* A built-in module should always have a refcount >= 0 ! */
596 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
597 p_module->psz_name, p_module->i_usage );
601 if( p_module->i_usage != -1 )
603 /* This shouldn't happen. Ever. We have serious problems here. */
604 intf_ErrMsg( "module error: dynamic module `%s' has refcount %i",
605 p_module->psz_name, p_module->i_usage );
609 /* i_usage == -1, which means that the module isn't in memory */
610 if( module_load( p_module->psz_filename, &p_module->handle ) )
612 /* The dynamic module couldn't be opened */
613 intf_ErrMsg( "module error: cannot open %s (%s)",
614 p_module->psz_filename, module_error() );
618 /* FIXME: what to do if the guy modified the plugin while it was
619 * unloaded ? It makes XMMS crash nastily, perhaps we should try
620 * to be a bit more clever here. */
622 /* Activate the module : fill the capability structure, etc. */
623 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
625 /* We couldn't call ActivateModule() -- looks nasty, but
626 * we can't do much about it. Just try to unload module. */
627 module_unload( p_module->handle );
628 p_module->i_usage = -1;
632 /* Everything worked fine ! The module is ready to be used */
633 p_module->i_usage = 1;
638 /*****************************************************************************
639 * UnlockModule: decrease the usage count of a module.
640 *****************************************************************************
641 * We decrease the usage count of a module so that we know when a module
642 * becomes unused and can be hidden.
643 *****************************************************************************/
644 static int UnlockModule( module_t * p_module )
646 if( p_module->i_usage <= 0 )
648 /* This shouldn't happen. Ever. We have serious problems here. */
649 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
650 " which isn't even in use", p_module->psz_name );
654 /* This module is still in use, we can return */
656 p_module->i_unused_delay = 0;
661 /*****************************************************************************
662 * CallSymbol: calls a module symbol.
663 *****************************************************************************
664 * This function calls a symbol given its name and a module structure. The
665 * symbol MUST refer to a function returning int and taking a module_t* as
667 *****************************************************************************/
668 static int CallSymbol( module_t * p_module, char * psz_name )
670 typedef int ( symbol_t ) ( module_t * p_module );
673 /* Try to resolve the symbol */
674 p_symbol = module_getsymbol( p_module->handle, psz_name );
678 /* We couldn't load the symbol */
679 intf_DbgMsg( "module warning: cannot find symbol %s in module %s (%s)",
680 psz_name, p_module->psz_filename, module_error() );
684 /* We can now try to call the symbol */
685 if( p_symbol( p_module ) != 0 )
687 /* With a well-written module we shouldn't have to print an
688 * additional error message here, but just make sure. */
689 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
690 psz_name, p_module->psz_filename );
694 /* Everything worked fine, we can return */