diff --git a/src/guicommon/tabs.rs b/src/guicommon/tabs.rs index 20bb07421..a0570930a 100644 --- a/src/guicommon/tabs.rs +++ b/src/guicommon/tabs.rs @@ -54,13 +54,12 @@ impl Tab { Ok(()) } - #[deprecated(note = "use writer or something else")] - pub fn pty(&self) -> RefMut { + pub fn writer(&self) -> RefMut { self.pty.borrow_mut() } - pub fn writer(&self) -> RefMut { - self.pty.borrow_mut() + pub fn reader(&self) -> Result, Error> { + self.pty.borrow_mut().try_clone_reader() } } diff --git a/src/mux/mod.rs b/src/mux/mod.rs index 815d9a5cf..660171067 100644 --- a/src/mux/mod.rs +++ b/src/mux/mod.rs @@ -1,5 +1,4 @@ use crate::guicommon::tabs::{Tab, TabId}; -use crate::MasterPty; use failure::Error; use std::cell::RefCell; use std::collections::HashMap; @@ -26,11 +25,11 @@ pub trait PtyEventSender: Send { fn send(&self, event: PtyEvent) -> Result<(), Error>; } -fn read_from_tab_pty(sender: Box, tab_id: TabId, mut pty: MasterPty) { +fn read_from_tab_pty(sender: Box, tab_id: TabId, mut reader: Box) { const BUFSIZE: usize = 32 * 1024; let mut buf = [0; BUFSIZE]; loop { - match pty.read(&mut buf) { + match reader.read(&mut buf) { Ok(size) if size == 0 => { eprintln!("read_pty EOF: tab_id {}", tab_id); sender.send(PtyEvent::Terminated { tab_id }).ok(); @@ -61,12 +60,12 @@ fn read_from_tab_pty(sender: Box, tab_id: TabId, mut pty: Master /// As such it only really has Host::writer get called. /// The GUI driven flows provide their own impl of TerminalHost. struct Host<'a> { - pty: &'a mut MasterPty, + writer: &'a mut std::io::Write, } impl<'a> TerminalHost for Host<'a> { fn writer(&mut self) -> &mut std::io::Write { - &mut self.pty + &mut self.writer } fn click_link(&mut self, link: &Rc) { @@ -96,10 +95,9 @@ impl Mux { pub fn add_tab(&self, sender: Box, tab: &Rc) -> Result<(), Error> { self.tabs.borrow_mut().insert(tab.tab_id(), Rc::clone(tab)); - let pty = tab.pty().try_clone()?; - pty.clear_nonblocking()?; + let reader = tab.reader()?; let tab_id = tab.tab_id(); - thread::spawn(move || read_from_tab_pty(sender, tab_id, pty)); + thread::spawn(move || read_from_tab_pty(sender, tab_id, reader)); Ok(()) } @@ -111,7 +109,7 @@ impl Mux { tab.terminal().advance_bytes( data, &mut Host { - pty: &mut tab.pty(), + writer: &mut *tab.writer(), }, ); } diff --git a/src/pty/conpty.rs b/src/pty/conpty.rs index b4db714ed..f365d3de1 100644 --- a/src/pty/conpty.rs +++ b/src/pty/conpty.rs @@ -471,24 +471,8 @@ impl MasterPty { Ok(inner.size.clone()) } - pub fn try_clone(&self) -> Result { - // FIXME: this isn't great. Replace this with a way to - // clone the output handle and read it. - let inner = self.inner.lock().unwrap(); - Ok(Self { - inner: Arc::new(Mutex::new(Inner { - con: PsuedoCon { - con: INVALID_HANDLE_VALUE, - }, - readable: inner.readable.try_clone()?, - writable: inner.writable.try_clone()?, - size: inner.size, - })), - }) - } - - pub fn clear_nonblocking(&self) -> Result<(), Error> { - Ok(()) + pub fn try_clone_reader(&self) -> Result, Error> { + Ok(Box::new(self.inner.readable.try_clone()?)) } } diff --git a/src/pty/unix.rs b/src/pty/unix.rs index 9b52fb760..ea6bc6f51 100644 --- a/src/pty/unix.rs +++ b/src/pty/unix.rs @@ -2,44 +2,100 @@ use failure::Error; use libc::{self, winsize}; -use mio::event::Evented; -use mio::unix::EventedFd; -use mio::{Poll, PollOpt, Ready, Token}; use std::io; use std::mem; -use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; use std::os::unix::process::CommandExt; use std::process::Stdio; use std::ptr; pub use std::process::{Child, Command, ExitStatus}; +pub struct OwnedFd { + fd: RawFd, +} + +impl OwnedFd { + fn try_clone(&self) -> Result { + // Note that linux has a variant of the dup syscall that can set + // the CLOEXEC flag at dup time. We could use that here but the + // additional code complexity isn't worth it: it's just a couple + // of syscalls at startup to do it the portable way below. + let new_fd = unsafe { libc::dup(self.fd) }; + if new_fd == -1 { + bail!("dup of pty fd failed: {:?}", io::Error::last_os_error()) + } + match cloexec(new_fd) { + Ok(_) => Ok(OwnedFd { fd: new_fd }), + Err(err) => { + unsafe { libc::close(new_fd) }; + Err(err) + } + } + } +} + +impl Drop for OwnedFd { + fn drop(&mut self) { + unsafe { + libc::close(self.fd); + } + } +} + +impl AsRawFd for OwnedFd { + fn as_raw_fd(&self) -> RawFd { + self.fd + } +} + +impl IntoRawFd for OwnedFd { + fn into_raw_fd(self) -> RawFd { + self.fd + } +} + +impl FromRawFd for OwnedFd { + unsafe fn from_raw_fd(fd: RawFd) -> Self { + Self { fd } + } +} + +impl io::Read for OwnedFd { + fn read(&mut self, buf: &mut [u8]) -> Result { + let size = unsafe { libc::read(self.fd, buf.as_mut_ptr() as *mut _, buf.len()) }; + if size == -1 { + Err(io::Error::last_os_error()) + } else { + Ok(size as usize) + } + } +} + +impl io::Write for OwnedFd { + fn write(&mut self, buf: &[u8]) -> Result { + let size = unsafe { libc::write(self.fd, buf.as_ptr() as *const _, buf.len()) }; + if size == -1 { + Err(io::Error::last_os_error()) + } else { + Ok(size as usize) + } + } + fn flush(&mut self) -> Result<(), io::Error> { + Ok(()) + } +} + /// Represents the master end of a pty. /// The file descriptor will be closed when the Pty is dropped. pub struct MasterPty { - fd: RawFd, + fd: OwnedFd, } /// Represents the slave end of a pty. /// The file descriptor will be closed when the Pty is dropped. pub struct SlavePty { - fd: RawFd, -} - -impl Drop for MasterPty { - fn drop(&mut self) { - unsafe { - libc::close(self.fd); - } - } -} - -impl Drop for SlavePty { - fn drop(&mut self) { - unsafe { - libc::close(self.fd); - } - } + fd: OwnedFd, } /// Helper function to set the close-on-exec flag for a raw descriptor @@ -61,26 +117,6 @@ fn cloexec(fd: RawFd) -> Result<(), Error> { Ok(()) } -/// Helper function to duplicate a file descriptor. -/// The duplicated descriptor will have the close-on-exec flag set. -fn dup(fd: RawFd) -> Result { - // Note that linux has a variant of the dup syscall that can set - // the CLOEXEC flag at dup time. We could use that here but the - // additional code complexity isn't worth it: it's just a couple - // of syscalls at startup to do it the portable way below. - let new_fd = unsafe { libc::dup(fd) }; - if new_fd == -1 { - bail!("dup of pty fd failed: {:?}", io::Error::last_os_error()) - } - match cloexec(new_fd) { - Ok(_) => Ok(new_fd), - Err(err) => { - unsafe { libc::close(new_fd) }; - Err(err) - } - } -} - fn clear_nonblocking(fd: RawFd) -> Result<(), Error> { let flags = unsafe { libc::fcntl(fd, libc::F_GETFL, 0) }; if flags == -1 { @@ -152,17 +188,21 @@ pub fn openpty( bail!("failed to openpty: {:?}", io::Error::last_os_error()); } - let master = MasterPty { fd: master }; - let slave = SlavePty { fd: slave }; + let master = MasterPty { + fd: OwnedFd { fd: master }, + }; + let slave = SlavePty { + fd: OwnedFd { fd: slave }, + }; // Ensure that these descriptors will get closed when we execute // the child process. This is done after constructing the Pty // instances so that we ensure that the Ptys get drop()'d if // the cloexec() functions fail (unlikely!). - cloexec(master.fd)?; - cloexec(slave.fd)?; + cloexec(master.fd.as_raw_fd())?; + cloexec(slave.fd.as_raw_fd())?; - set_nonblocking(master.fd)?; + set_nonblocking(master.fd.as_raw_fd())?; // FIXME: do we need this any more? Ok((master, slave)) } @@ -170,7 +210,8 @@ pub fn openpty( impl SlavePty { /// Helper for setting up a Command instance fn as_stdio(&self) -> Result { - dup(self.fd).map(|fd| unsafe { Stdio::from_raw_fd(fd) }) + let dup = self.fd.try_clone()?; + Ok(unsafe { Stdio::from_raw_fd(dup.into_raw_fd()) }) } /// this method prepares a Command builder to spawn a process with the Pty @@ -256,7 +297,7 @@ impl MasterPty { ws_ypixel: pixel_height, }; - if unsafe { libc::ioctl(self.fd, libc::TIOCSWINSZ, &size as *const _) } != 0 { + if unsafe { libc::ioctl(self.fd.as_raw_fd(), libc::TIOCSWINSZ, &size as *const _) } != 0 { bail!( "failed to ioctl(TIOCSWINSZ): {:?}", io::Error::last_os_error() @@ -268,7 +309,7 @@ impl MasterPty { pub fn get_size(&self) -> Result { let mut size: winsize = unsafe { mem::zeroed() }; - if unsafe { libc::ioctl(self.fd, libc::TIOCGWINSZ, &mut size as *mut _) } != 0 { + if unsafe { libc::ioctl(self.fd.as_raw_fd(), libc::TIOCGWINSZ, &mut size as *mut _) } != 0 { bail!( "failed to ioctl(TIOCGWINSZ): {:?}", io::Error::last_os_error() @@ -277,70 +318,24 @@ impl MasterPty { Ok(size) } - pub fn clear_nonblocking(&self) -> Result<(), Error> { - clear_nonblocking(self.fd) - } - - pub fn try_clone(&self) -> Result { - let fd = dup(self.fd)?; - Ok(Self { fd }) - } -} - -impl AsRawFd for MasterPty { - fn as_raw_fd(&self) -> RawFd { - self.fd + pub fn try_clone_reader(&self) -> Result, Error> { + let fd = self.fd.try_clone()?; + clear_nonblocking(fd.as_raw_fd())?; + Ok(Box::new(fd)) } } impl io::Write for MasterPty { fn write(&mut self, buf: &[u8]) -> Result { - let size = unsafe { libc::write(self.fd, buf.as_ptr() as *const _, buf.len()) }; - if size == -1 { - Err(io::Error::last_os_error()) - } else { - Ok(size as usize) - } + self.fd.write(buf) } fn flush(&mut self) -> Result<(), io::Error> { - Ok(()) + self.fd.flush() } } impl io::Read for MasterPty { fn read(&mut self, buf: &mut [u8]) -> Result { - let size = unsafe { libc::read(self.fd, buf.as_mut_ptr() as *mut _, buf.len()) }; - if size == -1 { - Err(io::Error::last_os_error()) - } else { - Ok(size as usize) - } - } -} - -/// Glue for working with mio -impl Evented for MasterPty { - fn register( - &self, - poll: &Poll, - token: Token, - interest: Ready, - opts: PollOpt, - ) -> io::Result<()> { - EventedFd(&self.fd).register(poll, token, interest, opts) - } - - fn reregister( - &self, - poll: &Poll, - token: Token, - interest: Ready, - opts: PollOpt, - ) -> io::Result<()> { - EventedFd(&self.fd).reregister(poll, token, interest, opts) - } - - fn deregister(&self, poll: &Poll) -> io::Result<()> { - EventedFd(&self.fd).deregister(poll) + self.fd.read(buf) } } diff --git a/src/xwindows/xwin.rs b/src/xwindows/xwin.rs index 6c17b90da..49ba53271 100644 --- a/src/xwindows/xwin.rs +++ b/src/xwindows/xwin.rs @@ -202,7 +202,7 @@ impl X11TerminalWindow { None => return Ok(()), }; tab.terminal() - .mouse_event(event, &mut TabHost::new(&mut tab.pty(), &mut self.host))?; + .mouse_event(event, &mut TabHost::new(&mut *tab.writer(), &mut self.host))?; Ok(()) }