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

170 {
171  void *libhandle;
173  struct PluginList plug;
174 
175  if (! initialized)
176  {
178  plugin_init ();
179  }
180  libhandle = lt_dlopenext (library_name);
181  if (NULL == libhandle)
182  return GNUNET_NO;
183  plug.handle = libhandle;
184  plug.name = (char *) library_name;
185  init = resolve_function (&plug, "init");
186  if (NULL == init)
187  {
188  GNUNET_break (0);
189  lt_dlclose (libhandle);
190  return GNUNET_NO;
191  }
192  lt_dlclose (libhandle);
193  return GNUNET_YES;
194 }
#define GNUNET_NO
Definition: gnunet_common.h:81
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:69
#define GNUNET_YES
Definition: gnunet_common.h:80
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 210 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().

211 {
212  void *libhandle;
213  struct PluginList *plug;
215  void *ret;
216 
217  if (!initialized)
218  {
220  plugin_init ();
221  }
222  libhandle = lt_dlopenext (library_name);
223  if (libhandle == NULL)
224  {
226  _("`%s' failed for library `%s' with error: %s\n"),
227  "lt_dlopenext",
228  library_name, lt_dlerror ());
229  return NULL;
230  }
231  plug = GNUNET_new (struct PluginList);
232  plug->handle = libhandle;
233  plug->name = GNUNET_strdup (library_name);
234  plug->next = plugins;
235  plugins = plug;
236  init = resolve_function (plug, "init");
237  if ((init == NULL) || (NULL == (ret = init (arg))))
238  {
239  lt_dlclose (libhandle);
240  GNUNET_free (plug->name);
241  plugins = plug->next;
242  GNUNET_free (plug);
243  return NULL;
244  }
245  return ret;
246 }
char * name
Name of the library.
Definition: plugin.c:46
#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
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:75
static int initialized
Have we been initialized?
Definition: plugin.c:58
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
#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:68
#define GNUNET_YES
Definition: gnunet_common.h:80
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 375 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().

377 {
378  struct LoadAllContext lac;
379  char *path;
380 
382  if (NULL == path)
383  {
385  _("Could not determine plugin installation path.\n"));
386  return;
387  }
388  lac.basename = basename;
389  lac.arg = arg;
390  lac.cb = cb;
391  lac.cb_cls = cb_cls;
393  GNUNET_free (path);
394 }
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:1233
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
void * cb_cls
Closure for cb.
Definition: plugin.c:319
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:333
#define GNUNET_log(kind,...)
GNUNET_PLUGIN_LoaderCallback cb
Function to call for each plugin.
Definition: plugin.c:314
Closure for find_libraries().
Definition: plugin.c:299
#define GNUNET_free(ptr)
Wrapper around free.
const char * basename
Prefix the plugin names we find have to match.
Definition: plugin.c:304
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 258 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().

260 {
261  struct PluginList *pos;
262  struct PluginList *prev;
264  void *ret;
265 
266  prev = NULL;
267  pos = plugins;
268  while ((NULL != pos) && (0 != strcmp (pos->name, library_name)))
269  {
270  prev = pos;
271  pos = pos->next;
272  }
273  if (NULL == pos)
274  return NULL;
275 
276  done = resolve_function (pos, "done");
277  ret = NULL;
278  if (NULL != done)
279  ret = done (arg);
280  if (NULL == prev)
281  plugins = pos->next;
282  else
283  prev->next = pos->next;
284  lt_dlclose (pos->handle);
285  GNUNET_free (pos->name);
286  GNUNET_free (pos);
287  if (NULL == plugins)
288  {
289  plugin_fini ();
291  }
292  return ret;
293 }
static void done()
char * name
Name of the library.
Definition: plugin.c:46
#define GNUNET_NO
Definition: gnunet_common.h:81
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 ret
Final status code.
Definition: gnunet-arm.c:89
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: