diff options
author | Emmanuel Gil Peyrot <[email protected]> | 2019-09-18 13:20:57 +0200 |
---|---|---|
committer | Emmanuel Gil Peyrot <[email protected]> | 2020-05-15 13:37:20 +0200 |
commit | 4c212a1168de9ffc83a7b8e8751ea7cf3dca5c4a (patch) | |
tree | ae08aa9420c0587a99ca21214fcd59270b8ef958 /src/util | |
parent | cf2b285c5592e5d8fce24ab6a34eaa9c168aa129 (diff) |
util/rand_xor: use getrandom() when available
This function has been added in glibc 2.25, and the related syscall in
Linux 3.17, in order to avoid requiring the /dev/urandom to exist, and
doing the open()/read()/close() dance on it.
We pass GRND_NONBLOCK so that it doesn’t block if not enough entropy has
been gathered to initialise the /dev/urandom source, and fallback to the
next source in any error case.
Signed-off-by: Emmanuel Gil Peyrot <[email protected]>
Reviewed-by: Eric Engestrom <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/2026>
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/rand_xor.c | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/util/rand_xor.c b/src/util/rand_xor.c index de04bbc284f..31612d57660 100644 --- a/src/util/rand_xor.c +++ b/src/util/rand_xor.c @@ -23,6 +23,9 @@ */ #if defined(__linux__) +#if defined(HAVE_GETRANDOM) +#include <sys/random.h> +#endif #include <sys/file.h> #include <unistd.h> #include <fcntl.h> @@ -58,11 +61,18 @@ s_rand_xorshift128plus(uint64_t *seed, bool randomised_seed) goto fixed_seed; #if defined(__linux__) + size_t seed_size = sizeof(uint64_t) * 2; + +#if defined(HAVE_GETRANDOM) + ssize_t ret = getrandom(seed, seed_size, GRND_NONBLOCK); + if (ret == seed_size) + return; +#endif + int fd = open("/dev/urandom", O_RDONLY); if (fd < 0) goto fixed_seed; - size_t seed_size = sizeof(uint64_t) * 2; if (read(fd, seed, seed_size) != seed_size) { close(fd); goto fixed_seed; |