1 /*****************************************************************************
2 * modules.c : Built-in and dynamic modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
26 #include <stdlib.h> /* free(), strtol() */
27 #include <stdio.h> /* sprintf() */
28 #include <string.h> /* strerror() */
29 #include <errno.h> /* ENOMEM */
30 #include <sys/types.h> /* open */
33 #include <unistd.h> /* close */
35 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
36 #include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
38 #elif defined(HAVE_IMAGE_H) /* BeOS */
42 /* FIXME: this isn't supposed to be an error */
43 #error no dynamic plugins available on your system !
47 #include "beos_specific.h"
56 /* Local prototypes */
57 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename );
58 static int HideModule( module_t * p_module );
59 static int FreeModule( module_bank_t * p_bank, module_t * p_module );
60 static int CallSymbol( module_t * p_module, char * psz_name );
62 /*****************************************************************************
63 * module_InitBank: create the module bank.
64 *****************************************************************************
65 * This function creates a module bank structure and fills it with the
66 * built-in modules, as well as all the dynamic modules it can find.
67 *****************************************************************************/
68 module_bank_t * module_InitBank( void )
70 module_bank_t * p_bank;
72 intf_ErrMsg( "FIXME: module_InitBank unimplemented" );
73 p_bank = malloc( sizeof( module_bank_t ) );
78 /*****************************************************************************
79 * module_DestroyBank: destroy the module bank.
80 *****************************************************************************
81 * This function unloads all unused dynamic modules and removes the module
82 * bank in case of success.
83 *****************************************************************************/
84 int module_DestroyBank( module_bank_t * p_bank )
86 intf_ErrMsg( "FIXME: module_DestroyBank unimplemented" );
90 /*****************************************************************************
91 * module_ResetBank: reset the module bank.
92 *****************************************************************************
93 * This function resets the plugin bank by unloading all unused dynamic
95 *****************************************************************************/
96 int module_ResetBank( module_bank_t * p_bank )
98 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
102 /*****************************************************************************
103 * module_ManageBank: manage the module bank.
104 *****************************************************************************
105 * This function parses the module bank and hides modules that have been
106 * unused for a while.
107 *****************************************************************************/
108 void module_ManageBank( module_bank_t * p_bank )
112 /* We take the global lock */
113 vlc_mutex_lock( &p_bank->lock );
115 /* Parse the module list to see if any modules need to be unloaded */
116 for( p_module = p_bank->first ;
118 p_module = p_module->next )
120 /* If the module is unused and if it is a dynamic module... */
121 if( p_module->i_usage == 0 && !p_module->b_builtin )
123 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
125 p_module->i_unused_delay++;
129 intf_Msg( "hiding unused module %s", p_module->psz_name );
130 HideModule( p_module );
135 /* We release the global lock */
136 vlc_mutex_lock( &p_bank->lock );
141 /*****************************************************************************
142 * module_Need: increase the usage count of a module and load it if needed.
143 *****************************************************************************
144 * This function has to be called before a thread starts using a module. If
145 * the module is already loaded, we just increase its usage count. If it isn't
146 * loaded, we have to dynamically open it and initialize it.
147 * If you successfully call module_Need() at any moment, be careful to call
148 * module_Unneed() when you don't need it anymore.
149 *****************************************************************************/
150 int module_Need( module_t * p_module )
152 if( p_module->i_usage >= 0 )
154 /* This module is already loaded and activated, we can return */
159 if( p_module->b_builtin )
161 /* A built-in module should always have a refcount >= 0 ! */
162 intf_ErrMsg( "module error: built-in module %s has refcount %i",
163 p_module->psz_name, p_module->i_usage );
167 if( p_module->i_usage != -1 )
169 /* This shouldn't happen. Ever. We have serious problems here. */
170 intf_ErrMsg( "module error: dynamic module %s has refcount %i",
171 p_module->psz_name, p_module->i_usage );
175 /* i_usage == -1, which means that the module isn't in memory */
176 if( ! module_load( p_module->psz_filename, &p_module->handle ) )
178 /* The dynamic module couldn't be opened */
179 intf_ErrMsg( "module error: cannot open %s (%s)",
180 p_module->psz_filename, module_error() );
184 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
186 /* We couldn't call ActivateModule() -- looks nasty, but
187 * we can't do much about it. Just try to unload module. */
188 module_unload( p_module->handle );
189 p_module->i_usage = -1;
193 /* Everything worked fine ! The module is ready to be used */
194 p_module->i_usage = 1;
199 /*****************************************************************************
200 * module_Unneed: decrease the usage count of a module.
201 *****************************************************************************
202 * This function has to be called before a thread starts using a module. If
203 * the module is already loaded, we just increase its usage count. If it isn't
204 * loaded, we have to dynamically open it and initialize it.
205 * If you successfully call module_Need() at any moment, be careful to call
206 * module_Unneed() when you don't need it anymore.
207 *****************************************************************************/
208 int module_Unneed( module_t * p_module )
210 if( p_module->i_usage <= 0 )
212 /* This shouldn't happen. Ever. We have serious problems here. */
213 intf_ErrMsg( "module error: trying to call module_Unneed() on %s"
214 " which isn't even in use", p_module->psz_name );
218 /* This module is still in use, we can return */
220 p_module->i_unused_delay = 0;
225 /*****************************************************************************
226 * Following functions are local.
227 *****************************************************************************/
229 /*****************************************************************************
230 * AllocateDynModule: load a module into memory and initialize it.
231 *****************************************************************************
232 * This function loads a dynamically loadable module and allocates a structure
233 * for its information data. The module can then be handled by module_Need,
234 * module_Unneed and HideModule. It can be removed by FreeModule.
235 *****************************************************************************/
236 static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename )
239 module_handle_t handle;
241 /* Try to dynamically load the module. */
242 if( ! module_load( psz_filename, &handle ) )
244 /* The dynamic module couldn't be opened */
245 intf_ErrMsg( "module error: cannot open %s (%s)",
246 psz_filename, module_error() );
250 /* Now that we have successfully loaded the module, we can
251 * allocate a structure for it */
252 p_module = malloc( sizeof( module_t ) );
253 if( p_module == NULL )
255 intf_ErrMsg( "module error: can't allocate p_module" );
256 module_unload( handle );
260 /* We need to fill these since they may be needed by CallSymbol() */
261 p_module->psz_filename = psz_filename;
262 p_module->handle = handle;
264 if( CallSymbol( p_module, "InitModule" ) != 0 )
266 /* We couldn't call InitModule() */
268 module_unload( handle );
272 if( CallSymbol( p_module, "ActivateModule" ) != 0 )
274 /* We couldn't call ActivateModule() */
276 module_unload( handle );
280 /* We strdup() these entries so that they are still valid when the
281 * module is unloaded. */
282 p_module->psz_filename = strdup( p_module->psz_filename );
283 p_module->psz_name = strdup( p_module->psz_name );
284 p_module->psz_longname = strdup( p_module->psz_longname );
285 p_module->psz_version = strdup( p_module->psz_version );
286 if( p_module->psz_filename == NULL
287 || p_module->psz_name == NULL
288 || p_module->psz_longname == NULL
289 || p_module->psz_version == NULL )
291 intf_ErrMsg( "module error: can't duplicate strings" );
292 free( p_module->psz_filename );
293 free( p_module->psz_name );
294 free( p_module->psz_longname );
295 free( p_module->psz_version );
297 module_unload( handle );
301 /* Everything worked fine ! The module is ready to be added to the list. */
302 p_module->i_usage = 0;
303 p_module->i_unused_delay = 0;
305 p_module->b_builtin = 0;
307 /* Link module across linked list */
308 if( p_bank->first != NULL )
310 p_bank->first->prev = p_module;
312 p_module->next = p_bank->first;
313 p_bank->first = p_module;
318 /*****************************************************************************
319 * HideModule: remove a module from memory but keep its structure.
320 *****************************************************************************
321 * This function can only be called if i_usage == 0. It will make a call
322 * to the module's inner DeactivateModule() symbol, and then unload it
323 * from memory. A call to module_Need() will automagically load it again.
324 *****************************************************************************/
325 static int HideModule( module_t * p_module )
327 if( p_module->b_builtin )
329 /* A built-in module should never be hidden. */
330 intf_ErrMsg( "module error: trying to hide built-in module %s",
331 p_module->psz_name );
335 if( p_module->i_usage >= 1 )
337 intf_ErrMsg( "module error: trying to hide module %s which is still"
338 " in use", p_module->psz_name );
342 if( p_module->i_usage <= -1 )
344 intf_ErrMsg( "module error: trying to hide module %s which is already"
345 " hidden", p_module->psz_name );
349 if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
351 /* We couldn't call DeactivateModule() -- looks nasty, but
352 * we can't do much about it. Just try to unload module anyway. */
353 module_unload( p_module->handle );
354 p_module->i_usage = -1;
358 /* Everything worked fine, we can safely unload the module. */
359 module_unload( p_module->handle );
360 p_module->i_usage = -1;
365 /*****************************************************************************
366 * FreeModule: delete a module and its structure.
367 *****************************************************************************
368 * This function can only be called if i_usage <= 0.
369 *****************************************************************************/
370 static int FreeModule( module_bank_t * p_bank, module_t * p_module )
372 /* If the module is not in use but is still in memory, we first have
373 * to hide it and remove it from memory before we can free the
375 if( p_module->b_builtin )
377 if( p_module->i_usage != 0 )
379 intf_ErrMsg( "module error: trying to free builtin module %s with"
380 " usage %i", p_module->psz_name, p_module->i_usage );
386 if( p_module->i_usage >= 1 )
388 intf_ErrMsg( "module error: trying to free module %s which is"
389 " still in use", p_module->psz_name );
393 /* Two possibilities here: i_usage == -1 and the module is already
394 * unloaded, we can continue, or i_usage == 0, and we have to hide
395 * the module before going on. */
396 if( p_module->i_usage == 0 )
398 if( HideModule( p_module ) != 0 )
405 /* Unlink the module from the linked list. */
406 if( p_module == p_bank->first )
408 p_bank->first = p_module->next;
411 if( p_module->prev != NULL )
413 p_module->prev->next = p_module->next;
416 if( p_module->next != NULL )
418 p_module->next->prev = p_module->prev;
421 /* We free the structures that we strdup()ed in Allocate*Module(). */
422 free( p_module->psz_filename );
423 free( p_module->psz_name );
424 free( p_module->psz_longname );
425 free( p_module->psz_version );
432 /*****************************************************************************
433 * CallSymbol: calls a module symbol.
434 *****************************************************************************
435 * This function calls a symbol given its name and a module structure. The
436 * symbol MUST refer to a function returning int and taking a module_t* as
438 *****************************************************************************/
439 static int CallSymbol( module_t * p_module, char * psz_name )
441 typedef int ( symbol_t ) ( module_t * p_module );
444 /* Try to resolve the symbol */
445 p_symbol = module_getsymbol( p_module->handle, psz_name );
449 /* We couldn't load the symbol */
450 intf_ErrMsg( "module error: cannot find %s in module %s (%s)",
451 psz_name, p_module->psz_filename, module_error() );
455 /* We can now try to call the symbol */
456 if( p_symbol( p_module ) != 0 )
458 /* With a well-written module we shouldn't have to print an
459 * additional error message here, but just make sure. */
460 intf_ErrMsg( "module error: failed calling %s in module %s",
461 psz_name, p_module->psz_filename );
465 /* Everything worked fine, we can return */