From: Jason Ish Date: Tue, 25 Aug 2020 19:52:00 +0000 (-0600) Subject: plugins: require registration function SCPluginRegister X-Git-Tag: suricata-6.0.0-rc1~73 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=665328b29ef87468433bc215c2ae0d8768a98a41;p=thirdparty%2Fsuricata.git 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. --- diff --git a/src/util-plugin.c b/src/util-plugin.c index 3e34e05a86..14f22d23d0 100644 --- a/src/util-plugin.c +++ b/src/util-plugin.c @@ -23,6 +23,8 @@ #include +typedef SCPlugin *(*SCPluginRegisterFunc)(void); + typedef struct PluginListNode_ { SCPlugin *plugin; void *lib; @@ -49,29 +51,37 @@ static void InitPlugin(char *path) SCLogNotice("Failed to open %s as a plugin: %s", path, dlerror()); } else { 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) { - SCLogError(SC_ERR_PLUGIN, "Plugin does not export a plugin specification: %s", path); + SCLogError(SC_ERR_PLUGIN, "Plugin registration failed: %s", path); dlclose(lib); - } else { - BUG_ON(plugin->name == NULL); - BUG_ON(plugin->author == NULL); - BUG_ON(plugin->license == NULL); - BUG_ON(plugin->Init == NULL); - - PluginListNode *node = SCCalloc(1, sizeof(*node)); - if (node == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocated memory for plugin"); - dlclose(lib); - return; - } - 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)(); + return; + } + + BUG_ON(plugin->name == NULL); + BUG_ON(plugin->author == NULL); + BUG_ON(plugin->license == NULL); + BUG_ON(plugin->Init == NULL); + + PluginListNode *node = SCCalloc(1, sizeof(*node)); + if (node == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocated memory for plugin"); + dlclose(lib); + return; } + 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)(); } }