Skip to content

Commit 9da4e30

Browse files
devnexenweltling
authored andcommitted
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.
1 parent 0b94534 commit 9da4e30

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

ext/standard/random.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@
3333
#ifdef __linux__
3434
# include <sys/syscall.h>
3535
#endif
36-
#if defined(__OpenBSD__) || defined(__NetBSD__)
36+
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
3737
# include <sys/param.h>
38+
# if __FreeBSD__ && __FreeBSD_version > 1200000
39+
#include <sys/random.h>
40+
# endif
3841
#endif
3942

4043
#ifdef ZTS
@@ -96,8 +99,8 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, zend_bool should_throw)
9699
#else
97100
size_t read_bytes = 0;
98101
ssize_t n;
99-
#if defined(__linux__) && defined(SYS_getrandom)
100-
/* Linux getrandom(2) syscall */
102+
#if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000)
103+
/* Linux getrandom(2) syscall or FreeBSD getrandom(2) function*/
101104
/* Keep reading until we get enough entropy */
102105
while (read_bytes < size) {
103106
/* 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)
110113
111114
*/
112115
size_t amount_to_read = size - read_bytes;
116+
#if defined(__linux__)
113117
n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0);
118+
#else
119+
n = getrandom(bytes + read_bytes, amount_to_read, 0);
120+
#endif
114121

115122
if (n == -1) {
116123
if (errno == ENOSYS) {

0 commit comments

Comments
 (0)