Re: /dev/random and nonblocking I/O bug ?

tytso@mit.edu
Thu, 14 Oct 1999 12:57:21 -0400


Date: Tue, 12 Oct 1999 16:09:10 +0200
From: Natapov Gleb <gnatapov@qualcomm.com>

It seems that read from /dev/random can block even if the device was
opened with O_NONBLOCK flag.

Umm, no, it doesn't block for me....

I wrote the following program:
=================================================================
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

void main()
{
int randfd;
char c;
if( (randfd = open("/dev/random", O_RDONLY | O_NONBLOCK)) >= 0){
while(1){
if(read(randfd, &c, 1) != 1){
if( errno != EWOULDBLOCK && errno != 0 )
fprintf(stderr, "read error form random device");
break;
}
fprintf(stderr,".");
}
}
}
=====================================================================

This program is broken. Once you exhaust the pool of available
entropy, read will return 0, and errno will be set to EAGAIN (aka
EWOULDBLOCK).

If the random pool is empty and O_NONBLOCK is set it returns EAGAIN (as
we are expecting) else it calls extract_entropy().

No, it doesn't return EAGAIN; it breaks from the while loop, and after
the whole loop current->state is set back to TASK_RUNNING before it
returns.

- Ted

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/