From: Nick Mathewson Date: Sat, 8 May 2010 19:31:54 +0000 (-0400) Subject: Fix some crazy macro mistakes in arc4random.c X-Git-Tag: release-2.0.5-beta~17 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=90d4225137d00bc58eeee82f1a5fa4272dc17bd1;p=libevent Fix some crazy macro mistakes in arc4random.c --- diff --git a/arc4random.c b/arc4random.c index c75b5d79..21334b47 100644 --- a/arc4random.c +++ b/arc4random.c @@ -201,6 +201,47 @@ arc4_seed_sysctl_linux(void) } #endif +#if _EVENT_HAVE_DECL_CTL_KERN && _EVENT_HAVE_DECL_KERN_ARND +#define TRY_SEED_SYSCTL_BSD +static int +arc4_seed_sysctl_bsd(void) +{ + /* Based on code from William Ahern and from OpenBSD, this function + * tries to use the KERN_ARND syscall to get entropy from the kernel. + * This can work even if /dev/urandom is inaccessible for some reason + * (e.g., we're running in a chroot). */ + int mib[] = { CTL_KERN, KERN_ARND }; + unsigned char buf[ADD_ENTROPY]; + size_t len, n; + int i, any_set; + + memset(buf, 0, sizeof(buf)); + + len = sizeof(buf); + if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { + for (len = 0; len < sizeof(buf); len += sizeof(unsigned)) { + n = sizeof(unsigned); + if (n + len > sizeof(buf)) + n = len - sizeof(buf); + if (sysctl(mib, 2, &buf[len], &n, NULL, 0) == -1) + return -1; + } + } + /* make sure that the buffer actually got set. */ + for (i=any_set=0; i sizeof(buf)) - n = len - sizeof(buf); - if (sysctl(mib, 2, &buf[len], &n, NULL, 0) == -1) - return -1; - } - } - /* make sure that the buffer actually got set. */ - for (i=any_set=0; i