diff options
author | Janne Blomqvist <jb@gcc.gnu.org> | 2018-08-13 23:21:50 +0300 |
---|---|---|
committer | Janne Blomqvist <jb@gcc.gnu.org> | 2018-08-13 23:21:50 +0300 |
commit | d66d6d15f1ab42c503025548d6ab9772a85ad791 (patch) | |
tree | 0cb0150a31538dfc107e19dbeff59fef4420ae6f /libgfortran/intrinsics/random.c | |
parent | 72217988ca377398e5c7c1ae98b83ca53b1877d4 (diff) | |
download | gcc-d66d6d15f1ab42c503025548d6ab9772a85ad791.zip gcc-d66d6d15f1ab42c503025548d6ab9772a85ad791.tar.gz gcc-d66d6d15f1ab42c503025548d6ab9772a85ad791.tar.bz2 |
Use getentropy() for seeding PRNG
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 may not be available.
Regtested on x86_64-pc-linux-gnu, Ok for trunk?
2018-08-13 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.
From-SVN: r263522
Diffstat (limited to 'libgfortran/intrinsics/random.c')
-rw-r--r-- | libgfortran/intrinsics/random.c | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/libgfortran/intrinsics/random.c b/libgfortran/intrinsics/random.c index 234c5ff..229fa69 100644 --- a/libgfortran/intrinsics/random.c +++ b/libgfortran/intrinsics/random.c @@ -310,11 +310,10 @@ getosrandom (void *buf, size_t buflen) rand_s (&b[i]); return 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. */ +#ifdef HAVE_GETENTROPY + if (getentropy (buf, buflen) == 0) + return 0; +#endif int flags = O_RDONLY; #ifdef O_CLOEXEC flags |= O_CLOEXEC; |