* [PATCH] tools/nolibc: Add dirfd()
@ 2026-08-28 11:22 Daniel Palmer
2026-08-28 12:15 ` Thomas Weißschuh
0 siblings, 1 reply; 2+ messages in thread
From: Daniel Palmer @ 2026-08-28 11:22 UTC (permalink / raw)
To: w, linux; +Cc: linux-kernel, Daniel Palmer
This is useful for things like fstatat() on children of directory
opened with opendir().
At the same time use it to replace the two places that are converting
the DIR point back into an fd.
Signed-off-by: Daniel Palmer <daniel@thingy.jp>
---
tools/include/nolibc/dirent.h | 38 +++++++++++++++++++++++++++--------
1 file changed, 30 insertions(+), 8 deletions(-)
diff --git a/tools/include/nolibc/dirent.h b/tools/include/nolibc/dirent.h
index 4e02ef25e72d..ba229b215a0b 100644
--- a/tools/include/nolibc/dirent.h
+++ b/tools/include/nolibc/dirent.h
@@ -27,6 +27,18 @@ typedef struct {
char dummy[1];
} DIR;
+/* Internal dirfd() that does not set errno */
+static __attribute__((unused))
+int nolibc_dirfd(DIR *dirp)
+{
+ intptr_t i = (intptr_t)dirp;
+
+ if (i >= 0)
+ return -1;
+
+ return ~i;
+}
+
static __attribute__((unused))
DIR *fdopendir(int fd)
{
@@ -49,15 +61,27 @@ DIR *opendir(const char *name)
}
static __attribute__((unused))
-int closedir(DIR *dirp)
+int dirfd(DIR *dirp)
{
- intptr_t i = (intptr_t)dirp;
+ int fd = nolibc_dirfd(dirp);
- if (i >= 0) {
+ if (fd < 0) {
SET_ERRNO(EBADF);
return -1;
}
- return close(~i);
+
+ return fd;
+}
+
+static __attribute__((unused))
+int closedir(DIR *dirp)
+{
+ int fd = dirfd(dirp);
+
+ if (fd < 0)
+ return -1;
+
+ return close(fd);
}
static __attribute__((unused))
@@ -65,14 +89,12 @@ int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
{
char buf[sizeof(struct linux_dirent64) + NAME_MAX + 1] __nolibc_aligned_as(struct linux_dirent64);
struct linux_dirent64 *ldir = (void *)buf;
- intptr_t i = (intptr_t)dirp;
int fd, ret;
- if (i >= 0)
+ fd = nolibc_dirfd(dirp);
+ if (fd < 0)
return EBADF;
- fd = ~i;
-
ret = _sys_getdents64(fd, ldir, sizeof(buf));
if (ret < 0)
return -ret;
--
2.53.0
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] tools/nolibc: Add dirfd()
2026-08-28 11:22 [PATCH] tools/nolibc: Add dirfd() Daniel Palmer
@ 2026-08-28 12:15 ` Thomas Weißschuh
0 siblings, 0 replies; 2+ messages in thread
From: Thomas Weißschuh @ 2026-08-28 12:15 UTC (permalink / raw)
To: Daniel Palmer; +Cc: w, linux-kernel
On 2026-08-28 20:22:04+0900, Daniel Palmer wrote:
> This is useful for things like fstatat() on children of directory
> opened with opendir().
>
> At the same time use it to replace the two places that are converting
> the DIR point back into an fd.
>
> Signed-off-by: Daniel Palmer <daniel@thingy.jp>
> ---
> tools/include/nolibc/dirent.h | 38 +++++++++++++++++++++++++++--------
> 1 file changed, 30 insertions(+), 8 deletions(-)
>
> diff --git a/tools/include/nolibc/dirent.h b/tools/include/nolibc/dirent.h
> index 4e02ef25e72d..ba229b215a0b 100644
> --- a/tools/include/nolibc/dirent.h
> +++ b/tools/include/nolibc/dirent.h
> @@ -27,6 +27,18 @@ typedef struct {
> char dummy[1];
> } DIR;
>
> +/* Internal dirfd() that does not set errno */
> +static __attribute__((unused))
> +int nolibc_dirfd(DIR *dirp)
This pollutes the applications symbol namespace.
It should begin with underscores.
But IMO we could also just keep it inlined in the two users.
> +{
> + intptr_t i = (intptr_t)dirp;
> +
> + if (i >= 0)
> + return -1;
> +
> + return ~i;
> +}
Could you also add a test for an fdopendir() -> dirfd() roundtrip?
Thomas
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-08-28 12:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-28 11:22 [PATCH] tools/nolibc: Add dirfd() Daniel Palmer
2026-08-28 12:15 ` Thomas Weißschuh
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®