From b4ab357e42f53411ccc1aac38ee637de140abc0f Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 1 Jun 2020 15:53:27 -0700 Subject: [PATCH] maint: use getrandom, not getentropy MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 c4d3d7410e..afd14f02e3 100644 --- a/gl/lib/randread.c +++ b/gl/lib/randread.c @@ -35,8 +35,7 @@ #include #include #include -#include -#include +#include #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 aebe7d9628..5c68240387 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 d1743501e8..5a9c596e9e 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 51717ff655..ccfe949d4f 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 d689d58dda..242bf66d19 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.47.2