]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
priv: handle EROFS when creating chroot
authorVincent Bernat <vincent@bernat.ch>
Mon, 13 Sep 2021 06:18:28 +0000 (08:18 +0200)
committerVincent Bernat <vincent@bernat.ch>
Mon, 13 Sep 2021 06:18:28 +0000 (08:18 +0200)
src/daemon/priv.c

index b8ebd39adfeb5cd41943d388c5d83a003454578f..026e32c39aa7c921effec8580b9043a9c2914470 100644 (file)
@@ -518,6 +518,27 @@ sig_chld(int sig)
        priv_exit_rc_status(rc, status);
 }
 
+/* Create a subdirectory and check if it's here. */
+static int _mkdir(const char *pathname, mode_t mode)
+{
+       int save_errno;
+       if (mkdir(pathname, mode) == 0)
+               return 0;
+       if (errno == EEXIST)
+               return -1;
+
+       /* We can get EROFS on some platforms. Let's check if the directory exists. */
+       save_errno = errno;
+       if (chdir(pathname) == -1) {
+               errno = save_errno;
+               return -1;
+       }
+
+       /* We should restore current directory, but in the context we are
+        * running, we do not care. */
+       return 0;
+}
+
 /* Create a directory recursively. */
 static int mkdir_p(const char *pathname, mode_t mode)
 {
@@ -533,14 +554,13 @@ static int mkdir_p(const char *pathname, mode_t mode)
        for (current = path + 1; *current; current++) {
                if (*current != '/') continue;
                *current = '\0';
-               if (mkdir(path, mode) != 0 && errno != EEXIST)
+               if (_mkdir(path, mode) != 0)
                        return -1;
                *current = '/';
        }
-       if (mkdir(path, mode) != 0 && errno != EEXIST)
+       if (_mkdir(path, mode) != 0)
                return -1;
 
-       errno = 0;
        return 0;
 }