Skip to content

Commit 10a0ae0

Browse files
authored
Delegate to linux_android from linux_android_with_fallback (#459)
Instead of duplicating the implementation of `linux_android::getrandom_inner` inline, call `linux_android::getrandom_inner`, like we do for `use_file::getrandom_inner`. This way, there is no doubt that they do exactly the same thing. This then allows us to move `getrandom_syscall` to linux_android, which is where it belongs, since it is Linux/Android-specific.
1 parent 05cdf6f commit 10a0ae0

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ cfg_if! {
299299
mod util_libc;
300300
mod use_file;
301301
mod lazy;
302+
mod linux_android;
302303
#[path = "linux_android_with_fallback.rs"] mod imp;
303304
} else if #[cfg(any(target_os = "android", target_os = "linux"))] {
304305
mod util_libc;

src/linux_android.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,17 @@ use crate::{util_libc, 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+
util_libc::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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
//! Implementation for Linux / Android with `/dev/urandom` fallback
2-
use crate::{
3-
lazy::LazyBool,
4-
util_libc::{getrandom_syscall, last_os_error, sys_fill_exact},
5-
{use_file, Error},
6-
};
2+
use crate::{lazy::LazyBool, linux_android, use_file, util_libc::last_os_error, Error};
73
use core::mem::MaybeUninit;
84

95
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
106
// getrandom(2) was introduced in Linux 3.17
117
static HAS_GETRANDOM: LazyBool = LazyBool::new();
128
if HAS_GETRANDOM.unsync_init(is_getrandom_available) {
13-
sys_fill_exact(dest, getrandom_syscall)
9+
linux_android::getrandom_inner(dest)
1410
} else {
1511
use_file::getrandom_inner(dest)
1612
}
1713
}
1814

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

src/util_libc.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,3 @@ pub fn open_readonly(path: &[u8]) -> Result<libc::c_int, Error> {
9999
}
100100
}
101101
}
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-
}

0 commit comments

Comments
 (0)