9
9
//! Implementations that just need to read from a file
10
10
extern crate std;
11
11
12
- use crate :: util_libc:: { last_os_error, LazyFd } ;
12
+ use crate :: util_libc:: { last_os_error, open_readonly , sys_fill_exact , LazyFd } ;
13
13
use crate :: Error ;
14
- use core:: mem:: ManuallyDrop ;
15
- use std:: os:: unix:: io:: { FromRawFd , IntoRawFd , RawFd } ;
16
- use std:: { fs:: File , io:: Read } ;
17
14
18
15
#[ cfg( target_os = "redox" ) ]
19
- const FILE_PATH : & str = "rand:" ;
16
+ const FILE_PATH : & str = "rand:\0 " ;
20
17
#[ cfg( any( target_os = "android" , target_os = "linux" , target_os = "netbsd" ) ) ]
21
- const FILE_PATH : & str = "/dev/urandom" ;
18
+ const FILE_PATH : & str = "/dev/urandom\0 " ;
22
19
#[ cfg( any(
23
20
target_os = "dragonfly" ,
24
21
target_os = "emscripten" ,
@@ -27,32 +24,50 @@ const FILE_PATH: &str = "/dev/urandom";
27
24
target_os = "solaris" ,
28
25
target_os = "illumos"
29
26
) ) ]
30
- const FILE_PATH : & str = "/dev/random" ;
27
+ const FILE_PATH : & str = "/dev/random\0 " ;
31
28
32
29
pub fn getrandom_inner ( dest : & mut [ u8 ] ) -> Result < ( ) , Error > {
33
30
static FD : LazyFd = LazyFd :: new ( ) ;
34
31
let fd = FD . init ( init_file) . ok_or ( last_os_error ( ) ) ?;
35
- let file = ManuallyDrop :: new ( unsafe { File :: from_raw_fd ( fd) } ) ;
36
- let mut file_ref: & File = & file;
32
+ let read = |buf : & mut [ u8 ] | unsafe { libc:: read ( fd, buf. as_mut_ptr ( ) as * mut _ , buf. len ( ) ) } ;
37
33
38
34
if cfg ! ( target_os = "emscripten" ) {
39
35
// `Crypto.getRandomValues` documents `dest` should be at most 65536 bytes.
40
36
for chunk in dest. chunks_mut ( 65536 ) {
41
- file_ref . read_exact ( chunk) ?;
37
+ sys_fill_exact ( chunk, read ) ?;
42
38
}
43
39
} else {
44
- file_ref . read_exact ( dest) ?;
40
+ sys_fill_exact ( dest, read ) ?;
45
41
}
46
42
Ok ( ( ) )
47
43
}
48
44
49
- fn init_file ( ) -> Option < RawFd > {
50
- if FILE_PATH == "/dev/urandom" {
51
- // read one byte from "/dev/random" to ensure that OS RNG has initialized
52
- File :: open ( "/dev/random" )
53
- . ok ( ) ?
54
- . read_exact ( & mut [ 0u8 ; 1 ] )
55
- . ok ( ) ?;
45
+ fn init_file ( ) -> Option < libc:: c_int > {
46
+ if FILE_PATH == "/dev/urandom\0 " {
47
+ // Poll /dev/random to make sure it is ok to read from /dev/urandom.
48
+ let mut pfd = libc:: pollfd {
49
+ fd : unsafe { open_readonly ( "/dev/random\0 " ) ? } ,
50
+ events : libc:: POLLIN ,
51
+ revents : 0 ,
52
+ } ;
53
+
54
+ let mut res = -1 ;
55
+ while res <= 0 {
56
+ // A negative timeout means an infinite timeout.
57
+ res = unsafe { libc:: poll ( & mut pfd, 1 , -1 ) } ;
58
+ if res < 0 {
59
+ match last_os_error ( ) . raw_os_error ( ) {
60
+ Some ( libc:: EINTR ) | Some ( libc:: EAGAIN ) => { }
61
+ _ => break ,
62
+ }
63
+ }
64
+ }
65
+
66
+ unsafe { libc:: close ( pfd. fd ) } ;
67
+ if res != 1 {
68
+ // We either hard failed, or poll() returned the wrong pfd.
69
+ return None ;
70
+ }
56
71
}
57
- Some ( File :: open ( FILE_PATH ) . ok ( ) ? . into_raw_fd ( ) )
72
+ unsafe { open_readonly ( FILE_PATH ) }
58
73
}
0 commit comments