From 2358f4e70797e22265ff5f60b7e3d66767643f7b Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Wed, 30 Nov 2011 09:52:08 -0800 Subject: [PATCH] mkdir - Fix MP race in mkdir -p * mkdir -p can race other mkdir -p commands * Do the same fix that we did for xinstall... if the mkdir() fails after the stat() check, stat() again to see if the directory creation raced. --- bin/mkdir/mkdir.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/bin/mkdir/mkdir.c b/bin/mkdir/mkdir.c index 0584d4dc52..c3f2763da9 100644 --- a/bin/mkdir/mkdir.c +++ b/bin/mkdir/mkdir.c @@ -49,6 +49,7 @@ #include static int build(char *, mode_t); +static int mkdir_race(const char *path, int nmode); static void usage(void); int vflag; @@ -172,7 +173,7 @@ build(char *path, mode_t omode) umask(oumask); if (stat(path, &sb)) { if (errno != ENOENT || - mkdir(path, last ? omode : + mkdir_race(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) { warn("%s", path); retval = 1; @@ -197,6 +198,21 @@ build(char *path, mode_t omode) return (retval); } +int +mkdir_race(const char *path, int nmode) +{ + int res; + struct stat sb; + + res = mkdir(path, nmode); + if (res < 0 && errno == EEXIST) { + if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) + return(0); + res = mkdir(path, nmode); + } + return (res); +} + static void usage(void) { -- 2.41.0