|
|
|
@ -34,22 +34,21 @@
|
|
|
|
|
#define FLAGS_ACTIVE 0x01
|
|
|
|
|
|
|
|
|
|
static LIST_HEAD(plugin_list);
|
|
|
|
|
static struct event_timeout *probe_event;
|
|
|
|
|
|
|
|
|
|
static int plugin_init_cb(const char *filename, void *privdata)
|
|
|
|
|
{
|
|
|
|
|
static const char *plugin_dir;
|
|
|
|
|
if (plugin_dir == NULL)
|
|
|
|
|
plugin_dir = config_get_string("global", "plugin_dir", ".");
|
|
|
|
|
const char *plugin_dir = (const char *)privdata;
|
|
|
|
|
|
|
|
|
|
char *fullname = malloc(PATH_MAX);
|
|
|
|
|
if (fullname == NULL) {
|
|
|
|
|
log_print(LOG_ERROR, "plugin_load: out of memory");
|
|
|
|
|
log_print(LOG_ERROR, "plugin_init_cb(): out of memory");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int len = snprintf(fullname, PATH_MAX, "%s/%s", plugin_dir, filename);
|
|
|
|
|
if (len < 0 || len >= PATH_MAX) {
|
|
|
|
|
log_print(LOG_ERROR, "plugin_load: file name too long: %s/%s", plugin_dir, filename);
|
|
|
|
|
log_print(LOG_ERROR, "plugin_init_cb(): file name too long: %s/%s", plugin_dir, filename);
|
|
|
|
|
free(fullname);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
@ -57,7 +56,7 @@ static int plugin_init_cb(const char *filename, void *privdata)
|
|
|
|
|
dlerror();
|
|
|
|
|
void *dlhandle = dlopen(fullname, RTLD_NOW);
|
|
|
|
|
if (dlhandle == NULL) {
|
|
|
|
|
log_print(LOG_ERROR, "plugin_load: dlopen: %s", dlerror());
|
|
|
|
|
log_print(LOG_ERROR, "plugin_init_cb(): dlopen: %s", dlerror());
|
|
|
|
|
free(fullname);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
@ -66,13 +65,13 @@ static int plugin_init_cb(const char *filename, void *privdata)
|
|
|
|
|
|
|
|
|
|
struct sammler_plugin *plugin = dlsym(dlhandle, "plugin");
|
|
|
|
|
if (plugin == NULL) {
|
|
|
|
|
log_print(LOG_ERROR, "plugin_load: failed to load '%s'", filename);
|
|
|
|
|
log_print(LOG_ERROR, "plugin_init_cb(): failed to load '%s'", filename);
|
|
|
|
|
dlclose(dlhandle);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (plugin->init != NULL && (plugin->init() != 0)) {
|
|
|
|
|
log_print(LOG_ERROR, "Plugin '%s': init failed", plugin->name);
|
|
|
|
|
log_print(LOG_ERROR, "plugin_init_cb(): Plugin '%s': init() failed", plugin->name);
|
|
|
|
|
dlclose(dlhandle);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
@ -98,7 +97,7 @@ static int plugins_probe(void *privdata)
|
|
|
|
|
|
|
|
|
|
if (plugin->lastprobe + plugin->interval <= now) {
|
|
|
|
|
if (plugin->probe() != 0) {
|
|
|
|
|
log_print(LOG_ERROR, "plugin_probe(): plugin %s disabled", plugin->name);
|
|
|
|
|
log_print(LOG_ERROR, "plugin_probe(): plugin '%s' disabled", plugin->name);
|
|
|
|
|
plugin->flags &= ~FLAGS_ACTIVE;
|
|
|
|
|
}
|
|
|
|
|
plugin->lastprobe = now;
|
|
|
|
@ -111,7 +110,8 @@ static int plugins_probe(void *privdata)
|
|
|
|
|
|
|
|
|
|
int plugin_init(void)
|
|
|
|
|
{
|
|
|
|
|
int cnt = config_get_strings("global", "plugin", plugin_init_cb, NULL);
|
|
|
|
|
const char *plugin_dir = config_get_string("global", "plugin_dir", "plugins");
|
|
|
|
|
int cnt = config_get_strings("global", "plugin", plugin_init_cb, (void *)plugin_dir);
|
|
|
|
|
if (cnt == 0) {
|
|
|
|
|
log_print(LOG_ERROR, "plugin_init(): no working plugins");
|
|
|
|
|
return -1;
|
|
|
|
@ -120,11 +120,26 @@ int plugin_init(void)
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
event_add_timeout(&tv, plugins_probe, NULL);
|
|
|
|
|
probe_event = event_add_timeout(&tv, plugins_probe, NULL);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int plugin_close(void)
|
|
|
|
|
{
|
|
|
|
|
struct sammler_plugin *plugin, *tmp;
|
|
|
|
|
list_for_each_entry_safe(plugin, tmp, &plugin_list, list) {
|
|
|
|
|
list_del(&plugin->list);
|
|
|
|
|
if (plugin->fini != NULL && plugin->fini() != 0)
|
|
|
|
|
log_print(LOG_ERROR, "plugin_close(): Plugin '%s': fini() failed", plugin->name);
|
|
|
|
|
|
|
|
|
|
dlclose(plugin->dlhandle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event_remove_timeout(probe_event);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct sammler_plugin * plugin_lookup(const char *name)
|
|
|
|
|
{
|
|
|
|
|
struct sammler_plugin *plugin;
|
|
|
|
|