| 1 |
//! Implementation for VxWorks |
| 2 |
use crate::{util_libc::last_os_error, Error}; |
| 3 |
use core::{ |
| 4 |
mem::MaybeUninit, |
| 5 |
sync::atomic::{AtomicBool, Ordering::Relaxed}, |
| 6 |
}; |
| 7 |
|
| 8 |
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { |
| 9 |
static RNG_INIT: AtomicBool = AtomicBool::new(false); |
| 10 |
while !RNG_INIT.load(Relaxed) { |
| 11 |
let ret = unsafe { libc::randSecure() }; |
| 12 |
if ret < 0 { |
| 13 |
return Err(Error::VXWORKS_RAND_SECURE); |
| 14 |
} else if ret > 0 { |
| 15 |
RNG_INIT.store(true, Relaxed); |
| 16 |
break; |
| 17 |
} |
| 18 |
unsafe { libc::usleep(10) }; |
| 19 |
} |
| 20 |
|
| 21 |
// Prevent overflow of i32 |
| 22 |
for chunk in dest.chunks_mut(i32::max_value() as usize) { |
| 23 |
let ret = unsafe { libc::randABytes(chunk.as_mut_ptr() as *mut u8, chunk.len() as i32) }; |
| 24 |
if ret != 0 { |
| 25 |
return Err(last_os_error()); |
| 26 |
} |
| 27 |
} |
| 28 |
Ok(()) |
| 29 |
} |