]> granicus.if.org Git - php/commitdiff
random_bytes improvements for FreeBSD (from 12.x serie)
authorDavid Carlier <devnexen@gmail.com>
Mon, 25 Jun 2018 10:17:13 +0000 (11:17 +0100)
committerAnatol Belski <ab@php.net>
Fri, 6 Jul 2018 15:47:59 +0000 (17:47 +0200)
giving the possiblity to pre-fill the buffer. A new getrandom
function was added for future version with a similar interface
than Linux's syscall.

ext/standard/random.c

index 691fd9a394d025116b3d4ea5871f6eccfbf73e02..1a394aae81be3df2a04d0539a617f2db7d0b4f11 100644 (file)
 #ifdef __linux__
 # include <sys/syscall.h>
 #endif
-#if defined(__OpenBSD__) || defined(__NetBSD__)
+#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
 # include <sys/param.h>
+# if __FreeBSD__ && __FreeBSD_version > 1200000
+    #include <sys/random.h>
+# endif
 #endif
 
 #ifdef ZTS
@@ -96,8 +99,8 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, zend_bool should_throw)
 #else
        size_t read_bytes = 0;
        ssize_t n;
-#if defined(__linux__) && defined(SYS_getrandom)
-       /* Linux getrandom(2) syscall */
+#if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000)
+       /* Linux getrandom(2) syscall or FreeBSD getrandom(2) function*/
        /* Keep reading until we get enough entropy */
        while (read_bytes < size) {
                /* Below, (bytes + read_bytes)  is pointer arithmetic.
@@ -110,7 +113,11 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, zend_bool should_throw)
 
                */
                size_t amount_to_read = size - read_bytes;
+#if defined(__linux__)
                n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0);
+#else
+               n = getrandom(bytes + read_bytes, amount_to_read, 0);
+#endif
 
                if (n == -1) {
                        if (errno == ENOSYS) {