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 int i_filelen = strlen( file->d_name );
110 /* We only load files ending with ".so" */
112 && !strcmp( file->d_name + i_filelen - 3, ".so" ) )
115 /* Under BeOS, we need to add beos_GetProgramPath() to
116 * access files under the current directory */
117 if( memcmp( file->d_name, "/", 1 ) )
119 psz_file = malloc( i_programlen + i_dirlen
121 if( psz_file == NULL )
125 sprintf( psz_file, "%s/%s/%s", psz_programlen,
126 *ppsz_path, file->d_name );
131 psz_file = malloc( i_dirlen + i_filelen + 2 );
132 if( psz_file == NULL )
136 sprintf( psz_file, "%s/%s", *ppsz_path, file->d_name );
138 /* We created a nice filename -- now we just try to load
139 * it as a dynamic module. */
140 AllocateDynModule( p_bank, psz_file );
142 /* We don't care if the allocation succeeded */
152 /*****************************************************************************
153 * module_DestroyBank: destroy the module bank.
154 *****************************************************************************
155 * This function unloads all unused dynamic modules and removes the module
156 * bank in case of success.
157 *****************************************************************************/
158 void module_DestroyBank( module_bank_t * p_bank )
162 while( p_bank->first != NULL )
164 if( FreeModule( p_bank, p_bank->first ) )
166 /* Module deletion failed */
167 intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
168 p_bank->first->psz_name );
170 /* We just free the module by hand. Niahahahahaha. */
171 p_next = p_bank->first->next;
173 p_bank->first = p_next;
177 /* Destroy the lock */
178 vlc_mutex_destroy( &p_bank->lock );
180 /* We can free the module bank */
186 /*****************************************************************************
187 * module_ResetBank: reset the module bank.
188 *****************************************************************************
189 * This function resets the module bank by unloading all unused dynamic
191 *****************************************************************************/
192 void module_ResetBank( module_bank_t * p_bank )
194 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
198 /*****************************************************************************
199 * module_ManageBank: manage the module bank.
200 *****************************************************************************
201 * This function parses the module bank and hides modules that have been
202 * unused for a while.
203 *****************************************************************************/
204 void module_ManageBank( module_bank_t * p_bank )
208 /* We take the global lock */
209 vlc_mutex_lock( &p_bank->lock );
211 /* Parse the module list to see if any modules need to be unloaded */
212 for( p_module = p_bank->first ;
214 p_module = p_module->next )
216 /* If the module is unused and if it is a dynamic module... */
217 if( p_module->i_usage == 0 && !p_module->b_builtin )
219 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
221 p_module->i_unused_delay++;
225 intf_Msg( "module: hiding unused module `%s'",
226 p_module->psz_name );
227 HideModule( p_module );
232 /* We release the global lock */
233 vlc_mutex_unlock( &p_bank->lock );
238 /*****************************************************************************
239 * module_Need: increase the usage count of a module and load it if needed.
240 *****************************************************************************
241 * This function has to be called before a thread starts using a module. If
242 * the module is already loaded, we just increase its usage count. If it isn't
243 * loaded, we have to dynamically open it and initialize it.
244 * If you successfully call module_Need() at any moment, be careful to call
245 * module_Unneed() when you don't need it anymore.
246 *****************************************************************************/
247 int module_Need( module_t * p_module )
249 if( p_module->i_usage >= 0 )
251 /* This module is already loaded and activated, we can return */
256 if( p_module->b_builtin )
258 /* A built-in module should always have a refcount >= 0 ! */
259 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
260 p_module->psz_name, p_module->i_usage );
264 if( p_module->i_usage != -1 )
266 /* This shouldn't happen. Ever. We have serious problems here. */
267 intf_ErrMsg( "module error: dynamic module `%s' has refcount %i",
268 p_module->psz_name, p_module->i_usage );
272 /* i_usage == -1, which means that the module isn't in memory */
273 if( ! module_load( p_module->psz_filename, &p_module->handle ) )
275 /* The dynamic module couldn't be opened */
276 intf_ErrMsg( "module error: cannot open %s (%s)",
277 p_module->psz_filename, module_error() );
281 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
283 /* We couldn't call ActivateModule() -- looks nasty, but
284 * we can't do much about it. Just try to unload module. */
285 module_unload( p_module->handle );
286 p_module->i_usage = -1;
290 /* Everything worked fine ! The module is ready to be used */
291 p_module->i_usage = 1;
296 /*****************************************************************************
297 * module_Unneed: decrease the usage count of a module.
298 *****************************************************************************
299 * This function has to be called before a thread starts using a module. If
300 * the module is already loaded, we just increase its usage count. If it isn't
301 * loaded, we have to dynamically open it and initialize it.
302 * If you successfully call module_Need() at any moment, be careful to call
303 * module_Unneed() when you don't need it anymore.
304 *****************************************************************************/
305 int module_Unneed( module_t * p_module )
307 if( p_module->i_usage <= 0 )
309 /* This shouldn't happen. Ever. We have serious problems here. */
310 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
311 " which isn't even in use", p_module->psz_name );
315 /* This module is still in use, we can return */
317 p_module->i_unused_delay = 0;
322 /*****************************************************************************
323 * Following functions are local.
324 *****************************************************************************/
326 /*****************************************************************************
327 * AllocateDynModule: load a module into memory and initialize it.
328 *****************************************************************************
329 * This function loads a dynamically loadable module and allocates a structure
330 * for its information data. The module can then be handled by module_Need,
331 * module_Unneed and HideModule. It can be removed by FreeModule.
332 *****************************************************************************/
333 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename )
336 module_handle_t handle;
338 /* Try to dynamically load the module. */
339 if( ! module_load( psz_filename, &handle ) )
341 /* The dynamic module couldn't be opened */
342 intf_DbgMsg( "module error: cannot open %s (%s)",
343 psz_filename, module_error() );
347 /* Now that we have successfully loaded the module, we can
348 * allocate a structure for it */
349 p_module = malloc( sizeof( module_t ) );
350 if( p_module == NULL )
352 intf_ErrMsg( "module error: can't allocate p_module" );
353 module_unload( handle );
357 /* We need to fill these since they may be needed by CallSymbol() */
358 p_module->psz_filename = psz_filename;
359 p_module->handle = handle;
361 if( CallSymbol( p_module, "InitModule" ) != 0 )
363 /* We couldn't call InitModule() */
365 module_unload( handle );
369 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
371 /* We couldn't call ActivateModule() */
373 module_unload( handle );
377 /* We strdup() these entries so that they are still valid when the
378 * module is unloaded. */
379 p_module->psz_filename = strdup( p_module->psz_filename );
380 p_module->psz_name = strdup( p_module->psz_name );
381 p_module->psz_longname = strdup( p_module->psz_longname );
382 p_module->psz_version = strdup( p_module->psz_version );
383 if( p_module->psz_filename == NULL
384 || p_module->psz_name == NULL
385 || p_module->psz_longname == NULL
386 || p_module->psz_version == NULL )
388 intf_ErrMsg( "module error: can't duplicate strings" );
389 free( p_module->psz_filename );
390 free( p_module->psz_name );
391 free( p_module->psz_longname );
392 free( p_module->psz_version );
394 module_unload( handle );
398 /* Everything worked fine ! The module is ready to be added to the list. */
399 p_module->i_usage = 0;
400 p_module->i_unused_delay = 0;
402 p_module->b_builtin = 0;
404 /* Link module into the linked list */
405 if( p_bank->first != NULL )
407 p_bank->first->prev = p_module;
409 p_module->next = p_bank->first;
410 p_module->prev = NULL;
411 p_bank->first = p_module;
413 intf_Msg( "module: dynamic module `%s', %s",
414 p_module->psz_name, p_module->psz_longname );
419 /*****************************************************************************
420 * HideModule: remove a module from memory but keep its structure.
421 *****************************************************************************
422 * This function can only be called if i_usage == 0. It will make a call
423 * to the module's inner DeactivateModule() symbol, and then unload it
424 * from memory. A call to module_Need() will automagically load it again.
425 *****************************************************************************/
426 static int HideModule( module_t * p_module )
428 if( p_module->b_builtin )
430 /* A built-in module should never be hidden. */
431 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
432 p_module->psz_name );
436 if( p_module->i_usage >= 1 )
438 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
439 " in use", p_module->psz_name );
443 if( p_module->i_usage <= -1 )
445 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
446 " hidden", p_module->psz_name );
450 if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
452 /* We couldn't call DeactivateModule() -- looks nasty, but
453 * we can't do much about it. Just try to unload module anyway. */
454 module_unload( p_module->handle );
455 p_module->i_usage = -1;
459 /* Everything worked fine, we can safely unload the module. */
460 module_unload( p_module->handle );
461 p_module->i_usage = -1;
466 /*****************************************************************************
467 * FreeModule: delete a module and its structure.
468 *****************************************************************************
469 * This function can only be called if i_usage <= 0.
470 *****************************************************************************/
471 static int FreeModule( module_bank_t * p_bank, module_t * p_module )
473 /* If the module is not in use but is still in memory, we first have
474 * to hide it and remove it from memory before we can free the
476 if( p_module->b_builtin )
478 if( p_module->i_usage != 0 )
480 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
481 " usage %i", p_module->psz_name, p_module->i_usage );
487 if( p_module->i_usage >= 1 )
489 intf_ErrMsg( "module error: trying to free module `%s' which is"
490 " still in use", p_module->psz_name );
494 /* Two possibilities here: i_usage == -1 and the module is already
495 * unloaded, we can continue, or i_usage == 0, and we have to hide
496 * the module before going on. */
497 if( p_module->i_usage == 0 )
499 if( HideModule( p_module ) != 0 )
506 /* Unlink the module from the linked list. */
507 if( p_module == p_bank->first )
509 p_bank->first = p_module->next;
512 if( p_module->prev != NULL )
514 p_module->prev->next = p_module->next;
517 if( p_module->next != NULL )
519 p_module->next->prev = p_module->prev;
522 /* We free the structures that we strdup()ed in Allocate*Module(). */
523 free( p_module->psz_filename );
524 free( p_module->psz_name );
525 free( p_module->psz_longname );
526 free( p_module->psz_version );
533 /*****************************************************************************
534 * CallSymbol: calls a module symbol.
535 *****************************************************************************
536 * This function calls a symbol given its name and a module structure. The
537 * symbol MUST refer to a function returning int and taking a module_t* as
539 *****************************************************************************/
540 static int CallSymbol( module_t * p_module, char * psz_name )
542 typedef int ( symbol_t ) ( module_t * p_module );
545 /* Try to resolve the symbol */
546 p_symbol = module_getsymbol( p_module->handle, psz_name );
550 /* We couldn't load the symbol */
551 intf_DbgMsg( "module warning: cannot find symbol %s in module %s (%s)",
552 psz_name, p_module->psz_filename, module_error() );
556 /* We can now try to call the symbol */
557 if( p_symbol( p_module ) != 0 )
559 /* With a well-written module we shouldn't have to print an
560 * additional error message here, but just make sure. */
561 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
562 psz_name, p_module->psz_filename );
566 /* Everything worked fine, we can return */