diff --git a/common.mk b/common.mk index 5f1946ad85..b63174c6df 100644 --- a/common.mk +++ b/common.mk @@ -1893,6 +1893,7 @@ random.$(OBJEXT): {$(VPATH)}io.h random.$(OBJEXT): {$(VPATH)}missing.h random.$(OBJEXT): {$(VPATH)}oniguruma.h random.$(OBJEXT): {$(VPATH)}random.c +random.$(OBJEXT): {$(VPATH)}ruby_atomic.h random.$(OBJEXT): {$(VPATH)}siphash.c random.$(OBJEXT): {$(VPATH)}siphash.h random.$(OBJEXT): {$(VPATH)}st.h diff --git a/random.c b/random.c index a5a63629e4..853c287040 100644 --- a/random.c +++ b/random.c @@ -77,6 +77,12 @@ The original copyright notice follows. #include #endif +#ifdef HAVE_SYSCALL_H +#include +#elif defined HAVE_SYS_SYSCALL_H +#include +#endif + #ifdef _WIN32 # if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0400 # undef _WIN32_WINNT @@ -84,8 +90,8 @@ The original copyright notice follows. # undef __WINCRYPT_H__ # endif #include -#include "ruby_atomic.h" #endif +#include "ruby_atomic.h" typedef int int_must_be_32bit_at_least[sizeof(int) * CHAR_BIT < 32 ? -1 : 1]; @@ -509,6 +515,22 @@ fill_random_bytes_syscall(void *seed, size_t size) CryptGenRandom(prov, size, seed); return 0; } +#elif defined __linux__ && defined SYS_getrandom +static int +fill_random_bytes_syscall(void *seed, size_t size) +{ + static rb_atomic_t try_syscall = 1; + if (try_syscall) { + errno = 0; + ret = syscall(SYS_getrandom, seed, size, 0) + if (errno == ENOSYS) { + try_syscall = 0; + return -1; + } + if ((size_t)ret == size) return 0; + } + return 0; +} #else # define fill_random_bytes_syscall(seed, size) -1 #endif