plugin cleanup
This commit is contained in:
parent
4ff80b5e24
commit
207649b7b6
@ -2,9 +2,7 @@
|
|||||||
#define _PLUGINS_H_
|
#define _PLUGINS_H_
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include "logging.h"
|
|
||||||
|
|
||||||
struct sammler_plugin {
|
struct sammler_plugin {
|
||||||
struct list_head list;
|
struct list_head list;
|
||||||
@ -25,6 +23,8 @@ struct sammler_plugin {
|
|||||||
};
|
};
|
||||||
|
|
||||||
int plugin_init(void);
|
int plugin_init(void);
|
||||||
|
int plugin_close(void);
|
||||||
|
|
||||||
struct sammler_plugin * plugin_lookup(const char *name);
|
struct sammler_plugin * plugin_lookup(const char *name);
|
||||||
|
|
||||||
#endif /* _PLUGINS_H_ */
|
#endif /* _PLUGINS_H_ */
|
||||||
|
37
plugins.c
37
plugins.c
@ -34,22 +34,21 @@
|
|||||||
#define FLAGS_ACTIVE 0x01
|
#define FLAGS_ACTIVE 0x01
|
||||||
|
|
||||||
static LIST_HEAD(plugin_list);
|
static LIST_HEAD(plugin_list);
|
||||||
|
static struct event_timeout *probe_event;
|
||||||
|
|
||||||
static int plugin_init_cb(const char *filename, void *privdata)
|
static int plugin_init_cb(const char *filename, void *privdata)
|
||||||
{
|
{
|
||||||
static const char *plugin_dir;
|
const char *plugin_dir = (const char *)privdata;
|
||||||
if (plugin_dir == NULL)
|
|
||||||
plugin_dir = config_get_string("global", "plugin_dir", ".");
|
|
||||||
|
|
||||||
char *fullname = malloc(PATH_MAX);
|
char *fullname = malloc(PATH_MAX);
|
||||||
if (fullname == NULL) {
|
if (fullname == NULL) {
|
||||||
log_print(LOG_ERROR, "plugin_load: out of memory");
|
log_print(LOG_ERROR, "plugin_init_cb(): out of memory");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int len = snprintf(fullname, PATH_MAX, "%s/%s", plugin_dir, filename);
|
int len = snprintf(fullname, PATH_MAX, "%s/%s", plugin_dir, filename);
|
||||||
if (len < 0 || len >= PATH_MAX) {
|
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);
|
free(fullname);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -57,7 +56,7 @@ static int plugin_init_cb(const char *filename, void *privdata)
|
|||||||
dlerror();
|
dlerror();
|
||||||
void *dlhandle = dlopen(fullname, RTLD_NOW);
|
void *dlhandle = dlopen(fullname, RTLD_NOW);
|
||||||
if (dlhandle == NULL) {
|
if (dlhandle == NULL) {
|
||||||
log_print(LOG_ERROR, "plugin_load: dlopen: %s", dlerror());
|
log_print(LOG_ERROR, "plugin_init_cb(): dlopen: %s", dlerror());
|
||||||
free(fullname);
|
free(fullname);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -66,13 +65,13 @@ static int plugin_init_cb(const char *filename, void *privdata)
|
|||||||
|
|
||||||
struct sammler_plugin *plugin = dlsym(dlhandle, "plugin");
|
struct sammler_plugin *plugin = dlsym(dlhandle, "plugin");
|
||||||
if (plugin == NULL) {
|
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);
|
dlclose(dlhandle);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (plugin->init != NULL && (plugin->init() != 0)) {
|
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);
|
dlclose(dlhandle);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -98,7 +97,7 @@ static int plugins_probe(void *privdata)
|
|||||||
|
|
||||||
if (plugin->lastprobe + plugin->interval <= now) {
|
if (plugin->lastprobe + plugin->interval <= now) {
|
||||||
if (plugin->probe() != 0) {
|
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->flags &= ~FLAGS_ACTIVE;
|
||||||
}
|
}
|
||||||
plugin->lastprobe = now;
|
plugin->lastprobe = now;
|
||||||
@ -111,7 +110,8 @@ static int plugins_probe(void *privdata)
|
|||||||
|
|
||||||
int plugin_init(void)
|
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) {
|
if (cnt == 0) {
|
||||||
log_print(LOG_ERROR, "plugin_init(): no working plugins");
|
log_print(LOG_ERROR, "plugin_init(): no working plugins");
|
||||||
return -1;
|
return -1;
|
||||||
@ -120,11 +120,26 @@ int plugin_init(void)
|
|||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
tv.tv_sec = 1;
|
tv.tv_sec = 1;
|
||||||
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
||||||
event_add_timeout(&tv, plugins_probe, NULL);
|
probe_event = event_add_timeout(&tv, plugins_probe, NULL);
|
||||||
|
|
||||||
return 0;
|
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_lookup(const char *name)
|
||||||
{
|
{
|
||||||
struct sammler_plugin *plugin;
|
struct sammler_plugin *plugin;
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
#include "configfile.h"
|
#include "configfile.h"
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
#include "logging.h"
|
||||||
#include "plugins.h"
|
#include "plugins.h"
|
||||||
#include "probe.h"
|
#include "probe.h"
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
|
#include "logging.h"
|
||||||
#include "plugins.h"
|
#include "plugins.h"
|
||||||
#include "probe.h"
|
#include "probe.h"
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
|
#include "logging.h"
|
||||||
#include "plugins.h"
|
#include "plugins.h"
|
||||||
#include "probe.h"
|
#include "probe.h"
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "logging.h"
|
||||||
#include "plugins.h"
|
#include "plugins.h"
|
||||||
#include "probe.h"
|
#include "probe.h"
|
||||||
|
|
||||||
|
@ -19,10 +19,12 @@
|
|||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <mntent.h>
|
|
||||||
#include <sys/vfs.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <mntent.h>
|
||||||
|
#include <sys/vfs.h>
|
||||||
|
|
||||||
|
#include "logging.h"
|
||||||
#include "plugins.h"
|
#include "plugins.h"
|
||||||
#include "probe.h"
|
#include "probe.h"
|
||||||
|
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
#include "configfile.h"
|
#include "configfile.h"
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
#include "logging.h"
|
||||||
#include "plugins.h"
|
#include "plugins.h"
|
||||||
#include "probe.h"
|
#include "probe.h"
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
|
#include "logging.h"
|
||||||
#include "plugins.h"
|
#include "plugins.h"
|
||||||
#include "probe.h"
|
#include "probe.h"
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
|
#include "logging.h"
|
||||||
#include "plugins.h"
|
#include "plugins.h"
|
||||||
#include "probe.h"
|
#include "probe.h"
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
|
#include "logging.h"
|
||||||
#include "plugins.h"
|
#include "plugins.h"
|
||||||
#include "probe.h"
|
#include "probe.h"
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
|
#include "logging.h"
|
||||||
#include "plugins.h"
|
#include "plugins.h"
|
||||||
#include "probe.h"
|
#include "probe.h"
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
|
#include "logging.h"
|
||||||
#include "plugins.h"
|
#include "plugins.h"
|
||||||
#include "probe.h"
|
#include "probe.h"
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "logging.h"
|
||||||
#include "plugins.h"
|
#include "plugins.h"
|
||||||
#include "probe.h"
|
#include "probe.h"
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user