summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Carlier <devnexen@gmail.com>2018-06-25 11:17:13 +0100
committerAnatol Belski <ab@php.net>2018-07-06 17:47:59 +0200
commit9da4e30c757b1b9cb70099ee67a0f0d7800c9eb2 (patch)
tree12f9a9b0102e39362d46e952ceb3b2af63ab21b6
parent0b94534e931074c49051bd9eba70fc4e0b7562e4 (diff)
downloadphp-git-9da4e30c757b1b9cb70099ee67a0f0d7800c9eb2.tar.gz
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.
-rw-r--r--ext/standard/random.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/ext/standard/random.c b/ext/standard/random.c
index 691fd9a394..1a394aae81 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) {