1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
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>
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.
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.
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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_memory.h>
36 #include <stdlib.h> /* free(), strtol() */
37 #include <stdio.h> /* sprintf() */
38 #include <string.h> /* strdup() */
45 #ifdef HAVE_SYS_TYPES_H
46 # include <sys/types.h>
48 #ifdef HAVE_SYS_STAT_H
49 # include <sys/stat.h>
55 #if !defined(HAVE_DYNAMIC_PLUGINS)
56 /* no support for plugins */
57 #elif defined(HAVE_DL_DYLD)
58 # if defined(HAVE_MACH_O_DYLD_H)
59 # include <mach-o/dyld.h>
61 #elif defined(HAVE_DL_BEOS)
62 # if defined(HAVE_IMAGE_H)
65 #elif defined(HAVE_DL_WINDOWS)
67 #elif defined(HAVE_DL_DLOPEN)
68 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
71 # if defined(HAVE_SYS_DL_H)
74 #elif defined(HAVE_DL_SHL_LOAD)
75 # if defined(HAVE_DL_H)
80 #include "config/configuration.h"
82 #include "vlc_charset.h"
83 #include "vlc_arrays.h"
86 #include "modules/modules.h"
88 static module_bank_t *p_module_bank = NULL;
89 static vlc_mutex_t module_lock = VLC_STATIC_MUTEX;
91 int vlc_entry__main( module_t * );
93 /*****************************************************************************
95 *****************************************************************************/
96 #ifdef HAVE_DYNAMIC_PLUGINS
97 static void AllocateAllPlugins( vlc_object_t *, module_bank_t * );
98 static void AllocatePluginDir( vlc_object_t *, module_bank_t *, const char *,
100 static int AllocatePluginFile( vlc_object_t *, module_bank_t *, const char *,
102 static module_t * AllocatePlugin( vlc_object_t *, const char * );
104 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
105 static void DeleteModule ( module_bank_t *, module_t * );
106 #ifdef HAVE_DYNAMIC_PLUGINS
107 static void DupModule ( module_t * );
108 static void UndupModule ( module_t * );
114 * Creates a module bank structure which will be filled later
115 * on with all the modules found.
116 * \param p_this vlc object structure
119 void __module_InitBank( vlc_object_t *p_this )
121 module_bank_t *p_bank = NULL;
123 vlc_mutex_lock( &module_lock );
125 if( p_module_bank == NULL )
127 p_bank = calloc (1, sizeof(*p_bank));
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 = false;
134 /* Everything worked, attach the object */
135 p_module_bank = p_bank;
137 /* Fills the module bank structure with the main module infos.
138 * This is very useful as it will allow us to consider the main
139 * library just as another module, and for instance the configuration
140 * options of main will be available in the module bank structure just
141 * as for every other module. */
142 AllocateBuiltinModule( p_this, vlc_entry__main );
145 p_module_bank->i_usage++;
147 /* We do retain the module bank lock until the plugins are loaded as well.
148 * This is ugly, this staged loading approach is needed: LibVLC gets
149 * some configuration parameters relevant to loading the plugins from
150 * the main (builtin) module. The module bank becomes shared read-only data
151 * once it is ready, so we need to fully serialize initialization.
152 * DO NOT UNCOMMENT the following line unless you managed to squeeze
153 * module_LoadPlugins() before you unlock the mutex. */
154 /*vlc_mutex_unlock( &module_lock );*/
157 #undef module_EndBank
159 * Unloads all unused plugin modules and empties the module
160 * bank in case of success.
161 * \param p_this vlc object structure
164 void module_EndBank( vlc_object_t *p_this, bool b_plugins )
166 module_bank_t *p_bank = p_module_bank;
168 assert (p_bank != NULL);
170 /* Save the configuration */
171 if( !config_GetInt( p_this, "ignore-config" ) )
172 config_AutoSaveConfigFile( p_this );
174 /* If plugins were _not_ loaded, then the caller still has the bank lock
175 * from module_InitBank(). */
177 vlc_mutex_lock( &module_lock );
179 vlc_assert_locked( &module_lock ); not for static mutexes :( */
181 if( --p_bank->i_usage > 0 )
183 vlc_mutex_unlock( &module_lock );
186 p_module_bank = NULL;
187 vlc_mutex_unlock( &module_lock );
189 #ifdef HAVE_DYNAMIC_PLUGINS
190 if( p_bank->b_cache )
191 CacheSave( p_this, p_bank );
192 while( p_bank->i_loaded_cache-- )
194 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
196 DeleteModule( p_bank,
197 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module );
198 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
199 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
202 free( p_bank->pp_loaded_cache );
203 while( p_bank->i_cache-- )
205 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
206 free( p_bank->pp_cache[p_bank->i_cache] );
208 free( p_bank->pp_cache );
211 while( p_bank->head != NULL )
212 DeleteModule( p_bank, p_bank->head );
217 #undef module_LoadPlugins
219 * Loads module descriptions for all available plugins.
220 * Fills the module bank structure with the plugin modules.
222 * \param p_this vlc object structure
225 void module_LoadPlugins( vlc_object_t * p_this, bool b_cache_delete )
227 module_bank_t *p_bank = p_module_bank;
230 /*vlc_assert_locked( &module_lock ); not for static mutexes :( */
232 #ifdef HAVE_DYNAMIC_PLUGINS
233 if( p_bank->i_usage == 1 )
235 msg_Dbg( p_this, "checking plugin modules" );
236 p_module_bank->b_cache = config_GetInt( p_this, "plugins-cache" ) > 0;
238 if( p_module_bank->b_cache || b_cache_delete )
239 CacheLoad( p_this, p_module_bank, b_cache_delete );
240 AllocateAllPlugins( p_this, p_module_bank );
243 p_module_bank->b_plugins = true;
244 vlc_mutex_unlock( &module_lock );
248 * Checks whether a module implements a capability.
250 * \param m the module
251 * \param cap the capability to check
252 * \return TRUE if the module have the capability
254 bool module_provides( const module_t *m, const char *cap )
256 return !strcmp( m->psz_capability, cap );
260 * Get the internal name of a module
262 * \param m the module
263 * \return the module name
265 const char *module_get_object( const module_t *m )
267 return m->psz_object_name;
271 * Get the human-friendly name of a module.
273 * \param m the module
274 * \param long_name TRUE to have the long name of the module
275 * \return the short or long name of the module
277 const char *module_get_name( const module_t *m, bool long_name )
279 if( long_name && ( m->psz_longname != NULL) )
280 return m->psz_longname;
282 return m->psz_shortname ? m->psz_shortname : m->psz_object_name;
286 * Get the help for a module
288 * \param m the module
291 const char *module_get_help( const module_t *m )
297 * Get the capability for a module
299 * \param m the module
300 * return the capability
302 const char *module_get_capability( const module_t *m )
304 return m->psz_capability;
308 * Get the score for a module
310 * \param m the module
311 * return the score for the capability
313 int module_get_score( const module_t *m )
318 module_t *module_hold (module_t *m)
320 vlc_hold (&m->vlc_gc_data);
324 void module_release (module_t *m)
326 vlc_release (&m->vlc_gc_data);
330 * Frees the flat list of VLC modules.
331 * @param list list obtained by module_list_get()
332 * @param length number of items on the list
335 void module_list_free (module_t **list)
340 for (size_t i = 0; list[i] != NULL; i++)
341 module_release (list[i]);
346 * Gets the flat list of VLC modules.
347 * @param n [OUT] pointer to the number of modules or NULL
348 * @return NULL-terminated table of module pointers
349 * (release with module_list_free()), or NULL in case of error.
351 module_t **module_list_get (size_t *n)
353 /* TODO: this whole module lookup is quite inefficient */
354 /* Remove this and improve module_need */
355 module_t **tab = NULL;
358 assert (p_module_bank);
359 for (module_t *mod = p_module_bank->head; mod; mod = mod->next)
362 nt = realloc (tab, (i + 2 + mod->submodule_count) * sizeof (*tab));
365 module_list_free (tab);
370 tab[i++] = module_hold (mod);
371 for (module_t *subm = mod->submodule; subm; subm = subm->next)
372 tab[i++] = module_hold (subm);
380 typedef struct module_list_t
387 static int modulecmp (const void *a, const void *b)
389 const module_list_t *la = a, *lb = b;
390 /* Note that qsort() uses _ascending_ order,
391 * so the smallest module is the one with the biggest score. */
392 return lb->i_score - la->i_score;
398 * Return the best module function, given a capability list.
400 * \param p_this the vlc object
401 * \param psz_capability list of capabilities needed
402 * \param psz_name name of the module asked
403 * \param b_strict TRUE yto use the strict mode
404 * \return the module or NULL in case of a failure
406 module_t * __module_need( vlc_object_t *p_this, const char *psz_capability,
407 const char *psz_name, bool b_strict )
409 stats_TimerStart( p_this, "module_need()", STATS_TIMER_MODULE_NEED );
411 module_list_t *p_list;
414 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
415 bool b_force_backup = p_this->b_force;
417 /* Deal with variables */
418 if( psz_name && psz_name[0] == '$' )
420 psz_name = psz_var = var_CreateGetString( p_this, psz_name + 1 );
423 /* Count how many different shortcuts were asked for */
424 if( psz_name && *psz_name )
426 char *psz_parser, *psz_last_shortcut;
428 /* If the user wants none, give him none. */
429 if( !strcmp( psz_name, "none" ) )
432 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
433 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
434 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
439 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
441 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
443 if( *psz_parser == ',' )
447 psz_last_shortcut = psz_parser + 1;
451 /* Check if the user wants to override the "strict" mode */
452 if( psz_last_shortcut )
454 if( !strcmp(psz_last_shortcut, "none") )
459 else if( !strcmp(psz_last_shortcut, "any") )
467 /* Sort the modules and test them */
469 module_t **p_all = module_list_get (&count);
470 p_list = malloc( count * sizeof( module_list_t ) );
471 unsigned i_cpu = vlc_CPU();
473 /* Parse the module list for capabilities and probe each of them */
475 for (size_t i = 0; (p_module = p_all[i]) != NULL; i++)
477 bool b_shortcut_bonus = false;
479 /* Test that this module can do what we need */
480 if( !module_provides( p_module, psz_capability ) )
482 /* Test if we have the required CPU */
483 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
486 /* If we required a shortcut, check this plugin provides it. */
487 if( i_shortcuts > 0 )
489 const char *name = psz_shortcuts;
491 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
493 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
496 if( ( c = strchr( name, '@' ) )
497 ? !strncasecmp( name, p_module->pp_shortcuts[i],
499 : !strcasecmp( name, p_module->pp_shortcuts[i] ) )
504 b_shortcut_bonus = true;
509 /* Go to the next shortcut... This is so lame! */
510 name += strlen( name ) + 1;
513 /* If we are in "strict" mode and we couldn't
514 * find the module in the list of provided shortcuts,
515 * then kick the bastard out of here!!! */
520 /* Trash <= 0 scored plugins (they can only be selected by shortcut) */
521 if( p_module->i_score <= 0 )
525 /* Store this new module */
526 p_list[count].p_module = module_hold (p_module);
527 p_list[count].i_score = p_module->i_score;
528 if( b_shortcut_bonus )
529 p_list[count].i_score += 10000;
530 p_list[count].b_force = b_shortcut_bonus && b_strict;
534 /* We can release the list, interesting modules are held */
535 module_list_free (p_all);
537 /* Sort candidates by descending score */
538 qsort (p_list, count, sizeof (p_list[0]), modulecmp);
539 msg_Dbg( p_this, "looking for %s module: %zu candidate%s", psz_capability,
540 count, count == 1 ? "" : "s" );
542 /* Parse the linked list and use the first successful module */
544 for (size_t i = 0; (i < count) && (p_module == NULL); i++)
546 module_t *p_cand = p_list[i].p_module;
547 #ifdef HAVE_DYNAMIC_PLUGINS
548 /* Make sure the module is loaded in mem */
549 module_t *p_real = p_cand->b_submodule ? p_cand->parent : p_cand;
551 if( !p_real->b_builtin && !p_real->b_loaded )
553 module_t *p_new_module =
554 AllocatePlugin( p_this, p_real->psz_filename );
555 if( p_new_module == NULL )
556 { /* Corrupted module */
557 msg_Err( p_this, "possibly corrupt module cache" );
558 module_release( p_cand );
561 CacheMerge( p_this, p_real, p_new_module );
562 DeleteModule( p_module_bank, p_new_module );
566 p_this->b_force = p_list[i].b_force;
568 int ret = VLC_SUCCESS;
569 if( p_cand->pf_activate )
570 ret = p_cand->pf_activate( p_this );
579 /* good module, but aborted */
580 module_release( p_cand );
583 default: /* bad module */
584 module_release( p_cand );
588 /* Release the remaining modules */
590 module_release (p_list[i].p_module);
594 p_this->b_force = b_force_backup;
596 if( p_module != NULL )
598 msg_Dbg( p_this, "using %s module \"%s\"",
599 psz_capability, p_module->psz_object_name );
600 vlc_object_set_name( p_this, psz_alias ? psz_alias
601 : p_module->psz_object_name );
603 else if( count == 0 )
605 if( !strcmp( psz_capability, "access_demux" )
606 || !strcmp( psz_capability, "stream_filter" )
607 || !strcmp( psz_capability, "vout_window" ) )
609 msg_Dbg( p_this, "no %s module matched \"%s\"",
610 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
614 msg_Err( p_this, "no %s module matched \"%s\"",
615 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
617 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
618 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
621 else if( psz_name != NULL && *psz_name )
623 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
624 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
627 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
629 free( psz_shortcuts );
632 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
633 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
634 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
636 /* Don't forget that the module is still locked */
643 * This function must be called by the thread that called module_need, to
644 * decrease the reference count and allow for hiding of modules.
645 * \param p_this vlc object structure
646 * \param p_module the module structure
649 void __module_unneed( vlc_object_t * p_this, module_t * p_module )
651 /* Use the close method */
652 if( p_module->pf_deactivate )
654 p_module->pf_deactivate( p_this );
657 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
659 module_release( p_module );
663 * Get a pointer to a module_t given it's name.
665 * \param psz_name the name of the module
666 * \return a pointer to the module or NULL in case of a failure
668 module_t *module_find( const char * psz_name )
670 module_t **list, *module;
672 list = module_list_get (NULL);
676 for (size_t i = 0; (module = list[i]) != NULL; i++)
678 const char *psz_module_name = module->psz_object_name;
680 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
682 module_hold (module);
686 module_list_free (list);
691 * Tell if a module exists and release it in thic case
693 * \param psz_name th name of the module
694 * \return TRUE if the module exists
696 bool module_exists (const char * psz_name)
698 module_t *p_module = module_find (psz_name);
700 module_release (p_module);
701 return p_module != NULL;
705 * Get a pointer to a module_t that matches a shortcut.
706 * This is a temporary hack for SD. Do not re-use (generally multiple modules
707 * can have the same shortcut, so this is *broken* - use module_need()!).
709 * \param psz_shortcut shortcut of the module
710 * \param psz_cap capability of the module
711 * \return a pointer to the module or NULL in case of a failure
713 module_t *module_find_by_shortcut (const char *psz_shortcut)
715 module_t **list, *module;
717 list = module_list_get (NULL);
721 for (size_t i = 0; (module = list[i]) != NULL; i++)
724 (module->pp_shortcuts[j] != NULL) && (j < MODULE_SHORTCUT_MAX);
727 if (!strcmp (module->pp_shortcuts[j], psz_shortcut))
729 module_hold (module);
735 module_list_free (list);
740 * GetModuleNamesForCapability
742 * Return a NULL terminated array with the names of the modules
743 * that have a certain capability.
744 * Free after uses both the string and the table.
745 * \param psz_capability the capability asked
746 * \param pppsz_longname an pointer to an array of string to contain
747 the long names of the modules. If set to NULL the function don't use it.
748 * \return the NULL terminated array
750 char ** module_GetModulesNamesForCapability( const char *psz_capability,
751 char ***pppsz_longname )
756 module_t **list = module_list_get (NULL);
758 /* Proceed in two passes: count the number of modules first */
759 for (size_t i = 0; list[i]; i++)
761 module_t *p_module = list[i];
762 const char *psz_module_capability = p_module->psz_capability;
764 if( psz_module_capability
765 && !strcmp( psz_module_capability, psz_capability ) )
769 /* Then get the names */
770 psz_ret = malloc( sizeof(char*) * (count+1) );
772 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
773 if( !psz_ret || ( pppsz_longname && *pppsz_longname == NULL ) )
778 free( *pppsz_longname );
779 *pppsz_longname = NULL;
781 module_list_free (list);
785 for (size_t i = 0, j = 0; list[i]; i++)
787 module_t *p_module = list[i];
788 const char *psz_module_capability = p_module->psz_capability;
790 if( psz_module_capability
791 && !strcmp( psz_module_capability, psz_capability ) )
793 /* Explicit hack: Use the last shortcut. It _should_ be
794 * different from the object name, at least if the object
795 * contains multiple submodules with the same capability. */
797 while( p_module->pp_shortcuts[k] != NULL )
799 assert( k > 0); /* pp_shortcuts[0] is always set */
800 psz_ret[j] = strdup( p_module->pp_shortcuts[k - 1] );
802 (*pppsz_longname)[j] = strdup( module_get_name( p_module, true ) );
806 psz_ret[count] = NULL;
808 module_list_free (list);
814 * Get the configuration of a module
816 * \param module the module
817 * \param psize the size of the configuration returned
818 * \return the configuration as an array
820 module_config_t *module_config_get( const module_t *module, unsigned *restrict psize )
823 unsigned size = module->confsize;
824 module_config_t *config = malloc( size * sizeof( *config ) );
826 assert( psize != NULL );
832 for( i = 0, j = 0; i < size; i++ )
834 const module_config_t *item = module->p_config + i;
835 if( item->b_internal /* internal option */
836 || item->b_unsaveable /* non-modifiable option */
837 || item->b_removed /* removed option */ )
840 memcpy( config + j, item, sizeof( *config ) );
849 * Release the configuration
851 * \param the configuration
854 void module_config_free( module_config_t *config )
859 /*****************************************************************************
860 * Following functions are local.
861 *****************************************************************************/
863 /*****************************************************************************
864 * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
866 *****************************************************************************/
867 static char * copy_next_paths_token( char * paths, char ** remaining_paths )
871 bool escaped = false;
875 /* Alloc a buffer to store the path */
876 path = malloc( strlen( paths ) + 1 );
877 if( !path ) return NULL;
879 /* Look for PATH_SEP_CHAR (a ':' or a ';') */
880 for( i = 0, done = 0 ; paths[i]; i++ )
882 /* Take care of \\ and \: or \; escapement */
886 path[done++] = paths[i];
889 else if( paths[i] == '/' )
892 else if( paths[i] == '\\' )
895 else if( paths[i] == PATH_SEP_CHAR )
898 path[done++] = paths[i];
902 /* Return the remaining paths */
903 if( remaining_paths ) {
904 *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
910 char *psz_vlcpath = NULL;
912 /*****************************************************************************
913 * AllocateAllPlugins: load all plugin modules we can find.
914 *****************************************************************************/
915 #ifdef HAVE_DYNAMIC_PLUGINS
916 static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank )
918 const char *vlcpath = psz_vlcpath;
921 vlc_array_t *arraypaths = vlc_array_new();
923 /* Contruct the special search path for system that have a relocatable
924 * executable. Set it to <vlc path>/modules and <vlc path>/plugins. */
926 if( vlcpath && asprintf( &path, "%s" DIR_SEP "modules", vlcpath ) != -1 )
927 vlc_array_append( arraypaths, path );
928 if( vlcpath && asprintf( &path, "%s" DIR_SEP "plugins", vlcpath ) != -1 )
929 vlc_array_append( arraypaths, path );
931 vlc_array_append( arraypaths, strdup( PLUGIN_PATH ) );
934 /* If the user provided a plugin path, we add it to the list */
935 char *userpaths = config_GetPsz( p_this, "plugin-path" );
938 for( paths_iter = userpaths; paths_iter; )
940 path = copy_next_paths_token( paths_iter, &paths_iter );
942 vlc_array_append( arraypaths, path );
945 count = vlc_array_count( arraypaths );
946 for( i = 0 ; i < count ; i++ )
948 path = vlc_array_item_at_index( arraypaths, i );
952 msg_Dbg( p_this, "recursively browsing `%s'", path );
954 /* Don't go deeper than 5 subdirectories */
955 AllocatePluginDir( p_this, p_bank, path, 5 );
960 vlc_array_destroy( arraypaths );
964 /*****************************************************************************
965 * AllocatePluginDir: recursively parse a directory to look for plugins
966 *****************************************************************************/
967 static void AllocatePluginDir( vlc_object_t *p_this, module_bank_t *p_bank,
968 const char *psz_dir, unsigned i_maxdepth )
970 if( i_maxdepth == 0 )
973 DIR *dh = utf8_opendir (psz_dir);
977 /* Parse the directory and try to load all files it contains. */
980 char *file = utf8_readdir (dh), *path;
987 if (!strcmp (file, ".") || !strcmp (file, ".."))
993 const int pathlen = asprintf (&path, "%s"DIR_SEP"%s", psz_dir, file);
995 if (pathlen == -1 || utf8_stat (path, &st))
998 if (S_ISDIR (st.st_mode))
999 /* Recurse into another directory */
1000 AllocatePluginDir (p_this, p_bank, path, i_maxdepth - 1);
1002 if (S_ISREG (st.st_mode)
1003 && ((size_t)pathlen >= strlen (LIBEXT))
1004 && !strncasecmp (path + pathlen - strlen (LIBEXT), LIBEXT,
1006 /* ^^ We only load files ending with LIBEXT */
1007 AllocatePluginFile (p_this, p_bank, path, st.st_mtime, st.st_size);
1014 /*****************************************************************************
1015 * AllocatePluginFile: load a module into memory and initialize it.
1016 *****************************************************************************
1017 * This function loads a dynamically loadable module and allocates a structure
1018 * for its information data. The module can then be handled by module_need
1019 * and module_unneed. It can be removed by DeleteModule.
1020 *****************************************************************************/
1021 static int AllocatePluginFile( vlc_object_t * p_this, module_bank_t *p_bank,
1022 const char *psz_file,
1023 int64_t i_file_time, int64_t i_file_size )
1025 module_t * p_module = NULL;
1026 module_cache_t *p_cache_entry = NULL;
1029 * Check our plugins cache first then load plugin if needed
1031 p_cache_entry = CacheFind( p_bank, psz_file, i_file_time, i_file_size );
1032 if( !p_cache_entry )
1034 p_module = AllocatePlugin( p_this, psz_file );
1037 /* If junk dll, don't try to load it */
1038 if( p_cache_entry->b_junk )
1042 module_config_t *p_item = NULL, *p_end = NULL;
1044 p_module = p_cache_entry->p_module;
1045 p_module->b_loaded = false;
1047 /* For now we force loading if the module's config contains
1048 * callbacks or actions.
1049 * Could be optimized by adding an API call.*/
1050 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1051 p_item < p_end; p_item++ )
1053 if( p_item->pf_callback || p_item->i_action )
1055 p_module = AllocatePlugin( p_this, psz_file );
1059 if( p_module == p_cache_entry->p_module )
1060 p_cache_entry->b_used = true;
1063 if( p_module == NULL )
1066 /* Everything worked fine !
1067 * The module is ready to be added to the list. */
1068 p_module->b_builtin = false;
1070 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1071 p_module->psz_object_name, p_module->psz_longname ); */
1072 p_module->next = p_bank->head;
1073 p_bank->head = p_module;
1075 if( !p_module_bank->b_cache )
1078 /* Add entry to cache */
1079 module_cache_t **pp_cache = p_bank->pp_cache;
1081 pp_cache = realloc_or_free( pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1082 if( pp_cache == NULL )
1084 pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1085 if( pp_cache[p_bank->i_cache] == NULL )
1087 pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1088 pp_cache[p_bank->i_cache]->i_time = i_file_time;
1089 pp_cache[p_bank->i_cache]->i_size = i_file_size;
1090 pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1091 pp_cache[p_bank->i_cache]->b_used = true;
1092 pp_cache[p_bank->i_cache]->p_module = p_module;
1093 p_bank->pp_cache = pp_cache;
1098 /*****************************************************************************
1099 * AllocatePlugin: load a module into memory and initialize it.
1100 *****************************************************************************
1101 * This function loads a dynamically loadable module and allocates a structure
1102 * for its information data. The module can then be handled by module_need
1103 * and module_unneed. It can be removed by DeleteModule.
1104 *****************************************************************************/
1105 static module_t * AllocatePlugin( vlc_object_t * p_this, const char *psz_file )
1107 module_t * p_module = NULL;
1108 module_handle_t handle;
1110 if( module_Load( p_this, psz_file, &handle ) )
1113 /* Now that we have successfully loaded the module, we can
1114 * allocate a structure for it */
1115 p_module = vlc_module_create( p_this );
1116 if( p_module == NULL )
1118 module_Unload( handle );
1122 p_module->psz_filename = strdup( psz_file );
1123 p_module->handle = handle;
1124 p_module->b_loaded = true;
1126 /* Initialize the module: fill p_module, default config */
1127 if( module_Call( p_this, p_module ) != 0 )
1129 /* We couldn't call module_init() */
1130 free( p_module->psz_filename );
1131 module_release( p_module );
1132 module_Unload( handle );
1136 DupModule( p_module );
1138 /* Everything worked fine ! The module is ready to be added to the list. */
1139 p_module->b_builtin = false;
1144 /*****************************************************************************
1145 * DupModule: make a plugin module standalone.
1146 *****************************************************************************
1147 * This function duplicates all strings in the module, so that the dynamic
1148 * object can be unloaded. It acts recursively on submodules.
1149 *****************************************************************************/
1150 static void DupModule( module_t *p_module )
1154 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1156 *pp_shortcut = strdup( *pp_shortcut );
1159 /* We strdup() these entries so that they are still valid when the
1160 * module is unloaded. */
1161 p_module->psz_capability = strdup( p_module->psz_capability );
1162 p_module->psz_shortname = p_module->psz_shortname ?
1163 strdup( p_module->psz_shortname ) : NULL;
1164 p_module->psz_longname = strdup( p_module->psz_longname );
1165 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1168 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1172 /*****************************************************************************
1173 * UndupModule: free a duplicated module.
1174 *****************************************************************************
1175 * This function frees the allocations done in DupModule().
1176 *****************************************************************************/
1177 static void UndupModule( module_t *p_module )
1181 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1184 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1186 free( *pp_shortcut );
1189 free( p_module->psz_capability );
1190 FREENULL( p_module->psz_shortname );
1191 free( p_module->psz_longname );
1192 FREENULL( p_module->psz_help );
1195 #endif /* HAVE_DYNAMIC_PLUGINS */
1197 /*****************************************************************************
1198 * AllocateBuiltinModule: initialize a builtin module.
1199 *****************************************************************************
1200 * This function registers a builtin module and allocates a structure
1201 * for its information data. The module can then be handled by module_need
1202 * and module_unneed. It can be removed by DeleteModule.
1203 *****************************************************************************/
1204 static int AllocateBuiltinModule( vlc_object_t * p_this,
1205 int ( *pf_entry ) ( module_t * ) )
1207 module_t * p_module;
1209 /* Now that we have successfully loaded the module, we can
1210 * allocate a structure for it */
1211 p_module = vlc_module_create( p_this );
1212 if( p_module == NULL )
1215 /* Initialize the module : fill p_module->psz_object_name, etc. */
1216 if( pf_entry( p_module ) != 0 )
1218 /* With a well-written module we shouldn't have to print an
1219 * additional error message here, but just make sure. */
1220 msg_Err( p_this, "failed calling entry point in builtin module" );
1221 module_release( p_module );
1225 /* Everything worked fine ! The module is ready to be added to the list. */
1226 p_module->b_builtin = true;
1228 p_module->next = p_module_bank->head;
1229 p_module_bank->head = p_module;
1232 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1233 p_module->psz_object_name, p_module->psz_longname ); */
1238 /*****************************************************************************
1239 * DeleteModule: delete a module and its structure.
1240 *****************************************************************************
1241 * This function can only be called if the module isn't being used.
1242 *****************************************************************************/
1243 static void DeleteModule( module_bank_t *p_bank, module_t * p_module )
1247 /* Unlist the module (if it is in the list) */
1248 module_t **pp_self = &p_bank->head;
1249 while (*pp_self != NULL && *pp_self != p_module)
1250 pp_self = &((*pp_self)->next);
1252 *pp_self = p_module->next;
1254 /* We free the structures that we strdup()ed in Allocate*Module(). */
1255 #ifdef HAVE_DYNAMIC_PLUGINS
1256 if( !p_module->b_builtin )
1258 if( p_module->b_loaded && p_module->b_unloadable )
1260 module_Unload( p_module->handle );
1262 UndupModule( p_module );
1263 free( p_module->psz_filename );
1267 /* Free and detach the object's children */
1268 while (p_module->submodule)
1270 module_t *submodule = p_module->submodule;
1271 p_module->submodule = submodule->next;
1272 module_release (submodule);
1275 config_Free( p_module );
1276 module_release( p_module );