[LTP] [PATCH 2/2] lapi: getrandom05: Add getrandom() fallback

Petr Vorel
2024-04-23T13:29:16Z

Fix missing getrandom() support detection on glibc < 2.25 and musl
< 1.1.20.

Add m4 check and use lapi header in getrandom05 to fix error:

getrandom05.c:16:24: fatal error: sys/random.h: No such file or directory
#include <sys/random.h>

on openSUSE Leap 42.2 (glibc 2.22, kernel 4.4).

NOTE: getrandom() requires Linux >= 3.17, which is not supported. While
it'd be quite easy to check (it would require e.g. AC_LINK_IFELSE()
check), I skipped that, because we are going to drop kernel 3.10 support.

Fixes: d9280782d ("getrandom: Add negative tests for getrandom")
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
Based on the fix I did some this last year for nfs-utils:

https://git.linux-nfs.org/?p=steved/nfs-utils.git;a=commitdiff;h=f92fd6ca815025c435dabf45da28472ac0aa04a4

For LTP I decided to ignore kernel 3.10 support check, thus it's enough
just to check for <sys/random.h>.

Can we drop 3.10 support? I suppose yes, but more ack for my commit
would be great.

https://patchwork.ozlabs.org/project/ltp/patch/20240419124221.207445-1-pvorel@suse.cz/
https://lore.kernel.org/ltp/20240419124221.207445-1-pvorel@suse.cz/

Kind regards,
Petr

configure.ac | 1 +
include/lapi/getrandom.h | 15 +++++++++++++--
testcases/kernel/syscalls/getrandom/getrandom05.c | 3 ++-
3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index 1d7e862d8..15a5847fa 100644
--- a/configure.ac
+++ b/configure.ac
@@ -76,6 +76,7 @@ AC_CHECK_HEADERS_ONCE([ \
sys/inotify.h \
sys/pidfd.h
sys/prctl.h \
+ sys/random.h \
sys/shm.h \
sys/timerfd.h \
sys/ustat.h \
diff --git a/include/lapi/getrandom.h b/include/lapi/getrandom.h
index c654ca1ac..706ef9b8f 100644
--- a/include/lapi/getrandom.h
+++ b/include/lapi/getrandom.h
@@ -8,10 +8,14 @@

#include "config.h"

-#if HAVE_LINUX_RANDOM_H
-#include <linux/random.h>
+#ifdef HAVE_SYS_RANDOM_H
+# include <sys/random.h>
+#elif HAVE_LINUX_RANDOM_H
+# include <linux/random.h>
#endif

+#include "lapi/syscalls.h"
+
/*
* Flags for getrandom(2)
*
@@ -27,4 +31,11 @@
# define GRND_RANDOM 0x0002
#endif

+#ifndef HAVE_SYS_RANDOM_H
+static inline int getrandom(void *buf, size_t buflen, unsigned int flags)
+{
+ return tst_syscall(SYS_getrandom, buf, buflen, flags);
+}
+#endif
+
#endif /* LAPI_GETRANDOM_H__ */
diff --git a/testcases/kernel/syscalls/getrandom/getrandom05.c b/testcases/kernel/syscalls/getrandom/getrandom05.c
index 1a9614330..92098deb7 100644
--- a/testcases/kernel/syscalls/getrandom/getrandom05.c
+++ b/testcases/kernel/syscalls/getrandom/getrandom05.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Copyright (c) Linux Test Project, 2024
* Author: Yang Xu <xuyang2018.jy@fujitsu.com>
*/

@@ -13,8 +14,8 @@
* - EINVAL when flag is invalid
*/

-#include <sys/random.h>
#include "tst_test.h"
+#include "lapi/getrandom.h"

static char buff_efault[64];
static char buff_einval[64];
--
2.43.0