Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/nolibc: add getrandom()

This is used in various selftests and will be handy when integrating
those with nolibc.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://lore.kernel.org/r/20250428-nolibc-misc-v2-5-3c043eeab06c@linutronix.de
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

authored by

Thomas Weißschuh and committed by
Thomas Weißschuh
801f020b 55175d86

+58
+1
tools/include/nolibc/Makefile
··· 48 48 sys.h \ 49 49 sys/auxv.h \ 50 50 sys/mman.h \ 51 + sys/random.h \ 51 52 sys/stat.h \ 52 53 sys/syscall.h \ 53 54 sys/time.h \
+1
tools/include/nolibc/nolibc.h
··· 98 98 #include "sys.h" 99 99 #include "sys/auxv.h" 100 100 #include "sys/mman.h" 101 + #include "sys/random.h" 101 102 #include "sys/stat.h" 102 103 #include "sys/syscall.h" 103 104 #include "sys/time.h"
+34
tools/include/nolibc/sys/random.h
··· 1 + /* SPDX-License-Identifier: LGPL-2.1 OR MIT */ 2 + /* 3 + * random definitions for NOLIBC 4 + * Copyright (C) 2025 Thomas Weißschuh <thomas.weissschuh@linutronix.de> 5 + */ 6 + 7 + /* make sure to include all global symbols */ 8 + #include "../nolibc.h" 9 + 10 + #ifndef _NOLIBC_SYS_RANDOM_H 11 + #define _NOLIBC_SYS_RANDOM_H 12 + 13 + #include "../arch.h" 14 + #include "../sys.h" 15 + 16 + #include <linux/random.h> 17 + 18 + /* 19 + * ssize_t getrandom(void *buf, size_t buflen, unsigned int flags); 20 + */ 21 + 22 + static __attribute__((unused)) 23 + ssize_t sys_getrandom(void *buf, size_t buflen, unsigned int flags) 24 + { 25 + return my_syscall3(__NR_getrandom, buf, buflen, flags); 26 + } 27 + 28 + static __attribute__((unused)) 29 + ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) 30 + { 31 + return __sysret(sys_getrandom(buf, buflen, flags)); 32 + } 33 + 34 + #endif /* _NOLIBC_SYS_RANDOM_H */
+22
tools/testing/selftests/nolibc/nolibc-test.c
··· 20 20 #include <sys/mman.h> 21 21 #include <sys/mount.h> 22 22 #include <sys/prctl.h> 23 + #include <sys/random.h> 23 24 #include <sys/reboot.h> 24 25 #include <sys/resource.h> 25 26 #include <sys/stat.h> ··· 808 807 return 0; 809 808 } 810 809 810 + int test_getrandom(void) 811 + { 812 + uint64_t rng = 0; 813 + ssize_t ret; 814 + 815 + ret = getrandom(&rng, sizeof(rng), GRND_NONBLOCK); 816 + if (ret == -1 && errno == EAGAIN) 817 + return 0; /* No entropy available yet */ 818 + 819 + if (ret != sizeof(rng)) 820 + return ret; 821 + 822 + if (!rng) { 823 + errno = EINVAL; 824 + return -1; 825 + } 826 + 827 + return 0; 828 + } 829 + 811 830 int test_getpagesize(void) 812 831 { 813 832 int x = getpagesize(); ··· 1145 1124 CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1); break; 1146 1125 CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break; 1147 1126 CASE_TEST(directories); EXPECT_SYSZR(proc, test_dirent()); break; 1127 + CASE_TEST(getrandom); EXPECT_SYSZR(1, test_getrandom()); break; 1148 1128 CASE_TEST(gettimeofday_tv); EXPECT_SYSZR(1, gettimeofday(&tv, NULL)); break; 1149 1129 CASE_TEST(gettimeofday_tv_tz);EXPECT_SYSZR(1, gettimeofday(&tv, &tz)); break; 1150 1130 CASE_TEST(getpagesize); EXPECT_SYSZR(1, test_getpagesize()); break;