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>
35 #include <stdlib.h> /* free(), strtol() */
36 #include <stdio.h> /* sprintf() */
37 #include <string.h> /* strdup() */
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
54 #if !defined(HAVE_DYNAMIC_PLUGINS)
55 /* no support for plugins */
56 #elif defined(HAVE_DL_DYLD)
57 # if defined(HAVE_MACH_O_DYLD_H)
58 # include <mach-o/dyld.h>
60 #elif defined(HAVE_DL_BEOS)
61 # if defined(HAVE_IMAGE_H)
64 #elif defined(HAVE_DL_WINDOWS)
66 #elif defined(HAVE_DL_DLOPEN)
67 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
70 # if defined(HAVE_SYS_DL_H)
73 #elif defined(HAVE_DL_SHL_LOAD)
74 # if defined(HAVE_DL_H)
79 #include "config/configuration.h"
81 #include "vlc_charset.h"
82 #include "vlc_arrays.h"
85 #include "modules/modules.h"
87 static module_bank_t *p_module_bank = NULL;
88 static vlc_mutex_t module_lock = VLC_STATIC_MUTEX;
90 int vlc_entry__main( module_t * );
92 /*****************************************************************************
94 *****************************************************************************/
95 #ifdef HAVE_DYNAMIC_PLUGINS
96 static void AllocateAllPlugins( vlc_object_t *, module_bank_t * );
97 static void AllocatePluginDir( vlc_object_t *, module_bank_t *, const char *,
99 static int AllocatePluginFile( vlc_object_t *, module_bank_t *, const char *,
101 static module_t * AllocatePlugin( vlc_object_t *, const char * );
103 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
104 static void DeleteModule ( module_bank_t *, module_t * );
105 #ifdef HAVE_DYNAMIC_PLUGINS
106 static void DupModule ( module_t * );
107 static void UndupModule ( module_t * );
113 * Creates a module bank structure which will be filled later
114 * on with all the modules found.
115 * \param p_this vlc object structure
118 void __module_InitBank( vlc_object_t *p_this )
120 module_bank_t *p_bank = NULL;
122 vlc_mutex_lock( &module_lock );
124 if( p_module_bank == NULL )
126 p_bank = calloc (1, sizeof(*p_bank));
128 p_bank->i_cache = p_bank->i_loaded_cache = 0;
129 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
130 p_bank->b_cache = p_bank->b_cache_dirty = false;
133 /* Everything worked, attach the object */
134 p_module_bank = p_bank;
136 /* Fills the module bank structure with the main module infos.
137 * This is very useful as it will allow us to consider the main
138 * library just as another module, and for instance the configuration
139 * options of main will be available in the module bank structure just
140 * as for every other module. */
141 AllocateBuiltinModule( p_this, vlc_entry__main );
144 p_module_bank->i_usage++;
146 /* We do retain the module bank lock until the plugins are loaded as well.
147 * This is ugly, this staged loading approach is needed: LibVLC gets
148 * some configuration parameters relevant to loading the plugins from
149 * the main (builtin) module. The module bank becomes shared read-only data
150 * once it is ready, so we need to fully serialize initialization.
151 * DO NOT UNCOMMENT the following line unless you managed to squeeze
152 * module_LoadPlugins() before you unlock the mutex. */
153 /*vlc_mutex_unlock( &module_lock );*/
156 #undef module_EndBank
158 * Unloads all unused plugin modules and empties the module
159 * bank in case of success.
160 * \param p_this vlc object structure
163 void module_EndBank( vlc_object_t *p_this, bool b_plugins )
165 module_bank_t *p_bank = p_module_bank;
167 assert (p_bank != NULL);
169 /* Save the configuration */
170 if( !config_GetInt( p_this, "ignore-config" ) )
171 config_AutoSaveConfigFile( p_this );
173 /* If plugins were _not_ loaded, then the caller still has the bank lock
174 * from module_InitBank(). */
176 vlc_mutex_lock( &module_lock );
178 vlc_assert_locked( &module_lock ); not for static mutexes :( */
180 if( --p_bank->i_usage > 0 )
182 vlc_mutex_unlock( &module_lock );
185 p_module_bank = NULL;
186 vlc_mutex_unlock( &module_lock );
188 #ifdef HAVE_DYNAMIC_PLUGINS
189 if( p_bank->b_cache )
190 CacheSave( p_this, p_bank );
191 while( p_bank->i_loaded_cache-- )
193 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
195 DeleteModule( p_bank,
196 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module );
197 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
198 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
201 free( p_bank->pp_loaded_cache );
202 while( p_bank->i_cache-- )
204 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
205 free( p_bank->pp_cache[p_bank->i_cache] );
207 free( p_bank->pp_cache );
210 while( p_bank->head != NULL )
211 DeleteModule( p_bank, p_bank->head );
216 #undef module_LoadPlugins
218 * Loads module descriptions for all available plugins.
219 * Fills the module bank structure with the plugin modules.
221 * \param p_this vlc object structure
224 void module_LoadPlugins( vlc_object_t * p_this, bool b_cache_delete )
226 module_bank_t *p_bank = p_module_bank;
229 /*vlc_assert_locked( &module_lock ); not for static mutexes :( */
231 #ifdef HAVE_DYNAMIC_PLUGINS
232 if( p_bank->i_usage == 1 )
234 msg_Dbg( p_this, "checking plugin modules" );
235 p_module_bank->b_cache = config_GetInt( p_this, "plugins-cache" ) > 0;
237 if( p_module_bank->b_cache || b_cache_delete )
238 CacheLoad( p_this, p_module_bank, b_cache_delete );
239 AllocateAllPlugins( p_this, p_module_bank );
242 p_module_bank->b_plugins = true;
243 vlc_mutex_unlock( &module_lock );
247 * Checks whether a module implements a capability.
249 * \param m the module
250 * \param cap the capability to check
251 * \return TRUE if the module have the capability
253 bool module_provides( const module_t *m, const char *cap )
255 return !strcmp( m->psz_capability, cap );
259 * Get the internal name of a module
261 * \param m the module
262 * \return the module name
264 const char *module_get_object( const module_t *m )
266 return m->psz_object_name;
270 * Get the human-friendly name of a module.
272 * \param m the module
273 * \param long_name TRUE to have the long name of the module
274 * \return the short or long name of the module
276 const char *module_get_name( const module_t *m, bool long_name )
278 if( long_name && ( m->psz_longname != NULL) )
279 return m->psz_longname;
281 return m->psz_shortname ? m->psz_shortname : m->psz_object_name;
285 * Get the help for a module
287 * \param m the module
290 const char *module_get_help( const module_t *m )
296 * Get the capability for a module
298 * \param m the module
299 * return the capability
301 const char *module_get_capability( const module_t *m )
303 return m->psz_capability;
307 * Get the score for a module
309 * \param m the module
310 * return the score for the capability
312 int module_get_score( const module_t *m )
317 module_t *module_hold (module_t *m)
319 vlc_hold (&m->vlc_gc_data);
323 void module_release (module_t *m)
325 vlc_release (&m->vlc_gc_data);
329 * Frees the flat list of VLC modules.
330 * @param list list obtained by module_list_get()
331 * @param length number of items on the list
334 void module_list_free (module_t **list)
339 for (size_t i = 0; list[i] != NULL; i++)
340 module_release (list[i]);
345 * Gets the flat list of VLC modules.
346 * @param n [OUT] pointer to the number of modules or NULL
347 * @return NULL-terminated table of module pointers
348 * (release with module_list_free()), or NULL in case of error.
350 module_t **module_list_get (size_t *n)
352 /* TODO: this whole module lookup is quite inefficient */
353 /* Remove this and improve module_need */
354 module_t **tab = NULL;
357 assert (p_module_bank);
358 for (module_t *mod = p_module_bank->head; mod; mod = mod->next)
361 nt = realloc (tab, (i + 2 + mod->submodule_count) * sizeof (*tab));
364 module_list_free (tab);
369 tab[i++] = module_hold (mod);
370 for (module_t *subm = mod->submodule; subm; subm = subm->next)
371 tab[i++] = module_hold (subm);
379 typedef struct module_list_t
386 static int modulecmp (const void *a, const void *b)
388 const module_list_t *la = a, *lb = b;
389 /* Note that qsort() uses _ascending_ order,
390 * so the smallest module is the one with the biggest score. */
391 return lb->i_score - la->i_score;
397 * Return the best module function, given a capability list.
399 * \param p_this the vlc object
400 * \param psz_capability list of capabilities needed
401 * \param psz_name name of the module asked
402 * \param b_strict TRUE yto use the strict mode
403 * \return the module or NULL in case of a failure
405 module_t * __module_need( vlc_object_t *p_this, const char *psz_capability,
406 const char *psz_name, bool b_strict )
408 stats_TimerStart( p_this, "module_need()", STATS_TIMER_MODULE_NEED );
410 module_list_t *p_list;
413 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
414 bool b_force_backup = p_this->b_force;
416 /* Deal with variables */
417 if( psz_name && psz_name[0] == '$' )
419 psz_name = psz_var = var_CreateGetString( p_this, psz_name + 1 );
422 /* Count how many different shortcuts were asked for */
423 if( psz_name && *psz_name )
425 char *psz_parser, *psz_last_shortcut;
427 /* If the user wants none, give him none. */
428 if( !strcmp( psz_name, "none" ) )
431 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
432 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
433 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
438 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
440 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
442 if( *psz_parser == ',' )
446 psz_last_shortcut = psz_parser + 1;
450 /* Check if the user wants to override the "strict" mode */
451 if( psz_last_shortcut )
453 if( !strcmp(psz_last_shortcut, "none") )
458 else if( !strcmp(psz_last_shortcut, "any") )
466 /* Sort the modules and test them */
468 module_t **p_all = module_list_get (&count);
469 p_list = malloc( count * sizeof( module_list_t ) );
470 unsigned i_cpu = vlc_CPU();
472 /* Parse the module list for capabilities and probe each of them */
474 for (size_t i = 0; (p_module = p_all[i]) != NULL; i++)
476 bool b_shortcut_bonus = false;
478 /* Test that this module can do what we need */
479 if( !module_provides( p_module, psz_capability ) )
481 /* Test if we have the required CPU */
482 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
485 /* If we required a shortcut, check this plugin provides it. */
486 if( i_shortcuts > 0 )
488 const char *name = psz_shortcuts;
490 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
492 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
495 if( ( c = strchr( name, '@' ) )
496 ? !strncasecmp( name, p_module->pp_shortcuts[i],
498 : !strcasecmp( name, p_module->pp_shortcuts[i] ) )
503 b_shortcut_bonus = true;
508 /* Go to the next shortcut... This is so lame! */
509 name += strlen( name ) + 1;
512 /* If we are in "strict" mode and we couldn't
513 * find the module in the list of provided shortcuts,
514 * then kick the bastard out of here!!! */
519 /* Trash <= 0 scored plugins (they can only be selected by shortcut) */
520 if( p_module->i_score <= 0 )
524 /* Store this new module */
525 p_list[count].p_module = module_hold (p_module);
526 p_list[count].i_score = p_module->i_score;
527 if( b_shortcut_bonus )
528 p_list[count].i_score += 10000;
529 p_list[count].b_force = b_shortcut_bonus && b_strict;
533 /* We can release the list, interesting modules are held */
534 module_list_free (p_all);
536 /* Sort candidates by descending score */
537 qsort (p_list, count, sizeof (p_list[0]), modulecmp);
538 msg_Dbg( p_this, "looking for %s module: %zu candidate%s", psz_capability,
539 count, count == 1 ? "" : "s" );
541 /* Parse the linked list and use the first successful module */
543 for (size_t i = 0; (i < count) && (p_module == NULL); i++)
545 module_t *p_cand = p_list[i].p_module;
546 #ifdef HAVE_DYNAMIC_PLUGINS
547 /* Make sure the module is loaded in mem */
548 module_t *p_real = p_cand->b_submodule ? p_cand->parent : p_cand;
550 if( !p_real->b_builtin && !p_real->b_loaded )
552 module_t *p_new_module =
553 AllocatePlugin( p_this, p_real->psz_filename );
554 if( p_new_module == NULL )
555 { /* Corrupted module */
556 msg_Err( p_this, "possibly corrupt module cache" );
557 module_release( p_cand );
560 CacheMerge( p_this, p_real, p_new_module );
561 DeleteModule( p_module_bank, p_new_module );
565 p_this->b_force = p_list[i].b_force;
567 int ret = VLC_SUCCESS;
568 if( p_cand->pf_activate )
569 ret = p_cand->pf_activate( p_this );
578 /* good module, but aborted */
579 module_release( p_cand );
582 default: /* bad module */
583 module_release( p_cand );
587 /* Release the remaining modules */
589 module_release (p_list[i].p_module);
593 p_this->b_force = b_force_backup;
595 if( p_module != NULL )
597 msg_Dbg( p_this, "using %s module \"%s\"",
598 psz_capability, p_module->psz_object_name );
599 vlc_object_set_name( p_this, psz_alias ? psz_alias
600 : p_module->psz_object_name );
602 else if( count == 0 )
604 if( !strcmp( psz_capability, "access_demux" )
605 || !strcmp( psz_capability, "stream_filter" )
606 || !strcmp( psz_capability, "vout_window" ) )
608 msg_Dbg( p_this, "no %s module matched \"%s\"",
609 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
613 msg_Err( p_this, "no %s module matched \"%s\"",
614 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
616 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
617 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
620 else if( psz_name != NULL && *psz_name )
622 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
623 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
626 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
628 free( psz_shortcuts );
631 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
632 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
633 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
635 /* Don't forget that the module is still locked */
642 * This function must be called by the thread that called module_need, to
643 * decrease the reference count and allow for hiding of modules.
644 * \param p_this vlc object structure
645 * \param p_module the module structure
648 void __module_unneed( vlc_object_t * p_this, module_t * p_module )
650 /* Use the close method */
651 if( p_module->pf_deactivate )
653 p_module->pf_deactivate( p_this );
656 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
658 module_release( p_module );
662 * Get a pointer to a module_t given it's name.
664 * \param psz_name the name of the module
665 * \return a pointer to the module or NULL in case of a failure
667 module_t *module_find( const char * psz_name )
669 module_t **list, *module;
671 list = module_list_get (NULL);
675 for (size_t i = 0; (module = list[i]) != NULL; i++)
677 const char *psz_module_name = module->psz_object_name;
679 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
681 module_hold (module);
685 module_list_free (list);
690 * Tell if a module exists and release it in thic case
692 * \param psz_name th name of the module
693 * \return TRUE if the module exists
695 bool module_exists (const char * psz_name)
697 module_t *p_module = module_find (psz_name);
699 module_release (p_module);
700 return p_module != NULL;
704 * Get a pointer to a module_t that matches a shortcut.
705 * This is a temporary hack for SD. Do not re-use (generally multiple modules
706 * can have the same shortcut, so this is *broken* - use module_need()!).
708 * \param psz_shortcut shortcut of the module
709 * \param psz_cap capability of the module
710 * \return a pointer to the module or NULL in case of a failure
712 module_t *module_find_by_shortcut (const char *psz_shortcut)
714 module_t **list, *module;
716 list = module_list_get (NULL);
720 for (size_t i = 0; (module = list[i]) != NULL; i++)
723 (module->pp_shortcuts[j] != NULL) && (j < MODULE_SHORTCUT_MAX);
726 if (!strcmp (module->pp_shortcuts[j], psz_shortcut))
728 module_hold (module);
734 module_list_free (list);
739 * GetModuleNamesForCapability
741 * Return a NULL terminated array with the names of the modules
742 * that have a certain capability.
743 * Free after uses both the string and the table.
744 * \param psz_capability the capability asked
745 * \param pppsz_longname an pointer to an array of string to contain
746 the long names of the modules. If set to NULL the function don't use it.
747 * \return the NULL terminated array
749 char ** module_GetModulesNamesForCapability( const char *psz_capability,
750 char ***pppsz_longname )
755 module_t **list = module_list_get (NULL);
757 /* Proceed in two passes: count the number of modules first */
758 for (size_t i = 0; list[i]; i++)
760 module_t *p_module = list[i];
761 const char *psz_module_capability = p_module->psz_capability;
763 if( psz_module_capability
764 && !strcmp( psz_module_capability, psz_capability ) )
768 /* Then get the names */
769 psz_ret = malloc( sizeof(char*) * (count+1) );
771 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
772 if( !psz_ret || ( pppsz_longname && *pppsz_longname == NULL ) )
777 free( *pppsz_longname );
778 *pppsz_longname = NULL;
780 module_list_free (list);
784 for (size_t i = 0, j = 0; list[i]; i++)
786 module_t *p_module = list[i];
787 const char *psz_module_capability = p_module->psz_capability;
789 if( psz_module_capability
790 && !strcmp( psz_module_capability, psz_capability ) )
792 /* Explicit hack: Use the last shortcut. It _should_ be
793 * different from the object name, at least if the object
794 * contains multiple submodules with the same capability. */
796 while( p_module->pp_shortcuts[k] != NULL )
798 assert( k > 0); /* pp_shortcuts[0] is always set */
799 psz_ret[j] = strdup( p_module->pp_shortcuts[k - 1] );
801 (*pppsz_longname)[j] = strdup( module_get_name( p_module, true ) );
805 psz_ret[count] = NULL;
807 module_list_free (list);
813 * Get the configuration of a module
815 * \param module the module
816 * \param psize the size of the configuration returned
817 * \return the configuration as an array
819 module_config_t *module_config_get( const module_t *module, unsigned *restrict psize )
822 unsigned size = module->confsize;
823 module_config_t *config = malloc( size * sizeof( *config ) );
825 assert( psize != NULL );
831 for( i = 0, j = 0; i < size; i++ )
833 const module_config_t *item = module->p_config + i;
834 if( item->b_internal /* internal option */
835 || item->b_unsaveable /* non-modifiable option */
836 || item->b_removed /* removed option */ )
839 memcpy( config + j, item, sizeof( *config ) );
848 * Release the configuration
850 * \param the configuration
853 void module_config_free( module_config_t *config )
858 /*****************************************************************************
859 * Following functions are local.
860 *****************************************************************************/
862 /*****************************************************************************
863 * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
865 *****************************************************************************/
866 static char * copy_next_paths_token( char * paths, char ** remaining_paths )
870 bool escaped = false;
874 /* Alloc a buffer to store the path */
875 path = malloc( strlen( paths ) + 1 );
876 if( !path ) return NULL;
878 /* Look for PATH_SEP_CHAR (a ':' or a ';') */
879 for( i = 0, done = 0 ; paths[i]; i++ )
881 /* Take care of \\ and \: or \; escapement */
885 path[done++] = paths[i];
888 else if( paths[i] == '/' )
891 else if( paths[i] == '\\' )
894 else if( paths[i] == PATH_SEP_CHAR )
897 path[done++] = paths[i];
901 /* Return the remaining paths */
902 if( remaining_paths ) {
903 *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
909 char *psz_vlcpath = NULL;
911 /*****************************************************************************
912 * AllocateAllPlugins: load all plugin modules we can find.
913 *****************************************************************************/
914 #ifdef HAVE_DYNAMIC_PLUGINS
915 static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank )
917 const char *vlcpath = psz_vlcpath;
920 vlc_array_t *arraypaths = vlc_array_new();
922 /* Contruct the special search path for system that have a relocatable
923 * executable. Set it to <vlc path>/modules and <vlc path>/plugins. */
925 if( vlcpath && asprintf( &path, "%s" DIR_SEP "modules", vlcpath ) != -1 )
926 vlc_array_append( arraypaths, path );
927 if( vlcpath && asprintf( &path, "%s" DIR_SEP "plugins", vlcpath ) != -1 )
928 vlc_array_append( arraypaths, path );
930 vlc_array_append( arraypaths, strdup( PLUGIN_PATH ) );
933 /* If the user provided a plugin path, we add it to the list */
934 char *userpaths = config_GetPsz( p_this, "plugin-path" );
937 for( paths_iter = userpaths; paths_iter; )
939 path = copy_next_paths_token( paths_iter, &paths_iter );
941 vlc_array_append( arraypaths, path );
944 count = vlc_array_count( arraypaths );
945 for( i = 0 ; i < count ; i++ )
947 path = vlc_array_item_at_index( arraypaths, i );
951 msg_Dbg( p_this, "recursively browsing `%s'", path );
953 /* Don't go deeper than 5 subdirectories */
954 AllocatePluginDir( p_this, p_bank, path, 5 );
959 vlc_array_destroy( arraypaths );
963 /*****************************************************************************
964 * AllocatePluginDir: recursively parse a directory to look for plugins
965 *****************************************************************************/
966 static void AllocatePluginDir( vlc_object_t *p_this, module_bank_t *p_bank,
967 const char *psz_dir, unsigned i_maxdepth )
969 if( i_maxdepth == 0 )
972 DIR *dh = utf8_opendir (psz_dir);
976 /* Parse the directory and try to load all files it contains. */
979 char *file = utf8_readdir (dh), *path;
986 if (!strcmp (file, ".") || !strcmp (file, ".."))
992 const int pathlen = asprintf (&path, "%s"DIR_SEP"%s", psz_dir, file);
994 if (pathlen == -1 || utf8_stat (path, &st))
997 if (S_ISDIR (st.st_mode))
998 /* Recurse into another directory */
999 AllocatePluginDir (p_this, p_bank, path, i_maxdepth - 1);
1001 if (S_ISREG (st.st_mode)
1002 && ((size_t)pathlen >= strlen (LIBEXT))
1003 && !strncasecmp (path + pathlen - strlen (LIBEXT), LIBEXT,
1005 /* ^^ We only load files ending with LIBEXT */
1006 AllocatePluginFile (p_this, p_bank, path, st.st_mtime, st.st_size);
1013 /*****************************************************************************
1014 * AllocatePluginFile: load a module into memory and initialize it.
1015 *****************************************************************************
1016 * This function loads a dynamically loadable module and allocates a structure
1017 * for its information data. The module can then be handled by module_need
1018 * and module_unneed. It can be removed by DeleteModule.
1019 *****************************************************************************/
1020 static int AllocatePluginFile( vlc_object_t * p_this, module_bank_t *p_bank,
1021 const char *psz_file,
1022 int64_t i_file_time, int64_t i_file_size )
1024 module_t * p_module = NULL;
1025 module_cache_t *p_cache_entry = NULL;
1028 * Check our plugins cache first then load plugin if needed
1030 p_cache_entry = CacheFind( p_bank, psz_file, i_file_time, i_file_size );
1031 if( !p_cache_entry )
1033 p_module = AllocatePlugin( p_this, psz_file );
1036 /* If junk dll, don't try to load it */
1037 if( p_cache_entry->b_junk )
1041 module_config_t *p_item = NULL, *p_end = NULL;
1043 p_module = p_cache_entry->p_module;
1044 p_module->b_loaded = false;
1046 /* For now we force loading if the module's config contains
1047 * callbacks or actions.
1048 * Could be optimized by adding an API call.*/
1049 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1050 p_item < p_end; p_item++ )
1052 if( p_item->pf_callback || p_item->i_action )
1054 p_module = AllocatePlugin( p_this, psz_file );
1058 if( p_module == p_cache_entry->p_module )
1059 p_cache_entry->b_used = true;
1062 if( p_module == NULL )
1065 /* Everything worked fine !
1066 * The module is ready to be added to the list. */
1067 p_module->b_builtin = false;
1069 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1070 p_module->psz_object_name, p_module->psz_longname ); */
1071 p_module->next = p_bank->head;
1072 p_bank->head = p_module;
1074 if( !p_module_bank->b_cache )
1077 /* Add entry to cache */
1078 module_cache_t **pp_cache = p_bank->pp_cache;
1080 pp_cache = realloc( pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1081 if( pp_cache == NULL )
1083 pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1084 if( pp_cache[p_bank->i_cache] == NULL )
1086 pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1087 pp_cache[p_bank->i_cache]->i_time = i_file_time;
1088 pp_cache[p_bank->i_cache]->i_size = i_file_size;
1089 pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1090 pp_cache[p_bank->i_cache]->b_used = true;
1091 pp_cache[p_bank->i_cache]->p_module = p_module;
1092 p_bank->pp_cache = pp_cache;
1097 /*****************************************************************************
1098 * AllocatePlugin: load a module into memory and initialize it.
1099 *****************************************************************************
1100 * This function loads a dynamically loadable module and allocates a structure
1101 * for its information data. The module can then be handled by module_need
1102 * and module_unneed. It can be removed by DeleteModule.
1103 *****************************************************************************/
1104 static module_t * AllocatePlugin( vlc_object_t * p_this, const char *psz_file )
1106 module_t * p_module = NULL;
1107 module_handle_t handle;
1109 if( module_Load( p_this, psz_file, &handle ) )
1112 /* Now that we have successfully loaded the module, we can
1113 * allocate a structure for it */
1114 p_module = vlc_module_create( p_this );
1115 if( p_module == NULL )
1117 module_Unload( handle );
1121 p_module->psz_filename = strdup( psz_file );
1122 p_module->handle = handle;
1123 p_module->b_loaded = true;
1125 /* Initialize the module: fill p_module, default config */
1126 if( module_Call( p_this, p_module ) != 0 )
1128 /* We couldn't call module_init() */
1129 free( p_module->psz_filename );
1130 module_release( p_module );
1131 module_Unload( handle );
1135 DupModule( p_module );
1137 /* Everything worked fine ! The module is ready to be added to the list. */
1138 p_module->b_builtin = false;
1143 /*****************************************************************************
1144 * DupModule: make a plugin module standalone.
1145 *****************************************************************************
1146 * This function duplicates all strings in the module, so that the dynamic
1147 * object can be unloaded. It acts recursively on submodules.
1148 *****************************************************************************/
1149 static void DupModule( module_t *p_module )
1153 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1155 *pp_shortcut = strdup( *pp_shortcut );
1158 /* We strdup() these entries so that they are still valid when the
1159 * module is unloaded. */
1160 p_module->psz_capability = strdup( p_module->psz_capability );
1161 p_module->psz_shortname = p_module->psz_shortname ?
1162 strdup( p_module->psz_shortname ) : NULL;
1163 p_module->psz_longname = strdup( p_module->psz_longname );
1164 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1167 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1171 /*****************************************************************************
1172 * UndupModule: free a duplicated module.
1173 *****************************************************************************
1174 * This function frees the allocations done in DupModule().
1175 *****************************************************************************/
1176 static void UndupModule( module_t *p_module )
1180 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1183 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1185 free( *pp_shortcut );
1188 free( p_module->psz_capability );
1189 FREENULL( p_module->psz_shortname );
1190 free( p_module->psz_longname );
1191 FREENULL( p_module->psz_help );
1194 #endif /* HAVE_DYNAMIC_PLUGINS */
1196 /*****************************************************************************
1197 * AllocateBuiltinModule: initialize a builtin module.
1198 *****************************************************************************
1199 * This function registers a builtin module and allocates a structure
1200 * for its information data. The module can then be handled by module_need
1201 * and module_unneed. It can be removed by DeleteModule.
1202 *****************************************************************************/
1203 static int AllocateBuiltinModule( vlc_object_t * p_this,
1204 int ( *pf_entry ) ( module_t * ) )
1206 module_t * p_module;
1208 /* Now that we have successfully loaded the module, we can
1209 * allocate a structure for it */
1210 p_module = vlc_module_create( p_this );
1211 if( p_module == NULL )
1214 /* Initialize the module : fill p_module->psz_object_name, etc. */
1215 if( pf_entry( p_module ) != 0 )
1217 /* With a well-written module we shouldn't have to print an
1218 * additional error message here, but just make sure. */
1219 msg_Err( p_this, "failed calling entry point in builtin module" );
1220 module_release( p_module );
1224 /* Everything worked fine ! The module is ready to be added to the list. */
1225 p_module->b_builtin = true;
1227 p_module->next = p_module_bank->head;
1228 p_module_bank->head = p_module;
1231 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1232 p_module->psz_object_name, p_module->psz_longname ); */
1237 /*****************************************************************************
1238 * DeleteModule: delete a module and its structure.
1239 *****************************************************************************
1240 * This function can only be called if the module isn't being used.
1241 *****************************************************************************/
1242 static void DeleteModule( module_bank_t *p_bank, module_t * p_module )
1246 /* Unlist the module (if it is in the list) */
1247 module_t **pp_self = &p_bank->head;
1248 while (*pp_self != NULL && *pp_self != p_module)
1249 pp_self = &((*pp_self)->next);
1251 *pp_self = p_module->next;
1253 /* We free the structures that we strdup()ed in Allocate*Module(). */
1254 #ifdef HAVE_DYNAMIC_PLUGINS
1255 if( !p_module->b_builtin )
1257 if( p_module->b_loaded && p_module->b_unloadable )
1259 module_Unload( p_module->handle );
1261 UndupModule( p_module );
1262 free( p_module->psz_filename );
1266 /* Free and detach the object's children */
1267 while (p_module->submodule)
1269 module_t *submodule = p_module->submodule;
1270 p_module->submodule = submodule->next;
1271 module_release (submodule);
1274 config_Free( p_module );
1275 module_release( p_module );