1 /*****************************************************************************
2 * modules.c : Built-in and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.28 2001/05/01 04:18:18 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 #ifdef HAVE_DYNAMIC_PLUGINS
63 # include "modules_core.h"
65 #include "modules_builtin.h"
67 /* Local prototypes */
68 #ifdef HAVE_DYNAMIC_PLUGINS
69 static int AllocatePluginModule ( char * );
71 static int AllocateBuiltinModule( int ( * ) ( module_t * ),
72 int ( * ) ( module_t * ),
73 int ( * ) ( module_t * ) );
74 static int DeleteModule ( module_t * );
75 static int LockModule ( module_t * );
76 static int UnlockModule ( module_t * );
77 #ifdef HAVE_DYNAMIC_PLUGINS
78 static int HideModule ( module_t * );
79 static int CallSymbol ( module_t *, char * );
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 plugin modules it can find.
87 *****************************************************************************/
88 void module_InitBank( )
90 #ifdef HAVE_DYNAMIC_PLUGINS
91 static char * path[] = { ".", "lib", PLUGIN_PATH, NULL, NULL };
93 char ** ppsz_path = path;
96 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
97 char * psz_vlcpath = system_GetProgramPath();
98 int i_vlclen = strlen( psz_vlcpath );
99 boolean_t b_notinroot;
102 struct dirent * file;
103 #endif /* HAVE_DYNAMIC_PLUGINS */
105 p_bank->first = NULL;
106 vlc_mutex_init( &p_bank->lock );
108 intf_WarnMsg( 2, "module: checking built-in modules" );
110 ALLOCATE_ALL_BUILTINS();
112 #ifdef HAVE_DYNAMIC_PLUGINS
113 intf_WarnMsg( 2, "module: checking plugin modules" );
115 for( ; *ppsz_path != NULL ; ppsz_path++ )
117 /* Store strlen(*ppsz_path) for later use. */
118 int i_dirlen = strlen( *ppsz_path );
120 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
122 /* Under BeOS, we need to add beos_GetProgramPath() to access
123 * files under the current directory */
124 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
126 i_dirlen += i_vlclen + 2;
129 psz_fullpath = malloc( i_dirlen );
130 if( psz_fullpath == NULL )
134 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
139 psz_fullpath = *ppsz_path;
142 intf_WarnMsgImm( 3, "module: browsing `%s'", psz_fullpath );
144 if( (dir = opendir( psz_fullpath )) )
146 /* Parse the directory and try to load all files it contains. */
147 while( (file = readdir( dir )) )
149 int i_filelen = strlen( file->d_name );
151 /* We only load files ending with ".so" */
153 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
155 psz_file = malloc( i_dirlen + i_filelen + 2 );
156 if( psz_file == NULL )
160 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
162 /* We created a nice filename -- now we just try to load
163 * it as a plugin module. */
164 AllocatePluginModule( psz_file );
166 /* We don't care if the allocation succeeded */
171 /* Close the directory if successfully opened */
175 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
178 free( psz_fullpath );
182 #endif /* HAVE_DYNAMIC_PLUGINS */
184 intf_WarnMsg( 1, "module: module bank initialized" );
189 /*****************************************************************************
190 * module_EndBank: destroy the module bank.
191 *****************************************************************************
192 * This function unloads all unused plugin modules and removes the module
193 * bank in case of success.
194 *****************************************************************************/
195 void module_EndBank( )
199 while( p_bank->first != NULL )
201 if( DeleteModule( p_bank->first ) )
203 /* Module deletion failed */
204 intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
205 p_bank->first->psz_name );
207 /* We just free the module by hand. Niahahahahaha. */
208 p_next = p_bank->first->next;
210 p_bank->first = p_next;
214 /* Destroy the lock */
215 vlc_mutex_destroy( &p_bank->lock );
220 /*****************************************************************************
221 * module_ResetBank: reset the module bank.
222 *****************************************************************************
223 * This function resets the module bank by unloading all unused plugin
225 *****************************************************************************/
226 void module_ResetBank( )
228 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
232 /*****************************************************************************
233 * module_ManageBank: manage the module bank.
234 *****************************************************************************
235 * This function parses the module bank and hides modules that have been
236 * unused for a while.
237 *****************************************************************************/
238 void module_ManageBank( )
240 #ifdef HAVE_DYNAMIC_PLUGINS
243 /* We take the global lock */
244 vlc_mutex_lock( &p_bank->lock );
246 /* Parse the module list to see if any modules need to be unloaded */
247 for( p_module = p_bank->first ;
249 p_module = p_module->next )
251 /* If the module is unused and if it is a plugin module... */
252 if( p_module->i_usage == 0 && !p_module->b_builtin )
254 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
256 p_module->i_unused_delay++;
260 intf_WarnMsg( 3, "module: hiding unused plugin module `%s'",
261 p_module->psz_name );
262 HideModule( p_module );
264 /* Break here, so that we only hide one module at a time */
270 /* We release the global lock */
271 vlc_mutex_unlock( &p_bank->lock );
272 #endif /* HAVE_DYNAMIC_PLUGINS */
277 /*****************************************************************************
278 * module_Need: return the best module function, given a capability list.
279 *****************************************************************************
280 * This function returns the module that best fits the asked capabilities.
281 *****************************************************************************/
282 module_t * module_Need( int i_capabilities, void *p_data )
285 module_t * p_bestmodule = NULL;
286 int i_score, i_totalscore, i_bestscore = 0;
289 /* We take the global lock */
290 vlc_mutex_lock( &p_bank->lock );
292 /* Parse the module list for capabilities and probe each of them */
293 for( p_module = p_bank->first ;
295 p_module = p_module->next )
297 /* Test that this module can do everything we need */
298 if( ( p_module->i_capabilities & i_capabilities ) == i_capabilities )
302 LockModule( p_module );
304 /* Parse all the requested capabilities and test them */
305 for( i_index = 0 ; (1 << i_index) <= i_capabilities ; i_index++ )
307 if( ( (1 << i_index) & i_capabilities ) )
309 i_score = ( (function_list_t *)p_module->p_functions)
310 [i_index].pf_probe( p_data );
314 i_totalscore += i_score;
323 /* If the high score was broken, we have a new champion */
324 if( i_totalscore > i_bestscore )
326 /* Keep the current module locked, but release the previous */
327 if( p_bestmodule != NULL )
329 UnlockModule( p_bestmodule );
332 /* This is the new best module */
333 i_bestscore = i_totalscore;
334 p_bestmodule = p_module;
338 /* This module wasn't interesting, unlock it and forget it */
339 UnlockModule( p_module );
344 /* We can release the global lock, module refcount was incremented */
345 vlc_mutex_unlock( &p_bank->lock );
347 if( p_bestmodule != NULL )
349 intf_WarnMsg( 3, "module: locking module `%s'",
350 p_bestmodule->psz_name );
353 /* Don't forget that the module is still locked if bestmodule != NULL */
354 return( p_bestmodule );
357 /*****************************************************************************
358 * module_Unneed: decrease the usage count of a module.
359 *****************************************************************************
360 * This function must be called by the thread that called module_Need, to
361 * decrease the reference count and allow for hiding of modules.
362 *****************************************************************************/
363 void module_Unneed( module_t * p_module )
365 /* We take the global lock */
366 vlc_mutex_lock( &p_bank->lock );
368 /* Just unlock the module - we can't do anything if it fails,
369 * so there is no need to check the return value. */
370 UnlockModule( p_module );
372 intf_WarnMsg( 3, "module: unlocking module `%s'", p_module->psz_name );
374 /* We release the global lock */
375 vlc_mutex_unlock( &p_bank->lock );
380 /*****************************************************************************
381 * Following functions are local.
382 *****************************************************************************/
384 #ifdef HAVE_DYNAMIC_PLUGINS
385 /*****************************************************************************
386 * AllocatePluginModule: load a module into memory and initialize it.
387 *****************************************************************************
388 * This function loads a dynamically loadable module and allocates a structure
389 * for its information data. The module can then be handled by module_Need,
390 * module_Unneed and HideModule. It can be removed by DeleteModule.
391 *****************************************************************************/
392 static int AllocatePluginModule( char * psz_filename )
394 module_t * p_module, * p_othermodule;
395 module_handle_t handle;
397 /* Try to dynamically load the module. */
398 if( module_load( psz_filename, &handle ) )
400 /* The plugin module couldn't be opened */
401 intf_WarnMsgImm( 3, "module warning: cannot open %s (%s)",
402 psz_filename, module_error() );
406 /* Now that we have successfully loaded the module, we can
407 * allocate a structure for it */
408 p_module = malloc( sizeof( module_t ) );
409 if( p_module == NULL )
411 intf_ErrMsg( "module error: can't allocate p_module" );
412 module_unload( handle );
416 /* We need to fill these since they may be needed by CallSymbol() */
417 p_module->is.plugin.psz_filename = psz_filename;
418 p_module->is.plugin.handle = handle;
420 /* Initialize the module : fill p_module->psz_name, etc. */
421 if( CallSymbol( p_module, "InitModule" ) != 0 )
423 /* We couldn't call InitModule() */
425 module_unload( handle );
429 /* Check that version numbers match */
430 if( strcmp( VERSION, p_module->psz_version ) )
433 module_unload( handle );
437 /* Check that we don't already have a module with this name */
438 for( p_othermodule = p_bank->first ;
439 p_othermodule != NULL ;
440 p_othermodule = p_othermodule->next )
442 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
445 module_unload( handle );
450 /* Activate the module : fill the capability structure, etc. */
451 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
453 /* We couldn't call ActivateModule() */
455 module_unload( handle );
459 /* We strdup() these entries so that they are still valid when the
460 * module is unloaded. */
461 p_module->is.plugin.psz_filename =
462 strdup( p_module->is.plugin.psz_filename );
463 p_module->psz_name = strdup( p_module->psz_name );
464 p_module->psz_longname = strdup( p_module->psz_longname );
465 p_module->psz_version = strdup( p_module->psz_version );
466 if( p_module->is.plugin.psz_filename == NULL
467 || p_module->psz_name == NULL
468 || p_module->psz_longname == NULL
469 || p_module->psz_version == NULL )
471 intf_ErrMsg( "module error: can't duplicate strings" );
472 free( p_module->is.plugin.psz_filename );
473 free( p_module->psz_name );
474 free( p_module->psz_longname );
475 free( p_module->psz_version );
477 module_unload( handle );
481 /* Everything worked fine ! The module is ready to be added to the list. */
482 p_module->i_usage = 0;
483 p_module->i_unused_delay = 0;
485 p_module->b_builtin = 0;
487 /* Link module into the linked list */
488 if( p_bank->first != NULL )
490 p_bank->first->prev = p_module;
492 p_module->next = p_bank->first;
493 p_module->prev = NULL;
494 p_bank->first = p_module;
496 /* Immediate message so that a slow module doesn't make the user wait */
497 intf_WarnMsgImm( 2, "module: plugin module `%s', %s",
498 p_module->psz_name, p_module->psz_longname );
502 #endif /* HAVE_DYNAMIC_PLUGINS */
504 /*****************************************************************************
505 * AllocateBuiltinModule: initialize a built-in module.
506 *****************************************************************************
507 * This function registers a built-in module and allocates a structure
508 * for its information data. The module can then be handled by module_Need,
509 * module_Unneed and HideModule. It can be removed by DeleteModule.
510 *****************************************************************************/
511 static int AllocateBuiltinModule( int ( *pf_init ) ( module_t * ),
512 int ( *pf_activate ) ( module_t * ),
513 int ( *pf_deactivate ) ( module_t * ) )
515 module_t * p_module, * p_othermodule;
517 /* Now that we have successfully loaded the module, we can
518 * allocate a structure for it */
519 p_module = malloc( sizeof( module_t ) );
520 if( p_module == NULL )
522 intf_ErrMsg( "module error: can't allocate p_module" );
526 /* Initialize the module : fill p_module->psz_name, etc. */
527 if( pf_init( p_module ) != 0 )
529 /* With a well-written module we shouldn't have to print an
530 * additional error message here, but just make sure. */
531 intf_ErrMsg( "module error: failed calling init in builtin module" );
536 /* Check that version numbers match */
537 if( strcmp( VERSION, p_module->psz_version ) )
543 /* Check that we don't already have a module with this name */
544 for( p_othermodule = p_bank->first ;
545 p_othermodule != NULL ;
546 p_othermodule = p_othermodule->next )
548 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
555 if( pf_activate( p_module ) != 0 )
557 /* With a well-written module we shouldn't have to print an
558 * additional error message here, but just make sure. */
559 intf_ErrMsg( "module error: failed calling activate "
560 "in builtin module" );
565 /* We strdup() these entries so that they are still valid when the
566 * module is unloaded. */
567 p_module->psz_name = strdup( p_module->psz_name );
568 p_module->psz_longname = strdup( p_module->psz_longname );
569 p_module->psz_version = strdup( p_module->psz_version );
570 if( p_module->psz_name == NULL
571 || p_module->psz_longname == NULL
572 || p_module->psz_version == NULL )
574 intf_ErrMsg( "module error: can't duplicate strings" );
575 free( p_module->psz_name );
576 free( p_module->psz_longname );
577 free( p_module->psz_version );
582 /* Everything worked fine ! The module is ready to be added to the list. */
583 p_module->i_usage = 0;
584 p_module->i_unused_delay = 0;
586 p_module->b_builtin = 1;
587 p_module->is.builtin.pf_deactivate = pf_deactivate;
589 /* Link module into the linked list */
590 if( p_bank->first != NULL )
592 p_bank->first->prev = p_module;
594 p_module->next = p_bank->first;
595 p_module->prev = NULL;
596 p_bank->first = p_module;
598 /* Immediate message so that a slow module doesn't make the user wait */
599 intf_WarnMsgImm( 2, "module: builtin module `%s', %s",
600 p_module->psz_name, p_module->psz_longname );
605 /*****************************************************************************
606 * DeleteModule: delete a module and its structure.
607 *****************************************************************************
608 * This function can only be called if i_usage <= 0.
609 *****************************************************************************/
610 static int DeleteModule( module_t * p_module )
612 /* If the module is not in use but is still in memory, we first have
613 * to hide it and remove it from memory before we can free the
615 if( p_module->b_builtin )
617 if( p_module->i_usage != 0 )
619 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
620 " usage %i", p_module->psz_name, p_module->i_usage );
625 /* We deactivate the module now. */
626 p_module->is.builtin.pf_deactivate( p_module );
629 #ifdef HAVE_DYNAMIC_PLUGINS
632 if( p_module->i_usage >= 1 )
634 intf_ErrMsg( "module error: trying to free module `%s' which is"
635 " still in use", p_module->psz_name );
639 /* Two possibilities here: i_usage == -1 and the module is already
640 * unloaded, we can continue, or i_usage == 0, and we have to hide
641 * the module before going on. */
642 if( p_module->i_usage == 0 )
644 if( HideModule( p_module ) != 0 )
652 /* Unlink the module from the linked list. */
653 if( p_module == p_bank->first )
655 p_bank->first = p_module->next;
658 if( p_module->prev != NULL )
660 p_module->prev->next = p_module->next;
663 if( p_module->next != NULL )
665 p_module->next->prev = p_module->prev;
668 /* We free the structures that we strdup()ed in Allocate*Module(). */
669 #ifdef HAVE_DYNAMIC_PLUGINS
670 if( !p_module->b_builtin )
672 free( p_module->is.plugin.psz_filename );
675 free( p_module->psz_name );
676 free( p_module->psz_longname );
677 free( p_module->psz_version );
684 /*****************************************************************************
685 * LockModule: increase the usage count of a module and load it if needed.
686 *****************************************************************************
687 * This function has to be called before a thread starts using a module. If
688 * the module is already loaded, we just increase its usage count. If it isn't
689 * loaded, we have to dynamically open it and initialize it.
690 * If you successfully call LockModule() at any moment, be careful to call
691 * UnlockModule() when you don't need it anymore.
692 *****************************************************************************/
693 static int LockModule( module_t * p_module )
695 if( p_module->i_usage >= 0 )
697 /* This module is already loaded and activated, we can return */
702 if( p_module->b_builtin )
704 /* A built-in module should always have a refcount >= 0 ! */
705 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
706 p_module->psz_name, p_module->i_usage );
710 #ifdef HAVE_DYNAMIC_PLUGINS
711 if( p_module->i_usage != -1 )
713 /* This shouldn't happen. Ever. We have serious problems here. */
714 intf_ErrMsg( "module error: plugin module `%s' has refcount %i",
715 p_module->psz_name, p_module->i_usage );
719 /* i_usage == -1, which means that the module isn't in memory */
720 if( module_load( p_module->is.plugin.psz_filename,
721 &p_module->is.plugin.handle ) )
723 /* The plugin module couldn't be opened */
724 intf_ErrMsg( "module error: cannot open %s (%s)",
725 p_module->is.plugin.psz_filename, module_error() );
729 /* FIXME: what to do if the guy modified the plugin while it was
730 * unloaded ? It makes XMMS crash nastily, perhaps we should try
731 * to be a bit more clever here. */
733 /* Activate the module : fill the capability structure, etc. */
734 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
736 /* We couldn't call ActivateModule() -- looks nasty, but
737 * we can't do much about it. Just try to unload module. */
738 module_unload( p_module->is.plugin.handle );
739 p_module->i_usage = -1;
743 /* Everything worked fine ! The module is ready to be used */
744 p_module->i_usage = 1;
745 #endif /* HAVE_DYNAMIC_PLUGINS */
750 /*****************************************************************************
751 * UnlockModule: decrease the usage count of a module.
752 *****************************************************************************
753 * We decrease the usage count of a module so that we know when a module
754 * becomes unused and can be hidden.
755 *****************************************************************************/
756 static int UnlockModule( module_t * p_module )
758 if( p_module->i_usage <= 0 )
760 /* This shouldn't happen. Ever. We have serious problems here. */
761 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
762 " which isn't even in use", p_module->psz_name );
766 /* This module is still in use, we can return */
768 p_module->i_unused_delay = 0;
773 #ifdef HAVE_DYNAMIC_PLUGINS
774 /*****************************************************************************
775 * HideModule: remove a module from memory but keep its structure.
776 *****************************************************************************
777 * This function can only be called if i_usage == 0. It will make a call
778 * to the module's inner DeactivateModule() symbol, and then unload it
779 * from memory. A call to module_Need() will automagically load it again.
780 *****************************************************************************/
781 static int HideModule( module_t * p_module )
783 if( p_module->b_builtin )
785 /* A built-in module should never be hidden. */
786 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
787 p_module->psz_name );
791 if( p_module->i_usage >= 1 )
793 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
794 " in use", p_module->psz_name );
798 if( p_module->i_usage <= -1 )
800 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
801 " hidden", p_module->psz_name );
805 /* Deactivate the module : free the capability structure, etc. */
806 if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
808 /* We couldn't call DeactivateModule() -- looks nasty, but
809 * we can't do much about it. Just try to unload module anyway. */
810 module_unload( p_module->is.plugin.handle );
811 p_module->i_usage = -1;
815 /* Everything worked fine, we can safely unload the module. */
816 module_unload( p_module->is.plugin.handle );
817 p_module->i_usage = -1;
822 /*****************************************************************************
823 * CallSymbol: calls a module symbol.
824 *****************************************************************************
825 * This function calls a symbol given its name and a module structure. The
826 * symbol MUST refer to a function returning int and taking a module_t* as
828 *****************************************************************************/
829 static int CallSymbol( module_t * p_module, char * psz_name )
831 typedef int ( symbol_t ) ( module_t * p_module );
834 /* Try to resolve the symbol */
835 p_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
839 /* We couldn't load the symbol */
840 intf_WarnMsg( 3, "module warning: "
841 "cannot find symbol %s in module %s (%s)",
842 psz_name, p_module->is.plugin.psz_filename,
847 /* We can now try to call the symbol */
848 if( p_symbol( p_module ) != 0 )
850 /* With a well-written module we shouldn't have to print an
851 * additional error message here, but just make sure. */
852 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
853 psz_name, p_module->is.plugin.psz_filename );
857 /* Everything worked fine, we can return */
860 #endif /* HAVE_DYNAMIC_PLUGINS */