]> git.ipfire.org Git - network.git/blobdiff - src/networkd/daemon.c
networkd: Implement smarter handling of the configuration file hierarchy
[network.git] / src / networkd / daemon.c
index a62e343fdef1318f07c7a9e69e2714ef236a77a2..dfbcc151d72cd1a4e2bd29897c2b5f95c9dac7c2 100644 (file)
@@ -51,7 +51,7 @@ struct nw_daemon {
        int nrefs;
 
        // Configuration
-       int configfd;
+       nw_configd* configd;
        nw_config* config;
 
        // Event Loop
@@ -103,36 +103,14 @@ static int __nw_daemon_reload(sd_event_source* source, const struct signalfd_sig
 */
 
 static int nw_daemon_config_open(nw_daemon* daemon, const char* path) {
-       // Open the directory
-       daemon->configfd = open(path, O_DIRECTORY);
-       if (daemon->configfd < 0) {
-               ERROR("Could not open %s: %m\n", path);
-               return -errno;
-       }
-
-       return 0;
-}
-
-FILE* nw_daemon_config_fopen(nw_daemon* daemon, const char* path, const char* mode) {
-       // Open the file
-       int fd = openat(daemon->configfd, 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);
-}
+       int r;
 
-DIR* nw_daemon_config_opendir(nw_daemon* daemon, const char* path) {
-       int fd = openat(daemon->configfd, path, O_DIRECTORY);
-       if (fd < 0) {
-               ERROR("Could not open configuration directory %s: %m\n", path);
-               return NULL;
-       }
+       // Open the configuration directory
+       r = nw_configd_create(&daemon->configd, path);
+       if (r < 0)
+               return r;
 
-       return fdopendir(fd);
+       return 0;
 }
 
 static int nw_daemon_parse_argv(nw_daemon* daemon, int argc, char* argv[]) {
@@ -213,33 +191,17 @@ static int nw_daemon_setup_loop(nw_daemon* daemon) {
 }
 
 static int nw_daemon_load_config(nw_daemon* daemon) {
-       FILE* f = NULL;
        int r;
 
        // If no configuration path has been opened yet, we will open something
-       if (!daemon->configfd) {
+       if (!daemon->configd) {
                r = nw_daemon_config_open(daemon, CONFIG_DIR);
                if (r < 0)
-                       goto ERROR;
+                       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, f);
-       if (r < 0)
-               goto ERROR;
-
-ERROR:
-       if (f)
-               fclose(f);
-
-       return r;
+       return nw_configd_open_config(&daemon->config, daemon->configd, "settings");
 }
 
 static int nw_start_device_monitor(nw_daemon* daemon) {
@@ -540,8 +502,8 @@ static void nw_daemon_free(nw_daemon* daemon) {
        // Cleanup common objects
        nw_daemon_cleanup(daemon);
 
-       if (daemon->configfd > 0)
-               close(daemon->configfd);
+       if (daemon->configd)
+               nw_configd_unref(daemon->configd);
        if (daemon->stats_collector_event)
                sd_event_source_unref(daemon->stats_collector_event);
        if (daemon->bus)
@@ -640,6 +602,16 @@ int nw_daemon_save(nw_daemon* daemon) {
        return 0;
 }
 
+nw_configd* nw_daemon_configd(nw_daemon* daemon, const char* path) {
+       if (!daemon->configd)
+               return NULL;
+
+       if (path)
+               return nw_configd_descend(daemon->configd, path);
+
+       return nw_configd_ref(daemon->configd);
+}
+
 /*
        Bus
 */