1 /*****************************************************************************
2 * modules.c : Built-in and dynamic modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.20 2001/04/06 18:18:10 massiot 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() */
42 #elif defined(HAVE_IMAGE_H) /* BeOS */
46 /* FIXME: this isn't supposed to be an error */
47 #error no dynamic plugins available on your system !
51 #include "beos_specific.h"
58 #include <sys/param.h> /* for MAXPATHLEN */
60 extern main_t *p_main;
65 #include "modules_core.h"
67 /* Local prototypes */
68 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename );
69 static int HideModule( module_t * p_module );
70 static int FreeModule( module_bank_t * p_bank, module_t * p_module );
71 static int LockModule( module_t * p_module );
72 static int UnlockModule( module_t * p_module );
73 static int CallSymbol( module_t * p_module, char * psz_name );
75 /*****************************************************************************
76 * module_CreateBank: create the module bank.
77 *****************************************************************************
78 * This function creates a module bank structure.
79 *****************************************************************************/
80 module_bank_t * module_CreateBank( void )
82 module_bank_t * p_bank;
84 p_bank = malloc( sizeof( module_bank_t ) );
89 /*****************************************************************************
90 * module_InitBank: create the module bank.
91 *****************************************************************************
92 * This function creates a module bank structure and fills it with the
93 * built-in modules, as well as all the dynamic modules it can find.
94 *****************************************************************************/
95 void module_InitBank( module_bank_t * p_bank )
97 static char * path[] = { ".", "lib", PLUGIN_PATH, NULL, NULL };
99 char ** ppsz_path = path;
103 char * psz_vlcpath = beos_GetProgramPath();
104 int i_vlclen = strlen( psz_vlcpath );
105 boolean_t b_notinroot;
106 #elif defined SYS_DARWIN1_3
107 static char once = 0;
108 static char app_path[ MAXPATHLEN ];
109 // HACK TO CUT OUT trailing 'vlc'
110 int i_pathlen = strlen( p_main->ppsz_argv[0] ) - 3;
113 struct dirent * file;
115 p_bank->first = NULL;
116 vlc_mutex_init( &p_bank->lock );
118 intf_WarnMsg( 1, "module: module bank initialized" );
124 strncpy( app_path, p_main->ppsz_argv[ 0 ], i_pathlen );
125 intf_ErrMsg( "%s", p_main->ppsz_argv[ 0 ] );
126 strcat( app_path, "lib" );
127 path[ 3 ] = app_path ;
128 intf_ErrMsg( "%s", path[ 3 ] );
132 for( ; *ppsz_path != NULL ; ppsz_path++ )
134 /* Store strlen(*ppsz_path) for later use. */
135 int i_dirlen = strlen( *ppsz_path );
139 /* Under BeOS, we need to add beos_GetProgramPath() to access
140 * files under the current directory */
141 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
143 i_dirlen += i_vlclen + 2;
146 psz_fullpath = malloc( i_dirlen );
147 if( psz_fullpath == NULL )
151 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
156 psz_fullpath = *ppsz_path;
159 intf_WarnMsgImm( 2, "module: browsing %s", psz_fullpath );
161 if( (dir = opendir( psz_fullpath )) )
163 /* Parse the directory and try to load all files it contains. */
164 while( (file = readdir( dir )) )
166 int i_filelen = strlen( file->d_name );
168 /* We only load files ending with ".so" */
170 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
172 psz_file = malloc( i_dirlen + i_filelen + 2 );
173 if( psz_file == NULL )
177 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
179 /* We created a nice filename -- now we just try to load
180 * it as a dynamic module. */
181 AllocateDynModule( p_bank, psz_file );
183 /* We don't care if the allocation succeeded */
188 /* Close the directory if successfully opened */
195 free( psz_fullpath );
203 /*****************************************************************************
204 * module_DestroyBank: destroy the module bank.
205 *****************************************************************************
206 * This function unloads all unused dynamic modules and removes the module
207 * bank in case of success.
208 *****************************************************************************/
209 void module_DestroyBank( module_bank_t * p_bank )
213 while( p_bank->first != NULL )
215 if( FreeModule( p_bank, p_bank->first ) )
217 /* Module deletion failed */
218 intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
219 p_bank->first->psz_name );
221 /* We just free the module by hand. Niahahahahaha. */
222 p_next = p_bank->first->next;
224 p_bank->first = p_next;
228 /* Destroy the lock */
229 vlc_mutex_destroy( &p_bank->lock );
231 /* We can free the module bank */
237 /*****************************************************************************
238 * module_ResetBank: reset the module bank.
239 *****************************************************************************
240 * This function resets the module bank by unloading all unused dynamic
242 *****************************************************************************/
243 void module_ResetBank( module_bank_t * p_bank )
245 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
249 /*****************************************************************************
250 * module_ManageBank: manage the module bank.
251 *****************************************************************************
252 * This function parses the module bank and hides modules that have been
253 * unused for a while.
254 *****************************************************************************/
255 void module_ManageBank( module_bank_t * p_bank )
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 dynamic 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( 1, "module: hiding unused module `%s'",
277 p_module->psz_name );
278 HideModule( p_module );
283 /* We release the global lock */
284 vlc_mutex_unlock( &p_bank->lock );
289 /*****************************************************************************
290 * module_Need: return the best module function, given a capability list.
291 *****************************************************************************
292 * This function returns the module that best fits the asked capabilities.
293 *****************************************************************************/
294 module_t * module_Need( module_bank_t *p_bank,
295 int i_capabilities, void *p_data )
298 module_t * p_bestmodule = NULL;
299 int i_score, i_totalscore, i_bestscore = 0;
302 /* We take the global lock */
303 vlc_mutex_lock( &p_bank->lock );
305 /* Parse the module list for capabilities and probe each of them */
306 for( p_module = p_bank->first ;
308 p_module = p_module->next )
310 /* Test that this module can do everything we need */
311 if( ( p_module->i_capabilities & i_capabilities ) == i_capabilities )
315 LockModule( p_module );
317 /* Parse all the requested capabilities and test them */
318 for( i_index = 0 ; (1 << i_index) <= i_capabilities ; i_index++ )
320 if( ( (1 << i_index) & i_capabilities ) )
322 i_score = ( (function_list_t *)p_module->p_functions)
323 [i_index].pf_probe( p_data );
327 i_totalscore += i_score;
336 /* If the high score was broken, we have a new champion */
337 if( i_totalscore > i_bestscore )
339 /* Keep the current module locked, but release the previous */
340 if( p_bestmodule != NULL )
342 UnlockModule( p_bestmodule );
345 /* This is the new best module */
346 i_bestscore = i_totalscore;
347 p_bestmodule = p_module;
351 /* This module wasn't interesting, unlock it and forget it */
352 UnlockModule( p_module );
357 /* We release the global lock */
358 vlc_mutex_unlock( &p_bank->lock );
360 if( p_bestmodule != NULL )
362 intf_WarnMsg( 1, "module: locking module `%s'",
363 p_bestmodule->psz_name );
366 /* Don't forget that the module is still locked if bestmodule != NULL */
367 return( p_bestmodule );
370 /*****************************************************************************
371 * module_Unneed: decrease the usage count of a module.
372 *****************************************************************************
373 * This function must be called by the thread that called module_Need, to
374 * decrease the reference count and allow for hiding of modules.
375 *****************************************************************************/
376 void module_Unneed( module_bank_t * p_bank, module_t * p_module )
378 /* We take the global lock */
379 vlc_mutex_lock( &p_bank->lock );
381 /* Just unlock the module - we can't do anything if it fails,
382 * so there is no need to check the return value. */
383 UnlockModule( p_module );
385 intf_WarnMsg( 1, "module: unlocking module `%s'", p_module->psz_name );
387 /* We release the global lock */
388 vlc_mutex_unlock( &p_bank->lock );
393 /*****************************************************************************
394 * Following functions are local.
395 *****************************************************************************/
397 /*****************************************************************************
398 * AllocateDynModule: load a module into memory and initialize it.
399 *****************************************************************************
400 * This function loads a dynamically loadable module and allocates a structure
401 * for its information data. The module can then be handled by module_Need,
402 * module_Unneed and HideModule. It can be removed by FreeModule.
403 *****************************************************************************/
404 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename )
406 module_t * p_module, * p_othermodule;
407 module_handle_t handle;
409 /* Try to dynamically load the module. */
410 if( module_load( psz_filename, &handle ) )
412 /* The dynamic module couldn't be opened */
413 intf_WarnMsgImm( 1, "module warning: cannot open %s (%s)",
414 psz_filename, module_error() );
418 /* Now that we have successfully loaded the module, we can
419 * allocate a structure for it */
420 p_module = malloc( sizeof( module_t ) );
421 if( p_module == NULL )
423 intf_ErrMsg( "module error: can't allocate p_module" );
424 module_unload( handle );
428 /* We need to fill these since they may be needed by CallSymbol() */
429 p_module->psz_filename = psz_filename;
430 p_module->handle = handle;
432 /* Initialize the module : fill p_module->psz_name, etc. */
433 if( CallSymbol( p_module, "InitModule" ) != 0 )
435 /* We couldn't call InitModule() */
437 module_unload( handle );
441 /* Check that version numbers match */
442 if( strcmp( VERSION, p_module->psz_version ) )
445 module_unload( handle );
449 /* Check that we don't already have a module with this name */
450 for( p_othermodule = p_bank->first ;
451 p_othermodule != NULL ;
452 p_othermodule = p_othermodule->next )
454 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
457 module_unload( handle );
462 /* Activate the module : fill the capability structure, etc. */
463 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
465 /* We couldn't call ActivateModule() */
467 module_unload( handle );
471 /* We strdup() these entries so that they are still valid when the
472 * module is unloaded. */
473 p_module->psz_filename = strdup( p_module->psz_filename );
474 p_module->psz_name = strdup( p_module->psz_name );
475 p_module->psz_longname = strdup( p_module->psz_longname );
476 p_module->psz_version = strdup( p_module->psz_version );
477 if( p_module->psz_filename == NULL
478 || p_module->psz_name == NULL
479 || p_module->psz_longname == NULL
480 || p_module->psz_version == NULL )
482 intf_ErrMsg( "module error: can't duplicate strings" );
483 free( p_module->psz_filename );
484 free( p_module->psz_name );
485 free( p_module->psz_longname );
486 free( p_module->psz_version );
488 module_unload( handle );
492 /* Everything worked fine ! The module is ready to be added to the list. */
493 p_module->i_usage = 0;
494 p_module->i_unused_delay = 0;
496 p_module->b_builtin = 0;
498 /* Link module into the linked list */
499 if( p_bank->first != NULL )
501 p_bank->first->prev = p_module;
503 p_module->next = p_bank->first;
504 p_module->prev = NULL;
505 p_bank->first = p_module;
507 /* Immediate message so that a slow module doesn't make the user wait */
508 intf_WarnMsgImm( 1, "module: dynamic module `%s', %s",
509 p_module->psz_name, p_module->psz_longname );
514 /*****************************************************************************
515 * HideModule: remove a module from memory but keep its structure.
516 *****************************************************************************
517 * This function can only be called if i_usage == 0. It will make a call
518 * to the module's inner DeactivateModule() symbol, and then unload it
519 * from memory. A call to module_Need() will automagically load it again.
520 *****************************************************************************/
521 static int HideModule( module_t * p_module )
523 if( p_module->b_builtin )
525 /* A built-in module should never be hidden. */
526 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
527 p_module->psz_name );
531 if( p_module->i_usage >= 1 )
533 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
534 " in use", p_module->psz_name );
538 if( p_module->i_usage <= -1 )
540 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
541 " hidden", p_module->psz_name );
545 /* Deactivate the module : free the capability structure, etc. */
546 if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
548 /* We couldn't call DeactivateModule() -- looks nasty, but
549 * we can't do much about it. Just try to unload module anyway. */
550 module_unload( p_module->handle );
551 p_module->i_usage = -1;
555 /* Everything worked fine, we can safely unload the module. */
556 module_unload( p_module->handle );
557 p_module->i_usage = -1;
562 /*****************************************************************************
563 * FreeModule: delete a module and its structure.
564 *****************************************************************************
565 * This function can only be called if i_usage <= 0.
566 *****************************************************************************/
567 static int FreeModule( module_bank_t * p_bank, module_t * p_module )
569 /* If the module is not in use but is still in memory, we first have
570 * to hide it and remove it from memory before we can free the
572 if( p_module->b_builtin )
574 if( p_module->i_usage != 0 )
576 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
577 " usage %i", p_module->psz_name, p_module->i_usage );
583 if( p_module->i_usage >= 1 )
585 intf_ErrMsg( "module error: trying to free module `%s' which is"
586 " still in use", p_module->psz_name );
590 /* Two possibilities here: i_usage == -1 and the module is already
591 * unloaded, we can continue, or i_usage == 0, and we have to hide
592 * the module before going on. */
593 if( p_module->i_usage == 0 )
595 if( HideModule( p_module ) != 0 )
602 /* Unlink the module from the linked list. */
603 if( p_module == p_bank->first )
605 p_bank->first = p_module->next;
608 if( p_module->prev != NULL )
610 p_module->prev->next = p_module->next;
613 if( p_module->next != NULL )
615 p_module->next->prev = p_module->prev;
618 /* We free the structures that we strdup()ed in Allocate*Module(). */
619 free( p_module->psz_filename );
620 free( p_module->psz_name );
621 free( p_module->psz_longname );
622 free( p_module->psz_version );
629 /*****************************************************************************
630 * LockModule: increase the usage count of a module and load it if needed.
631 *****************************************************************************
632 * This function has to be called before a thread starts using a module. If
633 * the module is already loaded, we just increase its usage count. If it isn't
634 * loaded, we have to dynamically open it and initialize it.
635 * If you successfully call LockModule() at any moment, be careful to call
636 * UnlockModule() when you don't need it anymore.
637 *****************************************************************************/
638 static int LockModule( module_t * p_module )
640 if( p_module->i_usage >= 0 )
642 /* This module is already loaded and activated, we can return */
647 if( p_module->b_builtin )
649 /* A built-in module should always have a refcount >= 0 ! */
650 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
651 p_module->psz_name, p_module->i_usage );
655 if( p_module->i_usage != -1 )
657 /* This shouldn't happen. Ever. We have serious problems here. */
658 intf_ErrMsg( "module error: dynamic module `%s' has refcount %i",
659 p_module->psz_name, p_module->i_usage );
663 /* i_usage == -1, which means that the module isn't in memory */
664 if( module_load( p_module->psz_filename, &p_module->handle ) )
666 /* The dynamic module couldn't be opened */
667 intf_ErrMsg( "module error: cannot open %s (%s)",
668 p_module->psz_filename, module_error() );
672 /* FIXME: what to do if the guy modified the plugin while it was
673 * unloaded ? It makes XMMS crash nastily, perhaps we should try
674 * to be a bit more clever here. */
676 /* Activate the module : fill the capability structure, etc. */
677 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
679 /* We couldn't call ActivateModule() -- looks nasty, but
680 * we can't do much about it. Just try to unload module. */
681 module_unload( p_module->handle );
682 p_module->i_usage = -1;
686 /* Everything worked fine ! The module is ready to be used */
687 p_module->i_usage = 1;
692 /*****************************************************************************
693 * UnlockModule: decrease the usage count of a module.
694 *****************************************************************************
695 * We decrease the usage count of a module so that we know when a module
696 * becomes unused and can be hidden.
697 *****************************************************************************/
698 static int UnlockModule( module_t * p_module )
700 if( p_module->i_usage <= 0 )
702 /* This shouldn't happen. Ever. We have serious problems here. */
703 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
704 " which isn't even in use", p_module->psz_name );
708 /* This module is still in use, we can return */
710 p_module->i_unused_delay = 0;
715 /*****************************************************************************
716 * CallSymbol: calls a module symbol.
717 *****************************************************************************
718 * This function calls a symbol given its name and a module structure. The
719 * symbol MUST refer to a function returning int and taking a module_t* as
721 *****************************************************************************/
722 static int CallSymbol( module_t * p_module, char * psz_name )
724 typedef int ( symbol_t ) ( module_t * p_module );
727 /* Try to resolve the symbol */
728 p_symbol = module_getsymbol( p_module->handle, psz_name );
732 /* We couldn't load the symbol */
733 intf_WarnMsg( 1, "module warning: "
734 "cannot find symbol %s in module %s (%s)",
735 psz_name, p_module->psz_filename, module_error() );
739 /* We can now try to call the symbol */
740 if( p_symbol( p_module ) != 0 )
742 /* With a well-written module we shouldn't have to print an
743 * additional error message here, but just make sure. */
744 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
745 psz_name, p_module->psz_filename );
749 /* Everything worked fine, we can return */