Debian Codegraph @

debian/rust-vendor/getrandom/src/backends/solaris.rs

raw
1 //! Solaris implementation using getrandom(2).
2 //!
3 //! While getrandom(2) has been available since Solaris 11.3, it has a few
4 //! quirks not present on other OSes. First, on Solaris 11.3, calls will always
5 //! fail if bufsz > 1024. Second, it will always either fail or completely fill
6 //! the buffer (returning bufsz). Third, error is indicated by returning 0,
7 //! rather than by returning -1. Finally, "if GRND_RANDOM is not specified
8 //! then getrandom(2) is always a non blocking call". This _might_ imply that
9 //! in early-boot scenarios with low entropy, getrandom(2) will not properly
10 //! block. To be safe, we set GRND_RANDOM, mirroring the man page examples.
11 //!
12 //! For more information, see the man page linked in lib.rs and this blog post:
13 //! https://blogs.oracle.com/solaris/post/solaris-new-system-calls-getentropy2-and-getrandom2
14 //! which also explains why this crate should not use getentropy(2).
15 use crate::Error;
16 use core::{ffi::c_void, mem::MaybeUninit};
17 use libc::___errno as errno_location;
18
19 pub use crate::util::{inner_u32, inner_u64};
20
21 const MAX_BYTES: usize = 1024;
22
23 #[inline]
24 pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
25 for chunk in dest.chunks_mut(MAX_BYTES) {
26 let ptr = chunk.as_mut_ptr().cast::<c_void>();
27 let ret = unsafe { libc::getrandom(ptr, chunk.len(), libc::GRND_RANDOM) };
28 // In case the man page has a typo, we also check for negative ret.
29 // If getrandom(2) succeeds, it should have completely filled chunk.
30 match usize::try_from(ret) {
31 // Good. Keep going.
32 Ok(ret) if ret == chunk.len() => {}
33 // The syscall failed.
34 Ok(0) => {
35 let errno = unsafe { core::ptr::read(errno_location()) };
36 return Err(Error::from_errno(errno));
37 }
38 // All other cases should be impossible.
39 _ => return Err(Error::UNEXPECTED),
40 }
41 }
42 Ok(())
43 }