interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / getrandom / src / netbsd.rs

46 lines · 1353 bytes Raw Blame History
1//! Implementation for NetBSD
2use crate::{
3 util_libc::{sys_fill_exact, Weak},
4 Error,
5};
6use core::{mem::MaybeUninit, ptr};
7
8fn kern_arnd(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
9 static MIB: [libc::c_int; 2] = [libc::CTL_KERN, libc::KERN_ARND];
10 let mut len = buf.len();
11 let ret = unsafe {
12 libc::sysctl(
13 MIB.as_ptr(),
14 MIB.len() as libc::c_uint,
15 buf.as_mut_ptr() as *mut _,
16 &mut len,
17 ptr::null(),
18 0,
19 )
20 };
21 if ret == -1 {
22 -1
23 } else {
24 len as libc::ssize_t
25 }
26}
27
28type GetRandomFn = unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t;
29
30pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
31 // getrandom(2) was introduced in NetBSD 10.0
32 static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") };
33 if let Some(fptr) = GETRANDOM.ptr() {
34 let func: GetRandomFn = unsafe { core::mem::transmute(fptr) };
35 return sys_fill_exact(dest, |buf| unsafe {
36 func(buf.as_mut_ptr() as *mut u8, buf.len(), 0)
37 });
38 }
39
40 // NetBSD will only return up to 256 bytes at a time, and
41 // older NetBSD kernels will fail on longer buffers.
42 for chunk in dest.chunks_mut(256) {
43 sys_fill_exact(chunk, kern_arnd)?
44 }
45 Ok(())
46}