-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplugin.c
151 lines (124 loc) · 3.35 KB
/
plugin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (C) 2016 prpl Foundation
* Written by Felix Fietkau <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <libubox/avl-cmp.h>
#include <libubox/kvlist.h>
#include <dlfcn.h>
#include <stdarg.h>
#include "scapi.h"
#include "scald.h"
static KVLIST(options, kvlist_strlen);
static AVL_TREE(models, avl_strcmp, false, NULL);
static struct scald_model *
scald_model_get(const char *name)
{
struct scald_model *m;
m = avl_find_element(&models, name, m, avl);
if (m)
return m;
m = scald_model_new(name);
if (!m)
return NULL;
avl_insert(&models, &m->avl);
return m;
}
static struct scapi_model *
scald_model_add(struct scapi_plugin *p, const char *name, void *priv)
{
struct scald_model *m;
void *new_plugins, *new_priv;
m = scald_model_get(name);
if (!m)
return NULL;
new_plugins = realloc(m->plugins, (m->n_plugins + 1) * sizeof(*m->plugins));
new_priv = realloc(m->plugin_priv, (m->n_plugins + 1) * sizeof(*m->plugin_priv));
if (!new_plugins || !new_priv)
return NULL;
m->plugins = new_plugins;
m->plugin_priv = new_priv;
m->plugins[m->n_plugins] = p;
m->plugin_priv[m->n_plugins] = priv;
m->n_plugins++;
return &m->scapi;
}
static void
scald_status_model(struct blob_buf *buf, struct scald_model *m)
{
void *c;
int i;
c = blobmsg_open_array(buf, "plugins");
for (i = 0; i < m->n_plugins; i++)
blobmsg_add_string(buf, NULL, m->plugins[i]->name);
blobmsg_close_array(buf, c);
}
void
scald_status_models_dump(struct blob_buf *buf)
{
struct scald_model *m;
avl_for_each_element(&models, m, avl) {
void *c2 = blobmsg_open_table(buf, m->scapi.name);
scald_status_model(buf, m);
blobmsg_close_table(buf, c2);
}
}
static const char *
scald_plugin_option_get(const char *name)
{
return kvlist_get(&options, name);
}
static void
scald_plugin_add(struct scapi_plugin *p)
{
}
static void
scald_plugin_log_msg(struct scapi_plugin *p, enum scald_log_level level, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "[%s] ", p->name);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
void
scald_plugin_load(const char *path)
{
static const struct scapi_cb cb = {
.plugin_add = scald_plugin_add,
.model_add = scald_model_add,
.option_get = scald_plugin_option_get,
.log_msg = scald_plugin_log_msg,
};
typeof(scapi_module_init) *init;
void *dlh;
dlh = dlopen(path, RTLD_NOW | RTLD_LOCAL);
if (!dlh) {
fprintf(stderr, "Failed to load plugin %s: %s\n", path, dlerror());
return;
}
init = dlsym(dlh, "scapi_module_init");
if (!init)
return;
init(&cb);
}
void scald_plugin_add_option(char *arg)
{
char *val = strchr(arg, '=');
if (!val)
val = arg + strlen(arg);
else
*(val++) = 0;
kvlist_set(&options, arg, val);
}