mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-16 00:00:02 +01:00
hooks: allow to have special functions for parsing command line arguemnts
This commit is contained in:
parent
f7608fd653
commit
8650e4278e
1 changed files with 45 additions and 55 deletions
98
src/hook.c
98
src/hook.c
|
@ -42,35 +42,36 @@
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
config_t cfg;
|
int cnt;
|
||||||
|
|
||||||
static int hook_parse_cli(struct hook *h, char *params[], int paramlen)
|
struct sample **samples;
|
||||||
|
struct plugin *p;
|
||||||
|
|
||||||
|
struct log l = { .state = STATE_DESTROYED };
|
||||||
|
struct pool q = { .state = STATE_DESTROYED };
|
||||||
|
struct hook h = { .state = STATE_DESTROYED };
|
||||||
|
|
||||||
|
static void quit(int signal, siginfo_t *sinfo, void *ctx)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
char *str;
|
|
||||||
config_setting_t *cfg_root;
|
|
||||||
|
|
||||||
/* Concat all params */
|
ret = hook_stop(&h);
|
||||||
str = NULL;
|
if (ret)
|
||||||
for (int i = 0; i < paramlen; i++)
|
error("Failed to stop hook");
|
||||||
str = strcatf(&str, "%s", params[i]);
|
|
||||||
|
|
||||||
config_set_auto_convert(&cfg, 1);
|
ret = hook_destroy(&h);
|
||||||
|
if (ret)
|
||||||
|
error("Failed to destroy hook");
|
||||||
|
|
||||||
if (str) {
|
sample_free(samples, cnt);
|
||||||
ret = config_read_string(&cfg, str);
|
|
||||||
if (ret != CONFIG_TRUE)
|
|
||||||
error("Failed to parse argument '%s': %s", str, config_error_text(&cfg));
|
|
||||||
}
|
|
||||||
|
|
||||||
//config_write(&cfg, stdout);
|
ret = pool_destroy(&q);
|
||||||
|
if (ret)
|
||||||
|
error("Failed to destroy memory pool");
|
||||||
|
|
||||||
cfg_root = config_root_setting(&cfg);
|
info(CLR_GRN("Goodbye!"));
|
||||||
ret = hook_parse(h, cfg_root);
|
|
||||||
|
|
||||||
free(str);
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void usage()
|
static void usage()
|
||||||
|
@ -95,21 +96,13 @@ static void usage()
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int ret, level;
|
int ret;
|
||||||
|
|
||||||
size_t cnt, recv;
|
size_t recv;
|
||||||
|
|
||||||
/* Default values */
|
/* Default values */
|
||||||
level = V;
|
|
||||||
cnt = 1;
|
cnt = 1;
|
||||||
|
|
||||||
struct log log;
|
|
||||||
struct plugin *p;
|
|
||||||
struct sample *samples[cnt];
|
|
||||||
|
|
||||||
struct pool q = { .state = STATE_DESTROYED };
|
|
||||||
struct hook h = { .state = STATE_DESTROYED };
|
|
||||||
|
|
||||||
char c;
|
char c;
|
||||||
while ((c = getopt(argc, argv, "hv:d:")) != -1) {
|
while ((c = getopt(argc, argv, "hv:d:")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
@ -117,7 +110,7 @@ int main(int argc, char *argv[])
|
||||||
cnt = atoi(optarg);
|
cnt = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
level = atoi(optarg);
|
l.level = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
case '?':
|
case '?':
|
||||||
|
@ -133,32 +126,37 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
char *hookstr = argv[optind];
|
char *hookstr = argv[optind];
|
||||||
|
|
||||||
|
ret = signals_init(quit);
|
||||||
|
if (ret)
|
||||||
|
error("Failed to intialize signals");
|
||||||
|
|
||||||
|
ret = log_init(&l, l.level, LOG_ALL);
|
||||||
|
if (ret)
|
||||||
|
error("Failed to initialize log");
|
||||||
|
|
||||||
|
log_start(&l);
|
||||||
|
|
||||||
if (cnt < 1)
|
if (cnt < 1)
|
||||||
error("Vectorize option must be greater than 0");
|
error("Vectorize option must be greater than 0");
|
||||||
|
|
||||||
log_init(&log, level, LOG_ALL);
|
|
||||||
log_start(&log);
|
|
||||||
|
|
||||||
memory_init(DEFAULT_NR_HUGEPAGES);
|
memory_init(DEFAULT_NR_HUGEPAGES);
|
||||||
|
|
||||||
|
samples = alloc(cnt * sizeof(struct sample *));
|
||||||
|
|
||||||
ret = pool_init(&q, 10 * cnt, SAMPLE_LEN(DEFAULT_SAMPLELEN), &memtype_hugepage);
|
ret = pool_init(&q, 10 * cnt, SAMPLE_LEN(DEFAULT_SAMPLELEN), &memtype_hugepage);
|
||||||
if (ret)
|
if (ret)
|
||||||
error("Failed to initilize memory pool");
|
error("Failed to initilize memory pool");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
p = plugin_lookup(PLUGIN_TYPE_HOOK, hookstr);
|
p = plugin_lookup(PLUGIN_TYPE_HOOK, hookstr);
|
||||||
if (!p)
|
if (!p)
|
||||||
error("Unknown hook function '%s'", hookstr);
|
error("Unknown hook function '%s'", hookstr);
|
||||||
|
|
||||||
config_init(&cfg);
|
|
||||||
|
|
||||||
/** @todo villas-hook does not use the path structure */
|
/** @todo villas-hook does not use the path structure */
|
||||||
ret = hook_init(&h, &p->hook, NULL);
|
ret = hook_init(&h, &p->hook, NULL);
|
||||||
if (ret)
|
if (ret)
|
||||||
error("Failed to initialize hook");
|
error("Failed to initialize hook");
|
||||||
|
|
||||||
ret = hook_parse_cli(&h, &argv[optind + 1], argc - optind - 1);
|
ret = hook_parse_cli(&h, argc - optind - 1, &argv[optind + 1]);
|
||||||
if (ret)
|
if (ret)
|
||||||
error("Failed to parse hook config");
|
error("Failed to parse hook config");
|
||||||
|
|
||||||
|
@ -166,10 +164,15 @@ int main(int argc, char *argv[])
|
||||||
if (ret)
|
if (ret)
|
||||||
error("Failed to start hook");
|
error("Failed to start hook");
|
||||||
|
|
||||||
while (!feof(stdin)) {
|
for (;;) {
|
||||||
|
if (feof(stdin)) {
|
||||||
|
killme(SIGTERM);
|
||||||
|
pause();
|
||||||
|
}
|
||||||
|
|
||||||
ret = sample_alloc(&q, samples, cnt);
|
ret = sample_alloc(&q, samples, cnt);
|
||||||
if (ret != cnt)
|
if (ret != cnt)
|
||||||
error("Failed to allocate %zu samples from pool", cnt);
|
error("Failed to allocate %d samples from pool", cnt);
|
||||||
|
|
||||||
recv = 0;
|
recv = 0;
|
||||||
for (int j = 0; j < cnt && !feof(stdin); j++) {
|
for (int j = 0; j < cnt && !feof(stdin); j++) {
|
||||||
|
@ -193,18 +196,5 @@ int main(int argc, char *argv[])
|
||||||
sample_free(samples, cnt);
|
sample_free(samples, cnt);
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = hook_stop(&h);
|
|
||||||
if (ret)
|
|
||||||
error("Failed to stop hook");
|
|
||||||
|
|
||||||
ret = hook_destroy(&h);
|
|
||||||
if (ret)
|
|
||||||
error("Failed to destroy hook");
|
|
||||||
|
|
||||||
config_destroy(&cfg);
|
|
||||||
|
|
||||||
sample_free(samples, cnt);
|
|
||||||
pool_destroy(&q);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue