1 /*****************************************************************************
2 * bank.c : Modules list
3 *****************************************************************************
4 * Copyright (C) 2001-2011 the VideoLAN team
6 * Authors: Sam Hocevar <sam@zoy.org>
7 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
8 * Hans-Peter Jansen <hpj@urpla.net>
9 * Gildas Bazin <gbazin@videolan.org>
10 * RĂ©mi Denis-Courmont
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 *****************************************************************************/
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
44 #include <vlc_common.h>
45 #include <vlc_plugin.h>
46 #include <vlc_modules.h>
49 #include "config/configuration.h"
50 #include "modules/modules.h"
57 } modules = { VLC_STATIC_MUTEX, NULL, 0 };
59 /*****************************************************************************
61 *****************************************************************************/
62 #ifdef HAVE_DYNAMIC_PLUGINS
63 static void AllocateAllPlugins (vlc_object_t *);
65 static module_t *module_InitStatic (vlc_plugin_cb);
67 static void module_StoreBank (module_t *module)
69 /*vlc_assert_locked (&modules.lock);*/
70 module->next = modules.head;
71 modules.head = module;
78 # pragma weak vlc_static_modules
80 extern vlc_plugin_cb vlc_static_modules[];
82 static void module_InitStaticModules(void)
84 if (!vlc_static_modules)
87 for (unsigned i = 0; vlc_static_modules[i]; i++) {
88 module_t *module = module_InitStatic (vlc_static_modules[i]);
89 if (likely(module != NULL))
90 module_StoreBank (module);
94 static void module_InitStaticModules(void) { }
100 * Creates a module bank structure which will be filled later
101 * on with all the modules found.
103 void module_InitBank (void)
105 vlc_mutex_lock (&modules.lock);
107 if (modules.usage == 0)
109 /* Fills the module bank structure with the main module infos.
110 * This is very useful as it will allow us to consider the main
111 * library just as another module, and for instance the configuration
112 * options of main will be available in the module bank structure just
113 * as for every other module. */
114 module_t *module = module_InitStatic (vlc_entry__main);
115 if (likely(module != NULL))
116 module_StoreBank (module);
118 module_InitStaticModules();
119 config_SortConfig ();
123 /* We do retain the module bank lock until the plugins are loaded as well.
124 * This is ugly, this staged loading approach is needed: LibVLC gets
125 * some configuration parameters relevant to loading the plugins from
126 * the main (builtin) module. The module bank becomes shared read-only data
127 * once it is ready, so we need to fully serialize initialization.
128 * DO NOT UNCOMMENT the following line unless you managed to squeeze
129 * module_LoadPlugins() before you unlock the mutex. */
130 /*vlc_mutex_unlock (&modules.lock);*/
134 * Unloads all unused plugin modules and empties the module
135 * bank in case of success.
137 void module_EndBank (bool b_plugins)
139 module_t *head = NULL;
141 /* If plugins were _not_ loaded, then the caller still has the bank lock
142 * from module_InitBank(). */
144 vlc_mutex_lock (&modules.lock);
146 vlc_assert_locked (&modules.lock); not for static mutexes :( */
148 assert (modules.usage > 0);
149 if (--modules.usage == 0)
151 config_UnsortConfig ();
155 vlc_mutex_unlock (&modules.lock);
159 module_t *module = head;
162 #ifdef HAVE_DYNAMIC_PLUGINS
163 if (module->b_loaded && module->b_unloadable)
165 module_Unload (module->handle);
166 module->b_loaded = false;
169 vlc_module_destroy (module);
173 #undef module_LoadPlugins
175 * Loads module descriptions for all available plugins.
176 * Fills the module bank structure with the plugin modules.
178 * \param p_this vlc object structure
179 * \return total number of modules in bank after loading all plug-ins
181 size_t module_LoadPlugins (vlc_object_t *obj)
183 /*vlc_assert_locked (&modules.lock); not for static mutexes :( */
185 #ifdef HAVE_DYNAMIC_PLUGINS
186 if (modules.usage == 1)
188 msg_Dbg (obj, "searching plug-in modules");
189 AllocateAllPlugins (obj);
190 config_UnsortConfig ();
191 config_SortConfig ();
194 vlc_mutex_unlock (&modules.lock);
197 module_t **list = module_list_get (&count);
198 module_list_free (list);
199 msg_Dbg (obj, "plug-ins loaded: %zu modules", count);
204 * Frees the flat list of VLC modules.
205 * @param list list obtained by module_list_get()
206 * @param length number of items on the list
209 void module_list_free (module_t **list)
215 * Gets the flat list of VLC modules.
216 * @param n [OUT] pointer to the number of modules or NULL
217 * @return NULL-terminated table of module pointers
218 * (release with module_list_free()), or NULL in case of error.
220 module_t **module_list_get (size_t *n)
222 /* TODO: this whole module lookup is quite inefficient */
223 /* Remove this and improve module_need */
224 module_t **tab = NULL;
227 for (module_t *mod = modules.head; mod; mod = mod->next)
230 nt = realloc (tab, (i + 2 + mod->submodule_count) * sizeof (*tab));
233 module_list_free (tab);
239 for (module_t *subm = mod->submodule; subm; subm = subm->next)
248 char *psz_vlcpath = NULL;
250 #ifdef HAVE_DYNAMIC_PLUGINS
251 typedef enum { CACHE_USE, CACHE_RESET, CACHE_IGNORE } cache_mode_t;
253 static void AllocatePluginPath (vlc_object_t *, const char *, cache_mode_t);
256 * Enumerates all dynamic plug-ins that can be found.
258 * This function will recursively browse the default plug-ins directory and any
259 * directory listed in the VLC_PLUGIN_PATH environment variable.
260 * For performance reasons, a cache is normally used so that plug-in shared
261 * objects do not need to loaded and linked into the process.
263 static void AllocateAllPlugins (vlc_object_t *p_this)
265 const char *vlcpath = psz_vlcpath;
269 if( !var_InheritBool( p_this, "plugins-cache" ) )
271 else if( var_InheritBool( p_this, "reset-plugins-cache" ) )
276 /* Contruct the special search path for system that have a relocatable
277 * executable. Set it to <vlc path>/plugins. */
280 if( asprintf( &paths, "%s" DIR_SEP "plugins", vlcpath ) != -1 )
282 AllocatePluginPath (p_this, paths, mode);
286 /* If the user provided a plugin path, we add it to the list */
287 paths = getenv( "VLC_PLUGIN_PATH" );
291 paths = strdup( paths ); /* don't harm the environment ! :) */
292 if( unlikely(paths == NULL) )
295 for( char *buf, *path = strtok_r( paths, PATH_SEP, &buf );
297 path = strtok_r( NULL, PATH_SEP, &buf ) )
298 AllocatePluginPath (p_this, path, mode);
303 typedef struct module_bank
310 module_cache_t *cache;
313 module_cache_t *loaded_cache;
316 static void AllocatePluginDir (module_bank_t *, unsigned,
317 const char *, const char *);
320 * Scans for plug-ins within a file system hierarchy.
321 * \param path base directory to browse
323 static void AllocatePluginPath (vlc_object_t *p_this, const char *path,
327 module_cache_t *cache = NULL;
333 count = CacheLoad( p_this, path, &cache );
336 CacheDelete( p_this, path );
339 msg_Dbg( p_this, "ignoring plugins cache file" );
342 msg_Dbg( p_this, "recursively browsing `%s'", path );
349 bank.loaded_cache = cache;
350 bank.i_loaded_cache = count;
352 /* Don't go deeper than 5 subdirectories */
353 AllocatePluginDir (&bank, 5, path, NULL);
358 /* Discard unmatched cache entries */
359 for( size_t i = 0; i < count; i++ )
361 if (cache[i].p_module != NULL)
362 vlc_module_destroy (cache[i].p_module);
363 free (cache[i].path);
367 CacheSave (p_this, path, bank.cache, bank.i_cache);
373 static int AllocatePluginFile (module_bank_t *, const char *,
374 const char *, const struct stat *);
377 * Recursively browses a directory to look for plug-ins.
379 static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth,
380 const char *absdir, const char *reldir)
386 DIR *dh = vlc_opendir (absdir);
390 /* Parse the directory and try to load all files it contains. */
393 char *file = vlc_readdir (dh), *relpath = NULL, *abspath = NULL;
398 if (!strcmp (file, ".") || !strcmp (file, ".."))
401 /* Compute path relative to plug-in base directory */
404 if (asprintf (&relpath, "%s"DIR_SEP"%s", reldir, file) == -1)
408 relpath = strdup (file);
409 if (unlikely(relpath == NULL))
412 /* Compute absolute path */
413 if (asprintf (&abspath, "%s"DIR_SEP"%s", bank->base, relpath) == -1)
420 if (vlc_stat (abspath, &st) == -1)
423 if (S_ISREG (st.st_mode))
425 static const char prefix[] = "lib";
426 static const char suffix[] = "_plugin"LIBEXT;
427 size_t len = strlen (file);
429 /* Check that file matches the "lib*_plugin"LIBEXT pattern */
430 if (len > strlen (suffix)
431 && !strncmp (file, prefix, strlen (prefix))
432 && !strcmp (file + len - strlen (suffix), suffix))
433 AllocatePluginFile (bank, abspath, relpath, &st);
435 else if (S_ISDIR (st.st_mode))
436 /* Recurse into another directory */
437 AllocatePluginDir (bank, maxdepth, abspath, relpath);
446 static module_t *module_InitDynamic (vlc_object_t *, const char *, bool);
449 * Scans a plug-in from a file.
451 static int AllocatePluginFile (module_bank_t *bank, const char *abspath,
452 const char *relpath, const struct stat *st)
454 module_t *module = NULL;
456 /* Check our plugins cache first then load plugin if needed */
457 if (bank->mode == CACHE_USE)
459 module = CacheFind (bank->loaded_cache, bank->i_loaded_cache,
463 module->psz_filename = strdup (abspath);
464 if (unlikely(module->psz_filename == NULL))
466 vlc_module_destroy (module);
472 module = module_InitDynamic (bank->obj, abspath, true);
476 /* We have not already scanned and inserted this module */
477 assert (module->next == NULL);
479 /* Unload plugin until we really need it */
480 if (module->b_loaded && module->b_unloadable)
482 module_Unload (module->handle);
483 module->b_loaded = false;
486 /* For now we force loading if the module's config contains
487 * callbacks or actions.
488 * Could be optimized by adding an API call.*/
489 for (size_t n = module->confsize, i = 0; i < n; i++)
490 if (module->p_config[i].i_action)
492 /* !unloadable not allowed for plugins with callbacks */
493 vlc_module_destroy (module);
494 module = module_InitDynamic (bank->obj, abspath, false);
498 module_StoreBank (module);
500 if (bank->mode != CACHE_IGNORE) /* Add entry to cache */
501 CacheAdd (&bank->cache, &bank->i_cache, relpath, st, module);
502 /* TODO: deal with errors */
507 * Loads a dynamically-linked plug-in into memory and initialize it.
509 * The module can then be handled by module_need() and module_unneed().
511 * \param path file path of the shared object
512 * \param fast whether to optimize loading for speed or safety
513 * (fast is used when the plug-in is registered but not used)
515 static module_t *module_InitDynamic (vlc_object_t *obj, const char *path,
518 module_handle_t handle;
520 if (module_Load (obj, path, &handle, fast))
523 /* Try to resolve the symbol */
524 static const char entry_name[] = "vlc_entry" MODULE_SUFFIX;
525 vlc_plugin_cb entry =
526 (vlc_plugin_cb) module_Lookup (handle, entry_name);
529 msg_Warn (obj, "cannot find plug-in entry point in %s", path);
533 /* We can now try to call the symbol */
534 module_t *module = vlc_plugin_describe (entry);
535 if (unlikely(module == NULL))
537 /* With a well-written module we shouldn't have to print an
538 * additional error message here, but just make sure. */
539 msg_Err (obj, "cannot initialize plug-in %s", path);
543 module->psz_filename = strdup (path);
544 if (unlikely(module->psz_filename == NULL))
546 vlc_module_destroy (module);
549 module->handle = handle;
550 module->b_loaded = true;
553 module_Unload( handle );
556 #endif /* HAVE_DYNAMIC_PLUGINS */
559 * Registers a statically-linked plug-in.
561 static module_t *module_InitStatic (vlc_plugin_cb entry)
563 /* Initializes the module */
564 module_t *module = vlc_plugin_describe (entry);
565 if (unlikely(module == NULL))
568 module->b_loaded = true;
569 module->b_unloadable = false;
574 * Makes sure the module is loaded in memory.
575 * \return 0 on success, -1 on failure
577 int module_Map (vlc_object_t *obj, module_t *module)
579 if (module->parent != NULL)
580 module = module->parent;
582 #warning FIXME: race condition!
583 if (module->b_loaded)
585 assert (module->psz_filename != NULL);
587 #ifdef HAVE_DYNAMIC_PLUGINS
588 module_t *uncache = module_InitDynamic (obj, module->psz_filename, false);
591 CacheMerge (obj, module, uncache);
592 vlc_module_destroy (uncache);
596 msg_Err (obj, "corrupt module: %s", module->psz_filename);