]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: use getrandom, not getentropy
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 1 Jun 2020 22:53:27 +0000 (15:53 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 1 Jun 2020 22:54:50 +0000 (15:54 -0700)
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
gl/modules/randread
src/shred.c
src/shuf.c
src/sort.c

index c4d3d7410e16cfff79938a817ba0623d18b576e9..afd14f02e3812a7d8c57f6a4204f61dccbe2116c 100644 (file)
@@ -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;
 }
index aebe7d96283f07f94ce2b8e9728ac936549d2272..5c682403872fde975073e6c09a425835d31e24ee 100644 (file)
@@ -12,7 +12,7 @@ error
 exitfail
 inline
 fopen-safer
-getentropy
+getrandom
 quote
 stdalign
 stdbool
index d1743501e89cf10057705a222b6b797e4ec1fedd..5a9c596e9e718a0247a0c434388e7537ba3f9df2 100644 (file)
@@ -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++)
index 51717ff655441fc1f332a4ef7b9f43e960dff91f..ccfe949d4fcd04d665314adf51c2d94ab9fa00bd 100644 (file)
@@ -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)
     {
index d689d58dda0d8806e6f990a5f50e50367fdf2510..242bf66d19434bf51c2bfa4002115b0f6379b56d 100644 (file)
@@ -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);