]> git.ipfire.org Git - people/ms/network.git/commitdiff
networkd: Open config directory and keep a handle to it
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 05:28:51 +0000 (05:28 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 05:28:51 +0000 (05:28 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/config.c
src/networkd/config.h
src/networkd/daemon.c

index ee5e8b832a748db7d0cf3e012139991aa23bf8c3..b7f3f7220e8f2835343374ce3e4495131fc4861e 100644 (file)
@@ -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;
index 63e9d180f217e95b4c24f12587ec3ab70696aaae..d532da33ad71e813b1cf1a67cf466ca4ff2ed021 100644 (file)
@@ -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);
 
index 31fda8ecd6306ae373a5579430284a4c009bdce3..e645beec36efdb96b0c89e54facf91336ca9940f 100644 (file)
@@ -18,7 +18,9 @@
 #                                                                             #
 #############################################################################*/
 
+#include <dirent.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <getopt.h>
 #include <limits.h>
 #include <stdlib.h>
@@ -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)