Re: Inquiry about the removal of flag O_NONBLOCK on /dev/random

Eric Biggers
2022-07-19T03:47:24Z

On Mon, Jul 18, 2022 at 04:52:59PM +0800, Guozihua (Scott) wrote:
> On 2022/7/14 15:33, Guozihua (Scott) wrote:
> > Hi Community,
> >
> > Recently we noticed the removal of flag O_NONBLOCK on /dev/random by
> > commit 30c08efec888 ("random: make /dev/random be almost like
> > /dev/urandom"), it seems that some of the open_source packages e.g.
> > random_get_fd() of util-linux and __getrandom() of glibc. The man page
> > for random() is not updated either.
>
> Correction: I mean various open source packages are still using O_NONBLOCK
> flag while accessing /dev/random
> >
> > Would anyone please kindly provide some background knowledge of this
> > flag and it's removal? Thanks!
> >

This was changed a while ago, in v5.6. /dev/random no longer recognizes
O_NONBLOCK because it no longer blocks, except before the entropy pool has been
initialized.

(I don't know why O_NONBLOCK stopped being recognized *before* the entropy pool
has been initialized; it's either an oversight, or it was decided it doesn't
matter. Probably the latter, since I can't think of a real use case for using
O_NONBLOCK on /dev/random.)

The random(4) man page is indeed in need of an update, not just for this reason
but for some other reasons too.

The util-linux code which you mentioned is opening /dev/random with O_NONBLOCK
if opening /dev/urandom fails, which is pretty much pointless. Perhaps the
author thought that /dev/random with O_NONBLOCK is equivalent to /dev/urandom
(it's not). The glibc code, if you mean sysdeps/mach/hurd/getrandom.c, is
actually code written for GNU Hurd, not for Linux, so it's not relevant.

- Eric