From: Michael Tremer Date: Fri, 9 Jun 2023 05:37:57 +0000 (+0000) Subject: networkd: Hold a file descriptor instead of DIR* X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f6e3fd80ad4d86681ad2949567b6763c41530f16;p=network.git networkd: Hold a file descriptor instead of DIR* Signed-off-by: Michael Tremer --- diff --git a/src/networkd/daemon.c b/src/networkd/daemon.c index e645beec..f0ef4aa3 100644 --- a/src/networkd/daemon.c +++ b/src/networkd/daemon.c @@ -18,12 +18,12 @@ # # #############################################################################*/ -#include #include #include #include #include #include +#include #include #include @@ -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)