Skip to content

Commit dca4961

Browse files
authored
Unconditionally use libc::getrandom on FreeBSD (#416)
Rust's minimum version is now FreeBSD 12, so we can drop the fallback code. We have to keep the NetBSD fallback code as NetBSD 10 is still quite new. Signed-off-by: Joe Richey <[email protected]>
1 parent d4b0ef0 commit dca4961

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! | Windows | `*‑windows‑*` | [`BCryptGenRandom`]
99
//! | macOS | `*‑apple‑darwin` | [`getentropy`][3]
1010
//! | iOS, tvOS, watchOS | `*‑apple‑ios`, `*-apple-tvos`, `*-apple-watchos` | [`CCRandomGenerateBytes`]
11-
//! | FreeBSD | `*‑freebsd` | [`getrandom`][5] if available, otherwise [`kern.arandom`][6]
11+
//! | FreeBSD | `*‑freebsd` | [`getrandom`][5]
1212
//! | OpenBSD | `*‑openbsd` | [`getentropy`][7]
1313
//! | NetBSD | `*‑netbsd` | [`getrandom`][16] if available, otherwise [`kern.arandom`][8]
1414
//! | Dragonfly BSD | `*‑dragonfly` | [`getrandom`][9]
@@ -173,7 +173,6 @@
173173
//! [3]: https://www.unix.com/man-page/mojave/2/getentropy/
174174
//! [4]: https://www.unix.com/man-page/mojave/4/urandom/
175175
//! [5]: https://www.freebsd.org/cgi/man.cgi?query=getrandom&manpath=FreeBSD+12.0-stable
176-
//! [6]: https://www.freebsd.org/cgi/man.cgi?query=random&sektion=4
177176
//! [7]: https://man.openbsd.org/getentropy.2
178177
//! [8]: https://man.netbsd.org/sysctl.7
179178
//! [9]: https://leaf.dragonflybsd.org/cgi/web-man?command=getrandom
@@ -240,6 +239,7 @@ cfg_if! {
240239
#[path = "use_file.rs"] mod imp;
241240
} else if #[cfg(any(
242241
target_os = "dragonfly",
242+
target_os = "freebsd",
243243
target_os = "hurd",
244244
// Check for target_arch = "arm" to only include the 3DS. Does not
245245
// include the Nintendo Switch (which is target_arch = "aarch64").
@@ -298,9 +298,9 @@ cfg_if! {
298298
mod util_libc;
299299
mod use_file;
300300
#[path = "solaris_illumos.rs"] mod imp;
301-
} else if #[cfg(any(target_os = "freebsd", target_os = "netbsd"))] {
301+
} else if #[cfg(target_os = "netbsd")] {
302302
mod util_libc;
303-
#[path = "bsd_arandom.rs"] mod imp;
303+
#[path = "netbsd.rs"] mod imp;
304304
} else if #[cfg(target_os = "fuchsia")] {
305305
#[path = "fuchsia.rs"] mod imp;
306306
} else if #[cfg(any(target_os = "ios", target_os = "visionos", target_os = "watchos", target_os = "tvos"))] {

src/bsd_arandom.rs src/netbsd.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Implementation for FreeBSD and NetBSD
1+
//! Implementation for NetBSD
22
use crate::{
33
util_libc::{sys_fill_exact, Weak},
44
Error,
@@ -28,7 +28,7 @@ fn kern_arnd(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
2828
type GetRandomFn = unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t;
2929

3030
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
31-
// getrandom(2) was introduced in FreeBSD 12.0 and NetBSD 10.0
31+
// getrandom(2) was introduced in NetBSD 10.0
3232
static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") };
3333
if let Some(fptr) = GETRANDOM.ptr() {
3434
let func: GetRandomFn = unsafe { core::mem::transmute(fptr) };
@@ -37,7 +37,7 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3737
});
3838
}
3939

40-
// Both FreeBSD and NetBSD will only return up to 256 bytes at a time, and
40+
// NetBSD will only return up to 256 bytes at a time, and
4141
// older NetBSD kernels will fail on longer buffers.
4242
for chunk in dest.chunks_mut(256) {
4343
sys_fill_exact(chunk, kern_arnd)?

0 commit comments

Comments
 (0)