Skip to content

Commit

Permalink
Use cfg-if to switch init_file implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
josephlr committed Aug 5, 2019
1 parent 2a638fd commit ac0bf75
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions src/use_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use crate::Error;

#[cfg(target_os = "redox")]
const FILE_PATH: &str = "rand:\0";
#[cfg(any(target_os = "android", target_os = "linux", target_os = "netbsd"))]
const FILE_PATH: &str = "/dev/urandom\0";
#[cfg(any(
target_os = "dragonfly",
target_os = "emscripten",
Expand All @@ -40,32 +38,38 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
Ok(())
}

fn init_file() -> Option<libc::c_int> {
if FILE_PATH == "/dev/urandom\0" {
// Poll /dev/random to make sure it is ok to read from /dev/urandom.
let mut pfd = libc::pollfd {
fd: unsafe { open_readonly("/dev/random\0")? },
events: libc::POLLIN,
revents: 0,
};
cfg_if! {
if #[cfg(any(target_os = "android", target_os = "linux", target_os = "netbsd"))] {
fn init_file() -> Option<libc::c_int> {
// Poll /dev/random to make sure it is ok to read from /dev/urandom.
let mut pfd = libc::pollfd {
fd: unsafe { open_readonly("/dev/random\0")? },
events: libc::POLLIN,
revents: 0,
};

let mut res = -1;
while res <= 0 {
// A negative timeout means an infinite timeout.
res = unsafe { libc::poll(&mut pfd, 1, -1) };
if res < 0 {
match last_os_error().raw_os_error() {
Some(libc::EINTR) | Some(libc::EAGAIN) => {}
_ => break,
let mut res = -1;
while res <= 0 {
// A negative timeout means an infinite timeout.
res = unsafe { libc::poll(&mut pfd, 1, -1) };
if res < 0 {
match last_os_error().raw_os_error() {
Some(libc::EINTR) | Some(libc::EAGAIN) => {}
_ => break,
}
}
}
}

unsafe { libc::close(pfd.fd) };
if res != 1 {
// We either hard failed, or poll() returned the wrong pfd.
return None;
unsafe { libc::close(pfd.fd) };
if res != 1 {
// We either hard failed, or poll() returned the wrong pfd.
return None;
}
unsafe { open_readonly("/dev/urandom\0") }
}
} else {
fn init_file() -> Option<libc::c_int> {
unsafe { open_readonly(FILE_PATH) }
}
}
unsafe { open_readonly(FILE_PATH) }
}

0 comments on commit ac0bf75

Please sign in to comment.