2017-02-12 14:35:05 -03:00
|
|
|
/** Loadable / plugin support.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-02-12 14:35:05 -03:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2017-03-03 20:21:33 -04:00
|
|
|
#include <dlfcn.h>
|
|
|
|
|
2017-02-12 14:35:05 -03:00
|
|
|
#include "plugin.h"
|
|
|
|
|
2017-03-03 20:21:33 -04:00
|
|
|
/** Global list of all known plugins */
|
2017-03-12 17:04:43 -03:00
|
|
|
struct list plugins = LIST_INIT();
|
2017-03-03 20:21:33 -04:00
|
|
|
|
2017-02-12 14:35:05 -03:00
|
|
|
int plugin_init(struct plugin *p, char *name, char *path)
|
|
|
|
{
|
|
|
|
p->name = strdup(name);
|
|
|
|
p->path = strdup(path);
|
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
p->state = STATE_INITIALIZED;
|
2017-03-03 20:21:33 -04:00
|
|
|
|
|
|
|
return 0;
|
2017-02-12 14:35:05 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int plugin_load(struct plugin *p)
|
|
|
|
{
|
|
|
|
p->handle = dlopen(p->path, RTLD_NOW);
|
|
|
|
if (!p->path)
|
|
|
|
return -1;
|
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
p->state = STATE_LOADED;
|
2017-02-12 14:35:05 -03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int plugin_unload(struct plugin *p)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-03-12 17:04:43 -03:00
|
|
|
assert(p->state == STATE_LOADED);
|
2017-02-12 14:35:05 -03:00
|
|
|
|
|
|
|
ret = dlclose(p->handle);
|
|
|
|
if (ret)
|
|
|
|
return -1;
|
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
p->state = STATE_UNLOADED;
|
2017-02-12 14:35:05 -03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int plugin_destroy(struct plugin *p)
|
|
|
|
{
|
2017-03-12 17:04:43 -03:00
|
|
|
assert(p->state != STATE_DESTROYED && p->state != STATE_LOADED);
|
2017-03-07 07:20:49 -04:00
|
|
|
|
|
|
|
if (p->path)
|
|
|
|
free(p->path);
|
2017-02-18 10:45:05 -05:00
|
|
|
|
|
|
|
return 0;
|
2017-02-12 14:35:05 -03:00
|
|
|
}
|
|
|
|
|
2017-02-18 10:51:43 -05:00
|
|
|
int plugin_parse(struct plugin *p, config_setting_t *cfg)
|
2017-02-12 14:35:05 -03:00
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
path = config_setting_get_string(cfg);
|
|
|
|
if (!path)
|
|
|
|
cerror(cfg, "Setting 'plugin' must be a string.");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:21:33 -04:00
|
|
|
struct plugin * plugin_lookup(enum plugin_type type, const char *name)
|
2017-02-12 14:35:05 -03:00
|
|
|
{
|
|
|
|
list_foreach(struct plugin *l, &plugins) {
|
|
|
|
if (l->type == type && strcmp(l->name, name) == 0)
|
2017-02-18 10:51:43 -05:00
|
|
|
return l;
|
2017-02-12 14:35:05 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-03-03 20:21:33 -04:00
|
|
|
|
|
|
|
void plugin_dump(enum plugin_type type)
|
|
|
|
{
|
2017-03-05 11:09:37 -04:00
|
|
|
list_foreach(struct plugin *p, &plugins) {
|
|
|
|
if (p->type == type)
|
|
|
|
printf(" - %-12s: %s\n", p->name, p->description);
|
|
|
|
}
|
2017-03-03 20:21:33 -04:00
|
|
|
}
|