1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Sam Hocevar <sam@zoy.org>
8 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
9 * Hans-Peter Jansen <hpj@urpla.net>
10 * Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_memory.h>
36 #include <stdlib.h> /* free(), strtol() */
37 #include <stdio.h> /* sprintf() */
38 #include <string.h> /* strdup() */
45 #include <sys/types.h>
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
56 #include "config/configuration.h"
58 #include "vlc_charset.h"
59 #include "vlc_arrays.h"
61 #include "modules/modules.h"
63 static module_bank_t *p_module_bank = NULL;
64 static vlc_mutex_t module_lock = VLC_STATIC_MUTEX;
66 int vlc_entry__main( module_t * );
68 /*****************************************************************************
70 *****************************************************************************/
71 #ifdef HAVE_DYNAMIC_PLUGINS
72 static void AllocateAllPlugins( vlc_object_t *, module_bank_t * );
73 static void AllocatePluginDir( vlc_object_t *, module_bank_t *, const char *,
75 static int AllocatePluginFile( vlc_object_t *, module_bank_t *, const char *,
77 static module_t * AllocatePlugin( vlc_object_t *, const char * );
79 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
80 static void DeleteModule ( module_bank_t *, module_t * );
81 #ifdef HAVE_DYNAMIC_PLUGINS
82 static void DupModule ( module_t * );
83 static void UndupModule ( module_t * );
89 * Creates a module bank structure which will be filled later
90 * on with all the modules found.
91 * \param p_this vlc object structure
94 void __module_InitBank( vlc_object_t *p_this )
96 module_bank_t *p_bank = NULL;
98 vlc_mutex_lock( &module_lock );
100 if( p_module_bank == NULL )
102 p_bank = calloc (1, sizeof(*p_bank));
104 p_bank->i_cache = p_bank->i_loaded_cache = 0;
105 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
106 p_bank->b_cache = p_bank->b_cache_dirty = false;
109 /* Everything worked, attach the object */
110 p_module_bank = p_bank;
112 /* Fills the module bank structure with the main module infos.
113 * This is very useful as it will allow us to consider the main
114 * library just as another module, and for instance the configuration
115 * options of main will be available in the module bank structure just
116 * as for every other module. */
117 AllocateBuiltinModule( p_this, vlc_entry__main );
118 vlc_rwlock_init (&config_lock);
119 config_SortConfig ();
122 p_module_bank->i_usage++;
124 /* We do retain the module bank lock until the plugins are loaded as well.
125 * This is ugly, this staged loading approach is needed: LibVLC gets
126 * some configuration parameters relevant to loading the plugins from
127 * the main (builtin) module. The module bank becomes shared read-only data
128 * once it is ready, so we need to fully serialize initialization.
129 * DO NOT UNCOMMENT the following line unless you managed to squeeze
130 * module_LoadPlugins() before you unlock the mutex. */
131 /*vlc_mutex_unlock( &module_lock );*/
134 #undef module_EndBank
136 * Unloads all unused plugin modules and empties the module
137 * bank in case of success.
138 * \param p_this vlc object structure
141 void module_EndBank( vlc_object_t *p_this, bool b_plugins )
143 module_bank_t *p_bank = p_module_bank;
145 assert (p_bank != NULL);
147 /* Save the configuration */
148 if( !var_InheritBool( p_this, "ignore-config" ) )
149 config_AutoSaveConfigFile( p_this );
151 /* If plugins were _not_ loaded, then the caller still has the bank lock
152 * from module_InitBank(). */
154 vlc_mutex_lock( &module_lock );
156 vlc_assert_locked( &module_lock ); not for static mutexes :( */
158 if( --p_bank->i_usage > 0 )
160 vlc_mutex_unlock( &module_lock );
164 config_UnsortConfig ();
165 vlc_rwlock_destroy (&config_lock);
166 p_module_bank = NULL;
167 vlc_mutex_unlock( &module_lock );
169 #ifdef HAVE_DYNAMIC_PLUGINS
170 while( p_bank->i_loaded_cache-- )
172 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
174 DeleteModule( p_bank,
175 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module );
176 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
177 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
180 free( p_bank->pp_loaded_cache );
181 while( p_bank->i_cache-- )
183 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
184 free( p_bank->pp_cache[p_bank->i_cache] );
186 free( p_bank->pp_cache );
189 while( p_bank->head != NULL )
190 DeleteModule( p_bank, p_bank->head );
195 #undef module_LoadPlugins
197 * Loads module descriptions for all available plugins.
198 * Fills the module bank structure with the plugin modules.
200 * \param p_this vlc object structure
203 void module_LoadPlugins( vlc_object_t * p_this )
205 module_bank_t *p_bank = p_module_bank;
208 /*vlc_assert_locked( &module_lock ); not for static mutexes :( */
210 #ifdef HAVE_DYNAMIC_PLUGINS
211 if( p_bank->i_usage == 1 )
213 msg_Dbg( p_this, "checking plugin modules" );
214 p_module_bank->b_cache = var_InheritBool( p_this, "plugins-cache" );
216 AllocateAllPlugins( p_this, p_module_bank );
217 if( p_module_bank->b_cache )
218 CacheSave( p_this, p_bank );
219 config_UnsortConfig ();
220 config_SortConfig ();
223 vlc_mutex_unlock( &module_lock );
227 * Checks whether a module implements a capability.
229 * \param m the module
230 * \param cap the capability to check
231 * \return TRUE if the module have the capability
233 bool module_provides( const module_t *m, const char *cap )
235 return !strcmp( m->psz_capability, cap );
239 * Get the internal name of a module
241 * \param m the module
242 * \return the module name
244 const char *module_get_object( const module_t *m )
246 return m->psz_object_name;
250 * Get the human-friendly name of a module.
252 * \param m the module
253 * \param long_name TRUE to have the long name of the module
254 * \return the short or long name of the module
256 const char *module_get_name( const module_t *m, bool long_name )
258 if( long_name && ( m->psz_longname != NULL) )
259 return m->psz_longname;
261 return m->psz_shortname ? m->psz_shortname : m->psz_object_name;
265 * Get the help for a module
267 * \param m the module
270 const char *module_get_help( const module_t *m )
276 * Get the capability for a module
278 * \param m the module
279 * return the capability
281 const char *module_get_capability( const module_t *m )
283 return m->psz_capability;
287 * Get the score for a module
289 * \param m the module
290 * return the score for the capability
292 int module_get_score( const module_t *m )
298 * Translate a string using the module's text domain
300 * \param m the module
301 * \param str the American English ASCII string to localize
302 * \return the gettext-translated string
304 const char *module_gettext (const module_t *m, const char *str)
307 const char *domain = m->domain ? m->domain : PACKAGE_NAME;
308 return dgettext (domain, str);
315 module_t *module_hold (module_t *m)
317 vlc_hold (&m->vlc_gc_data);
321 void module_release (module_t *m)
323 vlc_release (&m->vlc_gc_data);
327 * Frees the flat list of VLC modules.
328 * @param list list obtained by module_list_get()
329 * @param length number of items on the list
332 void module_list_free (module_t **list)
337 for (size_t i = 0; list[i] != NULL; i++)
338 module_release (list[i]);
343 * Gets the flat list of VLC modules.
344 * @param n [OUT] pointer to the number of modules or NULL
345 * @return NULL-terminated table of module pointers
346 * (release with module_list_free()), or NULL in case of error.
348 module_t **module_list_get (size_t *n)
350 /* TODO: this whole module lookup is quite inefficient */
351 /* Remove this and improve module_need */
352 module_t **tab = NULL;
355 assert (p_module_bank);
356 for (module_t *mod = p_module_bank->head; mod; mod = mod->next)
359 nt = realloc (tab, (i + 2 + mod->submodule_count) * sizeof (*tab));
362 module_list_free (tab);
367 tab[i++] = module_hold (mod);
368 for (module_t *subm = mod->submodule; subm; subm = subm->next)
369 tab[i++] = module_hold (subm);
377 typedef struct module_list_t
384 static int modulecmp (const void *a, const void *b)
386 const module_list_t *la = a, *lb = b;
387 /* Note that qsort() uses _ascending_ order,
388 * so the smallest module is the one with the biggest score. */
389 return lb->i_score - la->i_score;
395 * Return the best module function, given a capability list.
397 * \param p_this the vlc object
398 * \param psz_capability list of capabilities needed
399 * \param psz_name name of the module asked
400 * \param b_strict if true, do not fallback to plugin with a different name
401 * but the same capability
402 * \return the module or NULL in case of a failure
404 module_t * __module_need( vlc_object_t *p_this, const char *psz_capability,
405 const char *psz_name, bool b_strict )
407 stats_TimerStart( p_this, "module_need()", STATS_TIMER_MODULE_NEED );
409 module_list_t *p_list;
412 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
413 bool b_force_backup = p_this->b_force;
415 /* Deal with variables */
416 if( psz_name && psz_name[0] == '$' )
418 psz_name = psz_var = var_CreateGetString( p_this, psz_name + 1 );
421 /* Count how many different shortcuts were asked for */
422 if( psz_name && *psz_name )
424 char *psz_parser, *psz_last_shortcut;
426 /* If the user wants none, give him none. */
427 if( !strcmp( psz_name, "none" ) )
430 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
431 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
432 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
437 psz_parser = psz_shortcuts = psz_last_shortcut = strdup( psz_name );
439 while( ( psz_parser = strchr( psz_parser, ',' ) ) )
443 psz_last_shortcut = ++psz_parser;
446 /* Check if the user wants to override the "strict" mode */
447 if( psz_last_shortcut )
449 if( !strcmp(psz_last_shortcut, "none") )
454 else if( !strcmp(psz_last_shortcut, "any") )
462 /* Sort the modules and test them */
464 module_t **p_all = module_list_get (&count);
465 p_list = malloc( count * sizeof( module_list_t ) );
467 /* Parse the module list for capabilities and probe each of them */
469 for (size_t i = 0; (p_module = p_all[i]) != NULL; i++)
471 int i_shortcut_bonus = 0;
473 /* Test that this module can do what we need */
474 if( !module_provides( p_module, psz_capability ) )
477 /* If we required a shortcut, check this plugin provides it. */
478 if( i_shortcuts > 0 )
480 const char *name = psz_shortcuts;
482 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
484 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
487 if( ( c = strchr( name, '@' ) )
488 ? !strncasecmp( name, p_module->pp_shortcuts[i],
490 : !strcasecmp( name, p_module->pp_shortcuts[i] ) )
495 i_shortcut_bonus = i_short * 10000;
500 /* Go to the next shortcut... This is so lame! */
501 name += strlen( name ) + 1;
504 /* If we are in "strict" mode and we couldn't
505 * find the module in the list of provided shortcuts,
506 * then kick the bastard out of here!!! */
511 /* Trash <= 0 scored plugins (they can only be selected by shortcut) */
512 if( p_module->i_score <= 0 )
516 /* Store this new module */
517 p_list[count].p_module = module_hold (p_module);
518 p_list[count].i_score = p_module->i_score + i_shortcut_bonus;
519 p_list[count].b_force = i_shortcut_bonus && b_strict;
523 /* We can release the list, interesting modules are held */
524 module_list_free (p_all);
526 /* Sort candidates by descending score */
527 qsort (p_list, count, sizeof (p_list[0]), modulecmp);
528 msg_Dbg( p_this, "looking for %s module: %zu candidate%s", psz_capability,
529 count, count == 1 ? "" : "s" );
531 /* Parse the linked list and use the first successful module */
533 for (size_t i = 0; (i < count) && (p_module == NULL); i++)
535 module_t *p_cand = p_list[i].p_module;
536 #ifdef HAVE_DYNAMIC_PLUGINS
537 /* Make sure the module is loaded in mem */
538 module_t *p_real = p_cand->b_submodule ? p_cand->parent : p_cand;
540 if( !p_real->b_builtin && !p_real->b_loaded )
542 module_t *p_new_module =
543 AllocatePlugin( p_this, p_real->psz_filename );
544 if( p_new_module == NULL )
545 { /* Corrupted module */
546 msg_Err( p_this, "possibly corrupt module cache" );
547 module_release( p_cand );
550 CacheMerge( p_this, p_real, p_new_module );
551 DeleteModule( p_module_bank, p_new_module );
555 p_this->b_force = p_list[i].b_force;
557 int ret = VLC_SUCCESS;
558 if( p_cand->pf_activate )
559 ret = p_cand->pf_activate( p_this );
568 /* good module, but aborted */
569 module_release( p_cand );
572 default: /* bad module */
573 module_release( p_cand );
577 /* Release the remaining modules */
579 module_release (p_list[i].p_module);
583 p_this->b_force = b_force_backup;
585 if( p_module != NULL )
587 msg_Dbg( p_this, "using %s module \"%s\"",
588 psz_capability, p_module->psz_object_name );
589 vlc_object_set_name( p_this, psz_alias ? psz_alias
590 : p_module->psz_object_name );
592 else if( count == 0 )
594 if( !strcmp( psz_capability, "access_demux" )
595 || !strcmp( psz_capability, "stream_filter" )
596 || !strcmp( psz_capability, "vout_window" ) )
598 msg_Dbg( p_this, "no %s module matched \"%s\"",
599 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
603 msg_Err( p_this, "no %s module matched \"%s\"",
604 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
606 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
607 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
610 else if( psz_name != NULL && *psz_name )
612 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
613 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
616 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
618 free( psz_shortcuts );
621 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
622 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
623 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
625 /* Don't forget that the module is still locked */
632 * This function must be called by the thread that called module_need, to
633 * decrease the reference count and allow for hiding of modules.
634 * \param p_this vlc object structure
635 * \param p_module the module structure
638 void __module_unneed( vlc_object_t * p_this, module_t * p_module )
640 /* Use the close method */
641 if( p_module->pf_deactivate )
643 p_module->pf_deactivate( p_this );
646 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
648 module_release( p_module );
652 * Get a pointer to a module_t given it's name.
654 * \param psz_name the name of the module
655 * \return a pointer to the module or NULL in case of a failure
657 module_t *module_find( const char * psz_name )
659 module_t **list, *module;
661 list = module_list_get (NULL);
665 for (size_t i = 0; (module = list[i]) != NULL; i++)
667 const char *psz_module_name = module->psz_object_name;
669 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
671 module_hold (module);
675 module_list_free (list);
680 * Tell if a module exists and release it in thic case
682 * \param psz_name th name of the module
683 * \return TRUE if the module exists
685 bool module_exists (const char * psz_name)
687 module_t *p_module = module_find (psz_name);
689 module_release (p_module);
690 return p_module != NULL;
694 * Get a pointer to a module_t that matches a shortcut.
695 * This is a temporary hack for SD. Do not re-use (generally multiple modules
696 * can have the same shortcut, so this is *broken* - use module_need()!).
698 * \param psz_shortcut shortcut of the module
699 * \param psz_cap capability of the module
700 * \return a pointer to the module or NULL in case of a failure
702 module_t *module_find_by_shortcut (const char *psz_shortcut)
704 module_t **list, *module;
706 list = module_list_get (NULL);
710 for (size_t i = 0; (module = list[i]) != NULL; i++)
713 (module->pp_shortcuts[j] != NULL) && (j < MODULE_SHORTCUT_MAX);
716 if (!strcmp (module->pp_shortcuts[j], psz_shortcut))
718 module_hold (module);
724 module_list_free (list);
729 * Get the configuration of a module
731 * \param module the module
732 * \param psize the size of the configuration returned
733 * \return the configuration as an array
735 module_config_t *module_config_get( const module_t *module, unsigned *restrict psize )
738 unsigned size = module->confsize;
739 module_config_t *config = malloc( size * sizeof( *config ) );
741 assert( psize != NULL );
747 for( i = 0, j = 0; i < size; i++ )
749 const module_config_t *item = module->p_config + i;
750 if( item->b_internal /* internal option */
751 || item->b_unsaveable /* non-modifiable option */
752 || item->b_removed /* removed option */ )
755 memcpy( config + j, item, sizeof( *config ) );
764 * Release the configuration
766 * \param the configuration
769 void module_config_free( module_config_t *config )
774 /*****************************************************************************
775 * Following functions are local.
776 *****************************************************************************/
778 /*****************************************************************************
779 * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
781 *****************************************************************************/
782 static char * copy_next_paths_token( char * paths, char ** remaining_paths )
786 bool escaped = false;
790 /* Alloc a buffer to store the path */
791 path = malloc( strlen( paths ) + 1 );
792 if( !path ) return NULL;
794 /* Look for PATH_SEP_CHAR (a ':' or a ';') */
795 for( i = 0, done = 0 ; paths[i]; i++ )
797 /* Take care of \\ and \: or \; escapement */
801 path[done++] = paths[i];
804 else if( paths[i] == '/' )
807 else if( paths[i] == '\\' )
810 else if( paths[i] == PATH_SEP_CHAR )
813 path[done++] = paths[i];
817 /* Return the remaining paths */
818 if( remaining_paths ) {
819 *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
825 char *psz_vlcpath = NULL;
827 /*****************************************************************************
828 * AllocateAllPlugins: load all plugin modules we can find.
829 *****************************************************************************/
830 #ifdef HAVE_DYNAMIC_PLUGINS
831 static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank )
833 const char *vlcpath = psz_vlcpath;
836 vlc_array_t *arraypaths = vlc_array_new();
837 const bool b_reset = var_InheritBool( p_this, "reset-plugins-cache" );
839 /* Contruct the special search path for system that have a relocatable
840 * executable. Set it to <vlc path>/modules and <vlc path>/plugins. */
842 if( vlcpath && asprintf( &path, "%s" DIR_SEP "modules", vlcpath ) != -1 )
843 vlc_array_append( arraypaths, path );
844 if( vlcpath && asprintf( &path, "%s" DIR_SEP "plugins", vlcpath ) != -1 )
845 vlc_array_append( arraypaths, path );
847 vlc_array_append( arraypaths, strdup( PLUGIN_PATH ) );
850 /* If the user provided a plugin path, we add it to the list */
851 char *userpaths = var_InheritString( p_this, "plugin-path" );
854 for( paths_iter = userpaths; paths_iter; )
856 path = copy_next_paths_token( paths_iter, &paths_iter );
858 vlc_array_append( arraypaths, path );
861 count = vlc_array_count( arraypaths );
862 for( i = 0 ; i < count ; i++ )
864 path = vlc_array_item_at_index( arraypaths, i );
869 CacheDelete( p_this, path );
871 CacheLoad( p_this, p_module_bank, path );
873 msg_Dbg( p_this, "recursively browsing `%s'", path );
875 /* Don't go deeper than 5 subdirectories */
876 AllocatePluginDir( p_this, p_bank, path, 5 );
881 vlc_array_destroy( arraypaths );
885 /*****************************************************************************
886 * AllocatePluginDir: recursively parse a directory to look for plugins
887 *****************************************************************************/
888 static void AllocatePluginDir( vlc_object_t *p_this, module_bank_t *p_bank,
889 const char *psz_dir, unsigned i_maxdepth )
891 if( i_maxdepth == 0 )
894 DIR *dh = utf8_opendir (psz_dir);
898 /* Parse the directory and try to load all files it contains. */
901 char *file = utf8_readdir (dh), *path;
908 if (!strcmp (file, ".") || !strcmp (file, "..")
909 /* Skip directories for unsupported optimizations */
910 || !vlc_CPU_CheckPluginDir (file))
916 const int pathlen = asprintf (&path, "%s"DIR_SEP"%s", psz_dir, file);
918 if (pathlen == -1 || utf8_stat (path, &st))
921 if (S_ISDIR (st.st_mode))
922 /* Recurse into another directory */
923 AllocatePluginDir (p_this, p_bank, path, i_maxdepth - 1);
925 if (S_ISREG (st.st_mode)
926 && strncmp (path, "lib", 3)
927 && ((size_t)pathlen >= sizeof ("_plugin"LIBEXT))
928 && !strncasecmp (path + pathlen - strlen ("_plugin"LIBEXT),
929 "_plugin"LIBEXT, strlen ("_plugni"LIBEXT)))
930 /* ^^ We only load files matching "lib*_plugin"LIBEXT */
931 AllocatePluginFile (p_this, p_bank, path, st.st_mtime, st.st_size);
938 /*****************************************************************************
939 * AllocatePluginFile: load a module into memory and initialize it.
940 *****************************************************************************
941 * This function loads a dynamically loadable module and allocates a structure
942 * for its information data. The module can then be handled by module_need
943 * and module_unneed. It can be removed by DeleteModule.
944 *****************************************************************************/
945 static int AllocatePluginFile( vlc_object_t * p_this, module_bank_t *p_bank,
946 const char *psz_file,
947 int64_t i_file_time, int64_t i_file_size )
949 module_t * p_module = NULL;
950 module_cache_t *p_cache_entry = NULL;
953 * Check our plugins cache first then load plugin if needed
955 p_cache_entry = CacheFind( p_bank, psz_file, i_file_time, i_file_size );
958 p_module = AllocatePlugin( p_this, psz_file );
962 module_config_t *p_item = NULL, *p_end = NULL;
964 p_module = p_cache_entry->p_module;
965 p_module->b_loaded = false;
967 /* If plugin-path contains duplicate entries... */
968 if( p_module->next != NULL )
969 return 0; /* already taken care of that one */
971 /* For now we force loading if the module's config contains
972 * callbacks or actions.
973 * Could be optimized by adding an API call.*/
974 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
975 p_item < p_end; p_item++ )
977 if( p_item->pf_callback || p_item->i_action )
979 p_module = AllocatePlugin( p_this, psz_file );
985 if( p_module == NULL )
988 /* We have not already scanned and inserted this module */
989 assert( p_module->next == NULL );
991 /* Everything worked fine !
992 * The module is ready to be added to the list. */
993 p_module->b_builtin = false;
995 /* msg_Dbg( p_this, "plugin \"%s\", %s",
996 p_module->psz_object_name, p_module->psz_longname ); */
997 p_module->next = p_bank->head;
998 p_bank->head = p_module;
999 assert( p_module->next != NULL ); /* Insertion done */
1001 if( !p_module_bank->b_cache )
1004 /* Add entry to cache */
1005 module_cache_t **pp_cache = p_bank->pp_cache;
1007 pp_cache = realloc_or_free( pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1008 if( pp_cache == NULL )
1010 pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1011 if( pp_cache[p_bank->i_cache] == NULL )
1013 pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1014 pp_cache[p_bank->i_cache]->i_time = i_file_time;
1015 pp_cache[p_bank->i_cache]->i_size = i_file_size;
1016 pp_cache[p_bank->i_cache]->p_module = p_module;
1017 p_bank->pp_cache = pp_cache;
1022 /*****************************************************************************
1023 * AllocatePlugin: load a module into memory and initialize it.
1024 *****************************************************************************
1025 * This function loads a dynamically loadable module and allocates a structure
1026 * for its information data. The module can then be handled by module_need
1027 * and module_unneed. It can be removed by DeleteModule.
1028 *****************************************************************************/
1029 static module_t * AllocatePlugin( vlc_object_t * p_this, const char *psz_file )
1031 module_t * p_module = NULL;
1032 module_handle_t handle;
1034 if( module_Load( p_this, psz_file, &handle ) )
1037 /* Now that we have successfully loaded the module, we can
1038 * allocate a structure for it */
1039 p_module = vlc_module_create( p_this );
1040 if( p_module == NULL )
1042 module_Unload( handle );
1046 p_module->psz_filename = strdup( psz_file );
1047 p_module->handle = handle;
1048 p_module->b_loaded = true;
1050 /* Initialize the module: fill p_module, default config */
1051 if( module_Call( p_this, p_module ) != 0 )
1053 /* We couldn't call module_init() */
1054 free( p_module->psz_filename );
1055 module_release( p_module );
1056 module_Unload( handle );
1060 DupModule( p_module );
1062 /* Everything worked fine ! The module is ready to be added to the list. */
1063 p_module->b_builtin = false;
1068 /*****************************************************************************
1069 * DupModule: make a plugin module standalone.
1070 *****************************************************************************
1071 * This function duplicates all strings in the module, so that the dynamic
1072 * object can be unloaded. It acts recursively on submodules.
1073 *****************************************************************************/
1074 static void DupModule( module_t *p_module )
1078 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1080 *pp_shortcut = strdup( *pp_shortcut );
1083 /* We strdup() these entries so that they are still valid when the
1084 * module is unloaded. */
1085 p_module->psz_capability = strdup( p_module->psz_capability );
1086 p_module->psz_shortname = p_module->psz_shortname ?
1087 strdup( p_module->psz_shortname ) : NULL;
1088 p_module->psz_longname = strdup( p_module->psz_longname );
1089 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1091 p_module->domain = p_module->domain ? strdup( p_module->domain ) : NULL;
1093 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1097 /*****************************************************************************
1098 * UndupModule: free a duplicated module.
1099 *****************************************************************************
1100 * This function frees the allocations done in DupModule().
1101 *****************************************************************************/
1102 static void UndupModule( module_t *p_module )
1106 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1109 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1111 free( *pp_shortcut );
1114 free( p_module->psz_capability );
1115 FREENULL( p_module->psz_shortname );
1116 free( p_module->psz_longname );
1117 FREENULL( p_module->psz_help );
1118 free( p_module->domain );
1121 #endif /* HAVE_DYNAMIC_PLUGINS */
1123 /*****************************************************************************
1124 * AllocateBuiltinModule: initialize a builtin module.
1125 *****************************************************************************
1126 * This function registers a builtin module and allocates a structure
1127 * for its information data. The module can then be handled by module_need
1128 * and module_unneed. It can be removed by DeleteModule.
1129 *****************************************************************************/
1130 static int AllocateBuiltinModule( vlc_object_t * p_this,
1131 int ( *pf_entry ) ( module_t * ) )
1133 module_t * p_module;
1135 /* Now that we have successfully loaded the module, we can
1136 * allocate a structure for it */
1137 p_module = vlc_module_create( p_this );
1138 if( p_module == NULL )
1141 /* Initialize the module : fill p_module->psz_object_name, etc. */
1142 if( pf_entry( p_module ) != 0 )
1144 /* With a well-written module we shouldn't have to print an
1145 * additional error message here, but just make sure. */
1146 msg_Err( p_this, "failed calling entry point in builtin module" );
1147 module_release( p_module );
1151 /* Everything worked fine ! The module is ready to be added to the list. */
1152 p_module->b_builtin = true;
1154 p_module->next = p_module_bank->head;
1155 p_module_bank->head = p_module;
1158 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1159 p_module->psz_object_name, p_module->psz_longname ); */
1164 /*****************************************************************************
1165 * DeleteModule: delete a module and its structure.
1166 *****************************************************************************
1167 * This function can only be called if the module isn't being used.
1168 *****************************************************************************/
1169 static void DeleteModule( module_bank_t *p_bank, module_t * p_module )
1173 /* Unlist the module (if it is in the list) */
1174 module_t **pp_self = &p_bank->head;
1175 while (*pp_self != NULL && *pp_self != p_module)
1176 pp_self = &((*pp_self)->next);
1178 *pp_self = p_module->next;
1180 /* We free the structures that we strdup()ed in Allocate*Module(). */
1181 #ifdef HAVE_DYNAMIC_PLUGINS
1182 if( !p_module->b_builtin )
1184 if( p_module->b_loaded && p_module->b_unloadable )
1186 module_Unload( p_module->handle );
1188 UndupModule( p_module );
1189 free( p_module->psz_filename );
1193 /* Free and detach the object's children */
1194 while (p_module->submodule)
1196 module_t *submodule = p_module->submodule;
1197 p_module->submodule = submodule->next;
1198 module_release (submodule);
1201 config_Free( p_module );
1202 module_release( p_module );