/rust/registry/src/index.crates.io-6f17d22bba15001f/getrandom-0.2.10/src/linux_android.rs
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2018 Developers of the Rand project. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
4 | | // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
5 | | // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
6 | | // option. This file may not be copied, modified, or distributed |
7 | | // except according to those terms. |
8 | | |
9 | | //! Implementation for Linux / Android |
10 | | use crate::{ |
11 | | util::LazyBool, |
12 | | util_libc::{last_os_error, sys_fill_exact}, |
13 | | {use_file, Error}, |
14 | | }; |
15 | | use core::mem::MaybeUninit; |
16 | | |
17 | 0 | pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { |
18 | | // getrandom(2) was introduced in Linux 3.17 |
19 | | static HAS_GETRANDOM: LazyBool = LazyBool::new(); |
20 | 0 | if HAS_GETRANDOM.unsync_init(is_getrandom_available) { |
21 | 0 | sys_fill_exact(dest, |buf| unsafe { |
22 | 0 | getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0) |
23 | 0 | }) |
24 | | } else { |
25 | 0 | use_file::getrandom_inner(dest) |
26 | | } |
27 | 0 | } |
28 | | |
29 | 0 | fn is_getrandom_available() -> bool { |
30 | 0 | let res = unsafe { getrandom(core::ptr::null_mut(), 0, libc::GRND_NONBLOCK) }; |
31 | 0 | if res < 0 { |
32 | 0 | match last_os_error().raw_os_error() { |
33 | 0 | Some(libc::ENOSYS) => false, // No kernel support |
34 | 0 | Some(libc::EPERM) => false, // Blocked by seccomp |
35 | 0 | _ => true, |
36 | | } |
37 | | } else { |
38 | 0 | true |
39 | | } |
40 | 0 | } |
41 | | |
42 | 0 | unsafe fn getrandom( |
43 | 0 | buf: *mut libc::c_void, |
44 | 0 | buflen: libc::size_t, |
45 | 0 | flags: libc::c_uint, |
46 | 0 | ) -> libc::ssize_t { |
47 | 0 | libc::syscall(libc::SYS_getrandom, buf, buflen, flags) as libc::ssize_t |
48 | 0 | } |