[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: getrandom: Fail with ENOSYS when the system has no randomness source
From: |
Paul Eggert |
Subject: |
Re: getrandom: Fail with ENOSYS when the system has no randomness source |
Date: |
Mon, 10 May 2021 02:06:34 -0700 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 |
On 5/9/21 7:51 AM, Bruno Haible wrote:
if (fd < 0)
- return fd;
+ {
+ errno = ENOSYS;
+ return -1;
+ }
If 'open' fails with errno equal to (say) EINTR or EAGAIN or EMFILE,
this doesn't mean the operating system lacks a randomness source; it
merely means the 'open' failed. And the libc manual allows getrandom to
fail with EMFILE or with any other valid error number. (FWIW, the Hurd
implementation of getrandom simply passes the errno of 'open' through.)
How about if we instead change that code to something like this:
if (fd < 0)
{
if (errno == ENOENT || errno == ENOTDIR)
errno = ENOSYS;
return fd;
}
That is, if /dev/random (or whatever) doesn't exist, we assume the OS is
like IRIX and lacks randomness support, so we fail with ENOSYS;
otherwise we pass errno through as that's more useful to the caller.