1 /*****************************************************************************
2 * modules.c : Built-in and dynamic modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
27 #include <stdlib.h> /* free(), strtol() */
28 #include <stdio.h> /* sprintf() */
29 #include <string.h> /* strdup() */
32 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
33 #include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
35 #elif defined(HAVE_IMAGE_H) /* BeOS */
39 /* FIXME: this isn't supposed to be an error */
40 #error no dynamic plugins available on your system !
44 #include "beos_specific.h"
52 #include "modules_core.h"
54 /* Local prototypes */
55 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename );
56 static int HideModule( module_t * p_module );
57 static int FreeModule( module_bank_t * p_bank, module_t * p_module );
58 static int CallSymbol( module_t * p_module, char * psz_name );
60 /*****************************************************************************
61 * module_CreateBank: create the module bank.
62 *****************************************************************************
63 * This function creates a module bank structure.
64 *****************************************************************************/
65 module_bank_t * module_CreateBank( void )
67 module_bank_t * p_bank;
69 p_bank = malloc( sizeof( module_bank_t ) );
74 /*****************************************************************************
75 * module_InitBank: create the module bank.
76 *****************************************************************************
77 * This function creates a module bank structure and fills it with the
78 * built-in modules, as well as all the dynamic modules it can find.
79 *****************************************************************************/
80 void module_InitBank( module_bank_t * p_bank )
82 static char * path[] = { ".", "lib", PLUGIN_PATH, NULL } ;
84 char ** ppsz_path = path;
87 char * psz_program_path = beos_GetProgramPath();
88 int i_programlen = strlen( psz_program_path );
94 vlc_mutex_init( &p_bank->lock );
96 intf_Msg( "module: module bank initialized" );
98 for( ; *ppsz_path != NULL ; ppsz_path++ )
100 if( (dir = opendir( *ppsz_path )) )
102 /* Store strlen(*ppsz_path) for later use. */
103 int i_dirlen = strlen( *ppsz_path );
105 /* Parse the directory and try to load all files it contains. */
106 while( (file = readdir( dir )) )
108 if( memcmp( file->d_name, ".", 1 ) )
111 /* Under BeOS, we need to add beos_GetProgramPath() to
112 * access files under the current directory */
113 if( memcmp( file->d_name, "/", 1 ) )
115 psz_file = malloc( i_programlen + i_dirlen
116 + strlen( file->d_name ) + 3 );
117 if( psz_file == NULL )
121 sprintf( psz_file, "%s/%s/%s", psz_programlen,
122 *ppsz_path, file->d_name );
127 psz_file = malloc( i_dirlen
128 + strlen( file->d_name ) + 2 );
129 if( psz_file == NULL )
133 sprintf( psz_file, "%s/%s", *ppsz_path, file->d_name );
136 /* We created a nice filename -- now we just try to load
137 * it as a dynamic module. */
138 AllocateDynModule( p_bank, psz_file );
140 /* We don't care if the allocation succeeded */
150 /*****************************************************************************
151 * module_DestroyBank: destroy the module bank.
152 *****************************************************************************
153 * This function unloads all unused dynamic modules and removes the module
154 * bank in case of success.
155 *****************************************************************************/
156 void module_DestroyBank( module_bank_t * p_bank )
160 while( p_bank->first != NULL )
162 if( FreeModule( p_bank, p_bank->first ) )
164 /* Module deletion failed */
165 intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
166 p_bank->first->psz_name );
168 /* We just free the module by hand. Niahahahahaha. */
169 p_next = p_bank->first->next;
171 p_bank->first = p_next;
175 /* We can free the module bank */
181 /*****************************************************************************
182 * module_ResetBank: reset the module bank.
183 *****************************************************************************
184 * This function resets the module bank by unloading all unused dynamic
186 *****************************************************************************/
187 void module_ResetBank( module_bank_t * p_bank )
189 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
193 /*****************************************************************************
194 * module_ManageBank: manage the module bank.
195 *****************************************************************************
196 * This function parses the module bank and hides modules that have been
197 * unused for a while.
198 *****************************************************************************/
199 void module_ManageBank( module_bank_t * p_bank )
203 /* We take the global lock */
204 vlc_mutex_lock( &p_bank->lock );
206 /* Parse the module list to see if any modules need to be unloaded */
207 for( p_module = p_bank->first ;
209 p_module = p_module->next )
211 /* If the module is unused and if it is a dynamic module... */
212 if( p_module->i_usage == 0 && !p_module->b_builtin )
214 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
216 p_module->i_unused_delay++;
220 intf_Msg( "module: hiding unused module `%s'",
221 p_module->psz_name );
222 HideModule( p_module );
227 /* We release the global lock */
228 vlc_mutex_unlock( &p_bank->lock );
233 /*****************************************************************************
234 * module_Need: increase the usage count of a module and load it if needed.
235 *****************************************************************************
236 * This function has to be called before a thread starts using a module. If
237 * the module is already loaded, we just increase its usage count. If it isn't
238 * loaded, we have to dynamically open it and initialize it.
239 * If you successfully call module_Need() at any moment, be careful to call
240 * module_Unneed() when you don't need it anymore.
241 *****************************************************************************/
242 int module_Need( module_t * p_module )
244 if( p_module->i_usage >= 0 )
246 /* This module is already loaded and activated, we can return */
251 if( p_module->b_builtin )
253 /* A built-in module should always have a refcount >= 0 ! */
254 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
255 p_module->psz_name, p_module->i_usage );
259 if( p_module->i_usage != -1 )
261 /* This shouldn't happen. Ever. We have serious problems here. */
262 intf_ErrMsg( "module error: dynamic module `%s' has refcount %i",
263 p_module->psz_name, p_module->i_usage );
267 /* i_usage == -1, which means that the module isn't in memory */
268 if( ! module_load( p_module->psz_filename, &p_module->handle ) )
270 /* The dynamic module couldn't be opened */
271 intf_ErrMsg( "module error: cannot open %s (%s)",
272 p_module->psz_filename, module_error() );
276 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
278 /* We couldn't call ActivateModule() -- looks nasty, but
279 * we can't do much about it. Just try to unload module. */
280 module_unload( p_module->handle );
281 p_module->i_usage = -1;
285 /* Everything worked fine ! The module is ready to be used */
286 p_module->i_usage = 1;
291 /*****************************************************************************
292 * module_Unneed: decrease the usage count of a module.
293 *****************************************************************************
294 * This function has to be called before a thread starts using a module. If
295 * the module is already loaded, we just increase its usage count. If it isn't
296 * loaded, we have to dynamically open it and initialize it.
297 * If you successfully call module_Need() at any moment, be careful to call
298 * module_Unneed() when you don't need it anymore.
299 *****************************************************************************/
300 int module_Unneed( module_t * p_module )
302 if( p_module->i_usage <= 0 )
304 /* This shouldn't happen. Ever. We have serious problems here. */
305 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
306 " which isn't even in use", p_module->psz_name );
310 /* This module is still in use, we can return */
312 p_module->i_unused_delay = 0;
317 /*****************************************************************************
318 * Following functions are local.
319 *****************************************************************************/
321 /*****************************************************************************
322 * AllocateDynModule: load a module into memory and initialize it.
323 *****************************************************************************
324 * This function loads a dynamically loadable module and allocates a structure
325 * for its information data. The module can then be handled by module_Need,
326 * module_Unneed and HideModule. It can be removed by FreeModule.
327 *****************************************************************************/
328 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename )
331 module_handle_t handle;
333 /* Try to dynamically load the module. */
334 if( ! module_load( psz_filename, &handle ) )
336 /* The dynamic module couldn't be opened */
337 intf_DbgMsg( "module error: cannot open %s (%s)",
338 psz_filename, module_error() );
342 /* Now that we have successfully loaded the module, we can
343 * allocate a structure for it */
344 p_module = malloc( sizeof( module_t ) );
345 if( p_module == NULL )
347 intf_ErrMsg( "module error: can't allocate p_module" );
348 module_unload( handle );
352 /* We need to fill these since they may be needed by CallSymbol() */
353 p_module->psz_filename = psz_filename;
354 p_module->handle = handle;
356 if( CallSymbol( p_module, "InitModule" ) != 0 )
358 /* We couldn't call InitModule() */
360 module_unload( handle );
364 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
366 /* We couldn't call ActivateModule() */
368 module_unload( handle );
372 /* We strdup() these entries so that they are still valid when the
373 * module is unloaded. */
374 p_module->psz_filename = strdup( p_module->psz_filename );
375 p_module->psz_name = strdup( p_module->psz_name );
376 p_module->psz_longname = strdup( p_module->psz_longname );
377 p_module->psz_version = strdup( p_module->psz_version );
378 if( p_module->psz_filename == NULL
379 || p_module->psz_name == NULL
380 || p_module->psz_longname == NULL
381 || p_module->psz_version == NULL )
383 intf_ErrMsg( "module error: can't duplicate strings" );
384 free( p_module->psz_filename );
385 free( p_module->psz_name );
386 free( p_module->psz_longname );
387 free( p_module->psz_version );
389 module_unload( handle );
393 /* Everything worked fine ! The module is ready to be added to the list. */
394 p_module->i_usage = 0;
395 p_module->i_unused_delay = 0;
397 p_module->b_builtin = 0;
399 /* Link module into the linked list */
400 if( p_bank->first != NULL )
402 p_bank->first->prev = p_module;
404 p_module->next = p_bank->first;
405 p_module->prev = NULL;
406 p_bank->first = p_module;
408 intf_Msg( "module: dynamic module `%s', %s",
409 p_module->psz_name, p_module->psz_longname );
414 /*****************************************************************************
415 * HideModule: remove a module from memory but keep its structure.
416 *****************************************************************************
417 * This function can only be called if i_usage == 0. It will make a call
418 * to the module's inner DeactivateModule() symbol, and then unload it
419 * from memory. A call to module_Need() will automagically load it again.
420 *****************************************************************************/
421 static int HideModule( module_t * p_module )
423 if( p_module->b_builtin )
425 /* A built-in module should never be hidden. */
426 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
427 p_module->psz_name );
431 if( p_module->i_usage >= 1 )
433 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
434 " in use", p_module->psz_name );
438 if( p_module->i_usage <= -1 )
440 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
441 " hidden", p_module->psz_name );
445 if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
447 /* We couldn't call DeactivateModule() -- looks nasty, but
448 * we can't do much about it. Just try to unload module anyway. */
449 module_unload( p_module->handle );
450 p_module->i_usage = -1;
454 /* Everything worked fine, we can safely unload the module. */
455 module_unload( p_module->handle );
456 p_module->i_usage = -1;
461 /*****************************************************************************
462 * FreeModule: delete a module and its structure.
463 *****************************************************************************
464 * This function can only be called if i_usage <= 0.
465 *****************************************************************************/
466 static int FreeModule( module_bank_t * p_bank, module_t * p_module )
468 /* If the module is not in use but is still in memory, we first have
469 * to hide it and remove it from memory before we can free the
471 if( p_module->b_builtin )
473 if( p_module->i_usage != 0 )
475 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
476 " usage %i", p_module->psz_name, p_module->i_usage );
482 if( p_module->i_usage >= 1 )
484 intf_ErrMsg( "module error: trying to free module `%s' which is"
485 " still in use", p_module->psz_name );
489 /* Two possibilities here: i_usage == -1 and the module is already
490 * unloaded, we can continue, or i_usage == 0, and we have to hide
491 * the module before going on. */
492 if( p_module->i_usage == 0 )
494 if( HideModule( p_module ) != 0 )
501 /* Unlink the module from the linked list. */
502 if( p_module == p_bank->first )
504 p_bank->first = p_module->next;
507 if( p_module->prev != NULL )
509 p_module->prev->next = p_module->next;
512 if( p_module->next != NULL )
514 p_module->next->prev = p_module->prev;
517 /* We free the structures that we strdup()ed in Allocate*Module(). */
518 free( p_module->psz_filename );
519 free( p_module->psz_name );
520 free( p_module->psz_longname );
521 free( p_module->psz_version );
528 /*****************************************************************************
529 * CallSymbol: calls a module symbol.
530 *****************************************************************************
531 * This function calls a symbol given its name and a module structure. The
532 * symbol MUST refer to a function returning int and taking a module_t* as
534 *****************************************************************************/
535 static int CallSymbol( module_t * p_module, char * psz_name )
537 typedef int ( symbol_t ) ( module_t * p_module );
540 /* Try to resolve the symbol */
541 p_symbol = module_getsymbol( p_module->handle, psz_name );
545 /* We couldn't load the symbol */
546 intf_DbgMsg( "module warning: cannot find symbol %s in module %s (%s)",
547 psz_name, p_module->psz_filename, module_error() );
551 /* We can now try to call the symbol */
552 if( p_symbol( p_module ) != 0 )
554 /* With a well-written module we shouldn't have to print an
555 * additional error message here, but just make sure. */
556 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
557 psz_name, p_module->psz_filename );
561 /* Everything worked fine, we can return */