apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yla...@apache.org
Subject svn commit: r1814331 - /apr/apr/trunk/misc/unix/rand.c
Date Sun, 05 Nov 2017 00:58:05 GMT
Author: ylavic
Date: Sun Nov  5 00:58:05 2017
New Revision: 1814331

URL: http://svn.apache.org/viewvc?rev=1814331&view=rev
Log:
rand: follow up to r1814239, r1814240 and r1814326.

The above commits added the checks for HAVE_GETRANDOM and HAVE_ARC4RANDOM first
in configure.in so to avoid breaking the AC_MESSAGE_CHECKING/RESULT line, thus
it also defined the macros unconditionally (while the others were and still are
only defined if no previous one is elected already).

Yet the top priority one should remain HAVE_EGD when --with-egd is specified,
so we now have to rearrange apr_generate_random_bytes()'s #ifdefs to preserve
that now.

While at it, let's add an #error for the "should not happen" case where
APR_HAS_RANDOM is defined but no implementation is found.


Modified:
    apr/apr/trunk/misc/unix/rand.c

Modified: apr/apr/trunk/misc/unix/rand.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/misc/unix/rand.c?rev=1814331&r1=1814330&r2=1814331&view=diff
==============================================================================
--- apr/apr/trunk/misc/unix/rand.c (original)
+++ apr/apr/trunk/misc/unix/rand.c Sun Nov  5 00:58:05 2017
@@ -111,69 +111,7 @@ APR_DECLARE(apr_status_t) apr_os_uuid_ge
 APR_DECLARE(apr_status_t) apr_generate_random_bytes(unsigned char *buf, 
                                                     apr_size_t length)
 {
-#if defined(USE_GETRANDOM)
-
-    do {
-        int rc;
-
-        rc = getrandom(buf, length, GRND_NONBLOCK);
-        if (rc == -1) {
-            if (errno == EINTR) {
-                continue;
-            }
-            return errno;
-        }
-
-        buf += rc;
-        length -= rc;
-    } while (length > 0);
-
-#elif defined(HAVE_ARC4RANDOM)
-
-    arc4random_buf(buf, length);
-
-#elif defined(DEV_RANDOM)
-
-    int fd = -1;
-
-    /* On BSD/OS 4.1, /dev/random gives out 8 bytes at a time, then
-     * gives EOF, so reading 'length' bytes may require opening the
-     * device several times. */
-    do {
-        apr_ssize_t rc;
-
-        if (fd == -1)
-            if ((fd = open(DEV_RANDOM, O_RDONLY)) == -1)
-                return errno;
-        
-        do {
-            rc = read(fd, buf, length);
-        } while (rc == -1 && errno == EINTR);
-
-        if (rc < 0) {
-            int errnum = errno;
-            close(fd);
-            return errnum;
-        }
-        else if (rc == 0) {
-            close(fd);
-            fd = -1; /* force open() again */
-        }
-        else {
-            buf += rc;
-            length -= rc;
-        }
-    } while (length > 0);
-    
-    close(fd);
-#elif defined(OS2)
-    static UCHAR randbyte();
-    unsigned int idx;
-
-    for (idx=0; idx<length; idx++)
-	buf[idx] = randbyte();
-
-#elif defined(HAVE_EGD)
+#if defined(HAVE_EGD)
     /* use EGD-compatible socket daemon (such as EGD or PRNGd).
      * message format:
      * 0x00 (get entropy level)
@@ -269,6 +207,70 @@ APR_DECLARE(apr_status_t) apr_generate_r
         return bad_errno;
     }
 
+#elif defined(USE_GETRANDOM)
+
+    do {
+        int rc;
+
+        rc = getrandom(buf, length, GRND_NONBLOCK);
+        if (rc == -1) {
+            if (errno == EINTR) {
+                continue;
+            }
+            return errno;
+        }
+
+        buf += rc;
+        length -= rc;
+    } while (length > 0);
+
+#elif defined(HAVE_ARC4RANDOM)
+
+    arc4random_buf(buf, length);
+
+#elif defined(DEV_RANDOM)
+
+    int fd = -1;
+
+    /* On BSD/OS 4.1, /dev/random gives out 8 bytes at a time, then
+     * gives EOF, so reading 'length' bytes may require opening the
+     * device several times. */
+    do {
+        apr_ssize_t rc;
+
+        if (fd == -1)
+            if ((fd = open(DEV_RANDOM, O_RDONLY)) == -1)
+                return errno;
+        
+        do {
+            rc = read(fd, buf, length);
+        } while (rc == -1 && errno == EINTR);
+
+        if (rc < 0) {
+            int errnum = errno;
+            close(fd);
+            return errnum;
+        }
+        else if (rc == 0) {
+            close(fd);
+            fd = -1; /* force open() again */
+        }
+        else {
+            buf += rc;
+            length -= rc;
+        }
+    } while (length > 0);
+    
+    close(fd);
+
+#elif defined(OS2)
+
+    static UCHAR randbyte();
+    unsigned int idx;
+
+    for (idx=0; idx<length; idx++)
+	buf[idx] = randbyte();
+
 #elif defined(HAVE_TRUERAND) /* use truerand */
 
     extern int randbyte(void);	/* from the truerand library */
@@ -280,6 +282,10 @@ APR_DECLARE(apr_status_t) apr_generate_r
     for (idx=0; idx<length; idx++)
 	buf[idx] = (unsigned char) randbyte();
 
+#else
+
+#error APR_HAS_RANDOM defined with no implementation
+
 #endif	/* DEV_RANDOM */
 
     return APR_SUCCESS;



Mime
View raw message