File tree 7 files changed +37
-36
lines changed
7 files changed +37
-36
lines changed Original file line number Diff line number Diff line change 15
15
//! GRND_RANDOM is not recommended. On NetBSD/FreeBSD/Dragonfly/3ds, it does
16
16
//! nothing. On illumos, the default pool is used to implement getentropy(2),
17
17
//! 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 } ;
19
19
use core:: { ffi:: c_void, mem:: MaybeUninit } ;
20
20
21
21
pub fn getrandom_inner ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
Original file line number Diff line number Diff line change @@ -217,6 +217,8 @@ use core::mem::MaybeUninit;
217
217
218
218
mod error;
219
219
mod util;
220
+ #[ cfg( unix) ]
221
+ mod util_unix;
220
222
// To prevent a breaking change when targets are added, we always export the
221
223
// register_custom_getrandom macro, so old Custom RNG crates continue to build.
222
224
#[ cfg( feature = "custom" ) ]
Original file line number Diff line number Diff line change 1
1
//! Implementation for Linux / Android without `/dev/urandom` fallback
2
- use crate :: { util_libc , Error } ;
2
+ use crate :: { util_unix :: sys_fill_exact , Error } ;
3
3
use core:: mem:: MaybeUninit ;
4
4
5
5
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)
7
7
}
8
8
9
9
// Also used by linux_android_with_fallback to check if the syscall is available.
Original file line number Diff line number Diff line change 1
1
//! 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 } ;
3
3
use core:: { ffi:: c_void, mem:: MaybeUninit , ptr} ;
4
4
5
5
fn kern_arnd ( buf : & mut [ MaybeUninit < u8 > ] ) -> libc:: ssize_t {
Original file line number Diff line number Diff line change 1
1
//! 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 } ;
6
3
use core:: {
7
4
cell:: UnsafeCell ,
8
5
ffi:: c_void,
Original file line number Diff line number Diff line change 1
1
#![ allow( dead_code) ]
2
2
use crate :: Error ;
3
- use core:: mem:: MaybeUninit ;
4
3
5
4
cfg_if ! {
6
5
if #[ cfg( any( target_os = "netbsd" , target_os = "openbsd" , target_os = "android" ) ) ] {
@@ -46,33 +45,6 @@ pub fn last_os_error() -> Error {
46
45
}
47
46
}
48
47
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
-
76
48
/// Open a file in read-only mode.
77
49
///
78
50
/// # Panics
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments