Skip to content

Commit 778d4d9

Browse files
committed
Move getrandom_syscall from util_libc to linux_android.
1 parent 45781c7 commit 778d4d9

8 files changed

+51
-56
lines changed

src/getrandom.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! GRND_RANDOM is not recommended. On NetBSD/FreeBSD/Dragonfly/3ds, it does
1616
//! nothing. On illumos, the default pool is used to implement getentropy(2),
1717
//! so we assume it is acceptable here.
18-
use crate::{util_libc::sys_fill_exact, Error};
18+
use crate::{util_unix::sys_fill_exact, Error};
1919
use core::{ffi::c_void, mem::MaybeUninit};
2020

2121
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ use core::mem::MaybeUninit;
217217

218218
mod error;
219219
mod util;
220+
#[cfg(unix)]
221+
mod util_unix;
220222
// To prevent a breaking change when targets are added, we always export the
221223
// register_custom_getrandom macro, so old Custom RNG crates continue to build.
222224
#[cfg(feature = "custom")]

src/linux_android.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
//! Implementation for Linux / Android without `/dev/urandom` fallback
2-
use crate::{util_libc, Error};
2+
use crate::{util_unix::sys_fill_exact, Error};
33
use core::mem::MaybeUninit;
44

55
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
6-
util_libc::sys_fill_exact(dest, util_libc::getrandom_syscall)
6+
sys_fill_exact(dest, getrandom_syscall)
7+
}
8+
9+
// Also used by linux_android_with_fallback to check if the syscall is available.
10+
pub fn getrandom_syscall(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
11+
unsafe {
12+
libc::syscall(
13+
libc::SYS_getrandom,
14+
buf.as_mut_ptr().cast::<core::ffi::c_void>(),
15+
buf.len(),
16+
0,
17+
) as libc::ssize_t
18+
}
719
}

src/linux_android_with_fallback.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
//! Implementation for Linux / Android with `/dev/urandom` fallback
2-
use crate::{
3-
lazy::LazyBool,
4-
linux_android, use_file,
5-
util_libc::{getrandom_syscall, last_os_error},
6-
Error,
7-
};
2+
use crate::{lazy::LazyBool, linux_android, use_file, util_libc::last_os_error, Error};
83
use core::mem::MaybeUninit;
94

105
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
@@ -18,7 +13,7 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1813
}
1914

2015
fn is_getrandom_available() -> bool {
21-
if getrandom_syscall(&mut []) < 0 {
16+
if linux_android::getrandom_syscall(&mut []) < 0 {
2217
match last_os_error().raw_os_error() {
2318
Some(libc::ENOSYS) => false, // No kernel support
2419
// The fallback on EPERM is intentionally not done on Android since this workaround

src/netbsd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Implementation for NetBSD
2-
use crate::{lazy::LazyPtr, util_libc::sys_fill_exact, Error};
2+
use crate::{lazy::LazyPtr, util_unix::sys_fill_exact, Error};
33
use core::{ffi::c_void, mem::MaybeUninit, ptr};
44

55
fn kern_arnd(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {

src/use_file.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
//! Implementations that just need to read from a file
2-
use crate::{
3-
util_libc::{open_readonly, sys_fill_exact},
4-
Error,
5-
};
2+
use crate::{util_libc::open_readonly, util_unix::sys_fill_exact, Error};
63
use core::{
74
cell::UnsafeCell,
85
ffi::c_void,

src/util_libc.rs

-41
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![allow(dead_code)]
22
use crate::Error;
3-
use core::mem::MaybeUninit;
43

54
cfg_if! {
65
if #[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "android"))] {
@@ -46,33 +45,6 @@ pub fn last_os_error() -> Error {
4645
}
4746
}
4847

49-
// Fill a buffer by repeatedly invoking a system call. The `sys_fill` function:
50-
// - should return -1 and set errno on failure
51-
// - should return the number of bytes written on success
52-
pub fn sys_fill_exact(
53-
mut buf: &mut [MaybeUninit<u8>],
54-
sys_fill: impl Fn(&mut [MaybeUninit<u8>]) -> libc::ssize_t,
55-
) -> Result<(), Error> {
56-
while !buf.is_empty() {
57-
let res = sys_fill(buf);
58-
match res {
59-
res if res > 0 => buf = buf.get_mut(res as usize..).ok_or(Error::UNEXPECTED)?,
60-
-1 => {
61-
let err = last_os_error();
62-
// We should try again if the call was interrupted.
63-
if err.raw_os_error() != Some(libc::EINTR) {
64-
return Err(err);
65-
}
66-
}
67-
// Negative return codes not equal to -1 should be impossible.
68-
// EOF (ret = 0) should be impossible, as the data we are reading
69-
// should be an infinite stream of random bytes.
70-
_ => return Err(Error::UNEXPECTED),
71-
}
72-
}
73-
Ok(())
74-
}
75-
7648
/// Open a file in read-only mode.
7749
///
7850
/// # Panics
@@ -99,16 +71,3 @@ pub fn open_readonly(path: &[u8]) -> Result<libc::c_int, Error> {
9971
}
10072
}
10173
}
102-
103-
/// Thin wrapper around the `getrandom()` Linux system call
104-
#[cfg(any(target_os = "android", target_os = "linux"))]
105-
pub fn getrandom_syscall(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
106-
unsafe {
107-
libc::syscall(
108-
libc::SYS_getrandom,
109-
buf.as_mut_ptr().cast::<core::ffi::c_void>(),
110-
buf.len(),
111-
0,
112-
) as libc::ssize_t
113-
}
114-
}

src/util_unix.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![allow(dead_code)]
2+
use crate::{util_libc::last_os_error, Error};
3+
use core::mem::MaybeUninit;
4+
5+
// Fill a buffer by repeatedly invoking a system call. The `sys_fill` function:
6+
// - should return -1 and set errno on failure
7+
// - should return the number of bytes written on success
8+
pub fn sys_fill_exact(
9+
mut buf: &mut [MaybeUninit<u8>],
10+
sys_fill: impl Fn(&mut [MaybeUninit<u8>]) -> libc::ssize_t,
11+
) -> Result<(), Error> {
12+
while !buf.is_empty() {
13+
let res = sys_fill(buf);
14+
match res {
15+
res if res > 0 => buf = buf.get_mut(res as usize..).ok_or(Error::UNEXPECTED)?,
16+
-1 => {
17+
let err = last_os_error();
18+
// We should try again if the call was interrupted.
19+
if err.raw_os_error() != Some(libc::EINTR) {
20+
return Err(err);
21+
}
22+
}
23+
// Negative return codes not equal to -1 should be impossible.
24+
// EOF (ret = 0) should be impossible, as the data we are reading
25+
// should be an infinite stream of random bytes.
26+
_ => return Err(Error::UNEXPECTED),
27+
}
28+
}
29+
Ok(())
30+
}

0 commit comments

Comments
 (0)