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_load_all_in_context (const struct GNUNET_OS_ProjectData *ctx, const char *basename, void *arg, GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
 Load all compatible plugins with the given base name while inside the given context (i.e. 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 175 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().

176 {
177  void *libhandle;
179  struct PluginList plug;
180 
181  if (! initialized)
182  {
184  plugin_init ();
185  }
186  libhandle = lt_dlopenext (library_name);
187  if (NULL == libhandle)
188  return GNUNET_NO;
189  plug.handle = libhandle;
190  plug.name = (char *) library_name;
191  init = resolve_function (&plug,
192  "init");
193  if (NULL == init)
194  {
195  GNUNET_break (0);
196  lt_dlclose (libhandle);
197  return GNUNET_NO;
198  }
199  lt_dlclose (libhandle);
200  return GNUNET_YES;
201 }
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 int initialized
Have we been initialized?
Definition: plugin.c:58
static void plugin_init(void)
Setup libtool paths.
Definition: plugin.c:75
static char * init
Set to the name of a service to start.
Definition: gnunet-arm.c:74
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:139
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 217 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(), main(), run(), and tokenizer_cb().

219 {
220  void *libhandle;
221  struct PluginList *plug;
223  void *ret;
224 
225  if (! initialized)
226  {
228  plugin_init ();
229  }
230  libhandle = lt_dlopenext (library_name);
231  if (NULL == libhandle)
232  {
234  _ ("`%s' failed for library `%s' with error: %s\n"),
235  "lt_dlopenext",
236  library_name,
237  lt_dlerror ());
238  return NULL;
239  }
240  plug = GNUNET_new (struct PluginList);
241  plug->handle = libhandle;
242  plug->name = GNUNET_strdup (library_name);
243  plug->next = plugins;
244  plugins = plug;
245  init = resolve_function (plug,
246  "init");
247  if ( (NULL == init) ||
248  (NULL == (ret = init (arg))) )
249  {
250  lt_dlclose (libhandle);
251  GNUNET_free (plug->name);
252  plugins = plug->next;
253  GNUNET_free (plug);
254  return NULL;
255  }
256  return ret;
257 }
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 int initialized
Have we been initialized?
Definition: plugin.c:58
static void plugin_init(void)
Setup libtool paths.
Definition: plugin.c:75
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
#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
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:139
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.

If you are writing a service to which third-party applications can connect, like GNUnet's own GNS service for example, you should use GNUNET_PLUGIN_load_all_in_context instead of this function, passing your service's project data as context.

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 402 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_PLUGIN_load_all_in_context().

406 {
407  struct LoadAllContext lac;
408  char *path;
409 
411  if (NULL == path)
412  {
414  _ ("Could not determine plugin installation path.\n"));
415  return;
416  }
417  lac.basename = basename;
418  lac.arg = arg;
419  lac.cb = cb;
420  lac.cb_cls = cb_cls;
423  &lac);
424  GNUNET_free (path);
425 }
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:814
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
void * cb_cls
Closure for cb.
Definition: plugin.c:333
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:347
#define GNUNET_log(kind,...)
GNUNET_PLUGIN_LoaderCallback cb
Function to call for each plugin.
Definition: plugin.c:328
Closure for find_libraries().
Definition: plugin.c:313
#define GNUNET_free(ptr)
Wrapper around free.
const char * basename
Prefix the plugin names we find have to match.
Definition: plugin.c:318
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_PLUGIN_load_all_in_context()

void GNUNET_PLUGIN_load_all_in_context ( const struct GNUNET_OS_ProjectData ctx,
const char *  basename,
void *  arg,
GNUNET_PLUGIN_LoaderCallback  cb,
void *  cb_cls 
)

Load all compatible plugins with the given base name while inside the given context (i.e.

a specific project data structure.)

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
ctxthe context used to find the plugins
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 443 of file plugin.c.

References GNUNET_OS_init(), GNUNET_OS_project_data_get(), and GNUNET_PLUGIN_load_all().

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

448 {
449  const struct GNUNET_OS_ProjectData *cpd = GNUNET_OS_project_data_get ();
450 
451  GNUNET_OS_init (ctx);
452  GNUNET_PLUGIN_load_all (basename,
453  arg,
454  cb,
455  cb_cls);
456  GNUNET_OS_init (cpd);
457 }
Project-specific data used to help the OS subsystem find installation paths.
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.
Definition: plugin.c:402
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_get(void)
void GNUNET_OS_init(const struct GNUNET_OS_ProjectData *pd)
Setup OS subsystem with project data.
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 269 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().

271 {
272  struct PluginList *pos;
273  struct PluginList *prev;
275  void *ret;
276 
277  prev = NULL;
278  pos = plugins;
279  while ( (NULL != pos) &&
280  (0 != strcmp (pos->name,
281  library_name)) )
282  {
283  prev = pos;
284  pos = pos->next;
285  }
286  if (NULL == pos)
287  return NULL;
288 
289  done = resolve_function (pos,
290  "done");
291  ret = NULL;
292  if (NULL != done)
293  ret = done (arg);
294  if (NULL == prev)
295  plugins = pos->next;
296  else
297  prev->next = pos->next;
298  lt_dlclose (pos->handle);
299  GNUNET_free (pos->name);
300  GNUNET_free (pos);
301  if (NULL == plugins)
302  {
303  plugin_fini ();
305  }
306  return ret;
307 }
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
static void plugin_fini(void)
Shutdown libtool.
Definition: plugin.c:119
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 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:139
Here is the call graph for this function:
Here is the caller graph for this function: