]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
plugins: require registration function SCPluginRegister
authorJason Ish <jason.ish@oisf.net>
Tue, 25 Aug 2020 19:52:00 +0000 (13:52 -0600)
committerVictor Julien <victor@inliniac.net>
Thu, 3 Sep 2020 11:04:14 +0000 (13:04 +0200)
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.

src/util-plugin.c

index 3e34e05a865035fd869fc70c601444fcd1ece9a6..14f22d23d0bc4233c8b8947dcaffb5f68f4e870c 100644 (file)
@@ -23,6 +23,8 @@
 
 #include <dlfcn.h>
 
+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)();
     }
 }