2023-10-25 19:48:06

by Petr Vorel

[permalink] [raw]
Subject: [PATCH 3/3] support/backend_sqlite.c: Add getrandom() fallback

Allow to compile reexport on systems with older libc. (getrandom()
wrapper is supported on glibc 2.25+ and musl 1.1.20+, uclibc-ng does
not yet support it).

getrandom() syscall is supported Linux 3.17+ (old enough to bother with
a check).

Signed-off-by: Petr Vorel <[email protected]>
---
Makefile.am | 1 +
aclocal/getrandom.m4 | 16 ++++++++++++++++
configure.ac | 3 +++
support/reexport/backend_sqlite.c | 18 +++++++++++++++++-
4 files changed, 37 insertions(+), 1 deletion(-)
create mode 100644 aclocal/getrandom.m4

diff --git a/Makefile.am b/Makefile.am
index 00220842..72ad4ba7 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -10,6 +10,7 @@ EXTRA_DIST = \
autogen.sh \
\
aclocal/bsdsignals.m4 \
+ aclocal/getrandom.m4 \
aclocal/nfs-utils.m4 \
aclocal/kerberos5.m4 \
aclocal/tcp-wrappers.m4 \
diff --git a/aclocal/getrandom.m4 b/aclocal/getrandom.m4
new file mode 100644
index 00000000..bc0fe16a
--- /dev/null
+++ b/aclocal/getrandom.m4
@@ -0,0 +1,16 @@
+dnl Checks for getrandom support (glibc 2.25+, musl 1.1.20+)
+dnl
+AC_DEFUN([AC_GETRANDOM], [
+ AC_MSG_CHECKING(for getrandom())
+ AC_LINK_IFELSE(
+ [AC_LANG_PROGRAM([[
+ #include <stdlib.h> /* for NULL */
+ #include <sys/random.h>
+ ]],
+ [[ return getrandom(NULL, 0U, 0U); ]] )],
+ [AC_DEFINE([HAVE_GETRANDOM], [1], [Define to 1 if you have the `getrandom' function.])
+ AC_MSG_RESULT([yes])],
+ [AC_MSG_RESULT([no])])
+
+ AC_SUBST(HAVE_GETRANDOM)
+])
diff --git a/configure.ac b/configure.ac
index 6fbcb974..4bff679d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -277,6 +277,9 @@ AC_TCP_WRAPPERS
# Arrange for large-file support
AC_SYS_LARGEFILE

+dnl Check for getrandom() libc support
+AC_GETRANDOM
+
AC_CONFIG_SRCDIR([support/include/config.h.in])
AC_CONFIG_HEADERS([support/include/config.h])

diff --git a/support/reexport/backend_sqlite.c b/support/reexport/backend_sqlite.c
index 132f30c4..0eb5ea37 100644
--- a/support/reexport/backend_sqlite.c
+++ b/support/reexport/backend_sqlite.c
@@ -7,9 +7,16 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/random.h>
#include <unistd.h>

+#ifdef HAVE_GETRANDOM
+# include <sys/random.h>
+# if !defined(SYS_getrandom) && defined(__NR_getrandom)
+ /* usable kernel-headers, but old glibc-headers */
+# define SYS_getrandom __NR_getrandom
+# endif
+#endif
+
#include "conffile.h"
#include "reexport_backend.h"
#include "xlog.h"
@@ -20,6 +27,15 @@
static sqlite3 *db;
static int init_done;

+#if !defined(HAVE_GETRANDOM) && defined(SYS_getrandom)
+/* libc without function, but we have syscall */
+static int getrandom(void *buf, size_t buflen, unsigned int flags)
+{
+ return (syscall(SYS_getrandom, buf, buflen, flags));
+}
+# define HAVE_GETRANDOM
+#endif
+
static int prng_init(void)
{
int seed;
--
2.42.0