]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
stat-util: fix dir_is_empty_at without path
authorLuca Boccassi <luca.boccassi@microsoft.com>
Mon, 18 Jan 2021 20:15:03 +0000 (20:15 +0000)
committerLennart Poettering <lennart@poettering.net>
Tue, 16 Feb 2021 19:24:27 +0000 (20:24 +0100)
Use the right FD, and do a fd_reopen instead of a dup, since the
latter will still share the internal pointer which then gets
moved by FOREACH_DIRENT, affecting the caller's FD.

src/basic/stat-util.c

index f999681636da506c6183af589787fdd257ad779c..72a7e4a48bfb9c52cb26d46bf53035b0ec313e73 100644 (file)
@@ -72,12 +72,17 @@ int dir_is_empty_at(int dir_fd, const char *path) {
         _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
 
-        if (path)
+        if (path) {
                 fd = openat(dir_fd, path, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
-        else
-                fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
-        if (fd < 0)
-                return -errno;
+                if (fd < 0)
+                        return -errno;
+        } else {
+                /* Note that DUPing is not enough, as the internal pointer
+                 * would still be shared and moved by FOREACH_DIRENT. */
+                fd = fd_reopen(dir_fd, O_CLOEXEC);
+                if (fd < 0)
+                        return fd;
+        }
 
         d = take_fdopendir(&fd);
         if (!d)