#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
#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.
*/
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) {