@@ -262,7 +262,9 @@ mod random_device {
262
262
static READ_RNG_ONCE : Once = ONCE_INIT ;
263
263
264
264
#[ allow( unused) ]
265
- pub fn open ( path : & str ) -> Result < ( ) , Error > {
265
+ pub fn open < F > ( path : & ' static str , open_fn : F ) -> Result < ( ) , Error >
266
+ where F : Fn ( & ' static str ) -> Result < File , io:: Error >
267
+ {
266
268
READ_RNG_ONCE . call_once ( || {
267
269
unsafe { READ_RNG_FILE = Some ( Mutex :: new ( None ) ) }
268
270
} ) ;
@@ -274,7 +276,7 @@ mod random_device {
274
276
let mut guard = mutex. lock ( ) . unwrap ( ) ;
275
277
if ( * guard) . is_none ( ) {
276
278
info ! ( "OsRng: opening random device {}" , path) ;
277
- let file = File :: open ( path) . map_err ( map_err) ?;
279
+ let file = open_fn ( path) . map_err ( map_err) ?;
278
280
* guard = Some ( file) ;
279
281
} ;
280
282
Ok ( ( ) )
@@ -320,7 +322,7 @@ mod imp {
320
322
321
323
use std:: io;
322
324
use std:: io:: Read ;
323
- use std:: fs:: OpenOptions ;
325
+ use std:: fs:: { File , OpenOptions } ;
324
326
use std:: os:: unix:: fs:: OpenOptionsExt ;
325
327
use std:: sync:: atomic:: { AtomicBool , ATOMIC_BOOL_INIT , Ordering } ;
326
328
use std:: sync:: { Once , ONCE_INIT } ;
@@ -343,7 +345,7 @@ mod imp {
343
345
return Ok ( OsRng { method : OsRngMethod :: GetRandom ,
344
346
initialized : false } ) ;
345
347
}
346
- random_device:: open ( "/dev/urandom" ) ?;
348
+ random_device:: open ( "/dev/urandom" , & |p| File :: open ( p ) ) ?;
347
349
Ok ( OsRng { method : OsRngMethod :: RandomDevice , initialized : false } )
348
350
}
349
351
@@ -497,7 +499,7 @@ mod imp {
497
499
498
500
impl OsRngImpl for OsRng {
499
501
fn new ( ) -> Result < OsRng , Error > {
500
- random_device:: open ( "/dev/urandom" ) ?;
502
+ random_device:: open ( "/dev/urandom" , & |p| File :: open ( p ) ) ?;
501
503
Ok ( OsRng { initialized : false } )
502
504
}
503
505
@@ -536,13 +538,14 @@ mod imp {
536
538
use Error ;
537
539
use super :: random_device;
538
540
use super :: OsRngImpl ;
541
+ use std:: fs:: File ;
539
542
540
543
#[ derive( Clone , Debug ) ]
541
544
pub struct OsRng ( ) ;
542
545
543
546
impl OsRngImpl for OsRng {
544
547
fn new ( ) -> Result < OsRng , Error > {
545
- random_device:: open ( "/dev/random" ) ?;
548
+ random_device:: open ( "/dev/random" , & |p| File :: open ( p ) ) ?;
546
549
Ok ( OsRng ( ) )
547
550
}
548
551
@@ -560,13 +563,14 @@ mod imp {
560
563
use Error ;
561
564
use super :: random_device;
562
565
use super :: OsRngImpl ;
566
+ use std:: fs:: File ;
563
567
564
568
#[ derive( Clone , Debug ) ]
565
569
pub struct OsRng ( ) ;
566
570
567
571
impl OsRngImpl for OsRng {
568
572
fn new ( ) -> Result < OsRng , Error > {
569
- random_device:: open ( "/dev/random" ) ?;
573
+ random_device:: open ( "/dev/random" , & |p| File :: open ( p ) ) ?;
570
574
Ok ( OsRng ( ) )
571
575
}
572
576
@@ -607,7 +611,7 @@ mod imp {
607
611
608
612
use std:: io;
609
613
use std:: io:: Read ;
610
- use std:: fs:: OpenOptions ;
614
+ use std:: fs:: { File , OpenOptions } ;
611
615
use std:: os:: unix:: fs:: OpenOptionsExt ;
612
616
use std:: sync:: atomic:: { AtomicBool , ATOMIC_BOOL_INIT , Ordering } ;
613
617
@@ -629,7 +633,11 @@ mod imp {
629
633
return Ok ( OsRng { method : OsRngMethod :: GetRandom ,
630
634
initialized : false } ) ;
631
635
}
632
- random_device:: open ( "/dev/random" ) ?;
636
+ let open = |p| OpenOptions :: new ( )
637
+ . read ( true )
638
+ . custom_flags ( libc:: O_NONBLOCK )
639
+ . open ( p) ;
640
+ random_device:: open ( "/dev/random" , & open) ?;
633
641
Ok ( OsRng { method : OsRngMethod :: RandomDevice , initialized : false } )
634
642
}
635
643
@@ -657,10 +665,9 @@ mod imp {
657
665
OsRngMethod :: RandomDevice => {
658
666
if blocking {
659
667
info ! ( "OsRng: testing random device /dev/random" ) ;
660
- let mut file = OpenOptions :: new ( )
661
- . read ( true )
662
- . custom_flags ( libc:: O_NONBLOCK )
663
- . open ( "/dev/random" )
668
+ // We already have a non-blocking handle, but now need a
669
+ // blocking one. Not much choice except opening it twice
670
+ let mut file = File :: open ( "/dev/random" )
664
671
. map_err ( random_device:: map_err) ?;
665
672
file. read ( dest) . map_err ( random_device:: map_err) ?;
666
673
} else {
@@ -915,13 +922,14 @@ mod imp {
915
922
use Error ;
916
923
use super :: random_device;
917
924
use super :: OsRngImpl ;
925
+ use std:: fs:: File ;
918
926
919
927
#[ derive( Clone , Debug ) ]
920
928
pub struct OsRng ( ) ;
921
929
922
930
impl OsRngImpl for OsRng {
923
931
fn new ( ) -> Result < OsRng , Error > {
924
- random_device:: open ( "rand:" ) ?;
932
+ random_device:: open ( "rand:" , & |p| File :: open ( p ) ) ?;
925
933
Ok ( OsRng ( ) )
926
934
}
927
935
0 commit comments