GNUnet  0.20.0
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 (void)
 Setup libtool paths. More...
 
static void plugin_fini (void)
 Shutdown libtool. More...
 
static GNUNET_PLUGIN_Callback resolve_function (struct PluginList *plug, const char *name)
 Lookup a function in the plugin. More...
 
enum GNUNET_GenericReturnValue 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...
 
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...
 

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

Function Documentation

◆ plugin_init()

static void plugin_init ( void  )
static

Setup libtool paths.

Definition at line 76 of file plugin.c.

77 {
78  int err;
79  const char *opath;
80  char *path;
81  char *cpath;
82 
83  err = lt_dlinit ();
84  if (err > 0)
85  {
86  fprintf (stderr,
87  _ ("Initialization of plugin mechanism failed: %s!\n"),
88  lt_dlerror ());
89  return;
90  }
91  opath = lt_dlgetsearchpath ();
92  if (NULL != opath)
95  if (NULL != path)
96  {
97  if (NULL != opath)
98  {
99  GNUNET_asprintf (&cpath,
100  "%s:%s",
101  opath,
102  path);
103  lt_dlsetsearchpath (cpath);
104  GNUNET_free (path);
105  GNUNET_free (cpath);
106  }
107  else
108  {
109  lt_dlsetsearchpath (path);
110  GNUNET_free (path);
111  }
112  }
113 }
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_free(ptr)
Wrapper around free.
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.
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
static char * old_dlsearchpath
Libtool search path before we started.
Definition: plugin.c:64

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

Referenced by GNUNET_PLUGIN_load().

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

◆ plugin_fini()

static void plugin_fini ( void  )
static

Shutdown libtool.

Definition at line 120 of file plugin.c.

121 {
122  lt_dlsetsearchpath (old_dlsearchpath);
123  if (NULL != old_dlsearchpath)
124  {
126  old_dlsearchpath = NULL;
127  }
128  if (NULL == getenv ("VALGRINDING_PLUGINS"))
129  lt_dlexit ();
130 }
char * getenv()

References getenv(), GNUNET_free, and old_dlsearchpath.

Referenced by GNUNET_PLUGIN_unload().

Here is the call graph for this function:
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 141 of file plugin.c.

143 {
144  char *initName;
145  void *mptr;
146 
147  GNUNET_asprintf (&initName,
148  "_%s_%s",
149  plug->name,
150  name);
151  mptr = lt_dlsym (plug->handle,
152  &initName[1]);
153  if (NULL == mptr)
154  mptr = lt_dlsym (plug->handle,
155  initName);
156  if (NULL == mptr)
158  _ ("`%s' failed to resolve method '%s' with error: %s\n"),
159  "lt_dlsym",
160  &initName[1],
161  lt_dlerror ());
162  GNUNET_free (initName);
163  return mptr;
164 }
@ GNUNET_ERROR_TYPE_ERROR
#define LOG(kind,...)
Definition: plugin.c:32
const char * name
char * name
Name of the library.
Definition: plugin.c:47
void * handle
System handle.
Definition: plugin.c:52

Referenced by GNUNET_PLUGIN_load(), and GNUNET_PLUGIN_unload().

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

323 {
324  struct LoadAllContext *lac = cls;
325  const char *slashpos;
326  const char *libname;
327  char *basename;
328  char *dot;
329  void *lib_ret;
330  size_t n;
331 
332  libname = filename;
333  while (NULL != (slashpos = strstr (libname,
335  libname = slashpos + 1;
336  n = strlen (libname);
337  if (0 != strncmp (lac->basename,
338  libname,
339  strlen (lac->basename)))
340  return GNUNET_OK; /* wrong name */
341  if ( (n > 3) &&
342  (0 == strcmp (&libname[n - 3], ".la")) )
343  return GNUNET_OK; /* .la file */
344  basename = GNUNET_strdup (libname);
345  if (NULL != (dot = strstr (basename, ".")))
346  *dot = '\0';
347  lib_ret = GNUNET_PLUGIN_load (basename,
348  lac->arg);
349  if (NULL != lib_ret)
350  lac->cb (lac->cb_cls,
351  basename,
352  lib_ret);
354  return GNUNET_OK;
355 }
static char * filename
@ GNUNET_OK
void * GNUNET_PLUGIN_load(const char *library_name, void *arg)
Setup plugin (runs the "init" callback and returns whatever "init" returned).
Definition: plugin.c:198
#define DIR_SEPARATOR_STR
Definition: platform.h:166
Closure for find_libraries().
Definition: plugin.c:288
const char * basename
Prefix the plugin names we find have to match.
Definition: plugin.c:292
void * cb_cls
Closure for cb.
Definition: plugin.c:307
void * arg
Argument to give to 'init' when loading the plugin.
Definition: plugin.c:297
GNUNET_PLUGIN_LoaderCallback cb
Function to call for each plugin.
Definition: plugin.c:302

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

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

Referenced by GNUNET_PLUGIN_load(), and GNUNET_PLUGIN_unload().

◆ old_dlsearchpath

char* old_dlsearchpath
static

Libtool search path before we started.

Definition at line 64 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 69 of file plugin.c.

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