OSDN Git Service

Use urandom to get random seed
authorak <ak@138bc75d-0d04-0410-961f-82ee72b054a4>
Thu, 29 Sep 2011 13:15:13 +0000 (13:15 +0000)
committerak <ak@138bc75d-0d04-0410-961f-82ee72b054a4>
Thu, 29 Sep 2011 13:15:13 +0000 (13:15 +0000)
When available use /dev/urandom to get the random seem. This will lower the probability
of collisions.

On other systems it will fallback to the old methods.

Passes bootstrap + testsuite on x86_64. Ok?

gcc/:

2011-09-26   Andi Kleen <ak@linux.intel.com>

* toplev.c (init_local_tick): Try reading random seed from /dev/urandom

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@179348 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/toplev.c

index c0c963c..67afcd5 100644 (file)
@@ -1,5 +1,9 @@
 2011-09-26   Andi Kleen <ak@linux.intel.com>
 
+       * toplev.c (init_local_tick): Try reading random seed from /dev/urandom
+
+2011-09-26   Andi Kleen <ak@linux.intel.com>
+
        * hwint.h (HOST_WIDE_INT_PRINT_HEX_PURE): Add.
        * lto-streamer.c (lto_get_section_name): Remove crc32_string.
        Handle numerical random seed.
index 78583fc..ab6b5a4 100644 (file)
@@ -262,7 +262,17 @@ init_local_tick (void)
 {
   if (!flag_random_seed)
     {
-      /* Get some more or less random data.  */
+      /* Try urandom first. Time of day is too likely to collide. 
+        In case of any error we just use the local tick. */
+
+      int fd = open ("/dev/urandom", O_RDONLY);
+      if (fd >= 0)
+        {
+          read (fd, &random_seed, sizeof (random_seed));
+          close (fd);
+        }
+
+      /* Now get the tick anyways  */
 #ifdef HAVE_GETTIMEOFDAY
       {
        struct timeval tv;