public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Use getentropy() for seeding PRNG
@ 2018-08-03 13:19 Janne Blomqvist
  2018-08-03 13:28 ` Jakub Jelinek
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Janne Blomqvist @ 2018-08-03 13:19 UTC (permalink / raw)
  To: fortran, gcc-patches; +Cc: Janne Blomqvist

The getentropy function, found on Linux, OpenBSD, and recently also
FreeBSD, can be used to get random bytes to initialize the PRNG.  It
is similar to the traditional way of reading from /dev/urandom, but
being a system call rather than a special file, it doesn't suffer from
problems like running out of file descriptors, or failure when running
in a container where /dev/urandom is not available.

Regtested on x86_64-pc-linux-gnu, Ok for trunk?

2018-08-03  Janne Blomqvist  <jb@gcc.gnu.org>

	* configure.ac: Check for getentropy.
	* intrinsics/random.c (getosrandom): Use getentropy if available.
	* config.h.in: Regenerated.
	* configure: Regenerated.
---
 libgfortran/configure.ac        | 3 ++-
 libgfortran/intrinsics/random.c | 7 ++-----
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/libgfortran/configure.ac b/libgfortran/configure.ac
index bf6d3634dda..900c7466dec 100644
--- a/libgfortran/configure.ac
+++ b/libgfortran/configure.ac
@@ -312,7 +312,8 @@ if test "${hardwire_newlib:-0}" -eq 1; then
    fi
 else
    AC_CHECK_FUNCS_ONCE(getrusage times mkstemp strtof strtold snprintf \
-   ftruncate chsize chdir getlogin gethostname kill link symlink sleep ttyname \
+   ftruncate chsize chdir getentropy getlogin gethostname kill link symlink \
+   sleep ttyname \
    alarm access fork setmode fcntl \
    gettimeofday stat fstat lstat getpwuid vsnprintf dup \
    getcwd localtime_r gmtime_r getpwuid_r ttyname_r clock_gettime \
diff --git a/libgfortran/intrinsics/random.c b/libgfortran/intrinsics/random.c
index 234c5ff95fd..1f47f32188b 100644
--- a/libgfortran/intrinsics/random.c
+++ b/libgfortran/intrinsics/random.c
@@ -309,12 +309,9 @@ getosrandom (void *buf, size_t buflen)
   for (size_t i = 0; i < buflen / sizeof (unsigned int); i++)
     rand_s (&b[i]);
   return buflen;
+#elif defined(HAVE_GETENTROPY)
+  return getentropy (buf, buflen);
 #else
-  /*
-     TODO: When glibc adds a wrapper for the getrandom() system call
-     on Linux, one could use that.
-
-     TODO: One could use getentropy() on OpenBSD.  */
   int flags = O_RDONLY;
 #ifdef O_CLOEXEC
   flags |= O_CLOEXEC;
-- 
2.17.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2018-08-14 20:29 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-03 13:19 [PATCH] Use getentropy() for seeding PRNG Janne Blomqvist
2018-08-03 13:28 ` Jakub Jelinek
2018-08-03 14:05   ` Janne Blomqvist
2018-08-13 10:12     ` Janne Blomqvist
2018-08-13 20:07       ` Jakub Jelinek
2018-08-14 20:18       ` Rainer Orth
2018-08-14 20:29         ` Janne Blomqvist
2018-08-03 14:48 ` Paul Koning
2018-08-13 14:36 ` Fritz Reese
2018-08-13 20:12   ` Janne Blomqvist
2018-08-14 13:59     ` Fritz Reese

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).