Browse Source

Merge #10301: Check if sys/random.h is required for getentropy.

ee2d10a Check if sys/random.h is required for getentropy on OSX. (James Hilliard)

Pull request description:

  This should check and include sys/random.h if required for osx as mentioned [here](https://github.com/bitcoin/bitcoin/pull/9821#issuecomment-290936636).

Tree-SHA512: e9491f67f2e8b2e6bcdbcbb8063295e844d5627daf5336e3e17b4a8027d888fa65a08e4580a745abdc35ffd8d86b4fc7434daaac172c4a06ab7566a2ed0bfb92
0.15
Wladimir J. van der Laan 7 years ago
parent
commit
318392ca7c
No known key found for this signature in database
GPG Key ID: 1E4AED62986CD25D
  1. 8
      configure.ac
  2. 14
      src/random.cpp

8
configure.ac

@ -674,6 +674,14 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>]],
[ AC_MSG_RESULT(no)] [ AC_MSG_RESULT(no)]
) )
AC_MSG_CHECKING(for getentropy via random.h)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
#include <sys/random.h>]],
[[ getentropy(nullptr, 32) ]])],
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_GETENTROPY_RAND, 1,[Define this symbol if the BSD getentropy system call is available with sys/random.h]) ],
[ AC_MSG_RESULT(no)]
)
AC_MSG_CHECKING(for sysctl KERN_ARND) AC_MSG_CHECKING(for sysctl KERN_ARND)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h> AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
#include <sys/sysctl.h>]], #include <sys/sysctl.h>]],

14
src/random.cpp

@ -27,9 +27,12 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#include <linux/random.h> #include <linux/random.h>
#endif #endif
#ifdef HAVE_GETENTROPY #if defined(HAVE_GETENTROPY) || (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX))
#include <unistd.h> #include <unistd.h>
#endif #endif
#if defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)
#include <sys/random.h>
#endif
#ifdef HAVE_SYSCTL_ARND #ifdef HAVE_SYSCTL_ARND
#include <sys/sysctl.h> #include <sys/sysctl.h>
#endif #endif
@ -237,6 +240,15 @@ void GetOSRand(unsigned char *ent32)
if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) { if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) {
RandFailure(); RandFailure();
} }
#elif defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)
// We need a fallback for OSX < 10.12
if (&getentropy != NULL) {
if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) {
RandFailure();
}
} else {
GetDevURandom(ent32);
}
#elif defined(HAVE_SYSCTL_ARND) #elif defined(HAVE_SYSCTL_ARND)
/* FreeBSD and similar. It is possible for the call to return less /* FreeBSD and similar. It is possible for the call to return less
* bytes than requested, so need to read in a loop. * bytes than requested, so need to read in a loop.

Loading…
Cancel
Save