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"
84 #include "modules/modules.h"
86 static module_bank_t *p_module_bank = NULL;
87 static vlc_mutex_t module_lock = VLC_STATIC_MUTEX;
89 int vlc_entry__main( module_t * );
91 /*****************************************************************************
93 *****************************************************************************/
94 #ifdef HAVE_DYNAMIC_PLUGINS
95 static void AllocateAllPlugins( vlc_object_t *, module_bank_t * );
96 static void AllocatePluginDir( vlc_object_t *, module_bank_t *, const char *,
98 static int AllocatePluginFile( vlc_object_t *, module_bank_t *, const char *,
100 static module_t * AllocatePlugin( vlc_object_t *, const char * );
102 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
103 static void DeleteModule ( module_bank_t *, module_t * );
104 #ifdef HAVE_DYNAMIC_PLUGINS
105 static void DupModule ( module_t * );
106 static void UndupModule ( module_t * );
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
117 void __module_InitBank( vlc_object_t *p_this )
119 module_bank_t *p_bank = NULL;
121 vlc_mutex_lock( &module_lock );
123 if( p_module_bank == NULL )
125 p_bank = calloc (1, sizeof(*p_bank));
127 p_bank->i_cache = p_bank->i_loaded_cache = 0;
128 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
129 p_bank->b_cache = p_bank->b_cache_dirty = false;
132 /* Everything worked, attach the object */
133 p_module_bank = p_bank;
135 /* Fills the module bank structure with the main module infos.
136 * This is very useful as it will allow us to consider the main
137 * library just as another module, and for instance the configuration
138 * options of main will be available in the module bank structure just
139 * as for every other module. */
140 AllocateBuiltinModule( p_this, vlc_entry__main );
143 p_module_bank->i_usage++;
145 /* We do retain the module bank lock until the plugins are loaded as well.
146 * This is ugly, this staged loading approach is needed: LibVLC gets
147 * some configuration parameters relevant to loading the plugins from
148 * the main (builtin) module. The module bank becomes shared read-only data
149 * once it is ready, so we need to fully serialize initialization.
150 * DO NOT UNCOMMENT the following line unless you managed to squeeze
151 * module_LoadPlugins() before you unlock the mutex. */
152 /*vlc_mutex_unlock( &module_lock );*/
155 #undef module_EndBank
157 * Unloads all unused plugin modules and empties the module
158 * bank in case of success.
159 * \param p_this vlc object structure
162 void module_EndBank( vlc_object_t *p_this, bool b_plugins )
164 module_bank_t *p_bank = p_module_bank;
166 assert (p_bank != NULL);
168 /* Save the configuration */
169 if( !config_GetInt( p_this, "ignore-config" ) )
170 config_AutoSaveConfigFile( p_this );
172 /* If plugins were _not_ loaded, then the caller still has the bank lock
173 * from module_InitBank(). */
175 vlc_mutex_lock( &module_lock );
177 vlc_assert_locked( &module_lock ); not for static mutexes :( */
179 if( --p_bank->i_usage > 0 )
181 vlc_mutex_unlock( &module_lock );
184 p_module_bank = NULL;
185 vlc_mutex_unlock( &module_lock );
187 #ifdef HAVE_DYNAMIC_PLUGINS
188 if( p_bank->b_cache )
189 CacheSave( p_this, p_bank );
190 while( p_bank->i_loaded_cache-- )
192 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
194 DeleteModule( p_bank,
195 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module );
196 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
197 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
198 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
201 if( p_bank->pp_loaded_cache )
203 free( p_bank->pp_loaded_cache );
204 p_bank->pp_loaded_cache = NULL;
206 while( p_bank->i_cache-- )
208 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
209 free( p_bank->pp_cache[p_bank->i_cache] );
210 p_bank->pp_cache[p_bank->i_cache] = NULL;
212 if( p_bank->pp_cache )
214 free( p_bank->pp_cache );
215 p_bank->pp_cache = NULL;
219 while( p_bank->head != NULL )
220 DeleteModule( p_bank, p_bank->head );
225 #undef module_LoadPlugins
227 * Loads module descriptions for all available plugins.
228 * Fills the module bank structure with the plugin modules.
230 * \param p_this vlc object structure
233 void module_LoadPlugins( vlc_object_t * p_this, bool b_cache_delete )
235 module_bank_t *p_bank = p_module_bank;
238 /*vlc_assert_locked( &module_lock ); not for static mutexes :( */
240 #ifdef HAVE_DYNAMIC_PLUGINS
241 if( p_bank->i_usage == 1 )
243 msg_Dbg( p_this, "checking plugin modules" );
244 p_module_bank->b_cache = config_GetInt( p_this, "plugins-cache" ) > 0;
246 if( p_module_bank->b_cache || b_cache_delete )
247 CacheLoad( p_this, p_module_bank, b_cache_delete );
248 AllocateAllPlugins( p_this, p_module_bank );
251 p_module_bank->b_plugins = true;
252 vlc_mutex_unlock( &module_lock );
256 * Checks whether a module implements a capability.
258 * \param m the module
259 * \param cap the capability to check
260 * \return TRUE if the module have the capability
262 bool module_provides( const module_t *m, const char *cap )
264 return !strcmp( m->psz_capability, cap );
268 * Get the internal name of a module
270 * \param m the module
271 * \return the module name
273 const char *module_get_object( const module_t *m )
275 return m->psz_object_name;
279 * Get the human-friendly name of a module.
281 * \param m the module
282 * \param long_name TRUE to have the long name of the module
283 * \return the short or long name of the module
285 const char *module_get_name( const module_t *m, bool long_name )
287 if( long_name && ( m->psz_longname != NULL) )
288 return m->psz_longname;
290 return m->psz_shortname ? m->psz_shortname : m->psz_object_name;
294 * Get the help for a module
296 * \param m the module
299 const char *module_get_help( const module_t *m )
305 * Get the capability for a module
307 * \param m the module
308 * return the capability
310 const char *module_get_capability( const module_t *m )
312 return m->psz_capability;
316 * Get the score for a module
318 * \param m the module
319 * return the score for the capability
321 int module_get_score( const module_t *m )
326 module_t *module_hold (module_t *m)
328 vlc_hold (&m->vlc_gc_data);
332 void module_release (module_t *m)
334 vlc_release (&m->vlc_gc_data);
338 * Frees the flat list of VLC modules.
339 * @param list list obtained by module_list_get()
340 * @param length number of items on the list
343 void module_list_free (module_t **list)
348 for (size_t i = 0; list[i] != NULL; i++)
349 module_release (list[i]);
354 * Gets the flat list of VLC modules.
355 * @param n [OUT] pointer to the number of modules or NULL
356 * @return NULL-terminated table of module pointers
357 * (release with module_list_free()), or NULL in case of error.
359 module_t **module_list_get (size_t *n)
361 /* TODO: this whole module lookup is quite inefficient */
362 /* Remove this and improve module_need */
363 module_t **tab = NULL;
366 assert (p_module_bank);
367 for (module_t *mod = p_module_bank->head; mod; mod = mod->next)
370 nt = realloc (tab, (i + 2 + mod->submodule_count) * sizeof (*tab));
373 module_list_free (tab);
378 tab[i++] = module_hold (mod);
379 for (module_t *subm = mod->submodule; subm; subm = subm->next)
380 tab[i++] = module_hold (subm);
388 typedef struct module_list_t
395 static int modulecmp (const void *a, const void *b)
397 const module_list_t *la = a, *lb = b;
398 /* Note that qsort() uses _ascending_ order,
399 * so the smallest module is the one with the biggest score. */
400 return lb->i_score - la->i_score;
406 * Return the best module function, given a capability list.
408 * If the p_this object doesn't have it's psz_object_name set, then
409 * psz_object_name will be set to the module's name, unless the user
410 * provided an alias using the "module name@alias" syntax in which case
411 * psz_object_name will be set to the alias.
413 * \param p_this the vlc object
414 * \param psz_capability list of capabilities needed
415 * \param psz_name name of the module asked
416 * \param b_strict TRUE yto use the strict mode
417 * \return the module or NULL in case of a failure
419 module_t * __module_need( vlc_object_t *p_this, const char *psz_capability,
420 const char *psz_name, bool b_strict )
422 stats_TimerStart( p_this, "module_need()", STATS_TIMER_MODULE_NEED );
424 module_list_t *p_list;
427 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
428 bool b_force_backup = p_this->b_force;
430 /* Deal with variables */
431 if( psz_name && psz_name[0] == '$' )
433 psz_name = psz_var = var_CreateGetString( p_this, psz_name + 1 );
436 /* Count how many different shortcuts were asked for */
437 if( psz_name && *psz_name )
439 char *psz_parser, *psz_last_shortcut;
441 /* If the user wants none, give him none. */
442 if( !strcmp( psz_name, "none" ) )
445 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
446 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
447 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
452 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
454 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
456 if( *psz_parser == ',' )
460 psz_last_shortcut = psz_parser + 1;
464 /* Check if the user wants to override the "strict" mode */
465 if( psz_last_shortcut )
467 if( !strcmp(psz_last_shortcut, "none") )
472 else if( !strcmp(psz_last_shortcut, "any") )
480 /* Sort the modules and test them */
482 module_t **p_all = module_list_get (&count);
483 p_list = malloc( count * sizeof( module_list_t ) );
484 unsigned i_cpu = vlc_CPU();
486 /* Parse the module list for capabilities and probe each of them */
488 for (size_t i = 0; (p_module = p_all[i]) != NULL; i++)
490 bool b_shortcut_bonus = false;
492 /* Test that this module can do what we need */
493 if( !module_provides( p_module, psz_capability ) )
495 /* Test if we have the required CPU */
496 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
499 /* If we required a shortcut, check this plugin provides it. */
500 if( i_shortcuts > 0 )
502 const char *name = psz_shortcuts;
504 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
506 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
509 if( ( c = strchr( name, '@' ) )
510 ? !strncasecmp( name, p_module->pp_shortcuts[i],
512 : !strcasecmp( name, p_module->pp_shortcuts[i] ) )
517 b_shortcut_bonus = true;
522 /* Go to the next shortcut... This is so lame! */
523 name += strlen( name ) + 1;
526 /* If we are in "strict" mode and we couldn't
527 * find the module in the list of provided shortcuts,
528 * then kick the bastard out of here!!! */
533 /* Trash <= 0 scored plugins (they can only be selected by shortcut) */
534 if( p_module->i_score <= 0 )
538 /* Store this new module */
539 p_list[count].p_module = module_hold (p_module);
540 p_list[count].i_score = p_module->i_score;
541 if( b_shortcut_bonus )
542 p_list[count].i_score += 10000;
543 p_list[count].b_force = b_shortcut_bonus && b_strict;
547 /* We can release the list, interesting modules are held */
548 module_list_free (p_all);
550 /* Sort candidates by descending score */
551 qsort (p_list, count, sizeof (p_list[0]), modulecmp);
552 msg_Dbg( p_this, "looking for %s module: %zu candidate%s", psz_capability,
553 count, count == 1 ? "" : "s" );
555 /* Parse the linked list and use the first successful module */
557 for (size_t i = 0; (i < count) && (p_module == NULL); i++)
559 module_t *p_cand = p_list[i].p_module;
560 #ifdef HAVE_DYNAMIC_PLUGINS
561 /* Make sure the module is loaded in mem */
562 module_t *p_real = p_cand->b_submodule ? p_cand->parent : p_cand;
564 if( !p_real->b_builtin && !p_real->b_loaded )
566 module_t *p_new_module =
567 AllocatePlugin( p_this, p_real->psz_filename );
570 CacheMerge( p_this, p_real, p_new_module );
571 DeleteModule( p_module_bank, p_new_module );
576 p_this->b_force = p_list[i].b_force;
578 int ret = VLC_SUCCESS;
579 if( p_cand->pf_activate )
580 ret = p_cand->pf_activate( p_this );
589 /* good module, but aborted */
590 module_release( p_cand );
593 default: /* bad module */
594 module_release( p_cand );
598 /* Release the remaining modules */
600 module_release (p_list[i].p_module);
604 p_this->b_force = b_force_backup;
606 if( p_module != NULL )
608 msg_Dbg( p_this, "using %s module \"%s\"",
609 psz_capability, p_module->psz_object_name );
610 if( !vlc_internals(p_this)->psz_object_name )
612 /* This assumes that p_this is the object which will be using the
613 * module. That's not always the case ... but it is in most cases.
615 vlc_internals(p_this)->psz_object_name =
616 strdup( psz_alias ? psz_alias : p_module->psz_object_name );
619 else if( count == 0 )
621 if( !strcmp( psz_capability, "access_demux" )
622 || !strcmp( psz_capability, "stream_filter" )
623 || !strcmp( psz_capability, "vout_window" ) )
625 msg_Dbg( p_this, "no %s module matched \"%s\"",
626 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
630 msg_Err( p_this, "no %s module matched \"%s\"",
631 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
633 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
634 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
637 else if( psz_name != NULL && *psz_name )
639 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
640 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
643 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
645 free( psz_shortcuts );
648 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
649 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
650 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
652 /* Don't forget that the module is still locked */
659 * This function must be called by the thread that called module_need, to
660 * decrease the reference count and allow for hiding of modules.
661 * \param p_this vlc object structure
662 * \param p_module the module structure
665 void __module_unneed( vlc_object_t * p_this, module_t * p_module )
667 /* Use the close method */
668 if( p_module->pf_deactivate )
670 p_module->pf_deactivate( p_this );
673 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
675 module_release( p_module );
679 * Get a pointer to a module_t given it's name.
681 * \param psz_name the name of the module
682 * \return a pointer to the module or NULL in case of a failure
684 module_t *module_find( const char * psz_name )
686 module_t **list, *module;
688 list = module_list_get (NULL);
692 for (size_t i = 0; (module = list[i]) != NULL; i++)
694 const char *psz_module_name = module->psz_object_name;
696 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
698 module_hold (module);
702 module_list_free (list);
707 * Tell if a module exists and release it in thic case
709 * \param psz_name th name of the module
710 * \return TRUE if the module exists
712 bool module_exists (const char * psz_name)
714 module_t *p_module = module_find (psz_name);
716 module_release (p_module);
717 return p_module != NULL;
721 * Get a pointer to a module_t that matches a shortcut.
722 * This is a temporary hack for SD. Do not re-use (generally multiple modules
723 * can have the same shortcut, so this is *broken* - use module_need()!).
725 * \param psz_shortcut shortcut of the module
726 * \param psz_cap capability of the module
727 * \return a pointer to the module or NULL in case of a failure
729 module_t *module_find_by_shortcut (const char *psz_shortcut)
731 module_t **list, *module;
733 list = module_list_get (NULL);
737 for (size_t i = 0; (module = list[i]) != NULL; i++)
740 (module->pp_shortcuts[j] != NULL) && (j < MODULE_SHORTCUT_MAX);
743 if (!strcmp (module->pp_shortcuts[j], psz_shortcut))
745 module_hold (module);
751 module_list_free (list);
756 * GetModuleNamesForCapability
758 * Return a NULL terminated array with the names of the modules
759 * that have a certain capability.
760 * Free after uses both the string and the table.
761 * \param psz_capability the capability asked
762 * \param pppsz_longname an pointer to an array of string to contain
763 the long names of the modules. If set to NULL the function don't use it.
764 * \return the NULL terminated array
766 char ** module_GetModulesNamesForCapability( const char *psz_capability,
767 char ***pppsz_longname )
772 module_t **list = module_list_get (NULL);
774 /* Proceed in two passes: count the number of modules first */
775 for (size_t i = 0; list[i]; i++)
777 module_t *p_module = list[i];
778 const char *psz_module_capability = p_module->psz_capability;
780 if( psz_module_capability
781 && !strcmp( psz_module_capability, psz_capability ) )
785 /* Then get the names */
786 psz_ret = malloc( sizeof(char*) * (count+1) );
788 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
789 if( !psz_ret || ( pppsz_longname && *pppsz_longname == NULL ) )
794 free( *pppsz_longname );
795 *pppsz_longname = NULL;
797 module_list_free (list);
801 for (size_t i = 0, j = 0; list[i]; i++)
803 module_t *p_module = list[i];
804 const char *psz_module_capability = p_module->psz_capability;
806 if( psz_module_capability
807 && !strcmp( psz_module_capability, psz_capability ) )
809 /* Explicit hack: Use the last shortcut. It _should_ be
810 * different from the object name, at least if the object
811 * contains multiple submodules with the same capability. */
813 while( p_module->pp_shortcuts[k] != NULL )
815 assert( k > 0); /* pp_shortcuts[0] is always set */
816 psz_ret[j] = strdup( p_module->pp_shortcuts[k - 1] );
818 (*pppsz_longname)[j] = strdup( module_get_name( p_module, true ) );
822 psz_ret[count] = NULL;
824 module_list_free (list);
830 * Get the configuration of a module
832 * \param module the module
833 * \param psize the size of the configuration returned
834 * \return the configuration as an array
836 module_config_t *module_config_get( const module_t *module, unsigned *restrict psize )
839 unsigned size = module->confsize;
840 module_config_t *config = malloc( size * sizeof( *config ) );
842 assert( psize != NULL );
848 for( i = 0, j = 0; i < size; i++ )
850 const module_config_t *item = module->p_config + i;
851 if( item->b_internal /* internal option */
852 || item->b_unsaveable /* non-modifiable option */
853 || item->b_removed /* removed option */ )
856 memcpy( config + j, item, sizeof( *config ) );
865 * Release the configuration
867 * \param the configuration
870 void module_config_free( module_config_t *config )
875 /*****************************************************************************
876 * Following functions are local.
877 *****************************************************************************/
879 /*****************************************************************************
880 * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
882 *****************************************************************************/
883 static char * copy_next_paths_token( char * paths, char ** remaining_paths )
887 bool escaped = false;
891 /* Alloc a buffer to store the path */
892 path = malloc( strlen( paths ) + 1 );
893 if( !path ) return NULL;
895 /* Look for PATH_SEP_CHAR (a ':' or a ';') */
896 for( i = 0, done = 0 ; paths[i]; i++ )
898 /* Take care of \\ and \: or \; escapement */
902 path[done++] = paths[i];
905 else if( paths[i] == '/' )
908 else if( paths[i] == '\\' )
911 else if( paths[i] == PATH_SEP_CHAR )
914 path[done++] = paths[i];
918 /* Return the remaining paths */
919 if( remaining_paths ) {
920 *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
926 char *psz_vlcpath = NULL;
928 /*****************************************************************************
929 * AllocateAllPlugins: load all plugin modules we can find.
930 *****************************************************************************/
931 #ifdef HAVE_DYNAMIC_PLUGINS
932 static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank )
934 const char *vlcpath = psz_vlcpath;
937 vlc_array_t *arraypaths = vlc_array_new();
939 /* Contruct the special search path for system that have a relocatable
940 * executable. Set it to <vlc path>/modules and <vlc path>/plugins. */
942 if( vlcpath && asprintf( &path, "%s" DIR_SEP "modules", vlcpath ) != -1 )
943 vlc_array_append( arraypaths, path );
944 if( vlcpath && asprintf( &path, "%s" DIR_SEP "plugins", vlcpath ) != -1 )
945 vlc_array_append( arraypaths, path );
947 vlc_array_append( arraypaths, strdup( PLUGIN_PATH ) );
950 /* If the user provided a plugin path, we add it to the list */
951 char *userpaths = config_GetPsz( p_this, "plugin-path" );
954 for( paths_iter = userpaths; paths_iter; )
956 path = copy_next_paths_token( paths_iter, &paths_iter );
958 vlc_array_append( arraypaths, path );
961 count = vlc_array_count( arraypaths );
962 for( i = 0 ; i < count ; i++ )
964 path = vlc_array_item_at_index( arraypaths, i );
968 msg_Dbg( p_this, "recursively browsing `%s'", path );
970 /* Don't go deeper than 5 subdirectories */
971 AllocatePluginDir( p_this, p_bank, path, 5 );
976 vlc_array_destroy( arraypaths );
980 /*****************************************************************************
981 * AllocatePluginDir: recursively parse a directory to look for plugins
982 *****************************************************************************/
983 static void AllocatePluginDir( vlc_object_t *p_this, module_bank_t *p_bank,
984 const char *psz_dir, unsigned i_maxdepth )
986 if( i_maxdepth == 0 )
989 DIR *dh = utf8_opendir (psz_dir);
993 /* Parse the directory and try to load all files it contains. */
996 char *file = utf8_readdir (dh), *path;
1002 /* Skip ".", ".." */
1003 if (!strcmp (file, ".") || !strcmp (file, ".."))
1009 const int pathlen = asprintf (&path, "%s"DIR_SEP"%s", psz_dir, file);
1011 if (pathlen == -1 || utf8_stat (path, &st))
1014 if (S_ISDIR (st.st_mode))
1015 /* Recurse into another directory */
1016 AllocatePluginDir (p_this, p_bank, path, i_maxdepth - 1);
1018 if (S_ISREG (st.st_mode)
1019 && ((size_t)pathlen >= strlen (LIBEXT))
1020 && !strncasecmp (path + pathlen - strlen (LIBEXT), LIBEXT,
1022 /* ^^ We only load files ending with LIBEXT */
1023 AllocatePluginFile (p_this, p_bank, path, st.st_mtime, st.st_size);
1030 /*****************************************************************************
1031 * AllocatePluginFile: load a module into memory and initialize it.
1032 *****************************************************************************
1033 * This function loads a dynamically loadable module and allocates a structure
1034 * for its information data. The module can then be handled by module_need
1035 * and module_unneed. It can be removed by DeleteModule.
1036 *****************************************************************************/
1037 static int AllocatePluginFile( vlc_object_t * p_this, module_bank_t *p_bank,
1038 const char *psz_file,
1039 int64_t i_file_time, int64_t i_file_size )
1041 module_t * p_module = NULL;
1042 module_cache_t *p_cache_entry = NULL;
1045 * Check our plugins cache first then load plugin if needed
1047 p_cache_entry = CacheFind( p_bank, psz_file, i_file_time, i_file_size );
1048 if( !p_cache_entry )
1050 p_module = AllocatePlugin( p_this, psz_file );
1053 /* If junk dll, don't try to load it */
1054 if( p_cache_entry->b_junk )
1058 module_config_t *p_item = NULL, *p_end = NULL;
1060 p_module = p_cache_entry->p_module;
1061 p_module->b_loaded = false;
1063 /* For now we force loading if the module's config contains
1064 * callbacks or actions.
1065 * Could be optimized by adding an API call.*/
1066 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1067 p_item < p_end; p_item++ )
1069 if( p_item->pf_callback || p_item->i_action )
1071 p_module = AllocatePlugin( p_this, psz_file );
1075 if( p_module == p_cache_entry->p_module )
1076 p_cache_entry->b_used = true;
1079 if( p_module == NULL )
1082 /* Everything worked fine !
1083 * The module is ready to be added to the list. */
1084 p_module->b_builtin = false;
1086 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1087 p_module->psz_object_name, p_module->psz_longname ); */
1088 p_module->next = p_bank->head;
1089 p_bank->head = p_module;
1091 if( !p_module_bank->b_cache )
1094 /* Add entry to cache */
1095 module_cache_t **pp_cache = p_bank->pp_cache;
1097 pp_cache = realloc( pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1098 if( pp_cache == NULL )
1100 pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1101 if( pp_cache[p_bank->i_cache] == NULL )
1103 pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1104 pp_cache[p_bank->i_cache]->i_time = i_file_time;
1105 pp_cache[p_bank->i_cache]->i_size = i_file_size;
1106 pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1107 pp_cache[p_bank->i_cache]->b_used = true;
1108 pp_cache[p_bank->i_cache]->p_module = p_module;
1109 p_bank->pp_cache = pp_cache;
1114 /*****************************************************************************
1115 * AllocatePlugin: load a module into memory and initialize it.
1116 *****************************************************************************
1117 * This function loads a dynamically loadable module and allocates a structure
1118 * for its information data. The module can then be handled by module_need
1119 * and module_unneed. It can be removed by DeleteModule.
1120 *****************************************************************************/
1121 static module_t * AllocatePlugin( vlc_object_t * p_this, const char *psz_file )
1123 module_t * p_module = NULL;
1124 module_handle_t handle;
1126 if( module_Load( p_this, psz_file, &handle ) )
1129 /* Now that we have successfully loaded the module, we can
1130 * allocate a structure for it */
1131 p_module = vlc_module_create( p_this );
1132 if( p_module == NULL )
1134 module_Unload( handle );
1138 p_module->psz_filename = strdup( psz_file );
1139 p_module->handle = handle;
1140 p_module->b_loaded = true;
1142 /* Initialize the module: fill p_module, default config */
1143 if( module_Call( p_this, p_module ) != 0 )
1145 /* We couldn't call module_init() */
1146 free( p_module->psz_filename );
1147 module_release( p_module );
1148 module_Unload( handle );
1152 DupModule( p_module );
1154 /* Everything worked fine ! The module is ready to be added to the list. */
1155 p_module->b_builtin = false;
1160 /*****************************************************************************
1161 * DupModule: make a plugin module standalone.
1162 *****************************************************************************
1163 * This function duplicates all strings in the module, so that the dynamic
1164 * object can be unloaded. It acts recursively on submodules.
1165 *****************************************************************************/
1166 static void DupModule( module_t *p_module )
1170 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1172 *pp_shortcut = strdup( *pp_shortcut );
1175 /* We strdup() these entries so that they are still valid when the
1176 * module is unloaded. */
1177 p_module->psz_capability = strdup( p_module->psz_capability );
1178 p_module->psz_shortname = p_module->psz_shortname ?
1179 strdup( p_module->psz_shortname ) : NULL;
1180 p_module->psz_longname = strdup( p_module->psz_longname );
1181 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1184 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1188 /*****************************************************************************
1189 * UndupModule: free a duplicated module.
1190 *****************************************************************************
1191 * This function frees the allocations done in DupModule().
1192 *****************************************************************************/
1193 static void UndupModule( module_t *p_module )
1197 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1200 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1202 free( *pp_shortcut );
1205 free( p_module->psz_capability );
1206 FREENULL( p_module->psz_shortname );
1207 free( p_module->psz_longname );
1208 FREENULL( p_module->psz_help );
1211 #endif /* HAVE_DYNAMIC_PLUGINS */
1213 /*****************************************************************************
1214 * AllocateBuiltinModule: initialize a builtin module.
1215 *****************************************************************************
1216 * This function registers a builtin module and allocates a structure
1217 * for its information data. The module can then be handled by module_need
1218 * and module_unneed. It can be removed by DeleteModule.
1219 *****************************************************************************/
1220 static int AllocateBuiltinModule( vlc_object_t * p_this,
1221 int ( *pf_entry ) ( module_t * ) )
1223 module_t * p_module;
1225 /* Now that we have successfully loaded the module, we can
1226 * allocate a structure for it */
1227 p_module = vlc_module_create( p_this );
1228 if( p_module == NULL )
1231 /* Initialize the module : fill p_module->psz_object_name, etc. */
1232 if( pf_entry( p_module ) != 0 )
1234 /* With a well-written module we shouldn't have to print an
1235 * additional error message here, but just make sure. */
1236 msg_Err( p_this, "failed calling entry point in builtin module" );
1237 module_release( p_module );
1241 /* Everything worked fine ! The module is ready to be added to the list. */
1242 p_module->b_builtin = true;
1244 p_module->next = p_module_bank->head;
1245 p_module_bank->head = p_module;
1248 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1249 p_module->psz_object_name, p_module->psz_longname ); */
1254 /*****************************************************************************
1255 * DeleteModule: delete a module and its structure.
1256 *****************************************************************************
1257 * This function can only be called if the module isn't being used.
1258 *****************************************************************************/
1259 static void DeleteModule( module_bank_t *p_bank, module_t * p_module )
1263 /* Unlist the module (if it is in the list) */
1264 module_t **pp_self = &p_bank->head;
1265 while (*pp_self != NULL && *pp_self != p_module)
1266 pp_self = &((*pp_self)->next);
1268 *pp_self = p_module->next;
1270 /* We free the structures that we strdup()ed in Allocate*Module(). */
1271 #ifdef HAVE_DYNAMIC_PLUGINS
1272 if( !p_module->b_builtin )
1274 if( p_module->b_loaded && p_module->b_unloadable )
1276 module_Unload( p_module->handle );
1278 UndupModule( p_module );
1279 free( p_module->psz_filename );
1283 /* Free and detach the object's children */
1284 while (p_module->submodule)
1286 module_t *submodule = p_module->submodule;
1287 p_module->submodule = submodule->next;
1288 module_release (submodule);
1291 config_Free( p_module );
1292 module_release( p_module );