[PATCH] Make av_get_random_seed() non-blocking

Mans Rullgard mans
Tue Jun 29 15:22:51 CEST 2010


Attempt to read from /dev/urandom and /dev/random with O_NONBLOCK set.
If neither succeeds, proceed with fallbacks.
---
 libavutil/random_seed.c |   32 ++++++++++++++++++++++----------
 1 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/libavutil/random_seed.c b/libavutil/random_seed.c
index 00d6317..7fb1c1f 100644
--- a/libavutil/random_seed.c
+++ b/libavutil/random_seed.c
@@ -24,19 +24,31 @@
 #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 (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) != -1)
+        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 != 4)
+        err = read_random(&seed, "/dev/random");
+    if (err == 4)
+        return seed;
+
 #ifdef AV_READ_TIME
     seed = AV_READ_TIME();
 #endif
-- 
1.7.1


--=-=-=--



More information about the ffmpeg-devel mailing list