From ee79c9d3091f263c3509b99b9f707468bf17806e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 8 Oct 2025 09:39:17 +0200 Subject: [PATCH] fd-util: introduce XAT_FDROOT fd shortcut for the root dir This takes inspiration from AT_FDCWD, but always references the root dir rather than the cwd. This allows various convenience uses, as we can now reference the root dir without actually allocating an fd for chaseat(), fd_get_path() and various others. The value of XAT_FDROOT is chosen so that it is not a valid fd (essential, because we do not intend to hook this up everywhere, just where we need it, just like AT_FCWD only works in some syscalls), and we want uses at the wrong places to fail cleanly. It also uses a valud outside of the range where we usually return negative errnos, i.e. < -ERRNO_MAX. --- src/basic/fd-util.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index bb9669a84e..2663ff8aac 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -57,6 +57,13 @@ #define NR_OPEN_MINIMUM ((unsigned) (sizeof(long) * 8)) #define NR_OPEN_MAXIMUM ((unsigned) (CONST_MIN((size_t) INT_MAX, SIZE_MAX / __SIZEOF_POINTER__) & ~(sizeof(long) * 8 - 1))) +/* A special fd that can be passed in various helpers instead of an fd indicating the root dir. Inspired by, + * and an alternative to AT_FDCWD. We use specific negative value that is outside of the negative errno + * range, to avoid any potential ambiguities. */ +#define XAT_FDROOT -8192 +assert_cc(XAT_FDROOT != AT_FDCWD); +assert_cc(XAT_FDROOT < -ERRNO_MAX); + int close_nointr(int fd); int safe_close(int fd); void safe_close_pair(int p[static 2]);