GNUnet  0.18.1
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

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 59 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 103 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

Definition at line 140 of file plugin.c.

168 {
169  void *libhandle;
171  struct PluginList plug;
172 
173  if (! initialized)
174  {
176  plugin_init ();
177  }
178  libhandle = lt_dlopenext (library_name);
179  if (NULL == libhandle)
180  return GNUNET_NO;
181  plug.handle = libhandle;
182  plug.name = (char *) library_name;
183  init = resolve_function (&plug,
184  "init");
185  if (NULL == init)
186  {
187  GNUNET_break (0);
188  lt_dlclose (libhandle);
189  return GNUNET_NO;
190  }
191  lt_dlclose (libhandle);
192  return GNUNET_YES;
193 }
static char * init
Set to the name of a service to start.
Definition: gnunet-arm.c:74
@ GNUNET_YES
@ GNUNET_NO
Definition: gnunet_common.h:98
#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 GNUNET_PLUGIN_Callback resolve_function(struct PluginList *plug, const char *name)
Lookup a function in the plugin.
Definition: plugin.c:140
static void plugin_init(void)
Setup libtool paths.
Definition: plugin.c:75
static int initialized
Have we been initialized?
Definition: plugin.c:58
Linked list of active plugins.
Definition: plugin.c:37

References _, GNUNET_asprintf(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, PluginList::handle, LOG, name, and PluginList::name.

Referenced by run().

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

Definition at line 197 of file plugin.c.

199 {
200  void *libhandle;
201  struct PluginList *plug;
203  void *ret;
204 
205  if (! initialized)
206  {
208  plugin_init ();
209  }
210  libhandle = lt_dlopenext (library_name);
211  if (NULL == libhandle)
212  {
214  _ ("`%s' failed for library `%s' with error: %s\n"),
215  "lt_dlopenext",
216  library_name,
217  lt_dlerror ());
218  return NULL;
219  }
220  plug = GNUNET_new (struct PluginList);
221  plug->handle = libhandle;
222  plug->name = GNUNET_strdup (library_name);
223  plug->next = plugins;
224  plugins = plug;
225  init = resolve_function (plug,
226  "init");
227  if ( (NULL == init) ||
228  (NULL == (ret = init (arg))) )
229  {
230  lt_dlclose (libhandle);
231  GNUNET_free (plug->name);
232  plugins = plug->next;
233  GNUNET_free (plug);
234  return NULL;
235  }
236  return ret;
237 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
@ 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:177
static struct PluginList * plugins
List of plugins we have loaded.
Definition: plugin.c:68
#define LOG(kind,...)
Definition: plugin.c:31
char * name
Name of the library.
Definition: plugin.c:46
struct PluginList * next
This is a linked list.
Definition: plugin.c:41
void * handle
System handle.
Definition: plugin.c:51

References _, find_typedefs::arg, 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 client_connect_cb(), find_libraries(), GAS_plugin_init(), GNUNET_ATS_solvers_solver_start(), GNUNET_DATACACHE_create(), GPI_plugins_load(), GST_plugins_load(), load_plugin(), load_underlay(), run(), and tokenizer_cb().

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

Definition at line 358 of file plugin.c.

362 {
363  struct LoadAllContext lac;
364  char *path;
365 
367  if (NULL == path)
368  {
370  _ ("Could not determine plugin installation path.\n"));
371  return;
372  }
373  lac.basename = basename;
374  lac.arg = arg;
375  lac.cb = cb;
376  lac.cb_cls = cb_cls;
379  &lac);
380  GNUNET_free (path);
381 }
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 GNUNET_log(kind,...)
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,...
@ GNUNET_OS_IPK_LIBDIR
Return the directory where libraries are installed.
static int find_libraries(void *cls, const char *filename)
Function called on each plugin in the directory.
Definition: plugin.c:320
Closure for find_libraries().
Definition: plugin.c:287
const char * basename
Prefix the plugin names we find have to match.
Definition: plugin.c:291
void * cb_cls
Closure for cb.
Definition: plugin.c:306
GNUNET_PLUGIN_LoaderCallback cb
Function to call for each plugin.
Definition: plugin.c:301

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

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

390 {
391  const struct GNUNET_OS_ProjectData *cpd = GNUNET_OS_project_data_get ();
392 
394  GNUNET_PLUGIN_load_all (basename,
395  arg,
396  cb,
397  cb_cls);
398  GNUNET_OS_init (cpd);
399 }
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
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.
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:358
Project-specific data used to help the OS subsystem find installation paths.

References find_typedefs::arg, ctx, GNUNET_OS_init(), GNUNET_OS_project_data_get(), and GNUNET_PLUGIN_load_all().

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

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

243 {
244  struct PluginList *pos;
245  struct PluginList *prev;
247  void *ret;
248 
249  prev = NULL;
250  pos = plugins;
251  while ( (NULL != pos) &&
252  (0 != strcmp (pos->name,
253  library_name)) )
254  {
255  prev = pos;
256  pos = pos->next;
257  }
258  if (NULL == pos)
259  return NULL;
260 
261  done = resolve_function (pos,
262  "done");
263  ret = NULL;
264  if (NULL == prev)
265  plugins = pos->next;
266  else
267  prev->next = pos->next;
268  if (NULL != done)
269  ret = done (arg);
270  if (NULL == getenv ("VALGRINDING_PLUGINS"))
271  lt_dlclose (pos->handle);
272  GNUNET_free (pos->name);
273  GNUNET_free (pos);
274  if (NULL == plugins)
275  {
276  plugin_fini ();
278  }
279  return ret;
280 }
char * getenv()
static void done()
static void plugin_fini(void)
Shutdown libtool.
Definition: plugin.c:119

References find_typedefs::arg, done(), 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(), GAS_plugin_done(), GNUNET_ATS_solvers_solver_stop(), GNUNET_BLOCK_context_destroy(), GNUNET_DATACACHE_destroy(), GPI_plugins_unload(), GST_plugins_load(), GST_plugins_unload(), run(), shutdown_task(), and unload_plugin().

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