From b4f73d1e1774b4512d0e396b7060f2a2ff5f0fe3 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 21 Oct 2021 10:21:03 +0200 Subject: [PATCH] =?utf8?q?fd-util:=20when=20re-opening=20a=20directory=20w?= =?utf8?q?ith=20fd=5Freopen()=20go=20via=20openat(=E2=80=A6,=20".",=20?= =?utf8?q?=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index d7cf85e0ee8..cf6742c2b31 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -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) -- 2.47.3