diff --git a/termwiz/src/terminal/mod.rs b/termwiz/src/terminal/mod.rs index 90f361681..23ab5344e 100644 --- a/termwiz/src/terminal/mod.rs +++ b/termwiz/src/terminal/mod.rs @@ -8,6 +8,8 @@ use num::{self, NumCast}; use std::fmt::Display; use std::time::Duration; +#[cfg(unix)] +pub mod poll; #[cfg(unix)] pub mod unix; #[cfg(windows)] diff --git a/termwiz/src/terminal/poll.rs b/termwiz/src/terminal/poll.rs new file mode 100644 index 000000000..ee6e04737 --- /dev/null +++ b/termwiz/src/terminal/poll.rs @@ -0,0 +1,19 @@ +use libc::pollfd; +use std::time::Duration; + +pub fn poll(pfd: &mut [pollfd], duration: Option) -> Result { + let poll_result = unsafe { + libc::poll( + pfd.as_mut_ptr(), + pfd.len() as _, + duration + .map(|wait| wait.as_millis() as libc::c_int) + .unwrap_or(-1), + ) + }; + if poll_result < 0 { + Err(std::io::Error::last_os_error()) + } else { + Ok(poll_result as usize) + } +} diff --git a/termwiz/src/terminal/unix.rs b/termwiz/src/terminal/unix.rs index f4ca888ca..120b5a076 100644 --- a/termwiz/src/terminal/unix.rs +++ b/termwiz/src/terminal/unix.rs @@ -1,6 +1,6 @@ use failure::{bail, format_err, Error, Fallible}; use filedescriptor::FileDescriptor; -use libc::{self, poll, pollfd, winsize, POLLIN}; +use libc::{self, pollfd, winsize, POLLIN, POLLNVAL}; use signal_hook::{self, SigId}; use std::collections::VecDeque; use std::fs::OpenOptions; @@ -415,17 +415,7 @@ impl Terminal for UnixTerminal { Blocking::DoNotWait })?; - let poll_result = unsafe { - poll( - pfd.as_mut_ptr(), - pfd.len() as _, - wait.map(|wait| wait.as_millis() as libc::c_int) - .unwrap_or(-1), - ) - }; - if poll_result < 0 { - let err = IoError::last_os_error(); - + if let Err(err) = super::poll::poll(&mut pfd, wait) { if err.kind() == ErrorKind::Interrupted { // SIGWINCH may have been the source of the interrupt. // Check for that now so that we reduce the latency of @@ -437,7 +427,7 @@ impl Terminal for UnixTerminal { return Ok(None); } return Err(format_err!("poll(2) error: {}", err)); - } + }; if pfd[0].revents != 0 { // SIGWINCH received via our pipe? @@ -448,6 +438,9 @@ impl Terminal for UnixTerminal { if pfd[1].revents != 0 { let mut buf = [0u8; 64]; + if pfd[1].revents & POLLNVAL != 0 { + bail!("poll reports POLLNVAL for tty input"); + } match self.read.read(&mut buf) { Ok(n) => { let input_queue = &mut self.input_queue;