plugin.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2002-2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/plugin.c
  18. * @brief Methods to access plugins
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include <ltdl.h>
  23. #include "gnunet_util_lib.h"
  24. #define LOG(kind,...) GNUNET_log_from (kind, "util-plugin", __VA_ARGS__)
  25. /**
  26. * Linked list of active plugins.
  27. */
  28. struct PluginList
  29. {
  30. /**
  31. * This is a linked list.
  32. */
  33. struct PluginList *next;
  34. /**
  35. * Name of the library.
  36. */
  37. char *name;
  38. /**
  39. * System handle.
  40. */
  41. void *handle;
  42. };
  43. /**
  44. * Have we been initialized?
  45. */
  46. static int initialized;
  47. /**
  48. * Libtool search path before we started.
  49. */
  50. static char *old_dlsearchpath;
  51. /**
  52. * List of plugins we have loaded.
  53. */
  54. static struct PluginList *plugins;
  55. /**
  56. * Setup libtool paths.
  57. */
  58. static void
  59. plugin_init ()
  60. {
  61. int err;
  62. const char *opath;
  63. char *path;
  64. char *cpath;
  65. err = lt_dlinit ();
  66. if (err > 0)
  67. {
  68. FPRINTF (stderr,
  69. _("Initialization of plugin mechanism failed: %s!\n"),
  70. lt_dlerror ());
  71. return;
  72. }
  73. opath = lt_dlgetsearchpath ();
  74. if (NULL != opath)
  75. old_dlsearchpath = GNUNET_strdup (opath);
  76. path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
  77. if (NULL != path)
  78. {
  79. if (NULL != opath)
  80. {
  81. GNUNET_asprintf (&cpath, "%s:%s", opath, path);
  82. lt_dlsetsearchpath (cpath);
  83. GNUNET_free (path);
  84. GNUNET_free (cpath);
  85. }
  86. else
  87. {
  88. lt_dlsetsearchpath (path);
  89. GNUNET_free (path);
  90. }
  91. }
  92. }
  93. /**
  94. * Shutdown libtool.
  95. */
  96. static void
  97. plugin_fini ()
  98. {
  99. lt_dlsetsearchpath (old_dlsearchpath);
  100. if (NULL != old_dlsearchpath)
  101. {
  102. GNUNET_free (old_dlsearchpath);
  103. old_dlsearchpath = NULL;
  104. }
  105. lt_dlexit ();
  106. }
  107. /**
  108. * Lookup a function in the plugin.
  109. *
  110. * @param plug the plugin to check
  111. * @param name name of the symbol to look for
  112. * @return NULL if the symbol was not found
  113. */
  114. static GNUNET_PLUGIN_Callback
  115. resolve_function (struct PluginList *plug,
  116. const char *name)
  117. {
  118. char *initName;
  119. void *mptr;
  120. GNUNET_asprintf (&initName,
  121. "_%s_%s",
  122. plug->name,
  123. name);
  124. mptr = lt_dlsym (plug->handle, &initName[1]);
  125. if (NULL == mptr)
  126. mptr = lt_dlsym (plug->handle, initName);
  127. if (NULL == mptr)
  128. LOG (GNUNET_ERROR_TYPE_ERROR,
  129. _("`%s' failed to resolve method '%s' with error: %s\n"),
  130. "lt_dlsym",
  131. &initName[1], lt_dlerror ());
  132. GNUNET_free (initName);
  133. return mptr;
  134. }
  135. /**
  136. * Test if a plugin exists.
  137. *
  138. * Note that the library must export a symbol called
  139. * `library_name_init` for the test to succeed.
  140. *
  141. * @param library_name name of the plugin to test if it is installed
  142. * @return #GNUNET_YES if the plugin exists, #GNUNET_NO if not
  143. */
  144. int
  145. GNUNET_PLUGIN_test (const char *library_name)
  146. {
  147. void *libhandle;
  148. GNUNET_PLUGIN_Callback init;
  149. struct PluginList plug;
  150. if (! initialized)
  151. {
  152. initialized = GNUNET_YES;
  153. plugin_init ();
  154. }
  155. libhandle = lt_dlopenext (library_name);
  156. if (NULL == libhandle)
  157. return GNUNET_NO;
  158. plug.handle = libhandle;
  159. plug.name = (char *) library_name;
  160. init = resolve_function (&plug, "init");
  161. if (NULL == init)
  162. {
  163. GNUNET_break (0);
  164. lt_dlclose (libhandle);
  165. return GNUNET_NO;
  166. }
  167. lt_dlclose (libhandle);
  168. return GNUNET_YES;
  169. }
  170. /**
  171. * Setup plugin (runs the `init` callback and returns whatever `init`
  172. * returned). If `init` returns NULL, the plugin is unloaded.
  173. *
  174. * Note that the library must export symbols called
  175. * `library_name_init` and `library_name_done`. These will be called
  176. * when the library is loaded and unloaded respectively.
  177. *
  178. * @param library_name name of the plugin to load
  179. * @param arg argument to the plugin initialization function
  180. * @return whatever the initialization function returned
  181. */
  182. void *
  183. GNUNET_PLUGIN_load (const char *library_name, void *arg)
  184. {
  185. void *libhandle;
  186. struct PluginList *plug;
  187. GNUNET_PLUGIN_Callback init;
  188. void *ret;
  189. if (!initialized)
  190. {
  191. initialized = GNUNET_YES;
  192. plugin_init ();
  193. }
  194. libhandle = lt_dlopenext (library_name);
  195. if (libhandle == NULL)
  196. {
  197. LOG (GNUNET_ERROR_TYPE_ERROR,
  198. _("`%s' failed for library `%s' with error: %s\n"),
  199. "lt_dlopenext",
  200. library_name, lt_dlerror ());
  201. return NULL;
  202. }
  203. plug = GNUNET_new (struct PluginList);
  204. plug->handle = libhandle;
  205. plug->name = GNUNET_strdup (library_name);
  206. plug->next = plugins;
  207. plugins = plug;
  208. init = resolve_function (plug, "init");
  209. if ((init == NULL) || (NULL == (ret = init (arg))))
  210. {
  211. lt_dlclose (libhandle);
  212. GNUNET_free (plug->name);
  213. plugins = plug->next;
  214. GNUNET_free (plug);
  215. return NULL;
  216. }
  217. return ret;
  218. }
  219. /**
  220. * Unload plugin (runs the `done` callback and returns whatever `done`
  221. * returned). The plugin is then unloaded.
  222. *
  223. * @param library_name name of the plugin to unload
  224. * @param arg argument to the plugin shutdown function
  225. * @return whatever the shutdown function returned
  226. */
  227. void *
  228. GNUNET_PLUGIN_unload (const char *library_name,
  229. void *arg)
  230. {
  231. struct PluginList *pos;
  232. struct PluginList *prev;
  233. GNUNET_PLUGIN_Callback done;
  234. void *ret;
  235. prev = NULL;
  236. pos = plugins;
  237. while ((NULL != pos) && (0 != strcmp (pos->name, library_name)))
  238. {
  239. prev = pos;
  240. pos = pos->next;
  241. }
  242. if (NULL == pos)
  243. return NULL;
  244. done = resolve_function (pos, "done");
  245. ret = NULL;
  246. if (NULL != done)
  247. ret = done (arg);
  248. if (NULL == prev)
  249. plugins = pos->next;
  250. else
  251. prev->next = pos->next;
  252. lt_dlclose (pos->handle);
  253. GNUNET_free (pos->name);
  254. GNUNET_free (pos);
  255. if (NULL == plugins)
  256. {
  257. plugin_fini ();
  258. initialized = GNUNET_NO;
  259. }
  260. return ret;
  261. }
  262. /**
  263. * Closure for #find_libraries().
  264. */
  265. struct LoadAllContext
  266. {
  267. /**
  268. * Prefix the plugin names we find have to match.
  269. */
  270. const char *basename;
  271. /**
  272. * Argument to give to 'init' when loading the plugin.
  273. */
  274. void *arg;
  275. /**
  276. * Function to call for each plugin.
  277. */
  278. GNUNET_PLUGIN_LoaderCallback cb;
  279. /**
  280. * Closure for @e cb
  281. */
  282. void *cb_cls;
  283. };
  284. /**
  285. * Function called on each plugin in the directory. Loads
  286. * the plugins that match the given basename.
  287. *
  288. * @param cls the `struct LoadAllContext` describing which
  289. * plugins to load and what to do with them
  290. * @param filename name of a plugin library to check
  291. * @return #GNUNET_OK (continue loading)
  292. */
  293. static int
  294. find_libraries (void *cls, const char *filename)
  295. {
  296. struct LoadAllContext *lac = cls;
  297. const char *slashpos;
  298. const char *libname;
  299. char *basename;
  300. char *dot;
  301. void *lib_ret;
  302. size_t n;
  303. libname = filename;
  304. while (NULL != (slashpos = strstr (libname, DIR_SEPARATOR_STR)))
  305. libname = slashpos + 1;
  306. n = strlen (libname);
  307. if (0 != strncmp (lac->basename, libname, strlen (lac->basename)))
  308. return GNUNET_OK; /* wrong name */
  309. if ((n > 3) && (0 == strcmp (&libname[n - 3], ".la")))
  310. return GNUNET_OK; /* .la file */
  311. basename = GNUNET_strdup (libname);
  312. if (NULL != (dot = strstr (basename, ".")))
  313. *dot = '\0';
  314. lib_ret = GNUNET_PLUGIN_load (basename, lac->arg);
  315. if (NULL != lib_ret)
  316. lac->cb (lac->cb_cls, basename, lib_ret);
  317. GNUNET_free (basename);
  318. return GNUNET_OK;
  319. }
  320. /**
  321. * Load all compatible plugins with the given base name.
  322. *
  323. * Note that the library must export symbols called
  324. * `basename_ANYTHING_init` and `basename_ANYTHING__done`. These will
  325. * be called when the library is loaded and unloaded respectively.
  326. *
  327. * @param basename basename of the plugins to load
  328. * @param arg argument to the plugin initialization function
  329. * @param cb function to call for each plugin found
  330. * @param cb_cls closure for @a cb
  331. */
  332. void
  333. GNUNET_PLUGIN_load_all (const char *basename, void *arg,
  334. GNUNET_PLUGIN_LoaderCallback cb, void *cb_cls)
  335. {
  336. struct LoadAllContext lac;
  337. char *path;
  338. path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
  339. if (NULL == path)
  340. {
  341. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  342. _("Could not determine plugin installation path.\n"));
  343. return;
  344. }
  345. lac.basename = basename;
  346. lac.arg = arg;
  347. lac.cb = cb;
  348. lac.cb_cls = cb_cls;
  349. GNUNET_DISK_directory_scan (path, &find_libraries, &lac);
  350. GNUNET_free (path);
  351. }
  352. /* end of plugin.c */