2014-07-14 11:49:44 +00:00
|
|
|
/** Configuration parser.
|
2014-06-05 09:34:32 +00:00
|
|
|
*
|
2014-06-05 09:35:41 +00:00
|
|
|
* @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
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2014-06-05 09:34:32 +00:00
|
|
|
|
2014-06-10 19:44:21 +00:00
|
|
|
#include <stdlib.h>
|
2014-12-05 12:34:45 +01:00
|
|
|
#include <string.h>
|
2016-02-04 17:11:36 +01:00
|
|
|
#include <libgen.h>
|
2016-07-11 16:02:55 +02:00
|
|
|
#include <unistd.h>
|
2014-06-05 09:34:32 +00:00
|
|
|
|
2015-03-18 15:50:02 +01:00
|
|
|
#include "utils.h"
|
|
|
|
#include "list.h"
|
2014-06-05 09:34:50 +00:00
|
|
|
#include "cfg.h"
|
2014-06-05 09:35:23 +00:00
|
|
|
#include "node.h"
|
|
|
|
#include "path.h"
|
2016-11-20 13:11:37 -05:00
|
|
|
#include "hook.h"
|
2017-02-18 10:31:42 -05:00
|
|
|
#include "advio.h"
|
|
|
|
#include "web.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "api.h"
|
|
|
|
#include "plugin.h"
|
2017-03-05 10:06:32 -04:00
|
|
|
#include "node.h"
|
2014-06-05 09:34:32 +00:00
|
|
|
|
2017-02-18 10:31:42 -05:00
|
|
|
#include "kernel/rt.h"
|
|
|
|
|
|
|
|
int cfg_init_pre(struct cfg *cfg)
|
2016-07-14 09:47:00 +02:00
|
|
|
{
|
2017-03-05 10:06:32 -04:00
|
|
|
config_init(&cfg->cfg);
|
2017-02-18 10:31:42 -05:00
|
|
|
|
|
|
|
info("Inititliaze logging sub-system");
|
2017-03-05 10:06:32 -04:00
|
|
|
log_init(&cfg->log, V, LOG_ALL);
|
2017-02-18 10:31:42 -05:00
|
|
|
|
|
|
|
list_init(&cfg->nodes);
|
|
|
|
list_init(&cfg->paths);
|
|
|
|
list_init(&cfg->plugins);
|
|
|
|
|
|
|
|
return 0;
|
2016-07-14 09:47:00 +02:00
|
|
|
}
|
|
|
|
|
2017-02-18 10:31:42 -05:00
|
|
|
int cfg_init_post(struct cfg *cfg)
|
2016-07-14 09:47:00 +02:00
|
|
|
{
|
2017-03-05 10:06:32 -04:00
|
|
|
memory_init();
|
|
|
|
rt_init(cfg->priority, cfg->affinity);
|
2017-02-18 10:31:42 -05:00
|
|
|
api_init(&cfg->api, cfg);
|
|
|
|
web_init(&cfg->web, &cfg->api);
|
|
|
|
|
2017-03-06 08:57:43 -04:00
|
|
|
info("Initialize node types");
|
|
|
|
list_foreach(struct node *n, &cfg->nodes) { INDENT
|
|
|
|
config_setting_t *cfg_root = config_root_setting(&cfg->cfg);
|
|
|
|
|
|
|
|
node_type_init(n->_vt, cfg->cli.argc, cfg->cli.argv, cfg_root);
|
|
|
|
}
|
|
|
|
|
2017-02-18 10:31:42 -05:00
|
|
|
return 0;
|
2016-07-14 09:47:00 +02:00
|
|
|
}
|
|
|
|
|
2017-02-18 10:31:42 -05:00
|
|
|
int cfg_deinit(struct cfg *cfg)
|
2014-06-05 09:34:32 +00:00
|
|
|
{
|
2017-02-18 10:31:42 -05:00
|
|
|
info("De-initializing node types");
|
2017-03-06 08:57:43 -04:00
|
|
|
list_foreach(struct plugin *p, &plugins) { INDENT
|
|
|
|
if (p->type == PLUGIN_TYPE_NODE)
|
|
|
|
node_type_deinit(&p->node);
|
2017-02-18 10:31:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
info("De-initializing web interface");
|
|
|
|
web_deinit(&cfg->web);
|
|
|
|
|
|
|
|
info("De-initialize API");
|
|
|
|
api_deinit(&cfg->api);
|
|
|
|
|
2017-03-05 10:06:32 -04:00
|
|
|
info("De-initialize log sub-system");
|
|
|
|
log_deinit(&cfg->log);
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2017-02-18 10:31:42 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cfg_destroy(struct cfg *cfg)
|
|
|
|
{
|
2017-03-05 10:06:32 -04:00
|
|
|
config_destroy(&cfg->cfg);
|
2016-06-15 20:05:09 +02:00
|
|
|
|
2017-02-18 10:31:42 -05:00
|
|
|
web_destroy(&cfg->web);
|
|
|
|
log_destroy(&cfg->log);
|
|
|
|
api_destroy(&cfg->api);
|
2016-07-14 09:47:00 +02:00
|
|
|
|
2017-02-18 10:31:42 -05:00
|
|
|
list_destroy(&cfg->plugins, (dtor_cb_t) plugin_destroy, false);
|
|
|
|
list_destroy(&cfg->paths, (dtor_cb_t) path_destroy, true);
|
|
|
|
list_destroy(&cfg->nodes, (dtor_cb_t) node_destroy, true);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-04 17:11:36 +01:00
|
|
|
|
2017-03-06 08:59:28 -04:00
|
|
|
int cfg_parse_cli(struct cfg *cfg, int argc, char *argv[])
|
|
|
|
{
|
|
|
|
cfg->cli.argc = argc;
|
|
|
|
cfg->cli.argv = argv;
|
|
|
|
|
|
|
|
char *uri = (argc == 2) ? argv[1] : NULL;
|
|
|
|
|
|
|
|
return cfg_parse(cfg, uri);
|
|
|
|
}
|
|
|
|
|
2017-02-18 10:31:42 -05:00
|
|
|
int cfg_parse(struct cfg *cfg, const char *uri)
|
|
|
|
{
|
2017-03-05 10:06:32 -04:00
|
|
|
config_setting_t *cfg_root, *cfg_nodes, *cfg_paths, *cfg_plugins, *cfg_logging, *cfg_web;
|
2014-07-04 09:44:09 +00:00
|
|
|
|
2017-03-06 19:08:45 -04:00
|
|
|
info("Parsing configuration: uri=%s", uri);
|
2017-03-07 08:03:14 -04:00
|
|
|
|
|
|
|
{ INDENT
|
2017-03-06 19:08:45 -04:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
int ret = CONFIG_FALSE;
|
2017-02-18 10:31:42 -05:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
if (uri) {
|
|
|
|
/* Setup libconfig */
|
|
|
|
config_set_auto_convert(&cfg->cfg, 1);
|
2017-02-18 10:31:42 -05:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
FILE *f;
|
|
|
|
AFILE *af;
|
2016-07-11 16:02:55 +02:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
/* Via stdin */
|
|
|
|
if (strcmp("-", uri) == 0) {
|
|
|
|
af = NULL;
|
|
|
|
f = stdin;
|
|
|
|
}
|
|
|
|
/* Local file? */
|
|
|
|
else if (access(uri, F_OK) != -1) {
|
|
|
|
/* Setup libconfig include path.
|
|
|
|
* This is only supported for local files */
|
|
|
|
char *uri_cpy = strdup(uri);
|
|
|
|
char *include_dir = dirname(uri_cpy);
|
|
|
|
|
|
|
|
config_set_include_dir(&cfg->cfg, include_dir);
|
2017-02-18 10:31:42 -05:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
free(uri_cpy);
|
2017-02-18 10:31:42 -05:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
af = NULL;
|
|
|
|
f = fopen(uri, "r");
|
|
|
|
}
|
|
|
|
/* Use advio (libcurl) to fetch the config from a remote */
|
|
|
|
else {
|
|
|
|
af = afopen(uri, "r", ADVIO_MEM);
|
|
|
|
f = af ? af->file : NULL;
|
|
|
|
}
|
2017-02-18 10:31:42 -05:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
/* Check if file could be loaded / opened */
|
|
|
|
if (!f)
|
|
|
|
error("Failed to open configuration from: %s", uri);
|
2017-02-18 10:31:42 -05:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
/* Parse config */
|
|
|
|
ret = config_read(&cfg->cfg, f);
|
|
|
|
if (ret != CONFIG_TRUE)
|
|
|
|
error("Failed to parse configuration: %s in %s:%d", config_error_text(&cfg->cfg), uri, config_error_line(&cfg->cfg));
|
|
|
|
|
|
|
|
/* Close configuration file */
|
|
|
|
if (af)
|
|
|
|
afclose(af);
|
|
|
|
else
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
warn("No configuration file specified. Starting unconfigured. Use the API to configure this instance.");
|
2017-02-18 10:31:42 -05:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
cfg->web.port = 80;
|
|
|
|
cfg->web.htdocs = "/villas/web/socket/";
|
|
|
|
}
|
2014-06-05 09:34:50 +00:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
/* Parse global settings */
|
|
|
|
cfg_root = config_root_setting(&cfg->cfg);
|
|
|
|
if (cfg_root) {
|
|
|
|
if (!config_setting_is_group(cfg_root))
|
|
|
|
warn("Missing global section in config file.");
|
2017-02-18 10:31:42 -05:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
if (!config_setting_lookup_int(cfg_root, "affinity", &cfg->affinity))
|
|
|
|
cfg->affinity = 0;
|
2017-03-05 10:06:32 -04:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
if (!config_setting_lookup_int(cfg_root, "priority", &cfg->priority))
|
|
|
|
cfg->priority = 0;
|
2014-06-10 16:57:40 +00:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
if (!config_setting_lookup_float(cfg_root, "stats", &cfg->stats))
|
|
|
|
cfg->stats = 0;
|
|
|
|
}
|
2017-03-05 10:06:32 -04:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
cfg_web = config_setting_get_member(cfg_root, "http");
|
|
|
|
if (cfg_web)
|
|
|
|
web_parse(&cfg->web, cfg_web);
|
|
|
|
|
|
|
|
/* Parse logging settings */
|
|
|
|
cfg_logging = config_setting_get_member(cfg_root, "logging");
|
|
|
|
if (cfg_logging)
|
|
|
|
log_parse(&cfg->log, cfg_logging);
|
|
|
|
|
|
|
|
/* Parse plugins */
|
|
|
|
cfg_plugins = config_setting_get_member(cfg_root, "plugins");
|
|
|
|
if (cfg_plugins) {
|
|
|
|
if (!config_setting_is_array(cfg_plugins))
|
|
|
|
cerror(cfg_plugins, "Setting 'plugins' must be a list of strings");
|
|
|
|
|
|
|
|
for (int i = 0; i < config_setting_length(cfg_plugins); i++) {
|
|
|
|
struct config_setting_t *cfg_plugin = config_setting_get_elem(cfg_plugins, i);
|
2017-02-18 10:31:42 -05:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
struct plugin plugin;
|
2017-02-18 10:31:42 -05:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
ret = plugin_parse(&plugin, cfg_plugin);
|
|
|
|
if (ret)
|
|
|
|
cerror(cfg_plugin, "Failed to parse plugin");
|
2017-03-03 20:21:33 -04:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
list_push(&cfg->plugins, memdup(&plugin, sizeof(plugin)));
|
|
|
|
}
|
2017-02-18 10:31:42 -05:00
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
/* Parse nodes */
|
|
|
|
cfg_nodes = config_setting_get_member(cfg_root, "nodes");
|
|
|
|
if (cfg_nodes) {
|
|
|
|
if (!config_setting_is_group(cfg_nodes))
|
|
|
|
warn("Setting 'nodes' must be a group with node name => group mappings.");
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
for (int i = 0; i < config_setting_length(cfg_nodes); i++) {
|
|
|
|
config_setting_t *cfg_node = config_setting_get_elem(cfg_nodes, i);
|
2017-03-06 08:57:43 -04:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
struct plugin *p;
|
|
|
|
const char *type;
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
/* Required settings */
|
|
|
|
if (!config_setting_lookup_string(cfg_node, "type", &type))
|
|
|
|
cerror(cfg_node, "Missing node type");
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2017-03-06 08:57:43 -04:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
p = plugin_lookup(PLUGIN_TYPE_NODE, type);
|
|
|
|
if (!p)
|
|
|
|
cerror(cfg_node, "Invalid node type: %s", type);
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2017-03-11 23:33:41 -03:00
|
|
|
struct node n;
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2017-03-11 23:33:41 -03:00
|
|
|
ret = node_parse(&n, cfg_node);
|
2017-03-07 08:03:14 -04:00
|
|
|
if (ret)
|
|
|
|
cerror(cfg_node, "Failed to parse node");
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2017-03-11 23:33:41 -03:00
|
|
|
list_push(&cfg->nodes, memdup(&n, sizeof(n)));
|
2017-03-07 08:03:14 -04:00
|
|
|
}
|
2014-12-05 12:39:52 +01:00
|
|
|
}
|
2014-06-05 09:34:32 +00:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
/* Parse paths */
|
|
|
|
cfg_paths = config_setting_get_member(cfg_root, "paths");
|
|
|
|
if (cfg_paths) {
|
|
|
|
if (!config_setting_is_list(cfg_paths))
|
|
|
|
warn("Setting 'paths' must be a list.");
|
2014-12-05 12:39:52 +01:00
|
|
|
|
2017-03-07 08:03:14 -04:00
|
|
|
for (int i = 0; i < config_setting_length(cfg_paths); i++) {
|
|
|
|
config_setting_t *cfg_path = config_setting_get_elem(cfg_paths, i);
|
2014-06-05 09:34:50 +00:00
|
|
|
|
2017-03-11 23:33:41 -03:00
|
|
|
struct path p;
|
2015-10-09 17:21:20 +02:00
|
|
|
|
2017-03-11 23:33:41 -03:00
|
|
|
ret = path_parse(&p, cfg_path, &cfg->nodes);
|
2017-03-07 08:03:14 -04:00
|
|
|
if (ret)
|
|
|
|
cerror(cfg_path, "Failed to parse path");
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2017-03-11 23:33:41 -03:00
|
|
|
list_push(&cfg->paths, memdup(&p, sizeof(p)));
|
2015-10-12 16:17:51 +02:00
|
|
|
|
2017-03-11 23:33:41 -03:00
|
|
|
if (p.reverse) {
|
|
|
|
struct path r;
|
2016-01-14 22:54:08 +01:00
|
|
|
|
2017-03-11 23:33:41 -03:00
|
|
|
ret = path_reverse(&p, &r);
|
2017-03-07 08:03:14 -04:00
|
|
|
if (ret)
|
2017-03-11 23:33:41 -03:00
|
|
|
cerror(cfg_path, "Failed to reverse path %s", path_name(&p));
|
2015-10-12 16:17:51 +02:00
|
|
|
|
2017-03-11 23:33:41 -03:00
|
|
|
list_push(&cfg->paths, memdup(&r, sizeof(p)));
|
2017-03-07 08:03:14 -04:00
|
|
|
}
|
2015-03-18 15:50:02 +01:00
|
|
|
}
|
2016-11-20 12:59:37 -05:00
|
|
|
}
|
2015-03-18 16:13:18 +01:00
|
|
|
}
|
2017-03-11 23:33:41 -03:00
|
|
|
|
|
|
|
cfg->state = STATE_PARSED;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cfg_start(struct cfg *cfg)
|
|
|
|
{
|
|
|
|
memory_init(cfg->hugepages);
|
|
|
|
rt_init(cfg->priority, cfg->affinity);
|
|
|
|
|
|
|
|
api_start(&cfg->api);
|
|
|
|
web_start(&cfg->web);
|
|
|
|
|
|
|
|
info("Start node types");
|
|
|
|
list_foreach(struct node *n, &cfg->nodes) { INDENT
|
|
|
|
config_setting_t *cfg_root = config_root_setting(&cfg->cfg);
|
|
|
|
|
|
|
|
node_type_start(n->_vt, cfg->cli.argc, cfg->cli.argv, cfg_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
info("Starting nodes");
|
|
|
|
list_foreach(struct node *n, &cfg->nodes) { INDENT
|
|
|
|
int refs = list_count(&cfg->paths, (cmp_cb_t) path_uses_node, n);
|
|
|
|
if (refs > 0)
|
|
|
|
node_start(n);
|
|
|
|
else
|
|
|
|
warn("No path is using the node %s. Skipping...", node_name(n));
|
|
|
|
}
|
|
|
|
|
|
|
|
info("Starting paths");
|
|
|
|
list_foreach(struct path *p, &cfg->paths) { INDENT
|
|
|
|
if (p->enabled) {
|
|
|
|
path_init(p, cfg);
|
|
|
|
path_start(p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
warn("Path %s is disabled. Skipping...", path_name(p));
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg->state = STATE_STARTED;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cfg_stop(struct cfg *cfg)
|
|
|
|
{
|
|
|
|
info("Stopping paths");
|
|
|
|
list_foreach(struct path *p, &cfg->paths) { INDENT
|
|
|
|
path_stop(p);
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2017-03-11 23:33:41 -03:00
|
|
|
info("Stopping nodes");
|
|
|
|
list_foreach(struct node *n, &cfg->nodes) { INDENT
|
|
|
|
node_stop(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
info("De-initializing node types");
|
|
|
|
list_foreach(struct plugin *p, &plugins) { INDENT
|
|
|
|
if (p->type == PLUGIN_TYPE_NODE)
|
|
|
|
node_type_stop(&p->node);
|
|
|
|
}
|
|
|
|
|
|
|
|
web_stop(&cfg->web);
|
|
|
|
api_stop(&cfg->api);
|
|
|
|
log_stop(&cfg->log);
|
|
|
|
|
|
|
|
cfg->state = STATE_STOPPED;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cfg_destroy(struct cfg *cfg)
|
|
|
|
{
|
|
|
|
config_destroy(&cfg->cfg);
|
|
|
|
|
|
|
|
web_destroy(&cfg->web);
|
|
|
|
log_destroy(&cfg->log);
|
|
|
|
api_destroy(&cfg->api);
|
|
|
|
|
|
|
|
list_destroy(&cfg->plugins, (dtor_cb_t) plugin_destroy, false);
|
|
|
|
list_destroy(&cfg->paths, (dtor_cb_t) path_destroy, true);
|
|
|
|
list_destroy(&cfg->nodes, (dtor_cb_t) node_destroy, true);
|
|
|
|
|
|
|
|
cfg->state = STATE_DESTROYED;
|
|
|
|
|
2015-10-09 13:30:41 +02:00
|
|
|
return 0;
|
2016-11-20 12:59:37 -05:00
|
|
|
}
|