This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH 2/3] Use urandom to get random seed
- From: Andi Kleen <andi at firstfloor dot org>
- To: gcc-patches at gcc dot gnu dot org
- Cc: Andi Kleen <ak at linux dot intel dot com>
- Date: Wed, 28 Sep 2011 14:49:22 +0200
- Subject: [PATCH 2/3] Use urandom to get random seed
- References: <1317214163-26010-1-git-send-email-andi@firstfloor.org>
From: Andi Kleen <ak@linux.intel.com>
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
---
gcc/toplev.c | 12 +++++++++++-
1 files changed, 11 insertions(+), 1 deletions(-)
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 78583fc..ab6b5a4 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -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;
--
1.7.5.4