rand/noise.c: Order <signal.h> properly.
[catacomb] / rand / noise.c
index 6ab1070..6cab8ca 100644 (file)
@@ -30,9 +30,9 @@
 #include "config.h"
 
 #include <setjmp.h>
+#include <signal.h>
 #include <stdio.h>
 #include <string.h>
-#include <signal.h>
 
 #include <sys/types.h>
 #include <sys/time.h>
@@ -113,6 +113,7 @@ static int timer(rand_pool *r, struct timeval *tv)
   x = tv->tv_usec + MILLION * tv->tv_sec;
   d = x ^ noise_last;
   dd = d ^ noise_diff;
+  noise_last = x;
   noise_diff = d;
   de = bitcount(d);
   dde = bitcount(dd);
@@ -156,6 +157,7 @@ int noise_devrandom(rand_pool *r)
   int fd;
   octet buf[RAND_POOLSZ];
   ssize_t len;
+  size_t n = 0;
   int ret = 0;
 
   /* --- Be nice to other clients of the random device --- *
@@ -169,11 +171,13 @@ int noise_devrandom(rand_pool *r)
   if ((fd = open("/dev/urandom", O_RDONLY | O_NONBLOCK)) >= 0 ||
       (fd = open("/dev/arandom", O_RDONLY | O_NONBLOCK)) >= 0 ||
       (fd = open("/dev/random", O_RDONLY | O_NONBLOCK)) >= 0) {
-    if ((len = read(fd, buf, sizeof(buf))) > 0) {
-      rand_add(r, buf, len, len * 8);
-      BURN(buf);
-      ret = 1;
+    while (n < sizeof(buf)) {
+      if ((len = read(fd, buf + n, sizeof(buf) - n)) <= 0) break;
+      n += len;
     }
+    rand_add(r, buf, n, n * 8);
+    BURN(buf);
+    if (n == sizeof(buf)) ret = 1;
     close(fd);
   }
   noise_timer(r);