File tree 3 files changed +14
-5
lines changed
3 files changed +14
-5
lines changed Original file line number Diff line number Diff line change 5
5
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6
6
// option. This file may not be copied, modified, or distributed
7
7
// except according to those terms.
8
-
9
-
10
8
#![ no_std]
11
9
12
10
#[ cfg( any(
Original file line number Diff line number Diff line change @@ -15,6 +15,9 @@ use std::fs::File;
15
15
use std:: io;
16
16
use std:: io:: Read ;
17
17
use std:: cell:: RefCell ;
18
+ use std:: sync:: atomic:: { AtomicBool , ATOMIC_BOOL_INIT , Ordering } ;
19
+
20
+ static RNG_INIT : AtomicBool = ATOMIC_BOOL_INIT ;
18
21
19
22
enum RngSource {
20
23
GetRandom ,
@@ -44,7 +47,10 @@ pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
44
47
} else {
45
48
// read one byte from "/dev/random" to ensure that
46
49
// OS RNG has initialized
47
- File :: open ( "/dev/random" ) ?. read_exact ( & mut [ 0u8 ; 1 ] ) ?;
50
+ if !RNG_INIT . load ( Ordering :: Relaxed ) {
51
+ File :: open ( "/dev/random" ) ?. read_exact ( & mut [ 0u8 ; 1 ] ) ?;
52
+ RNG_INIT . store ( true , Ordering :: Relaxed )
53
+ }
48
54
RngSource :: Device ( File :: open ( "/dev/urandom" ) ?)
49
55
} ;
50
56
Ok ( s)
@@ -58,7 +64,6 @@ pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
58
64
}
59
65
60
66
fn is_getrandom_available ( ) -> bool {
61
- use std:: sync:: atomic:: { AtomicBool , ATOMIC_BOOL_INIT , Ordering } ;
62
67
use std:: sync:: { Once , ONCE_INIT } ;
63
68
64
69
static CHECKER : Once = ONCE_INIT ;
Original file line number Diff line number Diff line change @@ -13,6 +13,9 @@ use super::utils::use_init;
13
13
use std:: fs:: File ;
14
14
use std:: io:: Read ;
15
15
use std:: cell:: RefCell ;
16
+ use std:: sync:: atomic:: { AtomicBool , ATOMIC_BOOL_INIT , Ordering } ;
17
+
18
+ static RNG_INIT : AtomicBool = ATOMIC_BOOL_INIT ;
16
19
17
20
thread_local ! ( static RNG_FILE : RefCell <Option <File >> = RefCell :: new( None ) ) ;
18
21
@@ -21,7 +24,10 @@ pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> {
21
24
use_init ( f, || {
22
25
// read one byte from "/dev/random" to ensure that
23
26
// OS RNG has initialized
24
- File :: open ( "/dev/random" ) ?. read_exact ( & mut [ 0u8 ; 1 ] ) ?;
27
+ if !RNG_INIT . load ( Ordering :: Relaxed ) {
28
+ File :: open ( "/dev/random" ) ?. read_exact ( & mut [ 0u8 ; 1 ] ) ?;
29
+ RNG_INIT . store ( true , Ordering :: Relaxed )
30
+ }
25
31
File :: open ( "/dev/urandom" )
26
32
} , |f| f. read_exact ( dest) )
27
33
} ) . map_err ( |_| Error :: Unknown )
You can’t perform that action at this time.
0 commit comments