From 00a2ea29131df702c5bcaf63c8512d1b63a0d544 Mon Sep 17 00:00:00 2001 From: Jeremy Lakeman Date: Mon, 24 Aug 2015 16:38:55 +0930 Subject: [PATCH] Ignore potential race condition between setting up instance path and first log message --- instance.c | 14 ++++++++------ os.c | 13 +++++++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/instance.c b/instance.c index 071f0ec0..38e27727 100644 --- a/instance.c +++ b/instance.c @@ -204,12 +204,8 @@ int create_serval_instance_dir() { int ret = 0; char path[PATH_MAX]; - if (FORMF_SERVAL_ETC_PATH(path, NULL) && emkdirs_info(path, 0755) == -1) - ret = -1; - if (FORMF_SERVAL_RUN_PATH(path, NULL) && emkdirs_info(path, 0700) == -1) - ret = -1; - if (FORMF_SERVAL_CACHE_PATH(path, NULL) && emkdirs_info(path, 0700) == -1) - ret = -1; + // emkdire_info can log if paths don't exist, which will also try to create paths... + // so try to create logging folders first strbuf sb = strbuf_local(path, sizeof path); strbuf_system_log_path(sb); if (!strbuf_overrun(sb) && emkdirs_info(path, 0700) == -1) @@ -218,6 +214,12 @@ int create_serval_instance_dir() strbuf_serval_log_path(sb); if (!strbuf_overrun(sb) && emkdirs_info(path, 0700) == -1) ret = -1; + if (FORMF_SERVAL_ETC_PATH(path, NULL) && emkdirs_info(path, 0755) == -1) + ret = -1; + if (FORMF_SERVAL_RUN_PATH(path, NULL) && emkdirs_info(path, 0700) == -1) + ret = -1; + if (FORMF_SERVAL_CACHE_PATH(path, NULL) && emkdirs_info(path, 0700) == -1) + ret = -1; if (FORMF_SERVAL_TMP_PATH(path, NULL) && emkdirs_info(path, 0700) == -1) ret = -1; if (FORMF_SERVALD_PROC_PATH(path, NULL) && emkdirs_info(path, 0755) == -1) diff --git a/os.c b/os.c index 88ecef5f..184352bb 100644 --- a/os.c +++ b/os.c @@ -100,11 +100,16 @@ int _mkdirsn(struct __sourceloc whence, const char *path, size_t len, mode_t mod if (lastsep != path) { if (_mkdirsn(whence, path, lastsep - path + 1, mode, logger) == -1) return -1; - if (mkdir(pathfrag, mode) != -1) { - if (logger) - logger(whence, pathfrag, mode); - return 0; + + if (mkdir(pathfrag, mode) == -1) { + if (errno==EEXIST) + return 0; + return -1; } + + if (logger) + logger(whence, pathfrag, mode); + return 0; } } }