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 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
36 * is set to 64. Don't try to be cleverer. */
37 #ifdef _FILE_OFFSET_BITS
38 #undef _FILE_OFFSET_BITS
41 #include <stdlib.h> /* free(), strtol() */
42 #include <stdio.h> /* sprintf() */
43 #include <string.h> /* strdup() */
50 #ifdef HAVE_SYS_TYPES_H
51 # include <sys/types.h>
53 #ifdef HAVE_SYS_STAT_H
54 # include <sys/stat.h>
60 #if !defined(HAVE_DYNAMIC_PLUGINS)
61 /* no support for plugins */
62 #elif defined(HAVE_DL_DYLD)
63 # if defined(HAVE_MACH_O_DYLD_H)
64 # include <mach-o/dyld.h>
66 #elif defined(HAVE_DL_BEOS)
67 # if defined(HAVE_IMAGE_H)
70 #elif defined(HAVE_DL_WINDOWS)
72 #elif defined(HAVE_DL_DLOPEN)
73 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
76 # if defined(HAVE_SYS_DL_H)
79 #elif defined(HAVE_DL_SHL_LOAD)
80 # if defined(HAVE_DL_H)
85 #include "config/configuration.h"
87 #include "vlc_charset.h"
88 #include "vlc_arrays.h"
90 #include "modules/modules.h"
91 #include "modules/builtin.h"
93 static module_bank_t *p_module_bank = NULL;
94 static vlc_mutex_t module_lock = VLC_STATIC_MUTEX;
96 /*****************************************************************************
98 *****************************************************************************/
99 #ifdef HAVE_DYNAMIC_PLUGINS
100 static void AllocateAllPlugins ( vlc_object_t * );
101 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
102 static int AllocatePluginFile ( vlc_object_t *, const char *, int64_t, int64_t );
103 static module_t * AllocatePlugin( vlc_object_t *, const char * );
105 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
106 static void DeleteModule ( module_bank_t *, module_t * );
107 #ifdef HAVE_DYNAMIC_PLUGINS
108 static void DupModule ( module_t * );
109 static void UndupModule ( module_t * );
115 * Creates a module bank structure which will be filled later
116 * on with all the modules found.
117 * \param p_this vlc object structure
120 void __module_InitBank( vlc_object_t *p_this )
122 module_bank_t *p_bank = NULL;
124 vlc_mutex_lock( &module_lock );
126 if( p_module_bank == NULL )
128 p_bank = calloc (1, sizeof(*p_bank));
130 p_bank->i_cache = p_bank->i_loaded_cache = 0;
131 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
132 p_bank->b_cache = p_bank->b_cache_dirty = false;
135 /* Everything worked, attach the object */
136 p_module_bank = p_bank;
138 /* Fills the module bank structure with the main module infos.
139 * This is very useful as it will allow us to consider the main
140 * library just as another module, and for instance the configuration
141 * options of main will be available in the module bank structure just
142 * as for every other module. */
143 AllocateBuiltinModule( p_this, vlc_entry__main );
146 p_module_bank->i_usage++;
148 vlc_mutex_unlock( &module_lock );
155 * Unloads all unused plugin modules and empties the module
156 * bank in case of success.
157 * \param p_this vlc object structure
160 void __module_EndBank( vlc_object_t *p_this )
162 module_bank_t *p_bank;
164 /* Save the configuration */
165 config_AutoSaveConfigFile( p_this );
167 vlc_mutex_lock( &module_lock );
168 p_bank = p_module_bank;
169 assert (p_bank != NULL);
170 if( --p_bank->i_usage > 0 )
172 vlc_mutex_unlock( &module_lock );
175 p_module_bank = NULL;
176 vlc_mutex_unlock( &module_lock );
178 #ifdef HAVE_DYNAMIC_PLUGINS
179 if( p_bank->b_cache )
180 CacheSave( p_this, p_bank );
181 while( p_bank->i_loaded_cache-- )
183 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
185 DeleteModule( p_bank,
186 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module );
187 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
188 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
189 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
192 if( p_bank->pp_loaded_cache )
194 free( p_bank->pp_loaded_cache );
195 p_bank->pp_loaded_cache = NULL;
197 while( p_bank->i_cache-- )
199 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
200 free( p_bank->pp_cache[p_bank->i_cache] );
201 p_bank->pp_cache[p_bank->i_cache] = NULL;
203 if( p_bank->pp_cache )
205 free( p_bank->pp_cache );
206 p_bank->pp_cache = NULL;
210 while( p_bank->head != NULL )
211 DeleteModule( p_bank, p_bank->head );
217 * Load all modules which we built with.
219 * Fills the module bank structure with the builtin modules.
220 * \param p_this vlc object structure
223 void __module_LoadBuiltins( vlc_object_t * p_this )
225 vlc_mutex_lock( &module_lock );
226 if( p_module_bank->b_builtins )
228 vlc_mutex_unlock( &module_lock );
231 p_module_bank->b_builtins = true;
232 vlc_mutex_unlock( &module_lock );
234 msg_Dbg( p_this, "checking builtin modules" );
235 /* FIXME: race here - do this under the lock!! */
236 ALLOCATE_ALL_BUILTINS();
239 #undef module_LoadPlugins
243 * Load all plugin modules we can find.
244 * Fills the module bank structure with the plugin modules.
245 * \param p_this vlc object structure
248 void module_LoadPlugins( vlc_object_t * p_this, bool b_cache_delete )
250 #ifdef HAVE_DYNAMIC_PLUGINS
251 vlc_mutex_lock( &module_lock );
252 if( p_module_bank->b_plugins )
254 vlc_mutex_unlock( &module_lock );
257 p_module_bank->b_plugins = true;
258 vlc_mutex_unlock( &module_lock );
260 msg_Dbg( p_this, "checking plugin modules" );
261 p_module_bank->b_cache = config_GetInt( p_this, "plugins-cache" ) > 0;
263 if( p_module_bank->b_cache || b_cache_delete )
264 CacheLoad( p_this, p_module_bank, b_cache_delete );
266 /* FIXME: race - do this under the lock */
267 AllocateAllPlugins( p_this );
272 * Checks whether a module implements a capability.
274 * \param m the module
275 * \param cap the capability to check
276 * \return TRUE if the module have the capability
278 bool module_provides( const module_t *m, const char *cap )
280 return !strcmp( m->psz_capability, cap );
284 * Get the internal name of a module
286 * \param m the module
287 * \return the module name
289 const char *module_get_object( const module_t *m )
291 return m->psz_object_name;
295 * Get the human-friendly name of a module.
297 * \param m the module
298 * \param long_name TRUE to have the long name of the module
299 * \return the short or long name of the module
301 const char *module_get_name( const module_t *m, bool long_name )
303 if( long_name && ( m->psz_longname != NULL) )
304 return m->psz_longname;
306 return m->psz_shortname ?: m->psz_object_name;
310 * Get the help for a module
312 * \param m the module
315 const char *module_get_help( const module_t *m )
321 * Get the capability for a module
323 * \param m the module
324 * return the capability
326 const char *module_get_capability( const module_t *m )
328 return m->psz_capability;
332 * Get the score for a module
334 * \param m the module
335 * return the score for the capability
337 int module_get_score( const module_t *m )
342 module_t *module_hold (module_t *m)
344 vlc_hold (&m->vlc_gc_data);
348 void module_release (module_t *m)
350 vlc_release (&m->vlc_gc_data);
354 * Frees the flat list of VLC modules.
355 * @param list list obtained by module_list_get()
356 * @param length number of items on the list
359 void module_list_free (module_t **list)
364 for (size_t i = 0; list[i] != NULL; i++)
365 module_release (list[i]);
370 * Gets the flat list of VLC modules.
371 * @param n [OUT] pointer to the number of modules or NULL
372 * @return NULL-terminated table of module pointers
373 * (release with module_list_free()), or NULL in case of error.
375 module_t **module_list_get (size_t *n)
377 /* TODO: this whole module lookup is quite inefficient */
378 /* Remove this and improve module_need */
379 module_t **tab = NULL;
382 assert (p_module_bank);
383 for (module_t *mod = p_module_bank->head; mod; mod = mod->next)
386 nt = realloc (tab, (i + 2 + mod->submodule_count) * sizeof (*tab));
389 module_list_free (tab);
394 tab[i++] = module_hold (mod);
395 for (module_t *subm = mod->submodule; subm; subm = subm->next)
396 tab[i++] = module_hold (subm);
404 typedef struct module_list_t
411 static int modulecmp (const void *a, const void *b)
413 const module_list_t *la = a, *lb = b;
414 /* Note that qsort() uses _ascending_ order,
415 * so the smallest module is the one with the biggest score. */
416 return lb->i_score - la->i_score;
422 * Return the best module function, given a capability list.
424 * If the p_this object doesn't have it's psz_object_name set, then
425 * psz_object_name will be set to the module's name, unless the user
426 * provided an alias using the "module name@alias" syntax in which case
427 * psz_object_name will be set to the alias.
429 * \param p_this the vlc object
430 * \param psz_capability list of capabilities needed
431 * \param psz_name name of the module asked
432 * \param b_strict TRUE yto use the strict mode
433 * \return the module or NULL in case of a failure
435 module_t * __module_need( vlc_object_t *p_this, const char *psz_capability,
436 const char *psz_name, bool b_strict )
438 stats_TimerStart( p_this, "module_need()", STATS_TIMER_MODULE_NEED );
440 module_list_t *p_list;
443 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
444 bool b_force_backup = p_this->b_force;
446 /* Deal with variables */
447 if( psz_name && psz_name[0] == '$' )
449 psz_name = psz_var = var_CreateGetString( p_this, psz_name + 1 );
452 /* Count how many different shortcuts were asked for */
453 if( psz_name && *psz_name )
455 char *psz_parser, *psz_last_shortcut;
457 /* If the user wants none, give him none. */
458 if( !strcmp( psz_name, "none" ) )
461 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
462 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
463 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
468 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
470 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
472 if( *psz_parser == ',' )
476 psz_last_shortcut = psz_parser + 1;
480 /* Check if the user wants to override the "strict" mode */
481 if( psz_last_shortcut )
483 if( !strcmp(psz_last_shortcut, "none") )
488 else if( !strcmp(psz_last_shortcut, "any") )
496 /* Sort the modules and test them */
498 module_t **p_all = module_list_get (&count);
499 p_list = malloc( count * sizeof( module_list_t ) );
500 unsigned i_cpu = vlc_CPU();
502 /* Parse the module list for capabilities and probe each of them */
504 for (size_t i = 0; (p_module = p_all[i]) != NULL; i++)
506 bool b_shortcut_bonus = false;
508 /* Test that this module can do what we need */
509 if( !module_provides( p_module, psz_capability ) )
511 /* Test if we have the required CPU */
512 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
515 /* If we required a shortcut, check this plugin provides it. */
516 if( i_shortcuts > 0 )
518 const char *psz_name = psz_shortcuts;
520 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
522 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
525 if( ( c = strchr( psz_name, '@' ) )
526 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
528 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
533 b_shortcut_bonus = true;
538 /* Go to the next shortcut... This is so lame! */
539 psz_name += strlen( psz_name ) + 1;
542 /* If we are in "strict" mode and we couldn't
543 * find the module in the list of provided shortcuts,
544 * then kick the bastard out of here!!! */
548 /* If we didn't require a shortcut, trash <= 0 scored plugins */
549 else if( p_module->i_score <= 0 )
555 /* Store this new module */
556 p_list[count].p_module = module_hold (p_module);
557 p_list[count].i_score = p_module->i_score;
558 if( b_shortcut_bonus )
559 p_list[count].i_score += 10000;
560 p_list[count].b_force = b_shortcut_bonus && b_strict;
564 /* We can release the list, interesting modules are held */
565 module_list_free (p_all);
567 /* Sort candidates by descending score */
568 qsort (p_list, count, sizeof (p_list[0]), modulecmp);
570 /* FIXME: Remove this hack after finding a general solution for %z's */
571 msg_Dbg( p_this, "looking for %s module: %u candidate%s", psz_capability,
572 count, count == 1 ? "" : "s" );
574 msg_Dbg( p_this, "looking for %s module: %zu candidate%s", psz_capability,
575 count, count == 1 ? "" : "s" );
578 /* Parse the linked list and use the first successful module */
580 for (size_t i = 0; (i < count) && (p_module == NULL); i++)
582 module_t *p_cand = p_list[i].p_module;
583 #ifdef HAVE_DYNAMIC_PLUGINS
584 /* Make sure the module is loaded in mem */
585 module_t *p_real = p_cand->b_submodule ? p_cand->parent : p_cand;
587 if( !p_real->b_builtin && !p_real->b_loaded )
589 module_t *p_new_module =
590 AllocatePlugin( p_this, p_real->psz_filename );
593 CacheMerge( p_this, p_real, p_new_module );
594 DeleteModule( p_module_bank, p_new_module );
599 p_this->b_force = p_list[i].b_force;
600 if( p_cand->pf_activate
601 && p_cand->pf_activate( p_this ) == VLC_SUCCESS )
604 /* Release the remaining modules */
606 module_release (p_list[i].p_module);
609 module_release( p_cand );
613 p_this->b_force = b_force_backup;
615 if( p_module != NULL )
617 msg_Dbg( p_this, "using %s module \"%s\"",
618 psz_capability, p_module->psz_object_name );
619 if( !p_this->psz_object_name )
621 /* This assumes that p_this is the object which will be using the
622 * module. That's not always the case ... but it is in most cases.
625 p_this->psz_object_name = strdup( psz_alias );
627 p_this->psz_object_name = strdup( p_module->psz_object_name );
630 else if( count == 0 )
632 if( !strcmp( psz_capability, "access_demux" )
633 || !strcmp( psz_capability, "stream_filter" )
634 || !strcmp( psz_capability, "vout_window" ) )
636 msg_Dbg( p_this, "no %s module matched \"%s\"",
637 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
641 msg_Err( p_this, "no %s module matched \"%s\"",
642 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
644 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
645 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
648 else if( psz_name != NULL && *psz_name )
650 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
651 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
654 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
656 free( psz_shortcuts );
659 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
660 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
661 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
663 /* Don't forget that the module is still locked */
670 * This function must be called by the thread that called module_need, to
671 * decrease the reference count and allow for hiding of modules.
672 * \param p_this vlc object structure
673 * \param p_module the module structure
676 void __module_unneed( vlc_object_t * p_this, module_t * p_module )
678 /* Use the close method */
679 if( p_module->pf_deactivate )
681 p_module->pf_deactivate( p_this );
684 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
686 module_release( p_module );
690 * Get a pointer to a module_t given it's name.
692 * \param psz_name the name of the module
693 * \return a pointer to the module or NULL in case of a failure
695 module_t *module_find( const char * psz_name )
697 module_t **list, *module;
699 list = module_list_get (NULL);
703 for (size_t i = 0; (module = list[i]) != NULL; i++)
705 const char *psz_module_name = module->psz_object_name;
707 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
709 module_hold (module);
713 module_list_free (list);
718 * Tell if a module exists and release it in thic case
720 * \param psz_name th name of the module
721 * \return TRUE if the module exists
723 bool module_exists (const char * psz_name)
725 module_t *p_module = module_find (psz_name);
727 module_release (p_module);
728 return p_module != NULL;
732 * Get a pointer to a module_t that matches a shortcut.
733 * This is a temporary hack for SD. Do not re-use (generally multiple modules
734 * can have the same shortcut, so this is *broken* - use module_need()!).
736 * \param psz_shortcut shortcut of the module
737 * \param psz_cap capability of the module
738 * \return a pointer to the module or NULL in case of a failure
740 module_t *module_find_by_shortcut (const char *psz_shortcut)
742 module_t **list, *module;
744 list = module_list_get (NULL);
748 for (size_t i = 0; (module = list[i]) != NULL; i++)
751 (module->pp_shortcuts[j] != NULL) && (j < MODULE_SHORTCUT_MAX);
754 if (!strcmp (module->pp_shortcuts[j], psz_shortcut))
756 module_hold (module);
762 module_list_free (list);
767 * GetModuleNamesForCapability
769 * Return a NULL terminated array with the names of the modules
770 * that have a certain capability.
771 * Free after uses both the string and the table.
772 * \param psz_capability the capability asked
773 * \param pppsz_longname an pointer to an array of string to contain
774 the long names of the modules. If set to NULL the function don't use it.
775 * \return the NULL terminated array
777 char ** module_GetModulesNamesForCapability( const char *psz_capability,
778 char ***pppsz_longname )
783 module_t **list = module_list_get (NULL);
785 /* Do it in two passes : count the number of modules before */
786 for (size_t i = 0; list[i]; i++)
788 module_t *p_module = list[i];
789 const char *psz_module_capability = p_module->psz_capability;
791 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
795 psz_ret = malloc( sizeof(char*) * (count+1) );
797 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
798 if( !psz_ret || ( pppsz_longname && *pppsz_longname == NULL ) )
803 free( *pppsz_longname );
804 *pppsz_longname = NULL;
806 module_list_free (list);
810 for (size_t i = 0, j = 0; list[i]; i++)
812 module_t *p_module = list[i];
813 const char *psz_module_capability = p_module->psz_capability;
815 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
817 int k = -1; /* hack to handle submodules properly */
818 if( p_module->b_submodule )
820 while( p_module->pp_shortcuts[++k] != NULL );
823 psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
824 :p_module->psz_object_name );
826 (*pppsz_longname)[j] = strdup( module_get_name( p_module, true ) );
830 psz_ret[count] = NULL;
832 module_list_free (list);
838 * Get the configuration of a module
840 * \param module the module
841 * \param psize the size of the configuration returned
842 * \return the configuration as an array
844 module_config_t *module_config_get( const module_t *module, unsigned *restrict psize )
847 unsigned size = module->confsize;
848 module_config_t *config = malloc( size * sizeof( *config ) );
850 assert( psize != NULL );
856 for( i = 0, j = 0; i < size; i++ )
858 const module_config_t *item = module->p_config + i;
859 if( item->b_internal /* internal option */
860 || item->b_unsaveable /* non-modifiable option */
861 || item->b_removed /* removed option */ )
864 memcpy( config + j, item, sizeof( *config ) );
873 * Release the configuration
875 * \param the configuration
878 void module_config_free( module_config_t *config )
883 /*****************************************************************************
884 * Following functions are local.
885 *****************************************************************************/
887 /*****************************************************************************
888 * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
890 *****************************************************************************/
891 static char * copy_next_paths_token( char * paths, char ** remaining_paths )
895 bool escaped = false;
899 /* Alloc a buffer to store the path */
900 path = malloc( strlen( paths ) + 1 );
901 if( !path ) return NULL;
903 /* Look for PATH_SEP_CHAR (a ':' or a ';') */
904 for( i = 0, done = 0 ; paths[i]; i++ )
906 /* Take care of \\ and \: or \; escapement */
910 path[done++] = paths[i];
913 else if( paths[i] == '/' )
916 else if( paths[i] == '\\' )
919 else if( paths[i] == PATH_SEP_CHAR )
922 path[done++] = paths[i];
926 /* Return the remaining paths */
927 if( remaining_paths ) {
928 *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
934 char *psz_vlcpath = NULL;
936 /*****************************************************************************
937 * AllocateAllPlugins: load all plugin modules we can find.
938 *****************************************************************************/
939 #ifdef HAVE_DYNAMIC_PLUGINS
940 static void AllocateAllPlugins( vlc_object_t *p_this )
942 const char *vlcpath = psz_vlcpath;
945 vlc_array_t *arraypaths = vlc_array_new();
947 /* Contruct the special search path for system that have a relocatable
948 * executable. Set it to <vlc path>/modules and <vlc path>/plugins. */
950 if( vlcpath && asprintf( &path, "%s" DIR_SEP "modules", vlcpath ) != -1 )
951 vlc_array_append( arraypaths, path );
952 if( vlcpath && asprintf( &path, "%s" DIR_SEP "plugins", vlcpath ) != -1 )
953 vlc_array_append( arraypaths, path );
955 vlc_array_append( arraypaths, strdup( PLUGIN_PATH ) );
958 /* If the user provided a plugin path, we add it to the list */
959 char *userpaths = config_GetPsz( p_this, "plugin-path" );
962 for( paths_iter = userpaths; paths_iter; )
964 path = copy_next_paths_token( paths_iter, &paths_iter );
966 vlc_array_append( arraypaths, path );
969 count = vlc_array_count( arraypaths );
970 for( i = 0 ; i < count ; i++ )
972 path = vlc_array_item_at_index( arraypaths, i );
976 msg_Dbg( p_this, "recursively browsing `%s'", path );
978 /* Don't go deeper than 5 subdirectories */
979 AllocatePluginDir( p_this, path, 5 );
984 vlc_array_destroy( arraypaths );
988 /*****************************************************************************
989 * AllocatePluginDir: recursively parse a directory to look for plugins
990 *****************************************************************************/
991 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
994 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
1000 #if defined( UNDER_CE ) || defined( _MSC_VER )
1002 wchar_t psz_wpath[MAX_PATH + 256];
1003 wchar_t psz_wdir[MAX_PATH];
1005 char psz_path[MAX_PATH + 256];
1006 WIN32_FIND_DATA finddata;
1012 struct dirent * file;
1016 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
1021 #if defined( UNDER_CE ) || defined( _MSC_VER )
1023 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
1025 rc = GetFileAttributes( psz_wdir );
1026 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1028 /* Parse all files in the directory */
1029 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
1031 rc = GetFileAttributes( psz_dir );
1032 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1035 /* Parse all files in the directory */
1036 sprintf( psz_path, "%s\\*", psz_dir );
1039 handle = FindFirstFile( psz_wpath, &finddata );
1041 handle = FindFirstFile( psz_path, &finddata );
1043 if( handle == INVALID_HANDLE_VALUE )
1045 /* Empty directory */
1049 /* Parse the directory and try to load all files it contains. */
1053 unsigned int i_len = wcslen( finddata.cFileName );
1054 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
1055 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
1057 unsigned int i_len = strlen( finddata.cFileName );
1058 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
1061 /* Skip ".", ".." */
1062 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
1063 || !strcmp( finddata.cFileName, ".." ) )
1065 if( !FindNextFile( handle, &finddata ) ) break;
1070 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
1072 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
1075 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
1077 else if( i_len > strlen( LIBEXT )
1078 /* We only load files ending with LIBEXT */
1079 && !strncasecmp( psz_path + strlen( psz_path)
1081 LIBEXT, strlen( LIBEXT ) ) )
1083 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
1084 int64_t i_time = 0, i_size = 0;
1087 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
1090 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
1094 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
1096 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
1097 i_size = attrbuf.nFileSizeHigh;
1099 i_size |= attrbuf.nFileSizeLow;
1101 psz_file = psz_path;
1103 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1106 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1108 /* Close the directory */
1109 FindClose( handle );
1112 dir = opendir( psz_dir );
1118 i_dirlen = strlen( psz_dir );
1120 /* Parse the directory and try to load all files it contains. */
1121 while( !p_this->p_libvlc->b_die && ( file = readdir( dir ) ) )
1123 struct stat statbuf;
1127 /* Skip ".", ".." */
1128 if( !*file->d_name || !strcmp( file->d_name, "." )
1129 || !strcmp( file->d_name, ".." ) )
1134 i_len = strlen( file->d_name );
1135 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1136 sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1138 i_stat = stat( psz_file, &statbuf );
1139 if( !i_stat && statbuf.st_mode & S_IFDIR )
1141 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1143 else if( i_len > strlen( LIBEXT )
1144 /* We only load files ending with LIBEXT */
1145 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1146 LIBEXT, strlen( LIBEXT ) ) )
1148 int64_t i_time = 0, i_size = 0;
1152 i_time = statbuf.st_mtime;
1153 i_size = statbuf.st_size;
1156 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1162 /* Close the directory */
1168 /*****************************************************************************
1169 * AllocatePluginFile: load a module into memory and initialize it.
1170 *****************************************************************************
1171 * This function loads a dynamically loadable module and allocates a structure
1172 * for its information data. The module can then be handled by module_need
1173 * and module_unneed. It can be removed by DeleteModule.
1174 *****************************************************************************/
1175 static int AllocatePluginFile( vlc_object_t * p_this, const char * psz_file,
1176 int64_t i_file_time, int64_t i_file_size )
1178 module_t * p_module = NULL;
1179 module_cache_t *p_cache_entry = NULL;
1182 * Check our plugins cache first then load plugin if needed
1185 CacheFind( p_module_bank, psz_file, i_file_time, i_file_size );
1187 if( !p_cache_entry )
1189 p_module = AllocatePlugin( p_this, psz_file );
1193 /* If junk dll, don't try to load it */
1194 if( p_cache_entry->b_junk )
1200 module_config_t *p_item = NULL, *p_end = NULL;
1202 p_module = p_cache_entry->p_module;
1203 p_module->b_loaded = false;
1205 /* For now we force loading if the module's config contains
1206 * callbacks or actions.
1207 * Could be optimized by adding an API call.*/
1208 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1209 p_item < p_end; p_item++ )
1211 if( p_item->pf_callback || p_item->i_action )
1213 p_module = AllocatePlugin( p_this, psz_file );
1217 if( p_module == p_cache_entry->p_module )
1218 p_cache_entry->b_used = true;
1224 /* Everything worked fine !
1225 * The module is ready to be added to the list. */
1226 p_module->b_builtin = false;
1228 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1229 p_module->psz_object_name, p_module->psz_longname ); */
1230 p_module->next = p_module_bank->head;
1231 p_module_bank->head = p_module;
1233 if( !p_module_bank->b_cache )
1236 #define p_bank p_module_bank
1237 /* Add entry to cache */
1239 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1240 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1241 if( !p_bank->pp_cache[p_bank->i_cache] )
1243 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1244 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1245 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1246 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1247 p_bank->pp_cache[p_bank->i_cache]->b_used = true;
1248 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1253 return p_module ? 0 : -1;
1256 /*****************************************************************************
1257 * AllocatePlugin: load a module into memory and initialize it.
1258 *****************************************************************************
1259 * This function loads a dynamically loadable module and allocates a structure
1260 * for its information data. The module can then be handled by module_need
1261 * and module_unneed. It can be removed by DeleteModule.
1262 *****************************************************************************/
1263 static module_t * AllocatePlugin( vlc_object_t * p_this, const char *psz_file )
1265 module_t * p_module = NULL;
1266 module_handle_t handle;
1268 if( module_Load( p_this, psz_file, &handle ) )
1271 /* Now that we have successfully loaded the module, we can
1272 * allocate a structure for it */
1273 p_module = vlc_module_create( p_this );
1274 if( p_module == NULL )
1276 module_Unload( handle );
1280 /* We need to fill these since they may be needed by module_Call() */
1281 p_module->psz_filename = psz_file;
1282 p_module->handle = handle;
1283 p_module->b_loaded = true;
1285 /* Initialize the module: fill p_module, default config */
1286 if( module_Call( p_this, p_module ) != 0 )
1288 /* We couldn't call module_init() */
1289 module_release( p_module );
1290 module_Unload( handle );
1294 DupModule( p_module );
1295 p_module->psz_filename = strdup( p_module->psz_filename );
1297 /* Everything worked fine ! The module is ready to be added to the list. */
1298 p_module->b_builtin = false;
1303 /*****************************************************************************
1304 * DupModule: make a plugin module standalone.
1305 *****************************************************************************
1306 * This function duplicates all strings in the module, so that the dynamic
1307 * object can be unloaded. It acts recursively on submodules.
1308 *****************************************************************************/
1309 static void DupModule( module_t *p_module )
1313 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1315 *pp_shortcut = strdup( *pp_shortcut );
1318 /* We strdup() these entries so that they are still valid when the
1319 * module is unloaded. */
1320 p_module->psz_capability = strdup( p_module->psz_capability );
1321 p_module->psz_shortname = p_module->psz_shortname ?
1322 strdup( p_module->psz_shortname ) : NULL;
1323 p_module->psz_longname = strdup( p_module->psz_longname );
1324 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1327 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1331 /*****************************************************************************
1332 * UndupModule: free a duplicated module.
1333 *****************************************************************************
1334 * This function frees the allocations done in DupModule().
1335 *****************************************************************************/
1336 static void UndupModule( module_t *p_module )
1340 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1343 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1345 free( *pp_shortcut );
1348 free( p_module->psz_capability );
1349 FREENULL( p_module->psz_shortname );
1350 free( p_module->psz_longname );
1351 FREENULL( p_module->psz_help );
1354 #endif /* HAVE_DYNAMIC_PLUGINS */
1356 /*****************************************************************************
1357 * AllocateBuiltinModule: initialize a builtin module.
1358 *****************************************************************************
1359 * This function registers a builtin module and allocates a structure
1360 * for its information data. The module can then be handled by module_need
1361 * and module_unneed. It can be removed by DeleteModule.
1362 *****************************************************************************/
1363 static int AllocateBuiltinModule( vlc_object_t * p_this,
1364 int ( *pf_entry ) ( module_t * ) )
1366 module_t * p_module;
1368 /* Now that we have successfully loaded the module, we can
1369 * allocate a structure for it */
1370 p_module = vlc_module_create( p_this );
1371 if( p_module == NULL )
1374 /* Initialize the module : fill p_module->psz_object_name, etc. */
1375 if( pf_entry( p_module ) != 0 )
1377 /* With a well-written module we shouldn't have to print an
1378 * additional error message here, but just make sure. */
1379 msg_Err( p_this, "failed calling entry point in builtin module" );
1380 module_release( p_module );
1384 /* Everything worked fine ! The module is ready to be added to the list. */
1385 p_module->b_builtin = true;
1387 p_module->next = p_module_bank->head;
1388 p_module_bank->head = p_module;
1391 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1392 p_module->psz_object_name, p_module->psz_longname ); */
1397 /*****************************************************************************
1398 * DeleteModule: delete a module and its structure.
1399 *****************************************************************************
1400 * This function can only be called if the module isn't being used.
1401 *****************************************************************************/
1402 static void DeleteModule( module_bank_t *p_bank, module_t * p_module )
1406 /* Unlist the module (if it is in the list) */
1407 module_t **pp_self = &p_bank->head;
1408 while (*pp_self != NULL && *pp_self != p_module)
1409 pp_self = &((*pp_self)->next);
1411 *pp_self = p_module->next;
1413 /* We free the structures that we strdup()ed in Allocate*Module(). */
1414 #ifdef HAVE_DYNAMIC_PLUGINS
1415 if( !p_module->b_builtin )
1417 if( p_module->b_loaded && p_module->b_unloadable )
1419 module_Unload( p_module->handle );
1421 UndupModule( p_module );
1422 free( p_module->psz_filename );
1426 /* Free and detach the object's children */
1427 while (p_module->submodule)
1429 module_t *submodule = p_module->submodule;
1430 p_module->submodule = submodule->next;
1431 module_release (submodule);
1434 config_Free( p_module );
1435 module_release( p_module );