From: Sebastian Harl Date: Sun, 29 Jun 2014 18:43:58 +0000 (+0200) Subject: frontend: Ensure that the base directory of a UNIX socket exists. X-Git-Tag: sysdb-0.2.0~15 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=b7e29806ac6a680aa34d497fee4031f7705c5bdc;p=sysdb.git frontend: Ensure that the base directory of a UNIX socket exists. --- diff --git a/src/frontend/sock.c b/src/frontend/sock.c index 2e5a6af..845274e 100644 --- a/src/frontend/sock.c +++ b/src/frontend/sock.c @@ -37,6 +37,7 @@ #include "utils/channel.h" #include "utils/error.h" #include "utils/llist.h" +#include "utils/os.h" #include "utils/strbuf.h" #include @@ -54,6 +55,8 @@ #include #include +#include + #include /* @@ -94,6 +97,7 @@ static int open_unix_sock(listener_t *listener) { const char *addr; + char *base_dir; struct sockaddr_un sa; int status; @@ -114,6 +118,25 @@ open_unix_sock(listener_t *listener) sa.sun_family = AF_UNIX; strncpy(sa.sun_path, addr, sizeof(sa.sun_path)); + base_dir = strdup(addr); + if (! base_dir) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: strdup failed: %s", + sdb_strerror(errno, errbuf, sizeof(errbuf))); + return -1; + } + base_dir = dirname(base_dir); + + /* ensure that the directory exists */ + if (sdb_mkdir_all(base_dir, 0777)) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: Failed to create directory '%s': %s", + base_dir, sdb_strerror(errno, errbuf, sizeof(errbuf))); + free(base_dir); + return -1; + } + free(base_dir); + if (unlink(addr) && (errno != ENOENT)) { char errbuf[1024]; sdb_log(SDB_LOG_WARNING, "frontend: Failed to remove stale UNIX "