Add getrandom compat for MacOS, FreeBSD and Cygwin
authorMichael Jeanson <mjeanson@efficios.com>
Tue, 18 Apr 2023 21:58:01 +0000 (17:58 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 19 Apr 2023 15:10:36 +0000 (11:10 -0400)
Use the BSD arc4random_buf() function which should be non-blocking on
all of these platforms.

Change-Id: Ib43373cad82373dc83995fdb3d01c2a2d43ab683
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
configure.ac
src/common/random.cpp

index 7257cdeffeee83d722206679a05848924fd4843e..4988aa9e62f88bfc7544ad04c75e0a0a2bdb0d89 100644 (file)
@@ -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
index ec74688ec976df098f60ea330b3a7ae4eb4903e2..26f51d3a4a0a674e19c3721b69ee5ad23eefa2b0 100644 (file)
@@ -72,6 +72,17 @@ void getrandom_nonblock(char *out_data, std::size_t size)
                        errno);
        }
 }
+#elif defined(HAVE_ARC4RANDOM)
+
+#include <stdlib.h>
+
+/*
+ * 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)))
This page took 0.027075 seconds and 4 git commands to generate.