# #
#############################################################################*/
-#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>
struct nw_daemon {
int nrefs;
- DIR* config_dir;
+ // Configuration
+ int configfd;
nw_config* config;
// Event Loop
*/
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;
}
}
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;
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) {
// 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)