From: Michael Jeanson Date: Tue, 18 Apr 2023 21:58:01 +0000 (-0400) Subject: Add getrandom compat for MacOS, FreeBSD and Cygwin X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=5a23e035fd978f4623b818d45a47ef89bc354579;p=lttng-tools.git Add getrandom compat for MacOS, FreeBSD and Cygwin 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 --- 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)))