2 This file is part of GNUnet
3 Copyright (C) 2002-2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
18 * @brief Methods to access plugins
19 * @author Christian Grothoff
24 #include "gnunet_util_lib.h"
26 #define LOG(kind,...) GNUNET_log_from (kind, "util-plugin", __VA_ARGS__)
29 * Linked list of active plugins.
34 * This is a linked list.
36 struct PluginList *next;
39 * Name of the library.
51 * Have we been initialized?
53 static int initialized;
56 * Libtool search path before we started.
58 static char *old_dlsearchpath;
61 * List of plugins we have loaded.
63 static struct PluginList *plugins;
67 * Setup libtool paths.
81 _("Initialization of plugin mechanism failed: %s!\n"),
85 opath = lt_dlgetsearchpath ();
87 old_dlsearchpath = GNUNET_strdup (opath);
88 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
93 GNUNET_asprintf (&cpath, "%s:%s", opath, path);
94 lt_dlsetsearchpath (cpath);
100 lt_dlsetsearchpath (path);
113 lt_dlsetsearchpath (old_dlsearchpath);
114 if (NULL != old_dlsearchpath)
116 GNUNET_free (old_dlsearchpath);
117 old_dlsearchpath = NULL;
124 * Lookup a function in the plugin.
126 * @param plug the plugin to check
127 * @param name name of the symbol to look for
128 * @return NULL if the symbol was not found
130 static GNUNET_PLUGIN_Callback
131 resolve_function (struct PluginList *plug,
137 GNUNET_asprintf (&initName,
141 mptr = lt_dlsym (plug->handle, &initName[1]);
143 mptr = lt_dlsym (plug->handle, initName);
145 LOG (GNUNET_ERROR_TYPE_ERROR,
146 _("`%s' failed to resolve method '%s' with error: %s\n"),
148 &initName[1], lt_dlerror ());
149 GNUNET_free (initName);
155 * Test if a plugin exists.
157 * Note that the library must export a symbol called
158 * `library_name_init` for the test to succeed.
160 * @param library_name name of the plugin to test if it is installed
161 * @return #GNUNET_YES if the plugin exists, #GNUNET_NO if not
164 GNUNET_PLUGIN_test (const char *library_name)
167 GNUNET_PLUGIN_Callback init;
168 struct PluginList plug;
172 initialized = GNUNET_YES;
175 libhandle = lt_dlopenext (library_name);
176 if (NULL == libhandle)
178 plug.handle = libhandle;
179 plug.name = (char *) library_name;
180 init = resolve_function (&plug, "init");
184 lt_dlclose (libhandle);
187 lt_dlclose (libhandle);
193 * Setup plugin (runs the `init` callback and returns whatever `init`
194 * returned). If `init` returns NULL, the plugin is unloaded.
196 * Note that the library must export symbols called
197 * `library_name_init` and `library_name_done`. These will be called
198 * when the library is loaded and unloaded respectively.
200 * @param library_name name of the plugin to load
201 * @param arg argument to the plugin initialization function
202 * @return whatever the initialization function returned
205 GNUNET_PLUGIN_load (const char *library_name, void *arg)
208 struct PluginList *plug;
209 GNUNET_PLUGIN_Callback init;
214 initialized = GNUNET_YES;
217 libhandle = lt_dlopenext (library_name);
218 if (libhandle == NULL)
220 LOG (GNUNET_ERROR_TYPE_ERROR,
221 _("`%s' failed for library `%s' with error: %s\n"),
223 library_name, lt_dlerror ());
226 plug = GNUNET_new (struct PluginList);
227 plug->handle = libhandle;
228 plug->name = GNUNET_strdup (library_name);
229 plug->next = plugins;
231 init = resolve_function (plug, "init");
232 if ((init == NULL) || (NULL == (ret = init (arg))))
234 lt_dlclose (libhandle);
235 GNUNET_free (plug->name);
236 plugins = plug->next;
245 * Unload plugin (runs the `done` callback and returns whatever `done`
246 * returned). The plugin is then unloaded.
248 * @param library_name name of the plugin to unload
249 * @param arg argument to the plugin shutdown function
250 * @return whatever the shutdown function returned
253 GNUNET_PLUGIN_unload (const char *library_name,
256 struct PluginList *pos;
257 struct PluginList *prev;
258 GNUNET_PLUGIN_Callback done;
263 while ((NULL != pos) && (0 != strcmp (pos->name, library_name)))
271 done = resolve_function (pos, "done");
278 prev->next = pos->next;
279 lt_dlclose (pos->handle);
280 GNUNET_free (pos->name);
285 initialized = GNUNET_NO;
292 * Closure for #find_libraries().
294 struct LoadAllContext
297 * Prefix the plugin names we find have to match.
299 const char *basename;
302 * Argument to give to 'init' when loading the plugin.
307 * Function to call for each plugin.
309 GNUNET_PLUGIN_LoaderCallback cb;
319 * Function called on each plugin in the directory. Loads
320 * the plugins that match the given basename.
322 * @param cls the `struct LoadAllContext` describing which
323 * plugins to load and what to do with them
324 * @param filename name of a plugin library to check
325 * @return #GNUNET_OK (continue loading)
328 find_libraries (void *cls, const char *filename)
330 struct LoadAllContext *lac = cls;
331 const char *slashpos;
339 while (NULL != (slashpos = strstr (libname, DIR_SEPARATOR_STR)))
340 libname = slashpos + 1;
341 n = strlen (libname);
342 if (0 != strncmp (lac->basename, libname, strlen (lac->basename)))
343 return GNUNET_OK; /* wrong name */
344 if ((n > 3) && (0 == strcmp (&libname[n - 3], ".la")))
345 return GNUNET_OK; /* .la file */
346 basename = GNUNET_strdup (libname);
347 if (NULL != (dot = strstr (basename, ".")))
349 lib_ret = GNUNET_PLUGIN_load (basename, lac->arg);
351 lac->cb (lac->cb_cls, basename, lib_ret);
352 GNUNET_free (basename);
358 * Load all compatible plugins with the given base name.
360 * Note that the library must export symbols called
361 * `basename_ANYTHING_init` and `basename_ANYTHING__done`. These will
362 * be called when the library is loaded and unloaded respectively.
364 * @param basename basename of the plugins to load
365 * @param arg argument to the plugin initialization function
366 * @param cb function to call for each plugin found
367 * @param cb_cls closure for @a cb
370 GNUNET_PLUGIN_load_all (const char *basename, void *arg,
371 GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
373 struct LoadAllContext lac;
376 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
379 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
380 _("Could not determine plugin installation path.\n"));
383 lac.basename = basename;
387 GNUNET_DISK_directory_scan (path, &find_libraries, &lac);
392 /* end of plugin.c */