1 /*****************************************************************************
2 * modules.c : Built-in and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.24 2001/04/15 10:54:46 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
28 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
29 * is set to 64. Don't try to be cleverer. */
30 #ifdef _FILE_OFFSET_BITS
31 #undef _FILE_OFFSET_BITS
34 #include <stdlib.h> /* free(), strtol() */
35 #include <stdio.h> /* sprintf() */
36 #include <string.h> /* strdup() */
39 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
40 # include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
41 # define HAVE_DYNAMIC_PLUGINS
42 #elif defined(HAVE_IMAGE_H) /* BeOS */
44 # define HAVE_DYNAMIC_PLUGINS
46 # undef HAVE_DYNAMIC_PLUGINS
50 # include "beos_specific.h"
54 # include "darwin_specific.h"
62 #include "modules_builtin.h"
63 #include "modules_core.h"
65 /* Local prototypes */
66 #ifdef HAVE_DYNAMIC_PLUGINS
67 static int AllocatePluginModule ( module_bank_t *, char * );
69 static int AllocateBuiltinModule( module_bank_t *,
70 int ( * ) ( module_t * ),
71 int ( * ) ( module_t * ),
72 int ( * ) ( module_t * ) );
73 static int FreeModule ( module_bank_t * p_bank, module_t * );
74 static int LockModule ( module_t * );
75 static int UnlockModule ( module_t * );
76 #ifdef HAVE_DYNAMIC_PLUGINS
77 static int HideModule ( module_t * );
78 static int CallSymbol ( module_t *, char * );
81 /*****************************************************************************
82 * module_CreateBank: create the module bank.
83 *****************************************************************************
84 * This function creates a module bank structure.
85 *****************************************************************************/
86 module_bank_t * module_CreateBank( void )
88 module_bank_t * p_bank;
90 p_bank = malloc( sizeof( module_bank_t ) );
95 /*****************************************************************************
96 * module_InitBank: create the module bank.
97 *****************************************************************************
98 * This function creates a module bank structure and fills it with the
99 * built-in modules, as well as all the plugin modules it can find.
100 *****************************************************************************/
101 void module_InitBank( module_bank_t * p_bank )
103 #ifdef HAVE_DYNAMIC_PLUGINS
104 static char * path[] = { ".", "lib", PLUGIN_PATH, NULL, NULL };
106 char ** ppsz_path = path;
109 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
110 char * psz_vlcpath = system_GetProgramPath();
111 int i_vlclen = strlen( psz_vlcpath );
112 boolean_t b_notinroot;
115 struct dirent * file;
116 #endif /* HAVE_DYNAMIC_PLUGINS */
118 p_bank->first = NULL;
119 vlc_mutex_init( &p_bank->lock );
121 intf_WarnMsg( 1, "module: module bank initialized" );
123 intf_WarnMsg( 2, "module: checking built-in modules" );
125 ALLOCATE_ALL_BUILTINS();
127 #ifdef HAVE_DYNAMIC_PLUGINS
128 intf_WarnMsg( 2, "module: checking plugin modules" );
130 for( ; *ppsz_path != NULL ; ppsz_path++ )
132 /* Store strlen(*ppsz_path) for later use. */
133 int i_dirlen = strlen( *ppsz_path );
135 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
137 /* Under BeOS, we need to add beos_GetProgramPath() to access
138 * files under the current directory */
139 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
141 i_dirlen += i_vlclen + 2;
144 psz_fullpath = malloc( i_dirlen );
145 if( psz_fullpath == NULL )
149 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
154 psz_fullpath = *ppsz_path;
157 intf_WarnMsgImm( 3, "module: browsing `%s'", psz_fullpath );
159 if( (dir = opendir( psz_fullpath )) )
161 /* Parse the directory and try to load all files it contains. */
162 while( (file = readdir( dir )) )
164 int i_filelen = strlen( file->d_name );
166 /* We only load files ending with ".so" */
168 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
170 psz_file = malloc( i_dirlen + i_filelen + 2 );
171 if( psz_file == NULL )
175 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
177 /* We created a nice filename -- now we just try to load
178 * it as a plugin module. */
179 AllocatePluginModule( p_bank, psz_file );
181 /* We don't care if the allocation succeeded */
186 /* Close the directory if successfully opened */
190 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
193 free( psz_fullpath );
197 #endif /* HAVE_DYNAMIC_PLUGINS */
202 /*****************************************************************************
203 * module_DestroyBank: destroy the module bank.
204 *****************************************************************************
205 * This function unloads all unused plugin modules and removes the module
206 * bank in case of success.
207 *****************************************************************************/
208 void module_DestroyBank( module_bank_t * p_bank )
212 while( p_bank->first != NULL )
214 if( FreeModule( p_bank, p_bank->first ) )
216 /* Module deletion failed */
217 intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
218 p_bank->first->psz_name );
220 /* We just free the module by hand. Niahahahahaha. */
221 p_next = p_bank->first->next;
223 p_bank->first = p_next;
227 /* Destroy the lock */
228 vlc_mutex_destroy( &p_bank->lock );
230 /* We can free the module bank */
236 /*****************************************************************************
237 * module_ResetBank: reset the module bank.
238 *****************************************************************************
239 * This function resets the module bank by unloading all unused plugin
241 *****************************************************************************/
242 void module_ResetBank( module_bank_t * p_bank )
244 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
248 /*****************************************************************************
249 * module_ManageBank: manage the module bank.
250 *****************************************************************************
251 * This function parses the module bank and hides modules that have been
252 * unused for a while.
253 *****************************************************************************/
254 void module_ManageBank( module_bank_t * p_bank )
256 #ifdef HAVE_DYNAMIC_PLUGINS
259 /* We take the global lock */
260 vlc_mutex_lock( &p_bank->lock );
262 /* Parse the module list to see if any modules need to be unloaded */
263 for( p_module = p_bank->first ;
265 p_module = p_module->next )
267 /* If the module is unused and if it is a plugin module... */
268 if( p_module->i_usage == 0 && !p_module->b_builtin )
270 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
272 p_module->i_unused_delay++;
276 intf_WarnMsg( 3, "module: hiding unused module `%s'",
277 p_module->psz_name );
278 HideModule( p_module );
280 /* Break here, so that we only hide one module at a time */
286 /* We release the global lock */
287 vlc_mutex_unlock( &p_bank->lock );
288 #endif /* HAVE_DYNAMIC_PLUGINS */
293 /*****************************************************************************
294 * module_Need: return the best module function, given a capability list.
295 *****************************************************************************
296 * This function returns the module that best fits the asked capabilities.
297 *****************************************************************************/
298 module_t * module_Need( module_bank_t *p_bank,
299 int i_capabilities, void *p_data )
302 module_t * p_bestmodule = NULL;
303 int i_score, i_totalscore, i_bestscore = 0;
306 /* We take the global lock */
307 vlc_mutex_lock( &p_bank->lock );
309 /* Parse the module list for capabilities and probe each of them */
310 for( p_module = p_bank->first ;
312 p_module = p_module->next )
314 /* Test that this module can do everything we need */
315 if( ( p_module->i_capabilities & i_capabilities ) == i_capabilities )
319 LockModule( p_module );
321 /* Parse all the requested capabilities and test them */
322 for( i_index = 0 ; (1 << i_index) <= i_capabilities ; i_index++ )
324 if( ( (1 << i_index) & i_capabilities ) )
326 i_score = ( (function_list_t *)p_module->p_functions)
327 [i_index].pf_probe( p_data );
331 i_totalscore += i_score;
340 /* If the high score was broken, we have a new champion */
341 if( i_totalscore > i_bestscore )
343 /* Keep the current module locked, but release the previous */
344 if( p_bestmodule != NULL )
346 UnlockModule( p_bestmodule );
349 /* This is the new best module */
350 i_bestscore = i_totalscore;
351 p_bestmodule = p_module;
355 /* This module wasn't interesting, unlock it and forget it */
356 UnlockModule( p_module );
361 /* We can release the global lock, module refcount was incremented */
362 vlc_mutex_unlock( &p_bank->lock );
364 if( p_bestmodule != NULL )
366 intf_WarnMsg( 3, "module: locking module `%s'",
367 p_bestmodule->psz_name );
370 /* Don't forget that the module is still locked if bestmodule != NULL */
371 return( p_bestmodule );
374 /*****************************************************************************
375 * module_Unneed: decrease the usage count of a module.
376 *****************************************************************************
377 * This function must be called by the thread that called module_Need, to
378 * decrease the reference count and allow for hiding of modules.
379 *****************************************************************************/
380 void module_Unneed( module_bank_t * p_bank, module_t * p_module )
382 /* We take the global lock */
383 vlc_mutex_lock( &p_bank->lock );
385 /* Just unlock the module - we can't do anything if it fails,
386 * so there is no need to check the return value. */
387 UnlockModule( p_module );
389 intf_WarnMsg( 3, "module: unlocking module `%s'", p_module->psz_name );
391 /* We release the global lock */
392 vlc_mutex_unlock( &p_bank->lock );
397 /*****************************************************************************
398 * Following functions are local.
399 *****************************************************************************/
401 #ifdef HAVE_DYNAMIC_PLUGINS
402 /*****************************************************************************
403 * AllocatePluginModule: load a module into memory and initialize it.
404 *****************************************************************************
405 * This function loads a dynamically loadable module and allocates a structure
406 * for its information data. The module can then be handled by module_Need,
407 * module_Unneed and HideModule. It can be removed by FreeModule.
408 *****************************************************************************/
409 static int AllocatePluginModule( module_bank_t * p_bank, char * psz_filename )
411 module_t * p_module, * p_othermodule;
412 module_handle_t handle;
414 /* Try to dynamically load the module. */
415 if( module_load( psz_filename, &handle ) )
417 /* The plugin module couldn't be opened */
418 intf_WarnMsgImm( 3, "module warning: cannot open %s (%s)",
419 psz_filename, module_error() );
423 /* Now that we have successfully loaded the module, we can
424 * allocate a structure for it */
425 p_module = malloc( sizeof( module_t ) );
426 if( p_module == NULL )
428 intf_ErrMsg( "module error: can't allocate p_module" );
429 module_unload( handle );
433 /* We need to fill these since they may be needed by CallSymbol() */
434 p_module->is.plugin.psz_filename = psz_filename;
435 p_module->is.plugin.handle = handle;
437 /* Initialize the module : fill p_module->psz_name, etc. */
438 if( CallSymbol( p_module, "InitModule" ) != 0 )
440 /* We couldn't call InitModule() */
442 module_unload( handle );
446 /* Check that version numbers match */
447 if( strcmp( VERSION, p_module->psz_version ) )
450 module_unload( handle );
454 /* Check that we don't already have a module with this name */
455 for( p_othermodule = p_bank->first ;
456 p_othermodule != NULL ;
457 p_othermodule = p_othermodule->next )
459 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
462 module_unload( handle );
467 /* Activate the module : fill the capability structure, etc. */
468 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
470 /* We couldn't call ActivateModule() */
472 module_unload( handle );
476 /* We strdup() these entries so that they are still valid when the
477 * module is unloaded. */
478 p_module->is.plugin.psz_filename =
479 strdup( p_module->is.plugin.psz_filename );
480 p_module->psz_name = strdup( p_module->psz_name );
481 p_module->psz_longname = strdup( p_module->psz_longname );
482 p_module->psz_version = strdup( p_module->psz_version );
483 if( p_module->is.plugin.psz_filename == NULL
484 || p_module->psz_name == NULL
485 || p_module->psz_longname == NULL
486 || p_module->psz_version == NULL )
488 intf_ErrMsg( "module error: can't duplicate strings" );
489 free( p_module->is.plugin.psz_filename );
490 free( p_module->psz_name );
491 free( p_module->psz_longname );
492 free( p_module->psz_version );
494 module_unload( handle );
498 /* Everything worked fine ! The module is ready to be added to the list. */
499 p_module->i_usage = 0;
500 p_module->i_unused_delay = 0;
502 p_module->b_builtin = 0;
504 /* Link module into the linked list */
505 if( p_bank->first != NULL )
507 p_bank->first->prev = p_module;
509 p_module->next = p_bank->first;
510 p_module->prev = NULL;
511 p_bank->first = p_module;
513 /* Immediate message so that a slow module doesn't make the user wait */
514 intf_WarnMsgImm( 2, "module: plugin module `%s', %s",
515 p_module->psz_name, p_module->psz_longname );
519 #endif /* HAVE_DYNAMIC_PLUGINS */
521 /*****************************************************************************
522 * AllocateBuiltinModule: initialize a built-in module.
523 *****************************************************************************
524 * This function registers a built-in module and allocates a structure
525 * for its information data. The module can then be handled by module_Need,
526 * module_Unneed and HideModule. It can be removed by FreeModule.
527 *****************************************************************************/
528 static int AllocateBuiltinModule( module_bank_t * p_bank,
529 int ( *pf_init ) ( module_t * ),
530 int ( *pf_activate ) ( module_t * ),
531 int ( *pf_deactivate ) ( module_t * ) )
533 module_t * p_module, * p_othermodule;
535 /* Now that we have successfully loaded the module, we can
536 * allocate a structure for it */
537 p_module = malloc( sizeof( module_t ) );
538 if( p_module == NULL )
540 intf_ErrMsg( "module error: can't allocate p_module" );
544 /* Initialize the module : fill p_module->psz_name, etc. */
545 if( pf_init( p_module ) != 0 )
547 /* With a well-written module we shouldn't have to print an
548 * additional error message here, but just make sure. */
549 intf_ErrMsg( "module error: failed calling init in builtin module" );
554 /* Check that version numbers match */
555 if( strcmp( VERSION, p_module->psz_version ) )
561 /* Check that we don't already have a module with this name */
562 for( p_othermodule = p_bank->first ;
563 p_othermodule != NULL ;
564 p_othermodule = p_othermodule->next )
566 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
573 if( pf_activate( p_module ) != 0 )
575 /* With a well-written module we shouldn't have to print an
576 * additional error message here, but just make sure. */
577 intf_ErrMsg( "module error: failed calling activate "
578 "in builtin module" );
583 /* We strdup() these entries so that they are still valid when the
584 * module is unloaded. */
585 p_module->psz_name = strdup( p_module->psz_name );
586 p_module->psz_longname = strdup( p_module->psz_longname );
587 p_module->psz_version = strdup( p_module->psz_version );
588 if( p_module->psz_name == NULL
589 || p_module->psz_longname == NULL
590 || p_module->psz_version == NULL )
592 intf_ErrMsg( "module error: can't duplicate strings" );
593 free( p_module->psz_name );
594 free( p_module->psz_longname );
595 free( p_module->psz_version );
600 /* Everything worked fine ! The module is ready to be added to the list. */
601 p_module->i_usage = 0;
602 p_module->i_unused_delay = 0;
604 p_module->b_builtin = 1;
605 p_module->is.builtin.pf_deactivate = pf_deactivate;
607 /* Link module into the linked list */
608 if( p_bank->first != NULL )
610 p_bank->first->prev = p_module;
612 p_module->next = p_bank->first;
613 p_module->prev = NULL;
614 p_bank->first = p_module;
616 /* Immediate message so that a slow module doesn't make the user wait */
617 intf_WarnMsgImm( 2, "module: builtin module `%s', %s",
618 p_module->psz_name, p_module->psz_longname );
623 /*****************************************************************************
624 * FreeModule: delete a module and its structure.
625 *****************************************************************************
626 * This function can only be called if i_usage <= 0.
627 *****************************************************************************/
628 static int FreeModule( module_bank_t * p_bank, module_t * p_module )
630 /* If the module is not in use but is still in memory, we first have
631 * to hide it and remove it from memory before we can free the
633 if( p_module->b_builtin )
635 if( p_module->i_usage != 0 )
637 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
638 " usage %i", p_module->psz_name, p_module->i_usage );
643 /* We deactivate the module now. */
644 p_module->is.builtin.pf_deactivate( p_module );
647 #ifdef HAVE_DYNAMIC_PLUGINS
650 if( p_module->i_usage >= 1 )
652 intf_ErrMsg( "module error: trying to free module `%s' which is"
653 " still in use", p_module->psz_name );
657 /* Two possibilities here: i_usage == -1 and the module is already
658 * unloaded, we can continue, or i_usage == 0, and we have to hide
659 * the module before going on. */
660 if( p_module->i_usage == 0 )
662 if( HideModule( p_module ) != 0 )
670 /* Unlink the module from the linked list. */
671 if( p_module == p_bank->first )
673 p_bank->first = p_module->next;
676 if( p_module->prev != NULL )
678 p_module->prev->next = p_module->next;
681 if( p_module->next != NULL )
683 p_module->next->prev = p_module->prev;
686 /* We free the structures that we strdup()ed in Allocate*Module(). */
687 #ifdef HAVE_DYNAMIC_PLUGINS
688 if( !p_module->b_builtin )
690 free( p_module->is.plugin.psz_filename );
693 free( p_module->psz_name );
694 free( p_module->psz_longname );
695 free( p_module->psz_version );
702 /*****************************************************************************
703 * LockModule: increase the usage count of a module and load it if needed.
704 *****************************************************************************
705 * This function has to be called before a thread starts using a module. If
706 * the module is already loaded, we just increase its usage count. If it isn't
707 * loaded, we have to dynamically open it and initialize it.
708 * If you successfully call LockModule() at any moment, be careful to call
709 * UnlockModule() when you don't need it anymore.
710 *****************************************************************************/
711 static int LockModule( module_t * p_module )
713 if( p_module->i_usage >= 0 )
715 /* This module is already loaded and activated, we can return */
720 if( p_module->b_builtin )
722 /* A built-in module should always have a refcount >= 0 ! */
723 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
724 p_module->psz_name, p_module->i_usage );
728 #ifdef HAVE_DYNAMIC_PLUGINS
729 if( p_module->i_usage != -1 )
731 /* This shouldn't happen. Ever. We have serious problems here. */
732 intf_ErrMsg( "module error: plugin module `%s' has refcount %i",
733 p_module->psz_name, p_module->i_usage );
737 /* i_usage == -1, which means that the module isn't in memory */
738 if( module_load( p_module->is.plugin.psz_filename,
739 &p_module->is.plugin.handle ) )
741 /* The plugin module couldn't be opened */
742 intf_ErrMsg( "module error: cannot open %s (%s)",
743 p_module->is.plugin.psz_filename, module_error() );
747 /* FIXME: what to do if the guy modified the plugin while it was
748 * unloaded ? It makes XMMS crash nastily, perhaps we should try
749 * to be a bit more clever here. */
751 /* Activate the module : fill the capability structure, etc. */
752 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
754 /* We couldn't call ActivateModule() -- looks nasty, but
755 * we can't do much about it. Just try to unload module. */
756 module_unload( p_module->is.plugin.handle );
757 p_module->i_usage = -1;
761 /* Everything worked fine ! The module is ready to be used */
762 p_module->i_usage = 1;
763 #endif /* HAVE_DYNAMIC_PLUGINS */
768 /*****************************************************************************
769 * UnlockModule: decrease the usage count of a module.
770 *****************************************************************************
771 * We decrease the usage count of a module so that we know when a module
772 * becomes unused and can be hidden.
773 *****************************************************************************/
774 static int UnlockModule( module_t * p_module )
776 if( p_module->i_usage <= 0 )
778 /* This shouldn't happen. Ever. We have serious problems here. */
779 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
780 " which isn't even in use", p_module->psz_name );
784 /* This module is still in use, we can return */
786 p_module->i_unused_delay = 0;
791 #ifdef HAVE_DYNAMIC_PLUGINS
792 /*****************************************************************************
793 * HideModule: remove a module from memory but keep its structure.
794 *****************************************************************************
795 * This function can only be called if i_usage == 0. It will make a call
796 * to the module's inner DeactivateModule() symbol, and then unload it
797 * from memory. A call to module_Need() will automagically load it again.
798 *****************************************************************************/
799 static int HideModule( module_t * p_module )
801 if( p_module->b_builtin )
803 /* A built-in module should never be hidden. */
804 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
805 p_module->psz_name );
809 if( p_module->i_usage >= 1 )
811 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
812 " in use", p_module->psz_name );
816 if( p_module->i_usage <= -1 )
818 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
819 " hidden", p_module->psz_name );
823 /* Deactivate the module : free the capability structure, etc. */
824 if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
826 /* We couldn't call DeactivateModule() -- looks nasty, but
827 * we can't do much about it. Just try to unload module anyway. */
828 module_unload( p_module->is.plugin.handle );
829 p_module->i_usage = -1;
833 /* Everything worked fine, we can safely unload the module. */
834 module_unload( p_module->is.plugin.handle );
835 p_module->i_usage = -1;
840 /*****************************************************************************
841 * CallSymbol: calls a module symbol.
842 *****************************************************************************
843 * This function calls a symbol given its name and a module structure. The
844 * symbol MUST refer to a function returning int and taking a module_t* as
846 *****************************************************************************/
847 static int CallSymbol( module_t * p_module, char * psz_name )
849 typedef int ( symbol_t ) ( module_t * p_module );
852 /* Try to resolve the symbol */
853 p_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
857 /* We couldn't load the symbol */
858 intf_WarnMsg( 3, "module warning: "
859 "cannot find symbol %s in module %s (%s)",
860 psz_name, p_module->is.plugin.psz_filename,
865 /* We can now try to call the symbol */
866 if( p_symbol( p_module ) != 0 )
868 /* With a well-written module we shouldn't have to print an
869 * additional error message here, but just make sure. */
870 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
871 psz_name, p_module->is.plugin.psz_filename );
875 /* Everything worked fine, we can return */
878 #endif /* HAVE_DYNAMIC_PLUGINS */