From: Hans Dedecker Date: Thu, 29 Dec 2016 11:33:39 +0000 (+0100) Subject: config: Create statefile dir X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=0572d1abbadf7b955f2788d04ca3d00f45963e6a;p=project%2Fodhcpd.git config: Create statefile dir Create statefile dir if the specified dir does not yet exist Signed-off-by: Hans Dedecker --- 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();