From 5a925d5f91e32245446ba568da2549b08913c0c7 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 9 Jun 2023 05:28:51 +0000 Subject: [PATCH] networkd: Open config directory and keep a handle to it Signed-off-by: Michael Tremer --- src/networkd/config.c | 2 +- src/networkd/config.h | 1 + src/networkd/daemon.c | 71 ++++++++++++++++++++++++++++++++++++++----- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/src/networkd/config.c b/src/networkd/config.c index ee5e8b83..b7f3f722 100644 --- a/src/networkd/config.c +++ b/src/networkd/config.c @@ -229,7 +229,7 @@ int nw_config_flush(nw_config* config) { return 0; } -static int nw_config_readf(nw_config* config, FILE* f) { +int nw_config_readf(nw_config* config, FILE* f) { char* line = NULL; size_t length = 0; int r; diff --git a/src/networkd/config.h b/src/networkd/config.h index 63e9d180..d532da33 100644 --- a/src/networkd/config.h +++ b/src/networkd/config.h @@ -40,6 +40,7 @@ const char* nw_config_path(nw_config* config); int nw_config_flush(nw_config* config); +int nw_config_readf(nw_config* config, FILE* f); int nw_config_read(nw_config* config); int nw_config_write(nw_config* config); diff --git a/src/networkd/daemon.c b/src/networkd/daemon.c index 31fda8ec..e645beec 100644 --- a/src/networkd/daemon.c +++ b/src/networkd/daemon.c @@ -18,7 +18,9 @@ # # #############################################################################*/ +#include #include +#include #include #include #include @@ -48,7 +50,7 @@ struct nw_daemon { int nrefs; - char config_path[PATH_MAX]; + DIR* config_dir; nw_config* config; // Event Loop @@ -95,6 +97,43 @@ static int __nw_daemon_reload(sd_event_source* source, const struct signalfd_sig return 0; } +/* + Configuration +*/ + +static int nw_daemon_config_open(nw_daemon* daemon, const char* path) { + daemon->config_dir = opendir(path); + if (!daemon->config_dir) { + ERROR("Could not open %s: %m\n", path); + return -errno; + } + + return 0; +} + +static FILE* nw_daemon_config_fopen(nw_daemon* daemon, const char* path, const char* mode) { + int r; + + // If no configuration path has been opened yet, we will open something + if (!daemon->config_dir) { + r = nw_daemon_config_open(daemon, CONFIG_DIR); + if (r < 0) { + errno = -r; + return NULL; + } + } + + // Open the file + int fd = openat(dirfd(daemon->config_dir), path, 0); + if (fd < 0) { + ERROR("Could not open configuration file %s: %m\n", path); + return NULL; + } + + // Return a file handle + return fdopen(fd, mode); +} + static int nw_daemon_parse_argv(nw_daemon* daemon, int argc, char* argv[]) { enum { ARG_CONFIG, @@ -114,10 +153,9 @@ static int nw_daemon_parse_argv(nw_daemon* daemon, int argc, char* argv[]) { switch (c) { case ARG_CONFIG: - r = nw_string_set(daemon->config_path, optarg); + r = nw_daemon_config_open(daemon, optarg); if (r < 0) return r; - break; // Abort on any unrecognised option @@ -174,12 +212,29 @@ static int nw_daemon_setup_loop(nw_daemon* daemon) { } static int nw_daemon_load_config(nw_daemon* daemon) { + FILE* f = NULL; int r; - // Read configuration file - r = nw_config_create(&daemon->config, CONFIG_DIR "/settings"); - if (r) - return r; + // Open the configuration file + f = nw_daemon_config_fopen(daemon, "settings", "r"); + if (!f) { + r = -errno; + goto ERROR; + } + + // Create configuration + r = nw_config_create(&daemon->config, NULL); + if (r < 0) + goto ERROR; + + // Parse configuration + r = nw_config_readf(daemon->config, f); + if (r < 0) + goto ERROR; + +ERROR: + if (f) + fclose(f); return r; } @@ -482,6 +537,8 @@ static void nw_daemon_free(nw_daemon* daemon) { // Cleanup common objects nw_daemon_cleanup(daemon); + if (daemon->config_dir) + closedir(daemon->config_dir); if (daemon->stats_collector_event) sd_event_source_unref(daemon->stats_collector_event); if (daemon->bus) -- 2.39.2