From e5abd52ab205ffa58f5756342df5b8fa91c02def Mon Sep 17 00:00:00 2001 From: akr Date: Mon, 3 Jan 2005 02:41:03 +0000 Subject: [PATCH] * random.c (random_seed): use /dev/urandom if available. [ruby-dev:25392] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7713 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ random.c | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index a3cf5489f3..8e207e7d53 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Jan 3 11:37:42 2005 Tanaka Akira + + * random.c (random_seed): use /dev/urandom if available. + [ruby-dev:25392] + Mon Jan 3 11:03:37 2005 Masatoshi SEKI * test/drb/test_drb.rb: move TestDRbReusePort to new file. diff --git a/random.c b/random.c index a317aa5cc1..e8fa78b944 100644 --- a/random.c +++ b/random.c @@ -144,6 +144,11 @@ genrand_real() #include #endif #include +#include +#include +#ifdef HAVE_FCNTL_H +#include +#endif static int first = 1; @@ -167,9 +172,22 @@ random_seed() { static int n = 0; struct timeval tv; + unsigned long result; + int fd; + unsigned long buf; gettimeofday(&tv, 0); - return tv.tv_sec ^ tv.tv_usec ^ getpid() ^ n++; + result = tv.tv_sec ^ tv.tv_usec ^ getpid() ^ n++; + + result += (unsigned long)&result; + + if ((fd = open("/dev/urandom", O_RDONLY)) >= 0) { + read(fd, &buf, sizeof(buf)); + close(fd); + result ^= buf; + } + + return result; } /*