Skip to contentSkip to conversation

nolibc: Add getcwd() and readlink()

PATCH Applied Daniel Palmer 3 participants 7 messages Jul 2 – Jul 3

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover nolibc: Add getcwd() and readlink()

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.

v3:
- Addressed the issues noted by sashiko (impressed by how good it is now)
- Use __LINE__ for the error numbers in the test to make it easy to spot where it broke.
- Redid the nolibc test for all archs, all passed.

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                | 58 ++++++++++++++++++++
 tools/testing/selftests/nolibc/nolibc-test.c | 53 ++++++++++++++++++
 2 files changed, 111 insertions(+)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v3 1/3 tools/nolibc: unistd: Add getcwd()

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.

---
 tools/include/nolibc/unistd.h | 42 +++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
tools/include/nolibc/unistd.h +42 -0
index 79599ceef45d..2e0edbc26315 100644
@@ -73,6 +73,48 @@ 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 */
+	if (!buf || !size) {
+		SET_ERRNO(EINVAL);
+		return NULL;
+	}
+
+	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)
 {
Signature · 1 line
2.53.0
maintainer Acked-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] tools/nolibc: unistd: Add getcwd()

Hi Daniel,

Quoted 9 lines · 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.

I entirely agree.

Quoted 49 lines
---
 tools/include/nolibc/unistd.h | 42 +++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
tools/include/nolibc/unistd.h +35 -0
index 79599ceef45d..2e0edbc26315 100644
@@ -73,6 +73,48 @@ 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 */
+	if (!buf || !size) {
+		SET_ERRNO(EINVAL);
+		return NULL;
+	}
+
+	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;
+	}

tools/include/nolibc/unistd.h int ftruncate(int fd, off_t length) · line 120

Quoted 10 lines
tools/include/nolibc/unistd.h +7 -0
+
+	/* 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)
 {

LGTM.

Willy

#3 lore raw

PATCH v3 2/3 tools/nolibc: unistd: Add readlink()

In reply to Daniel Palmer

Add readlink(). This is needed to test getcwd().

---
 tools/include/nolibc/unistd.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tools/include/nolibc/unistd.h +16 -0
index 2e0edbc26315..a264a20da13d 100644
@@ -128,6 +128,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)
 {
Signature · 1 line
2.53.0
maintainer Acked-by on v3 2/3
#4 lore raw

Re: [PATCH v3 2/3] tools/nolibc: unistd: Add readlink()

Quoted 24 lines · Daniel Palmer

Add readlink(). This is needed to test getcwd().

---
 tools/include/nolibc/unistd.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tools/include/nolibc/unistd.h +9 -0
index 2e0edbc26315..a264a20da13d 100644
@@ -128,6 +128,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);
+}

tools/include/nolibc/unistd.h int msleep(unsigned int msecs) · line 149

Quoted 10 lines
tools/include/nolibc/unistd.h +7 -0
+
+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)
 {

Willy

#5 lore raw

PATCH v3 3/3 selftests/nolibc: Add test for getcwd() and readlink()

In reply to 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.

---
 tools/testing/selftests/nolibc/nolibc-test.c | 53 ++++++++++++++++++++
 1 file changed, 53 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +53 -0
index c1c1ce43a047..ad3f6e0751be 100644
@@ -854,6 +854,58 @@ static int test_dirent(void)
 	return 0;
 }
 
+static int test_getcwd(void)
+{
+	char cwd_syscall[PATH_MAX];
+	char cwd_proc[PATH_MAX];
+	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 __LINE__;
+
+	/* Terminate the string from readlink() */
+	cwd_proc[len] = '\0';
+
+	/* Get the cwd via syscall */
+	if (getcwd(cwd_syscall, sizeof(cwd_syscall)) == NULL)
+		return __LINE__;
+
+	/* Fail if they aren't the same */
+	if (strcmp(cwd_proc, cwd_syscall) != 0)
+		return __LINE__;
+
+	/* Try getcwd() with NULL for the buffer,
+	 * should return NULL and an error in errno.
+	 * Other libc's allow this by allocating a buffer
+	 * internally.
+	 */
+	if (is_nolibc) {
+		errno = 0;
+		if (getcwd(NULL, 0) != NULL || !errno)
+			return __LINE__;
+	}
+
+	/* 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 __LINE__;
+
+	/* 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 __LINE__;
+
+	return 0;
+}
+
 int test_getrandom(void)
 {
 	uint64_t rng = 0;
@@ -1555,6 +1607,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;
Signature · 1 line
2.53.0
maintainer Applied
#6 lore raw

Re: [PATCH v3 3/3] selftests/nolibc: Add test for getcwd() and readlink()

(+Cc clang/llvm folks, scroll down to test_getcwd())

Hi Daniel,

Quoted 11 lines · 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.

Applied, thanks.

Quoted 9 lines
---
 tools/testing/selftests/nolibc/nolibc-test.c | 53 ++++++++++++++++++++
 1 file changed, 53 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +0 -0
index c1c1ce43a047..ad3f6e0751be 100644

tools/testing/selftests/nolibc/nolibc-test.c static int test_dirent(void) · line 858

Quoted 5 lines
tools/testing/selftests/nolibc/nolibc-test.c +2 -0
@@ -854,6 +854,58 @@ static int test_dirent(void)
 	return 0;
 }
 
+static int test_getcwd(void)
+{

I removed the static from this function.
When it is inlined the tests fail on SPARC on clang.
It seems the system call register is clobbered before the syscall is invoked. It works fine on GCC.
The fix in [0] makes things a bit better, but not completely.

To reproduce:

* Check out the branch for-next in [1]
* Apply [0]
* cd tools/testing/selftests/nolibc
* ./run-tests.sh -m user -l sparc32
* Only run the problematic testcase: ./nolibc-test syscall:1

(The "sparc64" configuration is also broken,
it looks like the same issue)

This happens for me with clang 22.1.6.

(...)

[0] https://lore.kernel.org/lkml/[email protected]/
[1] https://git.kernel.org/pub/scm/linux/kernel/git/nolibc/linux-nolibc.git/

Thomas