2025-01-14 14:42:39 +00:00
|
|
|
/* Configuration file parsing.
|
2019-03-26 07:03:57 +01:00
|
|
|
*
|
2025-01-14 14:42:39 +00:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <glob.h>
|
|
|
|
#include <libgen.h>
|
2021-09-13 19:14:22 +02:00
|
|
|
#include <linux/limits.h>
|
2019-03-26 07:03:57 +01:00
|
|
|
#include <unistd.h>
|
2021-08-31 13:57:37 +02:00
|
|
|
|
2019-03-26 07:03:57 +01:00
|
|
|
#include <string>
|
2021-08-31 13:57:37 +02:00
|
|
|
|
2019-04-07 15:46:14 +02:00
|
|
|
#include <villas/boxes.hpp>
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <villas/config_class.hpp>
|
2019-03-26 07:03:57 +01:00
|
|
|
#include <villas/config_helper.hpp>
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <villas/log.hpp>
|
|
|
|
#include <villas/node/config.hpp>
|
|
|
|
#include <villas/node/exceptions.hpp>
|
|
|
|
#include <villas/utils.hpp>
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2021-09-14 22:51:39 +02:00
|
|
|
#ifdef WITH_CONFIG
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <libconfig.h>
|
2021-09-14 22:51:39 +02:00
|
|
|
#endif
|
|
|
|
|
2019-03-26 07:03:57 +01:00
|
|
|
using namespace villas;
|
|
|
|
using namespace villas::node;
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
Config::Config() : logger(Log::get("config")), root(nullptr) {}
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
Config::Config(const std::string &u) : Config() { root = load(u); }
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
Config::~Config() { json_decref(root); }
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_t *Config::load(std::FILE *f, bool resolveInc, bool resolveEnvVars) {
|
|
|
|
json_t *root = decode(f);
|
2020-07-06 13:24:12 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (resolveInc) {
|
|
|
|
json_t *root_old = root;
|
|
|
|
root = expandIncludes(root);
|
|
|
|
json_decref(root_old);
|
|
|
|
}
|
2020-07-06 13:24:12 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (resolveEnvVars) {
|
|
|
|
json_t *root_old = root;
|
|
|
|
root = expandEnvVars(root);
|
|
|
|
json_decref(root_old);
|
|
|
|
}
|
2020-07-06 13:24:12 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return root;
|
2020-07-06 13:24:12 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_t *Config::load(const std::string &u, bool resolveInc,
|
|
|
|
bool resolveEnvVars) {
|
|
|
|
FILE *f;
|
2020-06-15 22:22:20 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (u == "-")
|
|
|
|
f = loadFromStdio();
|
|
|
|
else
|
|
|
|
f = loadFromLocalFile(u);
|
2020-06-15 22:22:20 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_t *root = load(f, resolveInc, resolveEnvVars);
|
2020-06-15 22:22:20 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
fclose(f);
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return root;
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
FILE *Config::loadFromStdio() {
|
|
|
|
logger->info("Reading configuration from standard input");
|
|
|
|
|
|
|
|
auto *cwd = new char[PATH_MAX];
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
configPath = getcwd(cwd, PATH_MAX);
|
|
|
|
|
|
|
|
delete[] cwd;
|
|
|
|
|
|
|
|
return stdin;
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
FILE *Config::loadFromLocalFile(const std::string &u) {
|
|
|
|
logger->info("Reading configuration from local file: {}", u);
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
configPath = u;
|
|
|
|
FILE *f = fopen(u.c_str(), "r");
|
|
|
|
if (!f)
|
|
|
|
throw RuntimeError("Failed to open configuration from: {}", u);
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return f;
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_t *Config::decode(FILE *f) {
|
|
|
|
json_error_t err;
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Update list of include directories
|
|
|
|
auto incDirs = getIncludeDirectories(f);
|
|
|
|
includeDirectories.insert(includeDirectories.end(), incDirs.begin(),
|
|
|
|
incDirs.end());
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_t *root = json_loadf(f, 0, &err);
|
|
|
|
if (root == nullptr) {
|
2019-04-05 11:01:49 +02:00
|
|
|
#ifdef WITH_CONFIG
|
2025-01-14 14:42:39 +00:00
|
|
|
// We try again to parse the config in the legacy format
|
|
|
|
root = libconfigDecode(f);
|
2019-03-26 07:03:57 +01:00
|
|
|
#else
|
2025-01-14 14:42:39 +00:00
|
|
|
throw JanssonParseError(err);
|
|
|
|
#endif // WITH_CONFIG
|
|
|
|
}
|
2020-06-15 22:22:20 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return root;
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
std::list<std::string> Config::getIncludeDirectories(FILE *f) const {
|
|
|
|
int ret, fd;
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
char *dir;
|
2021-09-13 19:14:22 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
std::list<std::string> dirs;
|
2021-09-13 19:14:22 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Adding directory of base configuration file
|
|
|
|
fd = fileno(f);
|
|
|
|
if (fd < 0)
|
|
|
|
throw SystemError("Failed to get file descriptor");
|
2021-09-13 19:14:22 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
auto path = fmt::format("/proc/self/fd/{}", fd);
|
2021-09-13 19:14:22 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = readlink(path.c_str(), buf, sizeof(buf));
|
|
|
|
if (ret > 0) {
|
|
|
|
buf[ret] = 0;
|
|
|
|
if (isLocalFile(buf)) {
|
|
|
|
dir = dirname(buf);
|
|
|
|
dirs.push_back(dir);
|
|
|
|
}
|
|
|
|
}
|
2021-09-13 19:14:22 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Adding current working directory
|
|
|
|
dir = getcwd(buf, sizeof(buf));
|
|
|
|
if (dir != nullptr)
|
|
|
|
dirs.push_back(dir);
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return dirs;
|
2021-02-19 06:39:47 +01:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
std::list<std::string> Config::resolveIncludes(const std::string &n) {
|
|
|
|
glob_t gb;
|
|
|
|
int ret, flags = 0;
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
memset(&gb, 0, sizeof(gb));
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
auto name = n;
|
|
|
|
resolveEnvVars(name);
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (name.size() >= 1 && name[0] == '/') { // absolute path
|
|
|
|
ret = glob(name.c_str(), flags, nullptr, &gb);
|
|
|
|
if (ret && ret != GLOB_NOMATCH)
|
|
|
|
gb.gl_pathc = 0;
|
|
|
|
} else { // relative path
|
|
|
|
for (auto &dir : includeDirectories) {
|
|
|
|
auto pattern = fmt::format("{}/{}", dir, name.c_str());
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = glob(pattern.c_str(), flags, nullptr, &gb);
|
|
|
|
if (ret && ret != GLOB_NOMATCH) {
|
|
|
|
gb.gl_pathc = 0;
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2021-09-19 18:15:05 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
flags |= GLOB_APPEND;
|
|
|
|
}
|
|
|
|
}
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
out:
|
|
|
|
std::list<std::string> files;
|
|
|
|
for (unsigned i = 0; i < gb.gl_pathc; i++)
|
|
|
|
files.push_back(gb.gl_pathv[i]);
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
globfree(&gb);
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return files;
|
2021-09-14 22:51:39 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
void Config::resolveEnvVars(std::string &text) {
|
|
|
|
static const std::regex env_re{R"--(\$\{([^}]+)\})--"};
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
std::smatch match;
|
|
|
|
while (std::regex_search(text, match, env_re)) {
|
|
|
|
auto const from = match[0];
|
|
|
|
auto const var_name = match[1].str().c_str();
|
|
|
|
char *var_value = std::getenv(var_name);
|
|
|
|
if (!var_value)
|
|
|
|
throw RuntimeError("Unresolved environment variable: {}", var_name);
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
text.replace(from.first - text.begin(), from.second - from.first,
|
|
|
|
var_value);
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
logger->debug("Replace env var {} in \"{}\" with value \"{}\"", var_name,
|
|
|
|
text, var_value);
|
|
|
|
}
|
2021-09-14 22:51:39 +02:00
|
|
|
}
|
|
|
|
|
2021-09-15 14:22:56 +02:00
|
|
|
#ifdef WITH_CONFIG
|
2025-01-14 14:42:39 +00:00
|
|
|
#if (LIBCONFIG_VER_MAJOR > 1) || \
|
|
|
|
((LIBCONFIG_VER_MAJOR == 1) && (LIBCONFIG_VER_MINOR >= 7))
|
|
|
|
const char **Config::includeFuncStub(config_t *cfg, const char *include_dir,
|
|
|
|
const char *path, const char **error) {
|
|
|
|
void *ctx = config_get_hook(cfg);
|
|
|
|
|
|
|
|
return reinterpret_cast<Config *>(ctx)->includeFunc(cfg, include_dir, path,
|
|
|
|
error);
|
2021-09-15 14:22:56 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
const char **Config::includeFunc(config_t *cfg, const char *include_dir,
|
|
|
|
const char *path, const char **error) {
|
|
|
|
auto paths = resolveIncludes(path);
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
unsigned i = 0;
|
|
|
|
auto files = (const char **)malloc(sizeof(char **) * (paths.size() + 1));
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
for (auto &path : paths)
|
|
|
|
files[i++] = strdup(path.c_str());
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
files[i] = NULL;
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return files;
|
2021-09-14 22:51:39 +02:00
|
|
|
}
|
2021-09-15 14:22:56 +02:00
|
|
|
#endif
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_t *Config::libconfigDecode(FILE *f) {
|
|
|
|
int ret;
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
config_t cfg;
|
|
|
|
config_setting_t *cfg_root;
|
|
|
|
config_init(&cfg);
|
|
|
|
config_set_auto_convert(&cfg, 1);
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Setup libconfig include path
|
|
|
|
#if (LIBCONFIG_VER_MAJOR > 1) || \
|
|
|
|
((LIBCONFIG_VER_MAJOR == 1) && (LIBCONFIG_VER_MINOR >= 7))
|
|
|
|
config_set_hook(&cfg, this);
|
2021-09-14 22:51:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
config_set_include_func(&cfg, includeFuncStub);
|
2021-09-14 22:51:39 +02:00
|
|
|
#else
|
2025-01-14 14:42:39 +00:00
|
|
|
if (includeDirectories.size() > 0) {
|
|
|
|
logger->info("Setting include dir to: {}", includeDirectories.front());
|
|
|
|
|
|
|
|
config_set_include_dir(&cfg, includeDirectories.front().c_str());
|
|
|
|
|
|
|
|
if (includeDirectories.size() > 1) {
|
|
|
|
logger->warn(
|
|
|
|
"Ignoring all but the first include directories for libconfig");
|
|
|
|
logger->warn(
|
|
|
|
" libconfig does not support more than a single include dir!");
|
|
|
|
}
|
|
|
|
}
|
2021-09-14 22:51:39 +02:00
|
|
|
#endif
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Rewind before re-reading
|
|
|
|
rewind(f);
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = config_read(&cfg, f);
|
|
|
|
if (ret != CONFIG_TRUE)
|
|
|
|
throw LibconfigParseError(&cfg);
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
cfg_root = config_root_setting(&cfg);
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_t *root = config_to_json(cfg_root);
|
|
|
|
if (!root)
|
|
|
|
throw RuntimeError("Failed to convert JSON to configuration file");
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
config_destroy(&cfg);
|
2020-06-15 22:22:20 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return root;
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|
2025-01-14 14:42:39 +00:00
|
|
|
#endif // WITH_CONFIG
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_t *Config::walkStrings(json_t *root, str_walk_fcn_t cb) {
|
|
|
|
const char *key;
|
|
|
|
size_t index;
|
|
|
|
json_t *val, *new_val, *new_root;
|
2020-06-15 22:22:20 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
switch (json_typeof(root)) {
|
|
|
|
case JSON_STRING:
|
|
|
|
return cb(root);
|
2020-06-15 22:22:20 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case JSON_OBJECT:
|
|
|
|
new_root = json_object();
|
2020-06-15 22:22:20 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_object_foreach(root, key, val) {
|
|
|
|
new_val = walkStrings(val, cb);
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_object_set_new(new_root, key, new_val);
|
|
|
|
}
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return new_root;
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case JSON_ARRAY:
|
|
|
|
new_root = json_array();
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_array_foreach(root, index, val) {
|
|
|
|
new_val = walkStrings(val, cb);
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_array_append_new(new_root, new_val);
|
|
|
|
}
|
2020-06-15 22:22:20 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return new_root;
|
2020-06-15 22:22:20 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
default:
|
|
|
|
return json_incref(root);
|
|
|
|
};
|
2020-06-15 22:22:20 +02:00
|
|
|
}
|
2019-03-26 07:03:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_t *Config::expandEnvVars(json_t *in) {
|
|
|
|
return walkStrings(in, [this](json_t *str) -> json_t * {
|
|
|
|
std::string text = json_string_value(str);
|
2020-06-15 22:22:20 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
resolveEnvVars(text);
|
2020-06-15 22:22:20 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return json_string(text.c_str());
|
|
|
|
});
|
2020-06-15 22:22:20 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_t *Config::expandIncludes(json_t *in) {
|
|
|
|
return walkStrings(in, [this](json_t *str) -> json_t * {
|
|
|
|
int ret;
|
|
|
|
std::string text = json_string_value(str);
|
|
|
|
static const std::string kw = "@include ";
|
|
|
|
|
|
|
|
if (text.find(kw) != 0)
|
|
|
|
return json_incref(str);
|
|
|
|
else {
|
|
|
|
std::string pattern = text.substr(kw.size());
|
|
|
|
|
|
|
|
resolveEnvVars(pattern);
|
|
|
|
|
|
|
|
json_t *incl = nullptr;
|
|
|
|
|
|
|
|
for (auto &path : resolveIncludes(pattern)) {
|
|
|
|
json_t *other = load(path);
|
|
|
|
if (!other)
|
|
|
|
throw ConfigError(str, "Failed to include config file from {}", path);
|
|
|
|
|
|
|
|
if (!incl)
|
|
|
|
incl = other;
|
|
|
|
else if (json_is_object(incl) && json_is_object(other)) {
|
|
|
|
ret = json_object_update_recursive(incl, other);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(
|
|
|
|
str, "Can not mix object and array-typed include files");
|
|
|
|
} else if (json_is_array(incl) && json_is_array(other)) {
|
|
|
|
ret = json_array_extend(incl, other);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(
|
|
|
|
str, "Can not mix object and array-typed include files");
|
|
|
|
}
|
|
|
|
|
|
|
|
logger->debug("Included config from: {}", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return incl;
|
|
|
|
}
|
|
|
|
});
|
2019-03-26 07:03:57 +01:00
|
|
|
}
|