GNUnet 0.22.2
Plugin library

Plugin loading and unloading. More...

Collaboration diagram for Plugin library:

Typedefs

typedef void *(* GNUNET_PLUGIN_Callback) (void *arg)
 Signature of any function exported by a plugin. More...
 
typedef void(* GNUNET_PLUGIN_LoaderCallback) (void *cls, const char *library_name, void *lib_ret)
 Signature of a function called by GNUNET_PLUGIN_load_all(). More...
 

Functions

enum GNUNET_GenericReturnValue GNUNET_PLUGIN_test (const struct GNUNET_OS_ProjectData *pd, const char *library_name)
 Test if a plugin exists. More...
 
void * GNUNET_PLUGIN_load (const struct GNUNET_OS_ProjectData *pd, const char *library_name, void *arg)
 Setup plugin (runs the "init" callback and returns whatever "init" returned). More...
 
void GNUNET_PLUGIN_load_all (const struct GNUNET_OS_ProjectData *pd, const char *basename, void *arg, GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
 Load all compatible plugins with the given base name. More...
 
void * GNUNET_PLUGIN_unload (const char *library_name, void *arg)
 Unload plugin (runs the "done" callback and returns whatever "done" returned). More...
 

Detailed Description

Plugin loading and unloading.

Typedef Documentation

◆ GNUNET_PLUGIN_Callback

typedef void *(* GNUNET_PLUGIN_Callback) (void *arg)

Signature of any function exported by a plugin.

Parameters
argargument to the function (context)
Returns
some pointer, NULL if the plugin was shutdown or if there was an error, otherwise the plugin's API on success

Definition at line 60 of file gnunet_plugin_lib.h.

◆ GNUNET_PLUGIN_LoaderCallback

typedef void(* GNUNET_PLUGIN_LoaderCallback) (void *cls, const char *library_name, void *lib_ret)

Signature of a function called by GNUNET_PLUGIN_load_all().

Parameters
clsclosure
library_namefull name of the library (to be used with GNUNET_PLUGIN_unload)
lib_retreturn value from the initialization function of the library (same as what GNUNET_PLUGIN_load would have returned for the given library name)

Definition at line 108 of file gnunet_plugin_lib.h.

Function Documentation

◆ GNUNET_PLUGIN_test()

enum GNUNET_GenericReturnValue GNUNET_PLUGIN_test ( const struct GNUNET_OS_ProjectData pd,
const char *  library_name 
)

Test if a plugin exists.

Note that the library must export a symbol called "library_name_init" for the test to succeed.

Parameters
pdproject data with library search path
library_namename of the plugin to test if it is installed
Returns
GNUNET_YES if the plugin exists, GNUNET_NO if not

Definition at line 172 of file plugin.c.

174{
175 void *libhandle;
177 struct PluginList plug;
178
179 if (! initialized)
180 {
182 plugin_init ();
183 }
184 libhandle = open_library (pd,
185 library_name);
186 if (NULL == libhandle)
187 {
188 if (NULL == plugins)
189 {
190 plugin_fini ();
192 }
193 return GNUNET_NO;
194 }
195 plug.handle = libhandle;
196 plug.name = (char *) library_name;
197 init = resolve_function (&plug,
198 "init");
199 if (NULL == init)
200 {
201 GNUNET_break (0);
202 lt_dlclose (libhandle);
203 if (NULL == plugins)
204 {
205 plugin_fini ();
207 }
208 return GNUNET_NO;
209 }
210 lt_dlclose (libhandle);
211 if (NULL == plugins)
212 {
213 plugin_fini ();
215 }
216 return GNUNET_YES;
217}
static char * init
Set to the name of a service to start.
Definition: gnunet-arm.c:73
@ GNUNET_YES
@ GNUNET_NO
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
void *(* GNUNET_PLUGIN_Callback)(void *arg)
Signature of any function exported by a plugin.
static void plugin_fini(void)
Shutdown libtool.
Definition: plugin.c:88
static GNUNET_PLUGIN_Callback resolve_function(struct PluginList *plug, const char *name)
Lookup a function in the plugin.
Definition: plugin.c:103
static void plugin_init(void)
Setup libtool paths.
Definition: plugin.c:69
static void * open_library(const struct GNUNET_OS_ProjectData *pd, const char *library_name)
Open library library_name using search path from pd.
Definition: plugin.c:137
static struct PluginList * plugins
List of plugins we have loaded.
Definition: plugin.c:62
static int initialized
Have we been initialized?
Definition: plugin.c:57
Linked list of active plugins.
Definition: plugin.c:36

References GNUNET_break, GNUNET_NO, GNUNET_YES, PluginList::handle, init, initialized, PluginList::name, open_library(), plugin_fini(), plugin_init(), plugins, and resolve_function().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PLUGIN_load()

void * GNUNET_PLUGIN_load ( const struct GNUNET_OS_ProjectData pd,
const char *  library_name,
void *  arg 
)

Setup plugin (runs the "init" callback and returns whatever "init" returned).

If "init" returns NULL, the plugin is unloaded.

Note that the library must export symbols called "library_name_init" and "library_name_done". These will be called when the library is loaded and unloaded respectively.

Parameters
pdproject data with library search path
library_namename of the plugin to load
argargument to the plugin initialization function
Returns
whatever the initialization function returned, NULL on error

Definition at line 221 of file plugin.c.

224{
225 void *libhandle;
226 struct PluginList *plug;
228 void *ret;
229
230 if (! initialized)
231 {
233 plugin_init ();
234 }
235 libhandle = open_library (pd,
236 library_name);
237 if (NULL == libhandle)
238 {
240 _ ("`%s' failed for library `%s' with error: %s\n"),
241 "lt_dlopenext",
242 library_name,
243 lt_dlerror ());
244 if (NULL == plugins)
245 {
246 plugin_fini ();
248 }
249 return NULL;
250 }
251 plug = GNUNET_new (struct PluginList);
252 plug->handle = libhandle;
253 plug->name = GNUNET_strdup (library_name);
254 plug->next = plugins;
255 plugins = plug;
256 init = resolve_function (plug,
257 "init");
258 if ( (NULL == init) ||
259 (NULL == (ret = init (arg))) )
260 {
261 lt_dlclose (libhandle);
262 GNUNET_free (plug->name);
263 plugins = plug->next;
264 GNUNET_free (plug);
265 if (NULL == plugins)
266 {
267 plugin_fini ();
269 }
270 return NULL;
271 }
272 return ret;
273}
static int ret
Final status code.
Definition: gnunet-arm.c:93
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
#define _(String)
GNU gettext support macro.
Definition: platform.h:179
#define LOG(kind,...)
Definition: plugin.c:30
char * name
Name of the library.
Definition: plugin.c:45
struct PluginList * next
This is a linked list.
Definition: plugin.c:40
void * handle
System handle.
Definition: plugin.c:50

References _, find_typedefs::arg, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_new, GNUNET_NO, GNUNET_strdup, GNUNET_YES, PluginList::handle, init, initialized, LOG, PluginList::name, PluginList::next, open_library(), plugin_fini(), plugin_init(), plugins, resolve_function(), and ret.

Referenced by client_connect_cb(), find_libraries(), GNUNET_DATACACHE_create(), handle_helper_init(), load_plugin(), and run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PLUGIN_load_all()

void GNUNET_PLUGIN_load_all ( const struct GNUNET_OS_ProjectData pd,
const char *  basename,
void *  arg,
GNUNET_PLUGIN_LoaderCallback  cb,
void *  cb_cls 
)

Load all compatible plugins with the given base name.

Note that the library must export symbols called "basename_ANYTHING_init" and "basename_ANYTHING__done". These will be called when the library is loaded and unloaded respectively.

Parameters
pdproject data with library search path
basenamebasename of the plugins to load
argargument to the plugin initialization function
cbfunction to call for each plugin found
cb_clsclosure for cb

Definition at line 401 of file plugin.c.

406{
407 struct LoadAllContext lac = {
408 .pd = pd,
409 .basename = basename,
410 .arg = arg,
411 .cb = cb,
412 .cb_cls = cb_cls
413 };
414 char *path;
415
418 if (NULL == path)
419 {
421 _ ("Could not determine plugin installation path.\n"));
422 return;
423 }
426 &lac);
427 GNUNET_free (path);
428}
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition: disk.c:826
#define GNUNET_log(kind,...)
char * GNUNET_OS_installation_get_path(const struct GNUNET_OS_ProjectData *pd, enum GNUNET_OS_InstallationPathKind dirkind)
Get the path to a specific GNUnet installation directory or, with GNUNET_OS_IPK_SELF_PREFIX,...
@ GNUNET_OS_IPK_LIBDIR
Return the directory where libraries are installed.
static enum GNUNET_GenericReturnValue find_libraries(void *cls, const char *filename)
Function called on each plugin in the directory.
Definition: plugin.c:362
Closure for find_libraries().
Definition: plugin.c:323
const char * basename
Prefix the plugin names we find have to match.
Definition: plugin.c:333
void * cb_cls
Closure for cb.
Definition: plugin.c:348
const struct GNUNET_OS_ProjectData * pd
Project data to determine load paths.
Definition: plugin.c:328
GNUNET_PLUGIN_LoaderCallback cb
Function to call for each plugin.
Definition: plugin.c:343

References _, find_typedefs::arg, LoadAllContext::basename, LoadAllContext::cb, LoadAllContext::cb_cls, find_libraries(), GNUNET_DISK_directory_scan(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_OS_installation_get_path(), GNUNET_OS_IPK_LIBDIR, and LoadAllContext::pd.

Referenced by GNUNET_BLOCK_context_create(), and init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PLUGIN_unload()

void * GNUNET_PLUGIN_unload ( const char *  library_name,
void *  arg 
)

Unload plugin (runs the "done" callback and returns whatever "done" returned).

The plugin is then unloaded.

Parameters
library_namename of the plugin to unload
argargument to the plugin shutdown function
Returns
whatever the shutdown function returned, typically NULL or a "char *" representing the error message

Definition at line 277 of file plugin.c.

279{
280 struct PluginList *pos;
281 struct PluginList *prev;
283 void *ret;
284
285 prev = NULL;
286 pos = plugins;
287 while ( (NULL != pos) &&
288 (0 != strcmp (pos->name,
289 library_name)) )
290 {
291 prev = pos;
292 pos = pos->next;
293 }
294 if (NULL == pos)
295 return NULL;
296
297 done = resolve_function (pos,
298 "done");
299 ret = NULL;
300 if (NULL == prev)
301 plugins = pos->next;
302 else
303 prev->next = pos->next;
304 if (NULL != done)
305 ret = done (arg);
306 if (NULL == getenv ("VALGRINDING_PLUGINS"))
307 lt_dlclose (pos->handle);
308 GNUNET_free (pos->name);
309 GNUNET_free (pos);
310 if (NULL == plugins)
311 {
312 plugin_fini ();
314 }
315 return ret;
316}
char * getenv()

References find_typedefs::arg, getenv(), GNUNET_free, GNUNET_NO, PluginList::handle, initialized, PluginList::name, PluginList::next, plugin_fini(), plugins, resolve_function(), and ret.

Referenced by __attribute__(), cleanup_task(), client_disconnect_cb(), do_shutdown_later(), GNUNET_BLOCK_context_destroy(), GNUNET_DATACACHE_destroy(), run(), shutdown_task(), and unload_plugin().

Here is the call graph for this function:
Here is the caller graph for this function: