Skip to content

Commit

Permalink
Fixed race condition in make_dir()
Browse files Browse the repository at this point in the history
Inspired by pull request ioi#7 by @bblackham and patch by @austrin.
  • Loading branch information
gollux committed Jan 24, 2016
1 parent 4693333 commit ce9dad0
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions isolate.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,21 @@ static void make_dir(char *path)
if (sep)
*sep = 0;

if (!dir_exists(path) && mkdir(path, 0777) < 0)
die("Cannot create directory %s: %m\n", path);
if (mkdir(path, 0777) < 0 && errno != EEXIST)
die("Cannot create directory %s: %m", path);

if (!sep)
return;
break;
*sep++ = '/';
}

// mkdir() above may have returned EEXIST even if the path was not
// a directory. Ensure that it is.
struct stat st;
if (stat(path, &st) < 0)
die("Cannot stat %s: %m", path);
if (!S_ISDIR(st.st_mode))
die("Cannot create %s: already exists, but not a directory", path);
}

static void apply_dir_rules(void)
Expand Down

0 comments on commit ce9dad0

Please sign in to comment.