diff options
| author | 王宇逸 <Strawberry_Str@hotmail.com> | 2025-03-07 15:53:00 +0800 |
|---|---|---|
| committer | 王宇逸 <Strawberry_Str@hotmail.com> | 2025-03-10 21:23:31 +0800 |
| commit | 268e73499652a27eeedd1d3e43430ebcdf1160f4 (patch) | |
| tree | c8083b62c168ef3e476881e74fe064c2c73f3a9a /library/std/src/sys/random | |
| parent | 7d80aaaca8f5828fe6ce265e1dc6cb2267e618a0 (diff) | |
| download | rust-268e73499652a27eeedd1d3e43430ebcdf1160f4.tar.gz rust-268e73499652a27eeedd1d3e43430ebcdf1160f4.zip | |
Impl cygwin rand with getrandom
Diffstat (limited to 'library/std/src/sys/random')
| -rw-r--r-- | library/std/src/sys/random/cygwin.rs | 8 | ||||
| -rw-r--r-- | library/std/src/sys/random/linux.rs | 10 | ||||
| -rw-r--r-- | library/std/src/sys/random/mod.rs | 6 |
3 files changed, 13 insertions, 11 deletions
diff --git a/library/std/src/sys/random/cygwin.rs b/library/std/src/sys/random/cygwin.rs new file mode 100644 index 00000000000..e6759c8a3ed --- /dev/null +++ b/library/std/src/sys/random/cygwin.rs @@ -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..]; + } +} diff --git a/library/std/src/sys/random/linux.rs b/library/std/src/sys/random/linux.rs index fb4274281d6..e3cb79285cd 100644 --- a/library/std/src/sys/random/linux.rs +++ b/library/std/src/sys/random/linux.rs @@ -94,14 +94,7 @@ fn getrandom(mut bytes: &mut [u8], insecure: bool) { 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(mut bytes: &mut [u8], insecure: bool) { 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; diff --git a/library/std/src/sys/random/mod.rs b/library/std/src/sys/random/mod.rs index b6a357e5b07..2e5765b8a42 100644 --- a/library/std/src/sys/random/mod.rs +++ b/library/std/src/sys/random/mod.rs @@ -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 @@ cfg_if::cfg_if! { 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]; |
