Skip to content

Commit 07f86f1

Browse files
committed
Use correct open flags
1 parent a69c6d5 commit 07f86f1

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/rngs/os.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ mod random_device {
262262
static READ_RNG_ONCE: Once = ONCE_INIT;
263263

264264
#[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+
{
266268
READ_RNG_ONCE.call_once(|| {
267269
unsafe { READ_RNG_FILE = Some(Mutex::new(None)) }
268270
});
@@ -274,7 +276,7 @@ mod random_device {
274276
let mut guard = mutex.lock().unwrap();
275277
if (*guard).is_none() {
276278
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)?;
278280
*guard = Some(file);
279281
};
280282
Ok(())
@@ -320,7 +322,7 @@ mod imp {
320322

321323
use std::io;
322324
use std::io::Read;
323-
use std::fs::OpenOptions;
325+
use std::fs::{File, OpenOptions};
324326
use std::os::unix::fs::OpenOptionsExt;
325327
use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
326328
use std::sync::{Once, ONCE_INIT};
@@ -343,7 +345,7 @@ mod imp {
343345
return Ok(OsRng { method: OsRngMethod::GetRandom,
344346
initialized: false });
345347
}
346-
random_device::open("/dev/urandom")?;
348+
random_device::open("/dev/urandom", &|p| File::open(p))?;
347349
Ok(OsRng { method: OsRngMethod::RandomDevice, initialized: false })
348350
}
349351

@@ -497,7 +499,7 @@ mod imp {
497499

498500
impl OsRngImpl for OsRng {
499501
fn new() -> Result<OsRng, Error> {
500-
random_device::open("/dev/urandom")?;
502+
random_device::open("/dev/urandom", &|p| File::open(p))?;
501503
Ok(OsRng { initialized: false })
502504
}
503505

@@ -536,13 +538,14 @@ mod imp {
536538
use Error;
537539
use super::random_device;
538540
use super::OsRngImpl;
541+
use std::fs::File;
539542

540543
#[derive(Clone, Debug)]
541544
pub struct OsRng();
542545

543546
impl OsRngImpl for OsRng {
544547
fn new() -> Result<OsRng, Error> {
545-
random_device::open("/dev/random")?;
548+
random_device::open("/dev/random", &|p| File::open(p))?;
546549
Ok(OsRng())
547550
}
548551

@@ -560,13 +563,14 @@ mod imp {
560563
use Error;
561564
use super::random_device;
562565
use super::OsRngImpl;
566+
use std::fs::File;
563567

564568
#[derive(Clone, Debug)]
565569
pub struct OsRng();
566570

567571
impl OsRngImpl for OsRng {
568572
fn new() -> Result<OsRng, Error> {
569-
random_device::open("/dev/random")?;
573+
random_device::open("/dev/random", &|p| File::open(p))?;
570574
Ok(OsRng())
571575
}
572576

@@ -607,7 +611,7 @@ mod imp {
607611

608612
use std::io;
609613
use std::io::Read;
610-
use std::fs::OpenOptions;
614+
use std::fs::{File, OpenOptions};
611615
use std::os::unix::fs::OpenOptionsExt;
612616
use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
613617

@@ -629,7 +633,11 @@ mod imp {
629633
return Ok(OsRng { method: OsRngMethod::GetRandom,
630634
initialized: false });
631635
}
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)?;
633641
Ok(OsRng { method: OsRngMethod::RandomDevice, initialized: false })
634642
}
635643

@@ -657,10 +665,9 @@ mod imp {
657665
OsRngMethod::RandomDevice => {
658666
if blocking {
659667
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")
664671
.map_err(random_device::map_err)?;
665672
file.read(dest).map_err(random_device::map_err)?;
666673
} else {
@@ -915,13 +922,14 @@ mod imp {
915922
use Error;
916923
use super::random_device;
917924
use super::OsRngImpl;
925+
use std::fs::File;
918926

919927
#[derive(Clone, Debug)]
920928
pub struct OsRng();
921929

922930
impl OsRngImpl for OsRng {
923931
fn new() -> Result<OsRng, Error> {
924-
random_device::open("rand:")?;
932+
random_device::open("rand:", &|p| File::open(p))?;
925933
Ok(OsRng())
926934
}
927935

0 commit comments

Comments
 (0)