diff options
author | Ondřej Surý <ondrej@sury.org> | 2016-08-30 10:25:36 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2016-08-30 10:25:36 +0200 |
commit | 7d6202bfa85e8f0bf90590a9381757f15e296a33 (patch) | |
tree | 1949eb9256ab93dbe512a37b1e75720dbdce5e8e /src | |
parent | 6de837ba4e208260ac6043d521b0a1d79ffd58a7 (diff) | |
download | luaossl-7d6202bfa85e8f0bf90590a9381757f15e296a33.tar.gz luaossl-7d6202bfa85e8f0bf90590a9381757f15e296a33.tar.bz2 luaossl-7d6202bfa85e8f0bf90590a9381757f15e296a33.zip |
Reinstate sysctl call for older Linux kernels
Diffstat (limited to 'src')
-rw-r--r-- | src/openssl.c | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/src/openssl.c b/src/openssl.c index 6addcaa..7c28c84 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -60,6 +60,8 @@ #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) #define HAVE_GETRANDOM #include <linux/random.h> +#else +#define HAVE_SYS_SYSCTL_H #endif #endif @@ -7824,13 +7826,20 @@ static struct randL_state *randL_getstate(lua_State *L) { return lua_touserdata(L, lua_upvalueindex(1)); } /* randL_getstate() */ +#if HAVE_SYS_SYSCTL_H +#include <sys/sysctl.h> /* CTL_KERN KERN_RANDOM RANDOM_UUID KERN_URND KERN_ARND sysctl(2) */ +#endif + +#ifndef HAVE_RANDOM_UUID +#define HAVE_RANDOM_UUID (HAVE_SYS_SYSCTL_H && defined __linux__) /* RANDOM_UUID is an enum, not macro */ +#endif static int randL_stir(struct randL_state *st, unsigned rqstd) { unsigned count = 0; int error; unsigned char data[256]; -#if HAVE_ARC4RANDOM - while (count < rqst) { +#if defined(HAVE_ARC4RANDOM) + while (count < rqstd) { size_t n = MIN(rqstd - count, sizeof data); arc4random(data, n); @@ -7839,22 +7848,34 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { count += n; } -#endif - -#if HAVE_GETRANDOM - while (count < rqst) { +#elif defined(HAVE_GETRANDOM) + while (count < rqstd) { size_t n = MIN(rqstd - count, sizeof data); n = getrandom(data, n, 0); if (n == -1) { - break; + break; } - RAND_add(data, n, n); + RAND_add(data, n, n); + + count += n; + } +#elif HAVE_RANDOM_UUID + int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID }; + + while (count < rqstd) { + size_t n = MIN(rqstd - count, sizeof data); + + if (0 != sysctl(mib, countof(mib), data, &n, (void *)0, 0)) + break; + + RAND_add(data, n, n); count += n; } + #endif if (count < rqstd) { |