diff options
| author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-01-03 05:13:18 +0000 |
|---|---|---|
| committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-01-03 05:13:18 +0000 |
| commit | ab7f282e577cb91fc54110bae30e971e2e2280d6 (patch) | |
| tree | 5000cf1e236d0ac6bf9cd905568387516ddec255 /random.c | |
| parent | 87a78bf1fc095b7165e342cf9efbc7ed3be22faf (diff) | |
| download | ruby-ab7f282e577cb91fc54110bae30e971e2e2280d6.tar.gz ruby-ab7f282e577cb91fc54110bae30e971e2e2280d6.tar.xz ruby-ab7f282e577cb91fc54110bae30e971e2e2280d6.zip | |
* random.c (random_seed): don't use /dev/urandom if it is not character device.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@7716 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'random.c')
| -rw-r--r-- | random.c | 18 |
1 files changed, 15 insertions, 3 deletions
@@ -175,17 +175,29 @@ random_seed() unsigned long result; int fd; unsigned long buf; + struct stat statbuf; gettimeofday(&tv, 0); 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)); +#ifdef S_ISCHR + 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); - result ^= buf; } +#endif return result; } |
