This makes for one Gnulib module less, and at runtime there’s
typically just one getrandom syscall instead of several for large
nonces.
* gl/lib/randread.c: Include sys/random.h instead of sys/time.h
and unistd.h.
(get_nonce): Use getrandom, not getentropy.
* gl/modules/randread (Depends-on):
Depend on getrandom, not getentropy.
* src/shred.c (main):
* src/shuf.c (main):
* src/sort.c (random_md5_state_init):
Say "getrandom" rather than "getentropy" in (unlikely) diagnostic.
---
 gl/lib/randread.c   | 11 +++++------
 gl/modules/randread |  2 +-
 src/shred.c         |  2 +-
 src/shuf.c          |  2 +-
 src/sort.c          |  2 +-
 5 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/gl/lib/randread.c b/gl/lib/randread.c
index c4d3d7410..afd14f02e 100644
--- a/gl/lib/randread.c
+++ b/gl/lib/randread.c
@@ -35,8 +35,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/time.h>
-#include <unistd.h>
+#include <sys/random.h>
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -148,11 +147,11 @@ get_nonce (void *buffer, size_t bufsize)
   char *buf = buffer, *buflim = buf + bufsize;
   while (buf < buflim)
     {
-      int getentropy_bound = 256;
-      int nbytes = MIN (buflim - buf, getentropy_bound);
-      if (getentropy (buf, nbytes) != 0)
+      ssize_t nbytes = getrandom (buf, buflim - buf, 0);
+      if (0 <= nbytes)
+        buf += nbytes;
+      else if (errno != EINTR)
         return false;
-      buf += nbytes;
     }
   return true;
 }
diff --git a/gl/modules/randread b/gl/modules/randread
index aebe7d962..5c6824038 100644
--- a/gl/modules/randread
+++ b/gl/modules/randread
@@ -12,7 +12,7 @@ error
 exitfail
 inline
 fopen-safer
-getentropy
+getrandom
 quote
 stdalign
 stdbool
diff --git a/src/shred.c b/src/shred.c
index d1743501e..5a9c596e9 100644
--- a/src/shred.c
+++ b/src/shred.c
@@ -1255,7 +1255,7 @@ main (int argc, char **argv)
   randint_source = randint_all_new (random_source, SIZE_MAX);
   if (! randint_source)
     die (EXIT_FAILURE, errno, "%s",
-         quotef (random_source ? random_source : "getentropy"));
+         quotef (random_source ? random_source : "getrandom"));
   atexit (clear_random_data);
 
   for (i = 0; i < n_files; i++)
diff --git a/src/shuf.c b/src/shuf.c
index 51717ff65..ccfe949d4 100644
--- a/src/shuf.c
+++ b/src/shuf.c
@@ -542,7 +542,7 @@ main (int argc, char **argv)
                                      : randperm_bound (ahead_lines, n_lines)));
   if (! randint_source)
     die (EXIT_FAILURE, errno, "%s",
-         quotef (random_source ? random_source : "getentropy"));
+         quotef (random_source ? random_source : "getrandom"));
 
   if (use_reservoir_sampling)
     {
diff --git a/src/sort.c b/src/sort.c
index d689d58dd..242bf66d1 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -2097,7 +2097,7 @@ random_md5_state_init (char const *random_source)
   unsigned char buf[MD5_DIGEST_SIZE];
   struct randread_source *r = randread_new (random_source, sizeof buf);
   if (! r)
-    sort_die (_("open failed"), random_source ? random_source : "getentropy");
+    sort_die (_("open failed"), random_source ? random_source : "getrandom");
   randread (r, buf, sizeof buf);
   if (randread_free (r) != 0)
     sort_die (_("close failed"), random_source);
-- 
2.17.1


Reply via email to