* [PATCH v2 0/3] nolibc: Add getcwd() and readlink()
@ 2026-06-30 13:24 Daniel Palmer
2026-06-30 13:24 ` [PATCH v2 1/3] tools/nolibc: unistd: Add getcwd() Daniel Palmer
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Daniel Palmer @ 2026-06-30 13:24 UTC (permalink / raw)
To: w, linux; +Cc: linux-kernel, Daniel Palmer
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.
Note: getcwd() has some behaviour where if the current directory
is unreachable then the kernel returns "(unreachable)" and this
needs some special handling. I checked what musl was doing for
this. It seems pretty difficult to test and maybe needs some
calls nolibc doesn't have yet. Basically: It looks right but
there is no test.
v2:
- Addressed Thomas' comments for readlink() and making the test depend on proc.
- Filled out getcwd() so it matches what musl is doing.
- Expanded the test a little to try to cover passing bad arguments.
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 | 56 ++++++++++++++++++++
tools/testing/selftests/nolibc/nolibc-test.c | 48 +++++++++++++++++
2 files changed, 104 insertions(+)
--
2.53.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 1/3] tools/nolibc: unistd: Add getcwd()
2026-06-30 13:24 [PATCH v2 0/3] nolibc: Add getcwd() and readlink() Daniel Palmer
@ 2026-06-30 13:24 ` Daniel Palmer
2026-06-30 13:24 ` [PATCH v2 2/3] tools/nolibc: unistd: Add readlink() Daniel Palmer
2026-06-30 13:24 ` [PATCH v2 3/3] selftests/nolibc: Add test for getcwd() and readlink() Daniel Palmer
2 siblings, 0 replies; 5+ messages in thread
From: Daniel Palmer @ 2026-06-30 13:24 UTC (permalink / raw)
To: w, linux; +Cc: linux-kernel, Daniel Palmer
Add getcwd() for getting the current working directory.
The behaviour matches what musl is doing except for one
important difference: If the passed buf is NULL musl (and glibc)
uses a big buffer on the stack and that is then strdup()'d and
returned.
According to the man page for getcwd() this is a glibc extension
and I don't think we need it in nolibc.
Signed-off-by: Daniel Palmer <daniel@thingy.jp>
---
tools/include/nolibc/unistd.h | 40 +++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h
index 79599ceef45d..057fc3a94179 100644
--- a/tools/include/nolibc/unistd.h
+++ b/tools/include/nolibc/unistd.h
@@ -73,6 +73,46 @@ int ftruncate(int fd, off_t length)
return __sysret(_sys_ftruncate(fd, length));
}
+/*
+ * 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)
+{
+ int ret;
+
+ /* Unlike other libc's we don't handle passing NULL for buf,
+ * we just call the syscall, if you passed NULL you'll get
+ * an error.
+ */
+ ret = __sysret(_sys_getcwd(buf, size));
+
+ /* On error return NULL, __sysret() above will have set errno */
+ if (ret < 0)
+ return NULL;
+
+ /* Handle no path being written or the kernel putting
+ * "(unreachable)" into the buffer instead of a path.
+ * This matches what musl is doing.
+ */
+ if (ret == 0 || buf[0] != '/') {
+ SET_ERRNO(ENOENT);
+ return NULL;
+ }
+
+ /* ret must be the number of bytes written at this point,
+ * so return the pointer to buf.
+ */
+ return buf;
+}
+
static __attribute__((unused))
int msleep(unsigned int msecs)
{
--
2.53.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/3] tools/nolibc: unistd: Add readlink()
2026-06-30 13:24 [PATCH v2 0/3] nolibc: Add getcwd() and readlink() Daniel Palmer
2026-06-30 13:24 ` [PATCH v2 1/3] tools/nolibc: unistd: Add getcwd() Daniel Palmer
@ 2026-06-30 13:24 ` Daniel Palmer
2026-06-30 13:24 ` [PATCH v2 3/3] selftests/nolibc: Add test for getcwd() and readlink() Daniel Palmer
2 siblings, 0 replies; 5+ messages in thread
From: Daniel Palmer @ 2026-06-30 13:24 UTC (permalink / raw)
To: w, linux; +Cc: linux-kernel, Daniel Palmer
Add readlink(). This is needed to test getcwd().
Signed-off-by: Daniel Palmer <daniel@thingy.jp>
---
tools/include/nolibc/unistd.h | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h
index 057fc3a94179..794727bf4e63 100644
--- a/tools/include/nolibc/unistd.h
+++ b/tools/include/nolibc/unistd.h
@@ -126,6 +126,22 @@ 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)
+{
+ return __nolibc_syscall4(__NR_readlinkat, AT_FDCWD, path, buf, bufsiz);
+}
+
+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
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 3/3] selftests/nolibc: Add test for getcwd() and readlink()
2026-06-30 13:24 [PATCH v2 0/3] nolibc: Add getcwd() and readlink() Daniel Palmer
2026-06-30 13:24 ` [PATCH v2 1/3] tools/nolibc: unistd: Add getcwd() Daniel Palmer
2026-06-30 13:24 ` [PATCH v2 2/3] tools/nolibc: unistd: Add readlink() Daniel Palmer
@ 2026-06-30 13:24 ` Daniel Palmer
2026-06-30 18:47 ` Thomas Weißschuh
2 siblings, 1 reply; 5+ messages in thread
From: Daniel Palmer @ 2026-06-30 13:24 UTC (permalink / raw)
To: w, linux; +Cc: linux-kernel, Daniel Palmer
Add a test that uses both getcwd() and readlink() so that both
are exercised.
First the happy path is tested by fetching what should be the
same string via getcwd() and readlink() and checking they match.
Then a few different combinations of bad parameters are passed to
getcwd() to make sure it returns NULL and sets errno in those
cases.
Signed-off-by: Daniel Palmer <daniel@thingy.jp>
---
tools/testing/selftests/nolibc/nolibc-test.c | 48 ++++++++++++++++++++
1 file changed, 48 insertions(+)
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index c1c1ce43a047..d54e7188e2ab 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -854,6 +854,53 @@ 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;
+
+ /* Try getcwd() with NULL for the buffer,
+ * should return NULL and an error in errno.
+ */
+ if (getcwd(NULL, 0) != NULL || !errno)
+ return -1;
+
+ /* Try getcwd() with a buffer but make the size 0,
+ * should return NULL and an error in errno.
+ */
+ errno = 0;
+ if (getcwd(cwd_syscall, 0) != NULL || !errno)
+ return -1;
+
+ /* Try getcwd() with a buffer but make the size 1,
+ * should return NULL and an error in errno because
+ * the string written to the buffer is terminated
+ * so you need at least 2 bytes even for "/".
+ */
+ errno = 0;
+ if (getcwd(cwd_syscall, 1) != NULL || !errno)
+ return -1;
+
+ return 0;
+}
+
int test_getrandom(void)
{
uint64_t rng = 0;
@@ -1555,6 +1602,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(proc, 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;
--
2.53.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 3/3] selftests/nolibc: Add test for getcwd() and readlink()
2026-06-30 13:24 ` [PATCH v2 3/3] selftests/nolibc: Add test for getcwd() and readlink() Daniel Palmer
@ 2026-06-30 18:47 ` Thomas Weißschuh
0 siblings, 0 replies; 5+ messages in thread
From: Thomas Weißschuh @ 2026-06-30 18:47 UTC (permalink / raw)
To: Daniel Palmer; +Cc: w, linux-kernel
Hi Daniel,
thanks for the patches!
On 2026-06-30 22:24:19+0900, Daniel Palmer wrote:
> Add a test that uses both getcwd() and readlink() so that both
> are exercised.
>
> First the happy path is tested by fetching what should be the
> same string via getcwd() and readlink() and checking they match.
>
> Then a few different combinations of bad parameters are passed to
> getcwd() to make sure it returns NULL and sets errno in those
> cases.
Sashiko found a few things. I think all of them are valid.
https://sashiko.dev/#/patchset/20260630132419.2523890-1-daniel%40thingy.jp
> Signed-off-by: Daniel Palmer <daniel@thingy.jp>
> ---
> tools/testing/selftests/nolibc/nolibc-test.c | 48 ++++++++++++++++++++
> 1 file changed, 48 insertions(+)
>
> diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> index c1c1ce43a047..d54e7188e2ab 100644
> --- a/tools/testing/selftests/nolibc/nolibc-test.c
> +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> @@ -854,6 +854,53 @@ 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;
Please return different values here, so it is obvious which subtest
failed.
> +
> + /* Terminate the string from readlink() */
> + cwd_proc[len] = '\0';
> +
> + /* Get the cwd via syscall */
> + if (getcwd(cwd_syscall, sizeof(cwd_syscall)) == NULL)
> + return -1;
(...)
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-06-30 18:47 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-30 13:24 [PATCH v2 0/3] nolibc: Add getcwd() and readlink() Daniel Palmer
2026-06-30 13:24 ` [PATCH v2 1/3] tools/nolibc: unistd: Add getcwd() Daniel Palmer
2026-06-30 13:24 ` [PATCH v2 2/3] tools/nolibc: unistd: Add readlink() Daniel Palmer
2026-06-30 13:24 ` [PATCH v2 3/3] selftests/nolibc: Add test for getcwd() and readlink() Daniel Palmer
2026-06-30 18:47 ` 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