1 /*****************************************************************************
2 * modules.c : Built-in and dynamic modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.22 2001/04/12 01:52:45 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() */
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"
55 # include "darwin_specific.h"
63 #include "modules_core.h"
65 /* Local prototypes */
66 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename );
67 static int HideModule( module_t * p_module );
68 static int FreeModule( module_bank_t * p_bank, module_t * p_module );
69 static int LockModule( module_t * p_module );
70 static int UnlockModule( module_t * p_module );
71 static int CallSymbol( module_t * p_module, char * psz_name );
73 /*****************************************************************************
74 * module_CreateBank: create the module bank.
75 *****************************************************************************
76 * This function creates a module bank structure.
77 *****************************************************************************/
78 module_bank_t * module_CreateBank( void )
80 module_bank_t * p_bank;
82 p_bank = malloc( sizeof( module_bank_t ) );
87 /*****************************************************************************
88 * module_InitBank: create the module bank.
89 *****************************************************************************
90 * This function creates a module bank structure and fills it with the
91 * built-in modules, as well as all the dynamic modules it can find.
92 *****************************************************************************/
93 void module_InitBank( module_bank_t * p_bank )
95 static char * path[] = { ".", "lib", PLUGIN_PATH, NULL, NULL };
97 char ** ppsz_path = path;
100 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
101 char * psz_vlcpath = system_GetProgramPath();
102 int i_vlclen = strlen( psz_vlcpath );
103 boolean_t b_notinroot;
106 struct dirent * file;
108 p_bank->first = NULL;
109 vlc_mutex_init( &p_bank->lock );
111 intf_WarnMsg( 1, "module: module bank initialized" );
113 for( ; *ppsz_path != NULL ; ppsz_path++ )
115 /* Store strlen(*ppsz_path) for later use. */
116 int i_dirlen = strlen( *ppsz_path );
118 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
120 /* Under BeOS, we need to add beos_GetProgramPath() to access
121 * files under the current directory */
122 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
124 i_dirlen += i_vlclen + 2;
127 psz_fullpath = malloc( i_dirlen );
128 if( psz_fullpath == NULL )
132 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
137 psz_fullpath = *ppsz_path;
140 intf_WarnMsgImm( 2, "module: browsing %s", psz_fullpath );
142 if( (dir = opendir( psz_fullpath )) )
144 /* Parse the directory and try to load all files it contains. */
145 while( (file = readdir( dir )) )
147 int i_filelen = strlen( file->d_name );
149 /* We only load files ending with ".so" */
151 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
153 psz_file = malloc( i_dirlen + i_filelen + 2 );
154 if( psz_file == NULL )
158 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
160 /* We created a nice filename -- now we just try to load
161 * it as a dynamic module. */
162 AllocateDynModule( p_bank, psz_file );
164 /* We don't care if the allocation succeeded */
169 /* Close the directory if successfully opened */
173 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
176 free( psz_fullpath );
184 /*****************************************************************************
185 * module_DestroyBank: destroy the module bank.
186 *****************************************************************************
187 * This function unloads all unused dynamic modules and removes the module
188 * bank in case of success.
189 *****************************************************************************/
190 void module_DestroyBank( module_bank_t * p_bank )
194 while( p_bank->first != NULL )
196 if( FreeModule( p_bank, p_bank->first ) )
198 /* Module deletion failed */
199 intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
200 p_bank->first->psz_name );
202 /* We just free the module by hand. Niahahahahaha. */
203 p_next = p_bank->first->next;
205 p_bank->first = p_next;
209 /* Destroy the lock */
210 vlc_mutex_destroy( &p_bank->lock );
212 /* We can free the module bank */
218 /*****************************************************************************
219 * module_ResetBank: reset the module bank.
220 *****************************************************************************
221 * This function resets the module bank by unloading all unused dynamic
223 *****************************************************************************/
224 void module_ResetBank( module_bank_t * p_bank )
226 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
230 /*****************************************************************************
231 * module_ManageBank: manage the module bank.
232 *****************************************************************************
233 * This function parses the module bank and hides modules that have been
234 * unused for a while.
235 *****************************************************************************/
236 void module_ManageBank( module_bank_t * p_bank )
240 /* We take the global lock */
241 vlc_mutex_lock( &p_bank->lock );
243 /* Parse the module list to see if any modules need to be unloaded */
244 for( p_module = p_bank->first ;
246 p_module = p_module->next )
248 /* If the module is unused and if it is a dynamic module... */
249 if( p_module->i_usage == 0 && !p_module->b_builtin )
251 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
253 p_module->i_unused_delay++;
257 intf_WarnMsg( 1, "module: hiding unused module `%s'",
258 p_module->psz_name );
259 HideModule( p_module );
264 /* We release the global lock */
265 vlc_mutex_unlock( &p_bank->lock );
270 /*****************************************************************************
271 * module_Need: return the best module function, given a capability list.
272 *****************************************************************************
273 * This function returns the module that best fits the asked capabilities.
274 *****************************************************************************/
275 module_t * module_Need( module_bank_t *p_bank,
276 int i_capabilities, void *p_data )
279 module_t * p_bestmodule = NULL;
280 int i_score, i_totalscore, i_bestscore = 0;
283 /* We take the global lock */
284 vlc_mutex_lock( &p_bank->lock );
286 /* Parse the module list for capabilities and probe each of them */
287 for( p_module = p_bank->first ;
289 p_module = p_module->next )
291 /* Test that this module can do everything we need */
292 if( ( p_module->i_capabilities & i_capabilities ) == i_capabilities )
296 LockModule( p_module );
298 /* Parse all the requested capabilities and test them */
299 for( i_index = 0 ; (1 << i_index) <= i_capabilities ; i_index++ )
301 if( ( (1 << i_index) & i_capabilities ) )
303 i_score = ( (function_list_t *)p_module->p_functions)
304 [i_index].pf_probe( p_data );
308 i_totalscore += i_score;
317 /* If the high score was broken, we have a new champion */
318 if( i_totalscore > i_bestscore )
320 /* Keep the current module locked, but release the previous */
321 if( p_bestmodule != NULL )
323 UnlockModule( p_bestmodule );
326 /* This is the new best module */
327 i_bestscore = i_totalscore;
328 p_bestmodule = p_module;
332 /* This module wasn't interesting, unlock it and forget it */
333 UnlockModule( p_module );
338 /* We release the global lock */
339 vlc_mutex_unlock( &p_bank->lock );
341 if( p_bestmodule != NULL )
343 intf_WarnMsg( 1, "module: locking module `%s'",
344 p_bestmodule->psz_name );
347 /* Don't forget that the module is still locked if bestmodule != NULL */
348 return( p_bestmodule );
351 /*****************************************************************************
352 * module_Unneed: decrease the usage count of a module.
353 *****************************************************************************
354 * This function must be called by the thread that called module_Need, to
355 * decrease the reference count and allow for hiding of modules.
356 *****************************************************************************/
357 void module_Unneed( module_bank_t * p_bank, module_t * p_module )
359 /* We take the global lock */
360 vlc_mutex_lock( &p_bank->lock );
362 /* Just unlock the module - we can't do anything if it fails,
363 * so there is no need to check the return value. */
364 UnlockModule( p_module );
366 intf_WarnMsg( 1, "module: unlocking module `%s'", p_module->psz_name );
368 /* We release the global lock */
369 vlc_mutex_unlock( &p_bank->lock );
374 /*****************************************************************************
375 * Following functions are local.
376 *****************************************************************************/
378 /*****************************************************************************
379 * AllocateDynModule: load a module into memory and initialize it.
380 *****************************************************************************
381 * This function loads a dynamically loadable module and allocates a structure
382 * for its information data. The module can then be handled by module_Need,
383 * module_Unneed and HideModule. It can be removed by FreeModule.
384 *****************************************************************************/
385 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename )
387 module_t * p_module, * p_othermodule;
388 module_handle_t handle;
390 /* Try to dynamically load the module. */
391 if( module_load( psz_filename, &handle ) )
393 /* The dynamic module couldn't be opened */
394 intf_WarnMsgImm( 1, "module warning: cannot open %s (%s)",
395 psz_filename, module_error() );
399 /* Now that we have successfully loaded the module, we can
400 * allocate a structure for it */
401 p_module = malloc( sizeof( module_t ) );
402 if( p_module == NULL )
404 intf_ErrMsg( "module error: can't allocate p_module" );
405 module_unload( handle );
409 /* We need to fill these since they may be needed by CallSymbol() */
410 p_module->psz_filename = psz_filename;
411 p_module->handle = handle;
413 /* Initialize the module : fill p_module->psz_name, etc. */
414 if( CallSymbol( p_module, "InitModule" ) != 0 )
416 /* We couldn't call InitModule() */
418 module_unload( handle );
422 /* Check that version numbers match */
423 if( strcmp( VERSION, p_module->psz_version ) )
426 module_unload( handle );
430 /* Check that we don't already have a module with this name */
431 for( p_othermodule = p_bank->first ;
432 p_othermodule != NULL ;
433 p_othermodule = p_othermodule->next )
435 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
438 module_unload( handle );
443 /* Activate the module : fill the capability structure, etc. */
444 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
446 /* We couldn't call ActivateModule() */
448 module_unload( handle );
452 /* We strdup() these entries so that they are still valid when the
453 * module is unloaded. */
454 p_module->psz_filename = strdup( p_module->psz_filename );
455 p_module->psz_name = strdup( p_module->psz_name );
456 p_module->psz_longname = strdup( p_module->psz_longname );
457 p_module->psz_version = strdup( p_module->psz_version );
458 if( p_module->psz_filename == NULL
459 || p_module->psz_name == NULL
460 || p_module->psz_longname == NULL
461 || p_module->psz_version == NULL )
463 intf_ErrMsg( "module error: can't duplicate strings" );
464 free( p_module->psz_filename );
465 free( p_module->psz_name );
466 free( p_module->psz_longname );
467 free( p_module->psz_version );
469 module_unload( handle );
473 /* Everything worked fine ! The module is ready to be added to the list. */
474 p_module->i_usage = 0;
475 p_module->i_unused_delay = 0;
477 p_module->b_builtin = 0;
479 /* Link module into the linked list */
480 if( p_bank->first != NULL )
482 p_bank->first->prev = p_module;
484 p_module->next = p_bank->first;
485 p_module->prev = NULL;
486 p_bank->first = p_module;
488 /* Immediate message so that a slow module doesn't make the user wait */
489 intf_WarnMsgImm( 1, "module: dynamic module `%s', %s",
490 p_module->psz_name, p_module->psz_longname );
495 /*****************************************************************************
496 * HideModule: remove a module from memory but keep its structure.
497 *****************************************************************************
498 * This function can only be called if i_usage == 0. It will make a call
499 * to the module's inner DeactivateModule() symbol, and then unload it
500 * from memory. A call to module_Need() will automagically load it again.
501 *****************************************************************************/
502 static int HideModule( module_t * p_module )
504 if( p_module->b_builtin )
506 /* A built-in module should never be hidden. */
507 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
508 p_module->psz_name );
512 if( p_module->i_usage >= 1 )
514 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
515 " in use", p_module->psz_name );
519 if( p_module->i_usage <= -1 )
521 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
522 " hidden", p_module->psz_name );
526 /* Deactivate the module : free the capability structure, etc. */
527 if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
529 /* We couldn't call DeactivateModule() -- looks nasty, but
530 * we can't do much about it. Just try to unload module anyway. */
531 module_unload( p_module->handle );
532 p_module->i_usage = -1;
536 /* Everything worked fine, we can safely unload the module. */
537 module_unload( p_module->handle );
538 p_module->i_usage = -1;
543 /*****************************************************************************
544 * FreeModule: delete a module and its structure.
545 *****************************************************************************
546 * This function can only be called if i_usage <= 0.
547 *****************************************************************************/
548 static int FreeModule( module_bank_t * p_bank, module_t * p_module )
550 /* If the module is not in use but is still in memory, we first have
551 * to hide it and remove it from memory before we can free the
553 if( p_module->b_builtin )
555 if( p_module->i_usage != 0 )
557 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
558 " usage %i", p_module->psz_name, p_module->i_usage );
564 if( p_module->i_usage >= 1 )
566 intf_ErrMsg( "module error: trying to free module `%s' which is"
567 " still in use", p_module->psz_name );
571 /* Two possibilities here: i_usage == -1 and the module is already
572 * unloaded, we can continue, or i_usage == 0, and we have to hide
573 * the module before going on. */
574 if( p_module->i_usage == 0 )
576 if( HideModule( p_module ) != 0 )
583 /* Unlink the module from the linked list. */
584 if( p_module == p_bank->first )
586 p_bank->first = p_module->next;
589 if( p_module->prev != NULL )
591 p_module->prev->next = p_module->next;
594 if( p_module->next != NULL )
596 p_module->next->prev = p_module->prev;
599 /* We free the structures that we strdup()ed in Allocate*Module(). */
600 free( p_module->psz_filename );
601 free( p_module->psz_name );
602 free( p_module->psz_longname );
603 free( p_module->psz_version );
610 /*****************************************************************************
611 * LockModule: increase the usage count of a module and load it if needed.
612 *****************************************************************************
613 * This function has to be called before a thread starts using a module. If
614 * the module is already loaded, we just increase its usage count. If it isn't
615 * loaded, we have to dynamically open it and initialize it.
616 * If you successfully call LockModule() at any moment, be careful to call
617 * UnlockModule() when you don't need it anymore.
618 *****************************************************************************/
619 static int LockModule( module_t * p_module )
621 if( p_module->i_usage >= 0 )
623 /* This module is already loaded and activated, we can return */
628 if( p_module->b_builtin )
630 /* A built-in module should always have a refcount >= 0 ! */
631 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
632 p_module->psz_name, p_module->i_usage );
636 if( p_module->i_usage != -1 )
638 /* This shouldn't happen. Ever. We have serious problems here. */
639 intf_ErrMsg( "module error: dynamic module `%s' has refcount %i",
640 p_module->psz_name, p_module->i_usage );
644 /* i_usage == -1, which means that the module isn't in memory */
645 if( module_load( p_module->psz_filename, &p_module->handle ) )
647 /* The dynamic module couldn't be opened */
648 intf_ErrMsg( "module error: cannot open %s (%s)",
649 p_module->psz_filename, module_error() );
653 /* FIXME: what to do if the guy modified the plugin while it was
654 * unloaded ? It makes XMMS crash nastily, perhaps we should try
655 * to be a bit more clever here. */
657 /* Activate the module : fill the capability structure, etc. */
658 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
660 /* We couldn't call ActivateModule() -- looks nasty, but
661 * we can't do much about it. Just try to unload module. */
662 module_unload( p_module->handle );
663 p_module->i_usage = -1;
667 /* Everything worked fine ! The module is ready to be used */
668 p_module->i_usage = 1;
673 /*****************************************************************************
674 * UnlockModule: decrease the usage count of a module.
675 *****************************************************************************
676 * We decrease the usage count of a module so that we know when a module
677 * becomes unused and can be hidden.
678 *****************************************************************************/
679 static int UnlockModule( module_t * p_module )
681 if( p_module->i_usage <= 0 )
683 /* This shouldn't happen. Ever. We have serious problems here. */
684 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
685 " which isn't even in use", p_module->psz_name );
689 /* This module is still in use, we can return */
691 p_module->i_unused_delay = 0;
696 /*****************************************************************************
697 * CallSymbol: calls a module symbol.
698 *****************************************************************************
699 * This function calls a symbol given its name and a module structure. The
700 * symbol MUST refer to a function returning int and taking a module_t* as
702 *****************************************************************************/
703 static int CallSymbol( module_t * p_module, char * psz_name )
705 typedef int ( symbol_t ) ( module_t * p_module );
708 /* Try to resolve the symbol */
709 p_symbol = module_getsymbol( p_module->handle, psz_name );
713 /* We couldn't load the symbol */
714 intf_WarnMsg( 1, "module warning: "
715 "cannot find symbol %s in module %s (%s)",
716 psz_name, p_module->psz_filename, module_error() );
720 /* We can now try to call the symbol */
721 if( p_symbol( p_module ) != 0 )
723 /* With a well-written module we shouldn't have to print an
724 * additional error message here, but just make sure. */
725 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
726 psz_name, p_module->psz_filename );
730 /* Everything worked fine, we can return */