From c96bfff10ba20326fc5a89bc7f51056ce8e19e38 Mon Sep 17 00:00:00 2001 From: Samuel Thibault Date: Tue, 29 Aug 2023 19:45:37 +0000 Subject: [PATCH] Add GNU/Hurd support Signed-off-by: Samuel Thibault --- .github/workflows/tests.yml | 2 ++ Cargo.toml | 2 +- src/hurd.rs | 18 ++++++++++++++++++ src/lib.rs | 5 +++++ src/util_libc.rs | 2 +- 5 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/hurd.rs diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 13fe973f..e6534e9e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -317,6 +317,8 @@ jobs: features: ["rdrand"] - target: x86_64-unknown-l4re-uclibc features: ["rdrand"] + - target: i686-unknown-hurd-gnu + features: ["std"] steps: - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@nightly # Required to build libcore diff --git a/Cargo.toml b/Cargo.toml index b0726ec8..4935e9fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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.143", default-features = false } +libc = { version = "0.2.149", default-features = false } [target.'cfg(target_os = "wasi")'.dependencies] wasi = { version = "0.11", default-features = false } diff --git a/src/hurd.rs b/src/hurd.rs new file mode 100644 index 00000000..842b9bc4 --- /dev/null +++ b/src/hurd.rs @@ -0,0 +1,18 @@ +// Copyright 2021 Developers of the Rand project. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Implementation for GNU/Hurd +use crate::util_libc::sys_fill_exact; +use crate::Error; +use core::mem::MaybeUninit; + +pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { + sys_fill_exact(dest, |buf| unsafe { + libc::getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0) + }) +} diff --git a/src/lib.rs b/src/lib.rs index fbfe7295..6336098b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,7 @@ //! | Redox | `*‑redox` | `/dev/urandom` //! | Haiku | `*‑haiku` | `/dev/urandom` (identical to `/dev/random`) //! | Hermit | `*-hermit` | [`sys_read_entropy`] +//! | Hurd | `*-hurd-*` | [`getrandom`][17] //! | SGX | `x86_64‑*‑sgx` | [`RDRAND`] //! | VxWorks | `*‑wrs‑vxworks‑*` | `randABytes` after checking entropy pool initialization with `randSecure` //! | ESP-IDF | `*‑espidf` | [`esp_fill_random`] @@ -166,6 +167,7 @@ //! [14]: https://www.qnx.com/developers/docs/7.1/index.html#com.qnx.doc.neutrino.utilities/topic/r/random.html //! [15]: https://www.ibm.com/docs/en/aix/7.3?topic=files-random-urandom-devices //! [16]: https://man.netbsd.org/getrandom.2 +//! [17]: https://www.gnu.org/software/libc/manual/html_mono/libc.html#index-getrandom //! //! [`BCryptGenRandom`]: https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom //! [`Crypto.getRandomValues`]: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues @@ -278,6 +280,9 @@ 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"), diff --git a/src/util_libc.rs b/src/util_libc.rs index 4b94144d..99bee382 100644 --- a/src/util_libc.rs +++ b/src/util_libc.rs @@ -19,7 +19,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 = "redox"))] { + } else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "hurd", target_os = "redox"))] { use libc::__errno_location as errno_location; } else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] { use libc::___errno as errno_location;