diff options
author | Lennart Poettering <lennart@poettering.net> | 2014-10-30 15:27:53 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2014-10-30 15:27:53 +0100 |
commit | 97768fc5746ea97cb2d3ac6854b4fc7ce1c14f24 (patch) | |
tree | 38fc777e924deba4030a40d1ad8c1c5e22ea83e6 /src/shared/util.c | |
parent | e3c72c21d62aadabf4df436c3e2c7219eeeccc1c (diff) |
util: don't block on getrandom()
Diffstat (limited to 'src/shared/util.c')
-rw-r--r-- | src/shared/util.c | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/shared/util.c b/src/shared/util.c index ceafba86af..1d67abe58f 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -2470,10 +2470,17 @@ int dev_urandom(void *p, size_t n) { int r, fd; ssize_t k; - /* Use the syscall unless we know we don't have it, or when - * the requested size is too large for it. */ + /* Gathers some randomness from the kernel. This call will + * never block, and will always return some data from the + * kernel, regardless if the random pool is fully initialized + * or not. It thus makes no guarantee for the quality of the + * returned entropy, but is good enough for or usual usecases + * of seeding the hash functions for hashtable */ + + /* Use the getrandom() syscall unless we know we don't have + * it, or when the requested size is too large for it. */ if (have_syscall != 0 || (size_t) (int) n != n) { - r = getrandom(p, n, 0); + r = getrandom(p, n, GRND_NONBLOCK); if (r == (int) n) { have_syscall = true; return 0; @@ -2481,8 +2488,17 @@ int dev_urandom(void *p, size_t n) { if (r < 0) { if (errno == ENOSYS) - /* we lack the syscall, continue with reading from /dev/urandom */ + /* we lack the syscall, continue with + * reading from /dev/urandom */ have_syscall = false; + else if (errno == EAGAIN) + /* not enough entropy for now. Let's + * remember to use the syscall the + * next time, again, but also read + * from /dev/urandom for now, which + * doesn't care about the current + * amount of entropy. */ + have_syscall = true; else return -errno; } else |