Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/linux_android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use core::cell::RefCell;
use core::num::NonZeroU32;
use core::sync::atomic::{AtomicBool, Ordering};

// This flag tells getrandom() to return EAGAIN instead of blocking.
const GRND_NONBLOCK: libc::c_uint = 0x0001;
static RNG_INIT: AtomicBool = AtomicBool::new(false);

enum RngSource {
Expand All @@ -27,9 +29,10 @@ thread_local!(
static RNG_SOURCE: RefCell<Option<RngSource>> = RefCell::new(None);
);

fn syscall_getrandom(dest: &mut [u8]) -> Result<(), io::Error> {
fn syscall_getrandom(dest: &mut [u8], block: bool) -> Result<(), io::Error> {
let flags = if block { 0 } else { GRND_NONBLOCK };
let ret = unsafe {
libc::syscall(libc::SYS_getrandom, dest.as_mut_ptr(), dest.len(), 0)
libc::syscall(libc::SYS_getrandom, dest.as_mut_ptr(), dest.len(), flags)
};
if ret < 0 || (ret as usize) != dest.len() {
error!("Linux getrandom syscall failed with return value {}", ret);
Expand All @@ -56,7 +59,7 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
Ok(s)
}, |f| {
match f {
RngSource::GetRandom => syscall_getrandom(dest),
RngSource::GetRandom => syscall_getrandom(dest, true),
RngSource::Device(f) => f.read_exact(dest),
}.map_err(From::from)
})
Expand All @@ -71,7 +74,7 @@ fn is_getrandom_available() -> bool {

CHECKER.call_once(|| {
let mut buf: [u8; 0] = [];
let available = match syscall_getrandom(&mut buf) {
let available = match syscall_getrandom(&mut buf, false) {
Ok(()) => true,
Err(err) => err.raw_os_error() != Some(libc::ENOSYS),
};
Expand Down