ruby-changes:54824
From: glass <ko1@a...>
Date: Sun, 10 Feb 2019 17:21:39 +0900 (JST)
Subject: [ruby-changes:54824] glass:r67043 (trunk): random.c: use getrandom() if available.
glass 2019-02-10 17:21:33 +0900 (Sun, 10 Feb 2019) New Revision: 67043 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=67043 Log: random.c: use getrandom() if available. getrandom() is supported by glibc 2.25 or later. Modified files: trunk/configure.ac trunk/random.c Index: configure.ac =================================================================== --- configure.ac (revision 67042) +++ configure.ac (revision 67043) @@ -1826,6 +1826,7 @@ AC_CHECK_FUNCS(getpgid) https://github.com/ruby/ruby/blob/trunk/configure.ac#L1826 AC_CHECK_FUNCS(getpgrp) AC_CHECK_FUNCS(getpriority) AC_CHECK_FUNCS(getpwnam_r) +AC_CHECK_FUNCS(getrandom) AC_CHECK_FUNCS(getresgid) AC_CHECK_FUNCS(getresuid) AC_CHECK_FUNCS(getrlimit) Index: random.c =================================================================== --- random.c (revision 67042) +++ random.c (revision 67043) @@ -390,6 +390,32 @@ fill_random_bytes_syscall(void *seed, si https://github.com/ruby/ruby/blob/trunk/random.c#L390 CryptGenRandom(prov, size, seed); return 0; } +#elif defined HAVE_GETRANDOM +#include <sys/random.h> + +static int +fill_random_bytes_syscall(void *seed, size_t size, int need_secure) +{ + static rb_atomic_t try_syscall = 1; + if (try_syscall) { + ssize_t ret; + size_t offset = 0; + int flags = 0; + if (!need_secure) + flags = GRND_NONBLOCK; + do { + errno = 0; + ret = getrandom(((char*)seed) + offset, size - offset, flags); + if (ret == -1) { + ATOMIC_SET(try_syscall, 0); + return -1; + } + offset += (size_t)ret; + } while(offset < size); + return 0; + } + return -1; +} #elif defined __linux__ && defined __NR_getrandom #include <linux/random.h> -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/