//! Implementation for NetBSD //! //! `getrandom(2)` was introduced in NetBSD 10. To support older versions we //! implement our own weak linkage to it, and provide a fallback based on the //! KERN_ARND sysctl. usecrate::Error; use core::{
cmp,
ffi::c_void,
mem::{self, MaybeUninit},
ptr,
sync::atomic::{AtomicPtr, Ordering},
};
// NetBSD will only return up to 256 bytes at a time, and // older NetBSD kernels will fail on longer buffers. letmut len = cmp::min(buflen, 256); let ret = unsafe { libc::sysctl(MIB.as_ptr(), MIB_LEN, buf, &mut len, ptr::null(), 0) };
match ret { 0if len <= 256 => libc::ssize_t::try_from(len).expect("len is in the range of 0..=256"),
-1 => -1, // Zero return result will be converted into `Error::UNEXPECTED` by `sys_fill_exact`
_ => 0,
}
}
type GetRandomFn = unsafeextern"C"fn(*mut c_void, libc::size_t, libc::c_uint) -> libc::ssize_t;
#[cold] #[inline(never)] fn init() -> *mut c_void { static NAME: &[u8] = b"getrandom\0"; let name_ptr = NAME.as_ptr().cast::<libc::c_char>(); letmut ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) }; if ptr.is_null() || cfg!(getrandom_test_netbsd_fallback) { // Verify `polyfill_using_kern_arand` has the right signature. const POLYFILL: GetRandomFn = polyfill_using_kern_arand;
ptr = POLYFILL as *mut c_void;
}
GETRANDOM.store(ptr, Ordering::Release);
ptr
}
#[inline] pubfn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { // Despite being only a single atomic variable, we still cannot always use // Ordering::Relaxed, as we need to make sure a successful call to `init` // is "ordered before" any data read through the returned pointer (which // occurs when the function is called). Our implementation mirrors that of // the one in libstd, meaning that the use of non-Relaxed operations is // probably unnecessary. letmut fptr = GETRANDOM.load(Ordering::Acquire); if fptr.is_null() {
fptr = init();
} let fptr = unsafe { mem::transmute::<*mut c_void, GetRandomFn>(fptr) };
util_libc::sys_fill_exact(dest, |buf| unsafe {
fptr(buf.as_mut_ptr().cast::<c_void>(), buf.len(), 0)
})
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.22 Sekunden
(vorverarbeitet am 2026-08-27)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.