[njs] Using getrandom() libc interface.

Sergey Kandaurov pluknet at nginx.com
Thu May 24 17:49:18 UTC 2018


details:   http://hg.nginx.org/njs/rev/822f975c933a
branches:  
changeset: 523:822f975c933a
user:      Sergey Kandaurov <pluknet at nginx.com>
date:      Thu May 24 20:32:09 2018 +0300
description:
Using getrandom() libc interface.

Available since Glibc 2.25, and FreeBSD 12.0.

diffstat:

 nxt/auto/getrandom |  33 ++++++++++++++++++++++++++++-----
 nxt/nxt_random.c   |   8 +++++++-
 2 files changed, 35 insertions(+), 6 deletions(-)

diffs (79 lines):

diff -r a994ab62d351 -r 822f975c933a nxt/auto/getrandom
--- a/nxt/auto/getrandom	Mon May 14 15:03:29 2018 +0300
+++ b/nxt/auto/getrandom	Thu May 24 20:32:09 2018 +0300
@@ -3,22 +3,45 @@
 # Copyright (C) NGINX, Inc.
 
 
-# Linux 3.17 getrandom().
+# getrandom().
 
 nxt_feature="getrandom()"
 nxt_feature_name=NXT_HAVE_GETRANDOM
-nxt_feature_run=no
+nxt_feature_run=yes
 nxt_feature_incs=
 nxt_feature_libs=
 nxt_feature_test="#include <unistd.h>
-                  #include <sys/syscall.h>
-                  #include <linux/random.h>
+                  #include <sys/random.h>
 
                   int main(void) {
                       char  buf[4];
 
-                      (void) syscall(SYS_getrandom, buf, 4, 0);
+                      if (getrandom(buf, 4, 0) < 0) {
+                          return 1;
+                      }
 
                       return 0;
                   }"
 . ${NXT_AUTO}feature
+
+if [ $nxt_found = no ]; then
+
+    # Linux 3.17 SYS_getrandom.
+
+    nxt_feature="SYS_getrandom in Linux"
+    nxt_feature_name=NXT_HAVE_LINUX_SYS_GETRANDOM
+    nxt_feature_test="#include <unistd.h>
+                      #include <sys/syscall.h>
+                      #include <linux/random.h>
+
+                      int main(void) {
+                          char  buf[4];
+
+                          if (syscall(SYS_getrandom, buf, 4, 0) < 0) {
+                              return 1;
+                          }
+
+                          return 0;
+                      }"
+    . ${NXT_AUTO}feature
+fi
diff -r a994ab62d351 -r 822f975c933a nxt/nxt_random.c
--- a/nxt/nxt_random.c	Mon May 14 15:03:29 2018 +0300
+++ b/nxt/nxt_random.c	Thu May 24 20:32:09 2018 +0300
@@ -13,6 +13,8 @@
 #include <sys/time.h>
 #include <unistd.h>
 #if (NXT_HAVE_GETRANDOM)
+#include <sys/random.h>
+#elif (NXT_HAVE_LINUX_SYS_GETRANDOM)
 #include <sys/syscall.h>
 #include <linux/random.h>
 #endif
@@ -66,7 +68,11 @@ nxt_random_stir(nxt_random_t *r, nxt_pid
 
 #if (NXT_HAVE_GETRANDOM)
 
-    /* Linux 3.17 getrandom(), it is not available in Glibc. */
+    n = getrandom(&key, NXT_RANDOM_KEY_SIZE, 0);
+
+#elif (NXT_HAVE_LINUX_SYS_GETRANDOM)
+
+    /* Linux 3.17 SYS_getrandom, not available in Glibc prior to 2.25. */
 
     n = syscall(SYS_getrandom, &key, NXT_RANDOM_KEY_SIZE, 0);
 


More information about the nginx-devel mailing list