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