1 /*****************************************************************************
2 * modules.c : Built-in and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.23 2001/04/15 04:19:58 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
28 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
29 * is set to 64. Don't try to be cleverer. */
30 #ifdef _FILE_OFFSET_BITS
31 #undef _FILE_OFFSET_BITS
34 #include <stdlib.h> /* free(), strtol() */
35 #include <stdio.h> /* sprintf() */
36 #include <string.h> /* strdup() */
39 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
40 # include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
41 # define HAVE_DYNAMIC_PLUGINS
42 #elif defined(HAVE_IMAGE_H) /* BeOS */
44 # define HAVE_DYNAMIC_PLUGINS
46 # undef HAVE_DYNAMIC_PLUGINS
50 # include "beos_specific.h"
54 # include "darwin_specific.h"
62 #include "modules_builtin.h"
63 #include "modules_core.h"
65 /* Local prototypes */
66 #ifdef HAVE_DYNAMIC_PLUGINS
67 static int AllocatePluginModule ( module_bank_t *, char * );
69 static int AllocateBuiltinModule( module_bank_t *,
70 int ( * ) ( module_t * ),
71 int ( * ) ( module_t * ),
72 int ( * ) ( module_t * ) );
73 static int FreeModule ( module_bank_t * p_bank, module_t * );
74 static int LockModule ( module_t * );
75 static int UnlockModule ( module_t * );
76 #ifdef HAVE_DYNAMIC_PLUGINS
77 static int HideModule ( module_t * );
78 static int CallSymbol ( module_t *, char * );
81 /*****************************************************************************
82 * module_CreateBank: create the module bank.
83 *****************************************************************************
84 * This function creates a module bank structure.
85 *****************************************************************************/
86 module_bank_t * module_CreateBank( void )
88 module_bank_t * p_bank;
90 p_bank = malloc( sizeof( module_bank_t ) );
95 /*****************************************************************************
96 * module_InitBank: create the module bank.
97 *****************************************************************************
98 * This function creates a module bank structure and fills it with the
99 * built-in modules, as well as all the plugin modules it can find.
100 *****************************************************************************/
101 void module_InitBank( module_bank_t * p_bank )
103 #ifdef HAVE_DYNAMIC_PLUGINS
104 static char * path[] = { ".", "lib", PLUGIN_PATH, NULL, NULL };
106 char ** ppsz_path = path;
109 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
110 char * psz_vlcpath = system_GetProgramPath();
111 int i_vlclen = strlen( psz_vlcpath );
112 boolean_t b_notinroot;
115 struct dirent * file;
116 #endif /* HAVE_DYNAMIC_PLUGINS */
118 p_bank->first = NULL;
119 vlc_mutex_init( &p_bank->lock );
121 intf_WarnMsg( 1, "module: module bank initialized" );
123 intf_WarnMsg( 2, "module: checking built-in modules" );
125 ALLOCATE_ALL_BUILTINS();
127 #ifdef HAVE_DYNAMIC_PLUGINS
128 intf_WarnMsg( 2, "module: checking plugin modules" );
130 for( ; *ppsz_path != NULL ; ppsz_path++ )
132 /* Store strlen(*ppsz_path) for later use. */
133 int i_dirlen = strlen( *ppsz_path );
135 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
137 /* Under BeOS, we need to add beos_GetProgramPath() to access
138 * files under the current directory */
139 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
141 i_dirlen += i_vlclen + 2;
144 psz_fullpath = malloc( i_dirlen );
145 if( psz_fullpath == NULL )
149 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
154 psz_fullpath = *ppsz_path;
157 intf_WarnMsgImm( 3, "module: browsing `%s'", psz_fullpath );
159 if( (dir = opendir( psz_fullpath )) )
161 /* Parse the directory and try to load all files it contains. */
162 while( (file = readdir( dir )) )
164 int i_filelen = strlen( file->d_name );
166 /* We only load files ending with ".so" */
168 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
170 psz_file = malloc( i_dirlen + i_filelen + 2 );
171 if( psz_file == NULL )
175 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
177 /* We created a nice filename -- now we just try to load
178 * it as a plugin module. */
179 AllocatePluginModule( p_bank, psz_file );
181 /* We don't care if the allocation succeeded */
186 /* Close the directory if successfully opened */
190 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
193 free( psz_fullpath );
197 #endif /* HAVE_DYNAMIC_PLUGINS */
202 /*****************************************************************************
203 * module_DestroyBank: destroy the module bank.
204 *****************************************************************************
205 * This function unloads all unused plugin modules and removes the module
206 * bank in case of success.
207 *****************************************************************************/
208 void module_DestroyBank( module_bank_t * p_bank )
212 while( p_bank->first != NULL )
214 if( FreeModule( p_bank, p_bank->first ) )
216 /* Module deletion failed */
217 intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
218 p_bank->first->psz_name );
220 /* We just free the module by hand. Niahahahahaha. */
221 p_next = p_bank->first->next;
223 p_bank->first = p_next;
227 /* Destroy the lock */
228 vlc_mutex_destroy( &p_bank->lock );
230 /* We can free the module bank */
236 /*****************************************************************************
237 * module_ResetBank: reset the module bank.
238 *****************************************************************************
239 * This function resets the module bank by unloading all unused plugin
241 *****************************************************************************/
242 void module_ResetBank( module_bank_t * p_bank )
244 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
248 /*****************************************************************************
249 * module_ManageBank: manage the module bank.
250 *****************************************************************************
251 * This function parses the module bank and hides modules that have been
252 * unused for a while.
253 *****************************************************************************/
254 void module_ManageBank( module_bank_t * p_bank )
256 #ifdef HAVE_DYNAMIC_PLUGINS
259 /* We take the global lock */
260 vlc_mutex_lock( &p_bank->lock );
262 /* Parse the module list to see if any modules need to be unloaded */
263 for( p_module = p_bank->first ;
265 p_module = p_module->next )
267 /* If the module is unused and if it is a plugin module... */
268 if( p_module->i_usage == 0 && !p_module->b_builtin )
270 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
272 p_module->i_unused_delay++;
276 intf_WarnMsg( 3, "module: hiding unused module `%s'",
277 p_module->psz_name );
278 HideModule( p_module );
283 /* We release the global lock */
284 vlc_mutex_unlock( &p_bank->lock );
285 #endif /* HAVE_DYNAMIC_PLUGINS */
290 /*****************************************************************************
291 * module_Need: return the best module function, given a capability list.
292 *****************************************************************************
293 * This function returns the module that best fits the asked capabilities.
294 *****************************************************************************/
295 module_t * module_Need( module_bank_t *p_bank,
296 int i_capabilities, void *p_data )
299 module_t * p_bestmodule = NULL;
300 int i_score, i_totalscore, i_bestscore = 0;
303 /* We take the global lock */
304 vlc_mutex_lock( &p_bank->lock );
306 /* Parse the module list for capabilities and probe each of them */
307 for( p_module = p_bank->first ;
309 p_module = p_module->next )
311 /* Test that this module can do everything we need */
312 if( ( p_module->i_capabilities & i_capabilities ) == i_capabilities )
316 LockModule( p_module );
318 /* Parse all the requested capabilities and test them */
319 for( i_index = 0 ; (1 << i_index) <= i_capabilities ; i_index++ )
321 if( ( (1 << i_index) & i_capabilities ) )
323 i_score = ( (function_list_t *)p_module->p_functions)
324 [i_index].pf_probe( p_data );
328 i_totalscore += i_score;
337 /* If the high score was broken, we have a new champion */
338 if( i_totalscore > i_bestscore )
340 /* Keep the current module locked, but release the previous */
341 if( p_bestmodule != NULL )
343 UnlockModule( p_bestmodule );
346 /* This is the new best module */
347 i_bestscore = i_totalscore;
348 p_bestmodule = p_module;
352 /* This module wasn't interesting, unlock it and forget it */
353 UnlockModule( p_module );
358 /* We can release the global lock, module refcount was incremented */
359 vlc_mutex_unlock( &p_bank->lock );
361 if( p_bestmodule != NULL )
363 intf_WarnMsg( 3, "module: locking module `%s'",
364 p_bestmodule->psz_name );
367 /* Don't forget that the module is still locked if bestmodule != NULL */
368 return( p_bestmodule );
371 /*****************************************************************************
372 * module_Unneed: decrease the usage count of a module.
373 *****************************************************************************
374 * This function must be called by the thread that called module_Need, to
375 * decrease the reference count and allow for hiding of modules.
376 *****************************************************************************/
377 void module_Unneed( module_bank_t * p_bank, module_t * p_module )
379 /* We take the global lock */
380 vlc_mutex_lock( &p_bank->lock );
382 /* Just unlock the module - we can't do anything if it fails,
383 * so there is no need to check the return value. */
384 UnlockModule( p_module );
386 intf_WarnMsg( 3, "module: unlocking module `%s'", p_module->psz_name );
388 /* We release the global lock */
389 vlc_mutex_unlock( &p_bank->lock );
394 /*****************************************************************************
395 * Following functions are local.
396 *****************************************************************************/
398 #ifdef HAVE_DYNAMIC_PLUGINS
399 /*****************************************************************************
400 * AllocatePluginModule: load a module into memory and initialize it.
401 *****************************************************************************
402 * This function loads a dynamically loadable module and allocates a structure
403 * for its information data. The module can then be handled by module_Need,
404 * module_Unneed and HideModule. It can be removed by FreeModule.
405 *****************************************************************************/
406 static int AllocatePluginModule( module_bank_t * p_bank, char * psz_filename )
408 module_t * p_module, * p_othermodule;
409 module_handle_t handle;
411 /* Try to dynamically load the module. */
412 if( module_load( psz_filename, &handle ) )
414 /* The plugin module couldn't be opened */
415 intf_WarnMsgImm( 3, "module warning: cannot open %s (%s)",
416 psz_filename, module_error() );
420 /* Now that we have successfully loaded the module, we can
421 * allocate a structure for it */
422 p_module = malloc( sizeof( module_t ) );
423 if( p_module == NULL )
425 intf_ErrMsg( "module error: can't allocate p_module" );
426 module_unload( handle );
430 /* We need to fill these since they may be needed by CallSymbol() */
431 p_module->is.plugin.psz_filename = psz_filename;
432 p_module->is.plugin.handle = handle;
434 /* Initialize the module : fill p_module->psz_name, etc. */
435 if( CallSymbol( p_module, "InitModule" ) != 0 )
437 /* We couldn't call InitModule() */
439 module_unload( handle );
443 /* Check that version numbers match */
444 if( strcmp( VERSION, p_module->psz_version ) )
447 module_unload( handle );
451 /* Check that we don't already have a module with this name */
452 for( p_othermodule = p_bank->first ;
453 p_othermodule != NULL ;
454 p_othermodule = p_othermodule->next )
456 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
459 module_unload( handle );
464 /* Activate the module : fill the capability structure, etc. */
465 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
467 /* We couldn't call ActivateModule() */
469 module_unload( handle );
473 /* We strdup() these entries so that they are still valid when the
474 * module is unloaded. */
475 p_module->is.plugin.psz_filename =
476 strdup( p_module->is.plugin.psz_filename );
477 p_module->psz_name = strdup( p_module->psz_name );
478 p_module->psz_longname = strdup( p_module->psz_longname );
479 p_module->psz_version = strdup( p_module->psz_version );
480 if( p_module->is.plugin.psz_filename == NULL
481 || p_module->psz_name == NULL
482 || p_module->psz_longname == NULL
483 || p_module->psz_version == NULL )
485 intf_ErrMsg( "module error: can't duplicate strings" );
486 free( p_module->is.plugin.psz_filename );
487 free( p_module->psz_name );
488 free( p_module->psz_longname );
489 free( p_module->psz_version );
491 module_unload( handle );
495 /* Everything worked fine ! The module is ready to be added to the list. */
496 p_module->i_usage = 0;
497 p_module->i_unused_delay = 0;
499 p_module->b_builtin = 0;
501 /* Link module into the linked list */
502 if( p_bank->first != NULL )
504 p_bank->first->prev = p_module;
506 p_module->next = p_bank->first;
507 p_module->prev = NULL;
508 p_bank->first = p_module;
510 /* Immediate message so that a slow module doesn't make the user wait */
511 intf_WarnMsgImm( 2, "module: plugin module `%s', %s",
512 p_module->psz_name, p_module->psz_longname );
516 #endif /* HAVE_DYNAMIC_PLUGINS */
518 /*****************************************************************************
519 * AllocateBuiltinModule: initialize a built-in module.
520 *****************************************************************************
521 * This function registers a built-in module and allocates a structure
522 * for its information data. The module can then be handled by module_Need,
523 * module_Unneed and HideModule. It can be removed by FreeModule.
524 *****************************************************************************/
525 static int AllocateBuiltinModule( module_bank_t * p_bank,
526 int ( *pf_init ) ( module_t * ),
527 int ( *pf_activate ) ( module_t * ),
528 int ( *pf_deactivate ) ( module_t * ) )
530 module_t * p_module, * p_othermodule;
532 /* Now that we have successfully loaded the module, we can
533 * allocate a structure for it */
534 p_module = malloc( sizeof( module_t ) );
535 if( p_module == NULL )
537 intf_ErrMsg( "module error: can't allocate p_module" );
541 /* Initialize the module : fill p_module->psz_name, etc. */
542 if( pf_init( p_module ) != 0 )
544 /* With a well-written module we shouldn't have to print an
545 * additional error message here, but just make sure. */
546 intf_ErrMsg( "module error: failed calling init in builtin module" );
551 /* Check that version numbers match */
552 if( strcmp( VERSION, p_module->psz_version ) )
558 /* Check that we don't already have a module with this name */
559 for( p_othermodule = p_bank->first ;
560 p_othermodule != NULL ;
561 p_othermodule = p_othermodule->next )
563 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
570 if( pf_activate( p_module ) != 0 )
572 /* With a well-written module we shouldn't have to print an
573 * additional error message here, but just make sure. */
574 intf_ErrMsg( "module error: failed calling activate "
575 "in builtin module" );
580 /* We strdup() these entries so that they are still valid when the
581 * module is unloaded. */
582 p_module->psz_name = strdup( p_module->psz_name );
583 p_module->psz_longname = strdup( p_module->psz_longname );
584 p_module->psz_version = strdup( p_module->psz_version );
585 if( p_module->psz_name == NULL
586 || p_module->psz_longname == NULL
587 || p_module->psz_version == NULL )
589 intf_ErrMsg( "module error: can't duplicate strings" );
590 free( p_module->psz_name );
591 free( p_module->psz_longname );
592 free( p_module->psz_version );
597 /* Everything worked fine ! The module is ready to be added to the list. */
598 p_module->i_usage = 0;
599 p_module->i_unused_delay = 0;
601 p_module->b_builtin = 1;
602 p_module->is.builtin.pf_deactivate = pf_deactivate;
604 /* Link module into the linked list */
605 if( p_bank->first != NULL )
607 p_bank->first->prev = p_module;
609 p_module->next = p_bank->first;
610 p_module->prev = NULL;
611 p_bank->first = p_module;
613 /* Immediate message so that a slow module doesn't make the user wait */
614 intf_WarnMsgImm( 2, "module: builtin module `%s', %s",
615 p_module->psz_name, p_module->psz_longname );
620 /*****************************************************************************
621 * FreeModule: delete a module and its structure.
622 *****************************************************************************
623 * This function can only be called if i_usage <= 0.
624 *****************************************************************************/
625 static int FreeModule( module_bank_t * p_bank, module_t * p_module )
627 /* If the module is not in use but is still in memory, we first have
628 * to hide it and remove it from memory before we can free the
630 if( p_module->b_builtin )
632 if( p_module->i_usage != 0 )
634 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
635 " usage %i", p_module->psz_name, p_module->i_usage );
640 /* We deactivate the module now. */
641 p_module->is.builtin.pf_deactivate( p_module );
644 #ifdef HAVE_DYNAMIC_PLUGINS
647 if( p_module->i_usage >= 1 )
649 intf_ErrMsg( "module error: trying to free module `%s' which is"
650 " still in use", p_module->psz_name );
654 /* Two possibilities here: i_usage == -1 and the module is already
655 * unloaded, we can continue, or i_usage == 0, and we have to hide
656 * the module before going on. */
657 if( p_module->i_usage == 0 )
659 if( HideModule( p_module ) != 0 )
667 /* Unlink the module from the linked list. */
668 if( p_module == p_bank->first )
670 p_bank->first = p_module->next;
673 if( p_module->prev != NULL )
675 p_module->prev->next = p_module->next;
678 if( p_module->next != NULL )
680 p_module->next->prev = p_module->prev;
683 /* We free the structures that we strdup()ed in Allocate*Module(). */
684 #ifdef HAVE_DYNAMIC_PLUGINS
685 if( !p_module->b_builtin )
687 free( p_module->is.plugin.psz_filename );
690 free( p_module->psz_name );
691 free( p_module->psz_longname );
692 free( p_module->psz_version );
699 /*****************************************************************************
700 * LockModule: increase the usage count of a module and load it if needed.
701 *****************************************************************************
702 * This function has to be called before a thread starts using a module. If
703 * the module is already loaded, we just increase its usage count. If it isn't
704 * loaded, we have to dynamically open it and initialize it.
705 * If you successfully call LockModule() at any moment, be careful to call
706 * UnlockModule() when you don't need it anymore.
707 *****************************************************************************/
708 static int LockModule( module_t * p_module )
710 if( p_module->i_usage >= 0 )
712 /* This module is already loaded and activated, we can return */
717 if( p_module->b_builtin )
719 /* A built-in module should always have a refcount >= 0 ! */
720 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
721 p_module->psz_name, p_module->i_usage );
725 #ifdef HAVE_DYNAMIC_PLUGINS
726 if( p_module->i_usage != -1 )
728 /* This shouldn't happen. Ever. We have serious problems here. */
729 intf_ErrMsg( "module error: plugin module `%s' has refcount %i",
730 p_module->psz_name, p_module->i_usage );
734 /* i_usage == -1, which means that the module isn't in memory */
735 if( module_load( p_module->is.plugin.psz_filename,
736 &p_module->is.plugin.handle ) )
738 /* The plugin module couldn't be opened */
739 intf_ErrMsg( "module error: cannot open %s (%s)",
740 p_module->is.plugin.psz_filename, module_error() );
744 /* FIXME: what to do if the guy modified the plugin while it was
745 * unloaded ? It makes XMMS crash nastily, perhaps we should try
746 * to be a bit more clever here. */
748 /* Activate the module : fill the capability structure, etc. */
749 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
751 /* We couldn't call ActivateModule() -- looks nasty, but
752 * we can't do much about it. Just try to unload module. */
753 module_unload( p_module->is.plugin.handle );
754 p_module->i_usage = -1;
758 /* Everything worked fine ! The module is ready to be used */
759 p_module->i_usage = 1;
760 #endif /* HAVE_DYNAMIC_PLUGINS */
765 /*****************************************************************************
766 * UnlockModule: decrease the usage count of a module.
767 *****************************************************************************
768 * We decrease the usage count of a module so that we know when a module
769 * becomes unused and can be hidden.
770 *****************************************************************************/
771 static int UnlockModule( module_t * p_module )
773 if( p_module->i_usage <= 0 )
775 /* This shouldn't happen. Ever. We have serious problems here. */
776 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
777 " which isn't even in use", p_module->psz_name );
781 /* This module is still in use, we can return */
783 p_module->i_unused_delay = 0;
788 #ifdef HAVE_DYNAMIC_PLUGINS
789 /*****************************************************************************
790 * HideModule: remove a module from memory but keep its structure.
791 *****************************************************************************
792 * This function can only be called if i_usage == 0. It will make a call
793 * to the module's inner DeactivateModule() symbol, and then unload it
794 * from memory. A call to module_Need() will automagically load it again.
795 *****************************************************************************/
796 static int HideModule( module_t * p_module )
798 if( p_module->b_builtin )
800 /* A built-in module should never be hidden. */
801 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
802 p_module->psz_name );
806 if( p_module->i_usage >= 1 )
808 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
809 " in use", p_module->psz_name );
813 if( p_module->i_usage <= -1 )
815 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
816 " hidden", p_module->psz_name );
820 /* Deactivate the module : free the capability structure, etc. */
821 if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
823 /* We couldn't call DeactivateModule() -- looks nasty, but
824 * we can't do much about it. Just try to unload module anyway. */
825 module_unload( p_module->is.plugin.handle );
826 p_module->i_usage = -1;
830 /* Everything worked fine, we can safely unload the module. */
831 module_unload( p_module->is.plugin.handle );
832 p_module->i_usage = -1;
837 /*****************************************************************************
838 * CallSymbol: calls a module symbol.
839 *****************************************************************************
840 * This function calls a symbol given its name and a module structure. The
841 * symbol MUST refer to a function returning int and taking a module_t* as
843 *****************************************************************************/
844 static int CallSymbol( module_t * p_module, char * psz_name )
846 typedef int ( symbol_t ) ( module_t * p_module );
849 /* Try to resolve the symbol */
850 p_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
854 /* We couldn't load the symbol */
855 intf_WarnMsg( 3, "module warning: "
856 "cannot find symbol %s in module %s (%s)",
857 psz_name, p_module->is.plugin.psz_filename,
862 /* We can now try to call the symbol */
863 if( p_symbol( p_module ) != 0 )
865 /* With a well-written module we shouldn't have to print an
866 * additional error message here, but just make sure. */
867 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
868 psz_name, p_module->is.plugin.psz_filename );
872 /* Everything worked fine, we can return */
875 #endif /* HAVE_DYNAMIC_PLUGINS */