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