use std::ffi::{OsStr, OsString};
use tokio::process::Command;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Environment {
pass: Option<Vec<String>>,
set: Vec<(OsString, OsString)>,
}
impl Environment {
pub fn inherit() -> Self {
Self::default()
}
pub fn only<I, S>(names: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
pass: Some(names.into_iter().map(Into::into).collect()),
set: Vec::new(),
}
}
#[must_use]
pub fn set(mut self, name: impl Into<OsString>, value: impl Into<OsString>) -> Self {
let name = name.into();
self.set.retain(|(set, _)| *set != name);
self.set.push((name, value.into()));
self
}
pub fn inherits(&self) -> bool {
self.pass.is_none()
}
pub fn passes(&self, name: &str) -> bool {
self.pass.as_ref().is_none_or(|pass| {
pass.iter().any(|pattern| match pattern.strip_suffix('*') {
Some(prefix) => name.starts_with(prefix),
None => pattern == name,
})
})
}
pub(crate) fn apply(&self, command: &mut Command) {
self.apply_from(command, std::env::vars_os());
}
fn apply_from(
&self,
command: &mut Command,
host: impl IntoIterator<Item = (OsString, OsString)>,
) {
if self.pass.is_some() {
command.env_clear();
for (name, value) in host {
if name.to_str().is_some_and(|name| self.passes(name)) {
command.env(name, value);
}
}
}
for (name, value) in &self.set {
command.env(name, value);
}
}
pub(crate) fn value(&self, name: &str) -> Option<OsString> {
match self
.set
.iter()
.rev()
.find(|(set, _)| set == OsStr::new(name))
{
Some((_, value)) => Some(value.clone()),
None if self.passes(name) => std::env::var_os(name),
None => None,
}
}
}
pub(crate) fn die_with_parent(command: &mut Command) {
#[cfg(target_os = "linux")]
{
let parent = std::process::id();
unsafe {
command.pre_exec(move || {
if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL) != 0 {
return Err(std::io::Error::last_os_error());
}
if u32::try_from(libc::getppid()).ok() != Some(parent) {
return Err(std::io::Error::from_raw_os_error(libc::ESRCH));
}
Ok(())
});
}
}
#[cfg(not(target_os = "linux"))]
let _ = command;
}
#[cfg(all(test, unix))]
pub(crate) fn write_executable(path: &std::path::Path, script: &str) {
use std::io::Write as _;
let mut writer = std::process::Command::new("sh")
.args(["-c", "cat > \"$1\" && chmod 700 \"$1\"", "sh"])
.arg(path)
.stdin(std::process::Stdio::piped())
.spawn()
.unwrap();
writer
.stdin
.take()
.unwrap()
.write_all(script.as_bytes())
.unwrap();
assert!(writer.wait().unwrap().success());
}
#[cfg(all(test, target_os = "linux"))]
mod tests {
use std::time::Duration;
fn alive(pid: u32) -> bool {
std::fs::read_to_string(format!("/proc/{pid}/stat"))
.is_ok_and(|stat| !stat.rsplit(") ").next().unwrap_or("").starts_with('Z'))
}
#[test]
fn a_child_dies_with_the_thread_that_spawned_it() {
let pid = std::thread::spawn(|| {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
runtime.block_on(async {
let mut command = tokio::process::Command::new("sleep");
command.arg("30");
super::die_with_parent(&mut command);
let child = command.spawn().unwrap();
let pid = child.id().unwrap();
std::mem::forget(child);
pid
})
})
.join()
.unwrap();
let deadline = std::time::Instant::now() + Duration::from_secs(5);
while alive(pid) && std::time::Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(20));
}
assert!(!alive(pid), "the child outlived its parent");
}
}
#[cfg(all(test, unix))]
mod environment_tests {
use std::collections::BTreeMap;
use std::ffi::OsString;
use std::path::PathBuf;
use super::Environment;
fn env_program() -> PathBuf {
let path = std::env::var_os("PATH").unwrap();
std::env::split_paths(&path)
.map(|dir| dir.join("env"))
.find(|candidate| candidate.is_file())
.unwrap()
}
async fn seen(
environment: &Environment,
host: Option<&[(&str, &str)]>,
) -> BTreeMap<String, String> {
let mut command = tokio::process::Command::new(env_program());
match host {
Some(host) => environment.apply_from(
&mut command,
host.iter()
.map(|(name, value)| (OsString::from(name), OsString::from(value))),
),
None => environment.apply(&mut command),
}
let output = command.output().await.unwrap();
assert!(output.status.success());
String::from_utf8(output.stdout)
.unwrap()
.lines()
.filter_map(|line| line.split_once('='))
.map(|(name, value)| (name.to_owned(), value.to_owned()))
.collect()
}
#[tokio::test]
async fn a_process_sees_only_the_allowed_environment() {
let host = [
("PATH", "/usr/bin"),
("HOME", "/home/someone"),
("LC_ALL", "C.UTF-8"),
("LC_TIME", "en_GB.UTF-8"),
("TERM", "xterm-256color"),
("DATABASE_URL", "postgres://secret"),
("APP_SECRET_KEY", "secret"),
("XLC_ALL", "not a prefix match"),
];
let environment = Environment::only(["PATH", "HOME", "LC_*", "TERM", "UNSET"])
.set("TERM", "dumb")
.set("RUN_URL", "http://127.0.0.1:1");
let seen = seen(&environment, Some(&host)).await;
let expected: BTreeMap<String, String> = [
("PATH", "/usr/bin"),
("HOME", "/home/someone"),
("LC_ALL", "C.UTF-8"),
("LC_TIME", "en_GB.UTF-8"),
("TERM", "dumb"),
("RUN_URL", "http://127.0.0.1:1"),
]
.into_iter()
.map(|(name, value)| (name.to_owned(), value.to_owned()))
.collect();
assert_eq!(seen, expected);
}
#[tokio::test]
async fn by_default_a_process_inherits_the_host_environment() {
let environment = Environment::default().set("STUBB_TEST_EXTRA", "1");
assert!(environment.inherits());
let seen = seen(&environment, None).await;
assert_eq!(
seen.get("PATH").map(OsString::from),
std::env::var_os("PATH")
);
assert_eq!(seen.get("STUBB_TEST_EXTRA").map(String::as_str), Some("1"));
}
#[test]
fn a_value_comes_from_what_is_set_then_what_is_passed() {
let environment = Environment::only(["HOME"])
.set("CLAUDE_CONFIG_DIR", "/a")
.set("CLAUDE_CONFIG_DIR", "/b");
assert_eq!(environment.value("CLAUDE_CONFIG_DIR"), Some("/b".into()));
assert_eq!(environment.value("HOME"), std::env::var_os("HOME"));
assert_eq!(environment.value("PATH"), None);
assert!(environment.passes("HOME"));
assert!(!environment.passes("HOMEDIR"));
}
}