hurd: Make getrandom honour GRND_NONBLOCK

Message ID 20191217182929.90989-1-jrtc27@jrtc27.com
State New
Headers show
Series
  • hurd: Make getrandom honour GRND_NONBLOCK
Related show

Commit Message

Jessica Clarke Dec. 17, 2019, 6:29 p.m.
* sysdeps/mach/hurd/getrandom.c (__getrandom): Open the random source
with O_NONBLOCK when the GRND_NONBLOCK flag is provided.
---
 sysdeps/mach/hurd/getrandom.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

--
2.20.1

Comments

Samuel Thibault Dec. 17, 2019, 7:55 p.m. | #1
James Clarke, le mar. 17 déc. 2019 18:29:29 +0000, a ecrit:
> * sysdeps/mach/hurd/getrandom.c (__getrandom): Open the random source

> with O_NONBLOCK when the GRND_NONBLOCK flag is provided.


Applied, thanks!

Samuel

> ---

>  sysdeps/mach/hurd/getrandom.c | 6 +++++-

>  1 file changed, 5 insertions(+), 1 deletion(-)

> 

> diff --git a/sysdeps/mach/hurd/getrandom.c b/sysdeps/mach/hurd/getrandom.c

> index 8bf42aa40d..aac9c6f8c6 100644

> --- a/sysdeps/mach/hurd/getrandom.c

> +++ b/sysdeps/mach/hurd/getrandom.c

> @@ -27,13 +27,17 @@ ssize_t

>  __getrandom (void *buffer, size_t length, unsigned int flags)

>  {

>    const char *random_source = "/dev/urandom";

> +  int open_flags = O_RDONLY | O_CLOEXEC;

>    size_t amount_read;

>    int fd;

> 

>    if (flags & GRND_RANDOM)

>      random_source = "/dev/random";

> 

> -  fd = __open_nocancel(random_source, O_RDONLY | O_CLOEXEC);

> +  if (flags & GRND_NONBLOCK)

> +    open_flags |= O_NONBLOCK;

> +

> +  fd = __open_nocancel(random_source, open_flags);

>    if (fd == -1)

>      return -1;

> 

> --

> 2.20.1

> 


-- 
Samuel
Progress (n.): The process through which the Internet has evolved from
smart people in front of dumb terminals to dumb people in front of smart
terminals.

Patch

diff --git a/sysdeps/mach/hurd/getrandom.c b/sysdeps/mach/hurd/getrandom.c
index 8bf42aa40d..aac9c6f8c6 100644
--- a/sysdeps/mach/hurd/getrandom.c
+++ b/sysdeps/mach/hurd/getrandom.c
@@ -27,13 +27,17 @@  ssize_t
 __getrandom (void *buffer, size_t length, unsigned int flags)
 {
   const char *random_source = "/dev/urandom";
+  int open_flags = O_RDONLY | O_CLOEXEC;
   size_t amount_read;
   int fd;

   if (flags & GRND_RANDOM)
     random_source = "/dev/random";

-  fd = __open_nocancel(random_source, O_RDONLY | O_CLOEXEC);
+  if (flags & GRND_NONBLOCK)
+    open_flags |= O_NONBLOCK;
+
+  fd = __open_nocancel(random_source, open_flags);
   if (fd == -1)
     return -1;