2004-04-21 03:06:03 +00:00
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
|
|
|
|
|
|
|
ulong
|
|
|
|
truerand(void)
|
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
uchar buf[sizeof(ulong)];
|
|
|
|
static int randfd = -1;
|
|
|
|
|
|
|
|
if(randfd < 0){
|
|
|
|
randfd = open("/dev/random", OREAD);
|
2005-08-11 03:20:57 +00:00
|
|
|
if(randfd < 0)
|
|
|
|
randfd = open("/dev/srandom", OREAD); /* OpenBSD */
|
|
|
|
if(randfd < 0)
|
|
|
|
sysfatal("can't open /dev/random: %r");
|
2004-04-21 03:06:03 +00:00
|
|
|
fcntl(randfd, F_SETFD, FD_CLOEXEC);
|
|
|
|
}
|
|
|
|
for(i=0; i<sizeof(buf); i += n)
|
|
|
|
if((n = readn(randfd, buf+i, sizeof(buf)-i)) < 0)
|
|
|
|
sysfatal("can't read /dev/random: %r");
|
|
|
|
return *((ulong*)buf);
|
|
|
|
}
|