sammler/plugins.c

149 lines
4.4 KiB
C
Raw Permalink Normal View History

2006-06-13 21:34:36 +02:00
/***************************************************************************
* Copyright (C) 06/2006 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
2007-03-31 22:31:07 +02:00
#include <limits.h>
2006-06-13 21:34:36 +02:00
2006-08-03 19:49:07 +02:00
#include "configfile.h"
2007-04-01 00:17:50 +02:00
#include "event.h"
2006-06-13 21:34:36 +02:00
#include "logging.h"
2006-08-03 19:49:07 +02:00
#include "network.h"
2007-04-01 14:30:05 +02:00
#include "plugins.h"
2007-04-01 00:17:50 +02:00
2007-04-01 15:23:09 +02:00
#define FLAGS_ACTIVE 0x01
2006-06-13 21:34:36 +02:00
2007-04-01 15:23:09 +02:00
static LIST_HEAD(plugin_list);
2007-04-01 15:45:31 +02:00
static struct event_timeout *probe_event;
2007-03-31 23:21:31 +02:00
2007-03-31 22:15:00 +02:00
static int plugin_init_cb(const char *filename, void *privdata)
2006-06-13 21:34:36 +02:00
{
2007-04-01 15:45:31 +02:00
const char *plugin_dir = (const char *)privdata;
2006-06-13 21:34:36 +02:00
2007-04-01 15:23:09 +02:00
char *fullname = malloc(PATH_MAX);
if (fullname == NULL) {
2007-04-01 15:45:31 +02:00
log_print(LOG_ERROR, "plugin_init_cb(): out of memory");
2006-10-07 20:37:30 +02:00
return -1;
2006-06-13 21:34:36 +02:00
}
2007-04-01 15:23:09 +02:00
int len = snprintf(fullname, PATH_MAX, "%s/%s", plugin_dir, filename);
2007-03-31 22:31:07 +02:00
if (len < 0 || len >= PATH_MAX) {
2007-04-01 15:45:31 +02:00
log_print(LOG_ERROR, "plugin_init_cb(): file name too long: %s/%s", plugin_dir, filename);
2007-04-01 15:23:09 +02:00
free(fullname);
2006-10-07 20:37:30 +02:00
return -1;
2006-06-13 21:34:36 +02:00
}
dlerror();
2007-04-01 15:23:09 +02:00
void *dlhandle = dlopen(fullname, RTLD_NOW);
if (dlhandle == NULL) {
2007-04-01 15:45:31 +02:00
log_print(LOG_ERROR, "plugin_init_cb(): dlopen: %s", dlerror());
2007-04-01 15:23:09 +02:00
free(fullname);
2006-10-07 20:37:30 +02:00
return -1;
2006-06-13 21:34:36 +02:00
}
2007-04-01 15:23:09 +02:00
free(fullname);
2006-10-07 20:37:30 +02:00
2007-04-01 15:23:09 +02:00
struct sammler_plugin *plugin = dlsym(dlhandle, "plugin");
2006-06-13 21:34:36 +02:00
if (plugin == NULL) {
2007-04-01 15:45:31 +02:00
log_print(LOG_ERROR, "plugin_init_cb(): failed to load '%s'", filename);
2007-04-01 15:23:09 +02:00
dlclose(dlhandle);
2006-10-07 20:37:30 +02:00
return -1;
2006-06-13 21:34:36 +02:00
}
2006-10-07 20:37:30 +02:00
if (plugin->init != NULL && (plugin->init() != 0)) {
2007-04-01 15:45:31 +02:00
log_print(LOG_ERROR, "plugin_init_cb(): Plugin '%s': init() failed", plugin->name);
2007-04-01 15:23:09 +02:00
dlclose(dlhandle);
2006-10-07 20:37:30 +02:00
return -1;
}
2006-06-13 21:34:36 +02:00
2007-04-01 15:23:09 +02:00
log_print(LOG_INFO, "Plugin '%s' loaded", plugin->name);
plugin->lastprobe = 0;
plugin->flags = FLAGS_ACTIVE;
plugin->dlhandle = dlhandle;
2007-03-31 23:21:31 +02:00
2006-10-07 20:37:30 +02:00
list_add_tail(&plugin->list, &plugin_list);
return 0;
2006-06-13 21:34:36 +02:00
}
2012-12-09 12:26:51 +01:00
static int plugins_probe(int timerid, void *privdata)
2006-06-13 21:34:36 +02:00
{
2007-04-01 00:17:50 +02:00
time_t now;
time(&now);
2007-03-31 23:21:31 +02:00
struct sammler_plugin *plugin;
2007-04-01 00:17:50 +02:00
list_for_each_entry(plugin, &plugin_list, list) {
2007-04-01 15:23:09 +02:00
if (!(plugin->flags & FLAGS_ACTIVE))
continue;
2007-04-01 00:17:50 +02:00
if (plugin->lastprobe + plugin->interval <= now) {
2007-04-01 15:23:09 +02:00
if (plugin->probe() != 0) {
2007-04-01 15:45:31 +02:00
log_print(LOG_ERROR, "plugin_probe(): plugin '%s' disabled", plugin->name);
2007-04-01 15:23:09 +02:00
plugin->flags &= ~FLAGS_ACTIVE;
}
2007-04-01 00:17:50 +02:00
plugin->lastprobe = now;
}
}
net_submit_flush();
return 0;
}
2007-03-31 23:21:31 +02:00
2007-04-01 00:17:50 +02:00
int plugin_init(void)
{
2007-04-01 15:45:31 +02:00
const char *plugin_dir = config_get_string("global", "plugin_dir", "plugins");
int cnt = config_get_strings("global", "plugin", plugin_init_cb, (void *)plugin_dir);
2007-04-01 14:30:05 +02:00
if (cnt == 0) {
log_print(LOG_ERROR, "plugin_init(): no working plugins");
return -1;
}
2007-03-31 23:21:31 +02:00
2012-12-09 12:26:51 +01:00
probe_event = event_add_timeout_ms(1000, plugins_probe, 0, NULL);
2006-08-03 19:49:07 +02:00
2007-04-01 00:17:50 +02:00
return 0;
2006-09-30 20:27:56 +02:00
}
2007-04-01 15:45:31 +02:00
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;
}
2007-03-31 22:31:07 +02:00
struct sammler_plugin * plugin_lookup(const char *name)
2006-09-30 20:27:56 +02:00
{
struct sammler_plugin *plugin;
list_for_each_entry(plugin, &plugin_list, list) {
if (!strcmp(plugin->name, name))
return plugin;
}
return NULL;
2006-06-13 21:34:36 +02:00
}