Debian Codegraph @

.cargo/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
18 pub use crate::util::{inner_u32, inner_u64};
19
20 #[path = "../util_libc.rs"]
21 mod util_libc;
22
23 const MAX_BYTES: usize = 1024;
24
25 #[inline]
26 pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
27 for chunk in dest.chunks_mut(MAX_BYTES) {
28 let ptr = chunk.as_mut_ptr().cast::<c_void>();
29 let ret = unsafe { libc::getrandom(ptr, chunk.len(), libc::GRND_RANDOM) };
30 // In case the man page has a typo, we also check for negative ret.
31 // If getrandom(2) succeeds, it should have completely filled chunk.
32 match usize::try_from(ret) {
33 // Good. Keep going.
34 Ok(ret) if ret == chunk.len() => {}
35 // The syscall failed.
36 Ok(0) => return Err(util_libc::last_os_error()),
37 // All other cases should be impossible.
38 _ => return Err(Error::UNEXPECTED),
39 }
40 }
41 Ok(())
42 }