error handling
[oweals/gnunet.git] / src / util / plugin.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2002-2013 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero 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.
9
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.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file util/plugin.c
23  * @brief Methods to access plugins
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include <ltdl.h>
29 #include "gnunet_util_lib.h"
30
31 #define LOG(kind, ...) GNUNET_log_from (kind, "util-plugin", __VA_ARGS__)
32
33 /**
34  * Linked list of active plugins.
35  */
36 struct PluginList
37 {
38   /**
39    * This is a linked list.
40    */
41   struct PluginList *next;
42
43   /**
44    * Name of the library.
45    */
46   char *name;
47
48   /**
49    * System handle.
50    */
51   void *handle;
52 };
53
54
55 /**
56  * Have we been initialized?
57  */
58 static int initialized;
59
60 /**
61  * Libtool search path before we started.
62  */
63 static char *old_dlsearchpath;
64
65 /**
66  * List of plugins we have loaded.
67  */
68 static struct PluginList *plugins;
69
70
71 /**
72  * Setup libtool paths.
73  */
74 static void
75 plugin_init ()
76 {
77   int err;
78   const char *opath;
79   char *path;
80   char *cpath;
81
82   err = lt_dlinit ();
83   if (err > 0)
84   {
85     fprintf (stderr,
86              _ ("Initialization of plugin mechanism failed: %s!\n"),
87              lt_dlerror ());
88     return;
89   }
90   opath = lt_dlgetsearchpath ();
91   if (NULL != opath)
92     old_dlsearchpath = GNUNET_strdup (opath);
93   path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
94   if (NULL != path)
95   {
96     if (NULL != opath)
97     {
98       GNUNET_asprintf (&cpath, "%s:%s", opath, path);
99       lt_dlsetsearchpath (cpath);
100       GNUNET_free (path);
101       GNUNET_free (cpath);
102     }
103     else
104     {
105       lt_dlsetsearchpath (path);
106       GNUNET_free (path);
107     }
108   }
109 }
110
111
112 /**
113  * Shutdown libtool.
114  */
115 static void
116 plugin_fini ()
117 {
118   lt_dlsetsearchpath (old_dlsearchpath);
119   if (NULL != old_dlsearchpath)
120   {
121     GNUNET_free (old_dlsearchpath);
122     old_dlsearchpath = NULL;
123   }
124   lt_dlexit ();
125 }
126
127
128 /**
129  * Lookup a function in the plugin.
130  *
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
134  */
135 static GNUNET_PLUGIN_Callback
136 resolve_function (struct PluginList *plug, const char *name)
137 {
138   char *initName;
139   void *mptr;
140
141   GNUNET_asprintf (&initName, "_%s_%s", plug->name, name);
142   mptr = lt_dlsym (plug->handle, &initName[1]);
143   if (NULL == mptr)
144     mptr = lt_dlsym (plug->handle, initName);
145   if (NULL == mptr)
146     LOG (GNUNET_ERROR_TYPE_ERROR,
147          _ ("`%s' failed to resolve method '%s' with error: %s\n"),
148          "lt_dlsym",
149          &initName[1],
150          lt_dlerror ());
151   GNUNET_free (initName);
152   return mptr;
153 }
154
155
156 /**
157  * Test if a plugin exists.
158  *
159  * Note that the library must export a symbol called
160  * `library_name_init` for the test to succeed.
161  *
162  * @param library_name name of the plugin to test if it is installed
163  * @return #GNUNET_YES if the plugin exists, #GNUNET_NO if not
164  */
165 int
166 GNUNET_PLUGIN_test (const char *library_name)
167 {
168   void *libhandle;
169   GNUNET_PLUGIN_Callback init;
170   struct PluginList plug;
171
172   if (! initialized)
173   {
174     initialized = GNUNET_YES;
175     plugin_init ();
176   }
177   libhandle = lt_dlopenext (library_name);
178   if (NULL == libhandle)
179     return GNUNET_NO;
180   plug.handle = libhandle;
181   plug.name = (char *) library_name;
182   init = resolve_function (&plug, "init");
183   if (NULL == init)
184   {
185     GNUNET_break (0);
186     lt_dlclose (libhandle);
187     return GNUNET_NO;
188   }
189   lt_dlclose (libhandle);
190   return GNUNET_YES;
191 }
192
193
194 /**
195  * Setup plugin (runs the `init` callback and returns whatever `init`
196  * returned).  If `init` returns NULL, the plugin is unloaded.
197  *
198  * Note that the library must export symbols called
199  * `library_name_init` and `library_name_done`.  These will be called
200  * when the library is loaded and unloaded respectively.
201  *
202  * @param library_name name of the plugin to load
203  * @param arg argument to the plugin initialization function
204  * @return whatever the initialization function returned
205  */
206 void *
207 GNUNET_PLUGIN_load (const char *library_name, void *arg)
208 {
209   void *libhandle;
210   struct PluginList *plug;
211   GNUNET_PLUGIN_Callback init;
212   void *ret;
213
214   if (! initialized)
215   {
216     initialized = GNUNET_YES;
217     plugin_init ();
218   }
219   libhandle = lt_dlopenext (library_name);
220   if (libhandle == NULL)
221   {
222     LOG (GNUNET_ERROR_TYPE_ERROR,
223          _ ("`%s' failed for library `%s' with error: %s\n"),
224          "lt_dlopenext",
225          library_name,
226          lt_dlerror ());
227     return NULL;
228   }
229   plug = GNUNET_new (struct PluginList);
230   plug->handle = libhandle;
231   plug->name = GNUNET_strdup (library_name);
232   plug->next = plugins;
233   plugins = plug;
234   init = resolve_function (plug, "init");
235   if ((init == NULL) || (NULL == (ret = init (arg))))
236   {
237     lt_dlclose (libhandle);
238     GNUNET_free (plug->name);
239     plugins = plug->next;
240     GNUNET_free (plug);
241     return NULL;
242   }
243   return ret;
244 }
245
246
247 /**
248  * Unload plugin (runs the `done` callback and returns whatever `done`
249  * returned).  The plugin is then unloaded.
250  *
251  * @param library_name name of the plugin to unload
252  * @param arg argument to the plugin shutdown function
253  * @return whatever the shutdown function returned
254  */
255 void *
256 GNUNET_PLUGIN_unload (const char *library_name, void *arg)
257 {
258   struct PluginList *pos;
259   struct PluginList *prev;
260   GNUNET_PLUGIN_Callback done;
261   void *ret;
262
263   prev = NULL;
264   pos = plugins;
265   while ((NULL != pos) && (0 != strcmp (pos->name, library_name)))
266   {
267     prev = pos;
268     pos = pos->next;
269   }
270   if (NULL == pos)
271     return NULL;
272
273   done = resolve_function (pos, "done");
274   ret = NULL;
275   if (NULL != done)
276     ret = done (arg);
277   if (NULL == prev)
278     plugins = pos->next;
279   else
280     prev->next = pos->next;
281   lt_dlclose (pos->handle);
282   GNUNET_free (pos->name);
283   GNUNET_free (pos);
284   if (NULL == plugins)
285   {
286     plugin_fini ();
287     initialized = GNUNET_NO;
288   }
289   return ret;
290 }
291
292
293 /**
294  * Closure for #find_libraries().
295  */
296 struct LoadAllContext
297 {
298   /**
299    * Prefix the plugin names we find have to match.
300    */
301   const char *basename;
302
303   /**
304    * Argument to give to 'init' when loading the plugin.
305    */
306   void *arg;
307
308   /**
309    * Function to call for each plugin.
310    */
311   GNUNET_PLUGIN_LoaderCallback cb;
312
313   /**
314    * Closure for @e cb
315    */
316   void *cb_cls;
317 };
318
319
320 /**
321  * Function called on each plugin in the directory.  Loads
322  * the plugins that match the given basename.
323  *
324  * @param cls the `struct LoadAllContext` describing which
325  *            plugins to load and what to do with them
326  * @param filename name of a plugin library to check
327  * @return #GNUNET_OK (continue loading)
328  */
329 static int
330 find_libraries (void *cls, const char *filename)
331 {
332   struct LoadAllContext *lac = cls;
333   const char *slashpos;
334   const char *libname;
335   char *basename;
336   char *dot;
337   void *lib_ret;
338   size_t n;
339
340   libname = filename;
341   while (NULL != (slashpos = strstr (libname, DIR_SEPARATOR_STR)))
342     libname = slashpos + 1;
343   n = strlen (libname);
344   if (0 != strncmp (lac->basename, libname, strlen (lac->basename)))
345     return GNUNET_OK; /* wrong name */
346   if ((n > 3) && (0 == strcmp (&libname[n - 3], ".la")))
347     return GNUNET_OK; /* .la file */
348   basename = GNUNET_strdup (libname);
349   if (NULL != (dot = strstr (basename, ".")))
350     *dot = '\0';
351   lib_ret = GNUNET_PLUGIN_load (basename, lac->arg);
352   if (NULL != lib_ret)
353     lac->cb (lac->cb_cls, basename, lib_ret);
354   GNUNET_free (basename);
355   return GNUNET_OK;
356 }
357
358
359 /**
360  * Load all compatible plugins with the given base name.
361  *
362  * Note that the library must export symbols called
363  * `basename_ANYTHING_init` and `basename_ANYTHING__done`.  These will
364  * be called when the library is loaded and unloaded respectively.
365  *
366  * @param basename basename of the plugins to load
367  * @param arg argument to the plugin initialization function
368  * @param cb function to call for each plugin found
369  * @param cb_cls closure for @a cb
370  */
371 void
372 GNUNET_PLUGIN_load_all (const char *basename,
373                         void *arg,
374                         GNUNET_PLUGIN_LoaderCallback cb,
375                         void *cb_cls)
376 {
377   struct LoadAllContext lac;
378   char *path;
379
380   path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
381   if (NULL == path)
382   {
383     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
384                 _ ("Could not determine plugin installation path.\n"));
385     return;
386   }
387   lac.basename = basename;
388   lac.arg = arg;
389   lac.cb = cb;
390   lac.cb_cls = cb_cls;
391   GNUNET_DISK_directory_scan (path, &find_libraries, &lac);
392   GNUNET_free (path);
393 }
394
395
396 /* end of plugin.c */