getrandom/backends/getrandom.rs
1//! Implementation using getrandom(2).
2//!
3//! Available since:
4//! - Linux Kernel 3.17, Glibc 2.25, Musl 1.1.20
5//! - Android API level 23 (Marshmallow)
6//! - NetBSD 10.0
7//! - FreeBSD 12.0
8//! - illumos since Dec 2018
9//! - DragonFly 5.7
10//! - Hurd Glibc 2.31
11//! - shim-3ds since Feb 2022
12//!
13//! For these platforms, we always use the default pool and never set the
14//! GRND_RANDOM flag to use the /dev/random pool. On Linux/Android/Hurd, using
15//! GRND_RANDOM is not recommended. On NetBSD/FreeBSD/Dragonfly/3ds, it does
16//! nothing. On illumos, the default pool is used to implement getentropy(2),
17//! so we assume it is acceptable here.
18use crate::Error;
19use core::mem::MaybeUninit;
20
21pub use crate::util::{inner_u32, inner_u64};
22
23#[path = "../util_libc.rs"]
24mod util_libc;
25
26#[inline]
27pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
28 util_libc::sys_fill_exact(dest, |buf| unsafe {
29 let ret = libc::getrandom(buf.as_mut_ptr().cast(), buf.len(), 0);
30
31 #[cfg(any(target_os = "android", target_os = "linux"))]
32 #[allow(unused_unsafe)] // TODO(MSRV 1.65): Remove this.
33 unsafe {
34 super::sanitizer::unpoison_linux_getrandom_result(buf, ret);
35 }
36
37 ret
38 })
39}