mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
random.c: try getrandom
* random.c (fill_random_bytes_syscall): try getrandom system call on Linux if supported by the kernel. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51182 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e96955e8f6
commit
bcd96d92f3
2 changed files with 24 additions and 1 deletions
|
@ -1893,6 +1893,7 @@ random.$(OBJEXT): {$(VPATH)}io.h
|
||||||
random.$(OBJEXT): {$(VPATH)}missing.h
|
random.$(OBJEXT): {$(VPATH)}missing.h
|
||||||
random.$(OBJEXT): {$(VPATH)}oniguruma.h
|
random.$(OBJEXT): {$(VPATH)}oniguruma.h
|
||||||
random.$(OBJEXT): {$(VPATH)}random.c
|
random.$(OBJEXT): {$(VPATH)}random.c
|
||||||
|
random.$(OBJEXT): {$(VPATH)}ruby_atomic.h
|
||||||
random.$(OBJEXT): {$(VPATH)}siphash.c
|
random.$(OBJEXT): {$(VPATH)}siphash.c
|
||||||
random.$(OBJEXT): {$(VPATH)}siphash.h
|
random.$(OBJEXT): {$(VPATH)}siphash.h
|
||||||
random.$(OBJEXT): {$(VPATH)}st.h
|
random.$(OBJEXT): {$(VPATH)}st.h
|
||||||
|
|
24
random.c
24
random.c
|
@ -77,6 +77,12 @@ The original copyright notice follows.
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_SYSCALL_H
|
||||||
|
#include <syscall.h>
|
||||||
|
#elif defined HAVE_SYS_SYSCALL_H
|
||||||
|
#include <sys/syscall.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0400
|
# if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0400
|
||||||
# undef _WIN32_WINNT
|
# undef _WIN32_WINNT
|
||||||
|
@ -84,8 +90,8 @@ The original copyright notice follows.
|
||||||
# undef __WINCRYPT_H__
|
# undef __WINCRYPT_H__
|
||||||
# endif
|
# endif
|
||||||
#include <wincrypt.h>
|
#include <wincrypt.h>
|
||||||
#include "ruby_atomic.h"
|
|
||||||
#endif
|
#endif
|
||||||
|
#include "ruby_atomic.h"
|
||||||
|
|
||||||
typedef int int_must_be_32bit_at_least[sizeof(int) * CHAR_BIT < 32 ? -1 : 1];
|
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);
|
CryptGenRandom(prov, size, seed);
|
||||||
return 0;
|
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
|
#else
|
||||||
# define fill_random_bytes_syscall(seed, size) -1
|
# define fill_random_bytes_syscall(seed, size) -1
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue