GNUnet  0.10.x
Data Structures | Macros | Functions | Variables
plugin.c File Reference

Methods to access plugins. More...

#include "platform.h"
#include <ltdl.h>
#include "gnunet_util_lib.h"
Include dependency graph for plugin.c:

Go to the source code of this file.

Data Structures

struct  PluginList
 Linked list of active plugins. More...
 
struct  LoadAllContext
 Closure for find_libraries(). More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from(kind, "util-plugin", __VA_ARGS__)
 

Functions

static void plugin_init ()
 Setup libtool paths. More...
 
static void plugin_fini ()
 Shutdown libtool. More...
 
static GNUNET_PLUGIN_Callback resolve_function (struct PluginList *plug, const char *name)
 Lookup a function in the plugin. More...
 
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_unload (const char *library_name, void *arg)
 Unload plugin (runs the done callback and returns whatever done returned). More...
 
static int find_libraries (void *cls, const char *filename)
 Function called on each plugin in the directory. 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...
 

Variables

static int initialized
 Have we been initialized? More...
 
static char * old_dlsearchpath
 Libtool search path before we started. More...
 
static struct PluginListplugins
 List of plugins we have loaded. More...
 

Detailed Description

Methods to access plugins.

Author
Christian Grothoff

Definition in file plugin.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from(kind, "util-plugin", __VA_ARGS__)

Definition at line 31 of file plugin.c.

Referenced by GNUNET_PLUGIN_load(), and resolve_function().

Function Documentation

◆ plugin_init()

static void plugin_init ( )
static

Setup libtool paths.

Definition at line 74 of file plugin.c.

References _, GNUNET_asprintf(), GNUNET_free, GNUNET_OS_installation_get_path(), GNUNET_OS_IPK_LIBDIR, GNUNET_strdup, and old_dlsearchpath.

Referenced by GNUNET_PLUGIN_load(), and GNUNET_PLUGIN_test().

75 {
76  int err;
77  const char *opath;
78  char *path;
79  char *cpath;
80 
81  err = lt_dlinit();
82  if (err > 0)
83  {
84  fprintf(stderr,
85  _("Initialization of plugin mechanism failed: %s!\n"),
86  lt_dlerror());
87  return;
88  }
89  opath = lt_dlgetsearchpath();
90  if (NULL != opath)
93  if (NULL != path)
94  {
95  if (NULL != opath)
96  {
97  GNUNET_asprintf(&cpath, "%s:%s", opath, path);
98  lt_dlsetsearchpath(cpath);
99  GNUNET_free(path);
100  GNUNET_free(cpath);
101  }
102  else
103  {
104  lt_dlsetsearchpath(path);
105  GNUNET_free(path);
106  }
107  }
108 }
Return the directory where libraries are installed.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
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.
#define GNUNET_free(ptr)
Wrapper around free.
static char * old_dlsearchpath
Libtool search path before we started.
Definition: plugin.c:62
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plugin_fini()

static void plugin_fini ( )
static

Shutdown libtool.

Definition at line 115 of file plugin.c.

References GNUNET_free, and old_dlsearchpath.

Referenced by GNUNET_PLUGIN_unload().

116 {
117  lt_dlsetsearchpath(old_dlsearchpath);
118  if (NULL != old_dlsearchpath)
119  {
121  old_dlsearchpath = NULL;
122  }
123  lt_dlexit();
124 }
#define GNUNET_free(ptr)
Wrapper around free.
static char * old_dlsearchpath
Libtool search path before we started.
Definition: plugin.c:62
Here is the caller graph for this function:

◆ resolve_function()

static GNUNET_PLUGIN_Callback resolve_function ( struct PluginList plug,
const char *  name 
)
static

Lookup a function in the plugin.

Parameters
plugthe plugin to check
namename of the symbol to look for
Returns
NULL if the symbol was not found

Definition at line 135 of file plugin.c.

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

Referenced by GNUNET_PLUGIN_load(), GNUNET_PLUGIN_test(), and GNUNET_PLUGIN_unload().

136 {
137  char *initName;
138  void *mptr;
139 
140  GNUNET_asprintf(&initName, "_%s_%s", plug->name, name);
141  mptr = lt_dlsym(plug->handle, &initName[1]);
142  if (NULL == mptr)
143  mptr = lt_dlsym(plug->handle, initName);
144  if (NULL == mptr)
146  _("`%s' failed to resolve method '%s' with error: %s\n"),
147  "lt_dlsym",
148  &initName[1],
149  lt_dlerror());
150  GNUNET_free(initName);
151  return mptr;
152 }
char * name
Name of the library.
Definition: plugin.c:45
void * handle
System handle.
Definition: plugin.c:50
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
#define LOG(kind,...)
Definition: plugin.c:31
const char * name
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_libraries()

static int find_libraries ( void *  cls,
const char *  filename 
)
static

Function called on each plugin in the directory.

Loads the plugins that match the given basename.

Parameters
clsthe struct LoadAllContext describing which plugins to load and what to do with them
filenamename of a plugin library to check
Returns
GNUNET_OK (continue loading)

Definition at line 328 of file plugin.c.

References LoadAllContext::arg, LoadAllContext::basename, LoadAllContext::cb, LoadAllContext::cb_cls, DIR_SEPARATOR_STR, filename, GNUNET_free, GNUNET_OK, GNUNET_PLUGIN_load(), and GNUNET_strdup.

Referenced by GNUNET_PLUGIN_load_all().

329 {
330  struct LoadAllContext *lac = cls;
331  const char *slashpos;
332  const char *libname;
333  char *basename;
334  char *dot;
335  void *lib_ret;
336  size_t n;
337 
338  libname = filename;
339  while (NULL != (slashpos = strstr(libname, DIR_SEPARATOR_STR)))
340  libname = slashpos + 1;
341  n = strlen(libname);
342  if (0 != strncmp(lac->basename, libname, strlen(lac->basename)))
343  return GNUNET_OK; /* wrong name */
344  if ((n > 3) && (0 == strcmp(&libname[n - 3], ".la")))
345  return GNUNET_OK; /* .la file */
346  basename = GNUNET_strdup(libname);
347  if (NULL != (dot = strstr(basename, ".")))
348  *dot = '\0';
349  lib_ret = GNUNET_PLUGIN_load(basename, lac->arg);
350  if (NULL != lib_ret)
351  lac->cb(lac->cb_cls, basename, lib_ret);
352  GNUNET_free(basename);
353  return GNUNET_OK;
354 }
void * arg
Argument to give to &#39;init&#39; when loading the plugin.
Definition: plugin.c:304
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
void * cb_cls
Closure for cb.
Definition: plugin.c:314
#define DIR_SEPARATOR_STR
Definition: platform.h:168
static char * filename
GNUNET_PLUGIN_LoaderCallback cb
Function to call for each plugin.
Definition: plugin.c:309
Closure for find_libraries().
Definition: plugin.c:295
void * GNUNET_PLUGIN_load(const char *library_name, void *arg)
Setup plugin (runs the init callback and returns whatever init returned).
Definition: plugin.c:206
#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:

Variable Documentation

◆ initialized

int initialized
static

Have we been initialized?

Definition at line 57 of file plugin.c.

Referenced by GNUNET_PLUGIN_load(), GNUNET_PLUGIN_test(), and GNUNET_PLUGIN_unload().

◆ old_dlsearchpath

char* old_dlsearchpath
static

Libtool search path before we started.

Definition at line 62 of file plugin.c.

Referenced by plugin_fini(), and plugin_init().

◆ plugins

struct PluginList* plugins
static

List of plugins we have loaded.

Definition at line 67 of file plugin.c.

Referenced by extract_files(), GNUNET_PLUGIN_load(), GNUNET_PLUGIN_unload(), and main().