diff options
author | Måns Rullgård <mans@mansr.com> | 2010-06-30 10:38:04 +0000 |
---|---|---|
committer | Måns Rullgård <mans@mansr.com> | 2010-06-30 10:38:04 +0000 |
commit | 38e23c88db9a6b1ce15a2eca431b824f65b214bc (patch) | |
tree | f503843071dfee90612b0ebb2f77803d93c34a95 | |
parent | 534a2231f69638c3fdd998853ba4158ed79322a8 (diff) | |
download | ffmpeg-38e23c88db9a6b1ce15a2eca431b824f65b214bc.tar.gz |
Make av_get_random_seed() non-blocking
Attempt to read from /dev/urandom and /dev/random with O_NONBLOCK set.
If neither succeeds, proceed with fallbacks.
Originally committed as revision 23903 to svn://svn.ffmpeg.org/ffmpeg/trunk
-rw-r--r-- | libavutil/random_seed.c | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/libavutil/random_seed.c b/libavutil/random_seed.c index 00d6317241..a2b388549c 100644 --- a/libavutil/random_seed.c +++ b/libavutil/random_seed.c @@ -24,19 +24,33 @@ #include "random_seed.h" #include "avutil.h" +static int read_random(uint32_t *dst, const char *file) +{ + int fd = open(file, O_RDONLY); + int err = -1; + + if (fd == -1) + return -1; +#if HAVE_FCNTL && defined(O_NONBLOCK) + if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) != -1) +#endif + err = read(fd, dst, sizeof(*dst)); + close(fd); + + return err; +} + uint32_t av_get_random_seed(void) { uint32_t seed; - int fd; - - if ((fd = open("/dev/random", O_RDONLY)) == -1) - fd = open("/dev/urandom", O_RDONLY); - if (fd != -1){ - int err = read(fd, &seed, 4); - close(fd); - if (err == 4) - return seed; - } + int err; + + err = read_random(&seed, "/dev/urandom"); + if (err != sizeof(seed)) + err = read_random(&seed, "/dev/random"); + if (err == sizeof(seed)) + return seed; + #ifdef AV_READ_TIME seed = AV_READ_TIME(); #endif |