@@ -12,8 +12,6 @@ use crate::Error;
1212
1313#[ cfg( target_os = "redox" ) ]
1414const 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 " ;
1715#[ cfg( any(
1816 target_os = "dragonfly" ,
1917 target_os = "emscripten" ,
@@ -40,32 +38,38 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
4038 Ok ( ( ) )
4139}
4240
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+ } ;
5150
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+ }
6060 }
6161 }
62- }
6362
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 ) }
6873 }
6974 }
70- unsafe { open_readonly ( FILE_PATH ) }
7175}
0 commit comments