1 /*****************************************************************************
2 * modules.c : Built-in and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.32 2001/05/31 01:37:08 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() */
38 #if !defined( _MSC_VER )
42 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
43 # include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
44 # define HAVE_DYNAMIC_PLUGINS
45 #elif defined(HAVE_IMAGE_H) /* BeOS */
47 # define HAVE_DYNAMIC_PLUGINS
49 # undef HAVE_DYNAMIC_PLUGINS
53 # include "beos_specific.h"
57 # include "darwin_specific.h"
67 #include "stream_control.h"
68 #include "input_ext-intf.h"
71 #include "video_output.h"
73 #include "audio_output.h"
75 #include "interface.h"
77 #include "intf_playlist.h"
79 #ifdef HAVE_DYNAMIC_PLUGINS
80 # include "modules_core.h"
82 #include "modules_builtin.h"
83 #include "modules_export.h"
87 /*****************************************************************************
89 *****************************************************************************/
90 #ifdef HAVE_DYNAMIC_PLUGINS
91 static int AllocatePluginModule ( char * );
93 static int AllocateBuiltinModule( int ( * ) ( module_t * ),
94 int ( * ) ( module_t * ),
95 int ( * ) ( module_t * ) );
96 static int DeleteModule ( module_t * );
97 static int LockModule ( module_t * );
98 static int UnlockModule ( module_t * );
99 #ifdef HAVE_DYNAMIC_PLUGINS
100 static int HideModule ( module_t * );
101 static int CallSymbol ( module_t *, char * );
104 static module_symbols_t symbols;
106 /*****************************************************************************
107 * module_InitBank: create the module bank.
108 *****************************************************************************
109 * This function creates a module bank structure and fills it with the
110 * built-in modules, as well as all the plugin modules it can find.
111 *****************************************************************************/
112 void module_InitBank( void )
114 #ifdef HAVE_DYNAMIC_PLUGINS
115 static char * path[] = { ".", "lib", PLUGIN_PATH, NULL, NULL };
117 char ** ppsz_path = path;
120 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
121 char * psz_vlcpath = system_GetProgramPath();
122 int i_vlclen = strlen( psz_vlcpath );
123 boolean_t b_notinroot;
126 struct dirent * file;
127 #endif /* HAVE_DYNAMIC_PLUGINS */
129 p_module_bank->first = NULL;
130 vlc_mutex_init( &p_module_bank->lock );
133 * Store the symbols to be exported
135 STORE_SYMBOLS( &symbols );
138 * Check all the built-in modules
140 intf_WarnMsg( 2, "module: checking built-in modules" );
142 ALLOCATE_ALL_BUILTINS();
145 * Check all the plugin modules we can find
147 #ifdef HAVE_DYNAMIC_PLUGINS
148 intf_WarnMsg( 2, "module: checking plugin modules" );
150 for( ; *ppsz_path != NULL ; ppsz_path++ )
152 /* Store strlen(*ppsz_path) for later use. */
153 int i_dirlen = strlen( *ppsz_path );
155 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
157 /* Under BeOS, we need to add beos_GetProgramPath() to access
158 * files under the current directory */
159 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
161 i_dirlen += i_vlclen + 2;
164 psz_fullpath = malloc( i_dirlen );
165 if( psz_fullpath == NULL )
169 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
174 psz_fullpath = *ppsz_path;
177 intf_WarnMsgImm( 1, "module: browsing `%s'", psz_fullpath );
179 if( (dir = opendir( psz_fullpath )) )
181 /* Parse the directory and try to load all files it contains. */
182 while( (file = readdir( dir )) )
184 int i_filelen = strlen( file->d_name );
186 /* We only load files ending with ".so" */
188 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
190 psz_file = malloc( i_dirlen + i_filelen + 2 );
191 if( psz_file == NULL )
195 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
197 /* We created a nice filename -- now we just try to load
198 * it as a plugin module. */
199 AllocatePluginModule( psz_file );
201 /* We don't care if the allocation succeeded */
206 /* Close the directory if successfully opened */
210 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
213 free( psz_fullpath );
217 #endif /* HAVE_DYNAMIC_PLUGINS */
219 intf_WarnMsg( 3, "module: module bank initialized" );
224 /*****************************************************************************
225 * module_EndBank: empty the module bank.
226 *****************************************************************************
227 * This function unloads all unused plugin modules and empties the module
228 * bank in case of success.
229 *****************************************************************************/
230 void module_EndBank( void )
234 while( p_module_bank->first != NULL )
236 if( DeleteModule( p_module_bank->first ) )
238 /* Module deletion failed */
239 intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
240 p_module_bank->first->psz_name );
242 /* We just free the module by hand. Niahahahahaha. */
243 p_next = p_module_bank->first->next;
244 free(p_module_bank->first);
245 p_module_bank->first = p_next;
249 /* Destroy the lock */
250 vlc_mutex_destroy( &p_module_bank->lock );
255 /*****************************************************************************
256 * module_ResetBank: reset the module bank.
257 *****************************************************************************
258 * This function resets the module bank by unloading all unused plugin
260 *****************************************************************************/
261 void module_ResetBank( void )
263 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
267 /*****************************************************************************
268 * module_ManageBank: manage the module bank.
269 *****************************************************************************
270 * This function parses the module bank and hides modules that have been
271 * unused for a while.
272 *****************************************************************************/
273 void module_ManageBank( void )
275 #ifdef HAVE_DYNAMIC_PLUGINS
278 /* We take the global lock */
279 vlc_mutex_lock( &p_module_bank->lock );
281 /* Parse the module list to see if any modules need to be unloaded */
282 for( p_module = p_module_bank->first ;
284 p_module = p_module->next )
286 /* If the module is unused and if it is a plugin module... */
287 if( p_module->i_usage == 0 && !p_module->b_builtin )
289 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
291 p_module->i_unused_delay++;
295 intf_WarnMsg( 1, "module: hiding unused plugin module `%s'",
296 p_module->psz_name );
297 HideModule( p_module );
299 /* Break here, so that we only hide one module at a time */
305 /* We release the global lock */
306 vlc_mutex_unlock( &p_module_bank->lock );
307 #endif /* HAVE_DYNAMIC_PLUGINS */
312 /*****************************************************************************
313 * module_Need: return the best module function, given a capability list.
314 *****************************************************************************
315 * This function returns the module that best fits the asked capabilities.
316 *****************************************************************************/
317 module_t * module_Need( int i_capabilities, void *p_data )
320 module_t * p_bestmodule = NULL;
321 int i_score, i_totalscore, i_bestscore = 0;
324 /* We take the global lock */
325 vlc_mutex_lock( &p_module_bank->lock );
327 /* Parse the module list for capabilities and probe each of them */
328 for( p_module = p_module_bank->first ;
330 p_module = p_module->next )
332 /* Test that this module can do everything we need */
333 if( ( p_module->i_capabilities & i_capabilities ) == i_capabilities )
337 LockModule( p_module );
339 /* Parse all the requested capabilities and test them */
340 for( i_index = 0 ; (1 << i_index) <= i_capabilities ; i_index++ )
342 if( ( (1 << i_index) & i_capabilities ) )
344 i_score = ( (function_list_t *)p_module->p_functions)
345 [i_index].pf_probe( p_data );
349 i_totalscore += i_score;
358 /* If the high score was broken, we have a new champion */
359 if( i_totalscore > i_bestscore )
361 /* Keep the current module locked, but release the previous */
362 if( p_bestmodule != NULL )
364 UnlockModule( p_bestmodule );
367 /* This is the new best module */
368 i_bestscore = i_totalscore;
369 p_bestmodule = p_module;
373 /* This module wasn't interesting, unlock it and forget it */
374 UnlockModule( p_module );
379 /* We can release the global lock, module refcount was incremented */
380 vlc_mutex_unlock( &p_module_bank->lock );
382 if( p_bestmodule != NULL )
384 intf_WarnMsg( 1, "module: locking module `%s'",
385 p_bestmodule->psz_name );
388 /* Don't forget that the module is still locked if bestmodule != NULL */
389 return( p_bestmodule );
392 /*****************************************************************************
393 * module_Unneed: decrease the usage count of a module.
394 *****************************************************************************
395 * This function must be called by the thread that called module_Need, to
396 * decrease the reference count and allow for hiding of modules.
397 *****************************************************************************/
398 void module_Unneed( module_t * p_module )
400 /* We take the global lock */
401 vlc_mutex_lock( &p_module_bank->lock );
403 /* Just unlock the module - we can't do anything if it fails,
404 * so there is no need to check the return value. */
405 UnlockModule( p_module );
407 intf_WarnMsg( 1, "module: unlocking module `%s'", p_module->psz_name );
409 /* We release the global lock */
410 vlc_mutex_unlock( &p_module_bank->lock );
415 /*****************************************************************************
416 * Following functions are local.
417 *****************************************************************************/
419 #ifdef HAVE_DYNAMIC_PLUGINS
420 /*****************************************************************************
421 * AllocatePluginModule: load a module into memory and initialize it.
422 *****************************************************************************
423 * This function loads a dynamically loadable module and allocates a structure
424 * for its information data. The module can then be handled by module_Need,
425 * module_Unneed and HideModule. It can be removed by DeleteModule.
426 *****************************************************************************/
427 static int AllocatePluginModule( char * psz_filename )
429 module_t * p_module, * p_othermodule;
430 module_handle_t handle;
432 /* Try to dynamically load the module. */
433 if( module_load( psz_filename, &handle ) )
435 /* The plugin module couldn't be opened */
436 intf_WarnMsgImm( 1, "module warning: cannot open %s (%s)",
437 psz_filename, module_error() );
441 /* Now that we have successfully loaded the module, we can
442 * allocate a structure for it */
443 p_module = malloc( sizeof( module_t ) );
444 if( p_module == NULL )
446 intf_ErrMsg( "module error: can't allocate p_module" );
447 module_unload( handle );
451 /* We need to fill these since they may be needed by CallSymbol() */
452 p_module->is.plugin.psz_filename = psz_filename;
453 p_module->is.plugin.handle = handle;
454 p_module->p_symbols = &symbols;
456 /* Initialize the module : fill p_module->psz_name, etc. */
457 if( CallSymbol( p_module, "InitModule" ) != 0 )
459 /* We couldn't call InitModule() */
461 module_unload( handle );
465 /* Check that version numbers match */
466 if( strcmp( VERSION, p_module->psz_version ) )
469 module_unload( handle );
473 /* Check that we don't already have a module with this name */
474 for( p_othermodule = p_module_bank->first ;
475 p_othermodule != NULL ;
476 p_othermodule = p_othermodule->next )
478 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
481 module_unload( handle );
486 /* Activate the module : fill the capability structure, etc. */
487 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
489 /* We couldn't call ActivateModule() */
491 module_unload( handle );
495 /* We strdup() these entries so that they are still valid when the
496 * module is unloaded. */
497 p_module->is.plugin.psz_filename =
498 strdup( p_module->is.plugin.psz_filename );
499 p_module->psz_name = strdup( p_module->psz_name );
500 p_module->psz_longname = strdup( p_module->psz_longname );
501 p_module->psz_version = strdup( p_module->psz_version );
502 if( p_module->is.plugin.psz_filename == NULL
503 || p_module->psz_name == NULL
504 || p_module->psz_longname == NULL
505 || p_module->psz_version == NULL )
507 intf_ErrMsg( "module error: can't duplicate strings" );
508 free( p_module->is.plugin.psz_filename );
509 free( p_module->psz_name );
510 free( p_module->psz_longname );
511 free( p_module->psz_version );
513 module_unload( handle );
517 /* Everything worked fine ! The module is ready to be added to the list. */
518 p_module->i_usage = 0;
519 p_module->i_unused_delay = 0;
521 p_module->b_builtin = 0;
523 /* Link module into the linked list */
524 if( p_module_bank->first != NULL )
526 p_module_bank->first->prev = p_module;
528 p_module->next = p_module_bank->first;
529 p_module->prev = NULL;
530 p_module_bank->first = p_module;
532 /* Immediate message so that a slow module doesn't make the user wait */
533 intf_WarnMsgImm( 2, "module: plugin module `%s', %s",
534 p_module->psz_name, p_module->psz_longname );
538 #endif /* HAVE_DYNAMIC_PLUGINS */
540 /*****************************************************************************
541 * AllocateBuiltinModule: initialize a built-in module.
542 *****************************************************************************
543 * This function registers a built-in module and allocates a structure
544 * for its information data. The module can then be handled by module_Need,
545 * module_Unneed and HideModule. It can be removed by DeleteModule.
546 *****************************************************************************/
547 static int AllocateBuiltinModule( int ( *pf_init ) ( module_t * ),
548 int ( *pf_activate ) ( module_t * ),
549 int ( *pf_deactivate ) ( module_t * ) )
551 module_t * p_module, * p_othermodule;
553 /* Now that we have successfully loaded the module, we can
554 * allocate a structure for it */
555 p_module = malloc( sizeof( module_t ) );
556 if( p_module == NULL )
558 intf_ErrMsg( "module error: can't allocate p_module" );
562 /* Initialize the module : fill p_module->psz_name, etc. */
563 if( pf_init( p_module ) != 0 )
565 /* With a well-written module we shouldn't have to print an
566 * additional error message here, but just make sure. */
567 intf_ErrMsg( "module error: failed calling init in builtin module" );
572 /* Check that version numbers match */
573 if( strcmp( VERSION, p_module->psz_version ) )
579 /* Check that we don't already have a module with this name */
580 for( p_othermodule = p_module_bank->first ;
581 p_othermodule != NULL ;
582 p_othermodule = p_othermodule->next )
584 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
591 if( pf_activate( p_module ) != 0 )
593 /* With a well-written module we shouldn't have to print an
594 * additional error message here, but just make sure. */
595 intf_ErrMsg( "module error: failed calling activate "
596 "in builtin module" );
601 /* We strdup() these entries so that they are still valid when the
602 * module is unloaded. */
603 p_module->psz_name = strdup( p_module->psz_name );
604 p_module->psz_longname = strdup( p_module->psz_longname );
605 p_module->psz_version = strdup( p_module->psz_version );
606 if( p_module->psz_name == NULL
607 || p_module->psz_longname == NULL
608 || p_module->psz_version == NULL )
610 intf_ErrMsg( "module error: can't duplicate strings" );
611 free( p_module->psz_name );
612 free( p_module->psz_longname );
613 free( p_module->psz_version );
618 /* Everything worked fine ! The module is ready to be added to the list. */
619 p_module->i_usage = 0;
620 p_module->i_unused_delay = 0;
622 p_module->b_builtin = 1;
623 p_module->is.builtin.pf_deactivate = pf_deactivate;
625 /* Link module into the linked list */
626 if( p_module_bank->first != NULL )
628 p_module_bank->first->prev = p_module;
630 p_module->next = p_module_bank->first;
631 p_module->prev = NULL;
632 p_module_bank->first = p_module;
634 /* Immediate message so that a slow module doesn't make the user wait */
635 intf_WarnMsgImm( 2, "module: builtin module `%s', %s",
636 p_module->psz_name, p_module->psz_longname );
641 /*****************************************************************************
642 * DeleteModule: delete a module and its structure.
643 *****************************************************************************
644 * This function can only be called if i_usage <= 0.
645 *****************************************************************************/
646 static int DeleteModule( module_t * p_module )
648 /* If the module is not in use but is still in memory, we first have
649 * to hide it and remove it from memory before we can free the
651 if( p_module->b_builtin )
653 if( p_module->i_usage != 0 )
655 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
656 " usage %i", p_module->psz_name, p_module->i_usage );
661 /* We deactivate the module now. */
662 p_module->is.builtin.pf_deactivate( p_module );
665 #ifdef HAVE_DYNAMIC_PLUGINS
668 if( p_module->i_usage >= 1 )
670 intf_ErrMsg( "module error: trying to free module `%s' which is"
671 " still in use", p_module->psz_name );
675 /* Two possibilities here: i_usage == -1 and the module is already
676 * unloaded, we can continue, or i_usage == 0, and we have to hide
677 * the module before going on. */
678 if( p_module->i_usage == 0 )
680 if( HideModule( p_module ) != 0 )
688 /* Unlink the module from the linked list. */
689 if( p_module == p_module_bank->first )
691 p_module_bank->first = p_module->next;
694 if( p_module->prev != NULL )
696 p_module->prev->next = p_module->next;
699 if( p_module->next != NULL )
701 p_module->next->prev = p_module->prev;
704 /* We free the structures that we strdup()ed in Allocate*Module(). */
705 #ifdef HAVE_DYNAMIC_PLUGINS
706 if( !p_module->b_builtin )
708 free( p_module->is.plugin.psz_filename );
711 free( p_module->psz_name );
712 free( p_module->psz_longname );
713 free( p_module->psz_version );
720 /*****************************************************************************
721 * LockModule: increase the usage count of a module and load it if needed.
722 *****************************************************************************
723 * This function has to be called before a thread starts using a module. If
724 * the module is already loaded, we just increase its usage count. If it isn't
725 * loaded, we have to dynamically open it and initialize it.
726 * If you successfully call LockModule() at any moment, be careful to call
727 * UnlockModule() when you don't need it anymore.
728 *****************************************************************************/
729 static int LockModule( module_t * p_module )
731 if( p_module->i_usage >= 0 )
733 /* This module is already loaded and activated, we can return */
738 if( p_module->b_builtin )
740 /* A built-in module should always have a refcount >= 0 ! */
741 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
742 p_module->psz_name, p_module->i_usage );
746 #ifdef HAVE_DYNAMIC_PLUGINS
747 if( p_module->i_usage != -1 )
749 /* This shouldn't happen. Ever. We have serious problems here. */
750 intf_ErrMsg( "module error: plugin module `%s' has refcount %i",
751 p_module->psz_name, p_module->i_usage );
755 /* i_usage == -1, which means that the module isn't in memory */
756 if( module_load( p_module->is.plugin.psz_filename,
757 &p_module->is.plugin.handle ) )
759 /* The plugin module couldn't be opened */
760 intf_ErrMsg( "module error: cannot open %s (%s)",
761 p_module->is.plugin.psz_filename, module_error() );
765 /* FIXME: what to do if the guy modified the plugin while it was
766 * unloaded ? It makes XMMS crash nastily, perhaps we should try
767 * to be a bit more clever here. */
769 /* Activate the module : fill the capability structure, etc. */
770 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
772 /* We couldn't call ActivateModule() -- looks nasty, but
773 * we can't do much about it. Just try to unload module. */
774 module_unload( p_module->is.plugin.handle );
775 p_module->i_usage = -1;
779 /* Everything worked fine ! The module is ready to be used */
780 p_module->i_usage = 1;
781 #endif /* HAVE_DYNAMIC_PLUGINS */
786 /*****************************************************************************
787 * UnlockModule: decrease the usage count of a module.
788 *****************************************************************************
789 * We decrease the usage count of a module so that we know when a module
790 * becomes unused and can be hidden.
791 *****************************************************************************/
792 static int UnlockModule( module_t * p_module )
794 if( p_module->i_usage <= 0 )
796 /* This shouldn't happen. Ever. We have serious problems here. */
797 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
798 " which isn't even in use", p_module->psz_name );
802 /* This module is still in use, we can return */
804 p_module->i_unused_delay = 0;
809 #ifdef HAVE_DYNAMIC_PLUGINS
810 /*****************************************************************************
811 * HideModule: remove a module from memory but keep its structure.
812 *****************************************************************************
813 * This function can only be called if i_usage == 0. It will make a call
814 * to the module's inner DeactivateModule() symbol, and then unload it
815 * from memory. A call to module_Need() will automagically load it again.
816 *****************************************************************************/
817 static int HideModule( module_t * p_module )
819 if( p_module->b_builtin )
821 /* A built-in module should never be hidden. */
822 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
823 p_module->psz_name );
827 if( p_module->i_usage >= 1 )
829 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
830 " in use", p_module->psz_name );
834 if( p_module->i_usage <= -1 )
836 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
837 " hidden", p_module->psz_name );
841 /* Deactivate the module : free the capability structure, etc. */
842 if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
844 /* We couldn't call DeactivateModule() -- looks nasty, but
845 * we can't do much about it. Just try to unload module anyway. */
846 module_unload( p_module->is.plugin.handle );
847 p_module->i_usage = -1;
851 /* Everything worked fine, we can safely unload the module. */
852 module_unload( p_module->is.plugin.handle );
853 p_module->i_usage = -1;
858 /*****************************************************************************
859 * CallSymbol: calls a module symbol.
860 *****************************************************************************
861 * This function calls a symbol given its name and a module structure. The
862 * symbol MUST refer to a function returning int and taking a module_t* as
864 *****************************************************************************/
865 static int CallSymbol( module_t * p_module, char * psz_name )
867 int (* pf_symbol) ( module_t * p_module );
869 /* Try to resolve the symbol */
870 pf_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
872 if( pf_symbol == NULL )
874 /* We couldn't load the symbol */
875 intf_WarnMsg( 1, "module warning: "
876 "cannot find symbol %s in module %s (%s)",
877 psz_name, p_module->is.plugin.psz_filename,
882 /* We can now try to call the symbol */
883 if( pf_symbol( p_module ) != 0 )
885 /* With a well-written module we shouldn't have to print an
886 * additional error message here, but just make sure. */
887 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
888 psz_name, p_module->is.plugin.psz_filename );
892 /* Everything worked fine, we can return */
895 #endif /* HAVE_DYNAMIC_PLUGINS */