com php-src: random_bytes improvements for FreeBSD (from 12.x serie): ext/standard/random.c

From: Date: Fri, 06 Jul 2018 15:47:59 +0000
Subject: com php-src: random_bytes improvements for FreeBSD (from 12.x serie): ext/standard/random.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-102455@lists.php.net to get a copy of this message
Commit: 9da4e30c757b1b9cb70099ee67a0f0d7800c9eb2 Author: David Carlier <devnexen@gmail.com> Mon, 25 Jun 2018 11:17:13 +0100 Committer: Anatol Belski <ab@php.net> Fri, 6 Jul 2018 17:47:59 +0200 Parents: 0b94534e931074c49051bd9eba70fc4e0b7562e4 Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=9da4e30c757b1b9cb70099ee67a0f0d7800c9eb2 Log: random_bytes improvements for FreeBSD (from 12.x serie) 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. Changed paths: M ext/standard/random.c Diff: diff --git a/ext/standard/random.c b/ext/standard/random.c index 691fd9a..1a394aa 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -33,8 +33,11 @@ #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) {

« previous php.cvs (#102455) next »