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.
This commit is contained in:
Lennart Poettering
2026-01-17 12:35:33 +01:00
parent 947ecb0812
commit ee79c9d309
+7
View File
@@ -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]);