8a7a7b6ff73bcd46172f259c5e630d894ae78ae7
[detour.git] / devurandom.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5
6 /* it's really stupid that there isn't a syscall for this */
7
8 static int fd = -1;
9
10 void randombytes(void *x_,unsigned long long xlen)
11 {
12   unsigned char *x = x_;
13   int i;
14
15   if (fd == -1) {
16     for (;;) {
17       fd = open("/dev/urandom",O_RDONLY);
18       if (fd != -1) break;
19       sleep(1);
20     }
21   }
22
23   while (xlen > 0) {
24     if (xlen < 1048576) i = xlen; else i = 1048576;
25
26     i = read(fd,x,i);
27     if (i < 1) {
28       sleep(1);
29       continue;
30     }
31
32     x += i;
33     xlen -= i;
34   }
35 }