From 116b9e2c0cc684e76db19797ac840c22cf7301d0 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Fri, 5 Nov 2021 15:27:24 -0700 Subject: [PATCH] Add config set command to daemon command processor This change allows setting the configuration from the socket. It behaves similar to the override, and requires the client to send the section, key and value, and responds with an OK or error value. --- daemon/x52d_command.c | 23 +++++++++++++++++++---- daemon/x52d_config.c | 13 +++++++++++++ daemon/x52d_config.h | 4 ++++ daemon/x52d_config_parser.c | 10 +++++----- po/libx52.pot | 10 +++++----- po/xx_PL.po | 10 +++++----- 6 files changed, 51 insertions(+), 19 deletions(-) diff --git a/daemon/x52d_command.c b/daemon/x52d_command.c index 43d3911..6f6d2f2 100644 --- a/daemon/x52d_command.c +++ b/daemon/x52d_command.c @@ -224,7 +224,7 @@ static void cmd_config(char *buffer, int *buflen, int argc, char **argv) x52d_config_load(argv[2]); x52d_config_apply(); - OK("load", argv[2]); + OK("config", "load", argv[2]); } else { // Invalid number of args ERR_fmt("Unexpected arguments for 'config load' command; got %d, expected 3", argc); @@ -235,7 +235,7 @@ static void cmd_config(char *buffer, int *buflen, int argc, char **argv) MATCH(1, "reload") { if (argc == 2) { raise(SIGHUP); - OK("reload"); + OK("config", "reload"); } else { ERR_fmt("Unexpected arguments for 'config reload' command; got %d, expected 2", argc); } @@ -250,7 +250,7 @@ static void cmd_config(char *buffer, int *buflen, int argc, char **argv) } x52d_config_save(argv[2]); - OK("dump", argv[2]); + OK("config", "dump", argv[2]); } else { ERR_fmt("Unexpected arguments for 'config dump' command; got %d, expected 3", argc); } @@ -261,13 +261,28 @@ static void cmd_config(char *buffer, int *buflen, int argc, char **argv) MATCH(1, "save") { if (argc == 2) { raise(SIGUSR1); - OK("save"); + OK("config", "save"); } else { ERR_fmt("Unexpected arguments for 'config save' command; got %d, expected 2", argc); } return; } + MATCH(1, "set") { + if (argc == 5) { + int rc = x52d_config_set(argv[2], argv[3], argv[4]); + if (rc != 0) { + ERR_fmt("Error %d setting '%s.%s'='%s': %s", rc, + argv[2], argv[3], argv[4], strerror(rc)); + } else { + OK("config", "set", argv[2], argv[3], argv[4]); + } + } else { + ERR_fmt("Unexpected arguments for 'config set' command; got %d, expected 5", argc); + } + return; + } + ERR_fmt("Unknown subcommand '%s' for 'config' command", argv[1]); } diff --git a/daemon/x52d_config.c b/daemon/x52d_config.c index 7d9e12f..60f56ad 100644 --- a/daemon/x52d_config.c +++ b/daemon/x52d_config.c @@ -7,6 +7,8 @@ */ #include "config.h" +#include + #include "pinelog.h" #include "x52d_config.h" #include "x52d_const.h" @@ -55,6 +57,17 @@ void x52d_config_save(const char *cfg_file) } } +int x52d_config_set(const char *section, const char *key, const char *value) +{ + if (section == NULL || key == NULL || value == NULL) { + return EINVAL; + } + + PINELOG_TRACE("Processing config set '%s.%s'='%s'", section, key, value); + + return x52d_config_process_kv(&x52d_config, section, key, value); +} + /* Callback stubs * TODO: Remove the ones below when their implementation is complete */ diff --git a/daemon/x52d_config.h b/daemon/x52d_config.h index e959a99..db7dda5 100644 --- a/daemon/x52d_config.h +++ b/daemon/x52d_config.h @@ -75,6 +75,8 @@ void x52d_cfg_set_Profiles_Directory(char* param); void x52d_cfg_set_Profiles_ClutchEnabled(bool param); void x52d_cfg_set_Profiles_ClutchLatched(bool param); +int x52d_config_process_kv(void *user, const char *section, const char *key, const char *value); + int x52d_config_set_defaults(struct x52d_config *cfg); int x52d_config_load_file(struct x52d_config *cfg, const char *cfg_file); @@ -91,4 +93,6 @@ void x52d_config_apply(void); int x52d_config_save_file(struct x52d_config *cfg, const char *cfg_file); void x52d_config_save(const char *cfg_file); +int x52d_config_set(const char *section, const char *key, const char *value); + #endif // !defined X52D_CONFIG_H diff --git a/daemon/x52d_config_parser.c b/daemon/x52d_config_parser.c index 58f3839..15ebdf8 100644 --- a/daemon/x52d_config_parser.c +++ b/daemon/x52d_config_parser.c @@ -131,7 +131,7 @@ static int date_format_parser(struct x52d_config *cfg, size_t offset, const char /* Map for config->param */ #define CFG(section, key, name, type, def) {#section, #key, type ## _parser, offsetof(struct x52d_config, name)}, -const struct config_map { +static const struct config_map { const char *section; const char *key; parser_fn parser; @@ -143,7 +143,7 @@ const struct config_map { {NULL, NULL, NULL, 0} }; -static int process_config_kv(void *user, const char *section, const char *key, const char *value) +int x52d_config_process_kv(void *user, const char *section, const char *key, const char *value) { int i; int rc = 0; @@ -186,7 +186,7 @@ int x52d_config_set_defaults(struct x52d_config *cfg) { PINELOG_TRACE("Setting configuration defaults"); #define CFG(section, key, name, parser, def) \ - rc = process_config_kv(cfg, #section, #key, #def); \ + rc = x52d_config_process_kv(cfg, #section, #key, #def); \ if (rc != 0) { \ return rc; \ } @@ -203,7 +203,7 @@ int x52d_config_load_file(struct x52d_config *cfg, const char *cfg_file) } PINELOG_TRACE("Loading configuration from file %s", cfg_file); - rc = ini_parse(cfg_file, process_config_kv, cfg); + rc = ini_parse(cfg_file, x52d_config_process_kv, cfg); if (rc < 0) { PINELOG_ERROR(_("Failed processing configuration file %s - code %d"), cfg_file, rc); @@ -326,7 +326,7 @@ int x52d_config_apply_overrides(struct x52d_config *cfg) tmp->section, tmp->key, tmp->value); - rc = process_config_kv(cfg, + rc = x52d_config_process_kv(cfg, tmp->section, tmp->key, tmp->value); diff --git a/po/libx52.pot b/po/libx52.pot index ebf1b89..4575c01 100644 --- a/po/libx52.pot +++ b/po/libx52.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: libx52 0.2.3\n" "Report-Msgid-Bugs-To: https://github.com/nirenjan/libx52/issues\n" -"POT-Creation-Date: 2021-11-05 15:09-0700\n" +"POT-Creation-Date: 2021-11-05 15:27-0700\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -678,22 +678,22 @@ msgstr "" msgid "Timed out when polling for command" msgstr "" -#: daemon/x52d_command.c:330 +#: daemon/x52d_command.c:345 #, c-format msgid "Error reading from client %d: %s" msgstr "" -#: daemon/x52d_command.c:341 +#: daemon/x52d_command.c:356 #, c-format msgid "Short write to client %d; expected %d bytes, wrote %d bytes" msgstr "" -#: daemon/x52d_config.c:26 +#: daemon/x52d_config.c:28 #, c-format msgid "Error %d setting configuration defaults: %s" msgstr "" -#: daemon/x52d_config.c:53 +#: daemon/x52d_config.c:55 #, c-format msgid "Error %d saving configuration file: %s" msgstr "" diff --git a/po/xx_PL.po b/po/xx_PL.po index 3091c92..b11d1da 100644 --- a/po/xx_PL.po +++ b/po/xx_PL.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: libx52 0.2.3\n" "Report-Msgid-Bugs-To: https://github.com/nirenjan/libx52/issues\n" -"POT-Creation-Date: 2021-11-05 15:09-0700\n" +"POT-Creation-Date: 2021-11-05 15:27-0700\n" "PO-Revision-Date: 2021-11-04 15:35-0700\n" "Last-Translator: Nirenjan Krishnan \n" "Language-Team: Dummy Language for testing i18n\n" @@ -729,23 +729,23 @@ msgstr "Erroray enwhay ollingpay orfay ommandcay: %s" msgid "Timed out when polling for command" msgstr "Imedtay outay enwhay ollingpay orfay ommandcay" -#: daemon/x52d_command.c:330 +#: daemon/x52d_command.c:345 #, c-format msgid "Error reading from client %d: %s" msgstr "Erroray eadingray omfray ientclay %d: %s" -#: daemon/x52d_command.c:341 +#: daemon/x52d_command.c:356 #, c-format msgid "Short write to client %d; expected %d bytes, wrote %d bytes" msgstr "" "Ortshay itewray otay ientclay %d; expecteday %d ytesbay, otewray %d ytesbay" -#: daemon/x52d_config.c:26 +#: daemon/x52d_config.c:28 #, c-format msgid "Error %d setting configuration defaults: %s" msgstr "Erroray %d ettingsay onfigurationcay efaultsday: %s" -#: daemon/x52d_config.c:53 +#: daemon/x52d_config.c:55 #, c-format msgid "Error %d saving configuration file: %s" msgstr "Erroray %d avingsay onfigurationcay ilefay: %s"