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>
34 #include <vlc_modules.h>
37 #include <stdlib.h> /* free(), strtol() */
38 #include <stdio.h> /* sprintf() */
39 #include <string.h> /* strdup() */
46 #include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
57 #include "config/configuration.h"
60 #include "vlc_arrays.h"
62 #include "modules/modules.h"
64 static module_bank_t *p_module_bank = NULL;
65 static vlc_mutex_t module_lock = VLC_STATIC_MUTEX;
67 int vlc_entry__main( module_t * );
69 /*****************************************************************************
71 *****************************************************************************/
72 #ifdef HAVE_DYNAMIC_PLUGINS
73 static void AllocateAllPlugins( vlc_object_t *, module_bank_t * );
74 static void AllocatePluginDir( vlc_object_t *, module_bank_t *, const char *,
76 static int AllocatePluginFile( vlc_object_t *, module_bank_t *, const char *,
78 static module_t * AllocatePlugin( vlc_object_t *, const char * );
80 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
81 static void DeleteModule ( module_bank_t *, module_t * );
82 #ifdef HAVE_DYNAMIC_PLUGINS
83 static void DupModule ( module_t * );
84 static void UndupModule ( module_t * );
87 #undef module_InitBank
91 * Creates a module bank structure which will be filled later
92 * on with all the modules found.
93 * \param p_this vlc object structure
96 void module_InitBank( vlc_object_t *p_this )
98 module_bank_t *p_bank = NULL;
100 vlc_mutex_lock( &module_lock );
102 if( p_module_bank == NULL )
104 p_bank = calloc (1, sizeof(*p_bank));
106 p_bank->i_cache = p_bank->i_loaded_cache = 0;
107 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
108 p_bank->b_cache = p_bank->b_cache_dirty = false;
111 /* Everything worked, attach the object */
112 p_module_bank = p_bank;
114 /* Fills the module bank structure with the main module infos.
115 * This is very useful as it will allow us to consider the main
116 * library just as another module, and for instance the configuration
117 * options of main will be available in the module bank structure just
118 * as for every other module. */
119 AllocateBuiltinModule( p_this, vlc_entry__main );
120 vlc_rwlock_init (&config_lock);
121 config_SortConfig ();
124 p_module_bank->i_usage++;
126 /* We do retain the module bank lock until the plugins are loaded as well.
127 * This is ugly, this staged loading approach is needed: LibVLC gets
128 * some configuration parameters relevant to loading the plugins from
129 * the main (builtin) module. The module bank becomes shared read-only data
130 * once it is ready, so we need to fully serialize initialization.
131 * DO NOT UNCOMMENT the following line unless you managed to squeeze
132 * module_LoadPlugins() before you unlock the mutex. */
133 /*vlc_mutex_unlock( &module_lock );*/
136 #undef module_EndBank
138 * Unloads all unused plugin modules and empties the module
139 * bank in case of success.
140 * \param p_this vlc object structure
143 void module_EndBank( vlc_object_t *p_this, bool b_plugins )
145 module_bank_t *p_bank = p_module_bank;
147 assert (p_bank != NULL);
149 /* Save the configuration */
150 if( !var_InheritBool( p_this, "ignore-config" ) )
151 config_AutoSaveConfigFile( p_this );
153 /* If plugins were _not_ loaded, then the caller still has the bank lock
154 * from module_InitBank(). */
156 vlc_mutex_lock( &module_lock );
158 vlc_assert_locked( &module_lock ); not for static mutexes :( */
160 if( --p_bank->i_usage > 0 )
162 vlc_mutex_unlock( &module_lock );
166 config_UnsortConfig ();
167 vlc_rwlock_destroy (&config_lock);
168 p_module_bank = NULL;
169 vlc_mutex_unlock( &module_lock );
171 #ifdef HAVE_DYNAMIC_PLUGINS
172 while( p_bank->i_loaded_cache-- )
174 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
176 DeleteModule( p_bank,
177 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module );
178 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
179 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
182 free( p_bank->pp_loaded_cache );
183 while( p_bank->i_cache-- )
185 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
186 free( p_bank->pp_cache[p_bank->i_cache] );
188 free( p_bank->pp_cache );
191 while( p_bank->head != NULL )
192 DeleteModule( p_bank, p_bank->head );
197 #undef module_LoadPlugins
199 * Loads module descriptions for all available plugins.
200 * Fills the module bank structure with the plugin modules.
202 * \param p_this vlc object structure
205 void module_LoadPlugins( vlc_object_t * p_this )
207 module_bank_t *p_bank = p_module_bank;
210 /*vlc_assert_locked( &module_lock ); not for static mutexes :( */
212 #ifdef HAVE_DYNAMIC_PLUGINS
213 if( p_bank->i_usage == 1 )
215 msg_Dbg( p_this, "checking plugin modules" );
216 p_module_bank->b_cache = var_InheritBool( p_this, "plugins-cache" );
218 AllocateAllPlugins( p_this, p_module_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 int module_start (vlc_object_t *obj, module_t *m)
329 return m->pf_activate ? (m->pf_activate (obj)) : VLC_SUCCESS;
333 void module_stop (vlc_object_t *obj, module_t *m)
335 if (m->pf_deactivate)
336 m->pf_deactivate (obj);
340 * Frees the flat list of VLC modules.
341 * @param list list obtained by module_list_get()
342 * @param length number of items on the list
345 void module_list_free (module_t **list)
350 for (size_t i = 0; list[i] != NULL; i++)
351 module_release (list[i]);
356 * Gets the flat list of VLC modules.
357 * @param n [OUT] pointer to the number of modules or NULL
358 * @return NULL-terminated table of module pointers
359 * (release with module_list_free()), or NULL in case of error.
361 module_t **module_list_get (size_t *n)
363 /* TODO: this whole module lookup is quite inefficient */
364 /* Remove this and improve module_need */
365 module_t **tab = NULL;
368 assert (p_module_bank);
369 for (module_t *mod = p_module_bank->head; mod; mod = mod->next)
372 nt = realloc (tab, (i + 2 + mod->submodule_count) * sizeof (*tab));
375 module_list_free (tab);
380 tab[i++] = module_hold (mod);
381 for (module_t *subm = mod->submodule; subm; subm = subm->next)
382 tab[i++] = module_hold (subm);
390 typedef struct module_list_t
397 static int modulecmp (const void *a, const void *b)
399 const module_list_t *la = a, *lb = b;
400 /* Note that qsort() uses _ascending_ order,
401 * so the smallest module is the one with the biggest score. */
402 return lb->i_score - la->i_score;
409 * Return the best module function, given a capability list.
411 * \param p_this the vlc object
412 * \param psz_capability list of capabilities needed
413 * \param psz_name name of the module asked
414 * \param b_strict if true, do not fallback to plugin with a different name
415 * but the same capability
416 * \return the module or NULL in case of a failure
418 module_t * module_need( vlc_object_t *p_this, const char *psz_capability,
419 const char *psz_name, bool b_strict )
421 stats_TimerStart( p_this, "module_need()", STATS_TIMER_MODULE_NEED );
423 module_list_t *p_list;
426 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
427 bool b_force_backup = p_this->b_force;
429 /* Deal with variables */
430 if( psz_name && psz_name[0] == '$' )
432 psz_name = psz_var = var_CreateGetString( p_this, psz_name + 1 );
435 /* Count how many different shortcuts were asked for */
436 if( psz_name && *psz_name )
438 char *psz_parser, *psz_last_shortcut;
440 /* If the user wants none, give him none. */
441 if( !strcmp( psz_name, "none" ) )
444 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
445 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
446 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
451 psz_parser = psz_shortcuts = psz_last_shortcut = strdup( psz_name );
453 while( ( psz_parser = strchr( psz_parser, ',' ) ) )
457 psz_last_shortcut = ++psz_parser;
460 /* Check if the user wants to override the "strict" mode */
461 if( psz_last_shortcut )
463 if( !strcmp(psz_last_shortcut, "none") )
468 else if( !strcmp(psz_last_shortcut, "any") )
476 /* Sort the modules and test them */
478 module_t **p_all = module_list_get (&count);
479 p_list = malloc( count * sizeof( module_list_t ) );
481 /* Parse the module list for capabilities and probe each of them */
483 for (size_t i = 0; (p_module = p_all[i]) != NULL; i++)
485 int i_shortcut_bonus = 0;
487 /* Test that this module can do what we need */
488 if( !module_provides( p_module, psz_capability ) )
491 /* If we required a shortcut, check this plugin provides it. */
492 if( i_shortcuts > 0 )
494 const char *name = psz_shortcuts;
496 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
498 for( unsigned i = 0; i < p_module->i_shortcuts; i++ )
501 if( ( c = strchr( name, '@' ) )
502 ? !strncasecmp( name, p_module->pp_shortcuts[i],
504 : !strcasecmp( name, p_module->pp_shortcuts[i] ) )
509 i_shortcut_bonus = i_short * 10000;
514 /* Go to the next shortcut... This is so lame! */
515 name += strlen( name ) + 1;
518 /* If we are in "strict" mode and we couldn't
519 * find the module in the list of provided shortcuts,
520 * then kick the bastard out of here!!! */
525 /* Trash <= 0 scored plugins (they can only be selected by shortcut) */
526 if( p_module->i_score <= 0 )
530 /* Store this new module */
531 p_list[count].p_module = module_hold (p_module);
532 p_list[count].i_score = p_module->i_score + i_shortcut_bonus;
533 p_list[count].b_force = i_shortcut_bonus && b_strict;
537 /* We can release the list, interesting modules are held */
538 module_list_free (p_all);
540 /* Sort candidates by descending score */
541 qsort (p_list, count, sizeof (p_list[0]), modulecmp);
542 msg_Dbg( p_this, "looking for %s module: %zu candidate%s", psz_capability,
543 count, count == 1 ? "" : "s" );
545 /* Parse the linked list and use the first successful module */
547 for (size_t i = 0; (i < count) && (p_module == NULL); i++)
549 module_t *p_cand = p_list[i].p_module;
550 #ifdef HAVE_DYNAMIC_PLUGINS
551 /* Make sure the module is loaded in mem */
552 module_t *p_real = p_cand->b_submodule ? p_cand->parent : p_cand;
554 if( !p_real->b_builtin && !p_real->b_loaded )
556 module_t *p_new_module =
557 AllocatePlugin( p_this, p_real->psz_filename );
558 if( p_new_module == NULL )
559 { /* Corrupted module */
560 msg_Err( p_this, "possibly corrupt module cache" );
561 module_release( p_cand );
564 CacheMerge( p_this, p_real, p_new_module );
565 DeleteModule( p_module_bank, p_new_module );
569 p_this->b_force = p_list[i].b_force;
571 switch( module_start( p_this, p_cand ) )
579 /* good module, but aborted */
580 module_release( p_cand );
583 default: /* bad module */
584 module_release( p_cand );
588 /* Release the remaining modules */
590 module_release (p_list[i].p_module);
594 p_this->b_force = b_force_backup;
596 if( p_module != NULL )
598 msg_Dbg( p_this, "using %s module \"%s\"",
599 psz_capability, p_module->psz_object_name );
600 vlc_object_set_name( p_this, psz_alias ? psz_alias
601 : p_module->psz_object_name );
603 else if( count == 0 )
604 msg_Dbg( p_this, "no %s module matched \"%s\"",
605 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
607 msg_Dbg( p_this, "no %s module matching \"%s\" could be loaded",
608 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
610 free( psz_shortcuts );
613 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
614 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
615 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
617 /* Don't forget that the module is still locked */
625 * This function must be called by the thread that called module_need, to
626 * decrease the reference count and allow for hiding of modules.
627 * \param p_this vlc object structure
628 * \param p_module the module structure
631 void module_unneed( vlc_object_t * p_this, module_t * p_module )
633 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
634 module_stop( p_this, p_module );
635 module_release( p_module );
639 * Get a pointer to a module_t given it's name.
641 * \param psz_name the name of the module
642 * \return a pointer to the module or NULL in case of a failure
644 module_t *module_find( const char * psz_name )
646 module_t **list, *module;
648 list = module_list_get (NULL);
652 for (size_t i = 0; (module = list[i]) != NULL; i++)
654 const char *psz_module_name = module->psz_object_name;
656 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
658 module_hold (module);
662 module_list_free (list);
667 * Tell if a module exists and release it in thic case
669 * \param psz_name th name of the module
670 * \return TRUE if the module exists
672 bool module_exists (const char * psz_name)
674 module_t *p_module = module_find (psz_name);
676 module_release (p_module);
677 return p_module != NULL;
681 * Get a pointer to a module_t that matches a shortcut.
682 * This is a temporary hack for SD. Do not re-use (generally multiple modules
683 * can have the same shortcut, so this is *broken* - use module_need()!).
685 * \param psz_shortcut shortcut of the module
686 * \param psz_cap capability of the module
687 * \return a pointer to the module or NULL in case of a failure
689 module_t *module_find_by_shortcut (const char *psz_shortcut)
691 module_t **list, *module;
693 list = module_list_get (NULL);
697 for (size_t i = 0; (module = list[i]) != NULL; i++)
699 for (size_t j = 0; j < module->i_shortcuts; j++)
701 if (!strcmp (module->pp_shortcuts[j], psz_shortcut))
703 module_hold (module);
709 module_list_free (list);
714 * Get the configuration of a module
716 * \param module the module
717 * \param psize the size of the configuration returned
718 * \return the configuration as an array
720 module_config_t *module_config_get( const module_t *module, unsigned *restrict psize )
723 unsigned size = module->confsize;
724 module_config_t *config = malloc( size * sizeof( *config ) );
726 assert( psize != NULL );
732 for( i = 0, j = 0; i < size; i++ )
734 const module_config_t *item = module->p_config + i;
735 if( item->b_internal /* internal option */
736 || item->b_removed /* removed option */ )
739 memcpy( config + j, item, sizeof( *config ) );
748 * Release the configuration
750 * \param the configuration
753 void module_config_free( module_config_t *config )
758 /*****************************************************************************
759 * Following functions are local.
760 *****************************************************************************/
762 /*****************************************************************************
763 * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
765 *****************************************************************************/
766 static char * copy_next_paths_token( char * paths, char ** remaining_paths )
770 bool escaped = false;
774 /* Alloc a buffer to store the path */
775 path = malloc( strlen( paths ) + 1 );
776 if( !path ) return NULL;
778 /* Look for PATH_SEP_CHAR (a ':' or a ';') */
779 for( i = 0, done = 0 ; paths[i]; i++ )
781 /* Take care of \\ and \: or \; escapement */
785 path[done++] = paths[i];
788 else if( paths[i] == '/' )
791 else if( paths[i] == '\\' )
794 else if( paths[i] == PATH_SEP_CHAR )
797 path[done++] = paths[i];
801 /* Return the remaining paths */
802 if( remaining_paths ) {
803 *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
809 char *psz_vlcpath = NULL;
811 /*****************************************************************************
812 * AllocateAllPlugins: load all plugin modules we can find.
813 *****************************************************************************/
814 #ifdef HAVE_DYNAMIC_PLUGINS
815 static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank )
817 const char *vlcpath = psz_vlcpath;
820 vlc_array_t *arraypaths = vlc_array_new();
821 const bool b_reset = var_InheritBool( p_this, "reset-plugins-cache" );
823 /* Contruct the special search path for system that have a relocatable
824 * executable. Set it to <vlc path>/plugins. */
827 if( asprintf( &path, "%s" DIR_SEP "plugins", vlcpath ) != -1 )
828 vlc_array_append( arraypaths, path );
830 /* If the user provided a plugin path, we add it to the list */
831 char *userpaths = var_InheritString( p_this, "plugin-path" );
834 for( paths_iter = userpaths; paths_iter; )
836 path = copy_next_paths_token( paths_iter, &paths_iter );
838 vlc_array_append( arraypaths, path );
841 count = vlc_array_count( arraypaths );
842 for( i = 0 ; i < count ; i++ )
844 path = vlc_array_item_at_index( arraypaths, i );
848 size_t offset = p_module_bank->i_cache;
850 CacheDelete( p_this, path );
852 CacheLoad( p_this, p_module_bank, path );
854 msg_Dbg( p_this, "recursively browsing `%s'", path );
856 /* Don't go deeper than 5 subdirectories */
857 AllocatePluginDir( p_this, p_bank, path, 5 );
860 CacheSave( p_this, path, p_module_bank->pp_cache + offset,
861 p_module_bank->i_cache - offset );
865 vlc_array_destroy( arraypaths );
869 /*****************************************************************************
870 * AllocatePluginDir: recursively parse a directory to look for plugins
871 *****************************************************************************/
872 static void AllocatePluginDir( vlc_object_t *p_this, module_bank_t *p_bank,
873 const char *psz_dir, unsigned i_maxdepth )
875 if( i_maxdepth == 0 )
878 DIR *dh = vlc_opendir (psz_dir);
882 /* Parse the directory and try to load all files it contains. */
885 char *file = vlc_readdir (dh), *path;
892 if (!strcmp (file, ".") || !strcmp (file, "..")
893 /* Skip directories for unsupported optimizations */
894 || !vlc_CPU_CheckPluginDir (file))
900 const int pathlen = asprintf (&path, "%s"DIR_SEP"%s", psz_dir, file);
902 if (pathlen == -1 || vlc_stat (path, &st))
905 if (S_ISDIR (st.st_mode))
906 /* Recurse into another directory */
907 AllocatePluginDir (p_this, p_bank, path, i_maxdepth - 1);
909 if (S_ISREG (st.st_mode)
910 && strncmp (path, "lib", 3)
911 && ((size_t)pathlen >= sizeof ("_plugin"LIBEXT))
912 && !strncasecmp (path + pathlen - strlen ("_plugin"LIBEXT),
913 "_plugin"LIBEXT, strlen ("_plugni"LIBEXT)))
914 /* ^^ We only load files matching "lib*_plugin"LIBEXT */
915 AllocatePluginFile (p_this, p_bank, path, st.st_mtime, st.st_size);
922 /*****************************************************************************
923 * AllocatePluginFile: load a module into memory and initialize it.
924 *****************************************************************************
925 * This function loads a dynamically loadable module and allocates a structure
926 * for its information data. The module can then be handled by module_need
927 * and module_unneed. It can be removed by DeleteModule.
928 *****************************************************************************/
929 static int AllocatePluginFile( vlc_object_t * p_this, module_bank_t *p_bank,
930 const char *psz_file,
931 int64_t i_file_time, int64_t i_file_size )
933 module_t * p_module = NULL;
934 module_cache_t *p_cache_entry = NULL;
937 * Check our plugins cache first then load plugin if needed
939 p_cache_entry = CacheFind( p_bank, psz_file, i_file_time, i_file_size );
942 p_module = AllocatePlugin( p_this, psz_file );
946 module_config_t *p_item = NULL, *p_end = NULL;
948 p_module = p_cache_entry->p_module;
949 p_module->b_loaded = false;
951 /* If plugin-path contains duplicate entries... */
952 if( p_module->next != NULL )
953 return 0; /* already taken care of that one */
955 /* For now we force loading if the module's config contains
956 * callbacks or actions.
957 * Could be optimized by adding an API call.*/
958 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
959 p_item < p_end; p_item++ )
961 if( p_item->pf_callback || p_item->i_action )
963 p_module = AllocatePlugin( p_this, psz_file );
969 if( p_module == NULL )
972 /* We have not already scanned and inserted this module */
973 assert( p_module->next == NULL );
975 /* Everything worked fine !
976 * The module is ready to be added to the list. */
977 p_module->b_builtin = false;
979 /* msg_Dbg( p_this, "plugin \"%s\", %s",
980 p_module->psz_object_name, p_module->psz_longname ); */
981 p_module->next = p_bank->head;
982 p_bank->head = p_module;
983 assert( p_module->next != NULL ); /* Insertion done */
985 if( !p_module_bank->b_cache )
988 /* Add entry to cache */
989 module_cache_t **pp_cache = p_bank->pp_cache;
991 pp_cache = realloc_or_free( pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
992 if( pp_cache == NULL )
994 pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
995 if( pp_cache[p_bank->i_cache] == NULL )
997 pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
998 pp_cache[p_bank->i_cache]->i_time = i_file_time;
999 pp_cache[p_bank->i_cache]->i_size = i_file_size;
1000 pp_cache[p_bank->i_cache]->p_module = p_module;
1001 p_bank->pp_cache = pp_cache;
1006 /*****************************************************************************
1007 * AllocatePlugin: load a module into memory and initialize it.
1008 *****************************************************************************
1009 * This function loads a dynamically loadable module and allocates a structure
1010 * for its information data. The module can then be handled by module_need
1011 * and module_unneed. It can be removed by DeleteModule.
1012 *****************************************************************************/
1013 static module_t * AllocatePlugin( vlc_object_t * p_this, const char *psz_file )
1015 module_t * p_module = NULL;
1016 module_handle_t handle;
1018 if( module_Load( p_this, psz_file, &handle ) )
1021 /* Now that we have successfully loaded the module, we can
1022 * allocate a structure for it */
1023 p_module = vlc_module_create( p_this );
1024 if( p_module == NULL )
1026 module_Unload( handle );
1030 p_module->psz_filename = strdup( psz_file );
1031 p_module->handle = handle;
1032 p_module->b_loaded = true;
1034 /* Initialize the module: fill p_module, default config */
1035 if( module_Call( p_this, p_module ) != 0 )
1037 /* We couldn't call module_init() */
1038 free( p_module->psz_filename );
1039 module_release( p_module );
1040 module_Unload( handle );
1044 DupModule( p_module );
1046 /* Everything worked fine ! The module is ready to be added to the list. */
1047 p_module->b_builtin = false;
1052 /*****************************************************************************
1053 * DupModule: make a plugin module standalone.
1054 *****************************************************************************
1055 * This function duplicates all strings in the module, so that the dynamic
1056 * object can be unloaded. It acts recursively on submodules.
1057 *****************************************************************************/
1058 static void DupModule( module_t *p_module )
1060 char **pp_shortcuts = p_module->pp_shortcuts;
1061 for( unsigned i = 0; i < p_module->i_shortcuts; i++ )
1062 pp_shortcuts[i] = strdup( p_module->pp_shortcuts[i] );
1064 /* We strdup() these entries so that they are still valid when the
1065 * module is unloaded. */
1066 p_module->psz_capability = strdup( p_module->psz_capability );
1067 p_module->psz_shortname = p_module->psz_shortname ?
1068 strdup( p_module->psz_shortname ) : NULL;
1069 p_module->psz_longname = strdup( p_module->psz_longname );
1070 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1072 p_module->domain = p_module->domain ? strdup( p_module->domain ) : NULL;
1074 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1078 /*****************************************************************************
1079 * UndupModule: free a duplicated module.
1080 *****************************************************************************
1081 * This function frees the allocations done in DupModule().
1082 *****************************************************************************/
1083 static void UndupModule( module_t *p_module )
1085 char **pp_shortcuts = p_module->pp_shortcuts;
1087 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1090 for( unsigned i = 0; i < p_module->i_shortcuts; i++ )
1091 free( pp_shortcuts[i] );
1093 free( p_module->psz_capability );
1094 FREENULL( p_module->psz_shortname );
1095 free( p_module->psz_longname );
1096 FREENULL( p_module->psz_help );
1097 free( p_module->domain );
1100 #endif /* HAVE_DYNAMIC_PLUGINS */
1102 /*****************************************************************************
1103 * AllocateBuiltinModule: initialize a builtin module.
1104 *****************************************************************************
1105 * This function registers a builtin module and allocates a structure
1106 * for its information data. The module can then be handled by module_need
1107 * and module_unneed. It can be removed by DeleteModule.
1108 *****************************************************************************/
1109 static int AllocateBuiltinModule( vlc_object_t * p_this,
1110 int ( *pf_entry ) ( module_t * ) )
1112 module_t * p_module;
1114 /* Now that we have successfully loaded the module, we can
1115 * allocate a structure for it */
1116 p_module = vlc_module_create( p_this );
1117 if( p_module == NULL )
1120 /* Initialize the module : fill p_module->psz_object_name, etc. */
1121 if( pf_entry( p_module ) != 0 )
1123 /* With a well-written module we shouldn't have to print an
1124 * additional error message here, but just make sure. */
1125 msg_Err( p_this, "failed calling entry point in builtin module" );
1126 module_release( p_module );
1130 /* Everything worked fine ! The module is ready to be added to the list. */
1131 p_module->b_builtin = true;
1133 p_module->next = p_module_bank->head;
1134 p_module_bank->head = p_module;
1137 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1138 p_module->psz_object_name, p_module->psz_longname ); */
1143 /*****************************************************************************
1144 * DeleteModule: delete a module and its structure.
1145 *****************************************************************************
1146 * This function can only be called if the module isn't being used.
1147 *****************************************************************************/
1148 static void DeleteModule( module_bank_t *p_bank, module_t * p_module )
1152 /* Unlist the module (if it is in the list) */
1153 module_t **pp_self = &p_bank->head;
1154 while (*pp_self != NULL && *pp_self != p_module)
1155 pp_self = &((*pp_self)->next);
1157 *pp_self = p_module->next;
1159 /* We free the structures that we strdup()ed in Allocate*Module(). */
1160 #ifdef HAVE_DYNAMIC_PLUGINS
1161 if( !p_module->b_builtin )
1163 if( p_module->b_loaded && p_module->b_unloadable )
1165 module_Unload( p_module->handle );
1167 UndupModule( p_module );
1168 free( p_module->psz_filename );
1172 /* Free and detach the object's children */
1173 while (p_module->submodule)
1175 module_t *submodule = p_module->submodule;
1176 p_module->submodule = submodule->next;
1177 module_release (submodule);
1180 config_Free( p_module );
1181 module_release( p_module );