Skip to main content

codex_utils_pty/
process_group.rs

1//! Process-group helpers shared by pipe/pty and shell command execution.
2//!
3//! This module centralizes the OS-specific pieces that ensure a spawned
4//! command can be cleaned up reliably:
5//! - `set_process_group` is called in `pre_exec` so the child starts its own
6//!   process group.
7//! - `detach_from_tty` starts a new session so non-interactive children do not
8//!   inherit the controlling TTY.
9//! - `kill_process_group_by_pid` targets the whole group (children/grandchildren)
10//! - `kill_process_group` targets a known process group ID directly
11//!   instead of a single PID.
12//! - `set_parent_death_signal` (Linux only) arranges for the child to receive a
13//!   `SIGTERM` when the parent exits, and re-checks the parent PID to avoid
14//!   races during fork/exec.
15//!
16//! On non-Unix platforms these helpers are no-ops.
17
18use std::io;
19
20use tokio::process::Child;
21
22#[cfg(target_os = "linux")]
23/// Ensure the child receives SIGTERM when the original parent dies.
24///
25/// This should run in `pre_exec` and uses `parent_pid` captured before spawn to
26/// avoid a race where the parent exits between fork and exec.
27pub fn set_parent_death_signal(parent_pid: libc::pid_t) -> io::Result<()> {
28    if unsafe { libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM) } == -1 {
29        return Err(io::Error::last_os_error());
30    }
31
32    if unsafe { libc::getppid() } != parent_pid {
33        unsafe {
34            libc::raise(libc::SIGTERM);
35        }
36    }
37
38    Ok(())
39}
40
41#[cfg(not(target_os = "linux"))]
42/// No-op on non-Linux platforms.
43pub fn set_parent_death_signal(_parent_pid: i32) -> io::Result<()> {
44    Ok(())
45}
46
47#[cfg(unix)]
48/// Detach from the controlling TTY by starting a new session.
49pub fn detach_from_tty() -> io::Result<()> {
50    let result = unsafe { libc::setsid() };
51    if result == -1 {
52        let err = io::Error::last_os_error();
53        if err.raw_os_error() == Some(libc::EPERM) {
54            return set_process_group();
55        }
56        return Err(err);
57    }
58    Ok(())
59}
60
61#[cfg(not(unix))]
62/// No-op on non-Unix platforms.
63pub fn detach_from_tty() -> io::Result<()> {
64    Ok(())
65}
66
67#[cfg(unix)]
68/// Put the calling process into its own process group.
69///
70/// Intended for use in `pre_exec` so the child becomes the group leader.
71pub fn set_process_group() -> io::Result<()> {
72    let result = unsafe { libc::setpgid(0, 0) };
73    if result == -1 {
74        Err(io::Error::last_os_error())
75    } else {
76        Ok(())
77    }
78}
79
80#[cfg(not(unix))]
81/// No-op on non-Unix platforms.
82pub fn set_process_group() -> io::Result<()> {
83    Ok(())
84}
85
86#[cfg(unix)]
87/// Kill the process group for the given PID (best-effort).
88///
89/// This resolves the PGID for `pid` and sends SIGKILL to the whole group.
90pub fn kill_process_group_by_pid(pid: u32) -> io::Result<()> {
91    use std::io::ErrorKind;
92
93    let pid = pid as libc::pid_t;
94    let pgid = unsafe { libc::getpgid(pid) };
95    if pgid == -1 {
96        let err = io::Error::last_os_error();
97        if err.kind() != ErrorKind::NotFound && err.raw_os_error() != Some(libc::ESRCH) {
98            return Err(err);
99        }
100        return Ok(());
101    }
102
103    let result = unsafe { libc::killpg(pgid, libc::SIGKILL) };
104    if result == -1 {
105        let err = io::Error::last_os_error();
106        if err.kind() != ErrorKind::NotFound && err.raw_os_error() != Some(libc::ESRCH) {
107            return Err(err);
108        }
109    }
110
111    Ok(())
112}
113
114#[cfg(not(unix))]
115/// No-op on non-Unix platforms.
116pub fn kill_process_group_by_pid(_pid: u32) -> io::Result<()> {
117    Ok(())
118}
119
120#[cfg(unix)]
121fn signal_process_group_id(pgid: libc::pid_t, signal: libc::c_int) -> io::Result<bool> {
122    use std::io::ErrorKind;
123
124    let result = unsafe { libc::killpg(pgid, signal) };
125    if result == -1 {
126        let err = io::Error::last_os_error();
127        if err.kind() == ErrorKind::NotFound || err.raw_os_error() == Some(libc::ESRCH) {
128            return Ok(false);
129        }
130        return Err(err);
131    }
132
133    Ok(true)
134}
135
136#[cfg(unix)]
137/// Send SIGTERM to a specific process group ID (best-effort).
138///
139/// Returns `Ok(true)` when SIGTERM was delivered to an existing group and
140/// `Ok(false)` when the group no longer exists.
141pub fn terminate_process_group(process_group_id: u32) -> io::Result<bool> {
142    signal_process_group_id(process_group_id as libc::pid_t, libc::SIGTERM)
143}
144
145#[cfg(not(unix))]
146/// No-op on non-Unix platforms.
147pub fn terminate_process_group(_process_group_id: u32) -> io::Result<bool> {
148    Ok(false)
149}
150
151#[cfg(unix)]
152/// Send SIGINT to a specific process group ID (best-effort).
153pub fn interrupt_process_group(process_group_id: u32) -> io::Result<()> {
154    signal_process_group_id(process_group_id as libc::pid_t, libc::SIGINT).map(|_| ())
155}
156
157#[cfg(not(unix))]
158/// No-op on non-Unix platforms.
159pub fn interrupt_process_group(_process_group_id: u32) -> io::Result<()> {
160    Ok(())
161}
162
163#[cfg(unix)]
164/// Kill a specific process group ID (best-effort).
165pub fn kill_process_group(process_group_id: u32) -> io::Result<()> {
166    signal_process_group_id(process_group_id as libc::pid_t, libc::SIGKILL).map(|_| ())
167}
168
169#[cfg(not(unix))]
170/// No-op on non-Unix platforms.
171pub fn kill_process_group(_process_group_id: u32) -> io::Result<()> {
172    Ok(())
173}
174
175#[cfg(unix)]
176/// Kill the process group for a tokio child (best-effort).
177pub fn kill_child_process_group(child: &mut Child) -> io::Result<()> {
178    if let Some(pid) = child.id() {
179        return kill_process_group_by_pid(pid);
180    }
181
182    Ok(())
183}
184
185#[cfg(not(unix))]
186/// No-op on non-Unix platforms.
187pub fn kill_child_process_group(_child: &mut Child) -> io::Result<()> {
188    Ok(())
189}