From patchwork Wed Sep 28 12:49:22 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [2/3] Use urandom to get random seed Date: Wed, 28 Sep 2011 02:49:22 -0000 From: Andi Kleen X-Patchwork-Id: 116783 Message-Id: <1317214163-26010-3-git-send-email-andi@firstfloor.org> To: gcc-patches@gcc.gnu.org Cc: Andi Kleen From: Andi Kleen 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 * 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;