Skip to content

Commit 318392c

Browse files
committed
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](bitcoin/bitcoin#9821 (comment)). Tree-SHA512: e9491f67f2e8b2e6bcdbcbb8063295e844d5627daf5336e3e17b4a8027d888fa65a08e4580a745abdc35ffd8d86b4fc7434daaac172c4a06ab7566a2ed0bfb92
2 parents fa64636 + ee2d10a commit 318392c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

configure.ac

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,14 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>]],
674674
[ AC_MSG_RESULT(no)]
675675
)
676676

677+
AC_MSG_CHECKING(for getentropy via random.h)
678+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
679+
#include <sys/random.h>]],
680+
[[ getentropy(nullptr, 32) ]])],
681+
[ 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]) ],
682+
[ AC_MSG_RESULT(no)]
683+
)
684+
677685
AC_MSG_CHECKING(for sysctl KERN_ARND)
678686
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
679687
#include <sys/sysctl.h>]],

src/random.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
#include <sys/syscall.h>
2828
#include <linux/random.h>
2929
#endif
30-
#ifdef HAVE_GETENTROPY
30+
#if defined(HAVE_GETENTROPY) || (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX))
3131
#include <unistd.h>
3232
#endif
33+
#if defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)
34+
#include <sys/random.h>
35+
#endif
3336
#ifdef HAVE_SYSCTL_ARND
3437
#include <sys/sysctl.h>
3538
#endif
@@ -237,6 +240,15 @@ void GetOSRand(unsigned char *ent32)
237240
if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) {
238241
RandFailure();
239242
}
243+
#elif defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)
244+
// We need a fallback for OSX < 10.12
245+
if (&getentropy != NULL) {
246+
if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) {
247+
RandFailure();
248+
}
249+
} else {
250+
GetDevURandom(ent32);
251+
}
240252
#elif defined(HAVE_SYSCTL_ARND)
241253
/* FreeBSD and similar. It is possible for the call to return less
242254
* bytes than requested, so need to read in a loop.

0 commit comments

Comments
 (0)