From b33d6bfdec50d1873a5924153c7ebe2012163426 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Thu, 2 May 2024 00:46:51 -0700 Subject: [PATCH 1/2] Unconditionally use libc::getrandom on FreeBSD 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 --- src/bsd_arandom.rs | 6 +++--- src/lib.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bsd_arandom.rs b/src/bsd_arandom.rs index 6e133d89..b8a770f5 100644 --- a/src/bsd_arandom.rs +++ b/src/bsd_arandom.rs @@ -1,4 +1,4 @@ -//! Implementation for FreeBSD and NetBSD +//! Implementation for NetBSD use crate::{ util_libc::{sys_fill_exact, Weak}, Error, @@ -28,7 +28,7 @@ fn kern_arnd(buf: &mut [MaybeUninit]) -> libc::ssize_t { type GetRandomFn = unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t; pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { - // getrandom(2) was introduced in FreeBSD 12.0 and NetBSD 10.0 + // getrandom(2) was introduced in NetBSD 10.0 static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") }; if let Some(fptr) = GETRANDOM.ptr() { let func: GetRandomFn = unsafe { core::mem::transmute(fptr) }; @@ -37,7 +37,7 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { }); } - // Both FreeBSD and NetBSD will only return up to 256 bytes at a time, and + // NetBSD will only return up to 256 bytes at a time, and // older NetBSD kernels will fail on longer buffers. for chunk in dest.chunks_mut(256) { sys_fill_exact(chunk, kern_arnd)? diff --git a/src/lib.rs b/src/lib.rs index 31c18001..ac74000b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ //! | Windows | `*‑windows‑*` | [`BCryptGenRandom`] //! | macOS | `*‑apple‑darwin` | [`getentropy`][3] //! | iOS, tvOS, watchOS | `*‑apple‑ios`, `*-apple-tvos`, `*-apple-watchos` | [`CCRandomGenerateBytes`] -//! | FreeBSD | `*‑freebsd` | [`getrandom`][5] if available, otherwise [`kern.arandom`][6] +//! | FreeBSD | `*‑freebsd` | [`getrandom`][5] //! | OpenBSD | `*‑openbsd` | [`getentropy`][7] //! | NetBSD | `*‑netbsd` | [`getrandom`][16] if available, otherwise [`kern.arandom`][8] //! | Dragonfly BSD | `*‑dragonfly` | [`getrandom`][9] @@ -173,7 +173,6 @@ //! [3]: https://www.unix.com/man-page/mojave/2/getentropy/ //! [4]: https://www.unix.com/man-page/mojave/4/urandom/ //! [5]: https://www.freebsd.org/cgi/man.cgi?query=getrandom&manpath=FreeBSD+12.0-stable -//! [6]: https://www.freebsd.org/cgi/man.cgi?query=random&sektion=4 //! [7]: https://man.openbsd.org/getentropy.2 //! [8]: https://man.netbsd.org/sysctl.7 //! [9]: https://leaf.dragonflybsd.org/cgi/web-man?command=getrandom @@ -240,6 +239,7 @@ cfg_if! { #[path = "use_file.rs"] mod imp; } else if #[cfg(any( target_os = "dragonfly", + target_os = "freebsd", target_os = "hurd", // Check for target_arch = "arm" to only include the 3DS. Does not // include the Nintendo Switch (which is target_arch = "aarch64"). @@ -298,7 +298,7 @@ cfg_if! { mod util_libc; mod use_file; #[path = "solaris_illumos.rs"] mod imp; - } else if #[cfg(any(target_os = "freebsd", target_os = "netbsd"))] { + } else if #[cfg(target_os = "netbsd")] { mod util_libc; #[path = "bsd_arandom.rs"] mod imp; } else if #[cfg(target_os = "fuchsia")] { From cbdcc67cf105592c20493b1c4f616d6244b2d088 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Thu, 2 May 2024 01:02:08 -0700 Subject: [PATCH 2/2] Rename to netbsd.rs Signed-off-by: Joe Richey --- src/lib.rs | 2 +- src/{bsd_arandom.rs => netbsd.rs} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/{bsd_arandom.rs => netbsd.rs} (100%) diff --git a/src/lib.rs b/src/lib.rs index ac74000b..d44d87f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -300,7 +300,7 @@ cfg_if! { #[path = "solaris_illumos.rs"] mod imp; } else if #[cfg(target_os = "netbsd")] { mod util_libc; - #[path = "bsd_arandom.rs"] mod imp; + #[path = "netbsd.rs"] mod imp; } else if #[cfg(target_os = "fuchsia")] { #[path = "fuchsia.rs"] mod imp; } else if #[cfg(any(target_os = "ios", target_os = "visionos", target_os = "watchos", target_os = "tvos"))] { diff --git a/src/bsd_arandom.rs b/src/netbsd.rs similarity index 100% rename from src/bsd_arandom.rs rename to src/netbsd.rs