Blob


1 #include <u.h>
2 #include <libc.h>
4 ulong
5 truerand(void)
6 {
7 int i, n;
8 uchar buf[sizeof(ulong)];
9 static int randfd = -1;
10 static char *randfile;
12 if(randfd < 0){
13 randfd = open(randfile="/dev/random", OREAD);
14 /* OpenBSD lets you open /dev/random but not read it! */
15 if(randfd < 0 || read(randfd, buf, 1) != 1)
16 randfd = open(randfile="/dev/srandom", OREAD); /* OpenBSD */
17 if(randfd < 0)
18 sysfatal("can't open %s: %r", randfile);
19 fcntl(randfd, F_SETFD, FD_CLOEXEC);
20 }
21 for(i=0; i<sizeof(buf); i += n)
22 if((n = readn(randfd, buf+i, sizeof(buf)-i)) < 0)
23 sysfatal("can't read %s: %r", randfile);
24 return *((ulong*)buf);
25 }