Skip to content

Instantly share code, notes, and snippets.

@osa1
Created November 10, 2020 06:30
Show Gist options
  • Select an option

  • Save osa1/02eaaf1dabab8f455dc30d4461abd009 to your computer and use it in GitHub Desktop.

Select an option

Save osa1/02eaaf1dabab8f455dc30d4461abd009 to your computer and use it in GitHub Desktop.
/// Set `stdin` to non-blocking mode.
pub fn set_stdin_nonblocking() {
use nix::fcntl::{fcntl, FcntlArg, OFlag};
let mut stdin_flags: OFlag = match fcntl(libc::STDIN_FILENO, FcntlArg::F_GETFL) {
Err(err) => {
panic!("Unable to read stdin flags: {:?}", err);
}
Ok(flags) => match OFlag::from_bits(flags) {
None => panic!("Can't parse stdin flags: {:#?}", flags),
Some(flags) => flags,
},
};
stdin_flags.set(OFlag::O_NONBLOCK, true);
match fcntl(libc::STDIN_FILENO, FcntlArg::F_SETFL(stdin_flags)) {
Err(err) => {
panic!("Can't set stdin flags: {:?}", err);
}
Ok(_) => {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment