Skip to content

Commit bc680af

Browse files
committed
Impl cygwin rand with getrandom
1 parent 21866ee commit bc680af

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

library/std/src/sys/random/cygwin.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub fn fill_bytes(mut bytes: &mut [u8]) {
2+
while !bytes.is_empty() {
3+
let ret =
4+
unsafe { libc::getrandom(bytes.as_mut_ptr().cast(), bytes.len(), libc::GRND_NONBLOCK) };
5+
assert!(ret != -1, "failed to generate random data");
6+
bytes = &mut bytes[ret as usize..];
7+
}
8+
}

library/std/src/sys/random/linux.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,7 @@ fn getrandom(mut bytes: &mut [u8], insecure: bool) {
9494

9595
let flags = if insecure {
9696
if GRND_INSECURE_AVAILABLE.load(Relaxed) {
97-
#[cfg(target_os = "cygwin")]
98-
{
99-
libc::GRND_NONBLOCK
100-
}
101-
#[cfg(not(target_os = "cygwin"))]
102-
{
103-
libc::GRND_INSECURE
104-
}
97+
libc::GRND_INSECURE
10598
} else {
10699
libc::GRND_NONBLOCK
107100
}
@@ -117,7 +110,6 @@ fn getrandom(mut bytes: &mut [u8], insecure: bool) {
117110
libc::EINTR => continue,
118111
// `GRND_INSECURE` is not available, try
119112
// `GRND_NONBLOCK`.
120-
#[cfg(not(target_os = "cygwin"))]
121113
libc::EINVAL if flags == libc::GRND_INSECURE => {
122114
GRND_INSECURE_AVAILABLE.store(false, Relaxed);
123115
continue;

library/std/src/sys/random/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
cfg_if::cfg_if! {
22
// Tier 1
3-
if #[cfg(any(target_os = "linux", target_os = "android", target_os = "cygwin"))] {
3+
if #[cfg(any(target_os = "linux", target_os = "android"))] {
44
mod linux;
55
pub use linux::{fill_bytes, hashmap_random_keys};
66
} else if #[cfg(target_os = "windows")] {
77
mod windows;
88
pub use windows::fill_bytes;
9+
} else if #[cfg(target_os = "cygwin")] {
10+
mod cygwin;
11+
pub use cygwin::fill_bytes;
912
} else if #[cfg(target_vendor = "apple")] {
1013
mod apple;
1114
pub use apple::fill_bytes;
@@ -88,7 +91,6 @@ cfg_if::cfg_if! {
8891
target_os = "android",
8992
all(target_family = "wasm", target_os = "unknown"),
9093
target_os = "xous",
91-
target_os = "cygwin",
9294
)))]
9395
pub fn hashmap_random_keys() -> (u64, u64) {
9496
let mut buf = [0; 16];

0 commit comments

Comments
 (0)