cleaner
[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,
101                            "%s:%s",
102                            opath,
103                            path);
104           lt_dlsetsearchpath (cpath);
105           GNUNET_free (path);
106           GNUNET_free (cpath);
107         }
108       else
109         {
110           lt_dlsetsearchpath (path);
111           GNUNET_free (path);
112         }
113     }
114 }
115
116
117 /**
118  * Shutdown libtool.
119  */
120 static void
121 plugin_fini ()
122 {
123   lt_dlsetsearchpath (old_dlsearchpath);
124   if (old_dlsearchpath != NULL)
125     {
126       GNUNET_free (old_dlsearchpath);
127       old_dlsearchpath = NULL;
128     }
129   lt_dlexit ();
130 }
131
132
133 /**
134  * Lookup a function in the plugin.
135  */
136 static GNUNET_PLUGIN_Callback
137 resolve_function (struct PluginList *plug, const char *name)
138 {
139   char *initName;
140   void *mptr;
141
142   GNUNET_asprintf (&initName, "_%s_%s", plug->name, name);
143   mptr = lt_dlsym (plug->handle, &initName[1]);
144   if (mptr == NULL)
145     mptr = lt_dlsym (plug->handle, initName);
146   if (mptr == NULL)
147     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
148                 _("`%s' failed to resolve method '%s' with error: %s\n"),
149                 "lt_dlsym", &initName[1], lt_dlerror ());
150   GNUNET_free (initName);
151   return mptr;
152 }
153
154
155 /**
156  * Setup plugin (runs the "init" callback and returns whatever "init"
157  * returned).  If "init" returns NULL, the plugin is unloaded.
158  *
159  * Note that the library must export symbols called
160  * "library_name_init" and "library_name_done".  These will be called
161  * when the library is loaded and unloaded respectively.
162  *
163  * @param library_name name of the plugin to load
164  * @param arg argument to the plugin initialization function
165  * @return whatever the initialization function returned
166  */
167 void *
168 GNUNET_PLUGIN_load (const char *library_name, void *arg)
169 {
170   void *libhandle;
171   struct PluginList *plug;
172   GNUNET_PLUGIN_Callback init;
173   void *ret;
174
175   if (! initialized)
176     {
177       initialized = GNUNET_YES;
178       plugin_init ();
179     }
180   libhandle = lt_dlopenext (library_name);
181   if (libhandle == NULL)
182     {
183       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
184                   _("`%s' failed for library `%s' with error: %s\n"),
185                   "lt_dlopenext", library_name, lt_dlerror ());
186       return NULL;
187     }
188   plug = GNUNET_malloc (sizeof (struct PluginList));
189   plug->handle = libhandle;
190   plug->name = GNUNET_strdup (library_name);
191   plug->next = plugins;
192   plugins = plug;
193   init = resolve_function (plug, "init");
194   if ((init == NULL) || (NULL == (ret = init (arg))))
195     {
196       GNUNET_free (plug->name);
197       plugins = plug->next;
198       GNUNET_free (plug);
199       return NULL;
200     }
201   return ret;
202 }
203
204
205 /**
206  * Unload plugin (runs the "done" callback and returns whatever "done"
207  * returned).  The plugin is then unloaded.
208  *
209  * @param library_name name of the plugin to unload
210  * @param arg argument to the plugin shutdown function
211  * @return whatever the shutdown function returned
212  */
213 void *
214 GNUNET_PLUGIN_unload (const char *library_name, void *arg)
215 {
216   struct PluginList *pos;
217   struct PluginList *prev;
218   GNUNET_PLUGIN_Callback done;
219   void *ret;
220
221   prev = NULL;
222   pos = plugins;
223   while ((pos != NULL) && (0 != strcmp (pos->name, library_name)))
224     {
225       prev = pos;
226       pos = pos->next;
227     }
228   if (pos == NULL)
229     return NULL;
230
231   done = resolve_function (pos, "done");
232   ret = NULL;
233   if (done != NULL)
234     ret = done (arg);
235   if (prev == NULL)
236     plugins = pos->next;
237   else
238     prev->next = pos->next;
239   lt_dlclose (pos->handle);
240   GNUNET_free (pos->name);
241   GNUNET_free (pos);
242   if (plugins == NULL)
243     {
244       plugin_fini();
245       initialized = GNUNET_NO;
246     }
247   return ret;
248 }
249
250
251
252 /* end of plugin.c */