From fc37921f59380c5ec66dd511d5fdf2274b1237d1 Mon Sep 17 00:00:00 2001 From: Sergey Kandaurov Date: Mon, 16 Jul 2018 14:30:41 +0300 Subject: [PATCH] Added getentropy() support. --- nxt/auto/getrandom | 43 +++++++++++++++++++++++++++++++++++++++++++ nxt/nxt_random.c | 10 ++++++++++ 2 files changed, 53 insertions(+) diff --git a/nxt/auto/getrandom b/nxt/auto/getrandom index 09c52046..5e0e819e 100644 --- a/nxt/auto/getrandom +++ b/nxt/auto/getrandom @@ -46,3 +46,46 @@ if [ $nxt_found = no ]; then }" . ${NXT_AUTO}feature fi + + +if [ $nxt_found = no ]; then + + # OpenBSD 5.6 lacks . + + nxt_feature="getentropy()" + nxt_feature_name=NXT_HAVE_GETENTROPY + nxt_feature_test="#include + + int main(void) { + char buf[4]; + + if (getentropy(buf, 4) == -1) { + return 1; + } + + return 0; + }" + . ${NXT_AUTO}feature +fi + + +if [ $nxt_found = no ]; then + + # macOS 10.12. + + nxt_feature="getentropy() in sys/random.h" + nxt_feature_name=NXT_HAVE_GETENTROPY_SYS_RANDOM + nxt_feature_test="#include + #include + + int main(void) { + char buf[4]; + + if (getentropy(buf, 4) == -1) { + return 1; + } + + return 0; + }" + . ${NXT_AUTO}feature +fi diff --git a/nxt/nxt_random.c b/nxt/nxt_random.c index f99dee08..9e4b8654 100644 --- a/nxt/nxt_random.c +++ b/nxt/nxt_random.c @@ -17,6 +17,8 @@ #elif (NXT_HAVE_LINUX_SYS_GETRANDOM) #include #include +#elif (NXT_HAVE_GETENTROPY_SYS_RANDOM) +#include #endif @@ -76,6 +78,14 @@ nxt_random_stir(nxt_random_t *r, nxt_pid_t pid) n = syscall(SYS_getrandom, &key, NXT_RANDOM_KEY_SIZE, 0); +#elif (NXT_HAVE_GETENTROPY || NXT_HAVE_GETENTROPY_SYS_RANDOM) + + n = 0; + + if (getentropy(&key, NXT_RANDOM_KEY_SIZE) == 0) { + n = NXT_RANDOM_KEY_SIZE; + } + #else n = 0; -- 2.47.3