Repository navigation
Expand file tree
/
Copy pathpty.rs
More file actions
457 lines (415 loc) · 15.5 KB
/
Copy pathpty.rs
File metadata and controls
457 lines (415 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Pseudo‑terminal helper.
//!
//! Adapted from the `ghostling_rs` example in libghostty‑rs. We `forkpty`
//! a child shell, put the master fd into non‑blocking mode and expose
//! `read`/`write`/`resize` plus the underlying fd for `poll(2)`.
#![allow(unsafe_code)]
use std::{
ffi::OsString,
os::{
fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd},
unix::process::CommandExt,
},
path::PathBuf,
process::Command,
};
use anyhow::{Context, Result, bail};
use libghostty_vt::Terminal;
use nix::{
errno::Errno,
fcntl::{self, OFlag},
pty::{ForkptyResult, Winsize},
sys::{signal, wait},
unistd::Pid,
};
/// Master side of the pseudo-terminal.
pub struct Pty(OwnedFd);
#[derive(Clone, Copy, Debug)]
pub struct PtySize {
pub cols: u16,
pub rows: u16,
pub px_w: u16,
pub px_h: u16,
}
impl PtySize {
fn to_winsize(self) -> Winsize {
Winsize {
ws_col: self.cols,
ws_row: self.rows,
ws_xpixel: self.px_w,
ws_ypixel: self.px_h,
}
}
}
#[derive(Debug)]
pub enum PtyError {
EndOfStream,
Other(Errno),
}
impl std::fmt::Display for PtyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PtyError::EndOfStream => write!(f, "pty closed"),
PtyError::Other(e) => write!(f, "pty error: {e}"),
}
}
}
impl std::error::Error for PtyError {}
impl Pty {
/// Spawn `shell` (or the user's default) in a fresh pseudo-terminal,
/// applying `env` and an initial window `size`.
pub fn spawn(
shell: Option<&str>,
env: &[(String, String)],
size: PtySize,
mimic_vhs: bool,
) -> Result<(Self, Child)> {
let _timer = crate::telemetry::ScopeTimer::new("pty_spawn");
// Resolved command list and environment variables
let mut shell_cmd = Vec::new();
let mut extra_envs = Vec::new();
if mimic_vhs {
let shell_name = match shell {
Some(raw) if !raw.trim().is_empty() => {
let raw = raw.trim();
let parts = parse_shell_command(raw)?;
if parts.len() != 1 {
bail!("shell command has more than one word: `{}`", raw);
}
parts[0].clone()
}
_ => "bash".to_string(),
};
let (cmd_args, extra_env) = match shell_name.as_str() {
"bash" => (
vec!["bash".to_string(), "--noprofile".to_string(), "--norc".to_string(), "--login".to_string(), "+o".to_string(), "history".to_string()],
vec![("PS1".to_string(), "\\[\\e[38;2;90;86;224m\\]> \\[\\e[0m\\]".to_string()), ("BASH_SILENCE_DEPRECATION_WARNING".to_string(), "1".to_string())]
),
"zsh" => (
vec!["zsh".to_string(), "--histnostore".to_string(), "--no-rcs".to_string()],
vec![("PROMPT".to_string(), "%F{#5B56E0}> %F{reset_color}".to_string())]
),
"fish" => (
vec![
"fish".to_string(),
"--login".to_string(),
"--no-config".to_string(),
"--private".to_string(),
"-C".to_string(), "function fish_greeting; end".to_string(),
"-C".to_string(), "function fish_prompt; set_color 5B56E0; echo -n \"> \"; set_color normal; end".to_string()
],
vec![]
),
"powershell" => (
vec![
"powershell".to_string(),
"-NoLogo".to_string(),
"-NoExit".to_string(),
"-NoProfile".to_string(),
"-Command".to_string(),
"Set-PSReadLineOption -HistorySaveStyle SaveNothing; function prompt { Write-Host '>' -NoNewLine -ForegroundColor Blue; return ' ' }".to_string()
],
vec![]
),
"pwsh" => (
vec![
"pwsh".to_string(),
"-Login".to_string(),
"-NoLogo".to_string(),
"-NoExit".to_string(),
"-NoProfile".to_string(),
"-Command".to_string(),
"Set-PSReadLineOption -HistorySaveStyle SaveNothing; Function prompt { Write-Host -ForegroundColor Blue -NoNewLine '>'; return ' ' }".to_string()
],
vec![]
),
"cmd" => (
vec!["cmd.exe".to_string(), "/k".to_string(), "prompt=^> ".to_string()],
vec![]
),
"nu" => (
vec!["nu".to_string(), "--execute".to_string(), "$env.PROMPT_COMMAND = {'\x1b[;38;2;91;86;224m>\x1b[m '}; $env.PROMPT_COMMAND_RIGHT = {''}".to_string()],
vec![]
),
"osh" => (
vec!["osh".to_string(), "--norc".to_string()],
vec![("PS1".to_string(), "\\[\\e[38;2;90;86;224m\\]> \\[\\e[0m\\]".to_string())]
),
"xonsh" => (
vec!["xonsh".to_string(), "--no-rc".to_string(), "-D".to_string(), "PROMPT=\x1b[;38;2;91;86;224m>\x1b[m ".to_string()],
vec![]
),
other => bail!("invalid shell: {other}"),
};
shell_cmd = cmd_args;
extra_envs = extra_env;
} else {
match shell {
Some(raw) if !raw.trim().is_empty() => {
let parts = parse_shell_command(raw)?;
if parts.is_empty() {
let shell_path = default_shell_path();
shell_cmd.push(shell_path.to_string_lossy().to_string());
} else {
shell_cmd = parts;
}
}
_ => {
let shell_path = default_shell_path();
shell_cmd.push(shell_path.to_string_lossy().to_string());
}
}
}
let ws = size.to_winsize();
match unsafe { nix::pty::forkpty(&ws, None) }.context("forkpty failed")? {
ForkptyResult::Child => {
let program = &shell_cmd[0];
let mut cmd = Command::new(program);
if shell_cmd.len() > 1 {
cmd.args(&shell_cmd[1..]);
}
cmd.arg0(command_arg0(program));
cmd.env("TERM", "xterm-256color");
let is_utf8_locale = |val: &str| -> bool {
let val_lower = val.to_lowercase();
val_lower.contains("utf-8") || val_lower.contains("utf8")
};
let get_effective_env = |name: &str| -> Option<String> {
if let Some((_, v)) = env.iter().find(|(k, _)| k == name) {
Some(v.clone())
} else {
std::env::var(name).ok()
}
};
let has_utf8 = {
if let Some(lc_all) = get_effective_env("LC_ALL") {
is_utf8_locale(&lc_all)
} else if let Some(lc_ctype) = get_effective_env("LC_CTYPE") {
is_utf8_locale(&lc_ctype)
} else if let Some(lang) = get_effective_env("LANG") {
is_utf8_locale(&lang)
} else {
false
}
};
if !has_utf8 {
cmd.env("LANG", "C.UTF-8");
cmd.env("LC_ALL", "C.UTF-8");
}
// Apply VHS-specific envs first
for (k, v) in &extra_envs {
cmd.env(k, v);
}
// Apply user-specified envs next, which will override the VHS defaults if they match
for (k, v) in env {
cmd.env(k, v);
}
let _ = cmd.exec();
std::process::exit(127);
}
ForkptyResult::Parent { child, master: fd } => {
// Non‑blocking so reads return EAGAIN when the kernel buffer
// drains rather than blocking the scheduler loop.
let raw_flags = fcntl::fcntl(&fd, fcntl::F_GETFL)?;
let flags = OFlag::from_bits_retain(raw_flags) | OFlag::O_NONBLOCK;
fcntl::fcntl(&fd, fcntl::F_SETFL(flags))?;
// Wait a tiny bit to check if child failed to exec or exited immediately
std::thread::sleep(std::time::Duration::from_millis(15));
match nix::sys::wait::waitpid(child, Some(nix::sys::wait::WaitPidFlag::WNOHANG)) {
Ok(nix::sys::wait::WaitStatus::Exited(_, status)) => {
if status == 127 {
bail!(
"shell program failed to launch (command not found or exited with 127)"
);
}
}
Ok(nix::sys::wait::WaitStatus::Signaled(_, sig, _)) => {
bail!("shell program crashed immediately (signaled {})", sig);
}
_ => {}
}
Ok((Self(fd), Child::Active(child)))
}
}
}
pub fn fd(&self) -> RawFd {
self.0.as_raw_fd()
}
pub fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
pub fn try_clone(&self) -> Result<Self> {
let fd = self
.0
.try_clone()
.context("failed to clone PTY master fd")?;
Ok(Self(fd))
}
/// Drain all currently available output into the terminal's VT parser.
pub fn drain_into<F>(&self, term: &mut Terminal<'_, '_>, mut on_data: F) -> Result<(), PtyError>
where
F: FnMut(&[u8]),
{
let mut buf = [0u8; 8192];
loop {
match nix::unistd::read(&self.0, &mut buf) {
Ok(0) => return Err(PtyError::EndOfStream),
Ok(n) => {
let data = &buf[..n];
on_data(data);
term.vt_write(data);
}
Err(Errno::EAGAIN) => return Ok(()),
Err(Errno::EINTR) => continue,
Err(Errno::EIO) => return Err(PtyError::EndOfStream),
Err(e) => return Err(PtyError::Other(e)),
}
}
}
/// Best-effort write – drops trailing data on EAGAIN to match the
/// behaviour of typical terminal emulators under back-pressure.
pub fn write(&self, mut data: &[u8]) {
while !data.is_empty() {
match nix::unistd::write(&self.0, data) {
Ok(n) => data = &data[n..],
Err(Errno::EINTR) => continue,
Err(_) => break,
}
}
}
pub fn resize(&self, size: PtySize) {
nix::ioctl_write_ptr_bad!(tiocswinsz, nix::libc::TIOCSWINSZ, Winsize);
let ws = size.to_winsize();
let _ = unsafe { tiocswinsz(self.0.as_raw_fd(), &ws) };
}
}
fn default_shell_path() -> PathBuf {
match std::env::var_os("SHELL") {
Some(s) if !s.is_empty() => PathBuf::from(s),
_ => match nix::unistd::User::from_uid(nix::unistd::getuid()) {
Ok(Some(user)) => user.shell,
_ => PathBuf::from("/bin/sh"),
},
}
}
fn command_arg0(program: impl AsRef<std::ffi::OsStr>) -> OsString {
PathBuf::from(program.as_ref())
.file_name()
.map(|s| s.to_owned())
.unwrap_or_else(|| program.as_ref().to_owned())
}
fn parse_shell_command(input: &str) -> Result<Vec<String>> {
shell_words::split(input).with_context(|| format!("invalid shell command: `{input}`"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_shell_command_handles_shell_forms() {
assert_eq!(
parse_shell_command("bash").unwrap(),
vec!["bash".to_string()]
);
assert_eq!(
parse_shell_command("/bin/bash").unwrap(),
vec!["/bin/bash".to_string()]
);
assert_eq!(
parse_shell_command("bash --norc").unwrap(),
vec!["bash".to_string(), "--norc".to_string()]
);
assert_eq!(
parse_shell_command("bash --rcfile somefile.rc").unwrap(),
vec![
"bash".to_string(),
"--rcfile".to_string(),
"somefile.rc".to_string()
]
);
assert_eq!(
parse_shell_command("fish").unwrap(),
vec!["fish".to_string()]
);
assert_eq!(parse_shell_command("sh").unwrap(), vec!["sh".to_string()]);
assert_eq!(
parse_shell_command("/bin/sh").unwrap(),
vec!["/bin/sh".to_string()]
);
}
#[test]
fn test_pty_spawn_sets_utf8_locale_if_missing() {
use super::{Pty, PtySize};
let (pty, _child) = Pty::spawn(
Some("printenv LANG"),
&[],
PtySize {
cols: 80,
rows: 24,
px_w: 640,
px_h: 380,
},
false,
)
.unwrap();
let mut buf = [0u8; 1024];
let mut output = String::new();
let mut attempts = 0;
let fd = unsafe { std::os::fd::BorrowedFd::borrow_raw(pty.fd()) };
loop {
match nix::unistd::read(&fd, &mut buf) {
Ok(0) => break,
Ok(n) => {
output.push_str(&String::from_utf8_lossy(&buf[..n]));
}
Err(nix::errno::Errno::EAGAIN) => {
attempts += 1;
if attempts > 100 {
break;
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
Err(_) => break,
}
}
assert!(!output.is_empty());
let val = output.trim();
assert!(val.to_lowercase().contains("utf-8") || val.to_lowercase().contains("utf8"));
}
#[test]
fn test_mimic_vhs_validation() {
let size = PtySize {
cols: 80,
rows: 24,
px_w: 640,
px_h: 380,
};
// Multi-word shells are rejected
let res = Pty::spawn(Some("bash --norc"), &[], size, true);
match res {
Err(e) => assert!(e.to_string().contains("more than one word")),
_ => panic!("Expected error, got Ok"),
}
// Unsupported shells are rejected
let res = Pty::spawn(Some("unknown_shell_xyz"), &[], size, true);
match res {
Err(e) => assert!(e.to_string().contains("invalid shell")),
_ => panic!("Expected error, got Ok"),
}
// Supported shell (single-word) is accepted
let res = Pty::spawn(Some("bash"), &[], size, true);
assert!(res.is_ok());
}
}
/// Lifecycle wrapper that ensures we reap the child on drop.
pub enum Child {
Active(Pid),
Reaped,
}
impl Drop for Child {
fn drop(&mut self) {
if let Child::Active(pid) = *self {
let _ = signal::kill(pid, signal::SIGKILL);
let _ = wait::waitpid(pid, None);
}
}
}