systemprompt_models/subprocess/
mod.rs1use std::process::Command;
44use std::sync::OnceLock;
45use std::sync::mpsc::{Sender, channel};
46
47#[cfg(target_os = "linux")]
48mod linux;
49#[cfg(target_os = "linux")]
50pub use linux::{is_zombie, live_pid_is_subprocess};
51
52#[cfg(target_os = "macos")]
53mod darwin;
54#[cfg(target_os = "macos")]
55pub use darwin::{is_zombie, live_pid_is_subprocess};
56
57#[cfg(not(any(target_os = "linux", target_os = "macos")))]
58mod unsupported;
59#[cfg(not(any(target_os = "linux", target_os = "macos")))]
60pub use unsupported::{is_zombie, live_pid_is_subprocess};
61
62pub const SUBPROCESS_MARKER_ENV: &str = "SYSTEMPROMPT_SUBPROCESS";
63pub const AGENT_NAME_ENV: &str = "AGENT_NAME";
64pub const MCP_SERVICE_ID_ENV: &str = "MCP_SERVICE_ID";
65
66type SpawnReply = Sender<std::io::Result<u32>>;
67
68pub fn spawn_supervised(cmd: Command) -> std::io::Result<u32> {
69 let sender = spawner()
70 .as_ref()
71 .map_err(|e| std::io::Error::other(e.clone()))?;
72
73 let (reply_tx, reply_rx) = channel();
74 sender
75 .send((cmd, reply_tx))
76 .map_err(|disconnected| std::io::Error::other(disconnected.to_string()))?;
77 reply_rx
78 .recv()
79 .map_err(|disconnected| std::io::Error::other(disconnected.to_string()))?
80}
81
82fn spawner() -> &'static Result<Sender<(Command, SpawnReply)>, String> {
83 static SPAWNER: OnceLock<Result<Sender<(Command, SpawnReply)>, String>> = OnceLock::new();
84 SPAWNER.get_or_init(|| {
85 let (tx, rx) = channel::<(Command, SpawnReply)>();
86 std::thread::Builder::new()
87 .name("subprocess-spawner".to_owned())
88 .spawn(move || {
89 while let Ok((mut cmd, reply)) = rx.recv() {
90 let outcome = spawn_on_this_thread(&mut cmd);
91 if reply.send(outcome).is_err() {
92 tracing::warn!(
93 "Spawn requester vanished before collecting the child pid; the child \
94 is unregistered and will only be cleaned up by its parent-death signal"
95 );
96 }
97 }
98 })
99 .map(|_handle| tx)
100 .map_err(|e| format!("could not start the subprocess spawner thread: {e}"))
101 })
102}
103
104fn spawn_on_this_thread(cmd: &mut Command) -> std::io::Result<u32> {
105 #[cfg(target_os = "linux")]
106 linux::arm_parent_death_signal(cmd);
107
108 let child = cmd.spawn()?;
109 let pid = child.id();
110 #[expect(
111 clippy::mem_forget,
112 reason = "detached child: skip Child's drop-time wait so it keeps running after this \
113 returns; reaping is the caller's business via is_zombie"
114 )]
115 std::mem::forget(child);
116 Ok(pid)
117}
118
119#[cfg(unix)]
123pub fn place_in_own_process_group(command: &mut Command) {
124 use std::os::unix::process::CommandExt;
125 command.process_group(0);
126}
127
128#[cfg(windows)]
129pub fn place_in_own_process_group(command: &mut Command) {
130 use std::os::windows::process::CommandExt;
131 const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
132 command.creation_flags(CREATE_NEW_PROCESS_GROUP);
133}
134
135#[must_use]
136pub const fn identity_verification_supported() -> bool {
137 cfg!(any(target_os = "linux", target_os = "macos"))
138}
139
140#[must_use]
141pub fn signalable_pid(pid: u32) -> Option<i32> {
142 if pid == 0 {
143 return None;
144 }
145 i32::try_from(pid).ok()
146}
147
148#[must_use]
149pub fn environ_identifies_child(environ: &[u8], name_key: &str, service_name: &str) -> bool {
150 let marker = format!("{SUBPROCESS_MARKER_ENV}=1");
151 let expected_name = format!("{name_key}={service_name}");
152
153 let mut has_marker = false;
154 let mut has_name = false;
155 for entry in environ.split(|&b| b == 0) {
156 if entry == marker.as_bytes() {
157 has_marker = true;
158 } else if entry == expected_name.as_bytes() {
159 has_name = true;
160 }
161 }
162
163 has_marker && has_name
164}
165
166#[must_use]
174pub fn environ_from_procargs2(blob: &[u8]) -> Option<&[u8]> {
175 const ARGC_LEN: usize = size_of::<i32>();
176
177 let argc_bytes: [u8; ARGC_LEN] = blob.get(..ARGC_LEN)?.try_into().ok()?;
178 let argc = usize::try_from(i32::from_ne_bytes(argc_bytes)).ok()?;
179
180 let mut rest = blob.get(ARGC_LEN..)?;
181 let exec_path_end = rest.iter().position(|&b| b == 0)?;
182 rest = rest.get(exec_path_end + 1..)?;
183
184 let argv_start = rest.iter().position(|&b| b != 0)?;
185 rest = rest.get(argv_start..)?;
186
187 for _ in 0..argc {
188 let entry_end = rest.iter().position(|&b| b == 0)?;
189 rest = rest.get(entry_end + 1..)?;
190 }
191
192 Some(rest)
193}