From 15d1af8951f68b342a5d093af1a09e65b7d4fd34 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Sun, 19 Jun 2022 10:31:39 -0700 Subject: [PATCH] xgetrandom: fix macOS build. BSD has first, but has getentropy() in there. glibc has getentropy() in addition to getrandom(), but they keep it in instead. Since both went in in glibc 2.25 I don't think this will matter in practice, but if this does cause trouble, we can have separate getentropy() and getrandom() implementations for the BSDs versus glibc. This does mean that the only valid values for `flags` are now 0 or WARN_ONLY, but that was already effectively true anyway because GRND_NONBLOCK != O_NONBLOCK so the fallback path was already broken. --- lib/portability.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/portability.c b/lib/portability.c index 0523d9f3..58ef384b 100644 --- a/lib/portability.c +++ b/lib/portability.c @@ -34,8 +34,12 @@ int xgetrandom(void *buf, unsigned buflen, unsigned flags) { int fd; + // Linux keeps getrandom() in and getentropy() in + // BSD/macOS only has getentropy(), but it's in (to be fair, + // they were there first). getrandom() and getentropy() both went into glibc + // in the same release (2.25 in 2017), so this test still works. #if __has_include() - if (buflen == getrandom(buf, buflen, flags&~WARN_ONLY)) return 1; + if (!getentropy(buf, buflen)) return 1; if (errno!=ENOSYS && !(flags&WARN_ONLY)) perror_exit("getrandom"); #endif fd = xopen(flags ? "/dev/random" : "/dev/urandom",O_RDONLY|(flags&WARN_ONLY)); -- 2.39.2