[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: |
Bruno Haible |
Subject: |
Re: getrandom: Fail with ENOSYS when the system has no randomness source |
Date: |
Thu, 13 May 2021 21:35:37 +0200 |
User-agent: |
KMail/5.1.3 (Linux/4.4.0-206-generic; KDE/5.18.0; x86_64; ; ) |
Paul Eggert wrote:
> 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.
Good point. Thanks for the correction. Done:
2021-05-13 Bruno Haible <bruno@clisp.org>
getrandom: Produce a better error code.
Reported by Paul Eggert in
<https://lists.gnu.org/archive/html/bug-gnulib/2021-05/msg00025.html>.
* lib/getrandom.c (getrandom): When open() fails with an error that does
not indicate the absence of the file, fail with that error code, not
with ENOSYS.
diff --git a/lib/getrandom.c b/lib/getrandom.c
index 6160118..9e90e64 100644
--- a/lib/getrandom.c
+++ b/lib/getrandom.c
@@ -179,7 +179,8 @@ getrandom (void *buffer, size_t length, unsigned int flags)
fd = open (randdevice[devrandom], oflags);
if (fd < 0)
{
- errno = ENOSYS;
+ if (errno == ENOENT || errno == ENOTDIR)
+ errno = ENOSYS;
return -1;
}
randfd[devrandom] = fd;