From fc65ca36b1f89f122b7ef0fe32c3bea1f46cfd27 Mon Sep 17 00:00:00 2001 From: tomty89 Date: Mon, 5 Jul 2021 23:21:21 +0800 Subject: [PATCH] Use mkdirp() in get_process_filename() (#4397) Avoids race condition in case multiple i3 instances are started in parallel with e.g. systemd user units for multiple X(vfb) servers. --- libi3/get_process_filename.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/libi3/get_process_filename.c b/libi3/get_process_filename.c index 21429ec3..d29f8db1 100644 --- a/libi3/get_process_filename.c +++ b/libi3/get_process_filename.c @@ -27,15 +27,14 @@ char *get_process_filename(const char *prefix) { char *tmp; sasprintf(&tmp, "%s/i3", dir); dir = tmp; - struct stat buf; - if (stat(dir, &buf) != 0) { - if (mkdir(dir, 0700) == -1) { - warn("Could not mkdir(%s)", dir); - errx(EXIT_FAILURE, "Check permissions of $XDG_RUNTIME_DIR = '%s'", - getenv("XDG_RUNTIME_DIR")); - perror("mkdir()"); - return NULL; - } + /* mkdirp() should prevent race between multiple i3 instances started + * in parallel from causing problem */ + if (mkdirp(dir, 0700) == -1) { + warn("Could not mkdirp(%s)", dir); + errx(EXIT_FAILURE, "Check permissions of $XDG_RUNTIME_DIR = '%s'", + getenv("XDG_RUNTIME_DIR")); + perror("mkdirp()"); + return NULL; } } else { /* If not, we create a (secure) temp directory using the template