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.
This commit is contained in:
tomty89 2021-07-05 23:21:21 +08:00 committed by GitHub
parent abbf6a85d7
commit fc65ca36b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,16 +27,15 @@ 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);
/* 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("mkdir()");
perror("mkdirp()");
return NULL;
}
}
} else {
/* If not, we create a (secure) temp directory using the template
* /tmp/i3-<user>.XXXXXX */