mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-28 20:16:58 +03:00
Impl cygwin rand with getrandom
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
pub fn fill_bytes(mut bytes: &mut [u8]) {
|
||||
while !bytes.is_empty() {
|
||||
let ret =
|
||||
unsafe { libc::getrandom(bytes.as_mut_ptr().cast(), bytes.len(), libc::GRND_NONBLOCK) };
|
||||
assert!(ret != -1, "failed to generate random data");
|
||||
bytes = &mut bytes[ret as usize..];
|
||||
}
|
||||
}
|
||||
@@ -94,14 +94,7 @@ fn getrandom(
|
||||
|
||||
let flags = if insecure {
|
||||
if GRND_INSECURE_AVAILABLE.load(Relaxed) {
|
||||
#[cfg(target_os = "cygwin")]
|
||||
{
|
||||
libc::GRND_NONBLOCK
|
||||
}
|
||||
#[cfg(not(target_os = "cygwin"))]
|
||||
{
|
||||
libc::GRND_INSECURE
|
||||
}
|
||||
libc::GRND_INSECURE
|
||||
} else {
|
||||
libc::GRND_NONBLOCK
|
||||
}
|
||||
@@ -117,7 +110,6 @@ fn getrandom(
|
||||
libc::EINTR => continue,
|
||||
// `GRND_INSECURE` is not available, try
|
||||
// `GRND_NONBLOCK`.
|
||||
#[cfg(not(target_os = "cygwin"))]
|
||||
libc::EINVAL if flags == libc::GRND_INSECURE => {
|
||||
GRND_INSECURE_AVAILABLE.store(false, Relaxed);
|
||||
continue;
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
cfg_if::cfg_if! {
|
||||
// Tier 1
|
||||
if #[cfg(any(target_os = "linux", target_os = "android", target_os = "cygwin"))] {
|
||||
if #[cfg(any(target_os = "linux", target_os = "android"))] {
|
||||
mod linux;
|
||||
pub use linux::{fill_bytes, hashmap_random_keys};
|
||||
} else if #[cfg(target_os = "windows")] {
|
||||
mod windows;
|
||||
pub use windows::fill_bytes;
|
||||
} else if #[cfg(target_os = "cygwin")] {
|
||||
mod cygwin;
|
||||
pub use cygwin::fill_bytes;
|
||||
} else if #[cfg(target_vendor = "apple")] {
|
||||
mod apple;
|
||||
pub use apple::fill_bytes;
|
||||
@@ -88,7 +91,6 @@
|
||||
target_os = "android",
|
||||
all(target_family = "wasm", target_os = "unknown"),
|
||||
target_os = "xous",
|
||||
target_os = "cygwin",
|
||||
)))]
|
||||
pub fn hashmap_random_keys() -> (u64, u64) {
|
||||
let mut buf = [0; 16];
|
||||
|
||||
Reference in New Issue
Block a user