[Mesa-dev] [PATCH] util/rand_xor: seed with getentropy when available
Jonathan Gray
jsg at jsg.id.au
Thu Mar 23 03:22:52 UTC 2017
Instead of using using /dev/urandom on Linux and time(NULL) elsewhere
for a seed first use getentropy() for systems that have a kernel
interface to get a seed such as OpenBSD. This interface is also
present in other systems such as Solaris and even Linux with a recent
version of glibc.
Signed-off-by: Jonathan Gray <jsg at jsg.id.au>
---
configure.ac | 3 +++
src/util/rand_xor.c | 11 +++++++++--
2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/configure.ac b/configure.ac
index bc9c304c8d..1a2f58feaf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -788,6 +788,9 @@ esac
dnl See if posix_memalign is available
AC_CHECK_FUNC([posix_memalign], [DEFINES="$DEFINES -DHAVE_POSIX_MEMALIGN"])
+dnl See if getentropy is available
+AC_CHECK_FUNC([getentropy], [DEFINES="$DEFINES -DHAVE_GETENTROPY"])
+
dnl Check for zlib
PKG_CHECK_MODULES([ZLIB], [zlib >= $ZLIB_REQUIRED])
diff --git a/src/util/rand_xor.c b/src/util/rand_xor.c
index de05fa64b3..b71492b8fd 100644
--- a/src/util/rand_xor.c
+++ b/src/util/rand_xor.c
@@ -22,7 +22,9 @@
*
*/
-#if defined(__linux__)
+#ifdef HAVE_GETENTROPY
+#include <unistd.h>
+#elif defined(__linux__)
#include <sys/file.h>
#include <unistd.h>
#else
@@ -56,7 +58,12 @@ s_rand_xorshift128plus(uint64_t *seed, bool randomised_seed)
if (!randomised_seed)
goto fixed_seed;
-#if defined(__linux__)
+#ifdef HAVE_GETENTROPY
+ size_t seed_size = sizeof(uint64_t) * 2;
+ if (getentropy(seed, seed_size) == -1)
+ goto fixed_seed;
+ return;
+#elif defined(__linux__)
int fd = open("/dev/urandom", O_RDONLY);
if (fd < 0)
goto fixed_seed;
--
2.12.0
More information about the mesa-dev
mailing list