From 15235f7530d2b24bbd3215d916c1763180b41475 Mon Sep 17 00:00:00 2001 From: James Long Date: Wed, 11 Nov 2026 11:09:38 -0600 Subject: [PATCH] feat(process-daemon): pipe mode on Windows (#63196) --- packages/process-daemon/Cargo.toml | 2 + packages/process-daemon/SPEC.md | 81 +- packages/process-daemon/src/protocol.rs | 3 +- packages/process-daemon/src/service.rs | 70 +- packages/process-daemon/src/service/pipe.rs | 749 ++++------ .../process-daemon/src/service/pipe/unix.rs | 435 ++++++ .../src/service/pipe/windows.rs | 1201 +++++++++++++++++ packages/process-daemon/src/transport/mod.rs | 2 +- .../process-daemon/src/transport/windows.rs | 2 +- packages/process-daemon/tests/pipe_windows.rs | 778 +++++++++++ 10 files changed, 2728 insertions(+), 595 deletions(-) create mode 100644 packages/process-daemon/src/service/pipe/unix.rs create mode 100644 packages/process-daemon/src/service/pipe/windows.rs create mode 100644 packages/process-daemon/tests/pipe_windows.rs diff --git a/packages/process-daemon/Cargo.toml b/packages/process-daemon/Cargo.toml index c6287dbbadb..f6be7f45168 100644 --- a/packages/process-daemon/Cargo.toml +++ b/packages/process-daemon/Cargo.toml @@ -27,10 +27,12 @@ sha2 = "0.10" [target.'cfg(windows)'.dependencies] windows-sys = { version = "0.61.2", features = [ "Win32_Foundation", + "Win32_Globalization", "Win32_Security", "Win32_Security_Authorization", "Win32_Storage_FileSystem", "Win32_System_IO", + "Win32_System_JobObjects", "Win32_System_Pipes", "Win32_System_Threading", "Win32_System_WindowsProgramming", diff --git a/packages/process-daemon/SPEC.md b/packages/process-daemon/SPEC.md index ec4180bc76d..55a35075ca3 100644 --- a/packages/process-daemon/SPEC.md +++ b/packages/process-daemon/SPEC.md @@ -134,23 +134,33 @@ unknown id. Both refresh a PTY's foreground process first. `create_pty { program, args, cwd, env, group_id, title, cols, rows }` starts a PTY. `create_pipe { program, args, cwd, env, group_id, output_path, metadata }` starts a pipe process: any command without a terminal. Both return -`Created { process }`. Pipe processes are Unix-only for now; a Windows daemon -answers `create_pipe` with an error. +`Created { process }`. Pipe processes work on Unix and Windows. - The child gets exactly `env`: the daemon's own environment is not inherited, and no `TERM` or `COLORTERM` is added. `cwd` must be absolute. -- The child gets its own process group, an empty signal mask with default - actions (the daemon blocks SIGTERM and SIGINT, and std keeps the mask across - exec), stdin on `/dev/null`, and one pipe for both stdout and stderr, so - their bytes stay in order. Nothing parses the output: there is no emulator, - no terminal replies, no OSC title and no foreground process. +- The child gets no terminal: stdin is `/dev/null` on Unix and `NUL` on + Windows, and both stdout and stderr share one pipe, so their bytes stay in + order. Nothing parses the output: there is no emulator, no terminal replies, + no OSC title and no foreground process. +- On Unix the child gets its own process group and an empty signal mask with + default actions (the daemon blocks SIGTERM and SIGINT, and std keeps the + mask across exec). On Windows it is created with `CREATE_NO_WINDOW` (no + console window and no console) and is placed in a kill-on-close job from + creation. The program is resolved the way `CreateProcessW` resolves a bare + executable name (the application directory, the current directory, the + system directories and `PATH`); the daemon does not run a shell unless the + request itself asks for one, and passing `["/c", command]` to `cmd.exe` is + the caller's job. - `output_path` must be absolute. The daemon opens it before spawning, - truncating an existing file or creating one with mode `0666` minus the - daemon's umask, and refuses a symlink as the final path component. The - request is validated (program, absolute paths, metadata size) before the - file is opened, so such a rejected request never creates or truncates it; a - path that cannot be opened fails the request without starting the process. - A missing program or `cwd` fails at spawn, after the file was opened. + truncating an existing file or creating one (mode `0666` minus the daemon's + umask on Unix), and refuses a symlink or other reparse point as the final + path component. The request is validated (program, absolute paths, metadata + size) before the file is opened, so such a rejected request never creates or + truncates it; a path that cannot be opened fails the request without + starting the process. A missing program or `cwd` fails at spawn, after the + file was opened. The file is opened for reading, writing and deletion by + other handles, so a reader can follow it and the owner can delete it while + the process still runs. - The reader writes every output byte to the file before handing it to the actor. The file is the only copy of the output: a pipe process has no replay buffer, so `replay` and `subscribe` return an empty replay (with correct @@ -162,18 +172,22 @@ answers `create_pipe` with an error. - Exit is published once both the exit status and the end of output are known. A descendant that keeps the pipe open (`cmd &`, dev servers) delays that by at most one second after the child exits; the reader then closes the pipe, - so leftover writers get `SIGPIPE`/`EPIPE`. This is deliberate: the process - is reported as finished, and nothing reads what comes later. The file is - closed before the exit is published. + so leftover writers get `SIGPIPE`/`EPIPE` on Unix and a broken pipe on + Windows. This is deliberate: the process is reported as finished, and + nothing reads what comes later. The file is closed before the exit is + published. - `Exited` and the lifecycle carry `signal`, the signal's name such as - `"SIGTERM"`, when the process was killed by one; `exit_code` is then null. - `signal` is left out when there is none. -- An exited pipe process stays listed, as a zombie, until `terminate`. The - zombie keeps its PID and process group ID reserved, so killing the group can - never reach an unrelated process. `terminate` sends SIGKILL to the group and - to the child, reaps it and removes the process; it never waits on a - descendant that left the group and still holds the pipe. The daemon never - deletes output files. + `"SIGTERM"`, when the daemon terminated the process; `exit_code` is then + null. `signal` is left out when there is none. +- On Unix an exited pipe process stays listed, as a zombie, until `terminate`. + The zombie keeps its PID and process group ID reserved, so killing the group + can never reach an unrelated process. On Windows the process object and job + handle stay open until `terminate`, so the PID is not reused either. + `terminate` kills the whole tree (SIGKILL on Unix; `TerminateJobObject`, or + the main process only when no job could be used, on Windows), reaps it and + removes the process; it never waits on a descendant that left the process + group or job and still holds the pipe. The daemon never deletes output + files. ### Line count @@ -205,11 +219,20 @@ only; left out for PTYs), `output_error` (left out when there is none) and `exited_at`. PTY subscriptions send the same response. `signal { id, signal }` (pipe only; `"SIGTERM"`, `"SIGINT"` or `"SIGKILL"`) -signals the process group and the child, and returns `Ok`. Unlike -`terminate`, it keeps the process: `wait` then gets a normal `Exited` with -`signal`. Signalling an exited process is allowed and reaches whatever is left -of its group (an empty group is not an error); after `terminate` the process -is gone and `signal` fails. +signals the process tree and returns `Ok`. On Unix the signal goes to the +process group and the child, as a real signal. **On Windows every signal is a +hard kill** of the job (or of the main process when no job could be used) with +`TerminateJobObject`/`TerminateProcess`: there is no console and no console +control event, so SIGTERM and SIGINT cannot be caught or trapped, and the exit +reports the signal that was sent, not a platform exit code. Unlike +`terminate`, `signal` keeps the process: `wait` then gets a normal `Exited` +with `signal`. Signalling an exited process is allowed and reaches whatever is +left of its group or job (an empty group is not an error); after `terminate` +the process is gone and `signal` fails. If an outer job refuses the child +(`ERROR_ACCESS_DENIED`, for example a job with UI restrictions), the daemon +runs the command anyway without a job, logs a warning once, and kills only the +main process with `TerminateProcess`; descendants then survive a signal or +terminate. `metadata` on `create_pipe` is an opaque JSON object the daemon stores and returns as `pipe.metadata` without interpreting it. `set_metadata { id, diff --git a/packages/process-daemon/src/protocol.rs b/packages/process-daemon/src/protocol.rs index 1624fdaa5ce..6dc841aea71 100644 --- a/packages/process-daemon/src/protocol.rs +++ b/packages/process-daemon/src/protocol.rs @@ -52,8 +52,7 @@ pub enum Request { cols: u16, rows: u16, }, - /// Unix only. The child gets exactly `env`; `cwd` and `output_path` must - /// be absolute. + /// The child gets exactly `env`; `cwd` and `output_path` must be absolute. CreatePipe { program: String, args: Vec, diff --git a/packages/process-daemon/src/service.rs b/packages/process-daemon/src/service.rs index 8b07c1cb895..be54f6f84fe 100644 --- a/packages/process-daemon/src/service.rs +++ b/packages/process-daemon/src/service.rs @@ -17,7 +17,6 @@ use serde::{Deserialize, Serialize}; use crate::ghostty::{Format, Terminal, TerminalOptions}; use crate::protocol::AttachmentRole; -#[cfg(unix)] mod pipe; #[cfg(windows)] mod windows; @@ -311,7 +310,7 @@ impl TerminalService { self.insert(|id, order| TerminalHandle::spawn_pty(id, order, request, replay_capacity)) } - /// Starts a pipe process. Unix only. + /// Starts a pipe process. pub fn create_pipe(&self, request: CreatePipe) -> Result { // Everything is checked before the output file is opened, so a // rejected request never creates or truncates it. @@ -323,10 +322,7 @@ impl TerminalService { bail!("output_path must be absolute"); } validate_metadata(&request.metadata)?; - #[cfg(unix)] - return self.insert(|id, order| TerminalHandle::spawn_pipe(id, order, request)); - #[cfg(not(unix))] - bail!("pipe processes are not supported on this platform"); + self.insert(|id, order| TerminalHandle::spawn_pipe(id, order, request)) } fn insert( @@ -399,18 +395,11 @@ impl TerminalService { /// Signals a pipe process and its process group. The process stays /// listed; signalling one that already exited is allowed. pub fn signal(&self, id: TerminalId, signal: ProcessSignal) -> Result<()> { - let process = self.pipe(id)?; - #[cfg(unix)] - return process + self.pipe(id)? .process .as_ref() .ok_or_else(|| anyhow!("process {id} cannot be signalled"))? - .signal(signal); - #[cfg(not(unix))] - { - let _ = (process, signal); - bail!("pipe processes are not supported on this platform"); - } + .signal(signal) } /// Replaces a pipe process's metadata. @@ -644,7 +633,6 @@ struct TerminalHandle { /// Runs after the joins; reaps a pipe process kept as a zombie until now. reap: Mutex>>, /// Pipe only: the child, for `signal`. - #[cfg(unix)] process: Option>, info: Arc>, joins: Mutex>>>, @@ -789,7 +777,7 @@ impl TerminalHandle { ActorMaster { pty: Some(pair.master), #[cfg(windows)] - close_tx, + close_tx: Some(close_tx), }, writer_tx, &info, @@ -880,7 +868,6 @@ impl TerminalHandle { killer: Mutex::new(killer), cancel: Mutex::new(None), reap: Mutex::new(None), - #[cfg(unix)] process: None, info, joins: Mutex::new(Some(vec![ @@ -1017,10 +1004,8 @@ enum ActorMessage { WriterFailed(String), ChildExited(Result, String>), /// A pipe process ended by a signal; the value is the signal's name. - #[cfg(unix)] ChildKilled(String), /// Writing the `output_path` file failed; it stops at this point. - #[cfg(unix)] OutputFileFailed(String), #[cfg(not(windows))] RefreshForegroundProcess { @@ -1111,10 +1096,19 @@ enum WriterMessage { struct ActorMaster { pty: Option>, #[cfg(windows)] - close_tx: Sender>, + close_tx: Option>>, } impl ActorMaster { + /// A master for a process with no terminal. + fn pipe() -> Self { + Self { + pty: None, + #[cfg(windows)] + close_tx: None, + } + } + fn get(&self) -> Result<&(dyn MasterPty + Send)> { self.pty .as_deref() @@ -1123,15 +1117,20 @@ impl ActorMaster { #[cfg(windows)] fn close(&mut self) -> Result<()> { - if let Some(master) = self.pty.take() { - // A capacity-one channel receives this unique master exactly once; - // handoff cannot wait on ClosePseudoConsole or PTY output drainage. - if let Err(error) = self.close_tx.send(master) { - // Keep ownership on failure. The actor stops receiving before - // dropping this fallback master, so its reader can still drain. - self.pty = Some(error.0); - bail!("terminal close worker stopped"); - } + let Some(master) = self.pty.take() else { + return Ok(()); + }; + let Some(close_tx) = &self.close_tx else { + // Only PTYs have a close worker; a pipe process has no master. + return Ok(()); + }; + // A capacity-one channel receives this unique master exactly once; + // handoff cannot wait on ClosePseudoConsole or PTY output drainage. + if let Err(error) = close_tx.send(master) { + // Keep ownership on failure. The actor stops receiving before + // dropping this fallback master, so its reader can still drain. + self.pty = Some(error.0); + bail!("terminal close worker stopped"); } Ok(()) } @@ -1544,7 +1543,6 @@ fn run_actor(config: ActorConfig) -> Result<()> { } None => waiters.push(reply), }, - #[cfg(unix)] Ok(ActorMessage::OutputFileFailed(message)) => { if let Ok(mut value) = info.write() && let Some(pipe) = value.pipe.as_mut() @@ -1552,7 +1550,6 @@ fn run_actor(config: ActorConfig) -> Result<()> { pipe.output_error = Some(message); } } - #[cfg(unix)] Ok(ActorMessage::ChildKilled(signal)) => { child_exit = Some(Ok((None, Some(signal)))); } @@ -2250,7 +2247,7 @@ mod tests { .is_pty() .then(|| Box::new(TestMaster) as Box), #[cfg(windows)] - close_tx, + close_tx: mode.is_pty().then_some(close_tx), }, messages, writes, @@ -2274,7 +2271,6 @@ mod tests { killer: Mutex::new(Box::new(TestKiller)), cancel: Mutex::new(None), reap: Mutex::new(None), - #[cfg(unix)] process: None, info, joins: Mutex::new(Some(vec![ @@ -2428,17 +2424,13 @@ mod tests { for message in [ ActorMessage::Wait { reply }, ActorMessage::ChildExited(Ok(Some(0))), - #[cfg(unix)] ActorMessage::OutputFileFailed("disk full".into()), ActorMessage::ReaderFailed("boom".into()), ] { actor.actor_tx.send(message).unwrap(); } let exit = wait.recv_timeout(Duration::from_secs(5)).unwrap(); - #[cfg(unix)] assert_eq!(exit.output_error.as_deref(), Some("disk full")); - #[cfg(not(unix))] - assert!(exit.output_error.is_some()); actor.shutdown().unwrap(); } @@ -2507,7 +2499,7 @@ mod tests { drop(close_rx); let mut master = ActorMaster { pty: Some(Box::new(TestMaster)), - close_tx, + close_tx: Some(close_tx), }; assert!(master.close().is_err()); assert!(master.get().is_ok()); diff --git a/packages/process-daemon/src/service/pipe.rs b/packages/process-daemon/src/service/pipe.rs index 09672926b9f..b630bf2d44f 100644 --- a/packages/process-daemon/src/service/pipe.rs +++ b/packages/process-daemon/src/service/pipe.rs @@ -1,343 +1,258 @@ -//! Non-PTY ("pipe") processes on Unix. +//! Non-PTY ("pipe") processes: a command with no terminal, stdin closed and +//! stdout and stderr sharing one pipe so their bytes keep their order. //! -//! The child runs in its own process group with stdin on `/dev/null` and -//! stdout and stderr sharing one pipe, so their bytes keep their order. Three -//! choices keep the daemon safe from what the child leaves behind: +//! The platform modules own the child, its process tree and the output pipe: +//! Unix keeps a zombie so the PID and process group stay reserved, and Windows +//! keeps the process and its kill-on-close job. Everything the two must do the +//! same way lives here: //! -//! - The waiter observes the exit with `WNOWAIT` and leaves the child as a -//! zombie until shutdown. While the zombie exists, its PID and process group -//! ID cannot be reused, so killing the group can never hit an unrelated -//! process, even long after the exit. +//! - The reader writes every byte to the output file before handing it to the +//! actor, which keeps no replay buffer: the file is the only copy of the +//! output. //! - A descendant can keep the pipe open after the child exits (`cmd &`, build //! servers). The reader stops [`DRAIN_AFTER_EXIT`] after the exit instead of //! waiting for an EOF that may never come. -//! - The reader writes every byte to the output file before handing it to the -//! actor, which keeps no replay buffer: the file is the only copy of the -//! output. Leftover writers get `SIGPIPE` once the reader stops. -//! - The reader polls a cancel pipe too, so shutdown never waits on a pipe that -//! a descendant which left the process group still holds open. +//! - The file is closed before `ReaderEof` or `ReaderFailed`, so whoever +//! observes the exit can read everything the file will ever get. +//! - A process the daemon terminated is reported as the signal the daemon sent +//! (`SIGTERM`, `SIGINT`, `SIGKILL`) with no exit code, not as the platform's +//! hard-kill status, so clients format it like a Unix signal death. -use std::fs::{File, OpenOptions}; -use std::io::{self, PipeReader, PipeWriter, Read, Write}; -use std::os::fd::AsFd; -use std::os::unix::fs::OpenOptionsExt; -use std::os::unix::process::CommandExt; -use std::process::{Command, Stdio}; -use std::sync::{Arc, Mutex, PoisonError, RwLock}; -use std::thread; -use std::time::{Duration, Instant}; +use std::fs::File; +use std::io::{self, Write}; +use std::sync::{Arc, Mutex, RwLock}; +use std::thread::JoinHandle; +use std::time::Duration; -use anyhow::{Context, Result, bail}; +use anyhow::{Context, Result}; use crossbeam_channel::{Sender, bounded}; -use nix::errno::Errno; -use nix::poll::{PollFd, PollFlags, PollTimeout, poll}; use portable_pty::ChildKiller; -use super::{ - ActorMaster, ActorMessage, CreatePipe, PipeInfo, ProcessInfo, ProcessMode, ProcessSignal, +use crate::service::{ + ActorMaster, ActorMessage, ActorStart, CreatePipe, PipeInfo, ProcessInfo, ProcessMode, TerminalHandle, TerminalId, new_info, start_actor, }; +#[cfg(unix)] +mod unix; +#[cfg(windows)] +mod windows; + +#[cfg(unix)] +pub(in crate::service) use unix::Process; +#[cfg(windows)] +pub(in crate::service) use windows::Process; + /// How long output is still read after the child exits. const DRAIN_AFTER_EXIT: Duration = Duration::from_secs(1); -/// Serializes pipe creation against `spawn`. macOS has no `pipe2`, so std -/// creates a pipe and then marks it close-on-exec; a fork in between would -/// leak the pipe into another child, where it could hold output or cancel -/// pipes open. Pipes are created under the read lock, children spawned under -/// the write lock. -static SPAWN_LOCK: RwLock<()> = RwLock::new(()); - -struct Pipes { - output: PipeReader, - output_writer: PipeWriter, - stderr: PipeWriter, - cancel: PipeReader, - cancel_writer: PipeWriter, - exited: PipeReader, - exited_writer: PipeWriter, +/// The output file and the actor queue, in the order every byte goes through +/// them: the file first, so it always holds at least what subscribers have +/// seen, even when the actor is backpressured. +struct OutputSink { + file: Option, + actor: Sender, } -impl Pipes { - fn new() -> Result { - let _guard = SPAWN_LOCK.read().unwrap_or_else(PoisonError::into_inner); - let (output, output_writer) = io::pipe().context("failed to create output pipe")?; - let stderr = output_writer - .try_clone() - .context("failed to share output pipe")?; - let (cancel, cancel_writer) = io::pipe().context("failed to create cancel pipe")?; - let (exited, exited_writer) = io::pipe().context("failed to create exit pipe")?; - Ok(Self { - output, - output_writer, - stderr, - cancel, - cancel_writer, - exited, - exited_writer, - }) +impl OutputSink { + fn new(file: File, actor: Sender) -> Self { + Self { + file: Some(file), + actor, + } + } + + /// Returns false once the actor is gone. + fn write(&mut self, bytes: &[u8]) -> bool { + let mut newlines = 0; + if let Some(file) = &mut self.file + && let Err(error) = write_counting(file, bytes, &mut newlines) + { + // Ordered before this chunk's output, so the error is visible no + // later than the bytes the file lacks. + self.file = None; + if self + .actor + .send(ActorMessage::OutputFileFailed(format!( + "failed to write output file: {error}" + ))) + .is_err() + { + return false; + } + } + self.actor + .send(ActorMessage::Output { + bytes: bytes.to_vec(), + newlines, + }) + .is_ok() + } + + /// Closes the file before anyone can observe the exit. + fn eof(mut self) { + self.file = None; + let _ = self.actor.send(ActorMessage::ReaderEof); + } + + /// Closes the file before anyone can observe the failure. + fn failed(mut self, message: String) { + self.file = None; + let _ = self.actor.send(ActorMessage::ReaderFailed(message)); } } -impl TerminalHandle { - pub(super) fn spawn_pipe(id: TerminalId, order: u64, request: CreatePipe) -> Result { - let Pipes { - output, - output_writer, - stderr, - cancel, - cancel_writer, - exited, - exited_writer, - } = Pipes::new()?; - // Before spawning, so a bad path fails the request with nothing to clean up. - // The reader is the file's only writer, so it needs no append mode. - let sink = OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - // Never write through a symlink planted at the final component. - .custom_flags(libc::O_NOFOLLOW) - .open(&request.output_path) - .with_context(|| { - format!( - "failed to open output file {}", - request.output_path.display() - ) - })?; - let mut command = Command::new(&request.program); - command - .args(&request.args) - .current_dir(&request.cwd) - .env_clear() - .envs(&request.env) - .stdin(Stdio::null()) - .stdout(output_writer) - .stderr(stderr) - .process_group(0); - // SAFETY: only async-signal-safe calls run between fork and exec. - unsafe { - command.pre_exec(reset_signals); - } - let child = { - let _guard = SPAWN_LOCK.write().unwrap_or_else(PoisonError::into_inner); - command - .spawn() - .with_context(|| format!("failed to spawn {}", request.program))? - }; - // Close the daemon's copies of the write end, or EOF never arrives. - drop(command); - let process = Arc::new(Process { - pid: child.id(), - reaped: Mutex::new(false), - }); - // Dropping `Child` neither waits nor kills; `Process` reaps it. - drop(child); +/// How a pipe child ended. +enum Exit { + Code(u32), + /// The daemon terminated the process; the value is the signal it sent. + Signal(String), +} - let info = Arc::new(RwLock::new(ProcessInfo { - pipe: Some(PipeInfo { - output_path: request.output_path.clone(), - output_lines: 0, - output_error: None, - metadata: request.metadata, - }), - ..new_info( - id, - ProcessMode::Pipe, - Some(process.pid), - &request.program, - &request.args, - &request.cwd, - &request.group_id, - ) - })); - // Nothing is ever written to a pipe process; a disconnected queue - // turns any stray write into an error. - let (writes, _) = bounded(1); - let actor = match start_actor( +/// The one place a pipe exit becomes an actor message, so both platforms +/// report exits and signals the same way. +fn exit_message(result: io::Result) -> ActorMessage { + match result { + Ok(Exit::Code(code)) => ActorMessage::ChildExited(Ok(Some(code))), + Ok(Exit::Signal(name)) => ActorMessage::ChildKilled(name), + Err(error) => ActorMessage::ChildExited(Err(error.to_string())), + } +} + +/// Info for a pipe process: exactly one `pipe` group, never a `pty`. +fn new_pipe_info(id: TerminalId, pid: u32, request: CreatePipe) -> ProcessInfo { + let CreatePipe { + program, + args, + cwd, + env: _, + group_id, + output_path, + metadata, + } = request; + ProcessInfo { + pipe: Some(PipeInfo { + output_path, + output_lines: 0, + output_error: None, + metadata, + }), + ..new_info( id, ProcessMode::Pipe, - ActorMaster { pty: None }, - writes, - &info, - // The output file is the only buffer. - 0, - (0, 0), - ) { - Ok(actor) => actor, - Err(error) => { - process.kill(); - process.reap(); - return Err(error); - } - }; - - let mut joins = vec![actor.thread]; - let reader_actor_tx = actor.actor_tx.clone(); - let started = thread::Builder::new() - .name(format!("opencode-pty-reader-{id}")) - .spawn(move || read_output(output, sink, cancel, exited, reader_actor_tx)) - .context("failed to spawn terminal reader") - .and_then(|reader| { - joins.push(reader); - let wait_actor_tx = actor.actor_tx.clone(); - let pid = process.pid; - // Spawned last: the waiter must be joined before `reap`. - thread::Builder::new() - .name(format!("opencode-pty-wait-{id}")) - .spawn(move || { - let message = match wait_exit(pid) { - Ok(Exit::Code(code)) => ActorMessage::ChildExited(Ok(Some(code))), - Ok(Exit::Signal(name)) => ActorMessage::ChildKilled(name), - Err(error) => ActorMessage::ChildExited(Err(error.to_string())), - }; - let _ = wait_actor_tx.send(message); - // Starts the reader's drain deadline. - drop(exited_writer); - }) - .context("failed to spawn terminal child waiter") - }); - match started { - Ok(waiter) => joins.push(waiter), - Err(error) => { - // Stop what did start, kill the child before any join can wait - // on it, and reap only after every thread is gone. - drop(actor.shutdown_tx); - drop(cancel_writer); - process.kill(); - for join in joins { - let _ = join.join(); - } - process.reap(); - return Err(error); - } - } - - let reap = Arc::clone(&process); - Ok(Self { - order, - mode: ProcessMode::Pipe, - actor_tx: actor.actor_tx, - actor_done: actor.actor_done, - shutdown_tx: Mutex::new(Some(actor.shutdown_tx)), - killer: Mutex::new(Box::new(GroupKiller(Arc::clone(&process)))), - cancel: Mutex::new(Some(Box::new(cancel_writer))), - reap: Mutex::new(Some(Box::new(move || reap.reap()))), - process: Some(process), - info, - joins: Mutex::new(Some(joins)), - }) + Some(pid), + &program, + &args, + &cwd, + &group_id, + ) } } -/// Runs in the child between fork and exec. The daemon blocks SIGTERM and -/// SIGINT on all its threads, and std keeps the signal mask across exec, so -/// clear it, and give every signal portable-pty also resets its default action. -fn reset_signals() -> io::Result<()> { - // SAFETY: sigemptyset, sigprocmask and signal are async-signal-safe. - unsafe { - // Dispositions first, so no inherited handler can run once unmasked. - for signal in [ - libc::SIGCHLD, - libc::SIGHUP, - libc::SIGINT, - libc::SIGQUIT, - libc::SIGTERM, - libc::SIGALRM, - ] { - libc::signal(signal, libc::SIG_DFL); - } - let mut empty: libc::sigset_t = std::mem::zeroed(); - if libc::sigemptyset(&mut empty) != 0 - || libc::sigprocmask(libc::SIG_SETMASK, &empty, std::ptr::null_mut()) != 0 - { - return Err(io::Error::last_os_error()); - } - } - Ok(()) +/// The platform reader and waiter, each spawning its named worker thread. +struct Threads { + reader: Box io::Result>>, + waiter: Box) -> io::Result>>, } -/// A pipe child that stays a zombie after it exits, until `reap`. +/// Registers a spawned pipe process with its actor, then runs the platform's +/// reader and waiter. Both are started in that order, and the waiter last, so +/// it is joined before the process is reaped. +fn start_pipe( + id: TerminalId, + order: u64, + request: CreatePipe, + process: Arc, + sink: File, + cancel: Box, + threads: Threads, +) -> Result { + let info = Arc::new(RwLock::new(new_pipe_info(id, process.pid(), request))); + // Nothing is ever written to a pipe process; a disconnected queue + // turns any stray write into an error. + let (writes, _) = bounded(1); + let actor = match start_actor( + id, + ProcessMode::Pipe, + ActorMaster::pipe(), + writes, + &info, + // The output file is the only buffer. + 0, + (0, 0), + ) { + Ok(actor) => actor, + Err(error) => { + process.kill(); + process.reap(); + return Err(error); + } + }; + let ActorStart { + actor_tx, + actor_done, + shutdown_tx, + shutdown: _, + thread, + } = actor; + let mut joins = vec![thread]; + let reader = (threads.reader)(OutputSink::new(sink, actor_tx.clone())); + let reader = match reader { + Ok(reader) => reader, + Err(error) => { + stop(shutdown_tx, cancel, &process, joins); + return Err(error).context("failed to spawn terminal reader"); + } + }; + joins.push(reader); + let waiter = (threads.waiter)(actor_tx.clone()); + let waiter = match waiter { + Ok(waiter) => waiter, + Err(error) => { + stop(shutdown_tx, cancel, &process, joins); + return Err(error).context("failed to spawn terminal child waiter"); + } + }; + joins.push(waiter); + let reap = Arc::clone(&process); + Ok(TerminalHandle { + order, + mode: ProcessMode::Pipe, + actor_tx, + actor_done, + shutdown_tx: Mutex::new(Some(shutdown_tx)), + killer: Mutex::new(Box::new(ProcessKiller(Arc::clone(&process)))), + cancel: Mutex::new(Some(cancel)), + reap: Mutex::new(Some(Box::new(move || reap.reap()))), + process: Some(process), + info, + joins: Mutex::new(Some(joins)), + }) +} + +/// Stops a partially started pipe process: kill the child before any join can +/// wait on it, and reap only after every thread is gone. +fn stop( + shutdown_tx: Sender<()>, + cancel: Box, + process: &Process, + joins: Vec>, +) { + drop(shutdown_tx); + drop(cancel); + process.kill(); + for join in joins { + let _ = join.join(); + } + process.reap(); +} + +/// `ChildKiller` for service shutdown, which uses the same kill path as +/// `terminate`. #[derive(Debug)] -pub(super) struct Process { - pid: u32, - reaped: Mutex, -} +struct ProcessKiller(Arc); -impl Process { - /// Kills the whole process group. Never signals after `reap`, when the - /// group ID may belong to someone else. - fn kill(&self) { - let reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner); - if !*reaped && let Ok(pid) = i32::try_from(self.pid) { - // SAFETY: the unreaped child keeps its PID and group ID reserved. - // Signal the child directly too, in case it moved to another group. - unsafe { - libc::killpg(pid, libc::SIGKILL); - libc::kill(pid, libc::SIGKILL); - } - } - } - - /// Sends `signal` to the process group and the child. Allowed after the - /// exit, to reach what is left of the group; refused after `reap`. - pub(super) fn signal(&self, signal: ProcessSignal) -> Result<()> { - let signal = match signal { - ProcessSignal::Term => libc::SIGTERM, - ProcessSignal::Int => libc::SIGINT, - ProcessSignal::Kill => libc::SIGKILL, - }; - let reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner); - if *reaped { - bail!("process has been reaped"); - } - let pid = i32::try_from(self.pid).context("invalid pid")?; - // SAFETY: the unreaped child keeps its PID and group ID reserved. - check_signal(unsafe { libc::killpg(pid, signal) }, self.pid)?; - // Signal the child directly too, in case it moved to another group. - // SAFETY: as above. - check_signal(unsafe { libc::kill(pid, signal) }, self.pid) - } - - /// Call only after the waiter has returned: kill the group first, so the - /// child has exited and this does not block. - fn reap(&self) { - let mut reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner); - if *reaped { - return; - } - let Ok(pid) = i32::try_from(self.pid) else { - return; - }; - loop { - // SAFETY: waits only for this daemon's own child. - let result = unsafe { libc::waitpid(pid, std::ptr::null_mut(), 0) }; - if result != -1 || Errno::last() != Errno::EINTR { - break; - } - } - *reaped = true; - } -} - -/// Reads errno right after a `kill`/`killpg` result. A zombie group with no -/// live members gives ESRCH on macOS, which is not an error here. -/// Signalling an exited child whose group is empty fails with `ESRCH`, or -/// with `EPERM` on macOS (the zombie is still there); neither is an error. -fn check_signal(result: libc::c_int, pid: u32) -> Result<()> { - if result == -1 { - let errno = Errno::last(); - if errno != Errno::ESRCH && !(errno == Errno::EPERM && has_exited(pid)) { - return Err(io::Error::from(errno)).context("failed to signal process"); - } - } - Ok(()) -} - -#[derive(Debug)] -struct GroupKiller(Arc); - -impl ChildKiller for GroupKiller { +impl ChildKiller for ProcessKiller { fn kill(&mut self) -> io::Result<()> { self.0.kill(); Ok(()) @@ -348,218 +263,6 @@ impl ChildKiller for GroupKiller { } } -enum Exit { - Code(u32), - Signal(String), -} - -/// Whether the child has exited, without reaping it. -fn has_exited(pid: u32) -> bool { - // SAFETY: siginfo_t is plain data, and all-zero is a valid value. - let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() }; - // SAFETY: `info` is a valid out pointer; WNOWAIT leaves the zombie. - let result = unsafe { - libc::waitid( - libc::P_PID, - pid as libc::id_t, - &mut info, - libc::WEXITED | libc::WNOWAIT | libc::WNOHANG, - ) - }; - // SAFETY: waitid filled `info`; a zero PID means the child is still running. - result == 0 && unsafe { info.si_pid() } != 0 -} - -/// Waits for the child to exit without reaping it. -fn wait_exit(pid: u32) -> io::Result { - loop { - // SAFETY: siginfo_t is plain data, and all-zero is a valid value. - let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() }; - // SAFETY: `info` is a valid out pointer; WNOWAIT leaves the zombie. - let result = unsafe { - libc::waitid( - libc::P_PID, - pid as libc::id_t, - &mut info, - libc::WEXITED | libc::WNOWAIT, - ) - }; - if result == -1 { - let error = io::Error::last_os_error(); - if error.kind() == io::ErrorKind::Interrupted { - continue; - } - return Err(error); - } - // SAFETY: waitid filled `info` for a SIGCHLD event. - let status = unsafe { info.si_status() }; - return Ok(match info.si_code { - libc::CLD_EXITED => Exit::Code(status as u32), - _ => Exit::Signal(signal_name(status)), - }); - } -} - -fn signal_name(signal: i32) -> String { - let name = match signal { - libc::SIGHUP => "SIGHUP", - libc::SIGINT => "SIGINT", - libc::SIGQUIT => "SIGQUIT", - libc::SIGILL => "SIGILL", - libc::SIGTRAP => "SIGTRAP", - libc::SIGABRT => "SIGABRT", - libc::SIGBUS => "SIGBUS", - libc::SIGFPE => "SIGFPE", - libc::SIGKILL => "SIGKILL", - libc::SIGUSR1 => "SIGUSR1", - libc::SIGSEGV => "SIGSEGV", - libc::SIGUSR2 => "SIGUSR2", - libc::SIGPIPE => "SIGPIPE", - libc::SIGALRM => "SIGALRM", - libc::SIGTERM => "SIGTERM", - libc::SIGXCPU => "SIGXCPU", - libc::SIGXFSZ => "SIGXFSZ", - libc::SIGVTALRM => "SIGVTALRM", - libc::SIGPROF => "SIGPROF", - libc::SIGSYS => "SIGSYS", - _ => return format!("SIG{signal}"), - }; - name.to_string() -} - -/// Forwards output until EOF, until [`DRAIN_AFTER_EXIT`] after `exited` -/// closes, or until `cancel` closes. Once every writer has closed the output -/// pipe, what is left is finite, so it is read to the end regardless of the -/// deadline. -fn read_output( - output: PipeReader, - sink: File, - cancel: PipeReader, - exited: PipeReader, - actor: Sender, -) { - let mut output = output; - // Dropped after a write error: the rest is forwarded but not counted. - let mut sink = Some(sink); - let mut buffer = [0_u8; 8192]; - let mut deadline = None::; - let mut writers_closed = false; - loop { - let timeout = match deadline { - Some(deadline) if !writers_closed => { - let left = deadline.saturating_duration_since(Instant::now()); - if left.is_zero() { - // The reader may have been stalled past the deadline; take - // one last look before giving up on a held-open pipe. - let mut fds = [PollFd::new(output.as_fd(), PollFlags::POLLIN)]; - let hung_up = poll(&mut fds, PollTimeout::ZERO).is_ok_and(|count| count > 0) - && fds[0] - .revents() - .is_some_and(|events| events.contains(PollFlags::POLLHUP)); - if !hung_up { - // Close the file before anyone can observe the exit. - drop(sink); - let _ = actor.send(ActorMessage::ReaderEof); - return; - } - writers_closed = true; - PollTimeout::NONE - } else { - // Round up: a sub-millisecond remainder must not become a busy 0. - let millis = left.as_nanos().div_ceil(1_000_000).min(i32::MAX as u128); - PollTimeout::try_from(millis).unwrap_or(PollTimeout::MAX) - } - } - _ => PollTimeout::NONE, - }; - let mut fds = [ - PollFd::new(output.as_fd(), PollFlags::POLLIN), - PollFd::new(cancel.as_fd(), PollFlags::POLLIN), - PollFd::new(exited.as_fd(), PollFlags::POLLIN), - ]; - // Once the exit is seen, stop polling its closed pipe. - let count = if deadline.is_some() { 2 } else { 3 }; - match poll(&mut fds[..count], timeout) { - Err(Errno::EINTR) | Ok(0) => continue, - Err(error) => { - // Close the file before anyone can observe the exit. - drop(sink); - let _ = actor.send(ActorMessage::ReaderFailed(error.to_string())); - return; - } - Ok(_) => {} - } - let ready = |fd: &PollFd| { - fd.revents().is_none_or(|events| { - events.intersects( - PollFlags::POLLIN - | PollFlags::POLLHUP - | PollFlags::POLLERR - | PollFlags::POLLNVAL, - ) - }) - }; - if ready(&fds[1]) { - return; - } - if count == 3 && ready(&fds[2]) { - deadline = Some(Instant::now() + DRAIN_AFTER_EXIT); - } - if fds[0] - .revents() - .is_some_and(|events| events.contains(PollFlags::POLLHUP)) - { - writers_closed = true; - } - if ready(&fds[0]) { - match output.read(&mut buffer) { - Ok(0) => { - // Close the file before anyone can observe the exit. - drop(sink); - let _ = actor.send(ActorMessage::ReaderEof); - return; - } - Ok(length) => { - // The file first, so it always holds at least what - // subscribers have seen, even if the actor is backpressured. - let mut newlines = 0; - if let Some(file) = &mut sink - && let Err(error) = - write_counting(&mut *file, &buffer[..length], &mut newlines) - { - // Ordered before this chunk's output, so the error is - // visible no later than the bytes the file lacks. - sink = None; - if actor - .send(ActorMessage::OutputFileFailed(format!( - "failed to write output file: {error}" - ))) - .is_err() - { - return; - } - } - if actor - .send(ActorMessage::Output { - bytes: buffer[..length].to_vec(), - newlines, - }) - .is_err() - { - return; - } - } - Err(error) if error.kind() == io::ErrorKind::Interrupted => {} - Err(error) => { - drop(sink); - let _ = actor.send(ActorMessage::ReaderFailed(error.to_string())); - return; - } - } - } - } -} - /// Writes all of `bytes`, adding to `newlines` the `\n` bytes that reached /// the file, including those of a partial write before an error. fn write_counting(mut file: impl Write, bytes: &[u8], newlines: &mut u64) -> io::Result<()> { @@ -595,7 +298,7 @@ mod tests { fn write(&mut self, bytes: &[u8]) -> io::Result { let room = self.accept - self.written.len(); if room == 0 { - return Err(io::Error::from_raw_os_error(libc::ENOSPC)); + return Err(io::Error::new(io::ErrorKind::StorageFull, "no space")); } let length = bytes.len().min(room).min(3); self.written.extend_from_slice(&bytes[..length]); @@ -615,7 +318,7 @@ mod tests { }; let mut newlines = 0; let error = write_counting(&mut writer, b"a\nb\nc\nd\ne\n", &mut newlines).unwrap_err(); - assert_eq!(error.raw_os_error(), Some(libc::ENOSPC)); + assert_eq!(error.kind(), io::ErrorKind::StorageFull); assert_eq!(writer.written, b"a\nb\nc\nd"); assert_eq!(newlines, 3); diff --git a/packages/process-daemon/src/service/pipe/unix.rs b/packages/process-daemon/src/service/pipe/unix.rs new file mode 100644 index 00000000000..234be40fd33 --- /dev/null +++ b/packages/process-daemon/src/service/pipe/unix.rs @@ -0,0 +1,435 @@ +//! Pipe processes on Unix. +//! +//! The child runs in its own process group with stdin on `/dev/null`. Three +//! choices keep the daemon safe from what the child leaves behind: +//! +//! - The waiter observes the exit with `WNOWAIT` and leaves the child as a +//! zombie until shutdown. While the zombie exists, its PID and process group +//! ID cannot be reused, so killing the group can never hit an unrelated +//! process, even long after the exit. +//! - The reader polls a cancel pipe as well as the output pipe, so shutdown +//! never waits on a pipe that a descendant which left the process group +//! still holds open. +//! - A cancelled reader closes the pipe, so leftover writers get `SIGPIPE` +//! once the drain deadline passes. + +use std::fs::OpenOptions; +use std::io::{self, PipeReader, PipeWriter, Read}; +use std::os::fd::AsFd; +use std::os::unix::fs::OpenOptionsExt; +use std::os::unix::process::CommandExt; +use std::process::{Command, Stdio}; +use std::sync::{Arc, Mutex, PoisonError, RwLock}; +use std::thread; +use std::time::Instant; + +use anyhow::{Context, Result, bail}; +use nix::errno::Errno; +use nix::poll::{PollFd, PollFlags, PollTimeout, poll}; + +use super::{DRAIN_AFTER_EXIT, Exit, OutputSink, exit_message, start_pipe}; +use crate::service::{CreatePipe, ProcessSignal, TerminalHandle, TerminalId}; + +/// Serializes pipe creation against `spawn`. macOS has no `pipe2`, so std +/// creates a pipe and then marks it close-on-exec; a fork in between would +/// leak the pipe into another child, where it could hold output or cancel +/// pipes open. Pipes are created under the read lock, children spawned under +/// the write lock. +static SPAWN_LOCK: RwLock<()> = RwLock::new(()); + +struct Pipes { + output: PipeReader, + output_writer: PipeWriter, + stderr: PipeWriter, + cancel: PipeReader, + cancel_writer: PipeWriter, + exited: PipeReader, + exited_writer: PipeWriter, +} + +impl Pipes { + fn new() -> Result { + let _guard = SPAWN_LOCK.read().unwrap_or_else(PoisonError::into_inner); + let (output, output_writer) = io::pipe().context("failed to create output pipe")?; + let stderr = output_writer + .try_clone() + .context("failed to share output pipe")?; + let (cancel, cancel_writer) = io::pipe().context("failed to create cancel pipe")?; + let (exited, exited_writer) = io::pipe().context("failed to create exit pipe")?; + Ok(Self { + output, + output_writer, + stderr, + cancel, + cancel_writer, + exited, + exited_writer, + }) + } +} + +impl TerminalHandle { + pub(in crate::service) fn spawn_pipe( + id: TerminalId, + order: u64, + request: CreatePipe, + ) -> Result { + let Pipes { + output, + output_writer, + stderr, + cancel, + cancel_writer, + exited, + exited_writer, + } = Pipes::new()?; + // Before spawning, so a bad path fails the request with nothing to clean up. + // The reader is the file's only writer, so it needs no append mode. + let sink = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + // Never write through a symlink planted at the final component. + .custom_flags(libc::O_NOFOLLOW) + .open(&request.output_path) + .with_context(|| { + format!( + "failed to open output file {}", + request.output_path.display() + ) + })?; + let mut command = Command::new(&request.program); + command + .args(&request.args) + .current_dir(&request.cwd) + .env_clear() + .envs(&request.env) + .stdin(Stdio::null()) + .stdout(output_writer) + .stderr(stderr) + .process_group(0); + // SAFETY: only async-signal-safe calls run between fork and exec. + unsafe { + command.pre_exec(reset_signals); + } + let child = { + let _guard = SPAWN_LOCK.write().unwrap_or_else(PoisonError::into_inner); + command + .spawn() + .with_context(|| format!("failed to spawn {}", request.program))? + }; + // Close the daemon's copies of the write end, or EOF never arrives. + drop(command); + let process = Arc::new(Process { + pid: child.id(), + reaped: Mutex::new(false), + }); + // Dropping `Child` neither waits nor kills; `Process` reaps it. + drop(child); + + let reader_id = id; + let waiter_id = id; + start_pipe( + id, + order, + request, + Arc::clone(&process), + sink, + Box::new(cancel_writer), + super::Threads { + reader: Box::new(move |sink| { + thread::Builder::new() + .name(format!("opencode-pty-reader-{reader_id}")) + .spawn(move || read_output(output, sink, cancel, exited)) + }), + waiter: Box::new(move |actor| { + let pid = process.pid; + // Spawned last: the waiter must be joined before `reap`. + thread::Builder::new() + .name(format!("opencode-pty-wait-{waiter_id}")) + .spawn(move || { + let _ = actor.send(exit_message(wait_exit(pid))); + // Starts the reader's drain deadline. + drop(exited_writer); + }) + }), + }, + ) + } +} + +/// Runs in the child between fork and exec. The daemon blocks SIGTERM and +/// SIGINT on all its threads, and std keeps the signal mask across exec, so +/// clear it, and give every signal portable-pty also resets its default action. +fn reset_signals() -> io::Result<()> { + // SAFETY: sigemptyset, sigprocmask and signal are async-signal-safe. + unsafe { + // Dispositions first, so no inherited handler can run once unmasked. + for signal in [ + libc::SIGCHLD, + libc::SIGHUP, + libc::SIGINT, + libc::SIGQUIT, + libc::SIGTERM, + libc::SIGALRM, + ] { + libc::signal(signal, libc::SIG_DFL); + } + let mut empty: libc::sigset_t = std::mem::zeroed(); + if libc::sigemptyset(&mut empty) != 0 + || libc::sigprocmask(libc::SIG_SETMASK, &empty, std::ptr::null_mut()) != 0 + { + return Err(io::Error::last_os_error()); + } + } + Ok(()) +} + +/// A pipe child that stays a zombie after it exits, until `reap`. +#[derive(Debug)] +pub(in crate::service) struct Process { + pid: u32, + reaped: Mutex, +} + +impl Process { + pub(super) fn pid(&self) -> u32 { + self.pid + } + + /// Kills the whole process group. Never signals after `reap`, when the + /// group ID may belong to someone else. + pub(super) fn kill(&self) { + let reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner); + if !*reaped && let Ok(pid) = i32::try_from(self.pid) { + // SAFETY: the unreaped child keeps its PID and group ID reserved. + // Signal the child directly too, in case it moved to another group. + unsafe { + libc::killpg(pid, libc::SIGKILL); + libc::kill(pid, libc::SIGKILL); + } + } + } + + /// Sends `signal` to the process group and the child. Allowed after the + /// exit, to reach what is left of the group; refused after `reap`. + pub(in crate::service) fn signal(&self, signal: ProcessSignal) -> Result<()> { + let signal = match signal { + ProcessSignal::Term => libc::SIGTERM, + ProcessSignal::Int => libc::SIGINT, + ProcessSignal::Kill => libc::SIGKILL, + }; + let reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner); + if *reaped { + bail!("process has been reaped"); + } + let pid = i32::try_from(self.pid).context("invalid pid")?; + // SAFETY: the unreaped child keeps its PID and group ID reserved. + check_signal(unsafe { libc::killpg(pid, signal) }, self.pid)?; + // Signal the child directly too, in case it moved to another group. + // SAFETY: as above. + check_signal(unsafe { libc::kill(pid, signal) }, self.pid) + } + + /// Call only after the waiter has returned: kill the group first, so the + /// child has exited and this does not block. + pub(super) fn reap(&self) { + let mut reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner); + if *reaped { + return; + } + let Ok(pid) = i32::try_from(self.pid) else { + return; + }; + loop { + // SAFETY: waits only for this daemon's own child. + let result = unsafe { libc::waitpid(pid, std::ptr::null_mut(), 0) }; + if result != -1 || Errno::last() != Errno::EINTR { + break; + } + } + *reaped = true; + } +} + +/// Reads errno right after a `kill`/`killpg` result. A zombie group with no +/// live members gives ESRCH on macOS, which is not an error here. +/// Signalling an exited child whose group is empty fails with `ESRCH`, or +/// with `EPERM` on macOS (the zombie is still there); neither is an error. +fn check_signal(result: libc::c_int, pid: u32) -> Result<()> { + if result == -1 { + let errno = Errno::last(); + if errno != Errno::ESRCH && !(errno == Errno::EPERM && has_exited(pid)) { + return Err(io::Error::from(errno)).context("failed to signal process"); + } + } + Ok(()) +} + +/// Whether the child has exited, without reaping it. +fn has_exited(pid: u32) -> bool { + // SAFETY: siginfo_t is plain data, and all-zero is a valid value. + let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() }; + // SAFETY: `info` is a valid out pointer; WNOWAIT leaves the zombie. + let result = unsafe { + libc::waitid( + libc::P_PID, + pid as libc::id_t, + &mut info, + libc::WEXITED | libc::WNOWAIT | libc::WNOHANG, + ) + }; + // SAFETY: waitid filled `info`; a zero PID means the child is still running. + result == 0 && unsafe { info.si_pid() } != 0 +} + +/// Waits for the child to exit without reaping it. +fn wait_exit(pid: u32) -> io::Result { + loop { + // SAFETY: siginfo_t is plain data, and all-zero is a valid value. + let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() }; + // SAFETY: `info` is a valid out pointer; WNOWAIT leaves the zombie. + let result = unsafe { + libc::waitid( + libc::P_PID, + pid as libc::id_t, + &mut info, + libc::WEXITED | libc::WNOWAIT, + ) + }; + if result == -1 { + let error = io::Error::last_os_error(); + if error.kind() == io::ErrorKind::Interrupted { + continue; + } + return Err(error); + } + // SAFETY: waitid filled `info` for a SIGCHLD event. + let status = unsafe { info.si_status() }; + return Ok(match info.si_code { + libc::CLD_EXITED => Exit::Code(status as u32), + _ => Exit::Signal(signal_name(status)), + }); + } +} + +fn signal_name(signal: i32) -> String { + let name = match signal { + libc::SIGHUP => "SIGHUP", + libc::SIGINT => "SIGINT", + libc::SIGQUIT => "SIGQUIT", + libc::SIGILL => "SIGILL", + libc::SIGTRAP => "SIGTRAP", + libc::SIGABRT => "SIGABRT", + libc::SIGBUS => "SIGBUS", + libc::SIGFPE => "SIGFPE", + libc::SIGKILL => "SIGKILL", + libc::SIGUSR1 => "SIGUSR1", + libc::SIGSEGV => "SIGSEGV", + libc::SIGUSR2 => "SIGUSR2", + libc::SIGPIPE => "SIGPIPE", + libc::SIGALRM => "SIGALRM", + libc::SIGTERM => "SIGTERM", + libc::SIGXCPU => "SIGXCPU", + libc::SIGXFSZ => "SIGXFSZ", + libc::SIGVTALRM => "SIGVTALRM", + libc::SIGPROF => "SIGPROF", + libc::SIGSYS => "SIGSYS", + _ => return format!("SIG{signal}"), + }; + name.to_string() +} + +/// Forwards output until EOF, until [`DRAIN_AFTER_EXIT`] after `exited` +/// closes, or until `cancel` closes. Once every writer has closed the output +/// pipe, what is left is finite, so it is read to the end regardless of the +/// deadline. +fn read_output(output: PipeReader, sink: OutputSink, cancel: PipeReader, exited: PipeReader) { + let mut output = output; + let mut sink = sink; + let mut buffer = [0_u8; 8192]; + let mut deadline = None::; + let mut writers_closed = false; + loop { + let timeout = match deadline { + Some(deadline) if !writers_closed => { + let left = deadline.saturating_duration_since(Instant::now()); + if left.is_zero() { + // The reader may have been stalled past the deadline; take + // one last look before giving up on a held-open pipe. + let mut fds = [PollFd::new(output.as_fd(), PollFlags::POLLIN)]; + let hung_up = poll(&mut fds, PollTimeout::ZERO).is_ok_and(|count| count > 0) + && fds[0] + .revents() + .is_some_and(|events| events.contains(PollFlags::POLLHUP)); + if !hung_up { + sink.eof(); + return; + } + writers_closed = true; + PollTimeout::NONE + } else { + // Round up: a sub-millisecond remainder must not become a busy 0. + let millis = left.as_nanos().div_ceil(1_000_000).min(i32::MAX as u128); + PollTimeout::try_from(millis).unwrap_or(PollTimeout::MAX) + } + } + _ => PollTimeout::NONE, + }; + let mut fds = [ + PollFd::new(output.as_fd(), PollFlags::POLLIN), + PollFd::new(cancel.as_fd(), PollFlags::POLLIN), + PollFd::new(exited.as_fd(), PollFlags::POLLIN), + ]; + // Once the exit is seen, stop polling its closed pipe. + let count = if deadline.is_some() { 2 } else { 3 }; + match poll(&mut fds[..count], timeout) { + Err(Errno::EINTR) | Ok(0) => continue, + Err(error) => { + sink.failed(error.to_string()); + return; + } + Ok(_) => {} + } + let ready = |fd: &PollFd| { + fd.revents().is_none_or(|events| { + events.intersects( + PollFlags::POLLIN + | PollFlags::POLLHUP + | PollFlags::POLLERR + | PollFlags::POLLNVAL, + ) + }) + }; + if ready(&fds[1]) { + return; + } + if count == 3 && ready(&fds[2]) { + deadline = Some(Instant::now() + DRAIN_AFTER_EXIT); + } + if fds[0] + .revents() + .is_some_and(|events| events.contains(PollFlags::POLLHUP)) + { + writers_closed = true; + } + if ready(&fds[0]) { + match output.read(&mut buffer) { + Ok(0) => { + sink.eof(); + return; + } + Ok(length) => { + if !sink.write(&buffer[..length]) { + return; + } + } + Err(error) if error.kind() == io::ErrorKind::Interrupted => {} + Err(error) => { + sink.failed(error.to_string()); + return; + } + } + } + } +} diff --git a/packages/process-daemon/src/service/pipe/windows.rs b/packages/process-daemon/src/service/pipe/windows.rs new file mode 100644 index 00000000000..aba6c620d86 --- /dev/null +++ b/packages/process-daemon/src/service/pipe/windows.rs @@ -0,0 +1,1201 @@ +//! Pipe processes on Windows. +//! +//! The child has no console (`CREATE_NO_WINDOW`), stdin on `NUL`, and stdout +//! and stderr on one inheritable byte-mode named pipe that the daemon reads +//! with overlapped I/O. The child is created inside a kill-on-close job, so +//! `TerminateJobObject` reaches every descendant and a daemon crash takes the +//! tree with it. No console signals are used: every `signal` is a hard kill of +//! the tree, and the exit reports the signal the daemon sent. + +use std::cmp::Ordering; +use std::collections::HashMap; +use std::ffi::{OsStr, c_void}; +use std::fs::File; +use std::io; +use std::mem::{size_of, size_of_val}; +use std::os::windows::ffi::OsStrExt; +use std::os::windows::io::{AsRawHandle, OwnedHandle}; +use std::path::Path; +use std::ptr::{null, null_mut}; +use std::sync::{Arc, Mutex, PoisonError}; +use std::thread; +use std::time::Instant; + +use anyhow::{Context, Result, bail}; +use windows_sys::Win32::Foundation::{ + DUPLICATE_SAME_ACCESS, DuplicateHandle, ERROR_ACCESS_DENIED, ERROR_BROKEN_PIPE, + ERROR_IO_INCOMPLETE, ERROR_IO_PENDING, ERROR_NO_DATA, ERROR_OPERATION_ABORTED, + ERROR_PIPE_CONNECTED, ERROR_PIPE_NOT_CONNECTED, GENERIC_READ, GENERIC_WRITE, HANDLE, + WAIT_OBJECT_0, WAIT_TIMEOUT, +}; +use windows_sys::Win32::Globalization::{ + CSTR_EQUAL, CSTR_GREATER_THAN, CSTR_LESS_THAN, CompareStringOrdinal, +}; +use windows_sys::Win32::Security::SECURITY_ATTRIBUTES; +use windows_sys::Win32::Storage::FileSystem::{ + BY_HANDLE_FILE_INFORMATION, CreateFileW, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_REPARSE_POINT, + FILE_FLAG_FIRST_PIPE_INSTANCE, FILE_FLAG_OPEN_REPARSE_POINT, FILE_FLAG_OVERLAPPED, + FILE_READ_ATTRIBUTES, FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE, + GetFileInformationByHandle, OPEN_ALWAYS, OPEN_EXISTING, PIPE_ACCESS_INBOUND, ReadFile, + SetEndOfFile, +}; +use windows_sys::Win32::System::IO::{CancelIoEx, GetOverlappedResult, OVERLAPPED}; +use windows_sys::Win32::System::JobObjects::{ + CreateJobObjectW, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, JOBOBJECT_BASIC_LIMIT_INFORMATION, + JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JobObjectExtendedLimitInformation, + SetInformationJobObject, TerminateJobObject, +}; +use windows_sys::Win32::System::Pipes::{ + ConnectNamedPipe, CreateNamedPipeW, PIPE_READMODE_BYTE, PIPE_REJECT_REMOTE_CLIENTS, + PIPE_TYPE_BYTE, PIPE_WAIT, +}; +use windows_sys::Win32::System::Threading::{ + CREATE_NO_WINDOW, CREATE_UNICODE_ENVIRONMENT, CreateProcessW, DeleteProcThreadAttributeList, + EXTENDED_STARTUPINFO_PRESENT, GetCurrentProcess, GetExitCodeProcess, INFINITE, + InitializeProcThreadAttributeList, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, + PROC_THREAD_ATTRIBUTE_JOB_LIST, PROCESS_INFORMATION, ResetEvent, STARTF_USESTDHANDLES, + STARTUPINFOEXW, SetEvent, TerminateProcess, UpdateProcThreadAttribute, WaitForMultipleObjects, + WaitForSingleObject, +}; + +use super::{DRAIN_AFTER_EXIT, Exit, OutputSink, exit_message, start_pipe}; +use crate::service::{CreatePipe, ProcessSignal, TerminalHandle, TerminalId}; +use crate::transport::retained::Retained; +use crate::transport::windows::{event, owned_handle, security::PrivateSecurity}; + +/// Cancel is first so a pipe that is always readable cannot starve it. +const CANCEL_READY: u32 = WAIT_OBJECT_0; +/// The read event, and the process handle after it. +const READ_READY: u32 = WAIT_OBJECT_0 + 1; +const PROCESS_READY: u32 = WAIT_OBJECT_0 + 2; + +/// The pipe server's in-buffer: bytes the child can write before it blocks. +const BUFFER_BYTES: u32 = 64 * 1024; +/// The reader's fixed transfer size, as on Unix. +const READ_BYTES: usize = 8 * 1024; + +impl TerminalHandle { + pub(in crate::service) fn spawn_pipe( + id: TerminalId, + order: u64, + request: CreatePipe, + ) -> Result { + let OutputPipe { server, client } = output_pipe()?; + // Before spawning, so a bad path fails the request with nothing to + // clean up; the reader is the file's only writer. + let sink = open_output(&request.output_path)?; + let job = Job::new(id); + let process = Arc::new(spawn(id, &request, job.as_ref(), client)?); + let cancel = Cancel::new()?; + let reader = Arc::clone(&server); + let reader_process = Arc::clone(&process); + let reader_cancel = cancel.clone(); + start_pipe( + id, + order, + request, + Arc::clone(&process), + sink, + Box::new(cancel), + super::Threads { + reader: Box::new(move |sink| { + thread::Builder::new() + .name(format!("opencode-pty-reader-{id}")) + .spawn(move || read_output(reader, sink, reader_cancel, reader_process)) + }), + waiter: Box::new(move |actor| { + // Spawned last: the waiter must be joined before `reap`. + thread::Builder::new() + .name(format!("opencode-pty-wait-{id}")) + .spawn(move || { + let _ = actor.send(exit_message(wait_exit(process))); + }) + }), + }, + ) + } +} + +/// A pipe child on Windows: the process object, the job that owns its tree +/// (unless one could not be used), and the signal the daemon sent, if any. +#[derive(Debug)] +pub(in crate::service) struct Process { + pid: u32, + /// Taken by `reap`, which closes both handles. + handles: Mutex>, + /// The signal recorded by `kill`/`signal` before terminating the tree. + killed: Mutex>, +} + +#[derive(Debug)] +struct Handles { + process: Arc, + job: Option>, +} + +impl Process { + pub(super) fn pid(&self) -> u32 { + self.pid + } + + /// The process object, kept open until `reap` so its PID cannot be reused. + fn process_handle(&self) -> Option> { + self.handles + .lock() + .unwrap_or_else(PoisonError::into_inner) + .as_ref() + .map(|handles| Arc::clone(&handles.process)) + } + + /// Kills the process tree. Never terminates after `reap`. + pub(super) fn kill(&self) { + let handles = self.handles.lock().unwrap_or_else(PoisonError::into_inner); + let Some(handles) = handles.as_ref() else { + return; + }; + self.record_kill("SIGKILL", handles); + let _ = terminate(handles); + } + + /// Hard-kills the process tree and records the signal the client will see. + /// Allowed after the exit, to reach what is left of the job; refused after + /// `reap`. + pub(in crate::service) fn signal(&self, signal: ProcessSignal) -> Result<()> { + let name = match signal { + ProcessSignal::Term => "SIGTERM", + ProcessSignal::Int => "SIGINT", + ProcessSignal::Kill => "SIGKILL", + }; + let handles = self.handles.lock().unwrap_or_else(PoisonError::into_inner); + let Some(handles) = handles.as_ref() else { + bail!("process has been reaped"); + }; + self.record_kill(name, handles); + terminate(handles).context("failed to signal process") + } + + /// Call only after the waiter has returned: closes the process and job + /// handles. Closing the job alone kills anything still in it. + pub(super) fn reap(&self) { + self.handles + .lock() + .unwrap_or_else(PoisonError::into_inner) + .take(); + } + + /// Records the signal only while the process still runs, so a process that + /// exited on its own keeps its own exit code, as on Unix. + fn record_kill(&self, name: &'static str, handles: &Handles) { + // SAFETY: the handles lock keeps the process handle open. + if unsafe { WaitForSingleObject(handles.process.as_raw_handle(), 0) } == WAIT_TIMEOUT { + // Termination is asynchronous: the first signal that reached the + // running process is the one the client should see. + self.killed + .lock() + .unwrap_or_else(PoisonError::into_inner) + .get_or_insert(name); + } + } + + fn take_signal(&self) -> Option<&'static str> { + self.killed + .lock() + .unwrap_or_else(PoisonError::into_inner) + .take() + } +} + +/// Terminates the whole tree. +fn terminate(handles: &Handles) -> io::Result<()> { + // SAFETY: the handles stay open while they are borrowed here. + let terminated = unsafe { + match &handles.job { + Some(job) => TerminateJobObject(job.as_raw_handle(), 1), + None => TerminateProcess(handles.process.as_raw_handle(), 1), + } + }; + if terminated != 0 { + return Ok(()); + } + let error = io::Error::last_os_error(); + // A process that has already exited refuses termination; signalling an + // empty Unix process group is not an error either. + if error.raw_os_error() == Some(ERROR_ACCESS_DENIED as i32) { + return Ok(()); + } + Err(error) +} + +/// Returns the process and job handles for a spawned tree. +fn spawn( + id: TerminalId, + request: &CreatePipe, + job: Option<&Job>, + client: OwnedHandle, +) -> Result { + let process = match create_process(request, job, &client) { + Ok(process) => process, + Err(error) if job.is_some() && error.raw_os_error() == Some(ERROR_ACCESS_DENIED as i32) => { + // An outer job with UI restrictions refuses new members. Run the + // command anyway; only the main process can be killed then. + let process = create_process(request, None, &client) + .with_context(|| format!("failed to spawn {}", request.program))?; + eprintln!( + "pipe process {id}: cannot join the job object, killing only the main process: {error}" + ); + process + } + Err(error) => return Err(error).context(format!("failed to spawn {}", request.program)), + }; + // The daemon's copy of the child's write end: holding it open would keep + // the pipe from ever breaking. + drop(client); + Ok(process) +} + +fn create_process( + request: &CreatePipe, + job: Option<&Job>, + client: &OwnedHandle, +) -> io::Result { + let stdin = null_handle()?; + // A child that closes one of stdout or stderr must still write to the + // other, so stderr gets its own handle to the same pipe. + let stderr = duplicate(client)?; + let handles = [ + stdin.as_raw_handle(), + client.as_raw_handle(), + stderr.as_raw_handle(), + ]; + let jobs = job.map(|job| [job.handle()]); + let mut entries = vec![( + PROC_THREAD_ATTRIBUTE_HANDLE_LIST as usize, + handles.as_ptr().cast::(), + size_of_val(&handles), + )]; + entries.extend(jobs.as_ref().map(|jobs| { + ( + PROC_THREAD_ATTRIBUTE_JOB_LIST as usize, + jobs.as_ptr().cast::(), + size_of_val(jobs), + ) + })); + let attributes = Attributes::new(&entries)?; + let mut command = command_line(&request.program, &request.args)?; + let environment = environment_block(&request.env)?; + let directory = wide(request.cwd.as_os_str())?; + let mut startup = STARTUPINFOEXW::default(); + startup.StartupInfo.cb = size_of::() as u32; + startup.StartupInfo.dwFlags = STARTF_USESTDHANDLES; + startup.StartupInfo.hStdInput = stdin.as_raw_handle(); + startup.StartupInfo.hStdOutput = client.as_raw_handle(); + startup.StartupInfo.hStdError = stderr.as_raw_handle(); + startup.lpAttributeList = attributes.list(); + let mut info = PROCESS_INFORMATION::default(); + // SAFETY: every buffer and handle stays live through the call. The child + // gets no window and shares the output pipe through the handle list; the + // job list puts it in the job atomically, with no breakaway flags. + let created = unsafe { + CreateProcessW( + null(), + command.as_mut_ptr(), + null(), + null(), + 1, + CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT, + environment.as_ptr().cast(), + directory.as_ptr(), + (&raw const startup).cast(), + &mut info, + ) + }; + if created == 0 { + return Err(io::Error::last_os_error()); + } + // SAFETY: CreateProcessW returned new, uniquely owned handles; the thread + // handle is not needed. + let process = owned_handle(info.hProcess)?; + drop(owned_handle(info.hThread)?); + Ok(Process { + pid: info.dwProcessId, + handles: Mutex::new(Some(Handles { + process: Arc::new(process), + job: job.map(|job| Arc::clone(&job.0)), + })), + killed: Mutex::new(None), + }) +} + +/// Waits for the child to exit, then reports the exit code, or the signal the +/// daemon sent when it terminated the tree. +fn wait_exit(process: Arc) -> io::Result { + let Some(handle) = process.process_handle() else { + return Err(io::Error::other("process has been reaped")); + }; + // SAFETY: the process object stays open for the whole wait. + if unsafe { WaitForSingleObject(handle.as_raw_handle(), INFINITE) } != WAIT_OBJECT_0 { + return Err(io::Error::last_os_error()); + } + let mut code = 0; + // SAFETY: a signalled process object has a final exit code. + if unsafe { GetExitCodeProcess(handle.as_raw_handle(), &mut code) } == 0 { + return Err(io::Error::last_os_error()); + } + Ok(match process.take_signal() { + Some(name) => Exit::Signal(name.to_string()), + None => Exit::Code(code), + }) +} + +/// Signals the reader to stop. Every clone can signal; the first drop sets the +/// event, and later ones are harmless. +#[derive(Clone)] +struct Cancel(Arc); + +impl Cancel { + fn new() -> io::Result { + event().map(|event| Self(Arc::new(event))) + } + + fn handle(&self) -> HANDLE { + self.0.as_raw_handle() + } + + fn signalled(&self) -> bool { + // SAFETY: the shared event stays open through the call. + unsafe { WaitForSingleObject(self.0.as_raw_handle(), 0) == WAIT_OBJECT_0 } + } +} + +impl Drop for Cancel { + fn drop(&mut self) { + // SAFETY: the shared event stays open through the call. + unsafe { + SetEvent(self.0.as_raw_handle()); + } + } +} + +/// One overlapped read: stable allocations for the `OVERLAPPED` and the buffer +/// the kernel writes into, and an RAII drop that cancels and reaps a pending +/// operation before either allocation is released. +struct Read { + // Fields drop in declaration order, so the allocations the kernel points + // at go first and the handles they refer to stay open until last. + overlapped: Retained, + buffer: Retained<[u8; READ_BYTES]>, + event: OwnedHandle, + pipe: Arc, + pending: bool, +} + +impl Read { + fn new(pipe: Arc) -> io::Result { + let event = event()?; + Ok(Self { + overlapped: Retained::new(OVERLAPPED { + hEvent: event.as_raw_handle(), + ..Default::default() + }), + buffer: Retained::new([0; READ_BYTES]), + event, + pipe, + pending: false, + }) + } + + fn is_pending(&self) -> bool { + self.pending + } + + /// Submits one read. `Some` means it completed before returning. + fn submit(&mut self) -> io::Result> { + debug_assert!(!self.pending); + // SAFETY: the OVERLAPPED and buffer are stable Retained allocations and + // stay live until the completion is reaped; only this struct uses them. + unsafe { + ResetEvent(self.event.as_raw_handle()); + if ReadFile( + self.pipe.as_raw_handle(), + self.buffer.as_ptr().cast::(), + READ_BYTES as u32, + null_mut(), + self.overlapped.as_ptr(), + ) != 0 + { + self.pending = true; + return self.complete().map(Some); + } + } + let error = io::Error::last_os_error(); + if error.raw_os_error() != Some(ERROR_IO_PENDING as i32) { + return Err(error); + } + self.pending = true; + Ok(None) + } + + /// Reaps the completion. Waiting is required once the event is signalled: + /// the kernel may still be finishing the operation. + fn complete(&mut self) -> io::Result { + let mut count = 0; + // SAFETY: this operation was submitted and its buffers are live. + let completed = unsafe { + GetOverlappedResult( + self.pipe.as_raw_handle(), + self.overlapped.as_ptr(), + &mut count, + 1, + ) + }; + if completed != 0 { + self.pending = false; + return Ok(count as usize); + } + let error = io::Error::last_os_error(); + if error.raw_os_error() == Some(ERROR_IO_INCOMPLETE as i32) { + // Only possible without waiting; keep the operation pending so + // dropping `Read` cancels and reaps it. + return Err(error); + } + self.pending = false; + Err(error) + } + + /// The bytes of a completed read. + fn bytes(&self, length: usize) -> &[u8] { + debug_assert!(!self.pending); + // SAFETY: no read is pending, so the kernel has stopped using the + // allocation and this is its unique owner. + unsafe { std::slice::from_raw_parts(self.buffer.as_ptr().cast::(), length) } + } + + fn event(&self) -> HANDLE { + self.event.as_raw_handle() + } +} + +impl Drop for Read { + fn drop(&mut self) { + if !self.pending { + return; + } + let mut count = 0; + // SAFETY: cancel THIS operation, then reap its completion before the + // OVERLAPPED, buffer or event can be released. CancelIoEx alone does + // not guarantee the kernel has stopped using them. + unsafe { + CancelIoEx(self.pipe.as_raw_handle(), self.overlapped.as_ptr()); + GetOverlappedResult( + self.pipe.as_raw_handle(), + self.overlapped.as_ptr(), + &mut count, + 1, + ); + } + self.pending = false; + } +} + +/// Reads output until every writer is gone, until [`DRAIN_AFTER_EXIT`] after +/// the process exits, or until [`Cancel`] fires. +fn read_output(pipe: Arc, sink: OutputSink, cancel: Cancel, process: Arc) { + let mut sink = sink; + let Some(process_object) = process.process_handle() else { + return; + }; + let mut read = match Read::new(pipe) { + Ok(read) => read, + Err(error) => { + sink.failed(error.to_string()); + return; + } + }; + // The process may have exited before the reader started. + let mut exited = process_exited(&process_object); + let mut deadline = exited.then(|| Instant::now() + DRAIN_AFTER_EXIT); + loop { + // Cancellation and the exit win over output, so a descendant that + // writes without pause cannot keep the reader from noticing them. + if cancel.signalled() { + sink.eof(); + return; + } + if !exited && process_exited(&process_object) { + exited = true; + deadline = Some(Instant::now() + DRAIN_AFTER_EXIT); + } + if deadline.is_some_and(|deadline| Instant::now() >= deadline) { + // Dropping `read` cancels and reaps a pending operation. + sink.eof(); + return; + } + if !read.is_pending() { + match read.submit() { + Ok(Some(0)) => { + sink.eof(); + return; + } + Ok(Some(length)) => { + // Check cancel, the exit and the deadline again before + // reading more. + if !sink.write(read.bytes(length)) { + return; + } + continue; + } + Ok(None) => {} + Err(error) => { + end_on_read_error(sink, error); + return; + } + } + } + let timeout = match deadline { + None => INFINITE, + Some(deadline) => { + let left = deadline.saturating_duration_since(Instant::now()); + // Round up: a sub-millisecond remainder must not become a busy 0. + left.as_nanos().div_ceil(1_000_000).min(u32::MAX as u128) as u32 + } + }; + // Cancel is first: WaitForMultipleObjects reports the lowest signalled + // index, so an always-readable pipe cannot starve it. + let handles = [ + cancel.handle(), + read.event(), + process_object.as_raw_handle(), + ]; + // Once the exit is seen, stop waiting on its signalled handle. + let count = if exited { 2 } else { 3 }; + // SAFETY: every handle stays live through the wait. + match unsafe { WaitForMultipleObjects(count, handles.as_ptr(), 0, timeout) } { + CANCEL_READY => { + sink.eof(); + return; + } + READ_READY => match read.complete() { + Ok(0) => { + sink.eof(); + return; + } + Ok(length) => { + if !sink.write(read.bytes(length)) { + return; + } + } + Err(error) => { + end_on_read_error(sink, error); + return; + } + }, + PROCESS_READY => { + exited = true; + deadline = Some(Instant::now() + DRAIN_AFTER_EXIT); + } + WAIT_TIMEOUT => { + // The drain deadline passed with a descendant still writing. + sink.eof(); + return; + } + _ => { + let error = io::Error::last_os_error(); + sink.failed(error.to_string()); + return; + } + } + } +} + +fn process_exited(process: &OwnedHandle) -> bool { + // SAFETY: the process handle stays open for the whole read. + unsafe { WaitForSingleObject(process.as_raw_handle(), 0) == WAIT_OBJECT_0 } +} + +fn end_on_read_error(sink: OutputSink, error: io::Error) { + if is_end_of_output(&error) { + sink.eof(); + } else { + sink.failed(error.to_string()); + } +} + +fn is_end_of_output(error: &io::Error) -> bool { + matches!( + error.raw_os_error(), + Some(code) + if code == ERROR_BROKEN_PIPE as i32 + || code == ERROR_PIPE_NOT_CONNECTED as i32 + || code == ERROR_NO_DATA as i32 + // Our own CancelIoEx, or the handle closing under us. + || code == ERROR_OPERATION_ABORTED as i32 + ) +} + +/// The daemon's read end and the child's inheritable write end. +struct OutputPipe { + server: Arc, + client: OwnedHandle, +} + +fn output_pipe() -> Result { + let security = PrivateSecurity::new()?; + let name: Vec = format!(r"\\.\pipe\opencode-pty-{:032x}", rand::random::()) + .encode_utf16() + .chain([0]) + .collect(); + // SAFETY: the name is NUL-terminated and the descriptor and attributes + // outlive the call. One private, inbound, byte-mode instance with a 64 KiB + // in-buffer, so the child can write ahead while the reader is busy. + let server = unsafe { + CreateNamedPipeW( + name.as_ptr(), + PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED | FILE_FLAG_FIRST_PIPE_INSTANCE, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS, + 1, + 0, + BUFFER_BYTES, + 0, + &security.attributes(), + ) + }; + let server = Arc::new(owned_handle(server).context("failed to create output pipe")?); + // SAFETY: the same NUL-terminated name; only the returned handle is + // inheritable, and it is the only handle of this open. Read attributes + // are included so clients that fstat their stdout (Git Bash, Python, Go) + // can query the pipe. + let client = unsafe { + CreateFileW( + name.as_ptr(), + GENERIC_WRITE | FILE_READ_ATTRIBUTES, + 0, + &inheritable(), + OPEN_EXISTING, + 0, + null_mut(), + ) + }; + let client = owned_handle(client).context("failed to connect output pipe")?; + let connect_event = event()?; + let overlapped = Retained::new(OVERLAPPED { + hEvent: connect_event.as_raw_handle(), + ..Default::default() + }); + // The client end is already open, so the connect normally completes at + // once with ERROR_PIPE_CONNECTED instead of waiting. + if unsafe { ConnectNamedPipe(server.as_raw_handle(), overlapped.as_ptr()) } == 0 { + let error = io::Error::last_os_error(); + match error.raw_os_error() { + Some(code) if code == ERROR_PIPE_CONNECTED as i32 => {} + Some(code) if code == ERROR_IO_PENDING as i32 => { + let mut count = 0; + // SAFETY: the handle and the Retained OVERLAPPED stay live. + if unsafe { + GetOverlappedResult(server.as_raw_handle(), overlapped.as_ptr(), &mut count, 1) + } == 0 + { + let error = io::Error::last_os_error(); + // Never return while the kernel might still own the + // OVERLAPPED: cancel and reap the completion. + unsafe { + CancelIoEx(server.as_raw_handle(), overlapped.as_ptr()); + GetOverlappedResult( + server.as_raw_handle(), + overlapped.as_ptr(), + &mut count, + 1, + ); + } + return Err(error).context("failed to connect output pipe"); + } + } + _ => return Err(error).context("failed to connect output pipe"), + } + } + Ok(OutputPipe { server, client }) +} + +/// A kill-on-close job: terminating it kills every descendant, and a crashed +/// daemon closes it and kills them too. +struct Job(Arc); + +impl Job { + fn new(id: TerminalId) -> Option { + // SAFETY: an unnamed job with default attributes, adopted on success. + let job = match owned_handle(unsafe { CreateJobObjectW(null(), null()) }) { + Ok(job) => job, + Err(error) => { + warn_no_job(id, &error); + return None; + } + }; + let limits = JOBOBJECT_EXTENDED_LIMIT_INFORMATION { + BasicLimitInformation: JOBOBJECT_BASIC_LIMIT_INFORMATION { + LimitFlags: JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, + ..Default::default() + }, + ..Default::default() + }; + // SAFETY: the job stays open through the call and the limits match + // the extended-information class. + if unsafe { + SetInformationJobObject( + job.as_raw_handle(), + JobObjectExtendedLimitInformation, + (&raw const limits).cast(), + size_of_val(&limits) as u32, + ) + } == 0 + { + warn_no_job(id, &io::Error::last_os_error()); + return None; + } + Some(Self(Arc::new(job))) + } + + fn handle(&self) -> HANDLE { + self.0.as_raw_handle() + } +} + +fn warn_no_job(id: TerminalId, error: &io::Error) { + eprintln!("pipe process {id}: running without a job object: {error}"); +} + +/// Opens the output file first, so a bad path fails the request with nothing +/// to clean up. Truncates through the handle without following a reparse point +/// planted at the final component. +fn open_output(path: &Path) -> Result { + let name = wide(path.as_os_str())?; + let message = format!("failed to open output file {}", path.display()); + // SAFETY: the name is NUL-terminated; the returned handle is inherited by + // nothing and adopted at once. + let handle = unsafe { + CreateFileW( + name.as_ptr(), + GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, + null(), + OPEN_ALWAYS, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT, + null_mut(), + ) + }; + let file = File::from(owned_handle(handle).with_context(|| message.clone())?); + let mut info = BY_HANDLE_FILE_INFORMATION::default(); + // SAFETY: the file owns a live handle and info is valid writable output. + if unsafe { GetFileInformationByHandle(file.as_raw_handle(), &mut info) } == 0 { + return Err(io::Error::last_os_error()).with_context(|| message); + } + if info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT != 0 { + bail!("{message}: output path is a reparse point"); + } + // SAFETY: the handle has write access and the position is at the start + // after opening. + if unsafe { SetEndOfFile(file.as_raw_handle()) } == 0 { + return Err(io::Error::last_os_error()).with_context(|| message); + } + Ok(file) +} + +fn inheritable() -> SECURITY_ATTRIBUTES { + SECURITY_ATTRIBUTES { + nLength: size_of::() as u32, + lpSecurityDescriptor: null_mut(), + bInheritHandle: 1, + } +} + +fn null_handle() -> io::Result { + let name: Vec = "NUL\0".encode_utf16().collect(); + // SAFETY: the name is NUL-terminated; the handle is inheritable only for + // the spawn that lists it. + owned_handle(unsafe { + CreateFileW( + name.as_ptr(), + GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE, + &inheritable(), + OPEN_EXISTING, + 0, + null_mut(), + ) + }) +} + +/// Duplicates a handle for the child, inheritable and with the same access. +fn duplicate(handle: &OwnedHandle) -> io::Result { + let mut duplicate = null_mut(); + // SAFETY: both process handles are pseudo-handles for this process and the + // source handle is live; the new handle is adopted below. + if unsafe { + DuplicateHandle( + GetCurrentProcess(), + handle.as_raw_handle(), + GetCurrentProcess(), + &mut duplicate, + 0, + 1, + DUPLICATE_SAME_ACCESS, + ) + } == 0 + { + return Err(io::Error::last_os_error()); + } + owned_handle(duplicate) +} + +/// The command line `CreateProcessW` parses and the child recovers with +/// `CommandLineToArgvW`/MSVCRT rules: the same algorithm as Rust std. +fn command_line(program: &str, args: &[String]) -> io::Result> { + // A NUL would truncate the line, and the program is always quoted, so a + // quote in it cannot be represented. + if program.contains(['\0', '"']) || args.iter().any(|arg| arg.contains('\0')) { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "command cannot be passed to a child process", + )); + } + let mut line = Vec::new(); + // Always quoted: CreateProcessW finds the executable in the first token, + // and quoting removes any ambiguity for the child. + line.push('"' as u16); + line.extend(program.encode_utf16()); + line.push('"' as u16); + for arg in args { + line.push(' ' as u16); + append_arg(&mut line, arg); + } + // CreateProcessW needs a terminated string; without this it reads past the + // buffer and runs the last argument plus whatever follows it. + line.push(0); + Ok(line) +} + +/// Quotes one argument only when it needs it, doubling backslashes before a +/// quote and at the end of a quoted argument. +fn append_arg(line: &mut Vec, arg: &str) { + let quote = arg.is_empty() || arg.contains([' ', '\t']); + if quote { + line.push('"' as u16); + } + let mut backslashes = 0; + for unit in arg.encode_utf16() { + if unit == '\\' as u16 { + backslashes += 1; + } else { + if unit == '"' as u16 { + // Add n+1 backslashes to total 2n+1 before the internal quote. + line.extend((0..=backslashes).map(|_| '\\' as u16)); + } + backslashes = 0; + } + line.push(unit); + } + if quote { + // Add n backslashes to total 2n before the closing quote. + line.extend((0..backslashes).map(|_| '\\' as u16)); + line.push('"' as u16); + } +} + +/// Exactly the given environment, with nothing inherited from the daemon: +/// entries deduplicated and sorted the way Windows compares variable names, +/// terminated by a NUL, and a final NUL for the block. +fn environment_block(env: &HashMap) -> io::Result> { + let mut entries = Vec::with_capacity(env.len()); + for (key, value) in env { + // Windows allows a leading `=` for per-drive current directories + // (`=C:`), but no other `=` in the name, and a lone `=` is invalid. + if key.is_empty() + || key == "=" + || key.contains('\0') + || value.contains('\0') + || key.strip_prefix('=').unwrap_or(key).contains('=') + { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "environment variable cannot be passed to a child process", + )); + } + entries.push(( + key.encode_utf16().collect::>(), + value.encode_utf16().collect::>(), + )); + } + // Sort deterministically before collapsing case variants, so the same + // request always produces the same block; the last variant wins. + entries.sort(); + entries.sort_by(|a, b| compare_keys(&a.0, &b.0)); + let mut unique: Vec<(Vec, Vec)> = Vec::with_capacity(entries.len()); + for entry in entries { + match unique.last_mut() { + Some(last) if compare_keys(&last.0, &entry.0) == Ordering::Equal => *last = entry, + _ => unique.push(entry), + } + } + let mut block = Vec::new(); + // An empty environment is signalled by a null, like Rust std. + if unique.is_empty() { + block.push(0); + } + for (key, value) in unique { + block.extend(key); + block.push('=' as u16); + block.extend(value); + block.push(0); + } + block.push(0); + Ok(block) +} + +/// Compares variable names the way Windows does: case-insensitively, then +/// ordinally (`CompareStringOrdinal(..., TRUE)`, as Rust std's `EnvKey`). +fn compare_keys(a: &[u16], b: &[u16]) -> Ordering { + // SAFETY: both slices are valid UTF-16 of the given length. + let result = + unsafe { CompareStringOrdinal(a.as_ptr(), a.len() as i32, b.as_ptr(), b.len() as i32, 1) }; + match result { + CSTR_LESS_THAN => Ordering::Less, + CSTR_GREATER_THAN => Ordering::Greater, + CSTR_EQUAL => Ordering::Equal, + // Not expected for valid UTF-16; keep a total order regardless. + _ => a.cmp(b), + } +} + +fn wide(value: &OsStr) -> io::Result> { + let mut wide = value.encode_wide().collect::>(); + if wide.contains(&0) { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "path contains a NUL", + )); + } + wide.push(0); + Ok(wide) +} + +/// An initialized process-attribute list and the buffer it points into, freed +/// in the right order. +struct Attributes { + // Never read: the list points into this allocation, which must stay put + // and alive for the list's lifetime. + _buffer: Vec, + list: *mut c_void, +} + +impl Attributes { + fn new(entries: &[(usize, *const c_void, usize)]) -> io::Result { + let mut bytes = 0; + // SAFETY: the first call only reports the required size. + unsafe { + InitializeProcThreadAttributeList(null_mut(), entries.len() as u32, 0, &mut bytes); + } + if bytes == 0 { + return Err(io::Error::last_os_error()); + } + let mut buffer = vec![0_usize; bytes.div_ceil(size_of::())]; + let list = buffer.as_mut_ptr().cast::(); + // SAFETY: the buffer is at least `bytes` long, `usize`-aligned, and + // outlives the list. + if unsafe { InitializeProcThreadAttributeList(list, entries.len() as u32, 0, &mut bytes) } + == 0 + { + return Err(io::Error::last_os_error()); + } + let this = Self { + _buffer: buffer, + list, + }; + for &(attribute, value, length) in entries { + // SAFETY: the list was initialized for exactly `entries.len()` + // attributes; every value outlives the list. + if unsafe { + UpdateProcThreadAttribute( + this.list, + 0, + attribute, + value, + length, + null_mut(), + null(), + ) + } == 0 + { + return Err(io::Error::last_os_error()); + } + } + Ok(this) + } + + fn list(&self) -> *mut c_void { + self.list + } +} + +impl Drop for Attributes { + fn drop(&mut self) { + // SAFETY: the list was successfully initialized and is unused after. + unsafe { + DeleteProcThreadAttributeList(self.list); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::service::PipeMetadata; + use windows_sys::Win32::System::JobObjects::IsProcessInJob; + + fn text(value: &[u16]) -> String { + String::from_utf16(value).unwrap() + } + + fn test_request(program: &str, args: &[&str]) -> CreatePipe { + CreatePipe { + program: program.into(), + args: args.iter().map(|arg| arg.to_string()).collect(), + cwd: std::env::current_dir().unwrap(), + env: ["PATH", "SystemRoot", "TEMP"] + .into_iter() + .filter_map(|name| Some((name.to_string(), std::env::var(name).ok()?))) + .collect(), + group_id: "test".into(), + output_path: std::env::temp_dir().join(format!( + "opencode-pty-pipe-test-{:032x}.out", + rand::random::() + )), + metadata: PipeMetadata::new(), + } + } + + #[test] + fn command_lines_are_terminated_and_quoted_for_msvcrt_parsing() { + let line = command_line("cmd.exe", &["/d".into(), "/c".into(), "echo hi".into()]).unwrap(); + assert_eq!(line.last(), Some(&0)); + assert_eq!( + text(&line[..line.len() - 1]), + r#""cmd.exe" /d /c "echo hi""# + ); + + let line = command_line("a b.exe", &[String::new(), r#"echo "hi""#.into()]).unwrap(); + assert_eq!( + text(&line[..line.len() - 1]), + r#""a b.exe" "" "echo \"hi\"""# + ); + + // A quoted argument with a trailing backslash keeps the closing quote. + let line = command_line("x.exe", &[r"C:\a b\".into()]).unwrap(); + assert_eq!(text(&line[..line.len() - 1]), r#""x.exe" "C:\a b\\""#); + } + + #[test] + fn command_lines_reject_unrepresentable_programs_and_arguments() { + assert!(command_line("a\0b", &[]).is_err()); + assert!(command_line("a\"b", &[]).is_err()); + assert!(command_line("x.exe", &["a\0b".into()]).is_err()); + } + + #[test] + fn environment_blocks_are_sorted_and_double_terminated() { + let env = HashMap::from([ + ("B".to_string(), "2".to_string()), + ("A".to_string(), "1".to_string()), + ]); + let block = environment_block(&env).unwrap(); + assert_eq!(block, "A=1\0B=2\0\0".encode_utf16().collect::>()); + + let empty = environment_block(&HashMap::new()).unwrap(); + assert_eq!(empty, [0, 0]); + } + + #[test] + fn environment_blocks_accept_drive_directories_and_collapse_case_variants() { + let env = HashMap::from([ + ("=C:".to_string(), r"C:\work".to_string()), + ("PATH".to_string(), "upper".to_string()), + ("Path".to_string(), "lower".to_string()), + ]); + let block = environment_block(&env).unwrap(); + let block = text(&block); + // The last variant in ordinal order wins, deterministically. + assert_eq!(block, "=C:=C:\\work\0Path=lower\0\0"); + + for invalid in [("", "x"), ("=", "x"), ("A=B", "x"), ("A\0B", "x")] { + let env = HashMap::from([(invalid.0.to_string(), invalid.1.to_string())]); + assert!(environment_block(&env).is_err(), "{:?}", invalid.0); + } + } + + #[test] + fn environment_blocks_are_ordered_like_compare_string_ordinal() { + let env = HashMap::from([ + ("a".to_string(), "1".to_string()), + ("B".to_string(), "2".to_string()), + ]); + let block = environment_block(&env).unwrap(); + // Case-insensitive ordinal puts `a` before `B`; raw UTF-16 orders `B` + // first because its code unit is smaller. + assert_eq!(text(&block), "a=1\0B=2\0\0"); + } + + #[test] + fn a_jobless_spawn_runs_the_command_and_reports_its_exit() { + let request = test_request("cmd.exe", &["/d", "/c", "exit /b 5"]); + let OutputPipe { client, .. } = output_pipe().unwrap(); + let process = Arc::new(spawn(1, &request, None, client).unwrap()); + assert!(matches!( + wait_exit(Arc::clone(&process)).unwrap(), + Exit::Code(5) + )); + process.reap(); + } + + #[test] + fn a_job_handle_without_assign_access_falls_back_to_a_jobless_spawn() { + let job = Job::new(2).unwrap(); + let mut restricted = null_mut(); + // JOB_OBJECT_QUERY only: joining a job needs JOB_OBJECT_ASSIGN_PROCESS. + const JOB_OBJECT_QUERY: u32 = 4; + // SAFETY: both process handles are pseudo-handles for this process and + // the job handle is live; the duplicate is adopted below. + assert_ne!( + unsafe { + DuplicateHandle( + GetCurrentProcess(), + job.handle(), + GetCurrentProcess(), + &mut restricted, + JOB_OBJECT_QUERY, + 0, + 0, + ) + }, + 0 + ); + let job = Job(Arc::new(owned_handle(restricted).unwrap())); + let request = test_request("cmd.exe", &["/d", "/c", "exit /b 5"]); + let OutputPipe { client, .. } = output_pipe().unwrap(); + let process = Arc::new(spawn(2, &request, Some(&job), client).unwrap()); + let mut in_job = 0; + // SAFETY: the process handle stays open until `reap` and the job is live. + assert_ne!( + unsafe { + IsProcessInJob( + process.process_handle().unwrap().as_raw_handle(), + job.handle(), + &mut in_job, + ) + }, + 0 + ); + assert_eq!(in_job, 0, "the restricted job must have been refused"); + assert!(matches!( + wait_exit(Arc::clone(&process)).unwrap(), + Exit::Code(5) + )); + process.reap(); + } +} diff --git a/packages/process-daemon/src/transport/mod.rs b/packages/process-daemon/src/transport/mod.rs index 614c3dc3837..3779b62f336 100644 --- a/packages/process-daemon/src/transport/mod.rs +++ b/packages/process-daemon/src/transport/mod.rs @@ -18,7 +18,7 @@ mod unix; pub(crate) use unix::{Cancellation, Connection, Listener, Readiness, Wake}; #[cfg(windows)] -mod retained; +pub(crate) mod retained; #[cfg(windows)] pub(crate) mod windows; diff --git a/packages/process-daemon/src/transport/windows.rs b/packages/process-daemon/src/transport/windows.rs index 31b60a1519e..7a7149a0d9a 100644 --- a/packages/process-daemon/src/transport/windows.rs +++ b/packages/process-daemon/src/transport/windows.rs @@ -540,7 +540,7 @@ impl Drop for Operation { } } -fn event() -> io::Result { +pub(crate) fn event() -> io::Result { // SAFETY: create an unnamed, non-inheritable, initially unset manual-reset event. owned_handle(unsafe { CreateEventW(null(), 1, 0, null()) }) } diff --git a/packages/process-daemon/tests/pipe_windows.rs b/packages/process-daemon/tests/pipe_windows.rs new file mode 100644 index 00000000000..f93c47348f7 --- /dev/null +++ b/packages/process-daemon/tests/pipe_windows.rs @@ -0,0 +1,778 @@ +#![cfg(windows)] + +//! Pipe mode on Windows: no console, one output pipe, job-based kills. +//! +//! The Unix suite in `tests/pipe.rs` keeps its own cases; these only run on +//! Windows. Commands are passed the way OpenCode passes them (`cmd.exe /c +//! `), and longer checks use a PowerShell script file. Windows +//! PowerShell needs `SystemRoot` in its environment to start, so test +//! children get a PATH, SystemRoot and TEMP, never the daemon's own. + +use std::collections::HashMap; +use std::env; +use std::fs; +use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle}; +use std::path::{Path, PathBuf}; +use std::time::{Duration, Instant}; + +use opencode_pty::service::{ + CreatePipe, MAX_METADATA_BYTES, PipeMetadata, ProcessExit, ProcessInfo, ProcessMode, + ProcessSignal, TerminalId, TerminalLifecycle, TerminalService, +}; +use windows_sys::Win32::Foundation::{ERROR_PRIVILEGE_NOT_HELD, WAIT_OBJECT_0}; +use windows_sys::Win32::System::Threading::{ + OpenProcess, PROCESS_SYNCHRONIZE, WaitForSingleObject, +}; + +/// A unique temporary path, removed when dropped even if a test fails. +struct TempPath(PathBuf); + +impl TempPath { + fn new(name: &str) -> Self { + Self(env::temp_dir().join(format!( + "opencode-pty-pipe-{name}-{:032x}", + rand::random::() + ))) + } + + fn script(name: &str, extension: &str) -> Self { + Self(env::temp_dir().join(format!( + "opencode-pty-pipe-{name}-{:032x}.{extension}", + rand::random::() + ))) + } +} + +impl Drop for TempPath { + fn drop(&mut self) { + let _ = fs::remove_file(&self.0); + let _ = fs::remove_dir_all(&self.0); + } +} + +/// A PowerShell script on disk. +struct Script(TempPath); + +impl Script { + fn powershell(name: &str, body: &str) -> Self { + let path = TempPath::script(name, "ps1"); + fs::write(&path.0, body).unwrap(); + Self(path) + } + + /// A request that runs the script; `env` replaces the whole environment. + fn request(&self, output: &Path, env: HashMap) -> CreatePipe { + pipe( + "powershell.exe", + vec![ + "-NoProfile".into(), + "-NonInteractive".into(), + "-ExecutionPolicy".into(), + "Bypass".into(), + "-File".into(), + self.0.0.display().to_string(), + ], + output, + env, + ) + } +} + +fn pipe( + program: &str, + args: Vec, + output: &Path, + env: HashMap, +) -> CreatePipe { + CreatePipe { + program: program.into(), + args, + cwd: env::current_dir().expect("cwd"), + env, + group_id: "pipe".into(), + output_path: output.to_path_buf(), + metadata: PipeMetadata::new(), + } +} + +/// A `cmd.exe /c ` request, the way OpenCode runs shell commands. +fn cmd(command: &str, output: &Path) -> CreatePipe { + pipe( + "cmd.exe", + vec!["/d".into(), "/c".into(), command.into()], + output, + base_env(), + ) +} + +/// The daemon's own environment is never inherited, so children get just what +/// their program needs. Windows PowerShell fails to load without SystemRoot. +fn base_env() -> HashMap { + ["PATH", "SystemRoot", "TEMP"] + .into_iter() + .filter_map(|name| Some((name.to_string(), env::var(name).ok()?))) + .collect() +} + +/// Starts a request with a fresh output file. +fn start( + service: &TerminalService, + request: impl FnOnce(&Path) -> CreatePipe, +) -> (ProcessInfo, TempPath) { + let file = TempPath::new("out"); + let info = service.create_pipe(request(&file.0)).unwrap(); + (info, file) +} + +struct Finished { + output: String, + exit: ProcessExit, + elapsed: Duration, +} + +/// Waits for the exit and reads the output file. +fn run(service: &TerminalService, id: TerminalId, file: &TempPath) -> Finished { + let started = Instant::now(); + let exit = service.wait(id).unwrap().recv().unwrap(); + Finished { + output: String::from_utf8_lossy(&fs::read(&file.0).unwrap()).into_owned(), + exit, + elapsed: started.elapsed(), + } +} + +/// Polls the output file until it holds a whole first line. +fn first_line(file: &TempPath) -> String { + let deadline = Instant::now() + Duration::from_secs(10); + loop { + let output = fs::read_to_string(&file.0).unwrap_or_default(); + if let Some((line, _)) = output.split_once("\r\n") { + return line.to_string(); + } + assert!(Instant::now() < deadline, "no output line"); + std::thread::sleep(Duration::from_millis(10)); + } +} + +/// Polls a file written by a child until it holds a PID. +fn read_pid(file: &Path) -> u32 { + let deadline = Instant::now() + Duration::from_secs(10); + loop { + if let Ok(text) = fs::read_to_string(file) + && let Ok(pid) = text.trim().parse() + { + return pid; + } + assert!(Instant::now() < deadline, "child did not write its PID"); + std::thread::sleep(Duration::from_millis(20)); + } +} + +fn alive(pid: u32) -> bool { + // SAFETY: a successful handle is adopted immediately; it is only waited on. + let handle = unsafe { OpenProcess(PROCESS_SYNCHRONIZE, 0, pid) }; + if handle.is_null() { + return false; + } + // SAFETY: OpenProcess returned a new, uniquely owned handle. + let handle = unsafe { OwnedHandle::from_raw_handle(handle) }; + // SAFETY: the owned handle stays valid through this zero-timeout wait. + unsafe { WaitForSingleObject(handle.as_raw_handle(), 0) != WAIT_OBJECT_0 } +} + +fn wait_dead(pid: u32) { + let deadline = Instant::now() + Duration::from_secs(10); + while alive(pid) { + assert!(Instant::now() < deadline, "process {pid} is still running"); + std::thread::sleep(Duration::from_millis(10)); + } +} + +fn newlines(bytes: &[u8]) -> u64 { + bytes.iter().filter(|&&byte| byte == b'\n').count() as u64 +} + +/// A path as a single-quoted PowerShell string literal. +fn ps_path(path: &Path) -> String { + path.display().to_string().replace('\'', "''") +} + +/// Writes a batch file. Paths are quoted inside the file, where cmd parses +/// them normally; quoting them inside the `/c` argument would be mangled by +/// the MSVCRT escaping applied to command-line arguments. +fn batch(name: &str, lines: &[String]) -> TempPath { + let path = TempPath::script(name, "cmd"); + fs::write(&path.0, lines.join("\r\n") + "\r\n").unwrap(); + path +} + +#[test] +fn reports_ordered_output_and_exit_code_without_a_terminal() { + let service = TerminalService::default(); + let (info, file) = start(&service, |file| { + cmd( + "echo out1&echo err1>&2&echo out2&echo err2>&2&echo out3&echo err3>&2&set /p line=&echo stdin-closed&exit /b 7", + file, + ) + }); + assert_eq!(info.mode, ProcessMode::Pipe); + assert!(info.pty.is_none()); + let pipe = info.pipe.as_ref().unwrap(); + assert_eq!(pipe.output_path, file.0); + assert_eq!(info.exited_at, None); + let finished = run(&service, info.id, &file); + assert_eq!( + finished.output, + "out1\r\nerr1\r\nout2\r\nerr2\r\nout3\r\nerr3\r\nstdin-closed\r\n" + ); + assert_eq!(finished.exit.exit_code, Some(7)); + assert_eq!(finished.exit.signal, None); + assert_eq!(finished.exit.output_error, None); + assert_eq!(finished.exit.output_lines, Some(7)); + assert_eq!(finished.exit.final_offset, finished.output.len() as u64); + let listed = service.list(ProcessMode::Pipe).unwrap(); + assert_eq!( + listed[0].lifecycle, + TerminalLifecycle::Exited { + exit_code: Some(7), + signal: None + } + ); + assert_eq!(listed[0].exited_at, Some(finished.exit.exited_at)); + assert!(listed[0].started_at <= finished.exit.exited_at); + assert_eq!(listed[0].pipe.as_ref().unwrap().output_lines, 7); + assert_eq!(listed[0].output_tail, finished.exit.final_offset); + service.terminate(info.id).unwrap(); +} + +#[test] +fn fast_exit_without_output_is_reported() { + let service = TerminalService::default(); + let (info, file) = start(&service, |file| cmd("exit /b 3", file)); + let finished = run(&service, info.id, &file); + assert_eq!(finished.output, ""); + assert_eq!(finished.exit.exit_code, Some(3)); + assert_eq!(finished.exit.final_offset, 0); + assert_eq!(finished.exit.output_lines, Some(0)); + service.terminate(info.id).unwrap(); +} + +#[test] +fn the_child_gets_exactly_the_given_environment() { + let service = TerminalService::default(); + let system_root = env::var("SystemRoot").unwrap(); + let (info, file) = start(&service, |file| { + pipe( + "cmd.exe", + vec!["/d".into(), "/c".into(), "set".into()], + file, + // No PATH and nothing else from the daemon. + HashMap::from([ + ("ONLY".to_string(), "this".to_string()), + ("SystemRoot".to_string(), system_root.clone()), + ]), + ) + }); + let finished = run(&service, info.id, &file); + // `set` also prints each drive's current directory as an `=C:` entry, and + // cmd defines COMSPEC, PATHEXT and PROMPT itself when they are missing; + // everything else must come from the request. + let variables = finished + .output + .lines() + .map(|line| line.trim_end_matches('\r')) + .filter(|line| { + !line.starts_with('=') + && !line.starts_with("COMSPEC=") + && !line.starts_with("PATHEXT=") + && !line.starts_with("PROMPT=") + && !line.starts_with("PROCESSOR_ARCHITECTURE=") + }) + .collect::>(); + let expected = format!("SystemRoot={system_root}"); + assert_eq!(variables, ["ONLY=this", expected.as_str()]); + service.terminate(info.id).unwrap(); +} + +#[test] +fn output_file_keeps_everything_and_is_truncated_on_open() { + let service = TerminalService::default(); + let file = TempPath::new("full"); + fs::write(&file.0, "earlier output that must go\r\n").unwrap(); + let script = Script::powershell( + "full", + r#" + $lines = (1..20000) -join "`r`n" + [Console]::Out.Write($lines + "`r`n") + [Console]::Error.Write("done`r`n") + "#, + ); + let info = service + .create_pipe(script.request(&file.0, base_env())) + .unwrap(); + let exit = service.wait(info.id).unwrap().recv().unwrap(); + let expected = (1..=20000).map(|n| format!("{n}\r\n")).collect::() + "done\r\n"; + assert_eq!(fs::read_to_string(&file.0).unwrap(), expected); + assert_eq!(exit.final_offset, expected.len() as u64); + assert_eq!(exit.output_lines, Some(20001)); + assert_eq!(exit.output_error, None); + service.terminate(info.id).unwrap(); +} + +#[test] +fn rejected_requests_never_touch_the_output_file_or_run() { + let service = TerminalService::default(); + let missing = TempPath::new("missing-directory").0.join("out"); + let marker = TempPath::new("should-not-run"); + let target = TempPath::new("target"); + fs::write(&target.0, "kept").unwrap(); + let existing = TempPath::new("existing"); + fs::write(&existing.0, "kept").unwrap(); + let touch = format!("echo ran> \"{}\"", marker.0.display()); + let mut huge = PipeMetadata::new(); + huge.insert("command".into(), "x".repeat(MAX_METADATA_BYTES).into()); + let mut cases = vec![ + ( + CreatePipe { + output_path: PathBuf::from("relative.out"), + ..cmd(&touch, &existing.0) + }, + "must be absolute", + ), + ( + CreatePipe { + cwd: "relative".into(), + ..cmd(&touch, &existing.0) + }, + "cwd must be absolute", + ), + ( + CreatePipe { + metadata: huge, + ..cmd(&touch, &existing.0) + }, + "metadata exceeds", + ), + (cmd(&touch, &missing), "failed to open output file"), + ]; + let link = TempPath::new("link"); + let reparse = match std::os::windows::fs::symlink_file(&target.0, &link.0) { + Ok(()) => { + cases.push((cmd(&touch, &link.0), "failed to open output file")); + true + } + // Symbolic link creation needs Developer Mode or an elevated account. + Err(error) if error.raw_os_error() == Some(ERROR_PRIVILEGE_NOT_HELD as i32) => { + eprintln!("skipping the reparse point case: {error}"); + false + } + Err(error) => panic!("failed to create a symlink: {error}"), + }; + for (request, error) in cases { + let message = format!("{:#}", service.create_pipe(request).unwrap_err()); + assert!(message.contains(error), "{message}"); + } + assert!(service.list(ProcessMode::Pipe).unwrap().is_empty()); + assert_eq!(fs::read_to_string(&existing.0).unwrap(), "kept"); + assert_eq!(fs::read_to_string(&target.0).unwrap(), "kept"); + if reparse { + assert_eq!(fs::read_to_string(&link.0).unwrap(), "kept"); + } + // Give a wrongly spawned child time to run before checking. + std::thread::sleep(Duration::from_millis(100)); + assert!(!marker.0.exists()); +} + +#[test] +fn output_lines_count_newlines_in_the_file() { + let service = TerminalService::default(); + for (name, body, expected) in [ + ("two", r#"[Console]::Out.Write("a`nb`nc")"#, Some(2)), + ("blanks", r#"[Console]::Out.Write("a`n`n`nb`n")"#, Some(4)), + ("empty", r#"[Console]::Out.Write("")"#, Some(0)), + ( + "many", + r#"[Console]::Out.Write(((1..200000) -join "`n"))"#, + Some(199999), + ), + ( + "small", + r#"for ($i = 1; $i -le 2000; $i++) { [Console]::Out.WriteLine($i) }"#, + Some(2000), + ), + ( + "random", + r#" + $bytes = New-Object byte[] 300000 + (New-Object Random).NextBytes($bytes) + [Console]::OpenStandardOutput().Write($bytes, 0, $bytes.Length) + "#, + None, + ), + ] { + let script = Script::powershell(name, body); + let (info, file) = start(&service, |file| script.request(file, base_env())); + let exit = run(&service, info.id, &file).exit; + let bytes = fs::read(&file.0).unwrap(); + assert_eq!(exit.output_lines, Some(newlines(&bytes)), "{name}"); + if let Some(expected) = expected { + assert_eq!(exit.output_lines, Some(expected), "{name}"); + } + assert_eq!(exit.final_offset, bytes.len() as u64, "{name}"); + let listed = service.get(info.id).unwrap(); + assert_eq!( + listed.pipe.unwrap().output_lines, + exit.output_lines.unwrap() + ); + service.terminate(info.id).unwrap(); + } +} + +#[test] +fn wait_blocks_until_the_exit_and_answers_every_waiter() { + let service = TerminalService::default(); + let script = Script::powershell( + "wait", + r#" + Start-Sleep -Milliseconds 300 + [Console]::Out.WriteLine("done") + exit 2 + "#, + ); + let (info, file) = start(&service, |file| script.request(file, base_env())); + let waits = (0..3) + .map(|_| service.wait(info.id).unwrap()) + .collect::>(); + assert!(waits[0].exit.try_recv().is_err()); + let exits = waits + .iter() + .map(|wait| wait.recv().unwrap()) + .collect::>(); + assert!(exits.iter().all(|exit| *exit == exits[0])); + assert_eq!(exits[0].exit_code, Some(2)); + assert_eq!(exits[0].output_lines, Some(1)); + assert_eq!(fs::read_to_string(&file.0).unwrap(), "done\r\n"); + // After the exit, a new waiter is answered at once. + let started = Instant::now(); + assert_eq!(service.wait(info.id).unwrap().recv().unwrap(), exits[0]); + assert!(started.elapsed() < Duration::from_millis(500)); + service.terminate(info.id).unwrap(); + assert!(service.wait(info.id).is_err()); +} + +#[test] +fn terminate_ends_a_pending_wait_with_an_error() { + let service = TerminalService::default(); + let script = Script::powershell("sleep", "Start-Sleep -Seconds 30"); + let (info, _file) = start(&service, |file| script.request(file, base_env())); + let wait = service.wait(info.id).unwrap(); + service.terminate(info.id).unwrap(); + assert_eq!(wait.recv().unwrap_err().to_string(), "terminated"); +} + +#[test] +fn signal_stops_a_process_and_keeps_its_record() { + let service = TerminalService::default(); + let script = Script::powershell( + "signal", + r#" + [Console]::Out.WriteLine("started") + Start-Sleep -Seconds 30 + "#, + ); + let (info, file) = start(&service, |file| script.request(file, base_env())); + first_line(&file); + service.signal(info.id, ProcessSignal::Term).unwrap(); + let exit = service.wait(info.id).unwrap().recv().unwrap(); + assert_eq!(exit.exit_code, None); + assert_eq!(exit.signal.as_deref(), Some("SIGTERM")); + assert_eq!(exit.output_lines, Some(1)); + assert_eq!( + service.get(info.id).unwrap().lifecycle, + TerminalLifecycle::Exited { + exit_code: None, + signal: Some("SIGTERM".into()) + } + ); + // What is left of the job is killed again without an error. + service.signal(info.id, ProcessSignal::Kill).unwrap(); + service.terminate(info.id).unwrap(); + assert!(service.signal(info.id, ProcessSignal::Kill).is_err()); +} + +#[test] +fn signal_reaches_a_grandchild_in_the_job() { + let service = TerminalService::default(); + let pid_file = TempPath::new("grandchild-pid"); + let inner = Script::powershell( + "grandchild", + &format!( + r#" + [System.IO.File]::WriteAllText('{}', "$PID") + Start-Sleep -Seconds 60 + "#, + ps_path(&pid_file.0) + ), + ); + let outer = batch( + "grandchild-outer", + &[ + "@echo off".to_string(), + format!( + "start /b powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"{}\"", + inner.0.0.display() + ), + "ping -n 60 127.0.0.1".to_string(), + ], + ); + let (info, _file) = start(&service, |file| cmd(&outer.0.display().to_string(), file)); + let grandchild = read_pid(&pid_file.0); + assert!(alive(grandchild)); + service.signal(info.id, ProcessSignal::Term).unwrap(); + let exit = service.wait(info.id).unwrap().recv().unwrap(); + assert_eq!(exit.signal.as_deref(), Some("SIGTERM")); + wait_dead(grandchild); + service.terminate(info.id).unwrap(); +} + +#[test] +fn a_grandchild_holding_the_pipe_delays_the_exit_by_at_most_the_drain() { + let service = TerminalService::default(); + let (info, file) = start(&service, |file| cmd("start /b ping -n 60 127.0.0.1", file)); + let finished = run(&service, info.id, &file); + assert_eq!(finished.exit.exit_code, Some(0)); + assert!( + finished.elapsed < Duration::from_secs(10), + "{:?}", + finished.elapsed + ); + // Terminate kills the rest of the job. + service.terminate(info.id).unwrap(); +} + +#[test] +fn terminate_does_not_hang_on_a_grandchild() { + let service = TerminalService::default(); + let pid_file = TempPath::new("terminate-pid"); + let inner = Script::powershell( + "terminate-grandchild", + &format!( + r#" + [System.IO.File]::WriteAllText('{}', "$PID") + Start-Sleep -Seconds 60 + "#, + ps_path(&pid_file.0) + ), + ); + let outer = batch( + "terminate-outer", + &[ + "@echo off".to_string(), + format!( + "start /b powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"{}\"", + inner.0.0.display() + ), + "ping -n 60 127.0.0.1".to_string(), + ], + ); + let (info, _file) = start(&service, |file| cmd(&outer.0.display().to_string(), file)); + let grandchild = read_pid(&pid_file.0); + assert!(alive(grandchild)); + let started = Instant::now(); + service.terminate(info.id).unwrap(); + assert!( + started.elapsed() < Duration::from_secs(10), + "{:?}", + started.elapsed() + ); + wait_dead(grandchild); +} + +#[test] +fn a_noisy_grandchild_still_exits_after_the_drain() { + let service = TerminalService::default(); + let marker = TempPath::new("noisy-marker"); + let inner = Script::powershell( + "noisy-grandchild", + &format!( + r#" + # Prove the pipe is live before the parent exits. + [Console]::Out.Write('x') + [Console]::Out.Flush() + [System.IO.File]::WriteAllText('{}', 'started') + while ($true) {{ + [Console]::Out.Write('x') + }} + "#, + ps_path(&marker.0) + ), + ); + // A batch parent: it waits for the marker, so the grandchild is already + // writing when the parent exits and the drain deadline is exercised. + let outer = batch( + "noisy-outer", + &[ + "@echo off".to_string(), + format!( + "start /b powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"{}\"", + inner.0.0.display() + ), + ":wait".to_string(), + format!("if exist \"{}\" goto done", marker.0.display()), + "ping -n 2 127.0.0.1 >nul".to_string(), + "goto wait".to_string(), + ":done".to_string(), + "exit /b 0".to_string(), + ], + ); + let (info, file) = start(&service, |file| cmd(&outer.0.display().to_string(), file)); + let finished = run(&service, info.id, &file); + assert_eq!(finished.exit.exit_code, Some(0)); + assert!( + finished.elapsed < Duration::from_secs(10), + "{:?}", + finished.elapsed + ); + service.terminate(info.id).unwrap(); +} + +#[test] +fn signal_after_a_normal_exit_kills_a_leftover_grandchild() { + let service = TerminalService::default(); + let pid_file = TempPath::new("leftover-pid"); + let inner = Script::powershell( + "leftover-grandchild", + &format!( + r#" + [System.IO.File]::WriteAllText('{}', "$PID") + Start-Sleep -Seconds 60 + "#, + ps_path(&pid_file.0) + ), + ); + let outer = batch( + "leftover-outer", + &[ + "@echo off".to_string(), + format!( + "start /b powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"{}\"", + inner.0.0.display() + ), + ], + ); + let (info, _file) = start(&service, |file| cmd(&outer.0.display().to_string(), file)); + let grandchild = read_pid(&pid_file.0); + let exit = service.wait(info.id).unwrap().recv().unwrap(); + assert_eq!(exit.exit_code, Some(0)); + assert_eq!(exit.signal, None); + // The process keeps its record after the exit; the grandchild still runs. + assert!(alive(grandchild)); + assert_eq!( + service.get(info.id).unwrap().lifecycle, + TerminalLifecycle::Exited { + exit_code: Some(0), + signal: None + } + ); + assert_eq!(service.list(ProcessMode::Pipe).unwrap().len(), 1); + service.signal(info.id, ProcessSignal::Kill).unwrap(); + wait_dead(grandchild); + service.terminate(info.id).unwrap(); + assert!(service.list(ProcessMode::Pipe).unwrap().is_empty()); +} + +#[test] +fn the_stdout_survives_closing_stderr() { + let service = TerminalService::default(); + let script = Script::powershell( + "stderr", + r#"Add-Type -TypeDefinition @" +using System; +using System.Runtime.InteropServices; +public static class StandardHandles { + [DllImport("kernel32.dll")] + public static extern IntPtr GetStdHandle(int handle); + [DllImport("kernel32.dll")] + public static extern bool CloseHandle(IntPtr handle); +} +"@ +# STD_ERROR_HANDLE - stdout must be a separate handle to the same pipe. +[StandardHandles]::CloseHandle([StandardHandles]::GetStdHandle(-12)) | Out-Null +Start-Sleep -Milliseconds 200 +[Console]::Out.WriteLine("after-stderr-closed") +"#, + ); + let (info, file) = start(&service, |file| script.request(file, base_env())); + let finished = run(&service, info.id, &file); + assert_eq!(finished.output, "after-stderr-closed\r\n"); + assert_eq!(finished.exit.exit_code, Some(0)); + service.terminate(info.id).unwrap(); +} + +#[test] +fn arguments_round_trip_through_the_command_line() { + let service = TerminalService::default(); + let script = Script::powershell( + "quoting", + r#"for ($i = 0; $i -lt $args.Count; $i++) { [Console]::Out.WriteLine("$i<$($args[$i])>") }"#, + ); + // A trailing backslash needs doubling only when the argument is quoted. + let args = ["plain", "with space", "say \"hi\"", r"C:\with space\"]; + let (info, file) = start(&service, |file| { + let mut request = script.request(file, base_env()); + request.args.extend(args.iter().map(|arg| arg.to_string())); + request + }); + let finished = run(&service, info.id, &file); + assert_eq!( + finished.output, + "0\r\n1\r\n2\r\n3\r\n" + ); + assert_eq!(finished.exit.exit_code, Some(0)); + service.terminate(info.id).unwrap(); +} + +#[test] +fn the_child_has_no_console_window() { + let service = TerminalService::default(); + let script = Script::powershell( + "console", + r#"Add-Type -TypeDefinition @" +using System; +using System.Runtime.InteropServices; +public static class ConsoleWindow { + [DllImport("kernel32.dll")] + public static extern IntPtr GetConsoleWindow(); +} +"@ +$window = [ConsoleWindow]::GetConsoleWindow() +if ($window -eq [IntPtr]::Zero) { + [Console]::Out.WriteLine("no-console") +} else { + [Console]::Out.WriteLine("console") +} +"#, + ); + let (info, file) = start(&service, |file| script.request(file, base_env())); + let finished = run(&service, info.id, &file); + assert_eq!(finished.output, "no-console\r\n"); + assert_eq!(finished.exit.exit_code, Some(0)); + service.terminate(info.id).unwrap(); +} + +#[test] +fn the_output_file_can_be_read_and_deleted_while_open() { + let service = TerminalService::default(); + let script = Script::powershell( + "shared", + r#" + [Console]::Out.WriteLine("started") + Start-Sleep -Seconds 30 + "#, + ); + let (info, file) = start(&service, |file| script.request(file, base_env())); + first_line(&file); + assert!(fs::read_to_string(&file.0).unwrap().starts_with("started")); + fs::remove_file(&file.0).unwrap(); + service.terminate(info.id).unwrap(); +}