1 /*****************************************************************************
2 * modules.c : Built-in and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.31 2001/05/30 17:03:12 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"
64 #include "stream_control.h"
65 #include "input_ext-intf.h"
68 #include "video_output.h"
70 #include "audio_output.h"
72 #include "interface.h"
74 #include "intf_playlist.h"
76 #ifdef HAVE_DYNAMIC_PLUGINS
77 # include "modules_core.h"
79 #include "modules_builtin.h"
80 #include "modules_export.h"
84 /*****************************************************************************
86 *****************************************************************************/
87 #ifdef HAVE_DYNAMIC_PLUGINS
88 static int AllocatePluginModule ( char * );
90 static int AllocateBuiltinModule( int ( * ) ( module_t * ),
91 int ( * ) ( module_t * ),
92 int ( * ) ( module_t * ) );
93 static int DeleteModule ( module_t * );
94 static int LockModule ( module_t * );
95 static int UnlockModule ( module_t * );
96 #ifdef HAVE_DYNAMIC_PLUGINS
97 static int HideModule ( module_t * );
98 static int CallSymbol ( module_t *, char * );
101 static module_symbols_t symbols;
103 /*****************************************************************************
104 * module_InitBank: create the module bank.
105 *****************************************************************************
106 * This function creates a module bank structure and fills it with the
107 * built-in modules, as well as all the plugin modules it can find.
108 *****************************************************************************/
109 void module_InitBank( void )
111 #ifdef HAVE_DYNAMIC_PLUGINS
112 static char * path[] = { ".", "lib", PLUGIN_PATH, NULL, NULL };
114 char ** ppsz_path = path;
117 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
118 char * psz_vlcpath = system_GetProgramPath();
119 int i_vlclen = strlen( psz_vlcpath );
120 boolean_t b_notinroot;
123 struct dirent * file;
124 #endif /* HAVE_DYNAMIC_PLUGINS */
126 p_module_bank->first = NULL;
127 vlc_mutex_init( &p_module_bank->lock );
130 * Store the symbols to be exported
132 STORE_SYMBOLS( &symbols );
135 * Check all the built-in modules
137 intf_WarnMsg( 2, "module: checking built-in modules" );
139 ALLOCATE_ALL_BUILTINS();
142 * Check all the plugin modules we can find
144 #ifdef HAVE_DYNAMIC_PLUGINS
145 intf_WarnMsg( 2, "module: checking plugin modules" );
147 for( ; *ppsz_path != NULL ; ppsz_path++ )
149 /* Store strlen(*ppsz_path) for later use. */
150 int i_dirlen = strlen( *ppsz_path );
152 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
154 /* Under BeOS, we need to add beos_GetProgramPath() to access
155 * files under the current directory */
156 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
158 i_dirlen += i_vlclen + 2;
161 psz_fullpath = malloc( i_dirlen );
162 if( psz_fullpath == NULL )
166 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
171 psz_fullpath = *ppsz_path;
174 intf_WarnMsgImm( 1, "module: browsing `%s'", psz_fullpath );
176 if( (dir = opendir( psz_fullpath )) )
178 /* Parse the directory and try to load all files it contains. */
179 while( (file = readdir( dir )) )
181 int i_filelen = strlen( file->d_name );
183 /* We only load files ending with ".so" */
185 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
187 psz_file = malloc( i_dirlen + i_filelen + 2 );
188 if( psz_file == NULL )
192 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
194 /* We created a nice filename -- now we just try to load
195 * it as a plugin module. */
196 AllocatePluginModule( psz_file );
198 /* We don't care if the allocation succeeded */
203 /* Close the directory if successfully opened */
207 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
210 free( psz_fullpath );
214 #endif /* HAVE_DYNAMIC_PLUGINS */
216 intf_WarnMsg( 3, "module: module bank initialized" );
221 /*****************************************************************************
222 * module_EndBank: empty the module bank.
223 *****************************************************************************
224 * This function unloads all unused plugin modules and empties the module
225 * bank in case of success.
226 *****************************************************************************/
227 void module_EndBank( void )
231 while( p_module_bank->first != NULL )
233 if( DeleteModule( p_module_bank->first ) )
235 /* Module deletion failed */
236 intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
237 p_module_bank->first->psz_name );
239 /* We just free the module by hand. Niahahahahaha. */
240 p_next = p_module_bank->first->next;
241 free(p_module_bank->first);
242 p_module_bank->first = p_next;
246 /* Destroy the lock */
247 vlc_mutex_destroy( &p_module_bank->lock );
252 /*****************************************************************************
253 * module_ResetBank: reset the module bank.
254 *****************************************************************************
255 * This function resets the module bank by unloading all unused plugin
257 *****************************************************************************/
258 void module_ResetBank( void )
260 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
264 /*****************************************************************************
265 * module_ManageBank: manage the module bank.
266 *****************************************************************************
267 * This function parses the module bank and hides modules that have been
268 * unused for a while.
269 *****************************************************************************/
270 void module_ManageBank( void )
272 #ifdef HAVE_DYNAMIC_PLUGINS
275 /* We take the global lock */
276 vlc_mutex_lock( &p_module_bank->lock );
278 /* Parse the module list to see if any modules need to be unloaded */
279 for( p_module = p_module_bank->first ;
281 p_module = p_module->next )
283 /* If the module is unused and if it is a plugin module... */
284 if( p_module->i_usage == 0 && !p_module->b_builtin )
286 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
288 p_module->i_unused_delay++;
292 intf_WarnMsg( 1, "module: hiding unused plugin module `%s'",
293 p_module->psz_name );
294 HideModule( p_module );
296 /* Break here, so that we only hide one module at a time */
302 /* We release the global lock */
303 vlc_mutex_unlock( &p_module_bank->lock );
304 #endif /* HAVE_DYNAMIC_PLUGINS */
309 /*****************************************************************************
310 * module_Need: return the best module function, given a capability list.
311 *****************************************************************************
312 * This function returns the module that best fits the asked capabilities.
313 *****************************************************************************/
314 module_t * module_Need( int i_capabilities, void *p_data )
317 module_t * p_bestmodule = NULL;
318 int i_score, i_totalscore, i_bestscore = 0;
321 /* We take the global lock */
322 vlc_mutex_lock( &p_module_bank->lock );
324 /* Parse the module list for capabilities and probe each of them */
325 for( p_module = p_module_bank->first ;
327 p_module = p_module->next )
329 /* Test that this module can do everything we need */
330 if( ( p_module->i_capabilities & i_capabilities ) == i_capabilities )
334 LockModule( p_module );
336 /* Parse all the requested capabilities and test them */
337 for( i_index = 0 ; (1 << i_index) <= i_capabilities ; i_index++ )
339 if( ( (1 << i_index) & i_capabilities ) )
341 i_score = ( (function_list_t *)p_module->p_functions)
342 [i_index].pf_probe( p_data );
346 i_totalscore += i_score;
355 /* If the high score was broken, we have a new champion */
356 if( i_totalscore > i_bestscore )
358 /* Keep the current module locked, but release the previous */
359 if( p_bestmodule != NULL )
361 UnlockModule( p_bestmodule );
364 /* This is the new best module */
365 i_bestscore = i_totalscore;
366 p_bestmodule = p_module;
370 /* This module wasn't interesting, unlock it and forget it */
371 UnlockModule( p_module );
376 /* We can release the global lock, module refcount was incremented */
377 vlc_mutex_unlock( &p_module_bank->lock );
379 if( p_bestmodule != NULL )
381 intf_WarnMsg( 1, "module: locking module `%s'",
382 p_bestmodule->psz_name );
385 /* Don't forget that the module is still locked if bestmodule != NULL */
386 return( p_bestmodule );
389 /*****************************************************************************
390 * module_Unneed: decrease the usage count of a module.
391 *****************************************************************************
392 * This function must be called by the thread that called module_Need, to
393 * decrease the reference count and allow for hiding of modules.
394 *****************************************************************************/
395 void module_Unneed( module_t * p_module )
397 /* We take the global lock */
398 vlc_mutex_lock( &p_module_bank->lock );
400 /* Just unlock the module - we can't do anything if it fails,
401 * so there is no need to check the return value. */
402 UnlockModule( p_module );
404 intf_WarnMsg( 1, "module: unlocking module `%s'", p_module->psz_name );
406 /* We release the global lock */
407 vlc_mutex_unlock( &p_module_bank->lock );
412 /*****************************************************************************
413 * Following functions are local.
414 *****************************************************************************/
416 #ifdef HAVE_DYNAMIC_PLUGINS
417 /*****************************************************************************
418 * AllocatePluginModule: load a module into memory and initialize it.
419 *****************************************************************************
420 * This function loads a dynamically loadable module and allocates a structure
421 * for its information data. The module can then be handled by module_Need,
422 * module_Unneed and HideModule. It can be removed by DeleteModule.
423 *****************************************************************************/
424 static int AllocatePluginModule( char * psz_filename )
426 module_t * p_module, * p_othermodule;
427 module_handle_t handle;
429 /* Try to dynamically load the module. */
430 if( module_load( psz_filename, &handle ) )
432 /* The plugin module couldn't be opened */
433 intf_WarnMsgImm( 1, "module warning: cannot open %s (%s)",
434 psz_filename, module_error() );
438 /* Now that we have successfully loaded the module, we can
439 * allocate a structure for it */
440 p_module = malloc( sizeof( module_t ) );
441 if( p_module == NULL )
443 intf_ErrMsg( "module error: can't allocate p_module" );
444 module_unload( handle );
448 /* We need to fill these since they may be needed by CallSymbol() */
449 p_module->is.plugin.psz_filename = psz_filename;
450 p_module->is.plugin.handle = handle;
451 p_module->p_symbols = &symbols;
453 /* Initialize the module : fill p_module->psz_name, etc. */
454 if( CallSymbol( p_module, "InitModule" ) != 0 )
456 /* We couldn't call InitModule() */
458 module_unload( handle );
462 /* Check that version numbers match */
463 if( strcmp( VERSION, p_module->psz_version ) )
466 module_unload( handle );
470 /* Check that we don't already have a module with this name */
471 for( p_othermodule = p_module_bank->first ;
472 p_othermodule != NULL ;
473 p_othermodule = p_othermodule->next )
475 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
478 module_unload( handle );
483 /* Activate the module : fill the capability structure, etc. */
484 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
486 /* We couldn't call ActivateModule() */
488 module_unload( handle );
492 /* We strdup() these entries so that they are still valid when the
493 * module is unloaded. */
494 p_module->is.plugin.psz_filename =
495 strdup( p_module->is.plugin.psz_filename );
496 p_module->psz_name = strdup( p_module->psz_name );
497 p_module->psz_longname = strdup( p_module->psz_longname );
498 p_module->psz_version = strdup( p_module->psz_version );
499 if( p_module->is.plugin.psz_filename == NULL
500 || p_module->psz_name == NULL
501 || p_module->psz_longname == NULL
502 || p_module->psz_version == NULL )
504 intf_ErrMsg( "module error: can't duplicate strings" );
505 free( p_module->is.plugin.psz_filename );
506 free( p_module->psz_name );
507 free( p_module->psz_longname );
508 free( p_module->psz_version );
510 module_unload( handle );
514 /* Everything worked fine ! The module is ready to be added to the list. */
515 p_module->i_usage = 0;
516 p_module->i_unused_delay = 0;
518 p_module->b_builtin = 0;
520 /* Link module into the linked list */
521 if( p_module_bank->first != NULL )
523 p_module_bank->first->prev = p_module;
525 p_module->next = p_module_bank->first;
526 p_module->prev = NULL;
527 p_module_bank->first = p_module;
529 /* Immediate message so that a slow module doesn't make the user wait */
530 intf_WarnMsgImm( 2, "module: plugin module `%s', %s",
531 p_module->psz_name, p_module->psz_longname );
535 #endif /* HAVE_DYNAMIC_PLUGINS */
537 /*****************************************************************************
538 * AllocateBuiltinModule: initialize a built-in module.
539 *****************************************************************************
540 * This function registers a built-in module and allocates a structure
541 * for its information data. The module can then be handled by module_Need,
542 * module_Unneed and HideModule. It can be removed by DeleteModule.
543 *****************************************************************************/
544 static int AllocateBuiltinModule( int ( *pf_init ) ( module_t * ),
545 int ( *pf_activate ) ( module_t * ),
546 int ( *pf_deactivate ) ( module_t * ) )
548 module_t * p_module, * p_othermodule;
550 /* Now that we have successfully loaded the module, we can
551 * allocate a structure for it */
552 p_module = malloc( sizeof( module_t ) );
553 if( p_module == NULL )
555 intf_ErrMsg( "module error: can't allocate p_module" );
559 /* Initialize the module : fill p_module->psz_name, etc. */
560 if( pf_init( p_module ) != 0 )
562 /* With a well-written module we shouldn't have to print an
563 * additional error message here, but just make sure. */
564 intf_ErrMsg( "module error: failed calling init in builtin module" );
569 /* Check that version numbers match */
570 if( strcmp( VERSION, p_module->psz_version ) )
576 /* Check that we don't already have a module with this name */
577 for( p_othermodule = p_module_bank->first ;
578 p_othermodule != NULL ;
579 p_othermodule = p_othermodule->next )
581 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
588 if( pf_activate( p_module ) != 0 )
590 /* With a well-written module we shouldn't have to print an
591 * additional error message here, but just make sure. */
592 intf_ErrMsg( "module error: failed calling activate "
593 "in builtin module" );
598 /* We strdup() these entries so that they are still valid when the
599 * module is unloaded. */
600 p_module->psz_name = strdup( p_module->psz_name );
601 p_module->psz_longname = strdup( p_module->psz_longname );
602 p_module->psz_version = strdup( p_module->psz_version );
603 if( p_module->psz_name == NULL
604 || p_module->psz_longname == NULL
605 || p_module->psz_version == NULL )
607 intf_ErrMsg( "module error: can't duplicate strings" );
608 free( p_module->psz_name );
609 free( p_module->psz_longname );
610 free( p_module->psz_version );
615 /* Everything worked fine ! The module is ready to be added to the list. */
616 p_module->i_usage = 0;
617 p_module->i_unused_delay = 0;
619 p_module->b_builtin = 1;
620 p_module->is.builtin.pf_deactivate = pf_deactivate;
622 /* Link module into the linked list */
623 if( p_module_bank->first != NULL )
625 p_module_bank->first->prev = p_module;
627 p_module->next = p_module_bank->first;
628 p_module->prev = NULL;
629 p_module_bank->first = p_module;
631 /* Immediate message so that a slow module doesn't make the user wait */
632 intf_WarnMsgImm( 2, "module: builtin module `%s', %s",
633 p_module->psz_name, p_module->psz_longname );
638 /*****************************************************************************
639 * DeleteModule: delete a module and its structure.
640 *****************************************************************************
641 * This function can only be called if i_usage <= 0.
642 *****************************************************************************/
643 static int DeleteModule( module_t * p_module )
645 /* If the module is not in use but is still in memory, we first have
646 * to hide it and remove it from memory before we can free the
648 if( p_module->b_builtin )
650 if( p_module->i_usage != 0 )
652 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
653 " usage %i", p_module->psz_name, p_module->i_usage );
658 /* We deactivate the module now. */
659 p_module->is.builtin.pf_deactivate( p_module );
662 #ifdef HAVE_DYNAMIC_PLUGINS
665 if( p_module->i_usage >= 1 )
667 intf_ErrMsg( "module error: trying to free module `%s' which is"
668 " still in use", p_module->psz_name );
672 /* Two possibilities here: i_usage == -1 and the module is already
673 * unloaded, we can continue, or i_usage == 0, and we have to hide
674 * the module before going on. */
675 if( p_module->i_usage == 0 )
677 if( HideModule( p_module ) != 0 )
685 /* Unlink the module from the linked list. */
686 if( p_module == p_module_bank->first )
688 p_module_bank->first = p_module->next;
691 if( p_module->prev != NULL )
693 p_module->prev->next = p_module->next;
696 if( p_module->next != NULL )
698 p_module->next->prev = p_module->prev;
701 /* We free the structures that we strdup()ed in Allocate*Module(). */
702 #ifdef HAVE_DYNAMIC_PLUGINS
703 if( !p_module->b_builtin )
705 free( p_module->is.plugin.psz_filename );
708 free( p_module->psz_name );
709 free( p_module->psz_longname );
710 free( p_module->psz_version );
717 /*****************************************************************************
718 * LockModule: increase the usage count of a module and load it if needed.
719 *****************************************************************************
720 * This function has to be called before a thread starts using a module. If
721 * the module is already loaded, we just increase its usage count. If it isn't
722 * loaded, we have to dynamically open it and initialize it.
723 * If you successfully call LockModule() at any moment, be careful to call
724 * UnlockModule() when you don't need it anymore.
725 *****************************************************************************/
726 static int LockModule( module_t * p_module )
728 if( p_module->i_usage >= 0 )
730 /* This module is already loaded and activated, we can return */
735 if( p_module->b_builtin )
737 /* A built-in module should always have a refcount >= 0 ! */
738 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
739 p_module->psz_name, p_module->i_usage );
743 #ifdef HAVE_DYNAMIC_PLUGINS
744 if( p_module->i_usage != -1 )
746 /* This shouldn't happen. Ever. We have serious problems here. */
747 intf_ErrMsg( "module error: plugin module `%s' has refcount %i",
748 p_module->psz_name, p_module->i_usage );
752 /* i_usage == -1, which means that the module isn't in memory */
753 if( module_load( p_module->is.plugin.psz_filename,
754 &p_module->is.plugin.handle ) )
756 /* The plugin module couldn't be opened */
757 intf_ErrMsg( "module error: cannot open %s (%s)",
758 p_module->is.plugin.psz_filename, module_error() );
762 /* FIXME: what to do if the guy modified the plugin while it was
763 * unloaded ? It makes XMMS crash nastily, perhaps we should try
764 * to be a bit more clever here. */
766 /* Activate the module : fill the capability structure, etc. */
767 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
769 /* We couldn't call ActivateModule() -- looks nasty, but
770 * we can't do much about it. Just try to unload module. */
771 module_unload( p_module->is.plugin.handle );
772 p_module->i_usage = -1;
776 /* Everything worked fine ! The module is ready to be used */
777 p_module->i_usage = 1;
778 #endif /* HAVE_DYNAMIC_PLUGINS */
783 /*****************************************************************************
784 * UnlockModule: decrease the usage count of a module.
785 *****************************************************************************
786 * We decrease the usage count of a module so that we know when a module
787 * becomes unused and can be hidden.
788 *****************************************************************************/
789 static int UnlockModule( module_t * p_module )
791 if( p_module->i_usage <= 0 )
793 /* This shouldn't happen. Ever. We have serious problems here. */
794 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
795 " which isn't even in use", p_module->psz_name );
799 /* This module is still in use, we can return */
801 p_module->i_unused_delay = 0;
806 #ifdef HAVE_DYNAMIC_PLUGINS
807 /*****************************************************************************
808 * HideModule: remove a module from memory but keep its structure.
809 *****************************************************************************
810 * This function can only be called if i_usage == 0. It will make a call
811 * to the module's inner DeactivateModule() symbol, and then unload it
812 * from memory. A call to module_Need() will automagically load it again.
813 *****************************************************************************/
814 static int HideModule( module_t * p_module )
816 if( p_module->b_builtin )
818 /* A built-in module should never be hidden. */
819 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
820 p_module->psz_name );
824 if( p_module->i_usage >= 1 )
826 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
827 " in use", p_module->psz_name );
831 if( p_module->i_usage <= -1 )
833 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
834 " hidden", p_module->psz_name );
838 /* Deactivate the module : free the capability structure, etc. */
839 if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
841 /* We couldn't call DeactivateModule() -- looks nasty, but
842 * we can't do much about it. Just try to unload module anyway. */
843 module_unload( p_module->is.plugin.handle );
844 p_module->i_usage = -1;
848 /* Everything worked fine, we can safely unload the module. */
849 module_unload( p_module->is.plugin.handle );
850 p_module->i_usage = -1;
855 /*****************************************************************************
856 * CallSymbol: calls a module symbol.
857 *****************************************************************************
858 * This function calls a symbol given its name and a module structure. The
859 * symbol MUST refer to a function returning int and taking a module_t* as
861 *****************************************************************************/
862 static int CallSymbol( module_t * p_module, char * psz_name )
864 int (* pf_symbol) ( module_t * p_module );
866 /* Try to resolve the symbol */
867 pf_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
869 if( pf_symbol == NULL )
871 /* We couldn't load the symbol */
872 intf_WarnMsg( 1, "module warning: "
873 "cannot find symbol %s in module %s (%s)",
874 psz_name, p_module->is.plugin.psz_filename,
879 /* We can now try to call the symbol */
880 if( pf_symbol( p_module ) != 0 )
882 /* With a well-written module we shouldn't have to print an
883 * additional error message here, but just make sure. */
884 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
885 psz_name, p_module->is.plugin.psz_filename );
889 /* Everything worked fine, we can return */
892 #endif /* HAVE_DYNAMIC_PLUGINS */