Skip to content

Commit a7e0bab

Browse files
committed
rand: freebsd update, using getrandom.
supported since the 12th release, while 11.4 is EOL since 2021.
1 parent 7c306f6 commit a7e0bab

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

library/std/src/sys/unix/rand.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ pub fn hashmap_random_keys() -> (u64, u64) {
1616
not(target_os = "ios"),
1717
not(target_os = "watchos"),
1818
not(target_os = "openbsd"),
19-
not(target_os = "freebsd"),
2019
not(target_os = "netbsd"),
2120
not(target_os = "fuchsia"),
2221
not(target_os = "redox"),
@@ -65,11 +64,25 @@ mod imp {
6564
unsafe { libc::getrandom(buf.as_mut_ptr().cast(), buf.len(), 0) }
6665
}
6766

67+
#[cfg(target_os = "freebsd")]
68+
fn getrandom(buf: &mut [u8]) -> libc::ssize_t {
69+
// FIXME: using the above when libary std's libc is updated
70+
extern "C" {
71+
fn getrandom(
72+
buffer: *mut libc::c_void,
73+
length: libc::size_t,
74+
flags: libc::c_uint,
75+
) -> libc::ssize_t;
76+
}
77+
unsafe { getrandom(buf.as_mut_ptr().cast(), buf.len(), 0) }
78+
}
79+
6880
#[cfg(not(any(
6981
target_os = "linux",
7082
target_os = "android",
7183
target_os = "espidf",
72-
target_os = "horizon"
84+
target_os = "horizon",
85+
target_os = "freebsd"
7386
)))]
7487
fn getrandom_fill_bytes(_buf: &mut [u8]) -> bool {
7588
false
@@ -79,7 +92,8 @@ mod imp {
7992
target_os = "linux",
8093
target_os = "android",
8194
target_os = "espidf",
82-
target_os = "horizon"
95+
target_os = "horizon",
96+
target_os = "freebsd"
8397
))]
8498
fn getrandom_fill_bytes(v: &mut [u8]) -> bool {
8599
use crate::sync::atomic::{AtomicBool, Ordering};
@@ -219,7 +233,7 @@ mod imp {
219233
}
220234
}
221235

222-
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
236+
#[cfg(target_os = "netbsd")]
223237
mod imp {
224238
use crate::ptr;
225239

0 commit comments

Comments
 (0)