diff options
author | Christian Grothoff <christian@grothoff.org> | 2013-12-11 14:41:52 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2013-12-11 14:41:52 +0000 |
commit | 1fc94a08eff763910e64897a453cbc3da9143ec5 (patch) | |
tree | c43663472a3620793cd3f752fc533be40bf65fc8 /src/util/plugin.c | |
parent | de171471f2de11b08e9990fffd04f8164256cb01 (diff) | |
download | gnunet-1fc94a08eff763910e64897a453cbc3da9143ec5.tar.gz gnunet-1fc94a08eff763910e64897a453cbc3da9143ec5.zip |
-style and doxygen fixes
Diffstat (limited to 'src/util/plugin.c')
-rw-r--r-- | src/util/plugin.c | 87 |
1 files changed, 61 insertions, 26 deletions
diff --git a/src/util/plugin.c b/src/util/plugin.c index fe8281c32..a1a8aa681 100644 --- a/src/util/plugin.c +++ b/src/util/plugin.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet | 2 | This file is part of GNUnet |
3 | (C) 2002, 2003, 2004, 2005, 2006, 2009 Christian Grothoff (and other contributing authors) | 3 | (C) 2002-2013 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 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 | 6 | it under the terms of the GNU General Public License as published |
@@ -63,7 +63,6 @@ static int initialized; | |||
63 | */ | 63 | */ |
64 | static char *old_dlsearchpath; | 64 | static char *old_dlsearchpath; |
65 | 65 | ||
66 | |||
67 | /** | 66 | /** |
68 | * List of plugins we have loaded. | 67 | * List of plugins we have loaded. |
69 | */ | 68 | */ |
@@ -84,7 +83,8 @@ plugin_init () | |||
84 | err = lt_dlinit (); | 83 | err = lt_dlinit (); |
85 | if (err > 0) | 84 | if (err > 0) |
86 | { | 85 | { |
87 | FPRINTF (stderr, _("Initialization of plugin mechanism failed: %s!\n"), | 86 | FPRINTF (stderr, |
87 | _("Initialization of plugin mechanism failed: %s!\n"), | ||
88 | lt_dlerror ()); | 88 | lt_dlerror ()); |
89 | return; | 89 | return; |
90 | } | 90 | } |
@@ -128,6 +128,10 @@ plugin_fini () | |||
128 | 128 | ||
129 | /** | 129 | /** |
130 | * Lookup a function in the plugin. | 130 | * Lookup a function in the plugin. |
131 | * | ||
132 | * @param plug the plugin to check | ||
133 | * @param name name of the symbol to look for | ||
134 | * @return NULL if the symbol was not found | ||
131 | */ | 135 | */ |
132 | static GNUNET_PLUGIN_Callback | 136 | static GNUNET_PLUGIN_Callback |
133 | resolve_function (struct PluginList *plug, const char *name) | 137 | resolve_function (struct PluginList *plug, const char *name) |
@@ -137,24 +141,26 @@ resolve_function (struct PluginList *plug, const char *name) | |||
137 | 141 | ||
138 | GNUNET_asprintf (&initName, "_%s_%s", plug->name, name); | 142 | GNUNET_asprintf (&initName, "_%s_%s", plug->name, name); |
139 | mptr = lt_dlsym (plug->handle, &initName[1]); | 143 | mptr = lt_dlsym (plug->handle, &initName[1]); |
140 | if (mptr == NULL) | 144 | if (NULL == mptr) |
141 | mptr = lt_dlsym (plug->handle, initName); | 145 | mptr = lt_dlsym (plug->handle, initName); |
142 | if (mptr == NULL) | 146 | if (NULL == mptr) |
143 | LOG (GNUNET_ERROR_TYPE_ERROR, | 147 | LOG (GNUNET_ERROR_TYPE_ERROR, |
144 | _("`%s' failed to resolve method '%s' with error: %s\n"), "lt_dlsym", | 148 | _("`%s' failed to resolve method '%s' with error: %s\n"), |
149 | "lt_dlsym", | ||
145 | &initName[1], lt_dlerror ()); | 150 | &initName[1], lt_dlerror ()); |
146 | GNUNET_free (initName); | 151 | GNUNET_free (initName); |
147 | return mptr; | 152 | return mptr; |
148 | } | 153 | } |
149 | 154 | ||
155 | |||
150 | /** | 156 | /** |
151 | * Test if a plugin exists. | 157 | * Test if a plugin exists. |
152 | * | 158 | * |
153 | * Note that the library must export a symbol called | 159 | * Note that the library must export a symbol called |
154 | * "library_name_init" for the test to succeed. | 160 | * `library_name_init` for the test to succeed. |
155 | * | 161 | * |
156 | * @param library_name name of the plugin to test if it is installed | 162 | * @param library_name name of the plugin to test if it is installed |
157 | * @return GNUNET_YES if the plugin exists, GNUNET_NO if not | 163 | * @return #GNUNET_YES if the plugin exists, #GNUNET_NO if not |
158 | */ | 164 | */ |
159 | int | 165 | int |
160 | GNUNET_PLUGIN_test (const char *library_name) | 166 | GNUNET_PLUGIN_test (const char *library_name) |
@@ -163,18 +169,18 @@ GNUNET_PLUGIN_test (const char *library_name) | |||
163 | GNUNET_PLUGIN_Callback init; | 169 | GNUNET_PLUGIN_Callback init; |
164 | struct PluginList plug; | 170 | struct PluginList plug; |
165 | 171 | ||
166 | if (!initialized) | 172 | if (! initialized) |
167 | { | 173 | { |
168 | initialized = GNUNET_YES; | 174 | initialized = GNUNET_YES; |
169 | plugin_init (); | 175 | plugin_init (); |
170 | } | 176 | } |
171 | libhandle = lt_dlopenext (library_name); | 177 | libhandle = lt_dlopenext (library_name); |
172 | if (libhandle == NULL) | 178 | if (NULL == libhandle) |
173 | return GNUNET_NO; | 179 | return GNUNET_NO; |
174 | plug.handle = libhandle; | 180 | plug.handle = libhandle; |
175 | plug.name = (char *) library_name; | 181 | plug.name = (char *) library_name; |
176 | init = resolve_function (&plug, "init"); | 182 | init = resolve_function (&plug, "init"); |
177 | if (init == NULL) | 183 | if (NULL == init) |
178 | { | 184 | { |
179 | GNUNET_break (0); | 185 | GNUNET_break (0); |
180 | lt_dlclose (libhandle); | 186 | lt_dlclose (libhandle); |
@@ -186,11 +192,11 @@ GNUNET_PLUGIN_test (const char *library_name) | |||
186 | 192 | ||
187 | 193 | ||
188 | /** | 194 | /** |
189 | * Setup plugin (runs the "init" callback and returns whatever "init" | 195 | * Setup plugin (runs the `init` callback and returns whatever `init` |
190 | * returned). If "init" returns NULL, the plugin is unloaded. | 196 | * returned). If `init` returns NULL, the plugin is unloaded. |
191 | * | 197 | * |
192 | * Note that the library must export symbols called | 198 | * Note that the library must export symbols called |
193 | * "library_name_init" and "library_name_done". These will be called | 199 | * `library_name_init` and `library_name_done`. These will be called |
194 | * when the library is loaded and unloaded respectively. | 200 | * when the library is loaded and unloaded respectively. |
195 | * | 201 | * |
196 | * @param library_name name of the plugin to load | 202 | * @param library_name name of the plugin to load |
@@ -214,11 +220,12 @@ GNUNET_PLUGIN_load (const char *library_name, void *arg) | |||
214 | if (libhandle == NULL) | 220 | if (libhandle == NULL) |
215 | { | 221 | { |
216 | LOG (GNUNET_ERROR_TYPE_ERROR, | 222 | LOG (GNUNET_ERROR_TYPE_ERROR, |
217 | _("`%s' failed for library `%s' with error: %s\n"), "lt_dlopenext", | 223 | _("`%s' failed for library `%s' with error: %s\n"), |
224 | "lt_dlopenext", | ||
218 | library_name, lt_dlerror ()); | 225 | library_name, lt_dlerror ()); |
219 | return NULL; | 226 | return NULL; |
220 | } | 227 | } |
221 | plug = GNUNET_malloc (sizeof (struct PluginList)); | 228 | plug = GNUNET_new (struct PluginList); |
222 | plug->handle = libhandle; | 229 | plug->handle = libhandle; |
223 | plug->name = GNUNET_strdup (library_name); | 230 | plug->name = GNUNET_strdup (library_name); |
224 | plug->next = plugins; | 231 | plug->next = plugins; |
@@ -237,7 +244,7 @@ GNUNET_PLUGIN_load (const char *library_name, void *arg) | |||
237 | 244 | ||
238 | 245 | ||
239 | /** | 246 | /** |
240 | * Unload plugin (runs the "done" callback and returns whatever "done" | 247 | * Unload plugin (runs the `done` callback and returns whatever `done` |
241 | * returned). The plugin is then unloaded. | 248 | * returned). The plugin is then unloaded. |
242 | * | 249 | * |
243 | * @param library_name name of the plugin to unload | 250 | * @param library_name name of the plugin to unload |
@@ -245,7 +252,8 @@ GNUNET_PLUGIN_load (const char *library_name, void *arg) | |||
245 | * @return whatever the shutdown function returned | 252 | * @return whatever the shutdown function returned |
246 | */ | 253 | */ |
247 | void * | 254 | void * |
248 | GNUNET_PLUGIN_unload (const char *library_name, void *arg) | 255 | GNUNET_PLUGIN_unload (const char *library_name, |
256 | void *arg) | ||
249 | { | 257 | { |
250 | struct PluginList *pos; | 258 | struct PluginList *pos; |
251 | struct PluginList *prev; | 259 | struct PluginList *prev; |
@@ -254,26 +262,26 @@ GNUNET_PLUGIN_unload (const char *library_name, void *arg) | |||
254 | 262 | ||
255 | prev = NULL; | 263 | prev = NULL; |
256 | pos = plugins; | 264 | pos = plugins; |
257 | while ((pos != NULL) && (0 != strcmp (pos->name, library_name))) | 265 | while ((NULL != pos) && (0 != strcmp (pos->name, library_name))) |
258 | { | 266 | { |
259 | prev = pos; | 267 | prev = pos; |
260 | pos = pos->next; | 268 | pos = pos->next; |
261 | } | 269 | } |
262 | if (pos == NULL) | 270 | if (NULL == pos) |
263 | return NULL; | 271 | return NULL; |
264 | 272 | ||
265 | done = resolve_function (pos, "done"); | 273 | done = resolve_function (pos, "done"); |
266 | ret = NULL; | 274 | ret = NULL; |
267 | if (done != NULL) | 275 | if (NULL != done) |
268 | ret = done (arg); | 276 | ret = done (arg); |
269 | if (prev == NULL) | 277 | if (NULL == prev) |
270 | plugins = pos->next; | 278 | plugins = pos->next; |
271 | else | 279 | else |
272 | prev->next = pos->next; | 280 | prev->next = pos->next; |
273 | lt_dlclose (pos->handle); | 281 | lt_dlclose (pos->handle); |
274 | GNUNET_free (pos->name); | 282 | GNUNET_free (pos->name); |
275 | GNUNET_free (pos); | 283 | GNUNET_free (pos); |
276 | if (plugins == NULL) | 284 | if (NULL == plugins) |
277 | { | 285 | { |
278 | plugin_fini (); | 286 | plugin_fini (); |
279 | initialized = GNUNET_NO; | 287 | initialized = GNUNET_NO; |
@@ -282,15 +290,42 @@ GNUNET_PLUGIN_unload (const char *library_name, void *arg) | |||
282 | } | 290 | } |
283 | 291 | ||
284 | 292 | ||
293 | /** | ||
294 | * Closure for #find_libraries(). | ||
295 | */ | ||
285 | struct LoadAllContext | 296 | struct LoadAllContext |
286 | { | 297 | { |
298 | /** | ||
299 | * Prefix the plugin names we find have to match. | ||
300 | */ | ||
287 | const char *basename; | 301 | const char *basename; |
302 | |||
303 | /** | ||
304 | * Argument to give to 'init' when loading the plugin. | ||
305 | */ | ||
288 | void *arg; | 306 | void *arg; |
307 | |||
308 | /** | ||
309 | * Function to call for each plugin. | ||
310 | */ | ||
289 | GNUNET_PLUGIN_LoaderCallback cb; | 311 | GNUNET_PLUGIN_LoaderCallback cb; |
312 | |||
313 | /** | ||
314 | * Closure for @e cb | ||
315 | */ | ||
290 | void *cb_cls; | 316 | void *cb_cls; |
291 | }; | 317 | }; |
292 | 318 | ||
293 | 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 | */ | ||
294 | static int | 329 | static int |
295 | find_libraries (void *cls, const char *filename) | 330 | find_libraries (void *cls, const char *filename) |
296 | { | 331 | { |
@@ -325,13 +360,13 @@ find_libraries (void *cls, const char *filename) | |||
325 | * Load all compatible plugins with the given base name. | 360 | * Load all compatible plugins with the given base name. |
326 | * | 361 | * |
327 | * Note that the library must export symbols called | 362 | * Note that the library must export symbols called |
328 | * "basename_ANYTHING_init" and "basename_ANYTHING__done". These will | 363 | * `basename_ANYTHING_init` and `basename_ANYTHING__done`. These will |
329 | * be called when the library is loaded and unloaded respectively. | 364 | * be called when the library is loaded and unloaded respectively. |
330 | * | 365 | * |
331 | * @param basename basename of the plugins to load | 366 | * @param basename basename of the plugins to load |
332 | * @param arg argument to the plugin initialization function | 367 | * @param arg argument to the plugin initialization function |
333 | * @param cb function to call for each plugin found | 368 | * @param cb function to call for each plugin found |
334 | * @param cb_cls closure for 'cb' | 369 | * @param cb_cls closure for @a cb |
335 | */ | 370 | */ |
336 | void | 371 | void |
337 | GNUNET_PLUGIN_load_all (const char *basename, void *arg, | 372 | GNUNET_PLUGIN_load_all (const char *basename, void *arg, |
@@ -341,7 +376,7 @@ GNUNET_PLUGIN_load_all (const char *basename, void *arg, | |||
341 | char *path; | 376 | char *path; |
342 | 377 | ||
343 | path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR); | 378 | path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR); |
344 | if (path == NULL) | 379 | if (NULL == path) |
345 | { | 380 | { |
346 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 381 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
347 | _("Could not determine plugin installation path.\n")); | 382 | _("Could not determine plugin installation path.\n")); |