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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 * @brief Methods to access plugins
24 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util-plugin", __VA_ARGS__)
34 * Linked list of active plugins.
39 * This is a linked list.
41 struct PluginList *next;
44 * Name of the library.
56 * Have we been initialized?
58 static int initialized;
61 * Libtool search path before we started.
63 static char *old_dlsearchpath;
66 * List of plugins we have loaded.
68 static struct PluginList *plugins;
72 * Setup libtool paths.
86 _("Initialization of plugin mechanism failed: %s!\n"),
90 opath = lt_dlgetsearchpath ();
92 old_dlsearchpath = GNUNET_strdup (opath);
93 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
98 GNUNET_asprintf (&cpath, "%s:%s", opath, path);
99 lt_dlsetsearchpath (cpath);
105 lt_dlsetsearchpath (path);
118 lt_dlsetsearchpath (old_dlsearchpath);
119 if (NULL != old_dlsearchpath)
121 GNUNET_free (old_dlsearchpath);
122 old_dlsearchpath = NULL;
129 * Lookup a function in the plugin.
131 * @param plug the plugin to check
132 * @param name name of the symbol to look for
133 * @return NULL if the symbol was not found
135 static GNUNET_PLUGIN_Callback
136 resolve_function (struct PluginList *plug,
142 GNUNET_asprintf (&initName,
146 mptr = lt_dlsym (plug->handle, &initName[1]);
148 mptr = lt_dlsym (plug->handle, initName);
150 LOG (GNUNET_ERROR_TYPE_ERROR,
151 _("`%s' failed to resolve method '%s' with error: %s\n"),
153 &initName[1], lt_dlerror ());
154 GNUNET_free (initName);
160 * Test if a plugin exists.
162 * Note that the library must export a symbol called
163 * `library_name_init` for the test to succeed.
165 * @param library_name name of the plugin to test if it is installed
166 * @return #GNUNET_YES if the plugin exists, #GNUNET_NO if not
169 GNUNET_PLUGIN_test (const char *library_name)
172 GNUNET_PLUGIN_Callback init;
173 struct PluginList plug;
177 initialized = GNUNET_YES;
180 libhandle = lt_dlopenext (library_name);
181 if (NULL == libhandle)
183 plug.handle = libhandle;
184 plug.name = (char *) library_name;
185 init = resolve_function (&plug, "init");
189 lt_dlclose (libhandle);
192 lt_dlclose (libhandle);
198 * Setup plugin (runs the `init` callback and returns whatever `init`
199 * returned). If `init` returns NULL, the plugin is unloaded.
201 * Note that the library must export symbols called
202 * `library_name_init` and `library_name_done`. These will be called
203 * when the library is loaded and unloaded respectively.
205 * @param library_name name of the plugin to load
206 * @param arg argument to the plugin initialization function
207 * @return whatever the initialization function returned
210 GNUNET_PLUGIN_load (const char *library_name, void *arg)
213 struct PluginList *plug;
214 GNUNET_PLUGIN_Callback init;
219 initialized = GNUNET_YES;
222 libhandle = lt_dlopenext (library_name);
223 if (libhandle == NULL)
225 LOG (GNUNET_ERROR_TYPE_ERROR,
226 _("`%s' failed for library `%s' with error: %s\n"),
228 library_name, lt_dlerror ());
231 plug = GNUNET_new (struct PluginList);
232 plug->handle = libhandle;
233 plug->name = GNUNET_strdup (library_name);
234 plug->next = plugins;
236 init = resolve_function (plug, "init");
237 if ((init == NULL) || (NULL == (ret = init (arg))))
239 lt_dlclose (libhandle);
240 GNUNET_free (plug->name);
241 plugins = plug->next;
250 * Unload plugin (runs the `done` callback and returns whatever `done`
251 * returned). The plugin is then unloaded.
253 * @param library_name name of the plugin to unload
254 * @param arg argument to the plugin shutdown function
255 * @return whatever the shutdown function returned
258 GNUNET_PLUGIN_unload (const char *library_name,
261 struct PluginList *pos;
262 struct PluginList *prev;
263 GNUNET_PLUGIN_Callback done;
268 while ((NULL != pos) && (0 != strcmp (pos->name, library_name)))
276 done = resolve_function (pos, "done");
283 prev->next = pos->next;
284 lt_dlclose (pos->handle);
285 GNUNET_free (pos->name);
290 initialized = GNUNET_NO;
297 * Closure for #find_libraries().
299 struct LoadAllContext
302 * Prefix the plugin names we find have to match.
304 const char *basename;
307 * Argument to give to 'init' when loading the plugin.
312 * Function to call for each plugin.
314 GNUNET_PLUGIN_LoaderCallback cb;
324 * Function called on each plugin in the directory. Loads
325 * the plugins that match the given basename.
327 * @param cls the `struct LoadAllContext` describing which
328 * plugins to load and what to do with them
329 * @param filename name of a plugin library to check
330 * @return #GNUNET_OK (continue loading)
333 find_libraries (void *cls, const char *filename)
335 struct LoadAllContext *lac = cls;
336 const char *slashpos;
344 while (NULL != (slashpos = strstr (libname, DIR_SEPARATOR_STR)))
345 libname = slashpos + 1;
346 n = strlen (libname);
347 if (0 != strncmp (lac->basename, libname, strlen (lac->basename)))
348 return GNUNET_OK; /* wrong name */
349 if ((n > 3) && (0 == strcmp (&libname[n - 3], ".la")))
350 return GNUNET_OK; /* .la file */
351 basename = GNUNET_strdup (libname);
352 if (NULL != (dot = strstr (basename, ".")))
354 lib_ret = GNUNET_PLUGIN_load (basename, lac->arg);
356 lac->cb (lac->cb_cls, basename, lib_ret);
357 GNUNET_free (basename);
363 * Load all compatible plugins with the given base name.
365 * Note that the library must export symbols called
366 * `basename_ANYTHING_init` and `basename_ANYTHING__done`. These will
367 * be called when the library is loaded and unloaded respectively.
369 * @param basename basename of the plugins to load
370 * @param arg argument to the plugin initialization function
371 * @param cb function to call for each plugin found
372 * @param cb_cls closure for @a cb
375 GNUNET_PLUGIN_load_all (const char *basename, void *arg,
376 GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
378 struct LoadAllContext lac;
381 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
385 _("Could not determine plugin installation path.\n"));
388 lac.basename = basename;
392 GNUNET_DISK_directory_scan (path, &find_libraries, &lac);
397 /* end of plugin.c */