use std::cell::Cell;
use std::ffi::OsString;
use std::io;
use std::os::unix::process::CommandExt;
use std::process::{Child, Command, Stdio};
use std::rc::Rc;
use calloop::Interest;
use rustix::process::{Pid, PidfdFlags};
use crate::Shared;
pub(crate) fn spawn(shared: &Rc<Shared>, command: &mut Command) -> io::Result<u32> {
let program = command.get_program().to_string_lossy().into_owned();
untune(command);
let child = command
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.process_group(0)
.spawn()
.inspect_err(|error| tracing::warn!(%error, program, "couldn't start a program"))?;
let pid = child.id();
reap(shared, child, program);
Ok(pid)
}
const TUNABLE: &str = "glibc.malloc.mmap_threshold=131072";
#[doc(hidden)]
pub const FIRST_START: &str = "CRABSHELL_STARTED";
#[doc(hidden)]
pub fn shell_tunables(old: Option<OsString>) -> Option<OsString> {
match old {
Some(old)
if old
.to_string_lossy()
.contains("glibc.malloc.mmap_threshold") =>
{
None
}
Some(mut old) if !old.is_empty() => {
old.push(":");
old.push(TUNABLE);
Some(old)
}
_ => Some(TUNABLE.into()),
}
}
pub(crate) fn untune(command: &mut Command) {
untune_from(command, std::env::var_os("GLIBC_TUNABLES"));
}
fn untune_from(command: &mut Command, ours: Option<OsString>) {
command.env_remove("CRABSHELL_CONTROL");
command.env_remove(FIRST_START);
for name in ["XDG_ACTIVATION_TOKEN", "DESKTOP_STARTUP_ID"] {
if !command.get_envs().any(|(set, _)| set == name) {
command.env_remove(name);
}
}
if ours.is_some_and(|value| value == TUNABLE) {
command.env_remove("GLIBC_TUNABLES");
}
}
pub(crate) fn reap(shared: &Rc<Shared>, mut child: Child, program: String) {
let pidfd = match rustix::process::pidfd_open(Pid::from_child(&child), PidfdFlags::empty()) {
Ok(pidfd) => pidfd,
Err(error) => {
tracing::warn!(%error, program, "can't watch a program, so it stays a zombie once it exits");
return;
}
};
let token = Rc::new(Cell::new(None));
let reap = {
let (shared, token) = (Rc::clone(shared), Rc::clone(&token));
Box::new(move || {
match child.wait() {
Ok(status) => tracing::debug!(program, %status, "a program ended"),
Err(error) => tracing::warn!(%error, program, "couldn't reap a program"),
}
if let (Some(token), Some(event_loop)) = (token.take(), shared.event_loop()) {
event_loop.remove(token);
}
})
};
if let Some(event_loop) = shared.event_loop() {
token.set(event_loop.insert_fd(pidfd, Interest::READ, reap));
}
}
#[cfg(test)]
mod tests {
use std::process::Command;
use std::rc::Rc;
use std::time::{Duration, Instant};
use super::spawn;
use crate::Shared;
use crate::testing::{Harness, capture_logs, offline};
fn shared(shell: &Harness) -> Rc<Shared> {
Rc::new(Shared::new(
shell.cx.owner(),
&shell.event_loop.handle(),
offline(),
))
}
#[test]
fn a_program_runs_apart_and_is_reaped_when_it_exits() {
let mut shell = Harness::new(offline());
let shared = shared(&shell);
let pid = spawn(&shared, Command::new("sleep").arg("0.3")).expect("sleep starts");
let proc = format!("/proc/{pid}");
let stat = std::fs::read_to_string(format!("{proc}/stat")).expect("it runs");
let fields: Vec<&str> = stat
.rsplit_once(')')
.expect("a stat line")
.1
.split_whitespace()
.collect();
assert_eq!(fields[2], pid.to_string(), "its own process group: {stat}");
for fd in 0..3 {
let target = std::fs::read_link(format!("{proc}/fd/{fd}")).expect("an fd");
assert_eq!(target, std::path::Path::new("/dev/null"), "fd {fd}");
}
let wakeups = shell.run_until(Instant::now() + Duration::from_millis(800));
assert!(wakeups <= 2, "{wakeups} wakeups");
assert!(
!std::path::Path::new(&proc).exists(),
"reaped, not a zombie"
);
let wakeups = shell.run_until(Instant::now() + Duration::from_millis(200));
assert_eq!(wakeups, 1);
}
#[test]
fn a_program_that_cant_start_is_logged_and_returned() {
let shell = Harness::new(offline());
let shared = shared(&shell);
let logs = capture_logs(|| {
let started = spawn(&shared, &mut Command::new("/nonexistent/crabshell-program"));
assert!(started.is_err());
});
assert!(logs.contains("couldn't start"), "{logs}");
assert!(logs.contains("/nonexistent/crabshell-program"), "{logs}");
}
#[test]
fn the_shell_gets_a_fixed_mmap_threshold_added_to_the_users_tunables() {
use super::{TUNABLE, shell_tunables};
let tunables = |old: Option<&str>| shell_tunables(old.map(Into::into));
assert_eq!(tunables(None), Some(TUNABLE.into()));
assert_eq!(tunables(Some("")), Some(TUNABLE.into()));
assert_eq!(
tunables(Some("glibc.malloc.arena_max=2")),
Some(format!("glibc.malloc.arena_max=2:{TUNABLE}").into())
);
let own = "glibc.malloc.mmap_threshold=65536";
assert_eq!(tunables(Some(own)), None, "the user's own wins");
}
#[test]
fn the_shells_own_glibc_tunable_isnt_passed_on() {
use std::ffi::OsStr;
let removed = |ours: Option<&str>| {
let mut command = Command::new("true");
super::untune_from(&mut command, ours.map(Into::into));
command
.get_envs()
.any(|(name, value)| name == OsStr::new("GLIBC_TUNABLES") && value.is_none())
};
assert!(removed(Some(super::TUNABLE)));
assert!(!removed(Some("glibc.malloc.arena_max=2")), "the user's own");
assert!(!removed(None));
let mut command = Command::new("true");
super::untune_from(&mut command, None);
let control = command
.get_envs()
.any(|(name, value)| name == OsStr::new("CRABSHELL_CONTROL") && value.is_none());
assert!(control, "the supervisor's socket isn't passed on");
let started = command
.get_envs()
.any(|(name, value)| name == OsStr::new(super::FIRST_START) && value.is_none());
assert!(started, "the shell's first start isn't passed on");
}
#[test]
fn the_shells_activation_token_isnt_passed_on_but_one_set_is() {
use std::ffi::OsStr;
let token = |command: &Command, name: &str| {
let set = command.get_envs().find(|(set, _)| *set == OsStr::new(name));
set.map(|(_, value)| value.map(OsStr::to_owned))
};
let mut command = Command::new("true");
super::untune_from(&mut command, None);
for name in ["XDG_ACTIVATION_TOKEN", "DESKTOP_STARTUP_ID"] {
assert_eq!(token(&command, name), Some(None), "{name} removed");
}
let mut command = Command::new("true");
crate::activation::set_env(&mut command, "ours");
super::untune_from(&mut command, None);
for name in ["XDG_ACTIVATION_TOKEN", "DESKTOP_STARTUP_ID"] {
assert_eq!(
token(&command, name),
Some(Some("ours".into())),
"{name} kept"
);
}
}
}