rand(3): Fix up sranddev(3) a bit better.
authorSascha Wildner <saw@online.de>
Tue, 25 Feb 2014 22:50:13 +0000 (23:50 +0100)
committerSascha Wildner <saw@online.de>
Tue, 25 Feb 2014 23:09:08 +0000 (00:09 +0100)
In case we can't read /dev/random for the seed, try kern.random. If that
fails, use the getpid()/gettimeofday() xor. For the latter, remove the
usage of an uninitialized variable.

We should probably just abort() when kern.random can't be read.

Discussed-with: dillon

lib/libc/stdlib/rand.c

index aaf73d4..8b36e09 100644 (file)
@@ -35,6 +35,7 @@
 #include "namespace.h"
 #include <sys/time.h>          /* for sranddev() */
 #include <sys/types.h>
+#include <sys/sysctl.h>
 #include <fcntl.h>             /* for sranddev() */
 #include <stdlib.h>
 #include <unistd.h>            /* for sranddev() */
@@ -115,12 +116,18 @@ sranddev(void)
                _close(fd);
        }
 
+       if (!done) {
+               size_t len = sizeof(next);
+
+               if (sysctlbyname("kern.random", &next, &len, NULL, 0) == 0)
+                       done = 1;
+       }
+               
        if (!done) {
                struct timeval tv;
-               unsigned long junk;     /* XXX left uninitialized on purpose */
 
                gettimeofday(&tv, NULL);
-               srand((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ junk);
+               srand((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec);
        }
 }