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

References _, FPRINTF, 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().

76 {
77  int err;
78  const char *opath;
79  char *path;
80  char *cpath;
81 
82  err = lt_dlinit ();
83  if (err > 0)
84  {
85  FPRINTF (stderr,
86  _("Initialization of plugin mechanism failed: %s!\n"),
87  lt_dlerror ());
88  return;
89  }
90  opath = lt_dlgetsearchpath ();
91  if (NULL != opath)
94  if (NULL != path)
95  {
96  if (NULL != opath)
97  {
98  GNUNET_asprintf (&cpath, "%s:%s", opath, path);
99  lt_dlsetsearchpath (cpath);
100  GNUNET_free (path);
101  GNUNET_free (cpath);
102  }
103  else
104  {
105  lt_dlsetsearchpath (path);
106  GNUNET_free (path);
107  }
108  }
109 }
#define FPRINTF
Definition: plibc.h:683
Return the directory where libraries are installed.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
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:63
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 116 of file plugin.c.

References GNUNET_free, and old_dlsearchpath.

Referenced by GNUNET_PLUGIN_unload().

117 {
118  lt_dlsetsearchpath (old_dlsearchpath);
119  if (NULL != old_dlsearchpath)
120  {
122  old_dlsearchpath = NULL;
123  }
124  lt_dlexit ();
125 }
#define GNUNET_free(ptr)
Wrapper around free.
static char * old_dlsearchpath
Libtool search path before we started.
Definition: plugin.c:63
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 136 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().

138 {
139  char *initName;
140  void *mptr;
141 
142  GNUNET_asprintf (&initName,
143  "_%s_%s",
144  plug->name,
145  name);
146  mptr = lt_dlsym (plug->handle, &initName[1]);
147  if (NULL == mptr)
148  mptr = lt_dlsym (plug->handle, initName);
149  if (NULL == mptr)
151  _("`%s' failed to resolve method '%s' with error: %s\n"),
152  "lt_dlsym",
153  &initName[1], lt_dlerror ());
154  GNUNET_free (initName);
155  return mptr;
156 }
char * name
Name of the library.
Definition: plugin.c:46
void * handle
System handle.
Definition: plugin.c:51
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
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 333 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().

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

Variable Documentation

◆ initialized

int initialized
static

Have we been initialized?

Definition at line 58 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 63 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 68 of file plugin.c.

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