| 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 | 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 | if HAS_GETRANDOM.unsync_init(is_getrandom_available) { |
| 21 | sys_fill_exact(dest, |buf| unsafe { |
| 22 | getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0) |
| 23 | }) |
| 24 | } else { |
| 25 | use_file::getrandom_inner(dest) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | fn is_getrandom_available() -> bool { |
| 30 | let res = unsafe { getrandom(core::ptr::null_mut(), 0, libc::GRND_NONBLOCK) }; |
| 31 | if res < 0 { |
| 32 | match last_os_error().raw_os_error() { |
| 33 | Some(libc::ENOSYS) => false, // No kernel support |
| 34 | Some(libc::EPERM) => false, // Blocked by seccomp |
| 35 | _ => true, |
| 36 | } |
| 37 | } else { |
| 38 | true |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | unsafe fn getrandom( |
| 43 | buf: *mut libc::c_void, |
| 44 | buflen: libc::size_t, |
| 45 | flags: libc::c_uint, |
| 46 | ) -> libc::ssize_t { |
| 47 | libc::syscall(libc::SYS_getrandom, buf, buflen, flags) as libc::ssize_t |
| 48 | } |
| 49 | |