]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fd-util: when re-opening a directory with fd_reopen() go via openat(…, ".", …)
authorLennart Poettering <lennart@poettering.net>
Thu, 21 Oct 2021 08:21:03 +0000 (10:21 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 22 Oct 2021 12:05:12 +0000 (14:05 +0200)
This adds a tiny shortcut to fd_reopen(): if we are about to reopen the
fd via O_DIRECTORY then we know it#s a directory and we might as well
reopen it via opening "." using the fd as "at fd" in openat().

This has the benefit that we don't need /proc/self/fd/ around for this
special case: fewer sources of errors.

src/basic/fd-util.c

index d7cf85e0ee8e4826ae46b0c0be6fb520ce3ecaf6..cf6742c2b319503b3be825c7af94e503f5a95b3e 100644 (file)
@@ -654,6 +654,16 @@ int fd_reopen(int fd, int flags) {
          *
          * This implicitly resets the file read index to 0. */
 
+        if (FLAGS_SET(flags, O_DIRECTORY)) {
+                /* If we shall reopen the fd as directory we can just go via "." and thus bypass the whole
+                 * magic /proc/ directory, and make ourselves independent of that being mounted. */
+                new_fd = openat(fd, ".", flags);
+                if (new_fd < 0)
+                        return -errno;
+
+                return new_fd;
+        }
+
         new_fd = open(FORMAT_PROC_FD_PATH(fd), flags);
         if (new_fd < 0) {
                 if (errno != ENOENT)