-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathlinux_rustix.rs
More file actions
32 lines (29 loc) · 1.14 KB
/
linux_rustix.rs
File metadata and controls
32 lines (29 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//! Implementation for Linux / Android without `/dev/urandom` fallback
use crate::{Error, MaybeUninit};
use rustix::rand::{getrandom_uninit, GetRandomFlags};
pub use crate::util::{inner_u32, inner_u64};
#[cfg(not(any(target_os = "android", target_os = "linux")))]
compile_error!("`linux_rustix` backend can be enabled only for Linux/Android targets!");
pub fn fill_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
loop {
let res = getrandom_uninit(dest, GetRandomFlags::empty()).map(|(res, _)| res.len());
match res {
Ok(0) => return Err(Error::UNEXPECTED),
Ok(res_len) => {
dest = dest.get_mut(res_len..).ok_or(Error::UNEXPECTED)?;
if dest.is_empty() {
return Ok(());
}
}
Err(rustix::io::Errno::INTR) => continue,
Err(err) => {
let code = err
.raw_os_error()
.wrapping_neg()
.try_into()
.map_err(|_| Error::UNEXPECTED)?;
return Err(Error::from_os_error(code));
}
}
}
}