1 /*****************************************************************************
2 * modules.c : Built-in and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.29 2001/05/06 04:32:02 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( void )
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_module_bank->first = NULL;
106 vlc_mutex_init( &p_module_bank->lock );
109 * Check all the built-in modules
111 intf_WarnMsg( 2, "module: checking built-in modules" );
113 ALLOCATE_ALL_BUILTINS();
116 * Check all the plugin modules we can find
118 #ifdef HAVE_DYNAMIC_PLUGINS
119 intf_WarnMsg( 2, "module: checking plugin modules" );
121 for( ; *ppsz_path != NULL ; ppsz_path++ )
123 /* Store strlen(*ppsz_path) for later use. */
124 int i_dirlen = strlen( *ppsz_path );
126 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
128 /* Under BeOS, we need to add beos_GetProgramPath() to access
129 * files under the current directory */
130 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
132 i_dirlen += i_vlclen + 2;
135 psz_fullpath = malloc( i_dirlen );
136 if( psz_fullpath == NULL )
140 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
145 psz_fullpath = *ppsz_path;
148 intf_WarnMsgImm( 3, "module: browsing `%s'", psz_fullpath );
150 if( (dir = opendir( psz_fullpath )) )
152 /* Parse the directory and try to load all files it contains. */
153 while( (file = readdir( dir )) )
155 int i_filelen = strlen( file->d_name );
157 /* We only load files ending with ".so" */
159 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
161 psz_file = malloc( i_dirlen + i_filelen + 2 );
162 if( psz_file == NULL )
166 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
168 /* We created a nice filename -- now we just try to load
169 * it as a plugin module. */
170 AllocatePluginModule( psz_file );
172 /* We don't care if the allocation succeeded */
177 /* Close the directory if successfully opened */
181 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
184 free( psz_fullpath );
188 #endif /* HAVE_DYNAMIC_PLUGINS */
190 intf_WarnMsg( 1, "module: module bank initialized" );
195 /*****************************************************************************
196 * module_EndBank: empty the module bank.
197 *****************************************************************************
198 * This function unloads all unused plugin modules and empties the module
199 * bank in case of success.
200 *****************************************************************************/
201 void module_EndBank( void )
205 while( p_module_bank->first != NULL )
207 if( DeleteModule( p_module_bank->first ) )
209 /* Module deletion failed */
210 intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
211 p_module_bank->first->psz_name );
213 /* We just free the module by hand. Niahahahahaha. */
214 p_next = p_module_bank->first->next;
215 free(p_module_bank->first);
216 p_module_bank->first = p_next;
220 /* Destroy the lock */
221 vlc_mutex_destroy( &p_module_bank->lock );
226 /*****************************************************************************
227 * module_ResetBank: reset the module bank.
228 *****************************************************************************
229 * This function resets the module bank by unloading all unused plugin
231 *****************************************************************************/
232 void module_ResetBank( void )
234 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
238 /*****************************************************************************
239 * module_ManageBank: manage the module bank.
240 *****************************************************************************
241 * This function parses the module bank and hides modules that have been
242 * unused for a while.
243 *****************************************************************************/
244 void module_ManageBank( void )
246 #ifdef HAVE_DYNAMIC_PLUGINS
249 /* We take the global lock */
250 vlc_mutex_lock( &p_module_bank->lock );
252 /* Parse the module list to see if any modules need to be unloaded */
253 for( p_module = p_module_bank->first ;
255 p_module = p_module->next )
257 /* If the module is unused and if it is a plugin module... */
258 if( p_module->i_usage == 0 && !p_module->b_builtin )
260 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
262 p_module->i_unused_delay++;
266 intf_WarnMsg( 3, "module: hiding unused plugin module `%s'",
267 p_module->psz_name );
268 HideModule( p_module );
270 /* Break here, so that we only hide one module at a time */
276 /* We release the global lock */
277 vlc_mutex_unlock( &p_module_bank->lock );
278 #endif /* HAVE_DYNAMIC_PLUGINS */
283 /*****************************************************************************
284 * module_Need: return the best module function, given a capability list.
285 *****************************************************************************
286 * This function returns the module that best fits the asked capabilities.
287 *****************************************************************************/
288 module_t * module_Need( int i_capabilities, void *p_data )
291 module_t * p_bestmodule = NULL;
292 int i_score, i_totalscore, i_bestscore = 0;
295 /* We take the global lock */
296 vlc_mutex_lock( &p_module_bank->lock );
298 /* Parse the module list for capabilities and probe each of them */
299 for( p_module = p_module_bank->first ;
301 p_module = p_module->next )
303 /* Test that this module can do everything we need */
304 if( ( p_module->i_capabilities & i_capabilities ) == i_capabilities )
308 LockModule( p_module );
310 /* Parse all the requested capabilities and test them */
311 for( i_index = 0 ; (1 << i_index) <= i_capabilities ; i_index++ )
313 if( ( (1 << i_index) & i_capabilities ) )
315 i_score = ( (function_list_t *)p_module->p_functions)
316 [i_index].pf_probe( p_data );
320 i_totalscore += i_score;
329 /* If the high score was broken, we have a new champion */
330 if( i_totalscore > i_bestscore )
332 /* Keep the current module locked, but release the previous */
333 if( p_bestmodule != NULL )
335 UnlockModule( p_bestmodule );
338 /* This is the new best module */
339 i_bestscore = i_totalscore;
340 p_bestmodule = p_module;
344 /* This module wasn't interesting, unlock it and forget it */
345 UnlockModule( p_module );
350 /* We can release the global lock, module refcount was incremented */
351 vlc_mutex_unlock( &p_module_bank->lock );
353 if( p_bestmodule != NULL )
355 intf_WarnMsg( 3, "module: locking module `%s'",
356 p_bestmodule->psz_name );
359 /* Don't forget that the module is still locked if bestmodule != NULL */
360 return( p_bestmodule );
363 /*****************************************************************************
364 * module_Unneed: decrease the usage count of a module.
365 *****************************************************************************
366 * This function must be called by the thread that called module_Need, to
367 * decrease the reference count and allow for hiding of modules.
368 *****************************************************************************/
369 void module_Unneed( module_t * p_module )
371 /* We take the global lock */
372 vlc_mutex_lock( &p_module_bank->lock );
374 /* Just unlock the module - we can't do anything if it fails,
375 * so there is no need to check the return value. */
376 UnlockModule( p_module );
378 intf_WarnMsg( 3, "module: unlocking module `%s'", p_module->psz_name );
380 /* We release the global lock */
381 vlc_mutex_unlock( &p_module_bank->lock );
386 /*****************************************************************************
387 * Following functions are local.
388 *****************************************************************************/
390 #ifdef HAVE_DYNAMIC_PLUGINS
391 /*****************************************************************************
392 * AllocatePluginModule: load a module into memory and initialize it.
393 *****************************************************************************
394 * This function loads a dynamically loadable module and allocates a structure
395 * for its information data. The module can then be handled by module_Need,
396 * module_Unneed and HideModule. It can be removed by DeleteModule.
397 *****************************************************************************/
398 static int AllocatePluginModule( char * psz_filename )
400 module_t * p_module, * p_othermodule;
401 module_handle_t handle;
403 /* Try to dynamically load the module. */
404 if( module_load( psz_filename, &handle ) )
406 /* The plugin module couldn't be opened */
407 intf_WarnMsgImm( 3, "module warning: cannot open %s (%s)",
408 psz_filename, module_error() );
412 /* Now that we have successfully loaded the module, we can
413 * allocate a structure for it */
414 p_module = malloc( sizeof( module_t ) );
415 if( p_module == NULL )
417 intf_ErrMsg( "module error: can't allocate p_module" );
418 module_unload( handle );
422 /* We need to fill these since they may be needed by CallSymbol() */
423 p_module->is.plugin.psz_filename = psz_filename;
424 p_module->is.plugin.handle = handle;
426 /* Initialize the module : fill p_module->psz_name, etc. */
427 if( CallSymbol( p_module, "InitModule" ) != 0 )
429 /* We couldn't call InitModule() */
431 module_unload( handle );
435 /* Check that version numbers match */
436 if( strcmp( VERSION, p_module->psz_version ) )
439 module_unload( handle );
443 /* Check that we don't already have a module with this name */
444 for( p_othermodule = p_module_bank->first ;
445 p_othermodule != NULL ;
446 p_othermodule = p_othermodule->next )
448 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
451 module_unload( handle );
456 /* Activate the module : fill the capability structure, etc. */
457 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
459 /* We couldn't call ActivateModule() */
461 module_unload( handle );
465 /* We strdup() these entries so that they are still valid when the
466 * module is unloaded. */
467 p_module->is.plugin.psz_filename =
468 strdup( p_module->is.plugin.psz_filename );
469 p_module->psz_name = strdup( p_module->psz_name );
470 p_module->psz_longname = strdup( p_module->psz_longname );
471 p_module->psz_version = strdup( p_module->psz_version );
472 if( p_module->is.plugin.psz_filename == NULL
473 || p_module->psz_name == NULL
474 || p_module->psz_longname == NULL
475 || p_module->psz_version == NULL )
477 intf_ErrMsg( "module error: can't duplicate strings" );
478 free( p_module->is.plugin.psz_filename );
479 free( p_module->psz_name );
480 free( p_module->psz_longname );
481 free( p_module->psz_version );
483 module_unload( handle );
487 /* Everything worked fine ! The module is ready to be added to the list. */
488 p_module->i_usage = 0;
489 p_module->i_unused_delay = 0;
491 p_module->b_builtin = 0;
493 /* Link module into the linked list */
494 if( p_module_bank->first != NULL )
496 p_module_bank->first->prev = p_module;
498 p_module->next = p_module_bank->first;
499 p_module->prev = NULL;
500 p_module_bank->first = p_module;
502 /* Immediate message so that a slow module doesn't make the user wait */
503 intf_WarnMsgImm( 2, "module: plugin module `%s', %s",
504 p_module->psz_name, p_module->psz_longname );
508 #endif /* HAVE_DYNAMIC_PLUGINS */
510 /*****************************************************************************
511 * AllocateBuiltinModule: initialize a built-in module.
512 *****************************************************************************
513 * This function registers a built-in module and allocates a structure
514 * for its information data. The module can then be handled by module_Need,
515 * module_Unneed and HideModule. It can be removed by DeleteModule.
516 *****************************************************************************/
517 static int AllocateBuiltinModule( int ( *pf_init ) ( module_t * ),
518 int ( *pf_activate ) ( module_t * ),
519 int ( *pf_deactivate ) ( module_t * ) )
521 module_t * p_module, * p_othermodule;
523 /* Now that we have successfully loaded the module, we can
524 * allocate a structure for it */
525 p_module = malloc( sizeof( module_t ) );
526 if( p_module == NULL )
528 intf_ErrMsg( "module error: can't allocate p_module" );
532 /* Initialize the module : fill p_module->psz_name, etc. */
533 if( pf_init( p_module ) != 0 )
535 /* With a well-written module we shouldn't have to print an
536 * additional error message here, but just make sure. */
537 intf_ErrMsg( "module error: failed calling init in builtin module" );
542 /* Check that version numbers match */
543 if( strcmp( VERSION, p_module->psz_version ) )
549 /* Check that we don't already have a module with this name */
550 for( p_othermodule = p_module_bank->first ;
551 p_othermodule != NULL ;
552 p_othermodule = p_othermodule->next )
554 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
561 if( pf_activate( p_module ) != 0 )
563 /* With a well-written module we shouldn't have to print an
564 * additional error message here, but just make sure. */
565 intf_ErrMsg( "module error: failed calling activate "
566 "in builtin module" );
571 /* We strdup() these entries so that they are still valid when the
572 * module is unloaded. */
573 p_module->psz_name = strdup( p_module->psz_name );
574 p_module->psz_longname = strdup( p_module->psz_longname );
575 p_module->psz_version = strdup( p_module->psz_version );
576 if( p_module->psz_name == NULL
577 || p_module->psz_longname == NULL
578 || p_module->psz_version == NULL )
580 intf_ErrMsg( "module error: can't duplicate strings" );
581 free( p_module->psz_name );
582 free( p_module->psz_longname );
583 free( p_module->psz_version );
588 /* Everything worked fine ! The module is ready to be added to the list. */
589 p_module->i_usage = 0;
590 p_module->i_unused_delay = 0;
592 p_module->b_builtin = 1;
593 p_module->is.builtin.pf_deactivate = pf_deactivate;
595 /* Link module into the linked list */
596 if( p_module_bank->first != NULL )
598 p_module_bank->first->prev = p_module;
600 p_module->next = p_module_bank->first;
601 p_module->prev = NULL;
602 p_module_bank->first = p_module;
604 /* Immediate message so that a slow module doesn't make the user wait */
605 intf_WarnMsgImm( 2, "module: builtin module `%s', %s",
606 p_module->psz_name, p_module->psz_longname );
611 /*****************************************************************************
612 * DeleteModule: delete a module and its structure.
613 *****************************************************************************
614 * This function can only be called if i_usage <= 0.
615 *****************************************************************************/
616 static int DeleteModule( module_t * p_module )
618 /* If the module is not in use but is still in memory, we first have
619 * to hide it and remove it from memory before we can free the
621 if( p_module->b_builtin )
623 if( p_module->i_usage != 0 )
625 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
626 " usage %i", p_module->psz_name, p_module->i_usage );
631 /* We deactivate the module now. */
632 p_module->is.builtin.pf_deactivate( p_module );
635 #ifdef HAVE_DYNAMIC_PLUGINS
638 if( p_module->i_usage >= 1 )
640 intf_ErrMsg( "module error: trying to free module `%s' which is"
641 " still in use", p_module->psz_name );
645 /* Two possibilities here: i_usage == -1 and the module is already
646 * unloaded, we can continue, or i_usage == 0, and we have to hide
647 * the module before going on. */
648 if( p_module->i_usage == 0 )
650 if( HideModule( p_module ) != 0 )
658 /* Unlink the module from the linked list. */
659 if( p_module == p_module_bank->first )
661 p_module_bank->first = p_module->next;
664 if( p_module->prev != NULL )
666 p_module->prev->next = p_module->next;
669 if( p_module->next != NULL )
671 p_module->next->prev = p_module->prev;
674 /* We free the structures that we strdup()ed in Allocate*Module(). */
675 #ifdef HAVE_DYNAMIC_PLUGINS
676 if( !p_module->b_builtin )
678 free( p_module->is.plugin.psz_filename );
681 free( p_module->psz_name );
682 free( p_module->psz_longname );
683 free( p_module->psz_version );
690 /*****************************************************************************
691 * LockModule: increase the usage count of a module and load it if needed.
692 *****************************************************************************
693 * This function has to be called before a thread starts using a module. If
694 * the module is already loaded, we just increase its usage count. If it isn't
695 * loaded, we have to dynamically open it and initialize it.
696 * If you successfully call LockModule() at any moment, be careful to call
697 * UnlockModule() when you don't need it anymore.
698 *****************************************************************************/
699 static int LockModule( module_t * p_module )
701 if( p_module->i_usage >= 0 )
703 /* This module is already loaded and activated, we can return */
708 if( p_module->b_builtin )
710 /* A built-in module should always have a refcount >= 0 ! */
711 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
712 p_module->psz_name, p_module->i_usage );
716 #ifdef HAVE_DYNAMIC_PLUGINS
717 if( p_module->i_usage != -1 )
719 /* This shouldn't happen. Ever. We have serious problems here. */
720 intf_ErrMsg( "module error: plugin module `%s' has refcount %i",
721 p_module->psz_name, p_module->i_usage );
725 /* i_usage == -1, which means that the module isn't in memory */
726 if( module_load( p_module->is.plugin.psz_filename,
727 &p_module->is.plugin.handle ) )
729 /* The plugin module couldn't be opened */
730 intf_ErrMsg( "module error: cannot open %s (%s)",
731 p_module->is.plugin.psz_filename, module_error() );
735 /* FIXME: what to do if the guy modified the plugin while it was
736 * unloaded ? It makes XMMS crash nastily, perhaps we should try
737 * to be a bit more clever here. */
739 /* Activate the module : fill the capability structure, etc. */
740 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
742 /* We couldn't call ActivateModule() -- looks nasty, but
743 * we can't do much about it. Just try to unload module. */
744 module_unload( p_module->is.plugin.handle );
745 p_module->i_usage = -1;
749 /* Everything worked fine ! The module is ready to be used */
750 p_module->i_usage = 1;
751 #endif /* HAVE_DYNAMIC_PLUGINS */
756 /*****************************************************************************
757 * UnlockModule: decrease the usage count of a module.
758 *****************************************************************************
759 * We decrease the usage count of a module so that we know when a module
760 * becomes unused and can be hidden.
761 *****************************************************************************/
762 static int UnlockModule( module_t * p_module )
764 if( p_module->i_usage <= 0 )
766 /* This shouldn't happen. Ever. We have serious problems here. */
767 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
768 " which isn't even in use", p_module->psz_name );
772 /* This module is still in use, we can return */
774 p_module->i_unused_delay = 0;
779 #ifdef HAVE_DYNAMIC_PLUGINS
780 /*****************************************************************************
781 * HideModule: remove a module from memory but keep its structure.
782 *****************************************************************************
783 * This function can only be called if i_usage == 0. It will make a call
784 * to the module's inner DeactivateModule() symbol, and then unload it
785 * from memory. A call to module_Need() will automagically load it again.
786 *****************************************************************************/
787 static int HideModule( module_t * p_module )
789 if( p_module->b_builtin )
791 /* A built-in module should never be hidden. */
792 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
793 p_module->psz_name );
797 if( p_module->i_usage >= 1 )
799 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
800 " in use", p_module->psz_name );
804 if( p_module->i_usage <= -1 )
806 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
807 " hidden", p_module->psz_name );
811 /* Deactivate the module : free the capability structure, etc. */
812 if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
814 /* We couldn't call DeactivateModule() -- looks nasty, but
815 * we can't do much about it. Just try to unload module anyway. */
816 module_unload( p_module->is.plugin.handle );
817 p_module->i_usage = -1;
821 /* Everything worked fine, we can safely unload the module. */
822 module_unload( p_module->is.plugin.handle );
823 p_module->i_usage = -1;
828 /*****************************************************************************
829 * CallSymbol: calls a module symbol.
830 *****************************************************************************
831 * This function calls a symbol given its name and a module structure. The
832 * symbol MUST refer to a function returning int and taking a module_t* as
834 *****************************************************************************/
835 static int CallSymbol( module_t * p_module, char * psz_name )
837 typedef int ( symbol_t ) ( module_t * p_module );
840 /* Try to resolve the symbol */
841 p_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
845 /* We couldn't load the symbol */
846 intf_WarnMsg( 3, "module warning: "
847 "cannot find symbol %s in module %s (%s)",
848 psz_name, p_module->is.plugin.psz_filename,
853 /* We can now try to call the symbol */
854 if( p_symbol( p_module ) != 0 )
856 /* With a well-written module we shouldn't have to print an
857 * additional error message here, but just make sure. */
858 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
859 psz_name, p_module->is.plugin.psz_filename );
863 /* Everything worked fine, we can return */
866 #endif /* HAVE_DYNAMIC_PLUGINS */