Hi!

----

Attached is a small (unfinished) prototype toy patch
("ksh93_random_use_dev_urandom001.diff.txt") which switches the
discipline functions of ${RANDOM} from |rand()| to /dev/urandom,
resulting in a much better random number generation (/dev/random wasn't
used because it may block when the system's entropy is used-up and wait
until enougth entropy becomes available).

Questions:
- Can I register a C function which gets called at shell shutdown which
can be used to close any file descriptors ?
- Would it be usefull/Ok to add ${RANDOM.min} and ${RANDOM.max} to
define the upper and lower bounds for a random value ?
- How should I handle writing to ${RANDOM} (which normally defines the
seed of |rand()|) when /dev/urandom is used ? The device is read-only on
Solaris for normal users. Should a write failure (to /dev/urandom) be
ignored or handled as error ?

----

Bye,
Roland

-- 
  __ .  . __
 (o.\ \/ /.o) roland.mainz at nrubsig.org
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 7950090
 (;O/ \/ \O;)
-------------- next part --------------
Index: src/lib/libshell/common/sh/init.c
===================================================================
--- src/lib/libshell/common/sh/init.c   (revision 534)
+++ src/lib/libshell/common/sh/init.c   (working copy)
@@ -552,6 +552,27 @@
                np->nvalue.lp = &rp->rand_last;
 }
 
+
+static
+int fetch_dev_random(void)
+{
+    static int randdevfildes = -1; /* bug:we should close this file... */
+    int        randval = 0;
+    
+    if (randdevfildes == -1)
+    {
+        randdevfildes = open("/dev/urandom", O_RDONLY);
+    }
+    
+    if (randdevfildes != -1)
+    {
+        if (read(randdevfildes, &randval, sizeof(randval)) == sizeof(randval))
+           return randval;
+    }
+   
+    return rand();
+}
+
 /*
  * get random number in range of 0 - 2**15
  * never pick same number twice in a row
@@ -561,7 +582,7 @@
        register long cur, last= *np->nvalue.lp;
        NOT_USED(fp);
        do
-               cur = (rand()>>rand_shift)&RANDMASK;
+               cur = (fetch_dev_random()>>rand_shift)&RANDMASK;
        while(cur==last);
        *np->nvalue.lp = cur;
        return((Sfdouble_t)cur);

Reply via email to