[前][次][番号順一覧][スレッド一覧]

ruby-changes:39101

From: nobu <ko1@a...>
Date: Tue, 7 Jul 2015 15:26:36 +0900 (JST)
Subject: [ruby-changes:39101] nobu:r51182 (trunk): random.c: try getrandom

nobu	2015-07-07 15:26:06 +0900 (Tue, 07 Jul 2015)

  New Revision: 51182

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51182

  Log:
    random.c: try getrandom
    
    * random.c (fill_random_bytes_syscall): try getrandom system call
      on Linux if supported by the kernel.

  Modified files:
    trunk/common.mk
    trunk/random.c
Index: common.mk
===================================================================
--- common.mk	(revision 51181)
+++ common.mk	(revision 51182)
@@ -1893,6 +1893,7 @@ random.$(OBJEXT): {$(VPATH)}io.h https://github.com/ruby/ruby/blob/trunk/common.mk#L1893
 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
Index: random.c
===================================================================
--- random.c	(revision 51181)
+++ random.c	(revision 51182)
@@ -77,6 +77,12 @@ The original copyright notice follows. https://github.com/ruby/ruby/blob/trunk/random.c#L77
 #include <sys/time.h>
 #endif
 
+#ifdef HAVE_SYSCALL_H
+#include <syscall.h>
+#elif defined HAVE_SYS_SYSCALL_H
+#include <sys/syscall.h>
+#endif
+
 #ifdef _WIN32
 # if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0400
 #  undef _WIN32_WINNT
@@ -84,8 +90,8 @@ The original copyright notice follows. https://github.com/ruby/ruby/blob/trunk/random.c#L90
 #  undef __WINCRYPT_H__
 # endif
 #include <wincrypt.h>
-#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, si https://github.com/ruby/ruby/blob/trunk/random.c#L515
     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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]