nolibc: Add getcwd() and readlink()
Subsystems (2)
cover nolibc: Add getcwd() and readlink()
Me again :)
I needed getcwd() for something. So I added that, then realised I also needed readlink() to test the result of getcwd().
This adds getcwd() and readlink() then adds a test that uses both.
nolibc selftest passed on: i386 x86_64 x32 arm64 arm armthumb mips32le mips32be mipsn32le mipsn32be mips64le mips64be ppc ppc64 ppc64le riscv32 riscv64 sparc32 sparc64 m68k
Daniel Palmer (3):
tools/nolibc: unistd: Add getcwd() tools/nolibc: unistd: Add readlink() selftests/nolibc: Add test for getcwd() and readlink()
tools/include/nolibc/unistd.h | 42 ++++++++++++++++++++ tools/testing/selftests/nolibc/nolibc-test.c | 26 ++++++++++++ 2 files changed, 68 insertions(+)
base-commit: 266df86c4893fed1a7f027e767fe1c7f6456b100
Signature · 1 line
2.53.0
PATCH 1/3 tools/nolibc: unistd: Add getcwd()
Add getcwd() for getting the current working directory.
--- tools/include/nolibc/unistd.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
tools/include/nolibc/unistd.h +22 -0
@@ -48,6 +48,28 @@ int access(const char *path, int amode)return faccessat (AT_FDCWD , path, amode,0 ); } +/* +* char *getcwd(char *buf, size_t size); +*/ + +static __attribute__((unused)) +int _sys_getcwd (char *buf,size_t size) +{ +return __nolibc_syscall2 (__NR_getcwd, buf, size); +} + +static __attribute__((unused)) +char *getcwd (char *buf,size_t size) +{ +/* If successful we get the number of bytes written into +* the buffer, so check the result is positive. +*/ +if (__sysret (_sys_getcwd (buf, size)) >0 ) +return buf; + +/* On error return NULL, __sysret() above will have set errno */ +return NULL ; +}static __attribute__((unused))int msleep (unsigned int msecs)
Signature · 1 line
2.53.0
Thomas Weißschuh
maintainerRe: [PATCH 1/3] tools/nolibc: unistd: Add getcwd()
May 2, 2026 09:25:43 Daniel Palmer <[email protected]>:
Quoted 22 lines
Add
getcwd()for getting the current working directory.Signed-off-by Daniel Palmer <[email protected]>---
tools/include/nolibc/unistd.h| 22 ++++++++++++++++++++++1 file changed, 22 insertions(+)tools/include/nolibc/unistd.h +7
-0@@ -48,6 +48,28 @@ int access(const char *path, int amode) return faccessat(AT_FDCWD, path, amode, 0); } +/* +* char *getcwd(char *buf, size_t size); +*/ + +static __attribute__((unused)) +int _sys_getcwd (char *buf,size_t size) +{
tools/include/nolibc/unistd.h int access(const char *path, int amode) · line 67
Quoted 10 lines
tools/include/nolibc/unistd.h +10
-0+ return __nolibc_syscall2(__NR_getcwd, buf, size); +} + +static __attribute__((unused)) +char *getcwd (char *buf,size_t size) +{ + /* If successful we get the number of bytes written into + * the buffer, so check the result is positive. + */ + if (__sysret(_sys_getcwd(buf, size)) > 0)
According to the comment before the system call implementation this should be '>= 0'.
Quoted 10 lines
tools/include/nolibc/unistd.h +5
-1+ return buf; + + /* On error return NULL, __sysret() above will have set errno */ + return NULL; +}static __attribute__((unused))int msleep (unsigned int msecs) --2.53.0
Re: [PATCH 1/3] tools/nolibc: unistd: Add getcwd()
Hi Thomas,
Quoted 7 lines · Thomas Weißschuh
tools/include/nolibc/unistd.h int access(const char *path, int amode) · line 67
Quoted 5 lines
tools/include/nolibc/unistd.h +5
-0+{ +/* If successful we get the number of bytes written into +* the buffer, so check the result is positive. +*/ +if (__sysret (_sys_getcwd (buf, size)) >0 )According to the comment before the system call implementation this should be '>= 0'.
I think 0 is an error and the comment I wrote is a bit off. I think it should be "result is greater than 0".
Anyhow, I wondered what musl and glibc were doing around this as I couldn't work it out from the manpage.
Long story short I have adjusted this so the logic follows what musl is doing and I'll send a v2.
Thanks,
Daniel.
PATCH 2/3 tools/nolibc: unistd: Add readlink()
In reply to Daniel PalmerAdd readlink(). This is needed to test getcwd();
--- tools/include/nolibc/unistd.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
tools/include/nolibc/unistd.h +20 -0
@@ -84,6 +84,26 @@ int msleep(unsigned int msecs)return 0 ; } +/* +* ssize_t readlink(const char *path, char *buf, size_t bufsiz); +*/ + +static __attribute__((unused)) +ssize_t _sys_readlink (const char *path,char *buf,size_t bufsiz) +{ +#ifdef __NR_readlink +return __nolibc_syscall3 (__NR_readlink, path, buf, bufsiz); +#else +return __nolibc_syscall4 (__NR_readlinkat,AT_FDCWD , path, buf, bufsiz); +#endif +} + +static __attribute__((unused)) +ssize_t readlink (const char *path,char *buf,size_t bufsiz) +{ +return __sysret (_sys_readlink (path, buf, bufsiz)); +} +static __attribute__((unused))unsigned int sleep (unsigned int seconds) {
Signature · 1 line
2.53.0
Thomas Weißschuh
maintainerRe: [PATCH 2/3] tools/nolibc: unistd: Add readlink()
May 2, 2026 09:25:44 Daniel Palmer <[email protected]>:
Quoted 17 lines
Add
readlink(). This is needed to testgetcwd();Signed-off-by Daniel Palmer <[email protected]>---
tools/include/nolibc/unistd.h| 20 ++++++++++++++++++++1 file changed, 20 insertions(+)tools/include/nolibc/unistd.h +2
-0@@ -84,6 +84,26 @@ int msleep(unsigned int msecs) return 0; } +/* +* ssize_t readlink(const char *path, char *buf, size_t bufsiz);
tools/include/nolibc/unistd.h int msleep(unsigned int msecs) · line 98
Quoted 10 lines
tools/include/nolibc/unistd.h +10
-0+*/ + +static __attribute__((unused)) +ssize_t _sys_readlink (const char *path,char *buf,size_t bufsiz) +{ +#ifdef __NR_readlink + return __nolibc_syscall3(__NR_readlink, path, buf, bufsiz); +#else + return __nolibc_syscall4(__NR_readlinkat, AT_FDCWD, path, buf, bufsiz); +#endif
Every architecture should have readli kat, no? Then I think we can drop the fallback to regular readlink.
Quoted 13 lines
tools/include/nolibc/unistd.h +8
-1+} + +static __attribute__((unused)) +ssize_t readlink (const char *path,char *buf,size_t bufsiz) +{ + return __sysret(_sys_readlink(path, buf, bufsiz)); +} +static __attribute__((unused))unsigned int sleep (unsigned int seconds) { --2.53.0
PATCH 3/3 selftests/nolibc: Add test for getcwd() and readlink()
In reply to Daniel PalmerAdd a test for getcwd() that uses readlink() so tests that at the same time.
This is very basic, just getting what should be the same string via getcwd() and readlink() and then checking they are the same.
--- tools/testing/selftests/nolibc/nolibc-test.c | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +26 -0
@@ -849,6 +849,31 @@ static int test_dirent(void)return 0 ; } +static int test_getcwd (void ) +{ +char cwd_syscall[1024 ]; +char cwd_proc[1024 ]; +ssize_t len; + +/* Read where the link /proc/self/cwd points */ + len =readlink ("/proc/self/cwd" , cwd_proc,sizeof (cwd_proc) -1 ); +if (len <=0 ) +return -1 ; + +/* Terminate the string from readlink() */ + cwd_proc[len] ='\0' ; + +/* Get the cwd via syscall */ +if (getcwd (cwd_syscall,sizeof (cwd_syscall)) ==NULL ) +return -1 ; + +/* Fail if they aren't the same */ +if (strcmp (cwd_proc, cwd_syscall) !=0 ) +return -1 ; + +return 0 ; +} +int test_getrandom (void ) {uint64_t rng =0 ;
@@ -1477,6 +1502,7 @@ int run_syscall(int min, int max)CASE_TEST (clock_getres);EXPECT_SYSZR (1 ,clock_getres (CLOCK_MONOTONIC , &ts));break ;CASE_TEST (clock_gettime);EXPECT_SYSZR (1 ,clock_gettime (CLOCK_MONOTONIC , &ts));break ;CASE_TEST (clock_settime);EXPECT_SYSER (1 ,clock_settime (CLOCK_MONOTONIC , &ts),-1 ,EINVAL );break ; +CASE_TEST (getcwd);EXPECT_SYSZR (1 ,test_getcwd ());break ;CASE_TEST (getpid);EXPECT_SYSNE (1 ,getpid (),-1 );break ;CASE_TEST (getppid);EXPECT_SYSNE (1 ,getppid (),-1 );break ;CASE_TEST (gettid);EXPECT_SYSNE (has_gettid,gettid (),-1 );break ;
Signature · 1 line
2.53.0
Thomas Weißschuh
maintainerRe: [PATCH 3/3] selftests/nolibc: Add test for getcwd() and readlink()
May 2, 2026 09:25:45 Daniel Palmer <[email protected]>:
Quoted 49 lines
Add a test for
getcwd()that usesreadlink()so tests that at the same time.This is very basic, just getting what should be the same string via
getcwd()andreadlink()and then checking they are the same.Signed-off-by Daniel Palmer <[email protected]>---
tools/testing/selftests/nolibc/nolibc-test.c| 26 ++++++++++++++++++++1 file changed, 26 insertions(+)tools/testing/selftests/nolibc/nolibc-test.c +25
-0@@ -849,6 +849,31 @@ static int test_dirent(void) return 0; } +static int test_getcwd (void ) +{ + char cwd_syscall[1024]; + char cwd_proc[1024]; + ssize_t len; + + /* Read where the link /proc/self/cwd points */ + len = readlink("/proc/self/cwd", cwd_proc, sizeof(cwd_proc) - 1); + if (len <= 0) + return -1; + + /* Terminate the string from readlink() */ + cwd_proc[len] = '\0'; + + /* Get the cwd via syscall */ + if (getcwd(cwd_syscall, sizeof(cwd_syscall)) == NULL) + return -1; + + /* Fail if they aren't the same */ + if (strcmp(cwd_proc, cwd_syscall) != 0) + return -1; + + return 0; +} +int test_getrandom (void ) { uint64_t rng = 0;
tools/testing/selftests/nolibc/nolibc-test.c int run_syscall(int min, int max) · line 1505
Quoted 4 lines
tools/testing/selftests/nolibc/nolibc-test.c +1
-0@@ -1477,6 +1502,7 @@ int run_syscall(int min, int max) CASE_TEST(clock_getres); EXPECT_SYSZR(1, clock_getres(CLOCK_MONOTONIC, &ts)); break; CASE_TEST(clock_gettime); EXPECT_SYSZR(1, clock_gettime(CLOCK_MONOTONIC, &ts)); break; CASE_TEST(clock_settime); EXPECT_SYSER(1, clock_settime(CLOCK_MONOTONIC, &ts), -1, EINVAL); break; + CASE_TEST(getcwd); EXPECT_SYSZR(1, test_getcwd()); break;
EXPECT_SYSZR(proc, ...)
Quoted 5 lines
tools/testing/selftests/nolibc/nolibc-test.c +0
-1CASE_TEST(getpid); EXPECT_SYSNE(1, getpid(), -1); break; CASE_TEST(getppid); EXPECT_SYSNE(1, getppid(), -1); break; CASE_TEST(gettid); EXPECT_SYSNE(has_gettid, gettid(), -1); break; --2.53.0