GNUnet  0.10.x
Typedefs | Functions
Plugin library

Plugin loading and unloading. More...

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

int GNUNET_PLUGIN_test (const char *library_name)
 Test if a plugin exists. More...
 
void * GNUNET_PLUGIN_load (const char *library_name, void *arg)
 Setup plugin (runs the "init" callback and returns whatever "init" returned). More...
 
void GNUNET_PLUGIN_load_all (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 56 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 100 of file gnunet_plugin_lib.h.

Function Documentation

◆ GNUNET_PLUGIN_test()

int GNUNET_PLUGIN_test ( 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
library_namename of the plugin to test if it is installed
Returns
GNUNET_YES if the plugin exists, GNUNET_NO if not

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

Parameters
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 165 of file plugin.c.

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

Referenced by run().

166 {
167  void *libhandle;
169  struct PluginList plug;
170 
171  if (!initialized)
172  {
174  plugin_init();
175  }
176  libhandle = lt_dlopenext(library_name);
177  if (NULL == libhandle)
178  return GNUNET_NO;
179  plug.handle = libhandle;
180  plug.name = (char *)library_name;
181  init = resolve_function(&plug, "init");
182  if (NULL == init)
183  {
184  GNUNET_break(0);
185  lt_dlclose(libhandle);
186  return GNUNET_NO;
187  }
188  lt_dlclose(libhandle);
189  return GNUNET_YES;
190 }
#define GNUNET_NO
Definition: gnunet_common.h:78
Linked list of active plugins.
Definition: plugin.c:36
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static void plugin_init()
Setup libtool paths.
Definition: plugin.c:74
static int initialized
Have we been initialized?
Definition: plugin.c:57
static char * init
Set to the name of a service to start.
Definition: gnunet-arm.c:69
#define GNUNET_YES
Definition: gnunet_common.h:77
void *(* GNUNET_PLUGIN_Callback)(void *arg)
Signature of any function exported by a plugin.
static GNUNET_PLUGIN_Callback resolve_function(struct PluginList *plug, const char *name)
Lookup a function in the plugin.
Definition: plugin.c:135
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PLUGIN_load()

void* GNUNET_PLUGIN_load ( 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
library_namename of the plugin to load
argargument to the plugin initialization function
Returns
whatever the initialization function returned, NULL on error

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
library_namename of the plugin to load
argargument to the plugin initialization function
Returns
whatever the initialization function returned

Definition at line 206 of file plugin.c.

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

Referenced by find_libraries(), GAS_plugin_init(), GNUNET_ATS_solvers_solver_start(), GNUNET_DATACACHE_create(), GPI_plugins_load(), GST_plugins_load(), load_plugin(), and run().

207 {
208  void *libhandle;
209  struct PluginList *plug;
211  void *ret;
212 
213  if (!initialized)
214  {
216  plugin_init();
217  }
218  libhandle = lt_dlopenext(library_name);
219  if (libhandle == NULL)
220  {
222  _("`%s' failed for library `%s' with error: %s\n"),
223  "lt_dlopenext",
224  library_name,
225  lt_dlerror());
226  return NULL;
227  }
228  plug = GNUNET_new(struct PluginList);
229  plug->handle = libhandle;
230  plug->name = GNUNET_strdup(library_name);
231  plug->next = plugins;
232  plugins = plug;
233  init = resolve_function(plug, "init");
234  if ((init == NULL) || (NULL == (ret = init(arg))))
235  {
236  lt_dlclose(libhandle);
237  GNUNET_free(plug->name);
238  plugins = plug->next;
239  GNUNET_free(plug);
240  return NULL;
241  }
242  return ret;
243 }
char * name
Name of the library.
Definition: plugin.c:45
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct PluginList * next
This is a linked list.
Definition: plugin.c:40
Linked list of active plugins.
Definition: plugin.c:36
void * handle
System handle.
Definition: plugin.c:50
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
static void plugin_init()
Setup libtool paths.
Definition: plugin.c:74
static int initialized
Have we been initialized?
Definition: plugin.c:57
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define LOG(kind,...)
Definition: plugin.c:31
static char * init
Set to the name of a service to start.
Definition: gnunet-arm.c:69
static struct PluginList * plugins
List of plugins we have loaded.
Definition: plugin.c:67
#define GNUNET_YES
Definition: gnunet_common.h:77
void *(* GNUNET_PLUGIN_Callback)(void *arg)
Signature of any function exported by a plugin.
#define GNUNET_free(ptr)
Wrapper around free.
static GNUNET_PLUGIN_Callback resolve_function(struct PluginList *plug, const char *name)
Lookup a function in the plugin.
Definition: plugin.c:135
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 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
basenamebasename of the plugins to load
argargument to the plugin initialization function
cbfunction to call for each plugin found
cb_clsclosure for cb

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
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 370 of file plugin.c.

References _, find_typedefs::arg, LoadAllContext::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(), and GNUNET_OS_IPK_LIBDIR.

Referenced by GNUNET_BLOCK_context_create(), init(), and run().

374 {
375  struct LoadAllContext lac;
376  char *path;
377 
379  if (NULL == path)
380  {
382  _("Could not determine plugin installation path.\n"));
383  return;
384  }
385  lac.basename = basename;
386  lac.arg = arg;
387  lac.cb = cb;
388  lac.cb_cls = cb_cls;
390  GNUNET_free(path);
391 }
Return the directory where libraries are installed.
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition: disk.c:909
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
void * cb_cls
Closure for cb.
Definition: plugin.c:314
char * GNUNET_OS_installation_get_path(enum GNUNET_OS_InstallationPathKind dirkind)
Get the path to a specific GNUnet installation directory or, with GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation directory.
static int find_libraries(void *cls, const char *filename)
Function called on each plugin in the directory.
Definition: plugin.c:328
#define GNUNET_log(kind,...)
GNUNET_PLUGIN_LoaderCallback cb
Function to call for each plugin.
Definition: plugin.c:309
Closure for find_libraries().
Definition: plugin.c:295
#define GNUNET_free(ptr)
Wrapper around free.
const char * basename
Prefix the plugin names we find have to match.
Definition: plugin.c:299
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

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

Definition at line 255 of file plugin.c.

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

Referenced by __attribute__(), cleanup_task(), do_shutdown(), final_cleanup(), GAS_plugin_done(), GNUNET_ATS_solvers_solver_stop(), GNUNET_BLOCK_context_destroy(), GNUNET_DATACACHE_destroy(), GPI_plugins_unload(), GST_plugins_load(), GST_plugins_unload(), and unload_plugin().

256 {
257  struct PluginList *pos;
258  struct PluginList *prev;
260  void *ret;
261 
262  prev = NULL;
263  pos = plugins;
264  while ((NULL != pos) && (0 != strcmp(pos->name, library_name)))
265  {
266  prev = pos;
267  pos = pos->next;
268  }
269  if (NULL == pos)
270  return NULL;
271 
272  done = resolve_function(pos, "done");
273  ret = NULL;
274  if (NULL != done)
275  ret = done(arg);
276  if (NULL == prev)
277  plugins = pos->next;
278  else
279  prev->next = pos->next;
280  lt_dlclose(pos->handle);
281  GNUNET_free(pos->name);
282  GNUNET_free(pos);
283  if (NULL == plugins)
284  {
285  plugin_fini();
287  }
288  return ret;
289 }
static void done()
char * name
Name of the library.
Definition: plugin.c:45
#define GNUNET_NO
Definition: gnunet_common.h:78
struct PluginList * next
This is a linked list.
Definition: plugin.c:40
Linked list of active plugins.
Definition: plugin.c:36
void * handle
System handle.
Definition: plugin.c:50
static int ret
Final status code.
Definition: gnunet-arm.c:89
static int initialized
Have we been initialized?
Definition: plugin.c:57
static void plugin_fini()
Shutdown libtool.
Definition: plugin.c:115
static struct PluginList * plugins
List of plugins we have loaded.
Definition: plugin.c:67
void *(* GNUNET_PLUGIN_Callback)(void *arg)
Signature of any function exported by a plugin.
#define GNUNET_free(ptr)
Wrapper around free.
static GNUNET_PLUGIN_Callback resolve_function(struct PluginList *plug, const char *name)
Lookup a function in the plugin.
Definition: plugin.c:135
Here is the call graph for this function:
Here is the caller graph for this function: