2024-04-14 07:56:57

by Thomas Weißschuh

[permalink] [raw]
Subject: [PATCH] tools/nolibc: add support for uname(2)

All supported kernels are assumed to use struct new_utsname.
This is validated in test_uname().

uname(2) can for example be used in ksft_min_kernel_version() from the
kernels selftest framework.

Link: https://lore.kernel.org/lkml/[email protected]/
Signed-off-by: Thomas Weißschuh <[email protected]>
---
tools/include/nolibc/sys.h | 27 ++++++++++++++++++
tools/testing/selftests/nolibc/nolibc-test.c | 42 ++++++++++++++++++++++++++++
2 files changed, 69 insertions(+)

diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index dda9dffd1d74..7b82bc3cf107 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -22,6 +22,7 @@
#include <linux/stat.h> /* for statx() */
#include <linux/prctl.h>
#include <linux/resource.h>
+#include <linux/utsname.h>

#include "arch.h"
#include "errno.h"
@@ -1139,6 +1140,32 @@ int umount2(const char *path, int flags)
}


+/*
+ * int uname(struct utsname *buf);
+ */
+
+struct utsname {
+ char sysname[65];
+ char nodename[65];
+ char release[65];
+ char version[65];
+ char machine[65];
+ char domainname[65];
+};
+
+static __attribute__((unused))
+int sys_uname(struct utsname *buf)
+{
+ return my_syscall1(__NR_uname, buf);
+}
+
+static __attribute__((unused))
+int uname(struct utsname *buf)
+{
+ return __sysret(sys_uname(buf));
+}
+
+
/*
* int unlink(const char *path);
*/
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 6ba4f8275ac4..3c9a9bd38194 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -27,6 +27,7 @@
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <sys/time.h>
+#include <sys/utsname.h>
#include <sys/wait.h>
#include <dirent.h>
#include <errno.h>
@@ -761,6 +762,45 @@ int test_stat_timestamps(void)
return 0;
}

+int test_uname(void)
+{
+ struct utsname buf;
+ char osrelease[sizeof(buf.release)];
+ ssize_t r;
+ int fd;
+
+ memset(&buf.domainname, 'P', sizeof(buf.domainname));
+
+ if (uname(&buf))
+ return 1;
+
+ if (strncmp("Linux", buf.sysname, sizeof(buf.sysname)))
+ return 1;
+
+ fd = open("/proc/sys/kernel/osrelease", O_RDONLY);
+ if (fd == -1)
+ return 1;
+
+ r = read(fd, osrelease, sizeof(osrelease));
+ if (r == -1)
+ return 1;
+
+ close(fd);
+
+ if (osrelease[r - 1] == '\n')
+ r--;
+
+ /* Validate one of the later fields to ensure field sizes are correct */
+ if (strncmp(osrelease, buf.release, r))
+ return 1;
+
+ /* Ensure the field domainname is set, it is missing from struct old_utsname */
+ if (strnlen(buf.domainname, sizeof(buf.domainname)) == sizeof(buf.domainname))
+ return 1;
+
+ return 0;
+}
+
int test_mmap_munmap(void)
{
int ret, fd, i, page_size;
@@ -966,6 +1006,8 @@ int run_syscall(int min, int max)
CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break;
CASE_TEST(stat_timestamps); EXPECT_SYSZR(1, test_stat_timestamps()); break;
CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break;
+ CASE_TEST(uname); EXPECT_SYSZR(1, test_uname()); break;
+ CASE_TEST(uname_fault); EXPECT_SYSER(1, uname(NULL), -1, EFAULT); break;
CASE_TEST(unlink_root); EXPECT_SYSER(1, unlink("/"), -1, EISDIR); break;
CASE_TEST(unlink_blah); EXPECT_SYSER(1, unlink("/proc/self/blah"), -1, ENOENT); break;
CASE_TEST(wait_child); EXPECT_SYSER(1, wait(&tmp), -1, ECHILD); break;

---
base-commit: 4cece764965020c22cff7665b18a012006359095
change-id: 20240414-nolibc-uname-de3895a4425f

Best regards,
--
Thomas Weißschuh <[email protected]>



2024-04-14 11:07:10

by Willy Tarreau

[permalink] [raw]
Subject: Re: [PATCH] tools/nolibc: add support for uname(2)

Hi Thomas!

On Sun, Apr 14, 2024 at 09:56:23AM +0200, Thomas Wei?schuh wrote:
> All supported kernels are assumed to use struct new_utsname.
> This is validated in test_uname().
>
> uname(2) can for example be used in ksft_min_kernel_version() from the
> kernels selftest framework.
>
> Link: https://lore.kernel.org/lkml/[email protected]/

I find it really annoying when other developers waste time trying to
work around some missing trivial syscalls. I would have bet we already
had this one, but obviously not.

That's obviously an ack by me: Acked-by: Willy Tarreau <[email protected]>

Thank you!
Willy

2024-04-14 18:37:14

by Thomas Weißschuh

[permalink] [raw]
Subject: Re: [PATCH] tools/nolibc: add support for uname(2)

Hi Willy!

On 2024-04-14 12:56:46+0200, Willy Tarreau wrote:
> On Sun, Apr 14, 2024 at 09:56:23AM +0200, Thomas Weißschuh wrote:
> > All supported kernels are assumed to use struct new_utsname.
> > This is validated in test_uname().
> >
> > uname(2) can for example be used in ksft_min_kernel_version() from the
> > kernels selftest framework.
> >
> > Link: https://lore.kernel.org/lkml/[email protected]/
>
> I find it really annoying when other developers waste time trying to
> work around some missing trivial syscalls. I would have bet we already
> had this one, but obviously not.

It's a bit annoying to validate that it works given the fact there are
the structs new_utsname, old_utsname and oldold_utsname...

> That's obviously an ack by me: Acked-by: Willy Tarreau <[email protected]>

Thanks, pushed to nolibc/next.
(With a tiny change to skip the testcase if procfs is not available.

Thomas