//! `rand` on Linux: `getrandom`.
use core::mem::MaybeUninit;
use crate::sys::syscall;
use crate::{ErrorKind, Result};
pub(crate) fn fill(mut buf: &mut [MaybeUninit<u8>]) -> Result<()> {
// The kernel returns what it has written so far when a signal arrives
// during a request of more than 256 bytes, and never gives more than
// 32 MiB at once.
while !buf.is_empty() {
// SAFETY: the kernel writes at most `buf.len()` bytes to `buf`. No
// flag: block until the generator is initialised, then never.
let written = unsafe { syscall!(GETRANDOM, buf.as_mut_ptr(), buf.len(), 0) }?;
if written == 0 {
return Err(ErrorKind::Other.into());
}
buf = buf.get_mut(written..).ok_or(ErrorKind::Other)?;
}
Ok(())
}