]> git.itanic.dy.fi Git - rrdd/commitdiff
plugin_manager: Load parser plugins by name
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Mon, 19 Nov 2012 19:45:39 +0000 (21:45 +0200)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Mon, 19 Nov 2012 20:10:12 +0000 (22:10 +0200)
A parser plugin is a plugin that has name "%s_parser.so", where "%s"
is the name of the parser. Implement a function that will try to find
a parser plugin by a name. If we can't find such plugin from the
global library path, try some other places as well, such as current
working directory and executable path.

In order to know the executable path, the plugin manager needs to be
initialized with the executable path. We are now doing it in the
beginning of the main function.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
main.c
plugin_manager.c
plugin_manager.h

diff --git a/main.c b/main.c
index 9f344e5f2824f0804c5bd2d82c7deab2380bc9a2..a1d74cacd69449bf28cdeb0fa065b0e4849a186d 100644 (file)
--- a/main.c
+++ b/main.c
@@ -11,6 +11,8 @@
 #include "config.h"
 #include "built_in_parsers.h"
 
+#include "plugin_manager.h"
+
 struct user_options {
        int max_jobs;
        char *config_file;
@@ -67,6 +69,7 @@ int main(int argc, char *argv[])
        if (read_args(argc, argv, &opts) < 0)
                return -1;
 
+       init_plugin_manager(argv[0]);
        register_built_in_parsers();
 
        if (!opts.config_file) {
index a645f4e61321ca01e8a922f139fea43bc42e237b..1c33951d10860f6d93e969ebc9c35fd29b0cf458 100644 (file)
@@ -1,9 +1,23 @@
+#include <stdlib.h>
 #include <dlfcn.h>
+#include <string.h>
+#include <libgen.h>
 
 #include "plugin_manager.h"
 #include "plugin.h"
 #include "debug.h"
 
+static char *exec_path;
+
+int init_plugin_manager(const char *_exec_path)
+{
+       char *str = strdup(_exec_path);
+       exec_path = strdup(dirname(str));
+
+       free(str);
+       return 0;
+}
+
 int load_plugin(const char *path)
 {
        struct plugin_info *info;
@@ -37,3 +51,39 @@ out:
 
        return ret;
 }
+
+/*
+ * Try to load a parser plugin with file name matching some of the
+ * following candidates (%s being the requested name):
+ *
+ * "%s_parser.so"
+ * "./%s_parser.so"
+ * "<exec_path>/%s_parser.so"
+ */
+int load_parser_plugin(const char *name)
+{
+       char str[256], parser[] = "_parser.so";
+       int ret;
+
+       strncpy(str, name, sizeof(str));
+       strncat(str, parser, sizeof(str) - 1);
+       ret = load_plugin(str);
+       if (!ret)
+               return 0;
+
+       strncpy(str, "./", sizeof(str));
+       strncat(str, name, sizeof(str) - 1);
+       strncat(str, parser, sizeof(str) - 1);
+       ret = load_plugin(str);
+       if (!ret)
+               return 0;
+
+       if (!exec_path)
+               return 0;
+
+       strncpy(str, exec_path, sizeof(str));
+       strncat(str, "/", sizeof(str) - 1);
+       strncat(str, name, sizeof(str) - 1);
+       strncat(str, parser, sizeof(str) - 1);
+       return load_plugin(str);
+}
index 5d894b53b641eab2863b15930e06d25333e2118d..2410ef5b798b90e69a5dcfd310813bdae5ad8824 100644 (file)
@@ -2,5 +2,7 @@
 #define _PLUGIN_MANAGER_H
 
 int load_plugin(const char *path);
+int load_parser_plugin(const char *name);
+int init_plugin_manager(const char *exec_path);
 
 #endif