From ee63dff2258ff013e6905cb08d3d5e9ad53e3696 Mon Sep 17 00:00:00 2001 From: Nils Maier Date: Wed, 4 Mar 2015 19:40:19 +0100 Subject: [PATCH] Fix getrandom for system with libc not including errno or systems not supporting ENOSYS in the first place. Fixes GH-347 --- src/SimpleRandomizer.cc | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/SimpleRandomizer.cc b/src/SimpleRandomizer.cc index 31784c3e..72b647cf 100644 --- a/src/SimpleRandomizer.cc +++ b/src/SimpleRandomizer.cc @@ -46,6 +46,8 @@ #include "fmt.h" #ifdef HAVE_GETRANDOM_INTERFACE +# include +# include # include "getrandom_linux.h" #endif @@ -95,7 +97,15 @@ void SimpleRandomizer::getRandomBytes(unsigned char* buf, size_t len) static bool have_random_support = true; if (have_random_support) { auto rv = getrandom_linux(buf, len); - if (rv != -1 || errno != ENOSYS) { + if (rv != -1 +#ifdef ENOSYS + /* If the system does not know ENOSYS at this point, just leave the + * check out. If the call failed, we'll not take this branch at all + * and disable support below. + */ + || errno != ENOSYS +#endif + ) { if (rv < -1) { A2_LOG_ERROR(fmt("Failed to produce randomness: %d", errno)); }