Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

20.2 KB
Raw Blame
//! Working with pseudo-terminals

use crate::{Child, CommandBuilder, MasterPty, PtyPair, PtySize, PtySystem, SlavePty};
use anyhow::{bail, Error};
use filedescriptor::FileDescriptor;
use libc::{self, winsize};
use std::cell::RefCell;
use std::ffi::OsStr;
use std::io::{Read, Write};
use std::os::fd::AsFd;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::io::{AsRawFd, FromRawFd};
use std::os::unix::process::CommandExt;
use std::path::PathBuf;
use std::sync::Arc;
use std::{io, mem, ptr};

pub use std::os::unix::io::RawFd;

#[derive(Default)]
pub struct UnixPtySystem {}

fn openpty(size: PtySize) -> anyhow::Result<(UnixMasterPty, UnixSlavePty)> {
    let mut master: RawFd = -1;
    let mut slave: RawFd = -1;

    let mut size = winsize {
        ws_row: size.rows,
        ws_col: size.cols,
        ws_xpixel: size.pixel_width,
        ws_ypixel: size.pixel_height,
    };

    let result = unsafe {
        // BSDish systems may require mut pointers to some args
        #[allow(clippy::unnecessary_mut_passed)]
        libc::openpty(
            &mut master,
            &mut slave,
            ptr::null_mut(),
            ptr::null_mut(),
            &mut size,
        )
    };

    if result != 0 {
        bail!("failed to openpty: {:?}", io::Error::last_os_error());
    }

    let tty_name = tty_name(slave);

    let master = UnixMasterPty {
        fd: PtyFd(unsafe { FileDescriptor::from_raw_fd(master) }),
        took_writer: RefCell::new(false),
        tty_name,
        input_writer: RefCell::new(None),
    };
    let slave = UnixSlavePty {
        fd: PtyFd(unsafe { FileDescriptor::from_raw_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.as_raw_fd())?;
    cloexec(slave.fd.as_raw_fd())?;

    Ok((master, slave))
}

impl PtySystem for UnixPtySystem {
    fn openpty(&self, size: PtySize) -> anyhow::Result<PtyPair> {
        let (master, slave) = openpty(size)?;
        Ok(PtyPair {
            master: Box::new(master),
            slave: Box::new(slave),
        })
    }
}

struct PtyFd(pub FileDescriptor);
impl std::ops::Deref for PtyFd {
    type Target = FileDescriptor;
    fn deref(&self) -> &FileDescriptor {
        &self.0
    }
}
impl std::ops::DerefMut for PtyFd {
    fn deref_mut(&mut self) -> &mut FileDescriptor {
        &mut self.0
    }
}

impl Read for PtyFd {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
        loop {
            match self.0.read(buf) {
                Err(ref error) if error.kind() == io::ErrorKind::WouldBlock => {
                    wait_pty(&self.0, filedescriptor::POLLIN)?;
                }
                Err(ref e) if e.raw_os_error() == Some(libc::EIO) => {
                    // EIO indicates that the slave pty has been closed.
                    // Treat this as EOF so that std::io::Read::read_to_string
                    // and similar functions gracefully terminate when they
                    // encounter this condition
                    return Ok(0);
                }
                result => return result,
            }
        }
    }
}

fn wait_pty(fd: &FileDescriptor, events: i16) -> io::Result<()> {
    let mut fds = [filedescriptor::pollfd {
        fd: fd.as_raw_fd(),
        events,
        revents: 0,
    }];
    loop {
        match filedescriptor::poll(&mut fds, None) {
            Ok(_) => return Ok(()),
            // Io is what select reports it as, which is what poll is on macOS.
            Err(filedescriptor::Error::Poll(error) | filedescriptor::Error::Io(error))
                if error.kind() == io::ErrorKind::Interrupted =>
            {
                ()
            }
            Err(error) => return Err(io::Error::new(io::ErrorKind::Other, error)),
        }
    }
}

impl Write for PtyFd {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        loop {
            match self.0.write(buf) {
                Err(ref error) if error.kind() == io::ErrorKind::WouldBlock => {
                    wait_pty(&self.0, filedescriptor::POLLOUT)?;
                }
                result => return result,
            }
        }
    }

    fn flush(&mut self) -> io::Result<()> {
        self.0.flush()
    }
}

/// The delegated input handle of a master, which sets O_NONBLOCK on every
/// duplicate of it. Readers and writers of that master have to allow for it.
#[derive(Debug)]
pub struct NonblockingInput {
    fd: std::sync::Mutex<FileDescriptor>,
    closed: std::sync::atomic::AtomicBool,
}

impl NonblockingInput {
    pub fn new(master: &impl AsRawFd) -> Result<Arc<Self>, Error> {
        let mut fd = FileDescriptor::dup(master)?;
        fd.set_non_blocking(true)?;
        Ok(Arc::new(Self {
            fd: std::sync::Mutex::new(fd),
            closed: std::sync::atomic::AtomicBool::new(false),
        }))
    }

    pub fn writer(self: &Arc<Self>) -> Box<dyn crate::TryInputWriter> {
        Box::new(UnixTryInput(Arc::clone(self)))
    }

    /// Writers already handed out stop working, and the master is left as
    /// ordinary code expects it.
    pub fn close(&self) -> Result<(), Error> {
        let mut fd = self
            .fd
            .lock()
            .map_err(|_| anyhow::anyhow!("pty input is unavailable"))?;
        self.closed
            .store(true, std::sync::atomic::Ordering::Release);
        fd.set_non_blocking(false)?;
        Ok(())
    }
}

struct UnixTryInput(Arc<NonblockingInput>);

impl crate::TryInputWriter for UnixTryInput {
    fn try_write(&mut self, bytes: &[u8]) -> io::Result<usize> {
        let mut fd = self
            .0
            .fd
            .try_lock()
            .map_err(|_| io::ErrorKind::WouldBlock)?;
        if self.0.closed.load(std::sync::atomic::Ordering::Acquire) {
            return Err(io::ErrorKind::BrokenPipe.into());
        }
        fd.write(bytes)
    }
}

fn tty_name(fd: RawFd) -> Option<PathBuf> {
    let mut buf = vec![0 as std::ffi::c_char; 128];

    loop {
        let res = unsafe { libc::ttyname_r(fd, buf.as_mut_ptr(), buf.len()) };

        if res == libc::ERANGE {
            if buf.len() > 64 * 1024 {
                // on macOS, if the buf is "too big", ttyname_r can
                // return ERANGE, even though that is supposed to
                // indicate buf is "too small".
                return None;
            }
            buf.resize(buf.len() * 2, 0 as std::ffi::c_char);
            continue;
        }

        return if res == 0 {
            let cstr = unsafe { std::ffi::CStr::from_ptr(buf.as_ptr()) };
            let osstr = OsStr::from_bytes(cstr.to_bytes());
            Some(PathBuf::from(osstr))
        } else {
            None
        };
    }
}

/// On Big Sur, Cocoa leaks various file descriptors to child processes,
/// so we need to make a pass through the open descriptors beyond just the
/// stdio descriptors and close them all out.
/// This is approximately equivalent to the darwin `posix_spawnattr_setflags`
/// option POSIX_SPAWN_CLOEXEC_DEFAULT which is used as a bit of a cheat
/// on macOS.
/// On Linux, gnome/mutter leak shell extension fds to wezterm too, so we
/// also need to make an effort to clean up the mess.
///
/// This function enumerates the open filedescriptors in the current process
/// and then will forcibly call close(2) on each open fd that is numbered
/// 3 or higher, effectively closing all descriptors except for the stdio
/// streams.
///
/// The implementation of this function relies on `/dev/fd` being available
/// to provide the list of open fds.  Any errors in enumerating or closing
/// the fds are silently ignored.
pub fn close_random_fds() {
    // FreeBSD, macOS and presumably other BSDish systems have /dev/fd as
    // a directory listing the current fd numbers for the process.
    //
    // On Linux, /dev/fd is a symlink to /proc/self/fd
    if let Ok(dir) = std::fs::read_dir("/dev/fd") {
        let mut fds = vec![];
        for entry in dir {
            if let Some(num) = entry
                .ok()
                .map(|e| e.file_name())
                .and_then(|s| s.into_string().ok())
                .and_then(|n| n.parse::<libc::c_int>().ok())
            {
                if num > 2 {
                    fds.push(num);
                }
            }
        }
        for fd in fds {
            unsafe {
                libc::close(fd);
            }
        }
    }
}

impl PtyFd {
    fn resize(&self, size: PtySize) -> Result<(), Error> {
        let ws_size = winsize {
            ws_row: size.rows,
            ws_col: size.cols,
            ws_xpixel: size.pixel_width,
            ws_ypixel: size.pixel_height,
        };

        if unsafe {
            libc::ioctl(
                self.0.as_raw_fd(),
                libc::TIOCSWINSZ as _,
                &ws_size as *const _,
            )
        } != 0
        {
            bail!(
                "failed to ioctl(TIOCSWINSZ): {:?}",
                io::Error::last_os_error()
            );
        }

        Ok(())
    }

    fn get_size(&self) -> Result<PtySize, Error> {
        let mut size: winsize = unsafe { mem::zeroed() };
        if unsafe {
            libc::ioctl(
                self.0.as_raw_fd(),
                libc::TIOCGWINSZ as _,
                &mut size as *mut _,
            )
        } != 0
        {
            bail!(
                "failed to ioctl(TIOCGWINSZ): {:?}",
                io::Error::last_os_error()
            );
        }
        Ok(PtySize {
            rows: size.ws_row,
            cols: size.ws_col,
            pixel_width: size.ws_xpixel,
            pixel_height: size.ws_ypixel,
        })
    }

    fn spawn_command(&self, builder: CommandBuilder) -> anyhow::Result<std::process::Child> {
        let configured_umask = builder.umask;

        let mut cmd = builder.as_command()?;
        let controlling_tty = builder.get_controlling_tty();

        unsafe {
            cmd.stdin(self.as_stdio()?)
                .stdout(self.as_stdio()?)
                .stderr(self.as_stdio()?)
                .pre_exec(move || {
                    // Clean up a few things before we exec the program
                    // Clear out any potentially problematic signal
                    // dispositions that we might have inherited
                    for signo in &[
                        libc::SIGCHLD,
                        libc::SIGHUP,
                        libc::SIGINT,
                        libc::SIGQUIT,
                        libc::SIGTERM,
                        libc::SIGALRM,
                    ] {
                        libc::signal(*signo, libc::SIG_DFL);
                    }

                    let empty_set: libc::sigset_t = std::mem::zeroed();
                    libc::sigprocmask(libc::SIG_SETMASK, &empty_set, std::ptr::null_mut());

                    // Establish ourselves as a session leader.
                    if libc::setsid() == -1 {
                        return Err(io::Error::last_os_error());
                    }

                    // Clippy wants us to explicitly cast TIOCSCTTY using
                    // type::from(), but the size and potentially signedness
                    // are system dependent, which is why we're using `as _`.
                    // Suppress this lint for this section of code.
                    #[allow(clippy::cast_lossless)]
                    if controlling_tty {
                        // Set the pty as the controlling terminal.
                        // Failure to do this means that delivery of
                        // SIGWINCH won't happen when we resize the
                        // terminal, among other undesirable effects.
                        if libc::ioctl(0, libc::TIOCSCTTY as _, 0) == -1 {
                            return Err(io::Error::last_os_error());
                        }
                    }

                    close_random_fds();

                    if let Some(mask) = configured_umask {
                        libc::umask(mask);
                    }

                    Ok(())
                })
        };

        let mut child = cmd.spawn()?;

        // Ensure that we close out the slave fds that Child retains;
        // they are not what we need (we need the master side to reference
        // them) and won't work in the usual way anyway.
        // In practice these are None, but it seems best to be move them
        // out in case the behavior of Command changes in the future.
        child.stdin.take();
        child.stdout.take();
        child.stderr.take();

        Ok(child)
    }
}

/// Represents the master end of a pty.
/// The file descriptor will be closed when the Pty is dropped.
struct UnixMasterPty {
    fd: PtyFd,
    took_writer: RefCell<bool>,
    tty_name: Option<PathBuf>,
    input_writer: RefCell<Option<Arc<NonblockingInput>>>,
}

/// A master pty this process did not open: the descriptor came from
/// another process, a mux server handing its panes over. Ownership passes
/// here and the descriptor closes with the pty. The tty name is the
/// sender's to tell: `ttyname` on a master gives nothing useful, and
/// `ptsname` is not thread-safe everywhere.
pub fn master_from_raw_fd(
    fd: std::os::fd::OwnedFd,
    tty_name: Option<PathBuf>,
) -> Result<Box<dyn MasterPty>, Error> {
    let fd = FileDescriptor::new(fd);
    cloexec(fd.as_raw_fd())?;
    Ok(Box::new(UnixMasterPty {
        fd: PtyFd(fd),
        took_writer: RefCell::new(false),
        tty_name,
        input_writer: RefCell::new(None),
    }))
}

/// Represents the slave end of a pty.
/// The file descriptor will be closed when the Pty is dropped.
struct UnixSlavePty {
    fd: PtyFd,
}

/// Helper function to set the close-on-exec flag for a raw descriptor
fn cloexec(fd: RawFd) -> Result<(), Error> {
    let flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
    if flags == -1 {
        bail!(
            "fcntl to read flags failed: {:?}",
            io::Error::last_os_error()
        );
    }
    let result = unsafe { libc::fcntl(fd, libc::F_SETFD, flags | libc::FD_CLOEXEC) };
    if result == -1 {
        bail!(
            "fcntl to set CLOEXEC failed: {:?}",
            io::Error::last_os_error()
        );
    }
    Ok(())
}

impl SlavePty for UnixSlavePty {
    fn spawn_command(
        &self,
        builder: CommandBuilder,
    ) -> Result<Box<dyn Child + Send + Sync>, Error> {
        Ok(Box::new(self.fd.spawn_command(builder)?))
    }
}

impl MasterPty for UnixMasterPty {
    fn resize(&self, size: PtySize) -> Result<(), Error> {
        self.fd.resize(size)
    }

    fn get_size(&self) -> Result<PtySize, Error> {
        self.fd.get_size()
    }

    fn try_clone_reader(&self) -> Result<Box<dyn Read + Send>, Error> {
        let fd = PtyFd(self.fd.try_clone()?);
        Ok(Box::new(fd))
    }

    fn take_writer(&self) -> Result<Box<dyn Write + Send>, Error> {
        if *self.took_writer.borrow() {
            anyhow::bail!("cannot take writer more than once");
        }
        *self.took_writer.borrow_mut() = true;
        let fd = PtyFd(self.fd.try_clone()?);
        Ok(Box::new(UnixMasterWriter { fd }))
    }

    fn try_input_writer(&self) -> Result<Box<dyn crate::TryInputWriter>, Error> {
        let mut cached = self.input_writer.borrow_mut();
        if let Some(input) = cached.as_ref() {
            return Ok(input.writer());
        }
        let input = NonblockingInput::new(&self.fd.0)?;
        // Keep the mode stable for this master's lifetime. Restoring it
        // as a previous lease drops can race with acquiring a new lease.
        let writer = input.writer();
        *cached = Some(input);
        Ok(writer)
    }

    fn quiesce_input(&self) -> Result<(), Error> {
        if let Some(input) = self.input_writer.borrow_mut().take() {
            input.close()?;
        }
        Ok(())
    }

    fn as_raw_fd(&self) -> Option<RawFd> {
        Some(self.fd.0.as_raw_fd())
    }

    fn tty_name(&self) -> Option<PathBuf> {
        self.tty_name.clone()
    }

    fn process_group_leader(&self) -> Option<libc::pid_t> {
        match unsafe { libc::tcgetpgrp(self.fd.0.as_raw_fd()) } {
            pid if pid > 0 => Some(pid),
            _ => None,
        }
    }

    fn get_termios(&self) -> Option<nix::sys::termios::Termios> {
        nix::sys::termios::tcgetattr(self.fd.0.as_fd()).ok()
    }
}

/// Represents the master end of a pty.
/// EOT will be sent, and then the file descriptor will be closed when
/// the Pty is dropped.
struct UnixMasterWriter {
    fd: PtyFd,
}

impl Drop for UnixMasterWriter {
    fn drop(&mut self) {
        let mut t: libc::termios = unsafe { std::mem::MaybeUninit::zeroed().assume_init() };
        if unsafe { libc::tcgetattr(self.fd.0.as_raw_fd(), &mut t) } == 0 {
            // EOF is only interpreted after a newline, so if it is set,
            // we send a newline followed by EOF.
            let eot = t.c_cc[libc::VEOF];
            if eot != 0 {
                let _ = self.fd.0.write_all(&[b'\n', eot]);
            }
        }
    }
}

impl Write for UnixMasterWriter {
    fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
        self.fd.write(buf)
    }
    fn flush(&mut self) -> Result<(), io::Error> {
        self.fd.flush()
    }
}

#[cfg(test)]
mod delegated_input_tests {
    use super::*;
    use std::os::fd::AsFd;

    #[test]
    fn checked_input_is_nonblocking_and_preserves_regular_io() {
        let (master, slave) = openpty(PtySize::default()).unwrap();
        let mut attributes = nix::sys::termios::tcgetattr(slave.fd.as_fd()).unwrap();
        nix::sys::termios::cfmakeraw(&mut attributes);
        nix::sys::termios::tcsetattr(
            slave.fd.as_fd(),
            nix::sys::termios::SetArg::TCSANOW,
            &attributes,
        )
        .unwrap();
        let mut checked = master.try_input_writer().unwrap();
        let mut regular = master.take_writer().unwrap();
        let mut slave_reader = slave.fd.try_clone().unwrap();
        let block = [b'x'; 4096];
        let mut accepted = 0;
        loop {
            match checked.try_write(&block) {
                Ok(0) => panic!("nonempty PTY input made no progress"),
                Ok(count) => accepted += count,
                Err(error) if error.kind() == io::ErrorKind::WouldBlock => break,
                Err(error) => panic!("unexpected PTY error: {}", error),
            }
            assert!(
                accepted < 8 * 1024 * 1024,
                "PTY failed to apply backpressure"
            );
        }
        assert!(accepted > 0);
        // A normal write must still wait and eventually deliver all bytes.
        let drain = std::thread::spawn(move || {
            let mut received = vec![0; accepted + 4];
            slave_reader.read_exact(&mut received).unwrap();
            assert!(received[..accepted].iter().all(|byte| *byte == b'x'));
            assert_eq!(&received[accepted..], b"done");
        });
        regular.write_all(b"done").unwrap();
        drain.join().unwrap();

        let mut reader = master.try_clone_reader().unwrap();
        let read = std::thread::spawn(move || {
            let mut bytes = [0; 5];
            reader.read_exact(&mut bytes).unwrap();
            assert_eq!(&bytes, b"reply");
        });
        slave.fd.try_clone().unwrap().write_all(b"reply").unwrap();
        read.join().unwrap();
        drop(checked);
        let mut next = master.try_input_writer().unwrap();
        assert_eq!(next.try_write(b"next").unwrap(), 4);
    }

    #[test]
    fn handoff_quiesces_old_writers_without_changing_successor_mode_on_drop() {
        let (master, _slave) = openpty(PtySize::default()).unwrap();
        let mut old = master.try_input_writer().unwrap();
        master.quiesce_input().unwrap();
        assert_eq!(
            old.try_write(b"stale").unwrap_err().kind(),
            io::ErrorKind::BrokenPipe
        );
        let flags = || {
            nix::fcntl::OFlag::from_bits_truncate(
                nix::fcntl::fcntl(master.fd.0.as_raw_fd(), nix::fcntl::FcntlArg::F_GETFL).unwrap(),
            )
        };
        assert!(!flags().contains(nix::fcntl::OFlag::O_NONBLOCK));
        let mut current = master.try_input_writer().unwrap();
        assert!(flags().contains(nix::fcntl::OFlag::O_NONBLOCK));
        drop(old);
        assert!(flags().contains(nix::fcntl::OFlag::O_NONBLOCK));
        assert_eq!(current.try_write(b"current").unwrap(), 7);
    }
}