| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
use std::ffi::CString; |
| 13 |
use std::fs::File; |
| 14 |
use std::io::{self, Write}; |
| 15 |
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd}; |
| 16 |
use std::path::Path; |
| 17 |
|
| 18 |
use nix::fcntl::{FcntlArg, OFlag, fcntl}; |
| 19 |
use nix::libc; |
| 20 |
use nix::pty::{Winsize, openpty}; |
| 21 |
use nix::sys::signal::{Signal, kill}; |
| 22 |
use nix::unistd::{ForkResult, Pid, execvp, fork, setsid}; |
| 23 |
|
| 24 |
|
| 25 |
#[derive(Debug, Clone, Copy)] |
| 26 |
pub struct PtySize { |
| 27 |
pub cols: u16, |
| 28 |
pub rows: u16, |
| 29 |
pub cell_width: u16, |
| 30 |
pub cell_height: u16, |
| 31 |
} |
| 32 |
|
| 33 |
impl PtySize { |
| 34 |
fn to_winsize(self) -> Winsize { |
| 35 |
Winsize { |
| 36 |
ws_row: self.rows, |
| 37 |
ws_col: self.cols, |
| 38 |
ws_xpixel: self.cols.saturating_mul(self.cell_width), |
| 39 |
ws_ypixel: self.rows.saturating_mul(self.cell_height), |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
pub struct Pty { |
| 47 |
master: OwnedFd, |
| 48 |
child: Pid, |
| 49 |
recorder: Option<File>, |
| 50 |
} |
| 51 |
|
| 52 |
impl Pty { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
pub fn spawn(command: &str, args: &[&str], size: PtySize, term: &str) -> anyhow::Result<Self> { |
| 60 |
let ws = size.to_winsize(); |
| 61 |
let pair = openpty(Some(&ws), None)?; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#[allow(unsafe_code)] |
| 67 |
match unsafe { fork() }? { |
| 68 |
ForkResult::Parent { child } => { |
| 69 |
drop(pair.slave); |
| 70 |
let flags = fcntl(pair.master.as_fd(), FcntlArg::F_GETFL)?; |
| 71 |
let nb = OFlag::from_bits_truncate(flags) | OFlag::O_NONBLOCK; |
| 72 |
fcntl(pair.master.as_fd(), FcntlArg::F_SETFL(nb))?; |
| 73 |
Ok(Self { |
| 74 |
master: pair.master, |
| 75 |
child, |
| 76 |
recorder: None, |
| 77 |
}) |
| 78 |
} |
| 79 |
ForkResult::Child => { |
| 80 |
run_child(pair.slave, command, args, term); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
pub fn resize(&self, size: PtySize) -> anyhow::Result<()> { |
| 87 |
let ws = size.to_winsize(); |
| 88 |
|
| 89 |
|
| 90 |
#[allow(unsafe_code)] |
| 91 |
let rc = unsafe { |
| 92 |
libc::ioctl( |
| 93 |
self.master.as_raw_fd(), |
| 94 |
libc::TIOCSWINSZ, |
| 95 |
std::ptr::from_ref(&ws), |
| 96 |
) |
| 97 |
}; |
| 98 |
if rc != 0 { |
| 99 |
anyhow::bail!(io::Error::last_os_error()); |
| 100 |
} |
| 101 |
Ok(()) |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> { |
| 112 |
let n = nix::unistd::read(&self.master, buf).map_err(io::Error::from)?; |
| 113 |
if n > 0 { |
| 114 |
if let Some(f) = self.recorder.as_ref() { |
| 115 |
let _ = (&*f).write_all(&buf[..n]); |
| 116 |
} |
| 117 |
} |
| 118 |
Ok(n) |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
pub fn set_recorder(&mut self, path: impl AsRef<Path>) -> io::Result<()> { |
| 128 |
self.recorder = Some(File::create(path)?); |
| 129 |
Ok(()) |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
pub fn write(&self, buf: &[u8]) -> io::Result<usize> { |
| 134 |
nix::unistd::write(&self.master, buf).map_err(io::Error::from) |
| 135 |
} |
| 136 |
|
| 137 |
pub fn child(&self) -> Pid { |
| 138 |
self.child |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
impl AsFd for Pty { |
| 143 |
fn as_fd(&self) -> BorrowedFd<'_> { |
| 144 |
self.master.as_fd() |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
impl Drop for Pty { |
| 149 |
fn drop(&mut self) { |
| 150 |
let _ = kill(self.child, Signal::SIGHUP); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
fn run_child(slave: OwnedFd, command: &str, args: &[&str], term: &str) -> ! { |
| 155 |
|
| 156 |
let _ = setsid(); |
| 157 |
|
| 158 |
|
| 159 |
#[allow(unsafe_code)] |
| 160 |
unsafe { |
| 161 |
libc::ioctl(slave.as_raw_fd(), libc::TIOCSCTTY as _, 0); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
let slave_raw = slave.as_raw_fd(); |
| 168 |
#[allow(unsafe_code)] |
| 169 |
unsafe { |
| 170 |
libc::dup2(slave_raw, libc::STDIN_FILENO); |
| 171 |
libc::dup2(slave_raw, libc::STDOUT_FILENO); |
| 172 |
libc::dup2(slave_raw, libc::STDERR_FILENO); |
| 173 |
} |
| 174 |
drop(slave); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
#[allow(unsafe_code)] |
| 179 |
unsafe { |
| 180 |
std::env::set_var("TERM", term); |
| 181 |
} |
| 182 |
|
| 183 |
let cmd_c = CString::new(command).expect("command has NUL"); |
| 184 |
let mut arg_cstrs: Vec<CString> = Vec::with_capacity(args.len() + 1); |
| 185 |
arg_cstrs.push(cmd_c.clone()); |
| 186 |
for a in args { |
| 187 |
arg_cstrs.push(CString::new(*a).expect("arg has NUL")); |
| 188 |
} |
| 189 |
let _ = execvp(&cmd_c, &arg_cstrs); |
| 190 |
|
| 191 |
std::process::exit(127); |
| 192 |
} |
| 193 |
|