]> git.sesse.net Git - vlc/blob - src/modules/modules.c
Fix off by one
[vlc] / src / modules / modules.c
1 /*****************************************************************************
2  * modules.c : Builtin and plugin modules management functions
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *          Ethan C. Baldridge <BaldridgeE@cadmus.com>
9  *          Hans-Peter Jansen <hpj@urpla.net>
10  *          Gildas Bazin <gbazin@videolan.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc/vlc.h>
32 #include "libvlc.h"
33
34 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
35  * is set to 64. Don't try to be cleverer. */
36 #ifdef _FILE_OFFSET_BITS
37 #undef _FILE_OFFSET_BITS
38 #endif
39
40 #include <stdlib.h>                                      /* free(), strtol() */
41 #include <stdio.h>                                              /* sprintf() */
42 #include <string.h>                                              /* strdup() */
43 #include <assert.h>
44
45 #ifdef HAVE_DIRENT_H
46 #   include <dirent.h>
47 #endif
48
49 #ifdef HAVE_SYS_TYPES_H
50 #   include <sys/types.h>
51 #endif
52 #ifdef HAVE_SYS_STAT_H
53 #   include <sys/stat.h>
54 #endif
55 #ifdef HAVE_UNISTD_H
56 #   include <unistd.h>
57 #endif
58
59 #if !defined(HAVE_DYNAMIC_PLUGINS)
60     /* no support for plugins */
61 #elif defined(HAVE_DL_DYLD)
62 #   if defined(HAVE_MACH_O_DYLD_H)
63 #       include <mach-o/dyld.h>
64 #   endif
65 #elif defined(HAVE_DL_BEOS)
66 #   if defined(HAVE_IMAGE_H)
67 #       include <image.h>
68 #   endif
69 #elif defined(HAVE_DL_WINDOWS)
70 #   include <windows.h>
71 #elif defined(HAVE_DL_DLOPEN)
72 #   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
73 #       include <dlfcn.h>
74 #   endif
75 #   if defined(HAVE_SYS_DL_H)
76 #       include <sys/dl.h>
77 #   endif
78 #elif defined(HAVE_DL_SHL_LOAD)
79 #   if defined(HAVE_DL_H)
80 #       include <dl.h>
81 #   endif
82 #endif
83
84 #include "config/configuration.h"
85
86 #include "vlc_charset.h"
87
88 #include "modules/modules.h"
89 #include "modules/builtin.h"
90
91 /*****************************************************************************
92  * Local prototypes
93  *****************************************************************************/
94 #ifdef HAVE_DYNAMIC_PLUGINS
95 static void AllocateAllPlugins  ( vlc_object_t * );
96 static void AllocatePluginDir   ( vlc_object_t *, const char *, int );
97 static int  AllocatePluginFile  ( vlc_object_t *, char *, int64_t, int64_t );
98 static module_t * AllocatePlugin( vlc_object_t *, char * );
99 #endif
100 static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
101 static int  DeleteModule ( module_t *, vlc_bool_t );
102 #ifdef HAVE_DYNAMIC_PLUGINS
103 static void   DupModule        ( module_t * );
104 static void   UndupModule      ( module_t * );
105 #endif
106
107 static void module_LoadMain( vlc_object_t *p_this );
108
109 /**
110  * Init bank
111  *
112  * Creates a module bank structure which will be filled later
113  * on with all the modules found.
114  * \param p_this vlc object structure
115  * \return nothing
116  */
117 void __module_InitBank( vlc_object_t *p_this )
118 {
119     module_bank_t *p_bank = NULL;
120     vlc_value_t  lockval;
121     libvlc_global_data_t *p_libvlc_global = vlc_global();
122
123     var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
124     var_Get( p_libvlc_global, "libvlc", &lockval );
125     vlc_mutex_lock( lockval.p_address );
126     if( p_libvlc_global->p_module_bank )
127     {
128         p_libvlc_global->p_module_bank->i_usage++;
129         vlc_mutex_unlock( lockval.p_address );
130         var_Destroy( p_libvlc_global, "libvlc" );
131         return;
132     }
133     vlc_mutex_unlock( lockval.p_address );
134     var_Destroy( p_libvlc_global, "libvlc" );
135
136     p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
137     if( !p_bank )
138         return;
139     p_bank->psz_object_name = "module bank";
140     p_bank->i_usage = 1;
141     p_bank->i_cache = p_bank->i_loaded_cache = 0;
142     p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
143     p_bank->b_cache = p_bank->b_cache_dirty =
144         p_bank->b_cache_delete = VLC_FALSE;
145
146     /* Everything worked, attach the object */
147     p_libvlc_global->p_module_bank = p_bank;
148     vlc_object_attach( p_bank, p_libvlc_global );
149
150     module_LoadMain( p_this );
151 }
152
153
154 /**
155  * End bank
156  *
157  * Unloads all unused plugin modules and empties the module
158  * bank in case of success.
159  * \param p_this vlc object structure
160  * \return nothing
161  */
162 void __module_EndBank( vlc_object_t *p_this )
163 {
164     module_t * p_next = NULL;
165     vlc_value_t lockval;
166     libvlc_global_data_t *p_libvlc_global = vlc_global();
167
168     var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
169     var_Get( p_libvlc_global, "libvlc", &lockval );
170     vlc_mutex_lock( lockval.p_address );
171     if( !p_libvlc_global->p_module_bank )
172     {
173         vlc_mutex_unlock( lockval.p_address );
174         var_Destroy( p_libvlc_global, "libvlc" );
175         return;
176     }
177     if( --p_libvlc_global->p_module_bank->i_usage )
178     {
179         vlc_mutex_unlock( lockval.p_address );
180         var_Destroy( p_libvlc_global, "libvlc" );
181         return;
182     }
183     vlc_mutex_unlock( lockval.p_address );
184     var_Destroy( p_libvlc_global, "libvlc" );
185
186     /* Save the configuration */
187     config_AutoSaveConfigFile( p_this );
188
189 #ifdef HAVE_DYNAMIC_PLUGINS
190 # define p_bank p_libvlc_global->p_module_bank
191     if( p_bank->b_cache ) CacheSave( p_this );
192     while( p_bank->i_loaded_cache-- )
193     {
194         if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
195         {
196             DeleteModule(
197                     p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module,
198                     p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
199             free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
200             free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
201             p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
202         }
203     }
204     if( p_bank->pp_loaded_cache )
205     {
206         free( p_bank->pp_loaded_cache );
207         p_bank->pp_loaded_cache = NULL;
208     }
209     while( p_bank->i_cache-- )
210     {
211         free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
212         free( p_bank->pp_cache[p_bank->i_cache] );
213         p_bank->pp_cache[p_bank->i_cache] = NULL;
214     }
215     if( p_bank->pp_cache )
216     {
217         free( p_bank->pp_cache );
218         p_bank->pp_cache = NULL;
219     }
220 # undef p_bank
221 #endif
222
223     vlc_object_detach( p_libvlc_global->p_module_bank );
224
225     while( p_libvlc_global->p_module_bank->i_children )
226     {
227         p_next = (module_t *)p_libvlc_global->p_module_bank->pp_children[0];
228
229         if( DeleteModule( p_next, VLC_TRUE ) )
230         {
231             /* Module deletion failed */
232             msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
233                      p_next->psz_object_name );
234
235             /* We just free the module by hand. Niahahahahaha. */
236             vlc_object_detach( p_next );
237             vlc_object_release( p_next );
238         }
239     }
240
241     vlc_object_release( p_libvlc_global->p_module_bank );
242     p_libvlc_global->p_module_bank = NULL;
243 }
244
245 /**
246  * Load the main program info into the module bank.
247  *
248  * Fills the module bank structure with the main module infos.
249  * This is very useful as it will allow us to consider the main program just
250  * as another module, and for instance the configuration options of main will
251  * be available in the module bank structure just as for every other module.
252  * \param p_this vlc object structure
253  * \return nothing
254  */
255 static void module_LoadMain( vlc_object_t *p_this )
256 {
257     vlc_value_t lockval;
258     libvlc_global_data_t *p_libvlc_global = vlc_global();
259
260     var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
261     var_Get( p_libvlc_global, "libvlc", &lockval );
262     vlc_mutex_lock( lockval.p_address );
263     if( p_libvlc_global->p_module_bank->b_main )
264     {
265         vlc_mutex_unlock( lockval.p_address );
266         var_Destroy( p_libvlc_global, "libvlc" );
267         return;
268     }
269     p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
270     vlc_mutex_unlock( lockval.p_address );
271     var_Destroy( p_libvlc_global, "libvlc" );
272
273     AllocateBuiltinModule( p_this, vlc_entry__main );
274 }
275
276 /**
277  * Load all modules which we built with.
278  *
279  * Fills the module bank structure with the builtin modules.
280  * \param p_this vlc object structure
281  * \return nothing
282  */
283 void __module_LoadBuiltins( vlc_object_t * p_this )
284 {
285     vlc_value_t lockval;
286     libvlc_global_data_t *p_libvlc_global = vlc_global();
287
288     var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
289     var_Get( p_libvlc_global, "libvlc", &lockval );
290     vlc_mutex_lock( lockval.p_address );
291     if( p_libvlc_global->p_module_bank->b_builtins )
292     {
293         vlc_mutex_unlock( lockval.p_address );
294         var_Destroy( p_libvlc_global, "libvlc" );
295         return;
296     }
297     p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
298     vlc_mutex_unlock( lockval.p_address );
299     var_Destroy( p_libvlc_global, "libvlc" );
300
301     msg_Dbg( p_this, "checking builtin modules" );
302     ALLOCATE_ALL_BUILTINS();
303 }
304
305 /**
306  * Load all plugins
307  *
308  * Load all plugin modules we can find.
309  * Fills the module bank structure with the plugin modules.
310  * \param p_this vlc object structure
311  * \return nothing
312  */
313 void __module_LoadPlugins( vlc_object_t * p_this )
314 {
315 #ifdef HAVE_DYNAMIC_PLUGINS
316     vlc_value_t lockval;
317     libvlc_global_data_t *p_libvlc_global = vlc_global();
318
319     var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
320     var_Get( p_libvlc_global, "libvlc", &lockval );
321     vlc_mutex_lock( lockval.p_address );
322     if( p_libvlc_global->p_module_bank->b_plugins )
323     {
324         vlc_mutex_unlock( lockval.p_address );
325         var_Destroy( p_libvlc_global, "libvlc" );
326         return;
327     }
328     p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
329     vlc_mutex_unlock( lockval.p_address );
330     var_Destroy( p_libvlc_global, "libvlc" );
331
332     msg_Dbg( p_this, "checking plugin modules" );
333
334     if( config_GetInt( p_this, "plugins-cache" ) )
335         p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
336
337     if( p_libvlc_global->p_module_bank->b_cache ||
338         p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
339
340     AllocateAllPlugins( p_this );
341 #endif
342 }
343
344 /**
345  * Checks whether a module implements a capability.
346  *
347  * \param m the module
348  * \param cap the capability to check
349  * \return TRUE if the module have the capability
350  */
351 vlc_bool_t module_IsCapable( const module_t *m, const char *cap )
352 {
353     return !strcmp( m->psz_capability, cap );
354 }
355
356 /**
357  * Get the internal name of a module
358  *
359  * \param m the module
360  * \return the module name
361  */
362 const char *module_GetObjName( const module_t *m )
363 {
364     return m->psz_object_name;
365 }
366
367 /**
368  * Get the human-friendly name of a module.
369  *
370  * \param m the module
371  * \param long_name TRUE to have the long name of the module
372  * \return the short or long name of the module
373  */
374 const char *module_GetName( const module_t *m, vlc_bool_t long_name )
375 {
376     if( long_name && ( m->psz_longname != NULL) )
377         return m->psz_longname;
378
379     return m->psz_shortname ?: m->psz_object_name;
380 }
381
382 /**
383  * Get the help for a module
384  *
385  * \param m the module
386  * \return the help
387  */
388 const char *module_GetHelp( const module_t *m )
389 {
390     return m->psz_help;
391 }
392
393 /**
394  * module Need
395  *
396  * Return the best module function, given a capability list.
397  * \param p_this the vlc object
398  * \param psz_capability list of capabilities needed
399  * \param psz_name name of the module asked
400  * \param b_strict TRUE yto use the strict mode
401  * \return the module or NULL in case of a failure
402  */
403 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
404                           const char *psz_name, vlc_bool_t b_strict )
405 {
406     typedef struct module_list_t module_list_t;
407
408     struct module_list_t
409     {
410         module_t *p_module;
411         int i_score;
412         vlc_bool_t b_force;
413         module_list_t *p_next;
414     };
415
416     module_list_t *p_list, *p_first, *p_tmp;
417     vlc_list_t *p_all;
418
419     int i_which_module, i_index = 0;
420
421     module_t *p_module;
422
423     int   i_shortcuts = 0;
424     char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
425     vlc_bool_t b_force_backup = p_this->b_force;
426
427
428     /* Deal with variables */
429     if( psz_name && psz_name[0] == '$' )
430     {
431         vlc_value_t val;
432         var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
433         var_Get( p_this, psz_name + 1, &val );
434         psz_var = val.psz_string;
435         psz_name = psz_var;
436     }
437
438     /* Count how many different shortcuts were asked for */
439     if( psz_name && *psz_name )
440     {
441         char *psz_parser, *psz_last_shortcut;
442
443         /* If the user wants none, give him none. */
444         if( !strcmp( psz_name, "none" ) )
445         {
446             free( psz_var );
447             return NULL;
448         }
449
450         i_shortcuts++;
451         psz_shortcuts = psz_last_shortcut = strdup( psz_name );
452
453         for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
454         {
455             if( *psz_parser == ',' )
456             {
457                  *psz_parser = '\0';
458                  i_shortcuts++;
459                  psz_last_shortcut = psz_parser + 1;
460             }
461         }
462
463         /* Check if the user wants to override the "strict" mode */
464         if( psz_last_shortcut )
465         {
466             if( !strcmp(psz_last_shortcut, "none") )
467             {
468                 b_strict = VLC_TRUE;
469                 i_shortcuts--;
470             }
471             else if( !strcmp(psz_last_shortcut, "any") )
472             {
473                 b_strict = VLC_FALSE;
474                 i_shortcuts--;
475             }
476         }
477     }
478
479     /* Sort the modules and test them */
480     p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
481     p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
482     p_first = NULL;
483     unsigned i_cpu = vlc_CPU();
484
485     /* Parse the module list for capabilities and probe each of them */
486     for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
487     {
488         int i_shortcut_bonus = 0;
489
490         p_module = (module_t *)p_all->p_values[i_which_module].p_object;
491
492         /* Test that this module can do what we need */
493         if( !module_IsCapable( p_module, psz_capability ) )
494         {
495             /* Don't recurse through the sub-modules because vlc_list_find()
496              * will list them anyway. */
497             continue;
498         }
499
500         /* Test if we have the required CPU */
501         if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
502         {
503             continue;
504         }
505
506         /* If we required a shortcut, check this plugin provides it. */
507         if( i_shortcuts > 0 )
508         {
509             vlc_bool_t b_trash;
510             const char *psz_name = psz_shortcuts;
511
512             /* Let's drop modules with a <= 0 score (unless they are
513              * explicitly requested) */
514             b_trash = p_module->i_score <= 0;
515
516             for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
517             {
518                 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
519                 {
520                     char *c;
521                     if( ( c = strchr( psz_name, '@' ) )
522                         ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
523                                         c-psz_name )
524                         : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
525                     {
526                         /* Found it */
527                         if( c && c[1] )
528                             psz_alias = c+1;
529                         i_shortcut_bonus = i_short * 10000;
530                         goto found_shortcut;
531                     }
532                 }
533
534                 /* Go to the next shortcut... This is so lame! */
535                 psz_name += strlen( psz_name ) + 1;
536             }
537
538             /* If we are in "strict" mode and we couldn't
539              * find the module in the list of provided shortcuts,
540              * then kick the bastard out of here!!! */
541             if( b_strict )
542                 continue;
543         }
544         /* If we didn't require a shortcut, trash <= 0 scored plugins */
545         else if( p_module->i_score <= 0 )
546         {
547             continue;
548         }
549
550 found_shortcut:
551
552         /* Store this new module */
553         p_list[ i_index ].p_module = p_module;
554         p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
555         p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
556
557         /* Add it to the modules-to-probe list */
558         if( i_index == 0 )
559         {
560             p_list[ 0 ].p_next = NULL;
561             p_first = p_list;
562         }
563         else
564         {
565             /* Ok, so at school you learned that quicksort is quick, and
566              * bubble sort sucks raw eggs. But that's when dealing with
567              * thousands of items. Here we have barely 50. */
568             module_list_t *p_newlist = p_first;
569
570             if( p_first->i_score < p_list[ i_index ].i_score )
571             {
572                 p_list[ i_index ].p_next = p_first;
573                 p_first = &p_list[ i_index ];
574             }
575             else
576             {
577                 while( p_newlist->p_next != NULL &&
578                     p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
579                 {
580                     p_newlist = p_newlist->p_next;
581                 }
582
583                 p_list[ i_index ].p_next = p_newlist->p_next;
584                 p_newlist->p_next = &p_list[ i_index ];
585             }
586         }
587
588         i_index++;
589     }
590
591     msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
592                                             i_index, i_index == 1 ? "" : "s" );
593
594     /* Lock all candidate modules */
595     p_tmp = p_first;
596     while( p_tmp != NULL )
597     {
598         vlc_object_yield( p_tmp->p_module );
599         p_tmp = p_tmp->p_next;
600     }
601
602     /* We can release the list, interesting modules were yielded */
603     vlc_list_release( p_all );
604
605     /* Parse the linked list and use the first successful module */
606     p_tmp = p_first;
607     while( p_tmp != NULL )
608     {
609 #ifdef HAVE_DYNAMIC_PLUGINS
610         /* Make sure the module is loaded in mem */
611         module_t *p_module = p_tmp->p_module;
612         if( p_module->b_submodule )
613             p_module = (module_t *)p_module->p_parent;
614
615         if( !p_module->b_builtin && !p_module->b_loaded )
616         {
617             module_t *p_new_module =
618                 AllocatePlugin( p_this, p_module->psz_filename );
619             if( p_new_module )
620             {
621                 CacheMerge( p_this, p_module, p_new_module );
622                 vlc_object_attach( p_new_module, p_module );
623                 DeleteModule( p_new_module, VLC_TRUE );
624             }
625         }
626 #endif
627
628         p_this->b_force = p_tmp->b_force;
629         if( p_tmp->p_module->pf_activate
630              && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
631         {
632             break;
633         }
634
635         vlc_object_release( p_tmp->p_module );
636         p_tmp = p_tmp->p_next;
637     }
638
639     /* Store the locked module value */
640     if( p_tmp != NULL )
641     {
642         p_module = p_tmp->p_module;
643         p_tmp = p_tmp->p_next;
644     }
645     else
646     {
647         p_module = NULL;
648     }
649
650     /* Unlock the remaining modules */
651     while( p_tmp != NULL )
652     {
653         vlc_object_release( p_tmp->p_module );
654         p_tmp = p_tmp->p_next;
655     }
656
657     free( p_list );
658     p_this->b_force = b_force_backup;
659
660     if( p_module != NULL )
661     {
662         msg_Dbg( p_this, "using %s module \"%s\"",
663                  psz_capability, p_module->psz_object_name );
664     }
665     else if( p_first == NULL )
666     {
667         if( !strcmp( psz_capability, "access_demux" ) )
668         {
669             msg_Warn( p_this, "no %s module matched \"%s\"",
670                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
671         }
672         else
673         {
674             msg_Err( p_this, "no %s module matched \"%s\"",
675                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
676
677             msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
678                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
679         }
680     }
681     else if( psz_name != NULL && *psz_name )
682     {
683         msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
684                   psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
685     }
686     else
687         msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
688
689     if( p_module && !p_this->psz_object_name )
690     {
691         /* This assumes that p_this is the object which will be using the
692          * module. That's not always the case ... but it is in most cases.
693          */
694         p_this->psz_object_name = p_module->psz_object_name;
695     }
696
697     free( psz_shortcuts );
698     free( psz_var );
699
700     /* Don't forget that the module is still locked */
701     return p_module;
702 }
703
704 /**
705  * Module unneed
706  *
707  * This function must be called by the thread that called module_Need, to
708  * decrease the reference count and allow for hiding of modules.
709  * \param p_this vlc object structure
710  * \param p_module the module structure
711  * \return nothing
712  */
713 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
714 {
715     /* Use the close method */
716     if( p_module->pf_deactivate )
717     {
718         p_module->pf_deactivate( p_this );
719     }
720
721     msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
722
723     vlc_object_release( p_module );
724
725     return;
726 }
727
728 /**
729  * Get a pointer to a module_t given it's name.
730  *
731  * \param p_this vlc object structure
732  * \param psz_name the name of the module
733  * \return a pointer to the module or NULL in case of a failure
734  */
735 module_t *__module_Find( vlc_object_t *p_this, const char * psz_name )
736 {
737     vlc_list_t *p_list;
738     int i;
739     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
740     for( i = 0 ; i < p_list->i_count; i++)
741     {
742         module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
743         const char *psz_module_name = p_module->psz_object_name;
744         if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
745         {
746             /* We can release the list, and return yes */
747             vlc_object_yield( p_module );
748             vlc_list_release( p_list );
749             return p_module;
750         }
751     }
752     vlc_list_release( p_list );
753     return NULL;
754 }
755
756
757 /**
758  * Release a module_t pointer from module_Find().
759  *
760  * \param module the module to release
761  * \return nothing
762  */
763 void module_Put( module_t *module )
764 {
765     vlc_object_release( module );
766 }
767
768
769 /**
770  * Tell if a module exists and release it in thic case
771  *
772  * \param p_this vlc object structure
773  * \param psz_name th name of the module
774  * \return TRUE if the module exists
775  */
776 vlc_bool_t __module_Exists( vlc_object_t *p_this, const char * psz_name )
777 {
778     module_t *p_module = __module_Find( p_this, psz_name );
779     if( p_module )
780     {
781         module_Put( p_module );
782         return VLC_TRUE;
783     }
784     else
785     {
786         return VLC_FALSE;
787     }
788 }
789
790 /**
791  * GetModuleNamesForCapability
792  *
793  * Return a NULL terminated array with the names of the modules
794  * that have a certain capability.
795  * Free after uses both the string and the table.
796  * \param p_this vlc object structure
797  * \param psz_capability the capability asked
798  * \param pppsz_longname an pointer to an array of string to contain
799     the long names of the modules. If set to NULL the function don't use it.
800  * \return the NULL terminated array
801  */
802 char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
803                                                const char *psz_capability,
804                                                char ***pppsz_longname )
805 {
806     vlc_list_t *p_list;
807     int i, j, count = 0;
808     char **psz_ret;
809
810     /* Do it in two passes : count the number of modules before */
811     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
812     for( i = 0 ; i < p_list->i_count; i++)
813     {
814         module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
815         const char *psz_module_capability = p_module->psz_capability;
816         if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
817             count++;
818     }
819
820     psz_ret = malloc( sizeof(char*) * (count+1) );
821     if( pppsz_longname )
822         *pppsz_longname = malloc( sizeof(char*) * (count+1) );
823     if( !psz_ret || ( pppsz_longname && *pppsz_longname == NULL ) )
824     {
825         free( psz_ret );
826         free( *pppsz_longname );
827         *pppsz_longname = NULL;
828         vlc_list_release( p_list );
829         return NULL;
830     }
831
832     j = 0;
833     for( i = 0 ; i < p_list->i_count; i++)
834     {
835         module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
836         const char *psz_module_capability = p_module->psz_capability;
837         if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
838         {
839             int k = -1; /* hack to handle submodules properly */
840             if( p_module->b_submodule )
841             {
842                 while( p_module->pp_shortcuts[++k] != NULL );
843                 k--;
844             }
845             psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
846                                      :p_module->psz_object_name );
847             if( pppsz_longname )
848                 (*pppsz_longname)[j] = strdup( module_GetName( p_module, VLC_TRUE ) );
849             j++;
850         }
851     }
852     psz_ret[count] = NULL;
853
854     vlc_list_release( p_list );
855
856     return psz_ret;
857 }
858
859 /**
860  * Get the configuration of a module
861  *
862  * \param module the module
863  * \param psize the size of the configuration returned
864  * \return the configuration as an array
865  */
866 module_config_t *module_GetConfig( const module_t *module, unsigned *restrict psize )
867 {
868     unsigned i,j;
869     unsigned size = module->confsize;
870     module_config_t *config = malloc( size * sizeof( *config ) );
871
872     assert( psize != NULL );
873     *psize = 0;
874
875     if( !config )
876         return NULL;
877
878     for( i = 0, j = 0; i < size; i++ )
879     {
880         const module_config_t *item = module->p_config + i;
881         if( item->b_internal /* internal option */
882          || item->b_unsaveable /* non-modifiable option */
883          || item->b_removed /* removed option */ )
884             continue;
885
886         memcpy( config + j, item, sizeof( *config ) );
887         j++;
888     }
889     *psize = j;
890
891     return config;
892 }
893
894 /**
895  * Release the configuration
896  *
897  * \param the configuration
898  * \return nothing
899  */
900 void module_PutConfig( module_config_t *config )
901 {
902     free( config );
903 }
904
905 /*****************************************************************************
906  * Following functions are local.
907  *****************************************************************************/
908
909  /*****************************************************************************
910  * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
911  * return first path.
912  *****************************************************************************/
913 static char * copy_next_paths_token( char * paths, char ** remaining_paths )
914 {
915     char * path;
916     int i;
917     bool escaped = false;
918
919     assert( paths );
920
921     /* Alloc a buffer to store the path */
922     path = malloc( strlen( paths ) + 1 );
923     if( !path ) return NULL;
924
925     /* Look for PATH_SEP_CHAR (a ':' or a ';') */
926     for( i = 0; paths[i]; i++ ) {
927         /* Take care of \\ and \: or \; escapement */
928         if( escaped ) {
929             escaped = false;
930             path[i] = paths[i];
931         }
932         else if( paths[i] == '\\' )
933             escaped = true;
934         else if( paths[i] == PATH_SEP_CHAR )
935             break;
936         else
937             path[i] = paths[i];
938     }
939     path[i] = 0;
940
941     /* Return the remaining paths */
942     if( remaining_paths ) {
943         *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
944     }
945
946     return path;
947 }
948
949 /*****************************************************************************
950  * AllocateAllPlugins: load all plugin modules we can find.
951  *****************************************************************************/
952 #ifdef HAVE_DYNAMIC_PLUGINS
953 static void AllocateAllPlugins( vlc_object_t *p_this )
954 {
955     char *paths, *path, *paths_iter;
956     char * extra_path;
957
958     /* Contruct the special search path for system that have a relocatable
959      * executable. Set it to <vlc path>/modules and <vlc path>/plugins. */
960 #if defined( WIN32 ) || defined( UNDER_CE ) || defined( __APPLE__ ) || defined( SYS_BEOS )
961     if( asprintf( &extra_path,
962                     "%s" DIR_SEP "modules" PATH_SEP
963                     "%s" DIR_SEP "plugins"
964                     "%s",
965                     vlc_global()->psz_vlcpath,
966                     vlc_global()->psz_vlcpath,
967 # if defined( WIN32 ) || defined( UNDER_CE )
968                     "" ) < 0 )
969 # else
970                     PATH_SEP PLUGIN_PATH ) < 0 )
971 # endif
972
973     {
974         msg_Err( p_this, "Not enough memory" );
975         return;
976     }
977 #else
978     extra_path = strdup( PLUGIN_PATH );
979 #endif
980
981     /* If the user provided a plugin path, we add it to the list */
982     char * userpaths = config_GetPsz( p_this, "plugin-path" );
983
984     if( asprintf( &paths, "modules" PATH_SEP "%s" PATH_SEP "plugins%s%s",
985                     extra_path,
986                     userpaths ? PATH_SEP : "",
987                     userpaths ? userpaths : "" ) < 0 )
988     {
989         msg_Err( p_this, "Not enough memory" );
990         free( userpaths );
991         free( extra_path );
992         return;
993     }
994
995     /* Free plugin-path and extra path */
996     free( userpaths );
997     free( extra_path );
998
999     msg_Dbg( p_this, "We will be looking for modules in `%s'", paths );
1000
1001     for( paths_iter = paths; paths_iter; )
1002     {
1003         path = copy_next_paths_token( paths_iter, &paths_iter );
1004         if( !path )
1005         {
1006             msg_Err( p_this, "Not enough memory" );
1007             return;
1008         }
1009
1010         msg_Dbg( p_this, "recursively browsing `%s'", path );
1011
1012         /* Don't go deeper than 5 subdirectories */
1013         AllocatePluginDir( p_this, path, 5 );
1014
1015         free( path );
1016     }
1017
1018     free( paths );
1019 }
1020
1021 /*****************************************************************************
1022  * AllocatePluginDir: recursively parse a directory to look for plugins
1023  *****************************************************************************/
1024 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
1025                                int i_maxdepth )
1026 {
1027 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
1028 #ifdef WIN32
1029 # undef opendir
1030 # undef closedir
1031 # undef readdir
1032 #endif
1033 #if defined( UNDER_CE ) || defined( _MSC_VER )
1034 #ifdef UNDER_CE
1035     wchar_t psz_wpath[MAX_PATH + 256];
1036     wchar_t psz_wdir[MAX_PATH];
1037 #endif
1038     char psz_path[MAX_PATH + 256];
1039     WIN32_FIND_DATA finddata;
1040     HANDLE handle;
1041     int rc;
1042 #else
1043     int    i_dirlen;
1044     DIR *  dir;
1045     struct dirent * file;
1046 #endif
1047     char * psz_file;
1048
1049     if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
1050     {
1051         return;
1052     }
1053
1054 #if defined( UNDER_CE ) || defined( _MSC_VER )
1055 #ifdef UNDER_CE
1056     MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
1057
1058     rc = GetFileAttributes( psz_wdir );
1059     if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1060
1061     /* Parse all files in the directory */
1062     swprintf( psz_wpath, L"%ls\\*", psz_wdir );
1063 #else
1064     rc = GetFileAttributes( psz_dir );
1065     if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1066 #endif
1067
1068     /* Parse all files in the directory */
1069     sprintf( psz_path, "%s\\*", psz_dir );
1070
1071 #ifdef UNDER_CE
1072     handle = FindFirstFile( psz_wpath, &finddata );
1073 #else
1074     handle = FindFirstFile( psz_path, &finddata );
1075 #endif
1076     if( handle == INVALID_HANDLE_VALUE )
1077     {
1078         /* Empty directory */
1079         return;
1080     }
1081
1082     /* Parse the directory and try to load all files it contains. */
1083     do
1084     {
1085 #ifdef UNDER_CE
1086         unsigned int i_len = wcslen( finddata.cFileName );
1087         swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
1088         sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
1089 #else
1090         unsigned int i_len = strlen( finddata.cFileName );
1091         sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
1092 #endif
1093
1094         /* Skip ".", ".." */
1095         if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
1096          || !strcmp( finddata.cFileName, ".." ) )
1097         {
1098             if( !FindNextFile( handle, &finddata ) ) break;
1099             continue;
1100         }
1101
1102 #ifdef UNDER_CE
1103         if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
1104 #else
1105         if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
1106 #endif
1107         {
1108             AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
1109         }
1110         else if( i_len > strlen( LIBEXT )
1111                   /* We only load files ending with LIBEXT */
1112                   && !strncasecmp( psz_path + strlen( psz_path)
1113                                    - strlen( LIBEXT ),
1114                                    LIBEXT, strlen( LIBEXT ) ) )
1115         {
1116             WIN32_FILE_ATTRIBUTE_DATA attrbuf;
1117             int64_t i_time = 0, i_size = 0;
1118
1119 #ifdef UNDER_CE
1120             if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
1121                                      &attrbuf ) )
1122 #else
1123             if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
1124                                      &attrbuf ) )
1125 #endif
1126             {
1127                 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
1128                 i_time <<= 32;
1129                 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
1130                 i_size = attrbuf.nFileSizeHigh;
1131                 i_size <<= 32;
1132                 i_size |= attrbuf.nFileSizeLow;
1133             }
1134             psz_file = psz_path;
1135
1136             AllocatePluginFile( p_this, psz_file, i_time, i_size );
1137         }
1138     }
1139     while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1140
1141     /* Close the directory */
1142     FindClose( handle );
1143
1144 #else
1145     dir = opendir( psz_dir );
1146     if( !dir )
1147     {
1148         return;
1149     }
1150
1151     i_dirlen = strlen( psz_dir );
1152
1153     /* Parse the directory and try to load all files it contains. */
1154     while( !p_this->p_libvlc->b_die && ( file = readdir( dir ) ) )
1155     {
1156         struct stat statbuf;
1157         unsigned int i_len;
1158         int i_stat;
1159
1160         /* Skip ".", ".." */
1161         if( !*file->d_name || !strcmp( file->d_name, "." )
1162          || !strcmp( file->d_name, ".." ) )
1163         {
1164             continue;
1165         }
1166
1167         i_len = strlen( file->d_name );
1168         psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1169         sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1170
1171         i_stat = stat( psz_file, &statbuf );
1172         if( !i_stat && statbuf.st_mode & S_IFDIR )
1173         {
1174             AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1175         }
1176         else if( i_len > strlen( LIBEXT )
1177                   /* We only load files ending with LIBEXT */
1178                   && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1179                                    LIBEXT, strlen( LIBEXT ) ) )
1180         {
1181             int64_t i_time = 0, i_size = 0;
1182
1183             if( !i_stat )
1184             {
1185                 i_time = statbuf.st_mtime;
1186                 i_size = statbuf.st_size;
1187             }
1188
1189             AllocatePluginFile( p_this, psz_file, i_time, i_size );
1190         }
1191
1192         free( psz_file );
1193     }
1194
1195     /* Close the directory */
1196     closedir( dir );
1197
1198 #endif
1199 }
1200
1201 /*****************************************************************************
1202  * AllocatePluginFile: load a module into memory and initialize it.
1203  *****************************************************************************
1204  * This function loads a dynamically loadable module and allocates a structure
1205  * for its information data. The module can then be handled by module_Need
1206  * and module_Unneed. It can be removed by DeleteModule.
1207  *****************************************************************************/
1208 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1209                                int64_t i_file_time, int64_t i_file_size )
1210 {
1211     module_t * p_module = NULL;
1212     module_cache_t *p_cache_entry = NULL;
1213
1214     /*
1215      * Check our plugins cache first then load plugin if needed
1216      */
1217     p_cache_entry =
1218         CacheFind( psz_file, i_file_time, i_file_size );
1219
1220     if( !p_cache_entry )
1221     {
1222         p_module = AllocatePlugin( p_this, psz_file );
1223     }
1224     else
1225     {
1226         /* If junk dll, don't try to load it */
1227         if( p_cache_entry->b_junk )
1228         {
1229             p_module = NULL;
1230         }
1231         else
1232         {
1233             module_config_t *p_item = NULL, *p_end = NULL;
1234
1235             p_module = p_cache_entry->p_module;
1236             p_module->b_loaded = VLC_FALSE;
1237
1238             /* For now we force loading if the module's config contains
1239              * callbacks or actions.
1240              * Could be optimized by adding an API call.*/
1241             for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1242                  p_item < p_end; p_item++ )
1243             {
1244                 if( p_item->pf_callback || p_item->i_action )
1245                 {
1246                     p_module = AllocatePlugin( p_this, psz_file );
1247                     break;
1248                 }
1249             }
1250             if( p_module == p_cache_entry->p_module )
1251                 p_cache_entry->b_used = VLC_TRUE;
1252         }
1253     }
1254
1255     if( p_module )
1256     {
1257         libvlc_global_data_t *p_libvlc_global = vlc_global();
1258
1259         /* Everything worked fine !
1260          * The module is ready to be added to the list. */
1261         p_module->b_builtin = VLC_FALSE;
1262
1263         /* msg_Dbg( p_this, "plugin \"%s\", %s",
1264                     p_module->psz_object_name, p_module->psz_longname ); */
1265
1266         vlc_object_attach( p_module, p_libvlc_global->p_module_bank );
1267
1268         if( !p_libvlc_global->p_module_bank->b_cache )
1269             return 0;
1270
1271 #define p_bank p_libvlc_global->p_module_bank
1272         /* Add entry to cache */
1273         p_bank->pp_cache =
1274             realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1275         p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1276         if( !p_bank->pp_cache[p_bank->i_cache] )
1277             return -1;
1278         p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1279         p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1280         p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1281         p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1282         p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1283         p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1284         p_bank->i_cache++;
1285     }
1286
1287     return p_module ? 0 : -1;
1288 }
1289
1290 /*****************************************************************************
1291  * AllocatePlugin: load a module into memory and initialize it.
1292  *****************************************************************************
1293  * This function loads a dynamically loadable module and allocates a structure
1294  * for its information data. The module can then be handled by module_Need
1295  * and module_Unneed. It can be removed by DeleteModule.
1296  *****************************************************************************/
1297 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1298 {
1299     module_t * p_module = NULL;
1300     module_handle_t handle;
1301
1302     if( module_Load( p_this, psz_file, &handle ) )
1303         return NULL;
1304
1305     /* Now that we have successfully loaded the module, we can
1306      * allocate a structure for it */
1307     p_module = vlc_module_create( p_this );
1308     if( p_module == NULL )
1309     {
1310         msg_Err( p_this, "out of memory" );
1311         module_Unload( handle );
1312         return NULL;
1313     }
1314
1315     /* We need to fill these since they may be needed by module_Call() */
1316     p_module->psz_filename = psz_file;
1317     p_module->handle = handle;
1318     p_module->b_loaded = VLC_TRUE;
1319
1320     /* Initialize the module: fill p_module, default config */
1321     if( module_Call( p_module ) != 0 )
1322     {
1323         /* We couldn't call module_init() */
1324         vlc_object_release( p_module );
1325         module_Unload( handle );
1326         return NULL;
1327     }
1328
1329     DupModule( p_module );
1330     p_module->psz_filename = strdup( p_module->psz_filename );
1331
1332     /* Everything worked fine ! The module is ready to be added to the list. */
1333     p_module->b_builtin = VLC_FALSE;
1334
1335     return p_module;
1336 }
1337
1338 /*****************************************************************************
1339  * DupModule: make a plugin module standalone.
1340  *****************************************************************************
1341  * This function duplicates all strings in the module, so that the dynamic
1342  * object can be unloaded. It acts recursively on submodules.
1343  *****************************************************************************/
1344 static void DupModule( module_t *p_module )
1345 {
1346     const char **pp_shortcut;
1347     int i_submodule;
1348
1349     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1350     {
1351         *pp_shortcut = strdup( *pp_shortcut );
1352     }
1353
1354     /* We strdup() these entries so that they are still valid when the
1355      * module is unloaded. */
1356     p_module->psz_object_name = strdup( p_module->psz_object_name );
1357     p_module->psz_capability = strdup( p_module->psz_capability );
1358     p_module->psz_shortname = p_module->psz_shortname ?
1359                                  strdup( p_module->psz_shortname ) : NULL;
1360     p_module->psz_longname = strdup( p_module->psz_longname );
1361     p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1362                                             : NULL;
1363
1364     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1365     {
1366         DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1367     }
1368 }
1369
1370 /*****************************************************************************
1371  * UndupModule: free a duplicated module.
1372  *****************************************************************************
1373  * This function frees the allocations done in DupModule().
1374  *****************************************************************************/
1375 static void UndupModule( module_t *p_module )
1376 {
1377     const char **pp_shortcut;
1378     int i_submodule;
1379
1380     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1381     {
1382         UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1383     }
1384
1385     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1386     {
1387         free( (void*)*pp_shortcut );
1388     }
1389
1390     free( (void*)p_module->psz_object_name );
1391     free( p_module->psz_capability );
1392     free( (void*)p_module->psz_shortname );
1393     free( (void*)p_module->psz_longname );
1394     free( (void*)p_module->psz_help );
1395 }
1396
1397 #endif /* HAVE_DYNAMIC_PLUGINS */
1398
1399 /*****************************************************************************
1400  * AllocateBuiltinModule: initialize a builtin module.
1401  *****************************************************************************
1402  * This function registers a builtin module and allocates a structure
1403  * for its information data. The module can then be handled by module_Need
1404  * and module_Unneed. It can be removed by DeleteModule.
1405  *****************************************************************************/
1406 static int AllocateBuiltinModule( vlc_object_t * p_this,
1407                                   int ( *pf_entry ) ( module_t * ) )
1408 {
1409     module_t * p_module;
1410
1411     /* Now that we have successfully loaded the module, we can
1412      * allocate a structure for it */
1413     p_module = vlc_module_create( p_this );
1414     if( p_module == NULL )
1415     {
1416         msg_Err( p_this, "out of memory" );
1417         return -1;
1418     }
1419
1420     /* Initialize the module : fill p_module->psz_object_name, etc. */
1421     if( pf_entry( p_module ) != 0 )
1422     {
1423         /* With a well-written module we shouldn't have to print an
1424          * additional error message here, but just make sure. */
1425         msg_Err( p_this, "failed calling entry point in builtin module" );
1426         vlc_object_release( p_module );
1427         return -1;
1428     }
1429
1430     /* Everything worked fine ! The module is ready to be added to the list. */
1431     p_module->b_builtin = VLC_TRUE;
1432
1433     /* msg_Dbg( p_this, "builtin \"%s\", %s",
1434                 p_module->psz_object_name, p_module->psz_longname ); */
1435
1436     vlc_object_attach( p_module, vlc_global()->p_module_bank );
1437
1438     return 0;
1439 }
1440
1441 /*****************************************************************************
1442  * DeleteModule: delete a module and its structure.
1443  *****************************************************************************
1444  * This function can only be called if the module isn't being used.
1445  *****************************************************************************/
1446 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1447 {
1448     if( !p_module ) return VLC_EGENERIC;
1449     if( b_detach )
1450         vlc_object_detach( p_module );
1451
1452     /* We free the structures that we strdup()ed in Allocate*Module(). */
1453 #ifdef HAVE_DYNAMIC_PLUGINS
1454     if( !p_module->b_builtin )
1455     {
1456         if( p_module->b_loaded && p_module->b_unloadable )
1457         {
1458             module_Unload( p_module->handle );
1459         }
1460         UndupModule( p_module );
1461         free( p_module->psz_filename );
1462     }
1463 #endif
1464
1465     /* Free and detach the object's children */
1466     while( p_module->i_children )
1467     {
1468         vlc_object_t *p_this = p_module->pp_children[0];
1469         vlc_object_detach( p_this );
1470         vlc_object_release( p_this );
1471     }
1472
1473     config_Free( p_module );
1474     vlc_object_release( p_module );
1475     p_module = NULL;
1476     return 0;
1477 }