From 5a23e035fd978f4623b818d45a47ef89bc354579 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Tue, 18 Apr 2023 17:58:01 -0400 Subject: [PATCH] Add getrandom compat for MacOS, FreeBSD and Cygwin MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Use the BSD arc4random_buf() function which should be non-blocking on all of these platforms. Change-Id: Ib43373cad82373dc83995fdb3d01c2a2d43ab683 Signed-off-by: Michael Jeanson Signed-off-by: Jérémie Galarneau --- configure.ac | 3 ++- src/common/random.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 7257cdeff..4988aa9e6 100644 --- a/configure.ac +++ b/configure.ac @@ -315,7 +315,8 @@ AC_CHECK_FUNCS([ \ mkdir munmap putenv realpath rmdir socket strchr strcspn strdup \ strncasecmp strndup strnlen strpbrk strrchr strstr strtol strtoul \ strtoull dirfd gethostbyname2 getipnodebyname epoll_create1 \ - sched_getcpu sysconf sync_file_range getrandom posix_fadvise + sched_getcpu sysconf sync_file_range getrandom posix_fadvise \ + arc4random ]) # Check for pthread_setname_np and pthread_getname_np diff --git a/src/common/random.cpp b/src/common/random.cpp index ec74688ec..26f51d3a4 100644 --- a/src/common/random.cpp +++ b/src/common/random.cpp @@ -72,6 +72,17 @@ void getrandom_nonblock(char *out_data, std::size_t size) errno); } } +#elif defined(HAVE_ARC4RANDOM) + +#include + +/* + * According to the MacOS / FreeBSD manpage, this function never fails nor blocks. + */ +void getrandom_nonblock(char *out_data, std::size_t size) +{ + arc4random_buf(out_data, size); +} #else /* defined(__linux__) && defined(SYS_getrandom) && defined(HAVE_SYS_RANDOM_H) */ __attribute__((noreturn)) void getrandom_nonblock(char *out_data __attribute__((unused)), std::size_t size __attribute__((unused))) -- 2.34.1