]> git.itanic.dy.fi Git - rrdd/blobdiff - plugin_manager.c
plugin_manager: Load parser plugins by name
[rrdd] / plugin_manager.c
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);
+}