summaryrefslogtreecommitdiff
path: root/src/basic/stat-util.c
diff options
context:
space:
mode:
authorLuca Boccassi <luca.boccassi@microsoft.com>2021-01-18 20:15:03 +0000
committerLennart Poettering <lennart@poettering.net>2021-02-16 20:24:27 +0100
commitb8f762f2fe59c94323c95d2aadea68612dca2b04 (patch)
treeb6e08549cda83946ea58eb1a3b4b3566ca081d7e /src/basic/stat-util.c
parent42b23010b9e74a2a59cc1f8533ef45b69d4fba47 (diff)
downloadsystemd-b8f762f2fe59c94323c95d2aadea68612dca2b04.tar.gz
stat-util: fix dir_is_empty_at without path
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.
Diffstat (limited to 'src/basic/stat-util.c')
-rw-r--r--src/basic/stat-util.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/basic/stat-util.c b/src/basic/stat-util.c
index f999681636..72a7e4a48b 100644
--- a/src/basic/stat-util.c
+++ b/src/basic/stat-util.c
@@ -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)