]> git.itanic.dy.fi Git - rrdd/blob - plugin_manager.c
1c33951d10860f6d93e969ebc9c35fd29b0cf458
[rrdd] / plugin_manager.c
1 #include <stdlib.h>
2 #include <dlfcn.h>
3 #include <string.h>
4 #include <libgen.h>
5
6 #include "plugin_manager.h"
7 #include "plugin.h"
8 #include "debug.h"
9
10 static char *exec_path;
11
12 int init_plugin_manager(const char *_exec_path)
13 {
14         char *str = strdup(_exec_path);
15         exec_path = strdup(dirname(str));
16
17         free(str);
18         return 0;
19 }
20
21 int load_plugin(const char *path)
22 {
23         struct plugin_info *info;
24         void *handle;
25         int ret = -1;
26
27         handle = dlopen(path, RTLD_NOW);
28         if (!handle) {
29                 pr_err("Failed to load plugin %s: %s\n", path, dlerror());
30                 return -1;
31         }
32
33         info = dlsym(handle, "plugin_info");
34         if (!info) {
35                 pr_err("Plugin %s does not contain plugin info\n", path);
36                 goto out;
37         }
38
39         if (!info->init) {
40                 pr_err("Plugin info structure has NULL .init callback\n");
41                 goto out;
42         }
43
44         ret = info->init();
45         pr_info("Loading plugin %s %s\n", path,
46                 ret ? "failed" : "succeeded");
47
48 out:
49         if (ret)
50                 dlclose(handle);
51
52         return ret;
53 }
54
55 /*
56  * Try to load a parser plugin with file name matching some of the
57  * following candidates (%s being the requested name):
58  *
59  * "%s_parser.so"
60  * "./%s_parser.so"
61  * "<exec_path>/%s_parser.so"
62  */
63 int load_parser_plugin(const char *name)
64 {
65         char str[256], parser[] = "_parser.so";
66         int ret;
67
68         strncpy(str, name, sizeof(str));
69         strncat(str, parser, sizeof(str) - 1);
70         ret = load_plugin(str);
71         if (!ret)
72                 return 0;
73
74         strncpy(str, "./", sizeof(str));
75         strncat(str, name, sizeof(str) - 1);
76         strncat(str, parser, sizeof(str) - 1);
77         ret = load_plugin(str);
78         if (!ret)
79                 return 0;
80
81         if (!exec_path)
82                 return 0;
83
84         strncpy(str, exec_path, sizeof(str));
85         strncat(str, "/", sizeof(str) - 1);
86         strncat(str, name, sizeof(str) - 1);
87         strncat(str, parser, sizeof(str) - 1);
88         return load_plugin(str);
89 }