nolibc: Add dirfd()
Subsystems (2)
v2 cover nolibc: Add dirfd()
Add dirfd() to convert a DIR back to an fd for functions like fstatat() that take an fd.
Change log:
v2 - Fix the name of the dirfd helper, I debated over removing the helper
as suggested by Thomas but it seems right to have the logic in a single place.
Add test a basic test to make sure dirfd() actually does convert back to the expected fd and using dirfd() with fstatat().
Daniel Palmer (2):
tools/nolibc: Add dirfd()
selftests/nolibc: Add test for dirfd()
tools/include/nolibc/dirent.h | 38 ++++++++++++---- tools/testing/selftests/nolibc/nolibc-test.c | 47 ++++++++++++++++++++ 2 files changed, 77 insertions(+), 8 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/2 tools/nolibc: Add dirfd()
This is useful for things like fstatat() on children of a directory opened with opendir().
At the same time use the new internal helper to replace the two places that are converting the DIR pointer back into an fd.
--- tools/include/nolibc/dirent.h | 38 +++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-)
tools/include/nolibc/dirent.h +30 -8
@@ -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;
Signature · 1 line
2.53.0
Thomas Weißschuh
maintainerRe: [PATCH v2 1/2] tools/nolibc: Add dirfd()
Quoted 20 lines · Daniel Palmer
This is useful for things like
fstatat()on children of a directory opened withopendir().At the same time use the new internal helper to replace the two places that are converting the DIR pointer back into an fd.
Signed-off-by Daniel Palmer <[email protected]>--- tools/include/nolibc/dirent.h | 38 +++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-)tools/include/nolibc/dirent.h +1
-0@@ -27,6 +27,18 @@ typedef struct {char dummy[1 ]; }DIR ; +/* Internal dirfd() that does not set errno */
tools/include/nolibc/dirent.h typedef struct { · line 40
Quoted 10 lines
tools/include/nolibc/dirent.h +10
-0+static __attribute__((unused)) +int __nolibc_dirfd (DIR *dirp) +{ +intptr_t i = (intptr_t )dirp; + +if (i >=0 ) +return -1 ; + +return ~i; +}
I'm fine with keeping __nolibc_dirfd(), but the diffs would be nicer if it was introduced in a dedicated commit.
Quoted 23 lines
tools/include/nolibc/dirent.h +7
-4+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; +}
(...)
Re: [PATCH v2 1/2] tools/nolibc: Add dirfd()
In reply to Daniel PalmerQuoted 19 lines · On Wed, 7 Oct 2026 14:29:12 +0900 Daniel Palmer <[email protected]> wrote:
This is useful for things like
fstatat()on children of a directory opened withopendir().At the same time use the new internal helper to replace the two places that are converting the DIR pointer back into an fd.
Signed-off-by Daniel Palmer <[email protected]>--- tools/include/nolibc/dirent.h | 38 +++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-)tools/include/nolibc/dirent.h +0
-0@@ -27,6 +27,18 @@ typedef struct {char dummy[1 ]; }DIR ;
tools/include/nolibc/dirent.h typedef struct { · line 39
Quoted 10 lines
tools/include/nolibc/dirent.h +10
-0+/* 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;
Might be better to use a different name for the variable. The two return statements look the same at a first glance.
David
Re: [PATCH v2 1/2] tools/nolibc: Add dirfd()
Hi David,
Quoted 5 lines · David Laight
Quoted 5 lines
tools/include/nolibc/dirent.h +5
-0+/* Internal dirfd() that does not set errno */ +static __attribute__((unused)) +int __nolibc_dirfd (DIR *dirp) +{ +intptr_t i = (intptr_t )dirp;
Quoted 8 lines
tools/include/nolibc/dirent.h typedef struct { · line 39
Quoted 5 lines
tools/include/nolibc/dirent.h +5
-0+ +if (i >=0 ) +return -1 ; + +return ~i;Might be better to use a different name for the variable. The two return statements look the same at a first glance.
yeah, I didn't notice it until you brought it up but 1 and i is confusing. I guess I'll change i to fd.
Cheers,
Daniel
PATCH v2 2/2 selftests/nolibc: Add test for dirfd()
In reply to Daniel PalmerTest that open() and dirfd() use the same fd and then use dirfd() with fstatat() to stat each of its children.
--- tools/testing/selftests/nolibc/nolibc-test.c | 47 ++++++++++++++++++++ 1 file changed, 47 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +47 -0
@@ -858,6 +858,52 @@ static int test_dirent(void)return 0 ; } +static int test_dirfd (void ) +{ +struct dirent dirent, *result; +int ret =0 , fd; +struct stat st; +DIR *dir; + + fd =open ("/" ,O_RDONLY |O_DIRECTORY ); +if (fd <0 ) +return __LINE__; + + dir =fdopendir (fd); +if (!dir) { +close (fd); +return __LINE__; + } + +/* Check that dirfd() gives the same fd as open() */ +if (dirfd (dir) != fd) { + ret = __LINE__; + goto out; + } + +/* Loop through the children of the directory calling fstatat() on each */ +while (1 ) { +if (readdir_r (dir, &dirent, &result) !=0 ) { + ret = __LINE__; + goto out; + } + +if (!result) +break ; + +if (fstatat (dirfd (dir), dirent.d_name, &st,AT_SYMLINK_NOFOLLOW ) !=0 ) { + ret = __LINE__; + goto out; + + } + } + +out: +closedir (dir); + +return ret; +} +int test_getcwd (void ) {char cwd_syscall[PATH_MAX ];
@@ -1651,6 +1697,7 @@ int run_syscall(int min, int max)CASE_TEST (getdents64_root);EXPECT_SYSNE (1 ,test_getdents64 ("/" ),-1 );break ;CASE_TEST (getdents64_null);EXPECT_SYSER (1 ,test_getdents64 ("/dev/null" ),-1 ,ENOTDIR );break ;CASE_TEST (directories);EXPECT_SYSZR (is_nolibc && proc,test_dirent ());break ; +CASE_TEST (dirfd);EXPECT_SYSZR (is_nolibc,test_dirfd ());break ;CASE_TEST (getrandom);EXPECT_SYSZR (1 ,test_getrandom ());break ;CASE_TEST (gettimeofday_tv);EXPECT_SYSZR (1 ,gettimeofday (&tv,NULL ));break ;CASE_TEST (gettimeofday_tv_tz);EXPECT_SYSZR (1 ,gettimeofday (&tv, &tz));break ;
Signature · 1 line
2.53.0
Thomas Weißschuh
maintainerRe: [PATCH v2 2/2] selftests/nolibc: Add test for dirfd()
Hi Daniel,
Quoted 2 lines · Daniel Palmer
Test that
open()anddirfd()use the same fd and then usedirfd()withfstatat()to stat each of its children.
In general the series looks good. I have some minor requests, but can also implement these myself if you prefer.
Also I'd like to push the patches into the next development cycles.
Quoted 27 lines
Signed-off-by Daniel Palmer <[email protected]>--- tools/testing/selftests/nolibc/nolibc-test.c | 47 ++++++++++++++++++++ 1 file changed, 47 insertions(+)tools/testing/selftests/nolibc/nolibc-test.c +14
-0@@ -858,6 +858,52 @@ static int test_dirent(void)return 0 ; } +static int test_dirfd (void ) +{ +struct dirent dirent, *result; +int ret =0 , fd; +struct stat st; +DIR *dir; + + fd =open ("/" ,O_RDONLY |O_DIRECTORY ); +if (fd <0 ) +return __LINE__; + + dir =fdopendir (fd); +if (!dir) { +close (fd);
tools/testing/selftests/nolibc/nolibc-test.c static int test_dirent(void) · line 884
Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +10
-0+return __LINE__; + } + +/* Check that dirfd() gives the same fd as open() */ +if (dirfd (dir) != fd) { + ret = __LINE__; + goto out; + } + +/* Loop through the children of the directory calling fstatat() on each */
What is the advantage of reading all entries here? We already do something similar in test_dirent(). Reading a single entry should be enough.
Quoted 26 lines
tools/testing/selftests/nolibc/nolibc-test.c +22
-0+while (1 ) { +if (readdir_r (dir, &dirent, &result) !=0 ) { + ret = __LINE__; + goto out; + } + +if (!result) +break ; + +if (fstatat (dirfd (dir), dirent.d_name, &st,AT_SYMLINK_NOFOLLOW ) !=0 ) { + ret = __LINE__; + goto out; + + } + } + +out: +closedir (dir); + +return ret; +} +int test_getcwd (void ) {char cwd_syscall[PATH_MAX ];
tools/testing/selftests/nolibc/nolibc-test.c int run_syscall(int min, int max) · line 1700
Quoted 4 lines
tools/testing/selftests/nolibc/nolibc-test.c +1
-0@@ -1651,6 +1697,7 @@ int run_syscall(int min, int max)CASE_TEST (getdents64_root);EXPECT_SYSNE (1 ,test_getdents64 ("/" ),-1 );break ;CASE_TEST (getdents64_null);EXPECT_SYSER (1 ,test_getdents64 ("/dev/null" ),-1 ,ENOTDIR );break ;CASE_TEST (directories);EXPECT_SYSZR (is_nolibc && proc,test_dirent ());break ; +CASE_TEST (dirfd);EXPECT_SYSZR (is_nolibc,test_dirfd ());break ;
Could you mention in the commit message why this is specific to nolibc?
Quoted 6 lines
tools/testing/selftests/nolibc/nolibc-test.c +0
-0CASE_TEST (getrandom);EXPECT_SYSZR (1 ,test_getrandom ());break ;CASE_TEST (gettimeofday_tv);EXPECT_SYSZR (1 ,gettimeofday (&tv,NULL ));break ;CASE_TEST (gettimeofday_tv_tz);EXPECT_SYSZR (1 ,gettimeofday (&tv, &tz));break ;Signature · 1 line
2.53.0
Re: [PATCH v2 2/2] selftests/nolibc: Add test for dirfd()
Hi Thomas,
Quoted 3 lines · Thomas Weißschuh
In general the series looks good. I have some minor requests, but can also implement these myself if you prefer.
Also I'd like to push the patches into the next development cycles.
I'll do the fixes and send a v3. Also not in a hurry. :)
Cheers,
Daniel
Hi Daniel,
Quoted 9 lines · Daniel Palmer
Add
dirfd()to convert a DIR back to an fd for functions likefstatat()that take an fd.Change log:
v2 - Fix the name of the dirfd helper, I debated over removing the helper
as suggested by Thomas but it seems right to have the logic in a single place.
Add test a basic test to make suredirfd()actually does convert back to the expected fd and usingdirfd()withfstatat().
The series looks good to me:
Let's wait for Thomas in case he spots anything subtle.
Thanks!
Willy