From 5ba08dfef5404dd68f91a1acdd9dd97301791f99 Mon Sep 17 00:00:00 2001 From: Jeff Lucovsky Date: Sat, 15 May 2021 09:49:23 -0400 Subject: [PATCH] plugin: Refactor and create registration function This commit refactors the plugin registration code and creates an API for plugin registration --- src/suricata-plugin.h | 1 + src/util-plugin.c | 60 ++++++++++++++++++++++++++++--------------- src/util-plugin.h | 2 ++ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/suricata-plugin.h b/src/suricata-plugin.h index 1a68d2e255..860af8d221 100644 --- a/src/suricata-plugin.h +++ b/src/suricata-plugin.h @@ -63,6 +63,7 @@ typedef struct SCPluginFileType_ { } SCPluginFileType; bool SCPluginRegisterFileType(SCPluginFileType *); +bool SCRegisterEveFileType(SCPluginFileType *); typedef struct SCCapturePlugin_ { char *name; diff --git a/src/util-plugin.c b/src/util-plugin.c index 912cb2f006..0ed9ce225c 100644 --- a/src/util-plugin.c +++ b/src/util-plugin.c @@ -44,6 +44,27 @@ static TAILQ_HEAD(, SCPluginFileType_) output_types = static TAILQ_HEAD(, SCCapturePlugin_) capture_plugins = TAILQ_HEAD_INITIALIZER(capture_plugins); +bool RegisterPlugin(SCPlugin *plugin, void *lib) +{ + 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 allocate memory for plugin"); + return false; + } + 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 true; +} + static void InitPlugin(char *path) { void *lib = dlopen(path, RTLD_NOW); @@ -58,30 +79,12 @@ static void InitPlugin(char *path) dlclose(lib); return; } - SCPlugin *plugin = (*plugin_register)(); - if (plugin == NULL) { - SCLogError(SC_ERR_PLUGIN, "Plugin registration failed: %s", path); - dlclose(lib); - 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"); + if (!RegisterPlugin(plugin_register(), lib)) { + SCLogError(SC_ERR_PLUGIN, "Plugin registration failed: %s", path); 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)(); } } @@ -139,6 +142,23 @@ void SCPluginsLoad(const char *capture_plugin_name, const char *capture_plugin_a } } +bool SCRegisterEveFileType(SCPluginFileType *plugin) +{ + SCPluginFileType *existing = NULL; + TAILQ_FOREACH (existing, &output_types, entries) { + if (strcmp(existing->name, plugin->name) == 0) { + SCLogNotice("EVE file type plugin name conflicts with previously " + "registered plugin: %s", + plugin->name); + return false; + } + } + + SCLogDebug("Registering EVE file type plugin %s", plugin->name); + TAILQ_INSERT_TAIL(&output_types, plugin, entries); + return true; +} + /** * \brief Register an Eve/JSON file type plugin. * diff --git a/src/util-plugin.h b/src/util-plugin.h index 61c870be9c..7415eb68e4 100644 --- a/src/util-plugin.h +++ b/src/util-plugin.h @@ -25,4 +25,6 @@ void SCPluginsLoad(const char *capture_plugin_name, const char *capture_plugin_a SCPluginFileType *SCPluginFindFileType(const char *name); SCCapturePlugin *SCPluginFindCaptureByName(const char *name); +bool RegisterPlugin(SCPlugin *, void *); + #endif /* __UTIL_PLUGIN_H__ */