Skip to content
Snippets Groups Projects
Commit 38e23c88 authored by Måns Rullgård's avatar Måns Rullgård
Browse files

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
parent 534a2231
No related branches found
No related tags found
No related merge requests found
...@@ -24,19 +24,33 @@ ...@@ -24,19 +24,33 @@
#include "random_seed.h" #include "random_seed.h"
#include "avutil.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 av_get_random_seed(void)
{ {
uint32_t seed; uint32_t seed;
int fd; int err;
if ((fd = open("/dev/random", O_RDONLY)) == -1) err = read_random(&seed, "/dev/urandom");
fd = open("/dev/urandom", O_RDONLY); if (err != sizeof(seed))
if (fd != -1){ err = read_random(&seed, "/dev/random");
int err = read(fd, &seed, 4); if (err == sizeof(seed))
close(fd); return seed;
if (err == 4)
return seed;
}
#ifdef AV_READ_TIME #ifdef AV_READ_TIME
seed = AV_READ_TIME(); seed = AV_READ_TIME();
#endif #endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment