Skip to content

Commit c96bfff

Browse files
committed
Add GNU/Hurd support
Signed-off-by: Samuel Thibault <[email protected]>
1 parent 35808a4 commit c96bfff

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

.github/workflows/tests.yml

+2
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ jobs:
317317
features: ["rdrand"]
318318
- target: x86_64-unknown-l4re-uclibc
319319
features: ["rdrand"]
320+
- target: i686-unknown-hurd-gnu
321+
features: ["std"]
320322
steps:
321323
- uses: actions/checkout@v3
322324
- uses: dtolnay/rust-toolchain@nightly # Required to build libcore

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.143", default-features = false }
21+
libc = { version = "0.2.149", default-features = false }
2222

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

src/hurd.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2021 Developers of the Rand project.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
//! Implementation for GNU/Hurd
10+
use crate::util_libc::sys_fill_exact;
11+
use crate::Error;
12+
use core::mem::MaybeUninit;
13+
14+
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
15+
sys_fill_exact(dest, |buf| unsafe {
16+
libc::getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0)
17+
})
18+
}

src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
//! | Redox | `*‑redox` | `/dev/urandom`
2626
//! | Haiku | `*‑haiku` | `/dev/urandom` (identical to `/dev/random`)
2727
//! | Hermit | `*-hermit` | [`sys_read_entropy`]
28+
//! | Hurd | `*-hurd-*` | [`getrandom`][17]
2829
//! | SGX | `x86_64‑*‑sgx` | [`RDRAND`]
2930
//! | VxWorks | `*‑wrs‑vxworks‑*` | `randABytes` after checking entropy pool initialization with `randSecure`
3031
//! | ESP-IDF | `*‑espidf` | [`esp_fill_random`]
@@ -166,6 +167,7 @@
166167
//! [14]: https://www.qnx.com/developers/docs/7.1/index.html#com.qnx.doc.neutrino.utilities/topic/r/random.html
167168
//! [15]: https://www.ibm.com/docs/en/aix/7.3?topic=files-random-urandom-devices
168169
//! [16]: https://man.netbsd.org/getrandom.2
170+
//! [17]: https://www.gnu.org/software/libc/manual/html_mono/libc.html#index-getrandom
169171
//!
170172
//! [`BCryptGenRandom`]: https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
171173
//! [`Crypto.getRandomValues`]: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues
@@ -278,6 +280,9 @@ cfg_if! {
278280
any(target_arch = "wasm32", target_arch = "wasm64"),
279281
target_os = "unknown"))] {
280282
#[path = "js.rs"] mod imp;
283+
} else if #[cfg(target_os = "hurd")] {
284+
mod util_libc;
285+
#[path = "hurd.rs"] mod imp;
281286
} else if #[cfg(feature = "custom")] {
282287
use custom as imp;
283288
} else if #[cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"),

src/util_libc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use libc::c_void;
1919
cfg_if! {
2020
if #[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "android"))] {
2121
use libc::__errno as errno_location;
22-
} else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "redox"))] {
22+
} else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "hurd", target_os = "redox"))] {
2323
use libc::__errno_location as errno_location;
2424
} else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] {
2525
use libc::___errno as errno_location;

0 commit comments

Comments
 (0)