Skip to content

Commit

Permalink
Use libc::getrandom on DragonflyBSD (#411)
Browse files Browse the repository at this point in the history
This also moves the "only use libc::getrandom" implementaiton to its
own file, allowing multiple OSes to use it, simplifying the
implementation.

Bumps libc to include rust-lang/libc@e79e95f

Also moves Dragonfly to using `__errno_location`, as that binding was
added here: DragonFlyBSD/DragonFlyBSD@60d3113
which predates the introduction of `getrandom` support.

Signed-off-by: Joe Richey <joerichey@google.com>
  • Loading branch information
josephlr committed May 1, 2024
1 parent 0d55923 commit d4b0ef0
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -18,7 +18,7 @@ compiler_builtins = { version = "0.1", optional = true }
core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" }

[target.'cfg(unix)'.dependencies]
libc = { version = "0.2.149", default-features = false }
libc = { version = "0.2.154", default-features = false }

[target.'cfg(target_os = "wasi")'.dependencies]
wasi = { version = "0.11", default-features = false }
Expand Down
10 changes: 0 additions & 10 deletions src/3ds.rs

This file was deleted.

22 changes: 0 additions & 22 deletions src/dragonfly.rs

This file was deleted.

5 changes: 2 additions & 3 deletions src/hurd.rs → src/getrandom.rs
@@ -1,6 +1,5 @@
//! Implementation for GNU/Hurd
use crate::util_libc::sys_fill_exact;
use crate::Error;
//! Implementation using libc::getrandom
use crate::{util_libc::sys_fill_exact, Error};
use core::mem::MaybeUninit;

pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
Expand Down
24 changes: 10 additions & 14 deletions src/lib.rs
Expand Up @@ -11,7 +11,7 @@
//! | FreeBSD | `*‑freebsd` | [`getrandom`][5] if available, otherwise [`kern.arandom`][6]
//! | OpenBSD | `*‑openbsd` | [`getentropy`][7]
//! | NetBSD | `*‑netbsd` | [`getrandom`][16] if available, otherwise [`kern.arandom`][8]
//! | Dragonfly BSD | `*‑dragonfly` | [`getrandom`][9] if available, otherwise [`/dev/urandom`][10] (identical to `/dev/random`)
//! | Dragonfly BSD | `*‑dragonfly` | [`getrandom`][9]
//! | Solaris, illumos | `*‑solaris`, `*‑illumos` | [`getrandom`][11] if available, otherwise [`/dev/random`][12]
//! | Fuchsia OS | `*‑fuchsia` | [`cprng_draw`]
//! | Redox | `*‑redox` | `/dev/urandom`
Expand Down Expand Up @@ -177,7 +177,6 @@
//! [7]: https://man.openbsd.org/getentropy.2
//! [8]: https://man.netbsd.org/sysctl.7
//! [9]: https://leaf.dragonflybsd.org/cgi/web-man?command=getrandom
//! [10]: https://leaf.dragonflybsd.org/cgi/web-man?command=random&section=4
//! [11]: https://docs.oracle.com/cd/E88353_01/html/E37841/getrandom-2.html
//! [12]: https://docs.oracle.com/cd/E86824_01/html/E54777/random-7d.html
//! [13]: https://github.com/emscripten-core/emscripten/pull/12240
Expand Down Expand Up @@ -239,6 +238,15 @@ cfg_if! {
if #[cfg(any(target_os = "haiku", target_os = "redox", target_os = "nto", target_os = "aix"))] {
mod util_libc;
#[path = "use_file.rs"] mod imp;
} else if #[cfg(any(
target_os = "dragonfly",
target_os = "hurd",
// Check for target_arch = "arm" to only include the 3DS. Does not
// include the Nintendo Switch (which is target_arch = "aarch64").
all(target_os = "horizon", target_arch = "arm"),
))] {
mod util_libc;
#[path = "getrandom.rs"] mod imp;
} else if #[cfg(all(
not(feature = "linux_disable_fallback"),
any(
Expand Down Expand Up @@ -293,10 +301,6 @@ cfg_if! {
} else if #[cfg(any(target_os = "freebsd", target_os = "netbsd"))] {
mod util_libc;
#[path = "bsd_arandom.rs"] mod imp;
} else if #[cfg(target_os = "dragonfly")] {
mod util_libc;
mod use_file;
#[path = "dragonfly.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"))] {
Expand All @@ -320,11 +324,6 @@ cfg_if! {
#[path = "espidf.rs"] mod imp;
} else if #[cfg(windows)] {
#[path = "windows.rs"] mod imp;
} else if #[cfg(all(target_os = "horizon", target_arch = "arm"))] {
// We check for target_arch = "arm" because the Nintendo Switch also
// uses Horizon OS (it is aarch64).
mod util_libc;
#[path = "3ds.rs"] mod imp;
} else if #[cfg(target_os = "vita")] {
mod util_libc;
#[path = "vita.rs"] mod imp;
Expand All @@ -342,9 +341,6 @@ cfg_if! {
any(target_arch = "wasm32", target_arch = "wasm64"),
target_os = "unknown"))] {
#[path = "js.rs"] mod imp;
} else if #[cfg(target_os = "hurd")] {
mod util_libc;
#[path = "hurd.rs"] mod imp;
} else if #[cfg(feature = "custom")] {
use custom as imp;
} else if #[cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"),
Expand Down
6 changes: 1 addition & 5 deletions src/util_libc.rs
Expand Up @@ -11,7 +11,7 @@ use libc::c_void;
cfg_if! {
if #[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "android"))] {
use libc::__errno as errno_location;
} else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "hurd", target_os = "redox"))] {
} else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "hurd", target_os = "redox", target_os = "dragonfly"))] {
use libc::__errno_location as errno_location;
} else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] {
use libc::___errno as errno_location;
Expand All @@ -35,10 +35,6 @@ cfg_if! {
cfg_if! {
if #[cfg(target_os = "vxworks")] {
use libc::errnoGet as get_errno;
} else if #[cfg(target_os = "dragonfly")] {
// Until rust-lang/rust#29594 is stable, we cannot get the errno value
// on DragonFlyBSD. So we just return an out-of-range errno.
unsafe fn get_errno() -> libc::c_int { -1 }
} else {
unsafe fn get_errno() -> libc::c_int { *errno_location() }
}
Expand Down

0 comments on commit d4b0ef0

Please sign in to comment.