diff options
Diffstat (limited to 'lib/libc/stdlib/rand.c')
-rw-r--r-- | lib/libc/stdlib/rand.c | 33 |
1 files changed, 12 insertions, 21 deletions
diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c index 7041818..0cbd948 100644 --- a/lib/libc/stdlib/rand.c +++ b/lib/libc/stdlib/rand.c @@ -36,11 +36,10 @@ static char sccsid[] = "@(#)rand.c 8.1 (Berkeley) 6/14/93"; __FBSDID("$FreeBSD$"); #include "namespace.h" -#include <sys/time.h> /* for sranddev() */ +#include <sys/param.h> +#include <sys/sysctl.h> #include <sys/types.h> -#include <fcntl.h> /* for sranddev() */ #include <stdlib.h> -#include <unistd.h> /* for sranddev() */ #include "un-namespace.h" #ifdef TEST @@ -112,28 +111,20 @@ u_int seed; * sranddev: * * Many programs choose the seed value in a totally predictable manner. - * This often causes problems. We seed the generator using the much more - * secure random(4) interface. + * This often causes problems. We seed the generator using pseudo-random + * data from the kernel. */ void sranddev() { - int fd, done; - - done = 0; - fd = _open("/dev/random", O_RDONLY | O_CLOEXEC, 0); - if (fd >= 0) { - if (_read(fd, (void *) &next, sizeof(next)) == sizeof(next)) - done = 1; - _close(fd); - } - - if (!done) { - struct timeval tv; - - gettimeofday(&tv, NULL); - srand((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec); - } + int mib[2]; + size_t len; + + len = sizeof(next); + + mib[0] = CTL_KERN; + mib[1] = KERN_ARND; + sysctl(mib, 2, (void *)&next, &len, NULL, 0); } |