Blame


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