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 /* 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
33 #include <stdlib.h> /* free(), strtol() */
34 #include <stdio.h> /* sprintf() */
35 #include <string.h> /* strdup() */
38 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
39 #include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
41 #elif defined(HAVE_IMAGE_H) /* BeOS */
45 /* FIXME: this isn't supposed to be an error */
46 #error no dynamic plugins available on your system !
50 #include "beos_specific.h"
58 #include "modules_core.h"
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 );
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 )
75 module_bank_t * p_bank;
77 p_bank = malloc( sizeof( module_bank_t ) );
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 )
90 static char * path[] = { ".", "lib", PLUGIN_PATH, NULL };
92 char ** ppsz_path = path;
96 char * psz_vlcpath = beos_GetProgramPath();
97 int i_vlclen = strlen( psz_vlcpath );
98 boolean_t b_notinroot;
101 struct dirent * file;
103 p_bank->first = NULL;
104 vlc_mutex_init( &p_bank->lock );
106 intf_WarnMsg( 1, "module: module bank initialized" );
108 for( ; *ppsz_path != NULL ; ppsz_path++ )
110 /* Store strlen(*ppsz_path) for later use. */
111 int i_dirlen = strlen( *ppsz_path );
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 ) )
119 i_dirlen += i_vlclen + 2;
122 psz_fullpath = malloc( i_dirlen );
123 if( psz_fullpath == NULL )
127 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
132 psz_fullpath = *ppsz_path;
135 intf_WarnMsgImm( 2, "module: browsing %s", psz_fullpath );
137 if( (dir = opendir( psz_fullpath )) )
139 /* Parse the directory and try to load all files it contains. */
140 while( (file = readdir( dir )) )
142 int i_filelen = strlen( file->d_name );
144 /* We only load files ending with ".so" */
146 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
148 psz_file = malloc( i_dirlen + i_filelen + 2 );
149 if( psz_file == NULL )
153 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
155 /* We created a nice filename -- now we just try to load
156 * it as a dynamic module. */
157 AllocateDynModule( p_bank, psz_file );
159 /* We don't care if the allocation succeeded */
164 /* Close the directory if successfully opened */
171 free( psz_fullpath );
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 )
189 while( p_bank->first != NULL )
191 if( FreeModule( p_bank, p_bank->first ) )
193 /* Module deletion failed */
194 intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
195 p_bank->first->psz_name );
197 /* We just free the module by hand. Niahahahahaha. */
198 p_next = p_bank->first->next;
200 p_bank->first = p_next;
204 /* Destroy the lock */
205 vlc_mutex_destroy( &p_bank->lock );
207 /* We can free the module bank */
213 /*****************************************************************************
214 * module_ResetBank: reset the module bank.
215 *****************************************************************************
216 * This function resets the module bank by unloading all unused dynamic
218 *****************************************************************************/
219 void module_ResetBank( module_bank_t * p_bank )
221 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
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 )
235 /* We take the global lock */
236 vlc_mutex_lock( &p_bank->lock );
238 /* Parse the module list to see if any modules need to be unloaded */
239 for( p_module = p_bank->first ;
241 p_module = p_module->next )
243 /* If the module is unused and if it is a dynamic module... */
244 if( p_module->i_usage == 0 && !p_module->b_builtin )
246 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
248 p_module->i_unused_delay++;
252 intf_WarnMsg( 1, "module: hiding unused module `%s'",
253 p_module->psz_name );
254 HideModule( p_module );
259 /* We release the global lock */
260 vlc_mutex_unlock( &p_bank->lock );
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 )
274 module_t * p_bestmodule = NULL;
275 int i_score, i_totalscore, i_bestscore = 0;
278 /* We take the global lock */
279 vlc_mutex_lock( &p_bank->lock );
281 /* Parse the module list for capabilities and probe each of them */
282 for( p_module = p_bank->first ;
284 p_module = p_module->next )
286 /* Test that this module can do everything we need */
287 if( ( p_module->i_capabilities & i_capabilities ) == i_capabilities )
291 LockModule( p_module );
293 /* Parse all the requested capabilities and test them */
294 for( i_index = 0 ; (1 << i_index) <= i_capabilities ; i_index++ )
296 if( ( (1 << i_index) & i_capabilities ) )
298 i_score = ( (function_list_t *)p_module->p_functions)
299 [i_index].pf_probe( p_data );
303 i_totalscore += i_score;
312 /* If the high score was broken, we have a new champion */
313 if( i_totalscore > i_bestscore )
315 /* Keep the current module locked, but release the previous */
316 if( p_bestmodule != NULL )
318 UnlockModule( p_bestmodule );
321 /* This is the new best module */
322 i_bestscore = i_totalscore;
323 p_bestmodule = p_module;
327 /* This module wasn't interesting, unlock it and forget it */
328 UnlockModule( p_module );
333 /* We release the global lock */
334 vlc_mutex_unlock( &p_bank->lock );
336 if( p_bestmodule != NULL )
338 intf_WarnMsg( 1, "module: locking module `%s'",
339 p_bestmodule->psz_name );
342 /* Don't forget that the module is still locked if bestmodule != NULL */
343 return( p_bestmodule );
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 )
354 /* We take the global lock */
355 vlc_mutex_lock( &p_bank->lock );
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 );
361 intf_WarnMsg( 1, "module: unlocking module `%s'", p_module->psz_name );
363 /* We release the global lock */
364 vlc_mutex_unlock( &p_bank->lock );
369 /*****************************************************************************
370 * Following functions are local.
371 *****************************************************************************/
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 )
382 module_t * p_module, * p_othermodule;
383 module_handle_t handle;
385 /* Try to dynamically load the module. */
386 if( module_load( psz_filename, &handle ) )
388 /* The dynamic module couldn't be opened */
389 intf_WarnMsgImm( 1, "module warning: cannot open %s (%s)",
390 psz_filename, module_error() );
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 )
399 intf_ErrMsg( "module error: can't allocate p_module" );
400 module_unload( handle );
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;
408 /* Initialize the module : fill p_module->psz_name, etc. */
409 if( CallSymbol( p_module, "InitModule" ) != 0 )
411 /* We couldn't call InitModule() */
413 module_unload( handle );
417 /* Check that version numbers match */
418 if( strcmp( VERSION, p_module->psz_version ) )
421 module_unload( handle );
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 )
430 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
433 module_unload( handle );
438 /* Activate the module : fill the capability structure, etc. */
439 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
441 /* We couldn't call ActivateModule() */
443 module_unload( handle );
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 )
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 );
464 module_unload( handle );
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;
472 p_module->b_builtin = 0;
474 /* Link module into the linked list */
475 if( p_bank->first != NULL )
477 p_bank->first->prev = p_module;
479 p_module->next = p_bank->first;
480 p_module->prev = NULL;
481 p_bank->first = p_module;
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 );
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 )
499 if( p_module->b_builtin )
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 );
507 if( p_module->i_usage >= 1 )
509 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
510 " in use", p_module->psz_name );
514 if( p_module->i_usage <= -1 )
516 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
517 " hidden", p_module->psz_name );
521 /* Deactivate the module : free the capability structure, etc. */
522 if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
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;
531 /* Everything worked fine, we can safely unload the module. */
532 module_unload( p_module->handle );
533 p_module->i_usage = -1;
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 )
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
548 if( p_module->b_builtin )
550 if( p_module->i_usage != 0 )
552 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
553 " usage %i", p_module->psz_name, p_module->i_usage );
559 if( p_module->i_usage >= 1 )
561 intf_ErrMsg( "module error: trying to free module `%s' which is"
562 " still in use", p_module->psz_name );
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 )
571 if( HideModule( p_module ) != 0 )
578 /* Unlink the module from the linked list. */
579 if( p_module == p_bank->first )
581 p_bank->first = p_module->next;
584 if( p_module->prev != NULL )
586 p_module->prev->next = p_module->next;
589 if( p_module->next != NULL )
591 p_module->next->prev = p_module->prev;
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 );
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 )
616 if( p_module->i_usage >= 0 )
618 /* This module is already loaded and activated, we can return */
623 if( p_module->b_builtin )
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 );
631 if( p_module->i_usage != -1 )
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 );
639 /* i_usage == -1, which means that the module isn't in memory */
640 if( module_load( p_module->psz_filename, &p_module->handle ) )
642 /* The dynamic module couldn't be opened */
643 intf_ErrMsg( "module error: cannot open %s (%s)",
644 p_module->psz_filename, module_error() );
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. */
652 /* Activate the module : fill the capability structure, etc. */
653 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
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;
662 /* Everything worked fine ! The module is ready to be used */
663 p_module->i_usage = 1;
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 )
676 if( p_module->i_usage <= 0 )
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 );
684 /* This module is still in use, we can return */
686 p_module->i_unused_delay = 0;
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
697 *****************************************************************************/
698 static int CallSymbol( module_t * p_module, char * psz_name )
700 typedef int ( symbol_t ) ( module_t * p_module );
703 /* Try to resolve the symbol */
704 p_symbol = module_getsymbol( p_module->handle, psz_name );
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() );
715 /* We can now try to call the symbol */
716 if( p_symbol( p_module ) != 0 )
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 );
725 /* Everything worked fine, we can return */