summaryrefslogtreecommitdiff
path: root/src/basic/fd-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2023-03-21 14:34:44 +0100
committerDaan De Meyer <daan.j.demeyer@gmail.com>2023-03-21 20:51:55 +0100
commit42db4a8db704592866ccc96c4a068b1537b1dde8 (patch)
tree0785d8863cdb64a8343ad7343111f6fbfb2f8cf8 /src/basic/fd-util.c
parent1c2e550ec43226c2904df3304e3fe362e5b38671 (diff)
downloadsystemd-42db4a8db704592866ccc96c4a068b1537b1dde8.tar.gz
fd-util: make sure fd_reopen() works with AT_FDCWD systematically
Prompted by: https://github.com/systemd/systemd/pull/26827#pullrequestreview-1341171981
Diffstat (limited to 'src/basic/fd-util.c')
-rw-r--r--src/basic/fd-util.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c
index d729643265..5eb43e1f81 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)