GNUnet 0.22.2
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...
 
static void * open_library (const struct GNUNET_OS_ProjectData *pd, const char *library_name)
 Open library library_name using search path from pd. More...
 
enum GNUNET_GenericReturnValue GNUNET_PLUGIN_test (const struct GNUNET_OS_ProjectData *pd, const char *library_name)
 Test if a plugin exists. More...
 
void * GNUNET_PLUGIN_load (const struct GNUNET_OS_ProjectData *pd, 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 enum GNUNET_GenericReturnValue find_libraries (void *cls, const char *filename)
 Function called on each plugin in the directory. More...
 
void GNUNET_PLUGIN_load_all (const struct GNUNET_OS_ProjectData *pd, 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 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 30 of file plugin.c.

Function Documentation

◆ plugin_init()

static void plugin_init ( void  )
static

Setup libtool paths.

Definition at line 69 of file plugin.c.

70{
71 int err;
72
73 err = lt_dlinit ();
74 if (err > 0)
75 {
76 fprintf (stderr,
77 _ ("Initialization of plugin mechanism failed: %s!\n"),
78 lt_dlerror ());
79 return;
80 }
81}
#define _(String)
GNU gettext support macro.
Definition: platform.h:178

References _.

Referenced by GNUNET_PLUGIN_load(), and GNUNET_PLUGIN_test().

Here is the caller graph for this function:

◆ plugin_fini()

static void plugin_fini ( void  )
static

Shutdown libtool.

Definition at line 88 of file plugin.c.

89{
90 if (NULL == getenv ("VALGRINDING_PLUGINS"))
91 lt_dlexit ();
92}
char * getenv()

References getenv().

Referenced by GNUNET_PLUGIN_load(), GNUNET_PLUGIN_test(), and 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 103 of file plugin.c.

105{
106 char *initName;
107 void *mptr;
108
109 GNUNET_asprintf (&initName,
110 "_%s_%s",
111 plug->name,
112 name);
113 mptr = lt_dlsym (plug->handle,
114 &initName[1]);
115 if (NULL == mptr)
116 mptr = lt_dlsym (plug->handle,
117 initName);
118 if (NULL == mptr)
120 _ ("`%s' failed to resolve method '%s' with error: %s\n"),
121 "lt_dlsym",
122 &initName[1],
123 lt_dlerror ());
124 GNUNET_free (initName);
125 return mptr;
126}
static char * name
Name (label) of the records to list.
@ GNUNET_ERROR_TYPE_ERROR
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_free(ptr)
Wrapper around free.
#define LOG(kind,...)
Definition: plugin.c:30
char * name
Name of the library.
Definition: plugin.c:45
void * handle
System handle.
Definition: plugin.c:50

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

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

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

◆ open_library()

static void * open_library ( const struct GNUNET_OS_ProjectData pd,
const char *  library_name 
)
static

Open library library_name using search path from pd.

Parameters
pdprogram data with paths
library_namename of plugin to load
Returns
NULL on error, otherwise library handle

Definition at line 137 of file plugin.c.

139{
140 void *libhandle;
141 const char *opath;
142 char *path;
143 char *cpath;
144
145 opath = lt_dlgetsearchpath ();
148 if (NULL != path)
149 {
150 if (NULL != opath)
151 {
152 GNUNET_asprintf (&cpath,
153 "%s:%s",
154 opath,
155 path);
156 lt_dlsetsearchpath (cpath);
157 GNUNET_free (cpath);
158 }
159 else
160 {
161 lt_dlsetsearchpath (path);
162 }
163 }
164 GNUNET_free (path);
165 libhandle = lt_dlopenext (library_name);
166 lt_dlsetsearchpath (opath);
167 return libhandle;
168}
char * GNUNET_OS_installation_get_path(const struct GNUNET_OS_ProjectData *pd, 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.

References GNUNET_asprintf(), GNUNET_free, GNUNET_OS_installation_get_path(), and GNUNET_OS_IPK_LIBDIR.

Referenced by GNUNET_PLUGIN_load(), and GNUNET_PLUGIN_test().

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

◆ find_libraries()

static enum GNUNET_GenericReturnValue 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 362 of file plugin.c.

364{
365 struct LoadAllContext *lac = cls;
366 const char *slashpos;
367 const char *libname;
368 char *basename;
369 char *dot;
370 void *lib_ret;
371 size_t n;
372
373 libname = filename;
374 while (NULL != (slashpos = strstr (libname,
376 libname = slashpos + 1;
377 n = strlen (libname);
378 if (0 != strncmp (lac->basename,
379 libname,
380 strlen (lac->basename)))
381 return GNUNET_OK; /* wrong name */
382 if ( (n > 3) &&
383 (0 == strcmp (&libname[n - 3], ".la")) )
384 return GNUNET_OK; /* .la file */
385 basename = GNUNET_strdup (libname);
386 if (NULL != (dot = strstr (basename, ".")))
387 *dot = '\0';
388 lib_ret = GNUNET_PLUGIN_load (lac->pd,
389 basename,
390 lac->arg);
391 if (NULL != lib_ret)
392 lac->cb (lac->cb_cls,
393 basename,
394 lib_ret);
396 return GNUNET_OK;
397}
static char * filename
@ GNUNET_OK
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
void * GNUNET_PLUGIN_load(const struct GNUNET_OS_ProjectData *pd, const char *library_name, void *arg)
Setup plugin (runs the "init" callback and returns whatever "init" returned).
Definition: plugin.c:221
#define DIR_SEPARATOR_STR
Definition: platform.h:166
Closure for find_libraries().
Definition: plugin.c:323
const char * basename
Prefix the plugin names we find have to match.
Definition: plugin.c:333
void * cb_cls
Closure for cb.
Definition: plugin.c:348
const struct GNUNET_OS_ProjectData * pd
Project data to determine load paths.
Definition: plugin.c:328
void * arg
Argument to give to 'init' when loading the plugin.
Definition: plugin.c:338
GNUNET_PLUGIN_LoaderCallback cb
Function to call for each plugin.
Definition: plugin.c:343

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

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

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

◆ plugins

struct PluginList* plugins
static

List of plugins we have loaded.

Definition at line 62 of file plugin.c.

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