Skip to content

Commit 64b92e7

Browse files
committed
Add GNU/Hurd support
Signed-off-by: Samuel Thibault <[email protected]>
1 parent 2e483d6 commit 64b92e7

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/hurd.rs

Lines changed: 18 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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)