about summary refs log tree commit diff
path: root/library/std/src/sys/random/vxworks.rs
blob: d549ccebdb2cd483d4dddf8426f17ab5959dde24 (plain)
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
use crate::sync::atomic::AtomicBool;
use crate::sync::atomic::Ordering::Relaxed;

static RNG_INIT: AtomicBool = AtomicBool::new(false);

pub fn fill_bytes(mut bytes: &mut [u8]) {
    while !RNG_INIT.load(Relaxed) {
        let ret = unsafe { libc::randSecure() };
        if ret < 0 {
            panic!("failed to generate random data");
        } else if ret > 0 {
            RNG_INIT.store(true, Relaxed);
            break;
        }

        unsafe { libc::usleep(10) };
    }

    while !bytes.is_empty() {
        let len = bytes.len().try_into().unwrap_or(libc::c_int::MAX);
        let ret = unsafe { libc::randABytes(bytes.as_mut_ptr(), len) };
        assert!(ret >= 0, "failed to generate random data");
        bytes = &mut bytes[len as usize..];
    }
}