From: Lennart Poettering Date: Tue, 21 Mar 2023 13:34:44 +0000 (+0100) Subject: fd-util: make sure fd_reopen() works with AT_FDCWD systematically X-Git-Tag: v254-rc1~963 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=42db4a8db704592866ccc96c4a068b1537b1dde8;p=thirdparty%2Fsystemd.git fd-util: make sure fd_reopen() works with AT_FDCWD systematically Prompted by: https://github.com/systemd/systemd/pull/26827#pullrequestreview-1341171981 --- diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index d7296432650..5eb43e1f816 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -744,23 +744,29 @@ finish: int fd_reopen(int fd, int flags) { int new_fd, r; + assert(fd >= 0 || fd == AT_FDCWD); + /* Reopens the specified fd with new flags. This is useful for convert an O_PATH fd into a regular one, or to * turn O_RDWR fds into O_RDONLY fds. * * This doesn't work on sockets (since they cannot be open()ed, ever). * - * This implicitly resets the file read index to 0. */ + * This implicitly resets the file read index to 0. + * + * If AT_FDCWD is specified as file descriptor gets an fd to the current cwd */ - if (FLAGS_SET(flags, O_DIRECTORY)) { + if (FLAGS_SET(flags, O_DIRECTORY) || fd == AT_FDCWD) { /* 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); + new_fd = openat(fd, ".", flags | O_DIRECTORY); if (new_fd < 0) return -errno; return new_fd; } + assert(fd >= 0); + new_fd = open(FORMAT_PROC_FD_PATH(fd), flags); if (new_fd < 0) { if (errno != ENOENT)