summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-01-03 02:46:26 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-01-03 02:46:26 +0000
commit49785c0335ec0f1a27ee39f8fddba3f11dc27daa (patch)
treea078908f924168e03f10aa7d6a287fbdf3cc1472
parentce01db512cbb9a7f9e9f9d3e449540f54aee1f78 (diff)
downloadruby-49785c0335ec0f1a27ee39f8fddba3f11dc27daa.tar.gz
ruby-49785c0335ec0f1a27ee39f8fddba3f11dc27daa.tar.xz
ruby-49785c0335ec0f1a27ee39f8fddba3f11dc27daa.zip
* random.c (random_seed): use /dev/urandom if available.
[ruby-dev:25392] git-svn-id: http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8@7714 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--random.c20
2 files changed, 24 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 2f8755605..04ab9220d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Mon Jan 3 11:37:42 2005 Tanaka Akira <akr@m17n.org>
+
+ * random.c (random_seed): use /dev/urandom if available.
+ [ruby-dev:25392]
+
Mon Jan 3 07:46:42 2005 GOTOU Yuuzou <gotoyuzo@notwork.org>
* lib/webrick/httpauth/htpasswd.rb (WEBrick::Htpasswd#reload):
diff --git a/random.c b/random.c
index a317aa5cc..e8fa78b94 100644
--- a/random.c
+++ b/random.c
@@ -144,6 +144,11 @@ genrand_real()
#include <unistd.h>
#endif
#include <time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#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;
}
/*