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
66pub const DEPLOYMENT_HOST_ENV: &str = "SYSTEMPROMPT_DEPLOYMENT_HOST";
71
72const FLY_HOST_ENV: &str = "FLY_APP_NAME";
75
76pub fn deployment_host(lookup: impl Fn(&str) -> Option<String>) -> Option<String> {
80 [DEPLOYMENT_HOST_ENV, FLY_HOST_ENV].iter().find_map(|name| {
81 lookup(name)
82 .map(|value| value.trim().to_owned())
83 .filter(|value| !value.is_empty())
84 })
85}
86
87pub fn is_deployment_host(lookup: impl Fn(&str) -> Option<String>) -> bool {
92 deployment_host(lookup).is_some()
93}
94
95pub fn inherited_parent_env(lookup: impl Fn(&str) -> Option<String>) -> Vec<(String, String)> {
99 let mut env: Vec<(String, String)> = [DEPLOYMENT_HOST_ENV, FLY_HOST_ENV, "PATH", "HOME"]
100 .iter()
101 .filter_map(|name| lookup(name).map(|value| ((*name).to_owned(), value)))
102 .collect();
103
104 if let Some(entry) = crate::net::trusted_hosts_env_entry(&lookup) {
105 env.push(entry);
106 }
107
108 env
109}
110
111type SpawnReply = Sender<std::io::Result<u32>>;
112
113pub fn spawn_supervised(cmd: Command) -> std::io::Result<u32> {
114 let sender = spawner()
115 .as_ref()
116 .map_err(|e| std::io::Error::other(e.clone()))?;
117
118 let (reply_tx, reply_rx) = channel();
119 sender
120 .send((cmd, reply_tx))
121 .map_err(|disconnected| std::io::Error::other(disconnected.to_string()))?;
122 reply_rx
123 .recv()
124 .map_err(|disconnected| std::io::Error::other(disconnected.to_string()))?
125}
126
127fn spawner() -> &'static Result<Sender<(Command, SpawnReply)>, String> {
128 static SPAWNER: OnceLock<Result<Sender<(Command, SpawnReply)>, String>> = OnceLock::new();
129 SPAWNER.get_or_init(|| {
130 let (tx, rx) = channel::<(Command, SpawnReply)>();
131 std::thread::Builder::new()
132 .name("subprocess-spawner".to_owned())
133 .spawn(move || {
134 while let Ok((mut cmd, reply)) = rx.recv() {
135 let outcome = spawn_on_this_thread(&mut cmd);
136 if reply.send(outcome).is_err() {
137 tracing::warn!(
138 "Spawn requester vanished before collecting the child pid; the child \
139 is unregistered and will only be cleaned up by its parent-death signal"
140 );
141 }
142 }
143 })
144 .map(|_handle| tx)
145 .map_err(|e| format!("could not start the subprocess spawner thread: {e}"))
146 })
147}
148
149fn spawn_on_this_thread(cmd: &mut Command) -> std::io::Result<u32> {
150 #[cfg(target_os = "linux")]
151 linux::arm_parent_death_signal(cmd);
152
153 let child = cmd.spawn()?;
154 let pid = child.id();
155 #[expect(
156 clippy::mem_forget,
157 reason = "detached child: skip Child's drop-time wait so it keeps running after this \
158 returns; reaping is the caller's business via is_zombie"
159 )]
160 std::mem::forget(child);
161 Ok(pid)
162}
163
164#[cfg(unix)]
168pub fn place_in_own_process_group(command: &mut Command) {
169 use std::os::unix::process::CommandExt;
170 command.process_group(0);
171}
172
173#[cfg(windows)]
174pub fn place_in_own_process_group(command: &mut Command) {
175 use std::os::windows::process::CommandExt;
176 const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
177 command.creation_flags(CREATE_NEW_PROCESS_GROUP);
178}
179
180#[must_use]
181pub const fn identity_verification_supported() -> bool {
182 cfg!(any(target_os = "linux", target_os = "macos"))
183}
184
185#[must_use]
186pub fn signalable_pid(pid: u32) -> Option<i32> {
187 if pid == 0 {
188 return None;
189 }
190 i32::try_from(pid).ok()
191}
192
193#[must_use]
194pub fn environ_identifies_child(environ: &[u8], name_key: &str, service_name: &str) -> bool {
195 let marker = format!("{SUBPROCESS_MARKER_ENV}=1");
196 let expected_name = format!("{name_key}={service_name}");
197
198 let mut has_marker = false;
199 let mut has_name = false;
200 for entry in environ.split(|&b| b == 0) {
201 if entry == marker.as_bytes() {
202 has_marker = true;
203 } else if entry == expected_name.as_bytes() {
204 has_name = true;
205 }
206 }
207
208 has_marker && has_name
209}
210
211#[must_use]
219pub fn environ_from_procargs2(blob: &[u8]) -> Option<&[u8]> {
220 const ARGC_LEN: usize = size_of::<i32>();
221
222 let argc_bytes: [u8; ARGC_LEN] = blob.get(..ARGC_LEN)?.try_into().ok()?;
223 let argc = usize::try_from(i32::from_ne_bytes(argc_bytes)).ok()?;
224
225 let mut rest = blob.get(ARGC_LEN..)?;
226 let exec_path_end = rest.iter().position(|&b| b == 0)?;
227 rest = rest.get(exec_path_end + 1..)?;
228
229 let argv_start = rest.iter().position(|&b| b != 0)?;
230 rest = rest.get(argv_start..)?;
231
232 for _ in 0..argc {
233 let entry_end = rest.iter().position(|&b| b == 0)?;
234 rest = rest.get(entry_end + 1..)?;
235 }
236
237 Some(rest)
238}