1 // Copyright 2018 Developers of the Rand project.
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 //! Implementation for VxWorks
10 use crate::{util_libc::last_os_error, Error};
11 use core::sync::atomic::{AtomicBool, Ordering::Relaxed};
12
getrandom_inner(dest: &mut [u8]) -> Result<(), Error>13 pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
14 static RNG_INIT: AtomicBool = AtomicBool::new(false);
15 while !RNG_INIT.load(Relaxed) {
16 let ret = unsafe { libc::randSecure() };
17 if ret < 0 {
18 return Err(Error::VXWORKS_RAND_SECURE);
19 } else if ret > 0 {
20 RNG_INIT.store(true, Relaxed);
21 break;
22 }
23 unsafe { libc::usleep(10) };
24 }
25
26 // Prevent overflow of i32
27 for chunk in dest.chunks_mut(i32::max_value() as usize) {
28 let ret = unsafe { libc::randABytes(chunk.as_mut_ptr(), chunk.len() as i32) };
29 if ret != 0 {
30 return Err(last_os_error());
31 }
32 }
33 Ok(())
34 }
35