@@ -12,8 +12,6 @@ use crate::Error;
12
12
13
13
#[ cfg( target_os = "redox" ) ]
14
14
const FILE_PATH : & str = "rand:\0 " ;
15
- #[ cfg( any( target_os = "android" , target_os = "linux" , target_os = "netbsd" ) ) ]
16
- const FILE_PATH : & str = "/dev/urandom\0 " ;
17
15
#[ cfg( any(
18
16
target_os = "dragonfly" ,
19
17
target_os = "emscripten" ,
@@ -40,32 +38,38 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
40
38
Ok ( ( ) )
41
39
}
42
40
43
- fn init_file ( ) -> Option < libc:: c_int > {
44
- if FILE_PATH == "/dev/urandom\0 " {
45
- // Poll /dev/random to make sure it is ok to read from /dev/urandom.
46
- let mut pfd = libc:: pollfd {
47
- fd : unsafe { open_readonly ( "/dev/random\0 " ) ? } ,
48
- events : libc:: POLLIN ,
49
- revents : 0 ,
50
- } ;
41
+ cfg_if ! {
42
+ if #[ cfg( any( target_os = "android" , target_os = "linux" , target_os = "netbsd" ) ) ] {
43
+ fn init_file( ) -> Option <libc:: c_int> {
44
+ // Poll /dev/random to make sure it is ok to read from /dev/urandom.
45
+ let mut pfd = libc:: pollfd {
46
+ fd: unsafe { open_readonly( "/dev/random\0 " ) ? } ,
47
+ events: libc:: POLLIN ,
48
+ revents: 0 ,
49
+ } ;
51
50
52
- let mut res = -1 ;
53
- while res <= 0 {
54
- // A negative timeout means an infinite timeout.
55
- res = unsafe { libc:: poll ( & mut pfd, 1 , -1 ) } ;
56
- if res < 0 {
57
- match last_os_error ( ) . raw_os_error ( ) {
58
- Some ( libc:: EINTR ) | Some ( libc:: EAGAIN ) => { }
59
- _ => break ,
51
+ let mut res = -1 ;
52
+ while res <= 0 {
53
+ // A negative timeout means an infinite timeout.
54
+ res = unsafe { libc:: poll( & mut pfd, 1 , -1 ) } ;
55
+ if res < 0 {
56
+ match last_os_error( ) . raw_os_error( ) {
57
+ Some ( libc:: EINTR ) | Some ( libc:: EAGAIN ) => { }
58
+ _ => break ,
59
+ }
60
60
}
61
61
}
62
- }
63
62
64
- unsafe { libc:: close ( pfd. fd ) } ;
65
- if res != 1 {
66
- // We either hard failed, or poll() returned the wrong pfd.
67
- return None ;
63
+ unsafe { libc:: close( pfd. fd) } ;
64
+ if res != 1 {
65
+ // We either hard failed, or poll() returned the wrong pfd.
66
+ return None ;
67
+ }
68
+ unsafe { open_readonly( "/dev/urandom\0 " ) }
69
+ }
70
+ } else {
71
+ fn init_file( ) -> Option <libc:: c_int> {
72
+ unsafe { open_readonly( FILE_PATH ) }
68
73
}
69
74
}
70
- unsafe { open_readonly ( FILE_PATH ) }
71
75
}
0 commit comments