Skip to content

Commit c05a907

Browse files
committed
Move sys_fill_exact from util_libc to util_unix.
Prepare for removing the libc dependency from it.
1 parent 10a0ae0 commit c05a907

File tree

7 files changed

+37
-36
lines changed

7 files changed

+37
-36
lines changed

src/getrandom.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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, getrandom_syscall)
6+
sys_fill_exact(dest, getrandom_syscall)
77
}
88

99
// Also used by linux_android_with_fallback to check if the syscall is available.

src/netbsd.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 0 additions & 28 deletions
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

src/util_unix.rs

Lines changed: 30 additions & 0 deletions
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)