hlswmaster/src/plugin.c

157 lines
4.2 KiB
C
Raw Normal View History

2006-02-02 16:24:06 +01:00
/***************************************************************************
* Copyright (C) 03/2005 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 <stdlib.h>
#include <dlfcn.h>
#include <pthread.h>
#include "hlswmaster.h"
#include "plugin.h"
#include "netpkt.h"
#include "list.h"
/* liste der geladenen plugins */
LIST_HEAD(plugin_list);
/* sichert die plugin list UND die plugins ab */
static pthread_mutex_t plugin_lock = PTHREAD_MUTEX_INITIALIZER;
2006-02-02 16:25:55 +01:00
/**
* plugin_load()
* laedt ein plugin
*
* @param char *name
* @return false on error
2006-02-02 16:27:19 +01:00
*
2006-02-02 16:25:55 +01:00
* TODO: pfadname als parameter (config-file?)
* TODO: referenz speichern? wo? wie?
*/
2006-02-02 16:24:06 +01:00
int plugin_load(char *name) {
void *tmp;
if (!(tmp = dlopen(name, RTLD_NOW)))
log_print("unable to load plugin '%s'", name);
2006-02-02 16:25:55 +01:00
return 1;
2006-02-02 16:24:06 +01:00
}
2006-02-02 16:25:55 +01:00
/**
* plugin_unload()
* entfernt ein plugin
*
* @param char *name
* @return false on error
*
* TODO: implementieren? oder lieber nicht?
*/
2006-02-02 16:24:06 +01:00
int plugin_unload(char *name) {
2006-02-02 16:25:55 +01:00
return 1;
2006-02-02 16:24:06 +01:00
}
2006-02-02 16:25:55 +01:00
/**
* plugins_scan()
* ruft die scan() Funktionen der Plugins auf (wenn vorhanden)
*
* @return true
2006-02-02 16:27:19 +01:00
*/
2006-02-02 16:24:06 +01:00
int plugins_scan(void) {
struct hlswmaster_plugin *plugin;
pthread_mutex_lock(&plugin_lock);
list_for_each_entry(plugin, &plugin_list, list)
if (plugin->scan && !plugin->scan())
log_print("plugin %s: scan error", plugin->name);
pthread_mutex_unlock(&plugin_lock);
return 1;
}
2006-02-02 16:25:55 +01:00
/**
* plugins_parse()
* ruft die parse() Funktionen der Plugins auf (wenn vorhanden)
* bis ein Plugin das Paket annimmt
*
* @param struct net_pkt *pkt
* @return false wenn kein Plugin das Paket angenommen hat
2006-02-02 16:27:19 +01:00
*/
2006-02-02 16:24:06 +01:00
int plugins_parse(struct net_pkt *pkt) {
struct hlswmaster_plugin *plugin;
pthread_mutex_lock(&plugin_lock);
list_for_each_entry(plugin, &plugin_list, list) {
if (plugin->parse && plugin->parse(pkt)) {
pthread_mutex_unlock(&plugin_lock);
return 1;
}
}
pthread_mutex_unlock(&plugin_lock);
return 0;
}
2006-02-02 16:25:55 +01:00
/**
* plugins_gc()
* ruft die gc() Funktionen der Plugins auf (wenn vorhanden)
*
* @param unsigned long timeout
* @return true
2006-02-02 16:27:19 +01:00
*
* TODO: wird noch nicht benutzt
*/
2006-02-02 16:24:06 +01:00
int plugins_gc(unsigned long timeout) {
struct hlswmaster_plugin *plugin;
pthread_mutex_lock(&plugin_lock);
list_for_each_entry(plugin, &plugin_list, list)
if (plugin->gc)
plugin->gc(timeout);
pthread_mutex_unlock(&plugin_lock);
return 1;
}
2006-02-02 16:25:55 +01:00
/**
* register_plugin()
* wird von den Plugins beim laden (durch _init())aufgerufen
*
* @param struct hlswmaster_plugin *me
*/
2006-02-02 16:24:06 +01:00
void register_plugin(struct hlswmaster_plugin *me) {
if (!me->init || (me->init(NULL))) {
pthread_mutex_lock(&plugin_lock);
2006-02-02 16:27:19 +01:00
list_add_tail((struct list_head *)me, &plugin_list);
2006-02-02 16:24:06 +01:00
pthread_mutex_unlock(&plugin_lock);
}
}
2006-02-02 16:25:55 +01:00
/**
* register_plugin()
* wird von den Plugins beim entfernen (durch fini) aufgerufen
*
* @param struct hlswmaster_plugin *me
*/
2006-02-02 16:24:06 +01:00
void unregister_plugin(struct hlswmaster_plugin *me) {
pthread_mutex_lock(&plugin_lock);
list_del((struct list_head *)me);
pthread_mutex_unlock(&plugin_lock);
if (me->fini)
me->fini();
}