tTry to gather entropy from /dev/random. - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 60d3db8e6bafa21d807ea31690eddf44bb7ef020
 (DIR) parent 551445b92c1f11d4f543e96790ff29762ab1ad10
 (HTM) Author: wkj <devnull@localhost>
       Date:   Wed, 21 Apr 2004 03:06:03 +0000
       
       Try to gather entropy from /dev/random.
       
       Diffstat:
         A src/lib9/truerand.c                 |      21 +++++++++++++++++++++
       
       1 file changed, 21 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/src/lib9/truerand.c b/src/lib9/truerand.c
       t@@ -0,0 +1,21 @@
       +#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);
       +                fcntl(randfd, F_SETFD, FD_CLOEXEC);
       +        }
       +        if(randfd < 0)
       +                sysfatal("can't open /dev/random: %r");
       +        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);
       +}