mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* random.c (random_seed): don't use /dev/urandom if it is not character device.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7716 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
f0041381da
commit
1b27958bfb
2 changed files with 20 additions and 3 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Mon Jan 3 14:01:54 2005 Tanaka Akira <akr@m17n.org>
|
||||||
|
|
||||||
|
* random.c (random_seed): don't use /dev/urandom if it is not
|
||||||
|
character device.
|
||||||
|
|
||||||
Mon Jan 3 11:37:42 2005 Tanaka Akira <akr@m17n.org>
|
Mon Jan 3 11:37:42 2005 Tanaka Akira <akr@m17n.org>
|
||||||
|
|
||||||
* random.c (random_seed): use /dev/urandom if available.
|
* random.c (random_seed): use /dev/urandom if available.
|
||||||
|
|
18
random.c
18
random.c
|
@ -175,17 +175,29 @@ random_seed()
|
||||||
unsigned long result;
|
unsigned long result;
|
||||||
int fd;
|
int fd;
|
||||||
unsigned long buf;
|
unsigned long buf;
|
||||||
|
struct stat statbuf;
|
||||||
|
|
||||||
gettimeofday(&tv, 0);
|
gettimeofday(&tv, 0);
|
||||||
result = tv.tv_sec ^ tv.tv_usec ^ getpid() ^ n++;
|
result = tv.tv_sec ^ tv.tv_usec ^ getpid() ^ n++;
|
||||||
|
|
||||||
result += (unsigned long)&result;
|
result += (unsigned long)&result;
|
||||||
|
|
||||||
if ((fd = open("/dev/urandom", O_RDONLY)) >= 0) {
|
#ifdef S_ISCHR
|
||||||
read(fd, &buf, sizeof(buf));
|
if ((fd = open("/dev/urandom", O_RDONLY|O_NONBLOCK
|
||||||
|
#ifdef O_NOCTTY
|
||||||
|
|O_NOCTTY
|
||||||
|
#endif
|
||||||
|
#ifdef O_NOFOLLOW
|
||||||
|
|O_NOFOLLOW
|
||||||
|
#endif
|
||||||
|
)) >= 0) {
|
||||||
|
if (fstat(fd, &statbuf) == 0 && S_ISCHR(statbuf.st_mode)) {
|
||||||
|
read(fd, &buf, sizeof(buf));
|
||||||
|
result ^= buf;
|
||||||
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
result ^= buf;
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue