[FFmpeg-devel] [PATCH] Make av_get_random_seed not block when waiting for more entropy

Måns Rullgård mans
Tue Jun 29 15:24:53 CEST 2010


Martin Storsj? <martin at martin.st> writes:

> On Tue, 29 Jun 2010, M?ns Rullg?rd wrote:
>
>> Martin Storsj? <martin at martin.st> writes:
>> 
>> > Hi,
>> >
>> > I recently ran into a situation where av_get_random_seed() blocks for long 
>> > times if a machine is out of entropy. Since most (all?) uses of 
>> > av_get_random_seed() within ffmpeg don't really need full cryptographic 
>> > strength, using /dev/urandom instead of blocking while waiting for data 
>> > from /dev/random - the attached patch does this.
>> >
>> > // Martin
>> >
>> > From d793a93fd6f8504e9a454c109ca992fb59b2c104 Mon Sep 17 00:00:00 2001
>> > From: Martin Storsjo <martin at martin.st>
>> > Date: Tue, 29 Jun 2010 15:21:35 +0300
>> > Subject: [PATCH] Make av_get_random_seed not block when waiting for more entropy
>> >
>> > The /dev/random device currently used for gathering entropy may block
>> > for (very) long times if the kernel is out of entropy.
>> > ---
>> >  libavutil/random_seed.c |    1 -
>> >  1 files changed, 0 insertions(+), 1 deletions(-)
>> >
>> > diff --git a/libavutil/random_seed.c b/libavutil/random_seed.c
>> > index 00d6317..7f82a4f 100644
>> > --- a/libavutil/random_seed.c
>> > +++ b/libavutil/random_seed.c
>> > @@ -29,7 +29,6 @@ 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);
>> 
>> Neither /dev/random nor /dev/urandom are standard devices and may or
>> may not be available on a given system.  Attempting both is probably a
>> good idea.
>
> Good point, updated patch attached.
>
> From 5c57ef83b1ee46baf6153f2a930a327633eb4273 Mon Sep 17 00:00:00 2001
> From: Martin Storsjo <martin at martin.st>
> Date: Tue, 29 Jun 2010 15:21:35 +0300
> Subject: [PATCH] Make av_get_random_seed not block when waiting for more entropy
>
> The /dev/random device currently used for gathering entropy may block
> for (very) long times if the kernel is out of entropy. By preferring
> /dev/urandom over /dev/random, we avoid blocking, if both alternatives
> are available.

This will still block if urandom isn't available.  How about this?

-- 
M?ns Rullg?rd
mans at mansr.com



More information about the ffmpeg-devel mailing list