diff options
| author | Mark Brown <broonie@kernel.org> | 2026-07-06 15:09:30 +0100 |
|---|---|---|
| committer | Mark Brown <broonie@kernel.org> | 2026-07-06 15:09:30 +0100 |
| commit | 051f46b93e116743a576e8112d61bd48ac51513a (patch) | |
| tree | 2ca47ef6a2b7ac84dac1de0aa26fedb621594f8b | |
| parent | 64ce27158d432931761b44fcf11017255a946e18 (diff) | |
| parent | a3b2181459a2c74c03ddbad585f884eefc8ff8ff (diff) | |
| download | linux-next-051f46b93e116743a576e8112d61bd48ac51513a.tar.gz linux-next-051f46b93e116743a576e8112d61bd48ac51513a.zip | |
Merge branch 'for-next' of https://git.kernel.org/pub/scm/linux/kernel/git/nolibc/linux-nolibc.git
| -rw-r--r-- | tools/include/nolibc/arch-sparc.h | 2 | ||||
| -rw-r--r-- | tools/include/nolibc/unistd.h | 58 | ||||
| -rw-r--r-- | tools/testing/selftests/nolibc/nolibc-test.c | 53 |
3 files changed, 112 insertions, 1 deletions
diff --git a/tools/include/nolibc/arch-sparc.h b/tools/include/nolibc/arch-sparc.h index ddae9bc10dfe..23fab40accfa 100644 --- a/tools/include/nolibc/arch-sparc.h +++ b/tools/include/nolibc/arch-sparc.h @@ -45,7 +45,7 @@ \ __asm__ volatile ( \ _NOLIBC_SYSCALL \ - : "+r"(_arg1) \ + : "=r"(_arg1) \ : "r"(_num) \ : "memory", "cc" \ ); \ diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h index 79599ceef45d..a264a20da13d 100644 --- a/tools/include/nolibc/unistd.h +++ b/tools/include/nolibc/unistd.h @@ -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) { @@ -86,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) { diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index c1c1ce43a047..996e8d13508e 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -854,6 +854,58 @@ static int test_dirent(void) return 0; } +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; |
