indentation
[oweals/gnunet.git] / src / util / plugin.c
1 /*
2      This file is part of GNUnet
3      (C) 2002, 2003, 2004, 2005, 2006, 2009 Christian Grothoff (and other contributing authors)
4
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 2, or (at your
8      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      General Public License for more details.
14
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
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_common.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_plugin_lib.h"
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 /**
62  * Libtool search path before we started.
63  */
64 static char *old_dlsearchpath;
65
66
67 /**
68  * List of plugins we have loaded.
69  */
70 static struct PluginList *plugins;
71
72
73 /**
74  * Setup libtool paths.
75  */
76 static void
77 plugin_init ()
78 {
79   int err;
80   const char *opath;
81   char *path;
82   char *cpath;
83
84   err = lt_dlinit ();
85   if (err > 0)
86   {
87     fprintf (stderr,
88              _("Initialization of plugin mechanism failed: %s!\n"),
89              lt_dlerror ());
90     return;
91   }
92   opath = lt_dlgetsearchpath ();
93   if (opath != NULL)
94     old_dlsearchpath = GNUNET_strdup (opath);
95   path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
96   if (path != NULL)
97   {
98     if (opath != NULL)
99     {
100       GNUNET_asprintf (&cpath, "%s:%s", opath, path);
101       lt_dlsetsearchpath (cpath);
102       GNUNET_free (path);
103       GNUNET_free (cpath);
104     }
105     else
106     {
107       lt_dlsetsearchpath (path);
108       GNUNET_free (path);
109     }
110   }
111 }
112
113
114 /**
115  * Shutdown libtool.
116  */
117 static void
118 plugin_fini ()
119 {
120   lt_dlsetsearchpath (old_dlsearchpath);
121   if (old_dlsearchpath != NULL)
122   {
123     GNUNET_free (old_dlsearchpath);
124     old_dlsearchpath = NULL;
125   }
126   lt_dlexit ();
127 }
128
129
130 /**
131  * Lookup a function in the plugin.
132  */
133 static GNUNET_PLUGIN_Callback
134 resolve_function (struct PluginList *plug, const char *name)
135 {
136   char *initName;
137   void *mptr;
138
139   GNUNET_asprintf (&initName, "_%s_%s", plug->name, name);
140   mptr = lt_dlsym (plug->handle, &initName[1]);
141   if (mptr == NULL)
142     mptr = lt_dlsym (plug->handle, initName);
143   if (mptr == NULL)
144     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
145                 _("`%s' failed to resolve method '%s' with error: %s\n"),
146                 "lt_dlsym", &initName[1], lt_dlerror ());
147   GNUNET_free (initName);
148   return mptr;
149 }
150
151 /**
152  * Test if a plugin exists.
153  *
154  * Note that the library must export a symbol called
155  * "library_name_init" for the test to succeed. 
156  *
157  * @param library_name name of the plugin to test if it is installed
158  * @return GNUNET_YES if the plugin exists, GNUNET_NO if not
159  */
160 int
161 GNUNET_PLUGIN_test (const char *library_name)
162 {
163   void *libhandle;
164   GNUNET_PLUGIN_Callback init;
165   struct PluginList plug;
166
167   if (!initialized)
168   {
169     initialized = GNUNET_YES;
170     plugin_init ();
171   }
172   libhandle = lt_dlopenext (library_name);
173   if (libhandle == NULL)
174     return GNUNET_NO;
175   plug.handle = libhandle;
176   plug.name = (char *) library_name;
177   init = resolve_function (&plug, "init");
178   if (init == NULL)
179   {
180     GNUNET_break (0);
181     lt_dlclose (libhandle);
182     return GNUNET_NO;
183   }
184   lt_dlclose (libhandle);
185   return GNUNET_YES;
186 }
187
188
189 /**
190  * Setup plugin (runs the "init" callback and returns whatever "init"
191  * returned).  If "init" returns NULL, the plugin is unloaded.
192  *
193  * Note that the library must export symbols called
194  * "library_name_init" and "library_name_done".  These will be called
195  * when the library is loaded and unloaded respectively.
196  *
197  * @param library_name name of the plugin to load
198  * @param arg argument to the plugin initialization function
199  * @return whatever the initialization function returned
200  */
201 void *
202 GNUNET_PLUGIN_load (const char *library_name, void *arg)
203 {
204   void *libhandle;
205   struct PluginList *plug;
206   GNUNET_PLUGIN_Callback init;
207   void *ret;
208
209   if (!initialized)
210   {
211     initialized = GNUNET_YES;
212     plugin_init ();
213   }
214   libhandle = lt_dlopenext (library_name);
215   if (libhandle == NULL)
216   {
217     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218                 _("`%s' failed for library `%s' with error: %s\n"),
219                 "lt_dlopenext", library_name, lt_dlerror ());
220     return NULL;
221   }
222   plug = GNUNET_malloc (sizeof (struct PluginList));
223   plug->handle = libhandle;
224   plug->name = GNUNET_strdup (library_name);
225   plug->next = plugins;
226   plugins = plug;
227   init = resolve_function (plug, "init");
228   if ((init == NULL) || (NULL == (ret = init (arg))))
229   {
230     lt_dlclose (libhandle);
231     GNUNET_free (plug->name);
232     plugins = plug->next;
233     GNUNET_free (plug);
234     return NULL;
235   }
236   return ret;
237 }
238
239
240 /**
241  * Unload plugin (runs the "done" callback and returns whatever "done"
242  * returned).  The plugin is then unloaded.
243  *
244  * @param library_name name of the plugin to unload
245  * @param arg argument to the plugin shutdown function
246  * @return whatever the shutdown function returned
247  */
248 void *
249 GNUNET_PLUGIN_unload (const char *library_name, void *arg)
250 {
251   struct PluginList *pos;
252   struct PluginList *prev;
253   GNUNET_PLUGIN_Callback done;
254   void *ret;
255
256   prev = NULL;
257   pos = plugins;
258   while ((pos != NULL) && (0 != strcmp (pos->name, library_name)))
259   {
260     prev = pos;
261     pos = pos->next;
262   }
263   if (pos == NULL)
264     return NULL;
265
266   done = resolve_function (pos, "done");
267   ret = NULL;
268   if (done != NULL)
269     ret = done (arg);
270   if (prev == NULL)
271     plugins = pos->next;
272   else
273     prev->next = pos->next;
274   lt_dlclose (pos->handle);
275   GNUNET_free (pos->name);
276   GNUNET_free (pos);
277   if (plugins == NULL)
278   {
279     plugin_fini ();
280     initialized = GNUNET_NO;
281   }
282   return ret;
283 }
284
285
286
287 /* end of plugin.c */