use std::process::Command;
use std::time::Duration;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use std::io::{Read, Write};
#[cfg(any(target_os = "linux", target_os = "macos"))]
use std::os::fd::AsRawFd;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(any(target_os = "linux", target_os = "macos"))]
use std::sync::{Arc, Mutex};
#[cfg(any(target_os = "linux", target_os = "macos"))]
use crate::mxc_common::interruptible_reader::{InterruptibleReader, ReadCanceller};
#[cfg(any(target_os = "linux", target_os = "macos"))]
use crate::mxc_common::sandbox_process::StreamCloser;
#[cfg(any(target_os = "linux", target_os = "macos"))]
pub use nix::sys::signal::Signal;
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Signal {}
#[derive(Clone, Debug)]
pub struct PtyOptions {
pub timeout: Option<Duration>,
pub ready_wait: Duration,
pub unblock_signals: &'static [Signal],
}
impl PtyOptions {
pub const POLL_INTERVAL: Duration = Duration::from_millis(500);
}
impl Default for PtyOptions {
fn default() -> Self {
Self {
timeout: None,
ready_wait: Duration::from_secs(5),
unblock_signals: &[],
}
}
}
#[derive(Debug)]
pub enum PtyOutcome {
Exited(std::process::ExitStatus),
TimedOut,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct PtySize {
pub rows: u16,
pub cols: u16,
pub pixel_width: u16,
pub pixel_height: u16,
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
pub struct LivePty {
primary: std::fs::File,
access: Mutex<PtyAccess>,
size: Mutex<PtySize>,
eof: PtyEof,
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
struct PtyAccess {
writer: Option<std::fs::File>,
reader_claimed: bool,
native_bridge: Option<NativeBridge>,
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
struct NativeBridge {
input_canceller: ReadCanceller,
output_canceller: ReadCanceller,
input_shutdown: BridgeShutdown,
output_shutdown: BridgeShutdown,
input_thread: std::thread::JoinHandle<()>,
output_thread: std::thread::JoinHandle<()>,
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl NativeBridge {
fn shutdown(self) {
self.input_shutdown.cancel();
self.output_shutdown.cancel();
self.input_canceller.close();
self.output_canceller.close();
let _ = self.input_thread.join();
let _ = self.output_thread.join();
}
fn finish(self) {
const OUTPUT_DRAIN_TIMEOUT: Duration = Duration::from_secs(1);
const OUTPUT_DRAIN_POLL: Duration = Duration::from_millis(10);
self.input_shutdown.cancel();
self.input_canceller.close();
let _ = self.input_thread.join();
let deadline = std::time::Instant::now() + OUTPUT_DRAIN_TIMEOUT;
while !self.output_thread.is_finished() && std::time::Instant::now() < deadline {
std::thread::sleep(OUTPUT_DRAIN_POLL);
}
if !self.output_thread.is_finished() {
self.output_canceller.close();
self.output_shutdown.cancel();
}
let _ = self.output_thread.join();
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[derive(Clone)]
struct BridgeShutdown {
state: Arc<BridgeShutdownState>,
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
struct BridgeShutdownState {
cancelled: AtomicBool,
wake_read: std::os::fd::OwnedFd,
wake_write: std::os::fd::OwnedFd,
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl BridgeShutdown {
fn new() -> std::io::Result<Self> {
let (wake_read, wake_write) = create_pipe()?;
set_nonblocking(wake_write.as_raw_fd())?;
Ok(Self {
state: Arc::new(BridgeShutdownState {
cancelled: AtomicBool::new(false),
wake_read,
wake_write,
}),
})
}
fn cancel(&self) {
if !self.state.cancelled.swap(true, Ordering::AcqRel) {
let byte = [1_u8];
let _ = unsafe {
libc::write(
self.state.wake_write.as_raw_fd(),
byte.as_ptr().cast(),
byte.len(),
)
};
}
}
fn is_cancelled(&self) -> bool {
self.state.cancelled.load(Ordering::Acquire)
}
fn wait_writable(&self, fd: std::os::fd::RawFd) -> std::io::Result<bool> {
if self.state.cancelled.load(Ordering::Acquire) {
return Ok(false);
}
loop {
let mut poll_fds = [
libc::pollfd {
fd,
events: libc::POLLOUT,
revents: 0,
},
libc::pollfd {
fd: self.state.wake_read.as_raw_fd(),
events: libc::POLLIN,
revents: 0,
},
];
let result = unsafe { libc::poll(poll_fds.as_mut_ptr(), 2, -1) };
if result >= 0 {
return Ok(
!self.state.cancelled.load(Ordering::Acquire) && poll_fds[1].revents == 0
);
}
let error = std::io::Error::last_os_error();
if error.kind() != std::io::ErrorKind::Interrupted {
return Err(error);
}
}
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[derive(Clone, Copy)]
enum PtyEof {
CurrentCanonical,
Forwarded(PtyDiscipline),
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[derive(Clone, Copy)]
struct PtyDiscipline {
canonical: bool,
extended: bool,
ignore_cr: bool,
cr_to_nl: bool,
nl_to_cr: bool,
strip_high_bit: bool,
eof: u8,
eol: u8,
eol2: u8,
erase: u8,
kill: u8,
word_erase: u8,
literal_next: u8,
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[derive(Debug)]
pub struct NativePtyStdio {
pub stdin: std::os::fd::OwnedFd,
pub stdout: std::os::fd::OwnedFd,
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[derive(Clone)]
pub struct PtyReadCanceller(ReadCanceller);
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl std::fmt::Debug for PtyReadCanceller {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.debug_struct("PtyReadCanceller").finish()
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl StreamCloser for PtyReadCanceller {
fn close(&self) {
self.0.close();
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl std::fmt::Debug for LivePty {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("LivePty")
.field("size", &self.size())
.field(
"reader_claimed",
&self
.access
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.reader_claimed,
)
.finish_non_exhaustive()
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl LivePty {
pub fn attach(
command: &mut Command,
size: PtySize,
unblock_signals: &'static [Signal],
) -> std::io::Result<Self> {
Self::attach_with_eof(command, size, unblock_signals, false)
}
pub fn attach_forwarded(
command: &mut Command,
size: PtySize,
unblock_signals: &'static [Signal],
) -> std::io::Result<Self> {
Self::attach_with_eof(command, size, unblock_signals, true)
}
fn attach_with_eof(
command: &mut Command,
size: PtySize,
unblock_signals: &'static [Signal],
forward_eof: bool,
) -> std::io::Result<Self> {
use std::os::fd::AsRawFd;
use std::process::Stdio;
use nix::pty::Winsize;
let winsize = (size.rows != 0 && size.cols != 0).then_some(Winsize {
ws_row: size.rows,
ws_col: size.cols,
ws_xpixel: size.pixel_width,
ws_ypixel: size.pixel_height,
});
let (master, slave) = open_pty(winsize.as_ref())?;
let eof = if forward_eof {
PtyEof::Forwarded(terminal_discipline(&slave)?)
} else {
PtyEof::CurrentCanonical
};
set_nonblocking(master.as_raw_fd())?;
let secondary_in: Stdio = slave.try_clone()?.into();
let secondary_out: Stdio = slave.try_clone()?.into();
let secondary_err: Stdio = slave.into();
command
.stdin(secondary_in)
.stdout(secondary_out)
.stderr(secondary_err);
unsafe {
use std::os::unix::process::CommandExt;
command.pre_exec(move || {
nix::unistd::setsid().map_err(std::io::Error::from)?;
let _ = libc::ioctl(0, libc::TIOCSCTTY as _, 0);
let mut mask = nix::sys::signal::SigSet::empty();
mask.add(nix::sys::signal::Signal::SIGWINCH);
for signal in unblock_signals {
mask.add(*signal);
}
mask.thread_unblock().map_err(std::io::Error::from)
});
}
let primary: std::fs::File = master.into();
let writer = primary.try_clone()?;
Ok(Self {
primary,
access: Mutex::new(PtyAccess {
writer: Some(writer),
reader_claimed: false,
native_bridge: None,
}),
size: Mutex::new(size),
eof,
})
}
pub fn try_clone_reader(&self) -> std::io::Result<Box<dyn Read + Send>> {
self.try_clone_reader_with_canceller()
.map(|(reader, _)| reader)
}
pub fn try_clone_reader_with_canceller(
&self,
) -> std::io::Result<(Box<dyn Read + Send>, PtyReadCanceller)> {
let (reader, canceller) = self.new_reader()?;
let mut access = self
.access
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if access.native_bridge.is_some() {
return Err(std::io::Error::new(
std::io::ErrorKind::AlreadyExists,
"PTY native streams have already been transferred",
));
}
access.reader_claimed = true;
Ok((Box::new(reader), canceller))
}
pub fn take_writer(&self) -> std::io::Result<Box<dyn Write + Send>> {
self.access
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.writer
.take()
.map(|writer| Box::new(PtyWriter::new(writer, self.eof)) as Box<dyn Write + Send>)
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::BrokenPipe,
"PTY input writer has already been taken",
)
})
}
pub fn close_writer(&self) {
let writer = self
.access
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.writer
.take();
if let Some(writer) = writer {
drop(PtyWriter::new(writer, self.eof));
}
}
pub fn take_native_stdio(&self) -> std::io::Result<NativePtyStdio> {
use std::fs::File;
let input_primary = self.primary.try_clone()?;
let (input_read_fd, input_write_fd) = create_pipe()?;
let input_reader = InterruptibleReader::new(input_read_fd)?;
let input_canceller = input_reader.canceller();
let input_shutdown = BridgeShutdown::new()?;
let output_primary = self.primary.try_clone()?;
let (output_reader, output_canceller) = Self::reader_from_file(output_primary)?;
let (output_read_fd, output_write_fd) = create_pipe()?;
set_nonblocking(output_write_fd.as_raw_fd())?;
let output_shutdown = BridgeShutdown::new()?;
let mut access = self
.access
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if access.native_bridge.is_some() || access.writer.is_none() || access.reader_claimed {
return Err(std::io::Error::new(
std::io::ErrorKind::AlreadyExists,
"PTY streams have already been accessed",
));
}
let (output_setup_sender, output_setup_receiver) = std::sync::mpsc::sync_channel(1);
let output_thread = std::thread::Builder::new()
.name("mxc-pty-output-bridge".to_string())
.spawn({
let output_shutdown = output_shutdown.clone();
move || {
match block_sigpipe() {
Ok(()) => {
let _ = output_setup_sender.send(Ok(()));
}
Err(error) => {
let _ = output_setup_sender.send(Err(error));
return;
}
}
let mut reader = output_reader;
let mut writer = Some(File::from(output_write_fd));
let mut buffer = [0_u8; 8192];
loop {
let count = match reader.read(&mut buffer) {
Ok(0) | Err(_) => return,
Ok(count) => count,
};
let Some(sink) = writer.as_mut() else {
continue;
};
if write_all_until_shutdown(sink, &buffer[..count], &output_shutdown)
.is_err()
{
if output_shutdown.is_cancelled() {
return;
}
writer = None;
}
}
}
})?;
match output_setup_receiver.recv() {
Ok(Ok(())) => {}
Ok(Err(error)) => {
output_canceller.close();
output_shutdown.cancel();
let _ = output_thread.join();
return Err(error);
}
Err(_) => {
output_canceller.close();
output_shutdown.cancel();
let _ = output_thread.join();
return Err(std::io::Error::other(
"PTY output bridge exited before configuring SIGPIPE",
));
}
}
let input_thread = match std::thread::Builder::new()
.name("mxc-pty-input-bridge".to_string())
.spawn({
let input_shutdown = input_shutdown.clone();
let eof = self.eof;
move || {
let mut reader = input_reader;
let mut writer = PtyWriter::new(input_primary, eof);
let mut buffer = [0_u8; 8192];
loop {
let count = match reader.read(&mut buffer) {
Ok(0) => {
if input_shutdown.is_cancelled() {
writer.abandon_eof();
} else {
let _ = writer.send_eof_until_shutdown(&input_shutdown);
}
return;
}
Err(_) => {
writer.abandon_eof();
return;
}
Ok(count) => count,
};
if writer
.write_all_until_shutdown(&buffer[..count], &input_shutdown)
.is_err()
{
writer.abandon_eof();
return;
}
}
}
}) {
Ok(thread) => thread,
Err(error) => {
output_canceller.close();
output_shutdown.cancel();
let _ = output_thread.join();
return Err(error);
}
};
access.writer.take();
access.reader_claimed = true;
access.native_bridge = Some(NativeBridge {
input_canceller,
output_canceller: output_canceller.0.clone(),
input_shutdown,
output_shutdown,
input_thread,
output_thread,
});
Ok(NativePtyStdio {
stdin: input_write_fd,
stdout: output_read_fd,
})
}
pub fn finish_native_bridge(&self) {
let bridge = self
.access
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.native_bridge
.take();
if let Some(bridge) = bridge {
bridge.finish();
}
}
pub fn resize(&self, size: PtySize) -> std::io::Result<()> {
use std::os::fd::AsRawFd;
if size.rows == 0 || size.cols == 0 {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"PTY rows and columns must be non-zero",
));
}
let winsize = libc::winsize {
ws_row: size.rows,
ws_col: size.cols,
ws_xpixel: size.pixel_width,
ws_ypixel: size.pixel_height,
};
if unsafe { libc::ioctl(self.primary.as_raw_fd(), libc::TIOCSWINSZ, &winsize) } != 0 {
return Err(std::io::Error::last_os_error());
}
*self
.size
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner) = size;
Ok(())
}
pub fn size(&self) -> PtySize {
*self
.size
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
pub fn is_canonical_mode(&self) -> std::io::Result<bool> {
use nix::sys::termios::{self, LocalFlags};
termios::tcgetattr(&self.primary)
.map(|settings| settings.local_flags.contains(LocalFlags::ICANON))
.map_err(std::io::Error::from)
}
pub fn take_unclaimed_reader(
&self,
) -> std::io::Result<Option<(Box<dyn Read + Send>, PtyReadCanceller)>> {
let mut access = self
.access
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if access.reader_claimed {
return Ok(None);
}
let (reader, canceller) = self.new_reader()?;
access.reader_claimed = true;
Ok(Some((Box::new(reader), canceller)))
}
fn new_reader(&self) -> std::io::Result<(PtyReader, PtyReadCanceller)> {
Self::reader_from_file(self.primary.try_clone()?)
}
fn reader_from_file(reader: std::fs::File) -> std::io::Result<(PtyReader, PtyReadCanceller)> {
let reader = InterruptibleReader::new(reader.into())?;
let canceller = PtyReadCanceller(reader.canceller());
Ok((PtyReader(reader), canceller))
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl Drop for LivePty {
fn drop(&mut self) {
let bridge = self
.access
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.native_bridge
.take();
if let Some(bridge) = bridge {
bridge.shutdown();
}
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
struct PtyReader(InterruptibleReader);
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl Read for PtyReader {
fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> {
match self.0.read(buffer) {
Err(error) if error.raw_os_error() == Some(libc::EIO) => Ok(0),
result => result,
}
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
struct PtyWriter {
writer: Option<std::fs::File>,
eof: PtyEof,
eof_sent: bool,
canonical_line: Vec<u8>,
canonical_limit: usize,
literal_next: bool,
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl PtyWriter {
fn new(writer: std::fs::File, eof: PtyEof) -> Self {
let canonical_limit = canonical_line_limit(&writer);
Self {
writer: Some(writer),
eof,
eof_sent: false,
canonical_line: Vec::new(),
canonical_limit,
literal_next: false,
}
}
fn write_all_until_shutdown(
&mut self,
buffer: &[u8],
shutdown: &BridgeShutdown,
) -> std::io::Result<()> {
let writer = self
.writer
.as_mut()
.ok_or_else(|| std::io::Error::from(std::io::ErrorKind::BrokenPipe))?;
write_all_until_shutdown(writer, buffer, shutdown)?;
self.track_canonical_input(buffer);
Ok(())
}
fn send_eof_until_shutdown(&mut self, shutdown: &BridgeShutdown) -> std::io::Result<()> {
let Some((eof, count)) = self.take_eof() else {
return Ok(());
};
let writer = self
.writer
.as_mut()
.ok_or_else(|| std::io::Error::from(std::io::ErrorKind::BrokenPipe))?;
write_all_until_shutdown(writer, &[eof, eof, eof][..count], shutdown)?;
writer.flush()
}
fn send_eof(&mut self) -> std::io::Result<()> {
const EOF_WRITE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(1);
let Some((eof, count)) = self.take_eof() else {
return Ok(());
};
let writer = self
.writer
.as_mut()
.ok_or_else(|| std::io::Error::from(std::io::ErrorKind::BrokenPipe))?;
write_all_until_deadline(writer, &[eof, eof, eof][..count], EOF_WRITE_TIMEOUT)?;
writer.flush()
}
fn take_eof(&mut self) -> Option<(u8, usize)> {
if self.eof_sent {
return None;
}
self.eof_sent = true;
let eof = match self.eof {
PtyEof::CurrentCanonical => self.writer.as_ref().and_then(terminal_eof_byte),
PtyEof::Forwarded(discipline) => {
control_enabled(discipline.eof).then_some(discipline.eof)
}
}?;
let count = if self.literal_next {
3
} else if self.canonical_line.is_empty() {
1
} else {
2
};
Some((eof, count))
}
fn abandon_eof(&mut self) {
self.eof_sent = true;
}
fn track_canonical_input(&mut self, buffer: &[u8]) {
let discipline = match self.eof {
PtyEof::CurrentCanonical => self
.writer
.as_ref()
.and_then(|writer| terminal_discipline(writer).ok()),
PtyEof::Forwarded(discipline) => Some(discipline),
};
let Some(discipline) = discipline.filter(|discipline| discipline.canonical) else {
return;
};
for raw_byte in buffer {
let mut byte = *raw_byte;
if discipline.strip_high_bit {
byte &= 0x7f;
}
if byte == b'\r' {
if discipline.ignore_cr {
continue;
}
if discipline.cr_to_nl {
byte = b'\n';
}
} else if byte == b'\n' && discipline.nl_to_cr {
byte = b'\r';
}
if self.literal_next {
self.literal_next = false;
if self.canonical_line.len() < self.canonical_limit {
self.canonical_line.push(byte);
}
continue;
}
if discipline.extended && control_matches(byte, discipline.literal_next) {
self.literal_next = true;
continue;
}
if control_matches(byte, discipline.kill) {
self.canonical_line.clear();
continue;
}
if control_matches(byte, discipline.erase) {
self.canonical_line.pop();
continue;
}
if discipline.extended && control_matches(byte, discipline.word_erase) {
while self
.canonical_line
.last()
.is_some_and(|byte| byte.is_ascii_whitespace())
{
self.canonical_line.pop();
}
while self
.canonical_line
.last()
.is_some_and(|byte| !byte.is_ascii_whitespace())
{
self.canonical_line.pop();
}
continue;
}
if byte == b'\n'
|| control_matches(byte, discipline.eol)
|| control_matches(byte, discipline.eol2)
{
self.canonical_line.clear();
continue;
}
if !control_matches(byte, discipline.eof)
&& self.canonical_line.len() < self.canonical_limit
{
self.canonical_line.push(byte);
}
}
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn canonical_line_limit(writer: &std::fs::File) -> usize {
#[cfg(target_os = "linux")]
{
let _ = writer;
4095
}
#[cfg(target_os = "macos")]
{
let limit = unsafe { libc::fpathconf(writer.as_raw_fd(), libc::_PC_MAX_CANON) };
usize::try_from(limit)
.ok()
.filter(|limit| *limit > 0)
.unwrap_or(4096)
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl Write for PtyWriter {
fn write(&mut self, buffer: &[u8]) -> std::io::Result<usize> {
let writer = self
.writer
.as_mut()
.ok_or_else(|| std::io::Error::from(std::io::ErrorKind::BrokenPipe))?;
loop {
match writer.write(buffer) {
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
let mut poll_fd = libc::pollfd {
fd: writer.as_raw_fd(),
events: libc::POLLOUT,
revents: 0,
};
let result = unsafe { libc::poll(&mut poll_fd, 1, -1) };
if result < 0 {
let error = std::io::Error::last_os_error();
if error.kind() != std::io::ErrorKind::Interrupted {
return Err(error);
}
}
}
Ok(count) => {
self.track_canonical_input(&buffer[..count]);
return Ok(count);
}
Err(error) => return Err(error),
}
}
}
fn flush(&mut self) -> std::io::Result<()> {
self.writer
.as_mut()
.ok_or_else(|| std::io::Error::from(std::io::ErrorKind::BrokenPipe))?
.flush()
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl Drop for PtyWriter {
fn drop(&mut self) {
let _ = self.send_eof();
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn write_all_until_shutdown(
writer: &mut std::fs::File,
mut buffer: &[u8],
shutdown: &BridgeShutdown,
) -> std::io::Result<()> {
while !buffer.is_empty() {
if shutdown.is_cancelled() {
return Err(std::io::ErrorKind::Interrupted.into());
}
match writer.write(buffer) {
Ok(0) => return Err(std::io::ErrorKind::WriteZero.into()),
Ok(count) => buffer = &buffer[count..],
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
if !shutdown.wait_writable(writer.as_raw_fd())? {
return Err(std::io::ErrorKind::Interrupted.into());
}
}
Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {}
Err(error) => return Err(error),
}
}
Ok(())
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn write_all_until_deadline(
writer: &mut std::fs::File,
mut buffer: &[u8],
timeout: std::time::Duration,
) -> std::io::Result<()> {
let deadline = std::time::Instant::now() + timeout;
while !buffer.is_empty() {
match writer.write(buffer) {
Ok(0) => return Err(std::io::ErrorKind::WriteZero.into()),
Ok(count) => buffer = &buffer[count..],
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
let now = std::time::Instant::now();
if now >= deadline {
return Err(std::io::ErrorKind::TimedOut.into());
}
let timeout_ms = (deadline - now)
.as_millis()
.clamp(1, libc::c_int::MAX as u128)
as libc::c_int;
let mut poll_fd = libc::pollfd {
fd: writer.as_raw_fd(),
events: libc::POLLOUT,
revents: 0,
};
let result = unsafe { libc::poll(&mut poll_fd, 1, timeout_ms) };
if result == 0 {
return Err(std::io::ErrorKind::TimedOut.into());
}
if result < 0 {
let error = std::io::Error::last_os_error();
if error.kind() != std::io::ErrorKind::Interrupted {
return Err(error);
}
}
}
Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {}
Err(error) => return Err(error),
}
}
Ok(())
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn terminal_eof_byte(file: &std::fs::File) -> Option<u8> {
terminal_discipline(file)
.ok()
.filter(|discipline| discipline.canonical)
.map(|discipline| discipline.eof)
.filter(|eof| control_enabled(*eof))
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn terminal_discipline(fd: &impl std::os::fd::AsFd) -> std::io::Result<PtyDiscipline> {
use nix::sys::termios::{self, InputFlags, LocalFlags, SpecialCharacterIndices};
let settings = termios::tcgetattr(fd).map_err(std::io::Error::from)?;
let control = |index| settings.control_chars[index as usize];
Ok(PtyDiscipline {
canonical: settings.local_flags.contains(LocalFlags::ICANON),
extended: settings.local_flags.contains(LocalFlags::IEXTEN),
ignore_cr: settings.input_flags.contains(InputFlags::IGNCR),
cr_to_nl: settings.input_flags.contains(InputFlags::ICRNL),
nl_to_cr: settings.input_flags.contains(InputFlags::INLCR),
strip_high_bit: settings.input_flags.contains(InputFlags::ISTRIP),
eof: control(SpecialCharacterIndices::VEOF),
eol: control(SpecialCharacterIndices::VEOL),
eol2: control(SpecialCharacterIndices::VEOL2),
erase: control(SpecialCharacterIndices::VERASE),
kill: control(SpecialCharacterIndices::VKILL),
word_erase: control(SpecialCharacterIndices::VWERASE),
literal_next: control(SpecialCharacterIndices::VLNEXT),
})
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn control_matches(byte: u8, control: u8) -> bool {
control_enabled(control) && byte == control
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn control_enabled(control: u8) -> bool {
control != libc::_POSIX_VDISABLE
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn open_pty(
winsize: Option<&nix::pty::Winsize>,
) -> std::io::Result<(std::os::fd::OwnedFd, std::os::fd::OwnedFd)> {
use std::os::fd::{FromRawFd, IntoRawFd};
use std::path::Path;
use nix::fcntl::{open, OFlag};
use nix::pty::{grantpt, posix_openpt, unlockpt};
use nix::sys::stat::Mode;
let flags = OFlag::O_RDWR | OFlag::O_NOCTTY | OFlag::O_CLOEXEC;
let master = posix_openpt(flags).map_err(std::io::Error::from)?;
grantpt(&master).map_err(std::io::Error::from)?;
unlockpt(&master).map_err(std::io::Error::from)?;
#[cfg(target_os = "linux")]
let slave_name = nix::pty::ptsname_r(&master).map_err(std::io::Error::from)?;
#[cfg(target_os = "macos")]
let slave_name = {
static PTSNAME_LOCK: Mutex<()> = Mutex::new(());
let _guard = PTSNAME_LOCK
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
unsafe { nix::pty::ptsname(&master) }.map_err(std::io::Error::from)?
};
let slave_fd =
open(Path::new(&slave_name), flags, Mode::empty()).map_err(std::io::Error::from)?;
let slave = unsafe { std::os::fd::OwnedFd::from_raw_fd(slave_fd) };
if let Some(winsize) = winsize {
if unsafe { libc::ioctl(slave.as_raw_fd(), libc::TIOCSWINSZ as _, winsize) } != 0 {
return Err(std::io::Error::last_os_error());
}
}
let master = unsafe { std::os::fd::OwnedFd::from_raw_fd(master.into_raw_fd()) };
Ok((master, slave))
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn create_pipe() -> std::io::Result<(std::os::fd::OwnedFd, std::os::fd::OwnedFd)> {
#[cfg(target_os = "linux")]
{
nix::unistd::pipe2(nix::fcntl::OFlag::O_CLOEXEC).map_err(std::io::Error::from)
}
#[cfg(target_os = "macos")]
{
let (read, write) = nix::unistd::pipe().map_err(std::io::Error::from)?;
set_cloexec(read.as_raw_fd())?;
set_cloexec(write.as_raw_fd())?;
Ok((read, write))
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn block_sigpipe() -> std::io::Result<()> {
let mut signals = nix::sys::signal::SigSet::empty();
signals.add(nix::sys::signal::Signal::SIGPIPE);
signals.thread_block().map_err(std::io::Error::from)
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn set_nonblocking(fd: std::os::fd::RawFd) -> std::io::Result<()> {
let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
if flags < 0 {
return Err(std::io::Error::last_os_error());
}
if unsafe { libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) } < 0 {
return Err(std::io::Error::last_os_error());
}
Ok(())
}
#[cfg(target_os = "macos")]
fn set_cloexec(fd: std::os::fd::RawFd) -> std::io::Result<()> {
let flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
if flags < 0 {
return Err(std::io::Error::last_os_error());
}
if unsafe { libc::fcntl(fd, libc::F_SETFD, flags | libc::FD_CLOEXEC) } < 0 {
return Err(std::io::Error::last_os_error());
}
Ok(())
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
pub fn run_with_pty(mut command: Command, options: PtyOptions) -> Result<PtyOutcome, String> {
use std::os::unix::io::AsRawFd;
use std::sync::mpsc;
use std::thread;
use std::time::Instant;
let _outer_raw_guard = RawSecondaryGuard::install(std::io::stdin().as_raw_fd());
let outer_winsize = unsafe {
let mut ws: libc::winsize = std::mem::zeroed();
if libc::ioctl(0, libc::TIOCGWINSZ, &mut ws) == 0 && ws.ws_col > 0 && ws.ws_row > 0 {
Some(ws)
} else {
None
}
};
let size = outer_winsize.map_or_else(PtySize::default, |winsize| PtySize {
rows: winsize.ws_row,
cols: winsize.ws_col,
pixel_width: winsize.ws_xpixel,
pixel_height: winsize.ws_ypixel,
});
let terminal = LivePty::attach(&mut command, size, options.unblock_signals)
.map_err(|error| format!("failed to allocate PTY: {error}"))?;
let mut child = command
.spawn()
.map_err(|e| format!("failed to spawn child: {}", e))?;
drop(command);
let mut primary_writer = terminal
.take_writer()
.map_err(|error| format!("take PTY writer: {error}"))?;
let mut primary_reader = terminal
.take_unclaimed_reader()
.map_err(|error| format!("clone PTY reader: {error}"))?
.map(|(reader, _)| reader)
.ok_or_else(|| "PTY reader was already claimed".to_string())?;
let winch_primary = terminal
.primary
.try_clone()
.map_err(|e| format!("dup primary for sigwinch forwarder: {}", e))?;
let _winch_thread = spawn_sigwinch_forwarder(winch_primary);
let (ready_tx, ready_rx) = mpsc::channel::<()>();
let output_thread = thread::spawn(move || {
let mut buf = [0u8; 4096];
let mut signaled = false;
let mut stdout = std::io::stdout();
loop {
match primary_reader.read(&mut buf) {
Ok(0) => break,
Ok(n) => {
if !signaled {
let _ = ready_tx.send(());
signaled = true;
}
let _ = stdout.write_all(&buf[..n]);
let _ = stdout.flush();
}
Err(_) => break,
}
}
});
let deadline = options.timeout.map(|t| Instant::now() + t);
let ready_budget = match deadline {
Some(d) => options
.ready_wait
.min(d.saturating_duration_since(Instant::now())),
None => options.ready_wait,
};
if !ready_budget.is_zero() {
let _ = ready_rx.recv_timeout(ready_budget);
}
thread::spawn(move || {
let mut buf = [0u8; 4096];
let mut stdin = std::io::stdin();
loop {
match stdin.read(&mut buf) {
Ok(0) => break,
Ok(n) => {
if primary_writer.write_all(&buf[..n]).is_err() {
break;
}
}
Err(_) => break,
}
}
});
let outcome = match deadline {
None => {
let status = child.wait().map_err(|e| format!("wait: {}", e))?;
PtyOutcome::Exited(status)
}
Some(deadline) => loop {
match child.try_wait() {
Ok(Some(status)) => break PtyOutcome::Exited(status),
Ok(None) => {
if Instant::now() >= deadline {
let _ = child.kill();
let _ = child.wait();
break PtyOutcome::TimedOut;
}
thread::sleep(PtyOptions::POLL_INTERVAL);
}
Err(e) => {
let _ = child.kill();
let _ = child.wait();
return Err(format!("try_wait: {}", e));
}
}
},
};
let _ = output_thread.join();
Ok(outcome)
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn spawn_sigwinch_forwarder(primary: std::fs::File) -> Option<std::thread::JoinHandle<()>> {
use std::os::unix::io::AsRawFd;
let (read_end, write_end) = nix::unistd::pipe().ok()?;
let read_fd = read_end.as_raw_fd();
let write_fd = write_end.as_raw_fd();
let primary_fd = primary.as_raw_fd();
std::mem::forget(read_end);
std::mem::forget(write_end);
std::mem::forget(primary);
unsafe {
let flags = libc::fcntl(write_fd, libc::F_GETFL);
if flags >= 0 {
let _ = libc::fcntl(write_fd, libc::F_SETFL, flags | libc::O_NONBLOCK);
}
}
SIGWINCH_PIPE_WRITE_FD.store(write_fd, std::sync::atomic::Ordering::Release);
unsafe {
let mut sa: libc::sigaction = std::mem::zeroed();
sa.sa_sigaction = sigwinch_handler as *const () as usize;
libc::sigemptyset(&mut sa.sa_mask);
sa.sa_flags = libc::SA_RESTART;
if libc::sigaction(libc::SIGWINCH, &sa, std::ptr::null_mut()) != 0 {
return None;
}
}
Some(std::thread::spawn(move || {
let mut buf = [0u8; 64];
loop {
let n = unsafe { libc::read(read_fd, buf.as_mut_ptr() as *mut _, buf.len()) };
if n <= 0 {
return;
}
unsafe {
let mut ws: libc::winsize = std::mem::zeroed();
if libc::ioctl(0, libc::TIOCGWINSZ, &mut ws) != 0 {
continue;
}
if libc::ioctl(primary_fd, libc::TIOCSWINSZ, &ws) != 0 {
return;
}
}
}
}))
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
static SIGWINCH_PIPE_WRITE_FD: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(-1);
#[cfg(any(target_os = "linux", target_os = "macos"))]
extern "C" fn sigwinch_handler(_sig: libc::c_int) {
let fd = SIGWINCH_PIPE_WRITE_FD.load(std::sync::atomic::Ordering::Acquire);
if fd < 0 {
return;
}
let byte: u8 = 1;
unsafe {
let _ = libc::write(fd, &byte as *const _ as *const _, 1);
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
struct RawSecondaryGuard {
fd: std::os::unix::io::RawFd,
original: nix::sys::termios::Termios,
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl RawSecondaryGuard {
fn install(fd: std::os::unix::io::RawFd) -> Option<Self> {
use nix::sys::termios::{cfmakeraw, tcgetattr, tcsetattr, SetArg};
if unsafe { libc::isatty(fd) } == 0 {
return None;
}
let borrowed = unsafe { std::os::fd::BorrowedFd::borrow_raw(fd) };
let original = tcgetattr(borrowed).ok()?;
let mut raw = original.clone();
cfmakeraw(&mut raw);
if tcsetattr(borrowed, SetArg::TCSANOW, &raw).is_err() {
return None;
}
Some(Self { fd, original })
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl Drop for RawSecondaryGuard {
fn drop(&mut self) {
use nix::sys::termios::{tcsetattr, SetArg};
let borrowed = unsafe { std::os::fd::BorrowedFd::borrow_raw(self.fd) };
let _ = tcsetattr(borrowed, SetArg::TCSANOW, &self.original);
}
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
pub fn run_with_pty(_command: Command, _options: PtyOptions) -> Result<PtyOutcome, String> {
Err("crate::mxc_pty::run_with_pty is only supported on Linux and macOS".to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(any(target_os = "linux", target_os = "macos"))]
static RUN_WITH_PTY_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
#[cfg(any(target_os = "linux", target_os = "macos"))]
struct PtyDrain {
collected: Arc<Mutex<Vec<u8>>>,
worker: Option<std::thread::JoinHandle<()>>,
finished: std::sync::mpsc::Receiver<()>,
canceller: Option<PtyReadCanceller>,
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl PtyDrain {
const TIMEOUT: Duration = Duration::from_secs(20);
const DROP_GRACE: Duration = Duration::from_secs(2);
fn start(source: Box<dyn Read + Send>) -> Self {
Self::start_inner(source, None)
}
fn start_cancellable(source: (Box<dyn Read + Send>, PtyReadCanceller)) -> Self {
Self::start_inner(source.0, Some(source.1))
}
fn start_inner(
mut source: Box<dyn Read + Send>,
canceller: Option<PtyReadCanceller>,
) -> Self {
let collected = Arc::new(Mutex::new(Vec::new()));
let sink = Arc::clone(&collected);
let (done, finished) = std::sync::mpsc::sync_channel(1);
let worker = std::thread::spawn(move || {
let mut buffer = [0u8; 8192];
while let Ok(count) = source.read(&mut buffer) {
if count == 0 {
break;
}
sink.lock()
.unwrap_or_else(|e| e.into_inner())
.extend_from_slice(&buffer[..count]);
}
let _ = done.send(());
});
Self {
collected,
worker: Some(worker),
finished,
canceller,
}
}
fn snapshot(&self) -> String {
let bytes = self.collected.lock().unwrap_or_else(|e| e.into_inner());
String::from_utf8_lossy(&bytes).into_owned()
}
fn wait_for(&self, marker: &str) -> String {
let deadline = std::time::Instant::now() + Self::TIMEOUT;
loop {
let seen = self.snapshot();
if seen.contains(marker) {
return seen;
}
assert!(
std::time::Instant::now() < deadline,
"terminal output never contained {marker:?}; got: {seen:?}"
);
std::thread::sleep(Duration::from_millis(10));
}
}
fn cancel(&self) {
if let Some(canceller) = &self.canceller {
canceller.close();
}
}
fn finish(mut self) -> String {
self.finished
.recv_timeout(Self::TIMEOUT)
.unwrap_or_else(|_| panic!("terminal drain did not reach end of input"));
if let Some(worker) = self.worker.take() {
worker.join().expect("terminal drain thread");
}
self.snapshot()
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
impl Drop for PtyDrain {
fn drop(&mut self) {
let Some(worker) = self.worker.take() else {
return;
};
if let Some(canceller) = &self.canceller {
canceller.close();
let _ = worker.join();
return;
}
if self.finished.recv_timeout(Self::DROP_GRACE).is_ok() {
let _ = worker.join();
}
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn wait_for_exit(child: &mut std::process::Child) -> std::process::ExitStatus {
let deadline = std::time::Instant::now() + Duration::from_secs(20);
loop {
if let Some(status) = child.try_wait().expect("poll child") {
return status;
}
assert!(
std::time::Instant::now() < deadline,
"child did not exit within the test deadline"
);
std::thread::sleep(Duration::from_millis(10));
}
}
#[test]
fn default_options() {
let opts = PtyOptions::default();
assert!(opts.timeout.is_none());
assert_eq!(opts.ready_wait, Duration::from_secs(5));
assert!(opts.unblock_signals.is_empty());
}
#[test]
fn poll_interval_is_500ms() {
assert_eq!(PtyOptions::POLL_INTERVAL, Duration::from_millis(500));
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn live_pty_supports_input_output_and_resize() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command
.arg("-c")
.arg("stty size; read value; printf 'reply:%s\\n' \"$value\"");
let terminal = LivePty::attach(
&mut command,
PtySize {
rows: 24,
cols: 80,
pixel_width: 0,
pixel_height: 0,
},
&[],
)
.expect("attach PTY");
terminal
.resize(PtySize {
rows: 40,
cols: 120,
pixel_width: 0,
pixel_height: 0,
})
.expect("resize PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let drain = PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
);
let mut writer = terminal.take_writer().expect("take writer");
writer.write_all(b"hello\r").expect("write input");
drop(writer);
assert!(wait_for_exit(&mut child).success());
drop(terminal);
let output = drain.finish();
assert!(output.contains("40 120"), "got: {output:?}");
assert!(output.contains("reply:hello"), "got: {output:?}");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn dropping_writer_sends_canonical_terminal_eof() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command
.arg("-c")
.arg("cat >/dev/null; printf 'terminal-eof-observed\\n'");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let drain = PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
);
let mut writer = terminal.take_writer().expect("take writer");
writer
.write_all(b"partial-line")
.expect("write partial line");
drop(writer);
assert!(wait_for_exit(&mut child).success());
drop(terminal);
let output = drain.finish();
assert!(output.contains("terminal-eof-observed"), "got: {output:?}");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn close_writer_sends_canonical_terminal_eof() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command
.arg("-c")
.arg("cat >/dev/null; printf 'closed-writer-eof\\n'");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let drain = PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
);
terminal.close_writer();
terminal.close_writer();
assert_eq!(
terminal
.take_writer()
.err()
.expect("writer was already closed")
.kind(),
std::io::ErrorKind::BrokenPipe
);
assert!(wait_for_exit(&mut child).success());
drop(terminal);
let output = drain.finish();
assert!(output.contains("closed-writer-eof"), "got: {output:?}");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn close_writer_after_take_writer_leaves_the_taken_writer_usable() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command
.arg("-c")
.arg("read value; printf 'reply:%s\\n' \"$value\"");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let drain = PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
);
let mut writer = terminal.take_writer().expect("take writer");
terminal.close_writer();
writer.write_all(b"still-writable\r").expect("write input");
drop(writer);
assert!(wait_for_exit(&mut child).success());
drop(terminal);
let output = drain.finish();
assert!(output.contains("reply:still-writable"), "got: {output:?}");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn take_unclaimed_reader_drains_output_so_waiting_cannot_deadlock() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command
.arg("-c")
.arg("head -c 200000 /dev/zero; printf 'drained-to-exit\\n'");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
terminal.close_writer();
let drain = PtyDrain::start_cancellable(
terminal
.take_unclaimed_reader()
.expect("take unclaimed reader")
.expect("reader was unclaimed"),
);
assert!(
terminal
.take_unclaimed_reader()
.expect("second take")
.is_none(),
"the reader is claimed after the first take"
);
assert!(wait_for_exit(&mut child).success());
let seen = drain.wait_for("drained-to-exit");
assert!(seen.contains("drained-to-exit"), "got: {seen:?}");
drain.cancel();
drop(terminal);
let _ = drain.finish();
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn take_unclaimed_reader_yields_nothing_once_a_reader_is_claimed() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg("printf claimed");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let drain = PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
);
assert!(
terminal
.take_unclaimed_reader()
.expect("take unclaimed reader")
.is_none(),
"a cloned reader belongs to the caller"
);
assert!(wait_for_exit(&mut child).success());
drop(terminal);
let output = drain.finish();
assert!(output.contains("claimed"), "got: {output:?}");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn take_unclaimed_reader_yields_nothing_after_native_stdio() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg("printf bridged");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let stdio = terminal.take_native_stdio().expect("take native stdio");
assert!(
terminal
.take_unclaimed_reader()
.expect("take unclaimed reader")
.is_none(),
"the native bridge owns terminal output"
);
assert!(wait_for_exit(&mut child).success());
terminal.finish_native_bridge();
drop(stdio);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn dropping_untouched_writer_does_not_wait_for_an_input_reader() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg("sleep 30");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let drain = PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
);
let started = std::time::Instant::now();
drop(terminal.take_writer().expect("take writer"));
assert!(
started.elapsed() < Duration::from_secs(5),
"closing untouched canonical input blocked without a reader"
);
let _ = child.kill();
let _ = child.wait();
drop(terminal);
let _ = drain.finish();
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn dropping_writer_does_not_wait_for_a_full_input_queue() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg("sleep 30");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let drain = PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
);
let mut fill = terminal
.access
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.writer
.as_ref()
.expect("PTY writer")
.try_clone()
.expect("clone writer");
let buffer = [b'x'; 4096];
let mut queue_full = false;
for _ in 0..4096 {
match fill.write(&buffer) {
Ok(_) => {}
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
queue_full = true;
break;
}
Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {}
Err(error) => panic!("fill PTY input queue: {error}"),
}
}
#[cfg(target_os = "linux")]
assert!(queue_full, "PTY input queue did not reach backpressure");
#[cfg(target_os = "macos")]
let _ = queue_full;
drop(fill);
let started = std::time::Instant::now();
drop(terminal.take_writer().expect("take writer"));
assert!(
started.elapsed() < Duration::from_secs(3),
"closing canonical input blocked on a full input queue"
);
let _ = child.kill();
let _ = child.wait();
drop(terminal);
let _ = drain.finish();
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn assert_canonical_eof_after_input(setup: &str, input: &[u8], native: bool) {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg(format!(
"{setup}; printf ready; cat >/dev/null; printf terminal-eof-observed"
));
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let (mut writer, drain): (Box<dyn Write + Send>, PtyDrain) = if native {
let stdio = terminal.take_native_stdio().expect("take native stdio");
(
Box::new(std::fs::File::from(stdio.stdin)),
PtyDrain::start(Box::new(std::fs::File::from(stdio.stdout))),
)
} else {
(
Box::new(terminal.take_writer().expect("take writer")),
PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
),
)
};
drain.wait_for("ready");
writer.write_all(input).expect("write terminal input");
drop(writer);
assert!(wait_for_exit(&mut child).success());
terminal.finish_native_bridge();
drop(terminal);
let text = drain.finish();
assert!(text.contains("terminal-eof-observed"), "got: {text:?}");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn boxed_writer_tracks_cr_without_icrnl_as_partial_input() {
assert_canonical_eof_after_input("stty -echo -icrnl", b"hello\r", false);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn native_writer_tracks_cr_without_icrnl_as_partial_input() {
assert_canonical_eof_after_input("stty -echo -icrnl", b"hello\r", true);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn boxed_writer_tracks_kill_character_as_empty_input() {
assert_canonical_eof_after_input("stty -echo", b"abc\x15", false);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn native_writer_tracks_kill_character_as_empty_input() {
assert_canonical_eof_after_input("stty -echo", b"abc\x15", true);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn boxed_writer_closes_after_pending_literal_next() {
assert_canonical_eof_after_input("stty -echo", b"abc\x16", false);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn native_writer_closes_after_pending_literal_next() {
assert_canonical_eof_after_input("stty -echo", b"abc\x16", true);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn boxed_writer_tracks_input_beyond_posix_max_canon() {
let mut input = vec![b'x'; 300];
input.extend(std::iter::repeat_n(127, 255));
assert_canonical_eof_after_input("stty -echo erase '^?'", &input, false);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn native_writer_tracks_input_beyond_posix_max_canon() {
let mut input = vec![b'x'; 300];
input.extend(std::iter::repeat_n(127, 255));
assert_canonical_eof_after_input("stty -echo erase '^?'", &input, true);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn assert_disabled_eof_does_not_inject_input(native: bool) {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg(
"stty -echo eof undef; printf ready; dd bs=1 count=1 2>/dev/null; printf injected",
);
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let (writer, drain): (Option<Box<dyn Write + Send>>, PtyDrain) = if native {
let stdio = terminal.take_native_stdio().expect("take native stdio");
(
Some(Box::new(std::fs::File::from(stdio.stdin))),
PtyDrain::start(Box::new(std::fs::File::from(stdio.stdout))),
)
} else {
(
None,
PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
),
)
};
drain.wait_for("ready");
match writer {
Some(writer) => drop(writer),
None => drop(terminal.take_writer().expect("take writer")),
}
std::thread::sleep(Duration::from_millis(200));
assert!(child.try_wait().expect("poll child").is_none());
let _ = child.kill();
let _ = child.wait();
terminal.finish_native_bridge();
drop(terminal);
let text = drain.finish();
assert!(!text.contains("injected"), "got: {text:?}");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn boxed_writer_skips_disabled_eof() {
assert_disabled_eof_does_not_inject_input(false);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn native_writer_skips_disabled_eof() {
assert_disabled_eof_does_not_inject_input(true);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn canonical_tracking_is_bounded() {
let (master, _slave) = open_pty(None).expect("open PTY");
let file: std::fs::File = master.into();
let discipline = PtyDiscipline {
canonical: true,
extended: true,
strip_high_bit: false,
ignore_cr: false,
cr_to_nl: true,
nl_to_cr: false,
eof: 4,
eol: 0,
eol2: 0,
erase: 127,
kill: 21,
word_erase: 23,
literal_next: 22,
};
let mut writer = PtyWriter::new(file, PtyEof::Forwarded(discipline));
writer.track_canonical_input(&vec![b'x'; writer.canonical_limit + 1024]);
assert_eq!(writer.canonical_line.len(), writer.canonical_limit);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn writer_blocks_until_nonblocking_primary_accepts_large_input() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg("cat >/dev/null");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let drain = PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
);
let mut writer = terminal.take_writer().expect("take writer");
let mut input = b"x\n".repeat(128 * 1024);
input.extend_from_slice(b"partial-line");
writer.write_all(&input).expect("write large partial input");
drop(writer);
assert!(wait_for_exit(&mut child).success());
drop(terminal);
let _ = drain.finish();
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn reader_cancellation_unblocks_descendant_held_terminal() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command
.arg("-c")
.arg("(sleep 30) & printf 'foreground-exited\\n'");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let drain = PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
);
assert!(wait_for_exit(&mut child).success());
drain.wait_for("foreground-exited");
drain.cancel();
let output = drain.finish();
assert!(output.contains("foreground-exited"), "got: {output:?}");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn native_stdio_bridges_terminal_io_once() {
use std::fs::File;
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command
.arg("-c")
.arg("read value; printf 'native-reply:%s\\n' \"$value\"");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let stdio = terminal.take_native_stdio().expect("take native stdio");
assert!(terminal.take_writer().is_err());
assert!(terminal.try_clone_reader().is_err());
let mut input = File::from(stdio.stdin);
let mut output = File::from(stdio.stdout);
input.write_all(b"hello").expect("write partial input");
drop(input);
assert!(wait_for_exit(&mut child).success());
terminal.finish_native_bridge();
let mut text = String::new();
output.read_to_string(&mut text).expect("read output");
assert!(text.contains("native-reply:hello"), "got: {text:?}");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn native_bridge_delivers_eof_after_large_partial_input() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command
.arg("-c")
.arg("stty -echo; sleep 0.1; cat >/dev/null; printf 'native-eof-observed\\n'");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let stdio = terminal.take_native_stdio().expect("take native stdio");
let input_thread = std::thread::spawn(move || {
let mut input = std::fs::File::from(stdio.stdin);
let mut bytes = b"x\n".repeat(128 * 1024);
bytes.extend_from_slice(b"partial-line");
input.write_all(&bytes).expect("write large partial input");
drop(input);
stdio.stdout
});
assert!(wait_for_exit(&mut child).success());
terminal.finish_native_bridge();
let stdout = input_thread.join().expect("input thread");
let mut output = std::fs::File::from(stdout);
let mut text = String::new();
output.read_to_string(&mut text).expect("read output");
assert!(text.contains("native-eof-observed"), "got: {text:?}");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn native_bridge_finishes_when_descendant_holds_terminal() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg("(sleep 30) & printf 'done\\n'");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
let session = child.id() as libc::pid_t;
drop(command);
let stdio = terminal.take_native_stdio().expect("take native stdio");
drop(stdio.stdin);
assert!(wait_for_exit(&mut child).success());
let start = std::time::Instant::now();
terminal.finish_native_bridge();
assert!(
start.elapsed() < Duration::from_secs(5),
"native bridge did not cancel promptly"
);
unsafe {
libc::kill(-session, libc::SIGKILL);
}
let mut output = std::fs::File::from(stdio.stdout);
let mut text = String::new();
output.read_to_string(&mut text).expect("read output");
assert!(text.contains("done"), "got: {text:?}");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn drain_available(fd: std::os::fd::RawFd) -> String {
let previous = unsafe { libc::fcntl(fd, libc::F_GETFL) };
if previous < 0 {
return String::new();
}
unsafe { libc::fcntl(fd, libc::F_SETFL, previous | libc::O_NONBLOCK) };
let mut collected = Vec::new();
let mut buffer = [0_u8; 4096];
loop {
let count = unsafe { libc::read(fd, buffer.as_mut_ptr().cast(), buffer.len()) };
if count <= 0 {
break;
}
collected.extend_from_slice(&buffer[..count as usize]);
}
unsafe { libc::fcntl(fd, libc::F_SETFL, previous) };
String::from_utf8_lossy(&collected).into_owned()
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn wait_for_marker(fd: std::os::fd::RawFd, marker: &str) -> String {
const MARKER_TIMEOUT: Duration = Duration::from_secs(10);
const MARKER_POLL: Duration = Duration::from_millis(10);
let deadline = std::time::Instant::now() + MARKER_TIMEOUT;
let mut collected = String::new();
while std::time::Instant::now() < deadline {
collected.push_str(&drain_available(fd));
if collected.contains(marker) {
return collected;
}
std::thread::sleep(MARKER_POLL);
}
panic!("timed out waiting for {marker:?}; got: {collected:?}");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn native_bridge_cancellation_does_not_inject_eof() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command
.arg("-c")
.arg("printf 'done\\n'; read value; printf 'eof-injected'");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
let session = child.id() as libc::pid_t;
drop(command);
let stdio = terminal.take_native_stdio().expect("take native stdio");
let input = stdio.stdin;
let mut text = wait_for_marker(stdio.stdout.as_raw_fd(), "done");
terminal.finish_native_bridge();
drop(input);
let watch = std::time::Instant::now() + Duration::from_millis(500);
while std::time::Instant::now() < watch {
text.push_str(&drain_available(stdio.stdout.as_raw_fd()));
text.push_str(&drain_available(terminal.primary.as_raw_fd()));
assert!(!text.contains("eof-injected"), "got: {text:?}");
assert!(
child.try_wait().expect("poll child").is_none(),
"cancellation ended the shell's `read`; got: {text:?}"
);
std::thread::sleep(Duration::from_millis(10));
}
unsafe {
libc::kill(-session, libc::SIGKILL);
}
let deadline = std::time::Instant::now() + Duration::from_secs(10);
loop {
text.push_str(&drain_available(stdio.stdout.as_raw_fd()));
text.push_str(&drain_available(terminal.primary.as_raw_fd()));
if child.try_wait().expect("poll child").is_some() {
break;
}
assert!(
std::time::Instant::now() < deadline,
"the shell did not exit after SIGKILL; got: {text:?}"
);
std::thread::sleep(Duration::from_millis(10));
}
assert!(text.contains("done"), "got: {text:?}");
assert!(!text.contains("eof-injected"), "got: {text:?}");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn native_bridge_preserves_fast_multichunk_output() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command
.arg("-c")
.arg("head -c 262144 /dev/zero; printf 'complete-output\\n'");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let stdio = terminal.take_native_stdio().expect("take native stdio");
drop(stdio.stdin);
let output_thread = std::thread::spawn(move || {
let mut output = std::fs::File::from(stdio.stdout);
let mut bytes = Vec::new();
output.read_to_end(&mut bytes).expect("read output");
bytes
});
assert!(wait_for_exit(&mut child).success());
terminal.finish_native_bridge();
let output = output_thread.join().expect("output thread");
assert!(output.len() >= 262_144, "output was truncated");
assert!(
output.ends_with(b"complete-output\r\n"),
"final output marker was truncated"
);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn native_bridge_cancels_blocked_input_write() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg("(trap '' HUP; sleep 30) & exit 0");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let stdio = terminal.take_native_stdio().expect("take native stdio");
drop(stdio.stdout);
let input_thread = std::thread::spawn(move || {
let mut input = std::fs::File::from(stdio.stdin);
let buffer = vec![b'x'; 1024 * 1024];
let _ = input.write_all(&buffer);
});
assert!(wait_for_exit(&mut child).success());
let start = std::time::Instant::now();
terminal.finish_native_bridge();
assert!(
start.elapsed() < Duration::from_secs(5),
"blocked input bridge did not cancel promptly"
);
input_thread.join().expect("input thread");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn native_bridge_survives_closed_output_consumer() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg("printf output-after-close");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let stdio = terminal.take_native_stdio().expect("take native stdio");
drop(stdio.stdin);
drop(stdio.stdout);
assert!(wait_for_exit(&mut child).success());
terminal.finish_native_bridge();
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn native_bridge_keeps_draining_after_the_output_consumer_closes() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg("read go; head -c 1000000 /dev/zero");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let stdio = terminal.take_native_stdio().expect("take native stdio");
drop(stdio.stdout);
let mut input = std::fs::File::from(stdio.stdin);
input.write_all(b"go\n").expect("write input");
drop(input);
assert!(wait_for_exit(&mut child).success());
terminal.finish_native_bridge();
}
#[cfg(target_os = "linux")]
#[test]
fn native_bridge_cancels_full_output_pipe() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg("read ready; head -c 8192 /dev/zero");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let stdio = terminal.take_native_stdio().expect("take native stdio");
assert!(
unsafe { libc::fcntl(stdio.stdout.as_raw_fd(), libc::F_SETPIPE_SZ, 4096) } >= 0,
"set output pipe capacity: {}",
std::io::Error::last_os_error()
);
let mut input = std::fs::File::from(stdio.stdin);
input.write_all(b"go\n").expect("write input");
drop(input);
let deadline = std::time::Instant::now() + Duration::from_secs(5);
loop {
if let Some(status) = child.try_wait().expect("poll child") {
assert!(status.success());
break;
}
if std::time::Instant::now() >= deadline {
let _ = child.kill();
let _ = child.wait();
panic!("child blocked before filling the native output pipe");
}
std::thread::sleep(Duration::from_millis(10));
}
let start = std::time::Instant::now();
terminal.finish_native_bridge();
assert!(
start.elapsed() < Duration::from_secs(5),
"full output bridge did not cancel promptly"
);
drop(stdio.stdout);
}
#[cfg(target_os = "macos")]
#[test]
fn native_bridge_cancels_when_the_output_pipe_fills() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg("head -c 1000000 /dev/zero");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let stdio = terminal.take_native_stdio().expect("take native stdio");
drop(stdio.stdin);
std::thread::sleep(Duration::from_millis(250));
assert!(
child.try_wait().expect("poll child").is_none(),
"child should still be blocked behind the full output pipe"
);
let (done, finished) = std::sync::mpsc::sync_channel(1);
std::thread::scope(|scope| {
scope.spawn(|| {
terminal.finish_native_bridge();
let _ = done.send(());
});
let cancelled = finished.recv_timeout(Duration::from_secs(5)).is_ok();
if !cancelled {
let mut sink = std::fs::File::from(stdio.stdout.try_clone().expect("clone stdout"));
let mut scratch = [0u8; 8192];
while sink.read(&mut scratch).unwrap_or(0) > 0 {}
}
assert!(cancelled, "full output bridge did not cancel promptly");
});
drop(stdio.stdout);
drop(terminal);
let _ = wait_for_exit(&mut child);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn size_reports_the_last_applied_dimensions() {
let mut command = Command::new("/bin/sh");
command.arg("-c").arg("true");
let initial = PtySize {
rows: 24,
cols: 80,
pixel_width: 0,
pixel_height: 0,
};
let terminal = LivePty::attach(&mut command, initial, &[]).expect("attach PTY");
assert_eq!(terminal.size(), initial);
let resized = PtySize {
rows: 40,
cols: 120,
pixel_width: 0,
pixel_height: 0,
};
terminal.resize(resized).expect("resize PTY");
assert_eq!(terminal.size(), resized);
assert_eq!(
terminal
.resize(PtySize {
rows: 0,
cols: 0,
pixel_width: 0,
pixel_height: 0,
})
.expect_err("zero dimensions are rejected")
.kind(),
std::io::ErrorKind::InvalidInput
);
assert_eq!(terminal.size(), resized);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn is_canonical_mode_follows_the_terminal_discipline() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command
.arg("-c")
.arg("printf 'ready\\n'; read go; stty -icanon; printf 'canonical-off\\n'; read done");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let drain = PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
);
let mut writer = terminal.take_writer().expect("take writer");
drain.wait_for("ready");
assert!(terminal.is_canonical_mode().expect("read discipline"));
writer.write_all(b"go\r").expect("write input");
drain.wait_for("canonical-off");
assert!(!terminal.is_canonical_mode().expect("read discipline"));
drop(writer);
let _ = child.kill();
let _ = wait_for_exit(&mut child);
drop(terminal);
let _ = drain.finish();
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn resize_signals_the_foreground_process_group() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg(
"trap 'stty size; exit 0' WINCH; printf 'ready\\n'; \
i=0; while [ $i -lt 200 ]; do sleep 0.1; i=$((i+1)); done",
);
let terminal = LivePty::attach(
&mut command,
PtySize {
rows: 24,
cols: 80,
pixel_width: 0,
pixel_height: 0,
},
&[],
)
.expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let drain = PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
);
drain.wait_for("ready");
terminal
.resize(PtySize {
rows: 40,
cols: 120,
pixel_width: 0,
pixel_height: 0,
})
.expect("resize PTY");
let seen = drain.wait_for("40 120");
assert!(seen.contains("40 120"), "got: {seen:?}");
assert!(wait_for_exit(&mut child).success());
drop(terminal);
let _ = drain.finish();
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn forwarded_eof_is_delivered_through_a_raw_mode_terminal() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command.arg("-c").arg(
"stty raw -echo; printf 'ready\\r\\n'; \
head -c 1 | od -An -tx1 | tr -d ' \\n'; printf '\\r\\n'",
);
let terminal = LivePty::attach_forwarded(&mut command, PtySize::default(), &[])
.expect("attach forwarded PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let drain = PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
);
drain.wait_for("ready");
drop(terminal.take_writer().expect("take writer"));
assert!(wait_for_exit(&mut child).success());
drop(terminal);
let output = drain.finish();
assert!(output.contains("04"), "expected ^D byte; got: {output:?}");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn current_canonical_eof_is_skipped_for_a_raw_mode_terminal() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/bin/sh");
command
.arg("-c")
.arg("stty raw -echo; printf 'ready\\r\\n'; head -c 1 >/dev/null");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let mut child = command.spawn().expect("spawn child");
drop(command);
let drain = PtyDrain::start_cancellable(
terminal
.try_clone_reader_with_canceller()
.expect("clone reader"),
);
drain.wait_for("ready");
drop(terminal.take_writer().expect("take writer"));
std::thread::sleep(Duration::from_secs(1));
assert!(
child.try_wait().expect("poll child").is_none(),
"no EOF byte should reach a raw-mode terminal"
);
let _ = child.kill();
let _ = wait_for_exit(&mut child);
drop(terminal);
let _ = drain.finish();
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn raw_secondary_guard_sets_raw_mode_and_restores_on_drop() {
use nix::sys::termios::{self, LocalFlags};
let (primary, secondary) = open_pty(None).expect("open PTY pair");
let before = termios::tcgetattr(&secondary).expect("read termios");
assert!(before.local_flags.contains(LocalFlags::ICANON));
{
let _raw = RawSecondaryGuard::install(secondary.as_raw_fd()).expect("install raw mode");
let during = termios::tcgetattr(&secondary).expect("read termios");
assert!(!during.local_flags.contains(LocalFlags::ICANON));
assert!(!during.local_flags.contains(LocalFlags::ECHO));
}
let after = termios::tcgetattr(&secondary).expect("read termios");
let settable = |flags: LocalFlags| flags - LocalFlags::PENDIN;
assert_eq!(settable(after.local_flags), settable(before.local_flags));
assert_eq!(after.input_flags, before.input_flags);
assert_eq!(after.output_flags, before.output_flags);
drop(primary);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn raw_secondary_guard_declines_a_non_tty() {
let (read, write) = create_pipe().expect("create pipe");
assert!(RawSecondaryGuard::install(read.as_raw_fd()).is_none());
drop((read, write));
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn attach_still_reports_a_failed_exec() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut command = Command::new("/nonexistent/mxc-pty-missing-binary");
let terminal = LivePty::attach(&mut command, PtySize::default(), &[]).expect("attach PTY");
let error = command
.spawn()
.expect_err("missing binary must fail to spawn");
assert_eq!(error.kind(), std::io::ErrorKind::NotFound);
drop(terminal);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn echo_runs_under_pty() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut cmd = Command::new("/bin/sh");
cmd.arg("-c").arg("echo hello-from-pty");
let outcome = run_with_pty(cmd, PtyOptions::default()).expect("bridge spawns");
match outcome {
PtyOutcome::Exited(status) => assert!(status.success()),
PtyOutcome::TimedOut => panic!("echo should not time out"),
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn timeout_kills_long_running_child() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut cmd = Command::new("/bin/sh");
cmd.arg("-c").arg("sleep 30");
let opts = PtyOptions {
timeout: Some(Duration::from_millis(750)),
..PtyOptions::default()
};
let outcome = run_with_pty(cmd, opts).expect("bridge spawns");
assert!(matches!(outcome, PtyOutcome::TimedOut));
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn allocated_pty_descriptors_are_cloexec() {
use nix::fcntl::{fcntl, FcntlArg, FdFlag};
let (master, slave) = open_pty(None).expect("open PTY");
for (label, fd) in [
("primary", master.as_raw_fd()),
("secondary", slave.as_raw_fd()),
] {
let bits = fcntl(fd, FcntlArg::F_GETFD).expect("F_GETFD");
let flags = FdFlag::from_bits_truncate(bits);
assert!(flags.contains(FdFlag::FD_CLOEXEC), "{label} lacked CLOEXEC");
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn native_bridge_descriptors_are_cloexec() {
use nix::fcntl::{fcntl, FcntlArg, FdFlag};
let (read, write) = create_pipe().expect("create pipe");
for (label, fd) in [("read", read.as_raw_fd()), ("write", write.as_raw_fd())] {
let bits = fcntl(fd, FcntlArg::F_GETFD).expect("F_GETFD");
let flags = FdFlag::from_bits_truncate(bits);
assert!(flags.contains(FdFlag::FD_CLOEXEC), "{label} lacked CLOEXEC");
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn pty_fds_do_not_leak_into_child_across_exec() {
let _guard = RUN_WITH_PTY_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let inherited = process_pty_fds(std::process::id())
.expect("inspect parent descriptors")
.into_iter()
.map(|(fd, _)| fd)
.collect::<std::collections::HashSet<_>>();
let mut cmd = Command::new("/bin/sleep");
cmd.arg("30");
let terminal = LivePty::attach(&mut cmd, PtySize::default(), &[]).expect("attach PTY");
let mut child = cmd.spawn().expect("spawn child");
drop(cmd);
std::thread::sleep(Duration::from_millis(50));
let leaks = process_pty_fds(child.id())
.expect("inspect child descriptors")
.into_iter()
.filter(|(fd, _)| !inherited.contains(fd))
.map(|(fd, target)| format!("LEAK fd={fd} target={target}"))
.collect::<Vec<_>>();
let _ = child.kill();
let _ = child.wait();
drop(terminal);
assert!(
leaks.is_empty(),
"child inherited pty fd(s) across exec:\n{}",
leaks.join("\n"),
);
}
#[cfg(target_os = "linux")]
fn process_pty_fds(pid: u32) -> std::io::Result<Vec<(String, String)>> {
let mut pty_fds = Vec::new();
for entry in std::fs::read_dir(format!("/proc/{pid}/fd"))? {
let entry = entry?;
let fd = entry.file_name().to_string_lossy().into_owned();
if matches!(fd.as_str(), "0" | "1" | "2") {
continue;
}
let Ok(target) = std::fs::read_link(entry.path()) else {
continue;
};
let target = target.to_string_lossy();
if target == "/dev/ptmx" || target.starts_with("/dev/pts/") {
pty_fds.push((fd, target.into_owned()));
}
}
Ok(pty_fds)
}
#[cfg(target_os = "macos")]
fn process_pty_fds(pid: u32) -> std::io::Result<Vec<(String, String)>> {
let output = Command::new("/usr/sbin/lsof")
.args(["-p", &pid.to_string()])
.output()?;
Ok(String::from_utf8_lossy(&output.stdout)
.lines()
.skip(1)
.filter_map(|line| {
let columns = line.split_whitespace().collect::<Vec<_>>();
let fd = columns.get(3)?;
let target = columns.last()?;
let fd_number =
fd.trim_end_matches(|character: char| character.is_ascii_alphabetic());
(fd_number.parse::<u32>().ok()? > 2
&& (target.starts_with("/dev/ttys")
|| target.starts_with("/dev/pts/")
|| *target == "/dev/ptmx"))
.then(|| (fd_number.to_string(), (*target).to_string()))
})
.collect())
}
}