From 0895a977df0a5afc7f4b8bac85c9463838b8ea04 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Tue, 6 Apr 2021 22:10:14 +0300
Subject: [PATCH 11/15] Enable /dev/urandom support when possible

Add configure check to enable support for using /dev/urandom
as entropy source for randseed.
For cross-compiling the default is to disable support, but
you can override that by setting ac_cv_dev_urandom=yes

See osdn #41918

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 configure.ac       | 12 ++++++++++--
 utility/randseed.c |  4 ++--
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/configure.ac b/configure.ac
index dfcbd5b3b5..fca58bbac6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1324,8 +1324,16 @@ AC_CHECK_LIB([bcrypt], [BCryptGenRandom], [
   UTILITY_LIBS="${UTILITY_LIBS} -lbcrypt" ])
 AC_CHECK_FUNCS([getentropy clock_gettime])
 
-dnl TODO: Reimplement this check, in a cross-compiling friendly way
-dnl AC_CHECK_FILES([/dev/urandom])
+AC_CACHE_VAL([ac_cv_dev_urandom],
+  [if test "x$cross_compiling" != "xyes" ; then
+     AC_CHECK_FILES([/dev/urandom], [ac_cv_dev_urandom=yes], [ac_cv_dev_urandom=no])
+   else
+     dnl Pessimistic default when cross-compiling
+     ac_cv_dev_urandom=no
+   fi])
+if test "x${ac_cv_dev_urandom}" = "xyes" ; then
+  AC_DEFINE([HAVE_USABLE_URANDOM], [1], [Build in /dev/urandom support])
+fi
 
 dnl Possible "-Wmissing-declarations" and "-Werror" will prune out
 dnl cases where we should not use _mkdir() even if it's possible to link against it
diff --git a/utility/randseed.c b/utility/randseed.c
index 961a1d3fee..3a8a4ecaee 100644
--- a/utility/randseed.c
+++ b/utility/randseed.c
@@ -97,7 +97,7 @@ static bool generate_seed_bcryptgenrandom(randseed *ret)
 **************************************************************************/
 static bool generate_seed_urandom(randseed *ret)
 {
-#if HAVE__DEV_URANDOM   /* the first slash turns to an extra underline */
+#if HAVE_USABLE_URANDOM
   /*
    * /dev/urandom should be available on most Unixen. The Wikipedia page
    * mentions Linux, FreeBSD, OpenBSD, macOS as well as Solaris, NetBSD,
@@ -134,7 +134,7 @@ static bool generate_seed_urandom(randseed *ret)
 
     return TRUE;
   }
-#endif /* HAVE__DEV_URANDOM */
+#endif /* HAVE_USABLE_URANDOM */
 
   return FALSE;
 }
-- 
2.30.2