Skip to content

Commit 0adc101

Browse files
committed
Rename to sys_fill_exact and add docs
1 parent 813b117 commit 0adc101

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

src/freebsd.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// except according to those terms.
88

99
//! Implementation for FreeBSD
10-
use crate::util_libc::{fill_exact, Weak};
10+
use crate::util_libc::{sys_fill_exact, Weak};
1111
use crate::Error;
1212
use core::num::NonZeroU32;
1313
use core::{mem, ptr};
@@ -39,9 +39,9 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
3939
static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") };
4040
if let Some(fptr) = GETRANDOM.ptr() {
4141
let func: GetRandomFn = unsafe { mem::transmute(fptr) };
42-
fill_exact(dest, |buf| unsafe { func(buf.as_mut_ptr(), buf.len(), 0) })
42+
sys_fill_exact(dest, |buf| unsafe { func(buf.as_mut_ptr(), buf.len(), 0) })
4343
} else {
44-
fill_exact(dest, kern_arnd)
44+
sys_fill_exact(dest, kern_arnd)
4545
}
4646
}
4747

src/linux_android.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
extern crate std;
1111

1212
use crate::util::LazyBool;
13-
use crate::util_libc::fill_exact;
13+
use crate::util_libc::sys_fill_exact;
1414
use crate::{use_file, Error};
1515
use core::num::NonZeroU32;
1616
use std::io;
1717

1818
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
1919
static HAS_GETRANDOM: LazyBool = LazyBool::new();
2020
if HAS_GETRANDOM.unsync_init(is_getrandom_available) {
21-
fill_exact(dest, |buf| unsafe {
21+
sys_fill_exact(dest, |buf| unsafe {
2222
libc::syscall(libc::SYS_getrandom, buf.as_mut_ptr(), buf.len(), 0) as libc::ssize_t
2323
})
2424
} else {

src/solaris_illumos.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! To make sure we can compile on both Solaris and its derivatives, as well as
1818
//! function, we check for the existance of getrandom(2) in libc by calling
1919
//! libc::dlsym.
20-
use crate::util_libc::{fill_exact, Weak};
20+
use crate::util_libc::{sys_fill_exact, Weak};
2121
use crate::{use_file, Error};
2222
use core::mem;
2323
use core::num::NonZeroU32;
@@ -34,7 +34,7 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
3434
// 256 bytes is the lowest common denominator across all the Solaris
3535
// derived platforms for atomically obtaining random data.
3636
for chunk in dest.chunks_mut(256) {
37-
fill_exact(chunk, |buf| unsafe {
37+
sys_fill_exact(chunk, |buf| unsafe {
3838
func(buf.as_mut_ptr(), buf.len(), 0) as libc::ssize_t
3939
})?
4040
}

src/util_libc.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ use crate::Error;
1212
use core::ptr::NonNull;
1313
use std::io;
1414

15-
pub fn fill_exact(mut buf: &mut [u8], f: impl Fn(&mut [u8]) -> libc::ssize_t) -> Result<(), Error> {
15+
// Fill a buffer by repeatedly invoking a system call. The `sys_fill` function:
16+
// - should return -1 and set errno on failure
17+
// - should return the number of bytes written on success
18+
pub fn sys_fill_exact(
19+
mut buf: &mut [u8],
20+
sys_fill: impl Fn(&mut [u8]) -> libc::ssize_t,
21+
) -> Result<(), Error> {
1622
while !buf.is_empty() {
17-
let res = f(buf);
23+
let res = sys_fill(buf);
1824
if res < 0 {
1925
let err = io::Error::last_os_error();
2026
// We should try again if the call was interrupted.

0 commit comments

Comments
 (0)