plugins: require registration function SCPluginRegister

Instead of looking for a symbol, "PluginSpec" look for a function
named SCPluginRegister that returns a SCPlugin.

This makes it much easier to create Rust plugins without having
to deal with dlopen constructors and such, which is rather
straight forward in C, but a bit of advanced boilerplate in Rust
that can be eliminated by simply calling a registration function.
pull/5356/head
Jason Ish 6 years ago committed by Victor Julien
parent 647e304f4b
commit 665328b29e

@ -23,6 +23,8 @@
#include <dlfcn.h> #include <dlfcn.h>
typedef SCPlugin *(*SCPluginRegisterFunc)(void);
typedef struct PluginListNode_ { typedef struct PluginListNode_ {
SCPlugin *plugin; SCPlugin *plugin;
void *lib; void *lib;
@ -49,29 +51,37 @@ static void InitPlugin(char *path)
SCLogNotice("Failed to open %s as a plugin: %s", path, dlerror()); SCLogNotice("Failed to open %s as a plugin: %s", path, dlerror());
} else { } else {
SCLogNotice("Loading plugin %s", path); SCLogNotice("Loading plugin %s", path);
SCPlugin *plugin = dlsym(lib, "PluginSpec");
SCPluginRegisterFunc plugin_register = dlsym(lib, "SCPluginRegister");
if (plugin_register == NULL) {
SCLogError(SC_ERR_PLUGIN, "Plugin does not export SCPluginRegister function: %s", path);
dlclose(lib);
return;
}
SCPlugin *plugin = (*plugin_register)();
if (plugin == NULL) { if (plugin == NULL) {
SCLogError(SC_ERR_PLUGIN, "Plugin does not export a plugin specification: %s", path); SCLogError(SC_ERR_PLUGIN, "Plugin registration failed: %s", path);
dlclose(lib); dlclose(lib);
} else { return;
BUG_ON(plugin->name == NULL); }
BUG_ON(plugin->author == NULL);
BUG_ON(plugin->license == NULL); BUG_ON(plugin->name == NULL);
BUG_ON(plugin->Init == NULL); BUG_ON(plugin->author == NULL);
BUG_ON(plugin->license == NULL);
PluginListNode *node = SCCalloc(1, sizeof(*node)); BUG_ON(plugin->Init == NULL);
if (node == NULL) {
SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocated memory for plugin"); PluginListNode *node = SCCalloc(1, sizeof(*node));
dlclose(lib); if (node == NULL) {
return; SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocated memory for plugin");
} dlclose(lib);
node->plugin = plugin; return;
node->lib = lib;
TAILQ_INSERT_TAIL(&plugins, node, entries);
SCLogNotice("Initializing plugin %s; author=%s; license=%s",
plugin->name, plugin->author, plugin->license);
(*plugin->Init)();
} }
node->plugin = plugin;
node->lib = lib;
TAILQ_INSERT_TAIL(&plugins, node, entries);
SCLogNotice("Initializing plugin %s; author=%s; license=%s", plugin->name, plugin->author,
plugin->license);
(*plugin->Init)();
} }
} }

Loading…
Cancel
Save