1 /*****************************************************************************
2 * modules.c : Built-in and dynamic modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.19 2001/03/21 13:42:34 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"
59 #include "modules_core.h"
61 /* Local prototypes */
62 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename );
63 static int HideModule( module_t * p_module );
64 static int FreeModule( module_bank_t * p_bank, module_t * p_module );
65 static int LockModule( module_t * p_module );
66 static int UnlockModule( module_t * p_module );
67 static int CallSymbol( module_t * p_module, char * psz_name );
69 /*****************************************************************************
70 * module_CreateBank: create the module bank.
71 *****************************************************************************
72 * This function creates a module bank structure.
73 *****************************************************************************/
74 module_bank_t * module_CreateBank( void )
76 module_bank_t * p_bank;
78 p_bank = malloc( sizeof( module_bank_t ) );
83 /*****************************************************************************
84 * module_InitBank: create the module bank.
85 *****************************************************************************
86 * This function creates a module bank structure and fills it with the
87 * built-in modules, as well as all the dynamic modules it can find.
88 *****************************************************************************/
89 void module_InitBank( module_bank_t * p_bank )
91 static char * path[] = { ".", "lib", PLUGIN_PATH, NULL };
93 char ** ppsz_path = path;
97 char * psz_vlcpath = beos_GetProgramPath();
98 int i_vlclen = strlen( psz_vlcpath );
99 boolean_t b_notinroot;
102 struct dirent * file;
104 p_bank->first = NULL;
105 vlc_mutex_init( &p_bank->lock );
107 intf_WarnMsg( 1, "module: module bank initialized" );
109 for( ; *ppsz_path != NULL ; ppsz_path++ )
111 /* Store strlen(*ppsz_path) for later use. */
112 int i_dirlen = strlen( *ppsz_path );
116 /* Under BeOS, we need to add beos_GetProgramPath() to access
117 * files under the current directory */
118 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
120 i_dirlen += i_vlclen + 2;
123 psz_fullpath = malloc( i_dirlen );
124 if( psz_fullpath == NULL )
128 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
133 psz_fullpath = *ppsz_path;
136 intf_WarnMsgImm( 2, "module: browsing %s", psz_fullpath );
138 if( (dir = opendir( psz_fullpath )) )
140 /* Parse the directory and try to load all files it contains. */
141 while( (file = readdir( dir )) )
143 int i_filelen = strlen( file->d_name );
145 /* We only load files ending with ".so" */
147 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
149 psz_file = malloc( i_dirlen + i_filelen + 2 );
150 if( psz_file == NULL )
154 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
156 /* We created a nice filename -- now we just try to load
157 * it as a dynamic module. */
158 AllocateDynModule( p_bank, psz_file );
160 /* We don't care if the allocation succeeded */
165 /* Close the directory if successfully opened */
172 free( psz_fullpath );
180 /*****************************************************************************
181 * module_DestroyBank: destroy the module bank.
182 *****************************************************************************
183 * This function unloads all unused dynamic modules and removes the module
184 * bank in case of success.
185 *****************************************************************************/
186 void module_DestroyBank( module_bank_t * p_bank )
190 while( p_bank->first != NULL )
192 if( FreeModule( p_bank, p_bank->first ) )
194 /* Module deletion failed */
195 intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
196 p_bank->first->psz_name );
198 /* We just free the module by hand. Niahahahahaha. */
199 p_next = p_bank->first->next;
201 p_bank->first = p_next;
205 /* Destroy the lock */
206 vlc_mutex_destroy( &p_bank->lock );
208 /* We can free the module bank */
214 /*****************************************************************************
215 * module_ResetBank: reset the module bank.
216 *****************************************************************************
217 * This function resets the module bank by unloading all unused dynamic
219 *****************************************************************************/
220 void module_ResetBank( module_bank_t * p_bank )
222 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
226 /*****************************************************************************
227 * module_ManageBank: manage the module bank.
228 *****************************************************************************
229 * This function parses the module bank and hides modules that have been
230 * unused for a while.
231 *****************************************************************************/
232 void module_ManageBank( module_bank_t * p_bank )
236 /* We take the global lock */
237 vlc_mutex_lock( &p_bank->lock );
239 /* Parse the module list to see if any modules need to be unloaded */
240 for( p_module = p_bank->first ;
242 p_module = p_module->next )
244 /* If the module is unused and if it is a dynamic module... */
245 if( p_module->i_usage == 0 && !p_module->b_builtin )
247 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
249 p_module->i_unused_delay++;
253 intf_WarnMsg( 1, "module: hiding unused module `%s'",
254 p_module->psz_name );
255 HideModule( p_module );
260 /* We release the global lock */
261 vlc_mutex_unlock( &p_bank->lock );
266 /*****************************************************************************
267 * module_Need: return the best module function, given a capability list.
268 *****************************************************************************
269 * This function returns the module that best fits the asked capabilities.
270 *****************************************************************************/
271 module_t * module_Need( module_bank_t *p_bank,
272 int i_capabilities, void *p_data )
275 module_t * p_bestmodule = NULL;
276 int i_score, i_totalscore, i_bestscore = 0;
279 /* We take the global lock */
280 vlc_mutex_lock( &p_bank->lock );
282 /* Parse the module list for capabilities and probe each of them */
283 for( p_module = p_bank->first ;
285 p_module = p_module->next )
287 /* Test that this module can do everything we need */
288 if( ( p_module->i_capabilities & i_capabilities ) == i_capabilities )
292 LockModule( p_module );
294 /* Parse all the requested capabilities and test them */
295 for( i_index = 0 ; (1 << i_index) <= i_capabilities ; i_index++ )
297 if( ( (1 << i_index) & i_capabilities ) )
299 i_score = ( (function_list_t *)p_module->p_functions)
300 [i_index].pf_probe( p_data );
304 i_totalscore += i_score;
313 /* If the high score was broken, we have a new champion */
314 if( i_totalscore > i_bestscore )
316 /* Keep the current module locked, but release the previous */
317 if( p_bestmodule != NULL )
319 UnlockModule( p_bestmodule );
322 /* This is the new best module */
323 i_bestscore = i_totalscore;
324 p_bestmodule = p_module;
328 /* This module wasn't interesting, unlock it and forget it */
329 UnlockModule( p_module );
334 /* We release the global lock */
335 vlc_mutex_unlock( &p_bank->lock );
337 if( p_bestmodule != NULL )
339 intf_WarnMsg( 1, "module: locking module `%s'",
340 p_bestmodule->psz_name );
343 /* Don't forget that the module is still locked if bestmodule != NULL */
344 return( p_bestmodule );
347 /*****************************************************************************
348 * module_Unneed: decrease the usage count of a module.
349 *****************************************************************************
350 * This function must be called by the thread that called module_Need, to
351 * decrease the reference count and allow for hiding of modules.
352 *****************************************************************************/
353 void module_Unneed( module_bank_t * p_bank, module_t * p_module )
355 /* We take the global lock */
356 vlc_mutex_lock( &p_bank->lock );
358 /* Just unlock the module - we can't do anything if it fails,
359 * so there is no need to check the return value. */
360 UnlockModule( p_module );
362 intf_WarnMsg( 1, "module: unlocking module `%s'", p_module->psz_name );
364 /* We release the global lock */
365 vlc_mutex_unlock( &p_bank->lock );
370 /*****************************************************************************
371 * Following functions are local.
372 *****************************************************************************/
374 /*****************************************************************************
375 * AllocateDynModule: load a module into memory and initialize it.
376 *****************************************************************************
377 * This function loads a dynamically loadable module and allocates a structure
378 * for its information data. The module can then be handled by module_Need,
379 * module_Unneed and HideModule. It can be removed by FreeModule.
380 *****************************************************************************/
381 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename )
383 module_t * p_module, * p_othermodule;
384 module_handle_t handle;
386 /* Try to dynamically load the module. */
387 if( module_load( psz_filename, &handle ) )
389 /* The dynamic module couldn't be opened */
390 intf_WarnMsgImm( 1, "module warning: cannot open %s (%s)",
391 psz_filename, module_error() );
395 /* Now that we have successfully loaded the module, we can
396 * allocate a structure for it */
397 p_module = malloc( sizeof( module_t ) );
398 if( p_module == NULL )
400 intf_ErrMsg( "module error: can't allocate p_module" );
401 module_unload( handle );
405 /* We need to fill these since they may be needed by CallSymbol() */
406 p_module->psz_filename = psz_filename;
407 p_module->handle = handle;
409 /* Initialize the module : fill p_module->psz_name, etc. */
410 if( CallSymbol( p_module, "InitModule" ) != 0 )
412 /* We couldn't call InitModule() */
414 module_unload( handle );
418 /* Check that version numbers match */
419 if( strcmp( VERSION, p_module->psz_version ) )
422 module_unload( handle );
426 /* Check that we don't already have a module with this name */
427 for( p_othermodule = p_bank->first ;
428 p_othermodule != NULL ;
429 p_othermodule = p_othermodule->next )
431 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
434 module_unload( handle );
439 /* Activate the module : fill the capability structure, etc. */
440 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
442 /* We couldn't call ActivateModule() */
444 module_unload( handle );
448 /* We strdup() these entries so that they are still valid when the
449 * module is unloaded. */
450 p_module->psz_filename = strdup( p_module->psz_filename );
451 p_module->psz_name = strdup( p_module->psz_name );
452 p_module->psz_longname = strdup( p_module->psz_longname );
453 p_module->psz_version = strdup( p_module->psz_version );
454 if( p_module->psz_filename == NULL
455 || p_module->psz_name == NULL
456 || p_module->psz_longname == NULL
457 || p_module->psz_version == NULL )
459 intf_ErrMsg( "module error: can't duplicate strings" );
460 free( p_module->psz_filename );
461 free( p_module->psz_name );
462 free( p_module->psz_longname );
463 free( p_module->psz_version );
465 module_unload( handle );
469 /* Everything worked fine ! The module is ready to be added to the list. */
470 p_module->i_usage = 0;
471 p_module->i_unused_delay = 0;
473 p_module->b_builtin = 0;
475 /* Link module into the linked list */
476 if( p_bank->first != NULL )
478 p_bank->first->prev = p_module;
480 p_module->next = p_bank->first;
481 p_module->prev = NULL;
482 p_bank->first = p_module;
484 /* Immediate message so that a slow module doesn't make the user wait */
485 intf_WarnMsgImm( 1, "module: dynamic module `%s', %s",
486 p_module->psz_name, p_module->psz_longname );
491 /*****************************************************************************
492 * HideModule: remove a module from memory but keep its structure.
493 *****************************************************************************
494 * This function can only be called if i_usage == 0. It will make a call
495 * to the module's inner DeactivateModule() symbol, and then unload it
496 * from memory. A call to module_Need() will automagically load it again.
497 *****************************************************************************/
498 static int HideModule( module_t * p_module )
500 if( p_module->b_builtin )
502 /* A built-in module should never be hidden. */
503 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
504 p_module->psz_name );
508 if( p_module->i_usage >= 1 )
510 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
511 " in use", p_module->psz_name );
515 if( p_module->i_usage <= -1 )
517 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
518 " hidden", p_module->psz_name );
522 /* Deactivate the module : free the capability structure, etc. */
523 if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
525 /* We couldn't call DeactivateModule() -- looks nasty, but
526 * we can't do much about it. Just try to unload module anyway. */
527 module_unload( p_module->handle );
528 p_module->i_usage = -1;
532 /* Everything worked fine, we can safely unload the module. */
533 module_unload( p_module->handle );
534 p_module->i_usage = -1;
539 /*****************************************************************************
540 * FreeModule: delete a module and its structure.
541 *****************************************************************************
542 * This function can only be called if i_usage <= 0.
543 *****************************************************************************/
544 static int FreeModule( module_bank_t * p_bank, module_t * p_module )
546 /* If the module is not in use but is still in memory, we first have
547 * to hide it and remove it from memory before we can free the
549 if( p_module->b_builtin )
551 if( p_module->i_usage != 0 )
553 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
554 " usage %i", p_module->psz_name, p_module->i_usage );
560 if( p_module->i_usage >= 1 )
562 intf_ErrMsg( "module error: trying to free module `%s' which is"
563 " still in use", p_module->psz_name );
567 /* Two possibilities here: i_usage == -1 and the module is already
568 * unloaded, we can continue, or i_usage == 0, and we have to hide
569 * the module before going on. */
570 if( p_module->i_usage == 0 )
572 if( HideModule( p_module ) != 0 )
579 /* Unlink the module from the linked list. */
580 if( p_module == p_bank->first )
582 p_bank->first = p_module->next;
585 if( p_module->prev != NULL )
587 p_module->prev->next = p_module->next;
590 if( p_module->next != NULL )
592 p_module->next->prev = p_module->prev;
595 /* We free the structures that we strdup()ed in Allocate*Module(). */
596 free( p_module->psz_filename );
597 free( p_module->psz_name );
598 free( p_module->psz_longname );
599 free( p_module->psz_version );
606 /*****************************************************************************
607 * LockModule: increase the usage count of a module and load it if needed.
608 *****************************************************************************
609 * This function has to be called before a thread starts using a module. If
610 * the module is already loaded, we just increase its usage count. If it isn't
611 * loaded, we have to dynamically open it and initialize it.
612 * If you successfully call LockModule() at any moment, be careful to call
613 * UnlockModule() when you don't need it anymore.
614 *****************************************************************************/
615 static int LockModule( module_t * p_module )
617 if( p_module->i_usage >= 0 )
619 /* This module is already loaded and activated, we can return */
624 if( p_module->b_builtin )
626 /* A built-in module should always have a refcount >= 0 ! */
627 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
628 p_module->psz_name, p_module->i_usage );
632 if( p_module->i_usage != -1 )
634 /* This shouldn't happen. Ever. We have serious problems here. */
635 intf_ErrMsg( "module error: dynamic module `%s' has refcount %i",
636 p_module->psz_name, p_module->i_usage );
640 /* i_usage == -1, which means that the module isn't in memory */
641 if( module_load( p_module->psz_filename, &p_module->handle ) )
643 /* The dynamic module couldn't be opened */
644 intf_ErrMsg( "module error: cannot open %s (%s)",
645 p_module->psz_filename, module_error() );
649 /* FIXME: what to do if the guy modified the plugin while it was
650 * unloaded ? It makes XMMS crash nastily, perhaps we should try
651 * to be a bit more clever here. */
653 /* Activate the module : fill the capability structure, etc. */
654 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
656 /* We couldn't call ActivateModule() -- looks nasty, but
657 * we can't do much about it. Just try to unload module. */
658 module_unload( p_module->handle );
659 p_module->i_usage = -1;
663 /* Everything worked fine ! The module is ready to be used */
664 p_module->i_usage = 1;
669 /*****************************************************************************
670 * UnlockModule: decrease the usage count of a module.
671 *****************************************************************************
672 * We decrease the usage count of a module so that we know when a module
673 * becomes unused and can be hidden.
674 *****************************************************************************/
675 static int UnlockModule( module_t * p_module )
677 if( p_module->i_usage <= 0 )
679 /* This shouldn't happen. Ever. We have serious problems here. */
680 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
681 " which isn't even in use", p_module->psz_name );
685 /* This module is still in use, we can return */
687 p_module->i_unused_delay = 0;
692 /*****************************************************************************
693 * CallSymbol: calls a module symbol.
694 *****************************************************************************
695 * This function calls a symbol given its name and a module structure. The
696 * symbol MUST refer to a function returning int and taking a module_t* as
698 *****************************************************************************/
699 static int CallSymbol( module_t * p_module, char * psz_name )
701 typedef int ( symbol_t ) ( module_t * p_module );
704 /* Try to resolve the symbol */
705 p_symbol = module_getsymbol( p_module->handle, psz_name );
709 /* We couldn't load the symbol */
710 intf_WarnMsg( 1, "module warning: "
711 "cannot find symbol %s in module %s (%s)",
712 psz_name, p_module->psz_filename, module_error() );
716 /* We can now try to call the symbol */
717 if( p_symbol( p_module ) != 0 )
719 /* With a well-written module we shouldn't have to print an
720 * additional error message here, but just make sure. */
721 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
722 psz_name, p_module->psz_filename );
726 /* Everything worked fine, we can return */