summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
119cc5b)
This entails a little rewrite how we deal with where configuration files
are stored.
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
struct nw_config {
int nrefs;
struct nw_config {
int nrefs;
- // The path to the configuration file
- char path[PATH_MAX];
-
STAILQ_HEAD(config_entries, nw_config_entry) entries;
// Options
STAILQ_HEAD(config_entries, nw_config_entry) entries;
// Options
-int nw_config_create(nw_config** config, const char* path) {
+int nw_config_create(nw_config** config, FILE* f) {
int r;
nw_config* c = calloc(1, sizeof(*c));
int r;
nw_config* c = calloc(1, sizeof(*c));
// Initialise options
STAILQ_INIT(&c->options);
// Initialise options
STAILQ_INIT(&c->options);
- // Store the path
- if (path) {
- r = nw_string_set(c->path, path);
- if (r)
- goto ERROR;
-
- // Try to read the configuration from path
- r = nw_config_read(c);
- if (r)
+ // Read configuration
+ if (f) {
+ r = nw_config_read(c, f);
+ if (r < 0)
+int nw_config_open(nw_config** config, const char* path) {
+ FILE* f = NULL;
+ int r;
+
+ // Open path
+ f = fopen(path, "r");
+ if (!f)
+ return -errno;
+
+ // Create a new configuration
+ r = nw_config_create(config, f);
+
+ERROR:
+ if (f)
+ fclose(f);
+
+ return r;
+}
+
nw_config* nw_config_ref(nw_config* config) {
config->nrefs++;
nw_config* nw_config_ref(nw_config* config) {
config->nrefs++;
-int nw_config_destroy(nw_config* config) {
- int r;
-
- // Drop all entries
- r = nw_config_flush(config);
- if (r)
- return r;
-
- return unlink(config->path);
-}
-
int nw_config_copy(nw_config* config, nw_config** copy) {
struct nw_config_entry* entry = NULL;
nw_config* c = NULL;
int nw_config_copy(nw_config* config, nw_config** copy) {
struct nw_config_entry* entry = NULL;
nw_config* c = NULL;
-const char* nw_config_path(nw_config* config) {
- if (*config->path)
- return config->path;
-
- return NULL;
-}
-
int nw_config_flush(nw_config* config) {
struct nw_config_entry* entry = NULL;
int nw_config_flush(nw_config* config) {
struct nw_config_entry* entry = NULL;
-int nw_config_readf(nw_config* config, FILE* f) {
+int nw_config_read(nw_config* config, FILE* f) {
char* line = NULL;
size_t length = 0;
int r;
char* line = NULL;
size_t length = 0;
int r;
-int nw_config_read(nw_config* config) {
- FILE* f = NULL;
- int r;
-
- // We cannot read if path is not set
- if (!*config->path) {
- errno = ENOTSUP;
- return 1;
- }
-
- // Open the file
- f = fopen(config->path, "r");
- if (!f) {
- // Silently ignore if the file does not exist
- if (errno == ENOENT)
- return 0;
-
- ERROR("Could not read configuration file %s: %m\n", config->path);
- r = 1;
- goto ERROR;
- }
-
- // Read from file
- r = nw_config_readf(config, f);
-
-ERROR:
- if (f)
- fclose(f);
-
- return r;
-}
-
-static int nw_config_writef(nw_config* config, FILE* f) {
+int nw_config_write(nw_config* config, FILE* f) {
struct nw_config_entry* entry = NULL;
int r;
struct nw_config_entry* entry = NULL;
int r;
-int nw_config_write(nw_config* config) {
- int r;
-
- // We cannot write if path is not set
- if (!*config->path) {
- errno = ENOTSUP;
- return 1;
- }
-
- FILE* f = fopen(config->path, "w");
- if (!f) {
- ERROR("Failed to open %s for writing: %m\n", config->path);
- r = 1;
- goto ERROR;
- }
-
- // Write configuration
- r = nw_config_writef(config, f);
-
-ERROR:
- if (f)
- fclose(f);
-
- return r;
-}
-
static struct nw_config_entry* nw_config_find(nw_config* config, const char* key) {
struct nw_config_entry* entry = NULL;
static struct nw_config_entry* nw_config_find(nw_config* config, const char* key) {
struct nw_config_entry* entry = NULL;
typedef struct nw_config nw_config;
typedef struct nw_config nw_config;
-int nw_config_create(nw_config** config, const char* path);
+int nw_config_create(nw_config** config, FILE* f);
+int nw_config_open(nw_config** config, const char* path);
nw_config* nw_config_ref(nw_config* config);
nw_config* nw_config_unref(nw_config* config);
nw_config* nw_config_ref(nw_config* config);
nw_config* nw_config_unref(nw_config* config);
-int nw_config_destroy(nw_config* config);
int nw_config_copy(nw_config* config, nw_config** copy);
int nw_config_copy(nw_config* config, nw_config** copy);
-const char* nw_config_path(nw_config* config);
-
int nw_config_flush(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);
+int nw_config_read(nw_config* config, FILE* f);
+int nw_config_write(nw_config* config, FILE* f);
int nw_config_del(nw_config* config, const char* key);
int nw_config_del(nw_config* config, const char* key);
-static FILE* nw_daemon_config_fopen(nw_daemon* daemon, const char* path, const char* mode) {
+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) {
// Open the file
int fd = openat(daemon->configfd, path, 0);
if (fd < 0) {
return fdopen(fd, mode);
}
return fdopen(fd, mode);
}
+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;
+ }
+
+ return fdopendir(fd);
+}
+
static int nw_daemon_parse_argv(nw_daemon* daemon, int argc, char* argv[]) {
enum {
ARG_CONFIG,
static int nw_daemon_parse_argv(nw_daemon* daemon, int argc, char* argv[]) {
enum {
ARG_CONFIG,
}
// Create configuration
}
// Create configuration
- r = nw_config_create(&daemon->config, NULL);
- if (r < 0)
- goto ERROR;
-
- // Parse configuration
- r = nw_config_readf(daemon->config, f);
+ r = nw_config_create(&daemon->config, f);
DEBUG("Saving configuration...\n");
DEBUG("Saving configuration...\n");
- r = nw_config_write(daemon->config);
+ r = nw_config_write(daemon->config, f);
// Save ports
r = nw_ports_save(daemon->ports);
// Save ports
r = nw_ports_save(daemon->ports);
#ifndef NETWORKD_DAEMON_H
#define NETWORKD_DAEMON_H
#ifndef NETWORKD_DAEMON_H
#define NETWORKD_DAEMON_H
+#include <dirent.h>
+#include <stdio.h>
+
#include <systemd/sd-bus.h>
#include <systemd/sd-netlink.h>
#include <systemd/sd-bus.h>
#include <systemd/sd-netlink.h>
int nw_daemon_save(nw_daemon* daemon);
int nw_daemon_save(nw_daemon* daemon);
+/*
+ Configuration
+*/
+FILE* nw_daemon_config_fopen(nw_daemon* daemon, const char* path, const char* mode);
+DIR* nw_daemon_config_opendir(nw_daemon* daemon, const char* path);
+
// Allocate a new object
nw_port* p = calloc(1, sizeof(*p));
if (!p)
// Allocate a new object
nw_port* p = calloc(1, sizeof(*p));
if (!p)
// Store a reference to the daemon
p->daemon = nw_daemon_ref(daemon);
// Store a reference to the daemon
p->daemon = nw_daemon_ref(daemon);
// Store the name
r = nw_string_set(p->name, name);
// Store the name
r = nw_string_set(p->name, name);
goto ERROR;
// Copy the configuration
r = nw_config_copy(config, &p->config);
goto ERROR;
// Copy the configuration
r = nw_config_copy(config, &p->config);
goto ERROR;
// Setup the port
r = nw_port_setup(p);
goto ERROR;
// Setup the port
r = nw_port_setup(p);
goto ERROR;
// Validate the configuration
goto ERROR;
// Validate the configuration
-int nw_port_create_from_config(nw_port** port, nw_daemon* daemon,
- const char* name, const char* path) {
+int nw_port_open(nw_port** port, nw_daemon* daemon, const char* name) {
nw_config* config = NULL;
nw_config* config = NULL;
+ FILE* f = NULL;
+ char path[PATH_MAX];
+ // Make path
+ r = nw_string_format(path, "ports/%s", name);
+ if (r < 0)
+ goto ERROR;
+
+ // Open the configuration file
+ f = nw_daemon_config_fopen(daemon, path, "r");
+ if (!f) {
+ r = -errno;
+ goto ERROR;
+ }
+
// Initialize the configuration
// Initialize the configuration
- r = nw_config_create(&config, path);
- if (r)
+ r = nw_config_create(&config, f);
+ if (r < 0)
goto ERROR;
// Fetch the type
const char* type = nw_config_get(config, "TYPE");
if (!type) {
ERROR("Port configuration %s has no TYPE\n", path);
goto ERROR;
// Fetch the type
const char* type = nw_config_get(config, "TYPE");
if (!type) {
ERROR("Port configuration %s has no TYPE\n", path);
goto ERROR;
}
// Create a new port
r = nw_port_create(port, daemon, nw_port_type_id_from_string(type), name, config);
goto ERROR;
}
// Create a new port
r = nw_port_create(port, daemon, nw_port_type_id_from_string(type), name, config);
goto ERROR;
ERROR:
if (config)
nw_config_unref(config);
goto ERROR;
ERROR:
if (config)
nw_config_unref(config);
}
int nw_port_save(nw_port* port) {
}
int nw_port_save(nw_port* port) {
+ char path[PATH_MAX];
+ FILE* f = NULL;
+ // Compose path
+ r = nw_string_format(path, "ports/%s", port->name);
+ if (r < 0)
+ return r;
+
+ // Open file
+ f = nw_daemon_config_fopen(port->daemon, path, "w");
+ if (!f) {
+ r = -errno;
+ goto ERROR;
+ }
+
// Write out the configuration
r = nw_config_options_write(port->config);
if (r < 0)
goto ERROR;
// Write the configuration
// Write out the configuration
r = nw_config_options_write(port->config);
if (r < 0)
goto ERROR;
// Write the configuration
- r = nw_config_write(port->config);
- if (r)
- return r;
-
- return 0;
+ r = nw_config_write(port->config, f);
+ if (r < 0)
+ goto ERROR;
- ERROR("Could not save configuration for port %s: %s\n", port->name, strerror(-r));
+ if (f)
+ fclose(f);
+ if (r)
+ ERROR("Could not save configuration for port %s: %s\n", port->name, strerror(-r));
}
const char* nw_port_name(nw_port* port) {
}
const char* nw_port_name(nw_port* port) {
int nw_port_create(nw_port** port, nw_daemon* daemon,
const nw_port_type_id_t type, const char* name, nw_config* config);
int nw_port_create(nw_port** port, nw_daemon* daemon,
const nw_port_type_id_t type, const char* name, nw_config* config);
-int nw_port_create_from_config(nw_port** port, nw_daemon* daemon,
- const char* name, const char* path);
+int nw_port_open(nw_port** port, nw_daemon* daemon, const char* name);
nw_port* nw_port_ref(nw_port* port);
nw_port* nw_port_unref(nw_port* port);
nw_port* nw_port_ref(nw_port* port);
nw_port* nw_port_unref(nw_port* port);
# #
#############################################################################*/
# #
#############################################################################*/
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>
-static int __nw_ports_enumerate(const char* path, const struct stat* s, void* data) {
+static int nw_ports_enumerate_port(nw_ports* ports, const char* name) {
nw_port* port = NULL;
int r;
nw_port* port = NULL;
int r;
- nw_ports* ports = (nw_ports*)data;
-
- // Skip anything that isn't a regular file
- if (!S_ISREG(s->st_mode))
- return 0;
-
- // Find the basename of the file
- const char* name = nw_path_basename(path);
-
- // Break on invalid paths
- if (!name)
- return 0;
-
- // Skip any hidden files
- if (*name == '.')
- return 0;
-
- r = nw_port_create_from_config(&port, ports->daemon, name, path);
- switch (r) {
- // All okay
- case 0:
- break;
-
- // Invalid configuration
- case 1:
- ERROR("Could not open port %s\n", name);
- r = 0;
- goto ERROR;
-
- default:
- goto ERROR;
- }
+ r = nw_port_open(&port, ports->daemon, name);
+ if (r < 0 || r == 1)
+ goto ERROR;
// Add the port to the list
r = nw_ports_add_port(ports, port);
// Add the port to the list
r = nw_ports_add_port(ports, port);
}
int nw_ports_enumerate(nw_ports* ports) {
}
int nw_ports_enumerate(nw_ports* ports) {
- return nw_ftw(PORT_CONFIG_DIR, PORT_CONFIG_DIR "/*", __nw_ports_enumerate, ports);
+ DIR* d = NULL;
+ struct dirent* entry = NULL;
+ int r;
+
+ // Open the ports directory
+ d = nw_daemon_config_opendir(ports->daemon, "ports");
+ if (!d) {
+ switch (errno) {
+ case ENOENT:
+ return 0;
+
+ default:
+ return -errno;
+ }
+ }
+
+ for (;;) {
+ // Read the next entry
+ entry = readdir(d);
+ if (!entry)
+ break;
+
+ // Skip anything that is not a regular file
+ if (entry->d_type != DT_REG)
+ continue;
+
+ // Skip hidden files
+ if (entry->d_name[0] == '.')
+ continue;
+
+ // Enumerate the port
+ r = nw_ports_enumerate_port(ports, entry->d_name);
+ if (r < 0)
+ goto ERROR;
+ }
+
+ERROR:
+ if (d)
+ closedir(d);
+
+ return r;
}
nw_port* nw_ports_get_by_name(nw_ports* ports, const char* name) {
}
nw_port* nw_ports_get_by_name(nw_ports* ports, const char* name) {
// Compose the path to the main configuration file
r = nw_zone_path(zone, path, "%s", "settings");
if (r)
// Compose the path to the main configuration file
r = nw_zone_path(zone, path, "%s", "settings");
if (r)
r = nw_config_create(&zone->config, path);
if (r)
goto ERROR;
r = nw_config_create(&zone->config, path);
if (r)
goto ERROR;
}
int nw_zone_save(nw_zone* zone) {
}
int nw_zone_save(nw_zone* zone) {
+ char path[PATH_MAX];
+ FILE* f = NULL;
- r = nw_config_write(zone->config);
+ // Compose path
+ r = nw_string_format(path, "zones/%s/settings", zone->name);
+ if (r < 0)
+ goto ERROR;
+
+ // Open file
+ f = nw_daemon_config_fopen(zone->daemon, path, "w");
+ if (!f) {
+ r = -errno;
+ goto ERROR;
+ }
+
+ // Write the configuration
+ r = nw_config_write(zone->config, f);
+ERROR:
+ if (f)
+ fclose(f);
+
+ return r;
}
const char* nw_zone_name(nw_zone* zone) {
}
const char* nw_zone_name(nw_zone* zone) {