Skip to content

Commit d4b0ef0

Browse files
authored
Use libc::getrandom on DragonflyBSD (#411)
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 <[email protected]>
1 parent 0d55923 commit d4b0ef0

File tree

6 files changed

+14
-55
lines changed

6 files changed

+14
-55
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ compiler_builtins = { version = "0.1", optional = true }
1818
core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" }
1919

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

2323
[target.'cfg(target_os = "wasi")'.dependencies]
2424
wasi = { version = "0.11", default-features = false }

src/3ds.rs

-10
This file was deleted.

src/dragonfly.rs

-22
This file was deleted.

src/hurd.rs src/getrandom.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
//! Implementation for GNU/Hurd
2-
use crate::util_libc::sys_fill_exact;
3-
use crate::Error;
1+
//! Implementation using libc::getrandom
2+
use crate::{util_libc::sys_fill_exact, Error};
43
use core::mem::MaybeUninit;
54

65
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {

src/lib.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! | FreeBSD | `*‑freebsd` | [`getrandom`][5] if available, otherwise [`kern.arandom`][6]
1212
//! | OpenBSD | `*‑openbsd` | [`getentropy`][7]
1313
//! | NetBSD | `*‑netbsd` | [`getrandom`][16] if available, otherwise [`kern.arandom`][8]
14-
//! | Dragonfly BSD | `*‑dragonfly` | [`getrandom`][9] if available, otherwise [`/dev/urandom`][10] (identical to `/dev/random`)
14+
//! | Dragonfly BSD | `*‑dragonfly` | [`getrandom`][9]
1515
//! | Solaris, illumos | `*‑solaris`, `*‑illumos` | [`getrandom`][11] if available, otherwise [`/dev/random`][12]
1616
//! | Fuchsia OS | `*‑fuchsia` | [`cprng_draw`]
1717
//! | Redox | `*‑redox` | `/dev/urandom`
@@ -177,7 +177,6 @@
177177
//! [7]: https://man.openbsd.org/getentropy.2
178178
//! [8]: https://man.netbsd.org/sysctl.7
179179
//! [9]: https://leaf.dragonflybsd.org/cgi/web-man?command=getrandom
180-
//! [10]: https://leaf.dragonflybsd.org/cgi/web-man?command=random&section=4
181180
//! [11]: https://docs.oracle.com/cd/E88353_01/html/E37841/getrandom-2.html
182181
//! [12]: https://docs.oracle.com/cd/E86824_01/html/E54777/random-7d.html
183182
//! [13]: https://github.com/emscripten-core/emscripten/pull/12240
@@ -239,6 +238,15 @@ cfg_if! {
239238
if #[cfg(any(target_os = "haiku", target_os = "redox", target_os = "nto", target_os = "aix"))] {
240239
mod util_libc;
241240
#[path = "use_file.rs"] mod imp;
241+
} else if #[cfg(any(
242+
target_os = "dragonfly",
243+
target_os = "hurd",
244+
// Check for target_arch = "arm" to only include the 3DS. Does not
245+
// include the Nintendo Switch (which is target_arch = "aarch64").
246+
all(target_os = "horizon", target_arch = "arm"),
247+
))] {
248+
mod util_libc;
249+
#[path = "getrandom.rs"] mod imp;
242250
} else if #[cfg(all(
243251
not(feature = "linux_disable_fallback"),
244252
any(
@@ -293,10 +301,6 @@ cfg_if! {
293301
} else if #[cfg(any(target_os = "freebsd", target_os = "netbsd"))] {
294302
mod util_libc;
295303
#[path = "bsd_arandom.rs"] mod imp;
296-
} else if #[cfg(target_os = "dragonfly")] {
297-
mod util_libc;
298-
mod use_file;
299-
#[path = "dragonfly.rs"] mod imp;
300304
} else if #[cfg(target_os = "fuchsia")] {
301305
#[path = "fuchsia.rs"] mod imp;
302306
} else if #[cfg(any(target_os = "ios", target_os = "visionos", target_os = "watchos", target_os = "tvos"))] {
@@ -320,11 +324,6 @@ cfg_if! {
320324
#[path = "espidf.rs"] mod imp;
321325
} else if #[cfg(windows)] {
322326
#[path = "windows.rs"] mod imp;
323-
} else if #[cfg(all(target_os = "horizon", target_arch = "arm"))] {
324-
// We check for target_arch = "arm" because the Nintendo Switch also
325-
// uses Horizon OS (it is aarch64).
326-
mod util_libc;
327-
#[path = "3ds.rs"] mod imp;
328327
} else if #[cfg(target_os = "vita")] {
329328
mod util_libc;
330329
#[path = "vita.rs"] mod imp;
@@ -342,9 +341,6 @@ cfg_if! {
342341
any(target_arch = "wasm32", target_arch = "wasm64"),
343342
target_os = "unknown"))] {
344343
#[path = "js.rs"] mod imp;
345-
} else if #[cfg(target_os = "hurd")] {
346-
mod util_libc;
347-
#[path = "hurd.rs"] mod imp;
348344
} else if #[cfg(feature = "custom")] {
349345
use custom as imp;
350346
} else if #[cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"),

src/util_libc.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use libc::c_void;
1111
cfg_if! {
1212
if #[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "android"))] {
1313
use libc::__errno as errno_location;
14-
} else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "hurd", target_os = "redox"))] {
14+
} else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "hurd", target_os = "redox", target_os = "dragonfly"))] {
1515
use libc::__errno_location as errno_location;
1616
} else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] {
1717
use libc::___errno as errno_location;
@@ -35,10 +35,6 @@ cfg_if! {
3535
cfg_if! {
3636
if #[cfg(target_os = "vxworks")] {
3737
use libc::errnoGet as get_errno;
38-
} else if #[cfg(target_os = "dragonfly")] {
39-
// Until rust-lang/rust#29594 is stable, we cannot get the errno value
40-
// on DragonFlyBSD. So we just return an out-of-range errno.
41-
unsafe fn get_errno() -> libc::c_int { -1 }
4238
} else {
4339
unsafe fn get_errno() -> libc::c_int { *errno_location() }
4440
}

0 commit comments

Comments
 (0)