From 86b14eb4e0473360cd35163dfe2366f5c91384b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulius=20Ratkevi=C4=8Dius?= Date: Fri, 17 May 2019 15:29:11 +0300 Subject: daemon: semi-stub for config file handling. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The daemon now load and somewhat parses the config files. It does not do anything else with the data yet, that's a TODO. Signed-off-by: Paulius Ratkevičius --- src/daemon/settings.c | 66 +++++++++++++++++++++++++++++++++++++++++++ src/daemon/settings_private.h | 2 ++ 2 files changed, 68 insertions(+) diff --git a/src/daemon/settings.c b/src/daemon/settings.c index 4392300..acbf40d 100644 --- a/src/daemon/settings.c +++ b/src/daemon/settings.c @@ -19,6 +19,14 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include #include "settings.h" #include "settings_private.h" @@ -31,6 +39,7 @@ void settings_init(void) unset_flag(flag_daemonize); settings.port = 6996; settings.verboselevel = USURP_VERBOSITY; + setting_readconf("./usurpation.conf"); } int setting_detach(void) @@ -68,3 +77,60 @@ void unset_flag(unsigned int flag) { settings.flags &= ~flag; } + +/* TODO: semi-stub! */ +static int setting_readconf(const char * const path) +{ + struct stat filestat; + char *buf; + char *line; + char *entry; + char *value; + char *line_state; + char *token_state; + int fd; + + + if ((fd = open(path, O_RDONLY | O_NOATIME)) == -1) { + if (USURP_VERBOSITY >= 2) { + perror("error while opening the configuration file"); + return 1; + } + } + + fstat(fd, &filestat); + buf = malloc(filestat.st_size); + + if (read(fd, buf, filestat.st_size) != filestat.st_size) { + if (USURP_VERBOSITY >= 2) { + perror("partial read of configuration file detected"); + free(buf); + return 1; + } + } + + line = strtok_r(buf, "\n", &line_state); + + while (line) { + if (line[0] != '#') { + entry = strtok_r(line, "=", &token_state); + value = strtok_r(NULL, "=", &token_state); + setting_handle_config_entry(entry, value); + } + + line = strtok_r(NULL, "\n", &line_state); + } + + free(buf); + + return 0; +} + +/* TODO: stub! */ +static int setting_handle_config_entry(const char * const entry, const char * const value) +{ + printf( "key: %s\n" + "value: %s\n", entry, value); + + return 0; +} diff --git a/src/daemon/settings_private.h b/src/daemon/settings_private.h index ee49145..42c1fa5 100644 --- a/src/daemon/settings_private.h +++ b/src/daemon/settings_private.h @@ -37,5 +37,7 @@ static struct settings { int test_flag(unsigned int flag); void set_flag(unsigned int flag); void unset_flag(unsigned int flag); +static int setting_readconf(const char * const path); +static int setting_handle_config_entry(const char * const entry, const char * const value); #endif /* USURPATION_SETTINGS_PRIVATE_H */ -- cgit v1.2.3 From bd427117a36715cc602e85869cd7d6db6415a945 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Sun, 19 May 2019 16:15:11 +0300 Subject: daemon: handle the daemonize flag now. The flag is now successfully read from the config file and is honored. Logic does not handle leading or trailing whitespace yet. Signed-off-by: Gediminas Jakutis --- src/daemon/settings.c | 31 ++++++++++++++++++++++++++++--- src/daemon/settings_private.h | 14 ++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/daemon/settings.c b/src/daemon/settings.c index acbf40d..355fe1d 100644 --- a/src/daemon/settings.c +++ b/src/daemon/settings.c @@ -129,8 +129,33 @@ static int setting_readconf(const char * const path) /* TODO: stub! */ static int setting_handle_config_entry(const char * const entry, const char * const value) { - printf( "key: %s\n" - "value: %s\n", entry, value); + size_t i; - return 0; + for (i = 0; i < sizeof(ent_table)/sizeof(*ent_table); ++i) { + if (!(strcmp(entry, ent_table[i].name))) { + ent_table[0].set(value); + return 0; + } + } + + return 1; +} + +static void set_daemonize(const void * const arg) +{ + const char * const a = arg; + + atoi(a) ? set_flag(flag_daemonize) : unset_flag(flag_daemonize); +} + +/* TODO: stub! */ +static void set_port(const void * const arg) +{ + (void) arg; +} + +/* TODO: stub! */ +static void set_verbosity(const void * const arg) +{ + (void) arg; } diff --git a/src/daemon/settings_private.h b/src/daemon/settings_private.h index 42c1fa5..2d97bdd 100644 --- a/src/daemon/settings_private.h +++ b/src/daemon/settings_private.h @@ -34,10 +34,24 @@ static struct settings { enum verbosity verboselevel; } settings; +struct entry_description { + const char * const name; + void (*set)(const void * const); +}; + + int test_flag(unsigned int flag); void set_flag(unsigned int flag); void unset_flag(unsigned int flag); static int setting_readconf(const char * const path); static int setting_handle_config_entry(const char * const entry, const char * const value); +static void set_daemonize(const void * const arg); +static void set_port(const void * const arg); +static void set_verbosity(const void * const arg); + +struct entry_description ent_table[] = {{ "daemonize", set_daemonize }, + { "port", set_port }, + { "verbosity", set_verbosity }}; + #endif /* USURPATION_SETTINGS_PRIVATE_H */ -- cgit v1.2.3 From a40cd32e24e18857ab937e1142fe4146334b455c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulius=20Ratkevi=C4=8Dius?= Date: Sun, 19 May 2019 16:19:04 +0300 Subject: daemon: Settings actually parse. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paulius Ratkevičius --- src/daemon/settings.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/daemon/settings.c b/src/daemon/settings.c index 355fe1d..855d4bb 100644 --- a/src/daemon/settings.c +++ b/src/daemon/settings.c @@ -126,14 +126,13 @@ static int setting_readconf(const char * const path) return 0; } -/* TODO: stub! */ static int setting_handle_config_entry(const char * const entry, const char * const value) { size_t i; for (i = 0; i < sizeof(ent_table)/sizeof(*ent_table); ++i) { if (!(strcmp(entry, ent_table[i].name))) { - ent_table[0].set(value); + ent_table[i].set(value); return 0; } } @@ -148,14 +147,16 @@ static void set_daemonize(const void * const arg) atoi(a) ? set_flag(flag_daemonize) : unset_flag(flag_daemonize); } -/* TODO: stub! */ static void set_port(const void * const arg) { - (void) arg; + const char * const a = arg; + + settings.port = strtoul(a, NULL, 0); } -/* TODO: stub! */ static void set_verbosity(const void * const arg) { - (void) arg; + const char * const a = arg; + + settings.verboselevel = strtol(a, NULL, 0); } -- cgit v1.2.3 From 0389fdc7507238221a9747c5fcbca26952e3229c Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Sun, 19 May 2019 17:08:15 +0300 Subject: daemon: read the system-wide config file now. This usually, but now always means "/etc/usurpation.conf" Signed-off-by: Gediminas Jakutis --- meson.build | 2 +- src/daemon/settings.c | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/meson.build b/meson.build index 4a780c8..636aa4b 100644 --- a/meson.build +++ b/meson.build @@ -22,7 +22,7 @@ elif get_option('buildtype') == 'debugoptimized' verb = '3' endif -daemon = executable(progname, d_sources, version, include_directories : inc, install : true, dependencies : deps, extra_files : d_conf, c_args : '-DUSURP_VERBOSITY=' + verb) +daemon = executable(progname, d_sources, version, include_directories : inc, install : true, dependencies : deps, extra_files : d_conf, c_args : ['-DUSURP_VERBOSITY=' + verb, '-DSYSCONFDIR=' + get_option('sysconfdir')]) #install_data(extra, install_dir : resource_dir) install_data(d_conf, install_dir : get_option('sysconfdir')) diff --git a/src/daemon/settings.c b/src/daemon/settings.c index 855d4bb..e5941cc 100644 --- a/src/daemon/settings.c +++ b/src/daemon/settings.c @@ -39,7 +39,11 @@ void settings_init(void) unset_flag(flag_daemonize); settings.port = 6996; settings.verboselevel = USURP_VERBOSITY; - setting_readconf("./usurpation.conf"); +#define macro2str(a) _macro2str(a) +#define _macro2str(a) #a + setting_readconf("/" macro2str(SYSCONFDIR) "/usurpation.conf"); +#undef macro2str +#undef _macro2str } int setting_detach(void) @@ -91,7 +95,7 @@ static int setting_readconf(const char * const path) int fd; - if ((fd = open(path, O_RDONLY | O_NOATIME)) == -1) { + if ((fd = open(path, O_RDONLY)) == -1) { if (USURP_VERBOSITY >= 2) { perror("error while opening the configuration file"); return 1; -- cgit v1.2.3