]> git.ipfire.org Git - network.git/commitdiff
networkd: Hold a file descriptor instead of DIR*
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 05:37:57 +0000 (05:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 05:38:18 +0000 (05:38 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/daemon.c

index e645beec36efdb96b0c89e54facf91336ca9940f..f0ef4aa31ef1aa2db70c812ee9fba3bc36b4964d 100644 (file)
 #                                                                             #
 #############################################################################*/
 
-#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
 #include <limits.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include <systemd/sd-bus.h>
 #include <systemd/sd-daemon.h>
@@ -50,7 +50,8 @@
 struct nw_daemon {
        int nrefs;
 
-       DIR* config_dir;
+       // Configuration
+       int configfd;
        nw_config* config;
 
        // Event Loop
@@ -102,8 +103,9 @@ 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) {
-       daemon->config_dir = opendir(path);
-       if (!daemon->config_dir) {
+       // Open the directory
+       daemon->configfd = open(path, O_DIRECTORY);
+       if (daemon->configfd < 0) {
                ERROR("Could not open %s: %m\n", path);
                return -errno;
        }
@@ -112,19 +114,8 @@ static int nw_daemon_config_open(nw_daemon* daemon, const char* path) {
 }
 
 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);
+       int fd = openat(daemon->configfd, path, 0);
        if (fd < 0) {
                ERROR("Could not open configuration file %s: %m\n", path);
                return NULL;
@@ -215,6 +206,13 @@ 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) {
+               r = nw_daemon_config_open(daemon, CONFIG_DIR);
+               if (r < 0)
+                       goto ERROR;
+       }
+
        // Open the configuration file
        f = nw_daemon_config_fopen(daemon, "settings", "r");
        if (!f) {
@@ -537,8 +535,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->configfd > 0)
+               close(daemon->configfd);
        if (daemon->stats_collector_event)
                sd_event_source_unref(daemon->stats_collector_event);
        if (daemon->bus)