From 0572d1abbadf7b955f2788d04ca3d00f45963e6a Mon Sep 17 00:00:00 2001 From: Hans Dedecker Date: Thu, 29 Dec 2016 12:33:39 +0100 Subject: [PATCH] config: Create statefile dir Create statefile dir if the specified dir does not yet exist Signed-off-by: Hans Dedecker --- src/config.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/config.c b/src/config.c index a3cb2be..c0c1215 100644 --- a/src/config.c +++ b/src/config.c @@ -3,6 +3,9 @@ #include #include #include +#include +#include +#include #include #include @@ -140,6 +143,30 @@ const struct uci_blob_param_list odhcpd_attr_list = { .params = odhcpd_attrs, }; +static int mkdir_p(char *dir, mode_t mask) +{ + char *l = strrchr(dir, '/'); + int ret; + + if (!l) + return 0; + + *l = '\0'; + + if (mkdir_p(dir, mask)) + return -1; + + *l = '/'; + + ret = mkdir(dir, mask); + if (ret && errno == EEXIST) + return 0; + + if (ret) + syslog(LOG_ERR, "mkdir(%s, %d) failed: %s\n", dir, mask, strerror(errno)); + + return ret; +} static struct interface* get_interface(const char *name) { @@ -626,6 +653,12 @@ void odhcpd_reload(void) } } + if (config.dhcp_statefile) { + char *path = strdup(config.dhcp_statefile); + + mkdir_p(dirname(path), 0755); + free(path); + } #ifdef WITH_UBUS ubus_apply_network(); -- 2.25.1