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.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
pty: adopt a master pty and a process this process did not create
RoversXc6bd83fa88d6
diff --git a/pty/src/lib.rs b/pty/src/lib.rsindex 264a86be..fcb11ca0 100644--- a/pty/src/lib.rs+++ b/pty/src/lib.rs@@ -290,6 +290,72 @@ impl Child for std::process::Child {}}+/// A process this process did not spawn: the child of a mux server that+/// handed its pane over. Only a parent can wait on a process, so its exit+/// is noticed by asking the kernel whether the pid still exists, four+/// times a second; the exit status is not knowable that way and reads as+/// success. (A pid answers as long as it is a zombie, so whoever inherits+/// the process -- init, or a subreaper -- has to reap it.)+#[cfg(unix)]+pub fn adopted_child(pid: u32) -> Box<dyn Child + Send + Sync> {+ Box::new(AdoptedChild { pid })+}++#[cfg(unix)]+#[derive(Debug)]+struct AdoptedChild {+ pid: u32,+}++#[cfg(unix)]+impl AdoptedChild {+ fn alive(&self) -> bool {+ if unsafe { libc::kill(self.pid as libc::pid_t, 0) } == 0 {+ return true;+ }+ // EPERM: it exists and is not ours to signal. ESRCH: gone.+ std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)+ }+}++#[cfg(unix)]+impl Child for AdoptedChild {+ fn try_wait(&mut self) -> IoResult<Option<ExitStatus>> {+ Ok(if self.alive() {+ None+ } else {+ Some(ExitStatus::with_exit_code(0))+ })+ }++ fn wait(&mut self) -> IoResult<ExitStatus> {+ while self.alive() {+ std::thread::sleep(std::time::Duration::from_millis(250));+ }+ Ok(ExitStatus::with_exit_code(0))+ }++ fn process_id(&self) -> Option<u32> {+ Some(self.pid)+ }+}++#[cfg(unix)]+impl ChildKiller for AdoptedChild {+ fn kill(&mut self) -> IoResult<()> {+ ProcessSignaller {+ pid: Some(self.pid),+ }+ .kill()+ }++ fn clone_killer(&self) -> Box<dyn ChildKiller + Send + Sync> {+ Box::new(ProcessSignaller {+ pid: Some(self.pid),+ })+ }+}+#[derive(Debug)]struct ProcessSignaller {pid: Option<u32>,@@ -405,3 +471,32 @@ pub fn native_pty_system() -> Box<dyn PtySystem + Send> {pub type NativePtySystem = unix::UnixPtySystem;#[cfg(windows)]pub type NativePtySystem = win::conpty::ConPtySystem;++#[cfg(all(test, unix))]+mod adopted_child_tests {+ use super::*;++ /// An adopted child is seen to exit once whoever is its parent reaps+ /// it; until then it is alive, and a kill reaches it.+ #[test]+ fn an_adopted_child_is_waited_for_by_polling() {+ let mut real = std::process::Command::new("sh")+ .arg("-c")+ .arg("sleep 30")+ .spawn()+ .expect("spawn sh");+ let pid = real.id();+ let mut adopted = adopted_child(pid);+ assert_eq!(adopted.process_id(), Some(pid));+ assert!(adopted.try_wait().unwrap().is_none(), "alive");++ // The test process is the real parent; it reaps on a thread, as+ // init would for a process whose parent went away.+ let reaper = std::thread::spawn(move || real.wait());+ adopted.kill().expect("SIGHUP");+ let status = adopted.wait().expect("wait");+ assert!(status.success(), "the status of an adopted process reads as success");+ assert!(reaper.join().unwrap().is_ok());+ assert!(adopted.try_wait().unwrap().is_some(), "gone");+ }+}diff --git a/pty/src/unix.rs b/pty/src/unix.rsindex cc458ce9..43f0f52b 100644--- a/pty/src/unix.rs+++ b/pty/src/unix.rs@@ -306,6 +306,24 @@ struct UnixMasterPty {tty_name: Option<PathBuf>,}+/// 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,+ }))+}+/// Represents the slave end of a pty./// The file descriptor will be closed when the Pty is dropped.struct UnixSlavePty {diff --git a/wezterm-uds/src/lib.rs b/wezterm-uds/src/lib.rsindex 76abe5f8..01935bdd 100644--- a/wezterm-uds/src/lib.rs+++ b/wezterm-uds/src/lib.rs@@ -118,6 +118,13 @@ impl std::ops::DerefMut for UnixStream {pub struct UnixListener(ListenerImpl);+#[cfg(unix)]+impl FromRawFd for UnixListener {+ unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {+ UnixListener(ListenerImpl::from_raw_fd(fd))+ }+}+impl UnixListener {pub fn bind<P: AsRef<Path>>(path: P) -> std::io::Result<Self> {Ok(Self(ListenerImpl::bind(path)?))