diff options
| author | Simon J. Gerraty <sjg@FreeBSD.org> | 2013-04-12 20:48:55 +0000 |
|---|---|---|
| committer | Simon J. Gerraty <sjg@FreeBSD.org> | 2013-04-12 20:48:55 +0000 |
| commit | 69e6d7b75e96c406d072cb83ffc9b26fbf1a86fb (patch) | |
| tree | 54038c9ac32a45f8741dcc23fb9a8ffc0e15ff89 /lib/libc/stdlib/random.c | |
| parent | 51048477bcc79bcc8753121ec91c150648df3d1b (diff) | |
| parent | 8818042ff2ecd155adb5c248a22de2dbe5d9c2a9 (diff) | |
Notes
Diffstat (limited to 'lib/libc/stdlib/random.c')
| -rw-r--r-- | lib/libc/stdlib/random.c | 42 |
1 files changed, 15 insertions, 27 deletions
diff --git a/lib/libc/stdlib/random.c b/lib/libc/stdlib/random.c index a3c054e5ba0e..4c88ecbbc881 100644 --- a/lib/libc/stdlib/random.c +++ b/lib/libc/stdlib/random.c @@ -34,12 +34,11 @@ static char sccsid[] = "@(#)random.c 8.2 (Berkeley) 5/19/95"; __FBSDID("$FreeBSD$"); #include "namespace.h" -#include <sys/time.h> /* for srandomdev() */ -#include <fcntl.h> /* for srandomdev() */ +#include <sys/param.h> +#include <sys/sysctl.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> -#include <unistd.h> /* for srandomdev() */ #include "un-namespace.h" /* @@ -284,39 +283,28 @@ srandom(unsigned long x) * srandomdev: * * 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. Note that this particular seeding - * procedure can generate states which are impossible to reproduce by - * calling srandom() with any value, since the succeeding terms in the - * state buffer are no longer derived from the LC algorithm applied to - * a fixed seed. + * This often causes problems. We seed the generator using pseudo-random + * data from the kernel. + * + * Note that this particular seeding procedure can generate states + * which are impossible to reproduce by calling srandom() with any + * value, since the succeeding terms in the state buffer are no longer + * derived from the LC algorithm applied to a fixed seed. */ void srandomdev(void) { - int fd, done; + int mib[2]; size_t len; if (rand_type == TYPE_0) - len = sizeof state[0]; + len = sizeof(state[0]); else - len = rand_deg * sizeof state[0]; - - done = 0; - fd = _open("/dev/random", O_RDONLY | O_CLOEXEC, 0); - if (fd >= 0) { - if (_read(fd, (void *) state, len) == (ssize_t) len) - done = 1; - _close(fd); - } + len = rand_deg * sizeof(state[0]); - if (!done) { - struct timeval tv; - - gettimeofday(&tv, NULL); - srandom((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec); - return; - } + mib[0] = CTL_KERN; + mib[1] = KERN_ARND; + sysctl(mib, 2, state, &len, NULL, 0); if (rand_type != TYPE_0) { fptr = &state[rand_sep]; |
