]> git.sesse.net Git - vlc/blob - src/misc/modules.c
. Added files needed for the forthcoming module management.
[vlc] / src / misc / modules.c
1 /*****************************************************************************
2  * modules.c : Built-in and dynamic modules management functions
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22 #include "defs.h"
23
24 #include "config.h"
25
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 */
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <unistd.h>                                                 /* close */
34
35 #if defined(HAVE_DLFCN_H)                                /* Linux, BSD, Hurd */
36 #include <dlfcn.h>                           /* dlopen(), dlsym(), dlclose() */
37
38 #elif defined(HAVE_IMAGE_H)                                          /* BeOS */
39 #include <image.h>
40
41 #else
42 /* FIXME: this isn't supposed to be an error */
43 #error no dynamic plugins available on your system !
44 #endif
45
46 #ifdef SYS_BEOS
47 #include "beos_specific.h"
48 #endif
49
50 #include "common.h"
51 #include "threads.h"
52
53 #include "intf_msg.h"
54 #include "modules.h"
55
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 );
61
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 )
69 {
70     module_bank_t * p_bank;
71
72     intf_ErrMsg( "FIXME: module_InitBank unimplemented" );
73     p_bank = malloc( sizeof( module_bank_t ) );
74
75     return( p_bank );
76 }
77
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 )
85 {
86     intf_ErrMsg( "FIXME: module_DestroyBank unimplemented" );
87     return( -1 );
88 }
89
90 /*****************************************************************************
91  * module_ResetBank: reset the module bank.
92  *****************************************************************************
93  * This function resets the plugin bank by unloading all unused dynamic
94  * modules.
95  *****************************************************************************/
96 int module_ResetBank( module_bank_t * p_bank )
97 {
98     intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
99     return( -1 );
100 }
101
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 )
109 {
110     module_t * p_module;
111
112     /* We take the global lock */
113     vlc_mutex_lock( &p_bank->lock );
114
115     /* Parse the module list to see if any modules need to be unloaded */
116     for( p_module = p_bank->first ;
117          p_module != NULL ;
118          p_module = p_module->next )
119     {
120         /* If the module is unused and if it is a dynamic module... */
121         if( p_module->i_usage == 0 && !p_module->b_builtin )
122         {
123             if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
124             {
125                 p_module->i_unused_delay++;
126             }
127             else
128             {
129                 intf_Msg( "hiding unused module %s", p_module->psz_name );
130                 HideModule( p_module );
131             }
132         }
133     }
134
135     /* We release the global lock */
136     vlc_mutex_lock( &p_bank->lock );
137
138     return;
139 }
140
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 )
151 {
152     if( p_module->i_usage >= 0 )
153     {
154         /* This module is already loaded and activated, we can return */
155         p_module->i_usage++;
156         return( 0 );
157     }
158
159     if( p_module->b_builtin )
160     {
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 );
164         return( -1 );
165     }
166
167     if( p_module->i_usage != -1 )
168     {
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 );
172         return( -1 );
173     }
174
175     /* i_usage == -1, which means that the module isn't in memory */
176     if( ! module_load( p_module->psz_filename, &p_module->handle ) )
177     {
178         /* The dynamic module couldn't be opened */
179         intf_ErrMsg( "module error: cannot open %s (%s)",
180                      p_module->psz_filename, module_error() );
181         return( -1 );
182     }
183
184     if( CallSymbol( p_module, "ActivateModule" ) != 0 )
185     {
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;
190         return( -1 );
191     }
192
193     /* Everything worked fine ! The module is ready to be used */
194     p_module->i_usage = 1;
195
196     return( 0 );
197 }
198
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 )
209 {
210     if( p_module->i_usage <= 0 )
211     {
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 );
215         return( -1 );
216     }
217
218     /* This module is still in use, we can return */
219     p_module->i_usage--;
220     p_module->i_unused_delay = 0;
221
222     return( 0 );
223 }
224
225 /*****************************************************************************
226  * Following functions are local.
227  *****************************************************************************/
228
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 )
237 {
238     module_t * p_module;
239     module_handle_t handle;
240
241     /* Try to dynamically load the module. */
242     if( ! module_load( psz_filename, &handle ) )
243     {
244         /* The dynamic module couldn't be opened */
245         intf_ErrMsg( "module error: cannot open %s (%s)",
246                      psz_filename, module_error() );
247         return( -1 );
248     }
249
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 )
254     {
255         intf_ErrMsg( "module error: can't allocate p_module" );
256         module_unload( handle );
257         return( -1 );
258     }
259
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;
263
264     if( CallSymbol( p_module, "InitModule" ) != 0 )
265     {
266         /* We couldn't call InitModule() */
267         free( p_module );
268         module_unload( handle );
269         return( -1 );
270     }
271
272     if( CallSymbol( p_module, "ActivateModule" ) != 0 )
273     {
274         /* We couldn't call ActivateModule() */
275         free( p_module );
276         module_unload( handle );
277         return( -1 );
278     }
279
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 )
290     {
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 );
296         free( p_module );
297         module_unload( handle );
298         return( -1 );
299     }
300
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;
304
305     p_module->b_builtin = 0;
306
307     /* Link module across linked list */
308     if( p_bank->first != NULL )
309     {
310         p_bank->first->prev = p_module;
311     }
312     p_module->next = p_bank->first;
313     p_bank->first = p_module;
314
315     return( 0 );
316 }
317
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 )
326 {
327     if( p_module->b_builtin )
328     {
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 );
332         return( -1 );
333     }
334
335     if( p_module->i_usage >= 1 )
336     {
337         intf_ErrMsg( "module error: trying to hide module %s which is still"
338                      " in use", p_module->psz_name );
339         return( -1 );
340     }
341
342     if( p_module->i_usage <= -1 )
343     {
344         intf_ErrMsg( "module error: trying to hide module %s which is already"
345                      " hidden", p_module->psz_name );
346         return( -1 );
347     }
348
349     if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
350     {
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;
355         return( -1 );
356     }
357
358     /* Everything worked fine, we can safely unload the module. */
359     module_unload( p_module->handle );
360     p_module->i_usage = -1;
361
362     return( 0 );
363 }
364
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 )
371 {
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
374      * data structure. */
375     if( p_module->b_builtin )
376     {
377         if( p_module->i_usage != 0 )
378         {
379             intf_ErrMsg( "module error: trying to free builtin module %s with"
380                          " usage %i", p_module->psz_name, p_module->i_usage );
381             return( -1 );
382         }
383     }
384     else
385     {
386         if( p_module->i_usage >= 1 )
387         {
388             intf_ErrMsg( "module error: trying to free module %s which is"
389                          " still in use", p_module->psz_name );
390             return( -1 );
391         }
392
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 )
397         {
398             if( HideModule( p_module ) != 0 )
399             {
400                 return( -1 );
401             }
402         }
403     }
404
405     /* Unlink the module from the linked list. */
406     if( p_module == p_bank->first )
407     {
408         p_bank->first = p_module->next;
409     }
410
411     if( p_module->prev != NULL )
412     {
413         p_module->prev->next = p_module->next;
414     }
415
416     if( p_module->next != NULL )
417     {
418         p_module->next->prev = p_module->prev;
419     }
420
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 );
426
427     free( p_module );
428
429     return( 0 );
430 }
431
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
437  * an argument.
438  *****************************************************************************/
439 static int CallSymbol( module_t * p_module, char * psz_name )
440 {
441     typedef int ( symbol_t ) ( module_t * p_module );
442     symbol_t * p_symbol;
443
444     /* Try to resolve the symbol */
445     p_symbol = module_getsymbol( p_module->handle, psz_name );
446
447     if( !p_symbol )
448     {
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() );
452         return( -1 );
453     }
454
455     /* We can now try to call the symbol */
456     if( p_symbol( p_module ) != 0 )
457     {
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 );
462         return( -1 );
463     }
464
465     /* Everything worked fine, we can return */
466     return( 0 );
467 }
468