From 9b261c008a395a323e0516f4cd3f3134aa050577 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 8 Jun 2009 19:06:16 +1000 Subject: s4:heimdal: import lorikeet-heimdal-200906080040 (commit 904d0124b46eed7a8ad6e5b73e892ff34b6865ba) Also including the supporting changes required to pass make test A number of heimdal functions and constants have changed since we last imported a tree (for the better, but inconvenient for us). Andrew Bartlett --- source4/heimdal/lib/hcrypto/rand-unix.c | 40 ++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 10 deletions(-) (limited to 'source4/heimdal/lib/hcrypto/rand-unix.c') diff --git a/source4/heimdal/lib/hcrypto/rand-unix.c b/source4/heimdal/lib/hcrypto/rand-unix.c index 0c2185776c..07d81eb620 100644 --- a/source4/heimdal/lib/hcrypto/rand-unix.c +++ b/source4/heimdal/lib/hcrypto/rand-unix.c @@ -40,11 +40,15 @@ RCSID("$Id$"); #include #include #include +#include #include #include "randi.h" +static int random_fd = -1; +static HEIMDAL_MUTEX random_mutex = HEIMDAL_MUTEX_INITIALIZER; + /* * Unix /dev/random */ @@ -88,31 +92,47 @@ unix_seed(const void *indata, int size) } + static int unix_bytes(unsigned char *outdata, int size) { ssize_t count; - int fd; + int once = 0; if (size <= 0) return 0; - fd = get_device_fd(O_RDONLY); - if (fd < 0) - return 0; + HEIMDAL_MUTEX_lock(&random_mutex); + if (random_fd == -1) { + retry: + random_fd = get_device_fd(O_RDONLY); + if (random_fd < 0) { + HEIMDAL_MUTEX_unlock(&random_mutex); + return 0; + } + } while (size > 0) { - count = read (fd, outdata, size); - if (count < 0 && errno == EINTR) - continue; - else if (count <= 0) { - close(fd); + HEIMDAL_MUTEX_unlock(&random_mutex); + count = read (random_fd, outdata, size); + HEIMDAL_MUTEX_lock(&random_mutex); + if (random_fd < 0) { + if (errno == EINTR) + continue; + else if (errno == EBADF && once++ == 0) { + close(random_fd); + random_fd = -1; + goto retry; + } + return 0; + } else if (count <= 0) { + HEIMDAL_MUTEX_unlock(&random_mutex); return 0; } outdata += count; size -= count; } - close(fd); + HEIMDAL_MUTEX_unlock(&random_mutex); return 1; } -- cgit