GNUnet  0.11.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 166 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().

167 {
168  void *libhandle;
170  struct PluginList plug;
171 
172  if (! initialized)
173  {
175  plugin_init ();
176  }
177  libhandle = lt_dlopenext (library_name);
178  if (NULL == libhandle)
179  return GNUNET_NO;
180  plug.handle = libhandle;
181  plug.name = (char *) library_name;
182  init = resolve_function (&plug, "init");
183  if (NULL == init)
184  {
185  GNUNET_break (0);
186  lt_dlclose (libhandle);
187  return GNUNET_NO;
188  }
189  lt_dlclose (libhandle);
190  return GNUNET_YES;
191 }
#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:75
static int initialized
Have we been initialized?
Definition: plugin.c:58
static char * init
Set to the name of a service to start.
Definition: gnunet-arm.c:74
#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:136
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 207 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().

208 {
209  void *libhandle;
210  struct PluginList *plug;
212  void *ret;
213 
214  if (! initialized)
215  {
217  plugin_init ();
218  }
219  libhandle = lt_dlopenext (library_name);
220  if (libhandle == NULL)
221  {
223  _ ("`%s' failed for library `%s' with error: %s\n"),
224  "lt_dlopenext",
225  library_name,
226  lt_dlerror ());
227  return NULL;
228  }
229  plug = GNUNET_new (struct PluginList);
230  plug->handle = libhandle;
231  plug->name = GNUNET_strdup (library_name);
232  plug->next = plugins;
233  plugins = plug;
234  init = resolve_function (plug, "init");
235  if ((init == NULL) || (NULL == (ret = init (arg))))
236  {
237  lt_dlclose (libhandle);
238  GNUNET_free (plug->name);
239  plugins = plug->next;
240  GNUNET_free (plug);
241  return NULL;
242  }
243  return ret;
244 }
char * name
Name of the library.
Definition: plugin.c:46
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct PluginList * next
This is a linked list.
Definition: plugin.c:41
Linked list of active plugins.
Definition: plugin.c:36
void * handle
System handle.
Definition: plugin.c:51
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
static void plugin_init()
Setup libtool paths.
Definition: plugin.c:75
static int initialized
Have we been initialized?
Definition: plugin.c:58
#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:74
static struct PluginList * plugins
List of plugins we have loaded.
Definition: plugin.c:68
#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:136
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 372 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().

376 {
377  struct LoadAllContext lac;
378  char *path;
379 
381  if (NULL == path)
382  {
384  _ ("Could not determine plugin installation path.\n"));
385  return;
386  }
387  lac.basename = basename;
388  lac.arg = arg;
389  lac.cb = cb;
390  lac.cb_cls = cb_cls;
392  GNUNET_free (path);
393 }
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:912
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
void * cb_cls
Closure for cb.
Definition: plugin.c:316
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:330
#define GNUNET_log(kind,...)
GNUNET_PLUGIN_LoaderCallback cb
Function to call for each plugin.
Definition: plugin.c:311
Closure for find_libraries().
Definition: plugin.c:296
#define GNUNET_free(ptr)
Wrapper around free.
const char * basename
Prefix the plugin names we find have to match.
Definition: plugin.c:301
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 256 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(), 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().

257 {
258  struct PluginList *pos;
259  struct PluginList *prev;
261  void *ret;
262 
263  prev = NULL;
264  pos = plugins;
265  while ((NULL != pos) && (0 != strcmp (pos->name, library_name)))
266  {
267  prev = pos;
268  pos = pos->next;
269  }
270  if (NULL == pos)
271  return NULL;
272 
273  done = resolve_function (pos, "done");
274  ret = NULL;
275  if (NULL != done)
276  ret = done (arg);
277  if (NULL == prev)
278  plugins = pos->next;
279  else
280  prev->next = pos->next;
281  lt_dlclose (pos->handle);
282  GNUNET_free (pos->name);
283  GNUNET_free (pos);
284  if (NULL == plugins)
285  {
286  plugin_fini ();
288  }
289  return ret;
290 }
static void done()
char * name
Name of the library.
Definition: plugin.c:46
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_NO
Definition: gnunet_common.h:78
struct PluginList * next
This is a linked list.
Definition: plugin.c:41
Linked list of active plugins.
Definition: plugin.c:36
void * handle
System handle.
Definition: plugin.c:51
static int initialized
Have we been initialized?
Definition: plugin.c:58
static void plugin_fini()
Shutdown libtool.
Definition: plugin.c:116
static struct PluginList * plugins
List of plugins we have loaded.
Definition: plugin.c:68
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:136
Here is the call graph for this function:
Here is the caller graph for this function: