blob: e661008690298af4f56e1ea269790ef456ea0466 [file]
// Copyright 2026 The Fuchsia Authors
//
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
// This file may not be copied, modified, or distributed except according to
// those terms.
//! Process-group and subreaper subprocess supervision with replay fidelity.
//!
//! Replaces direct-child-only timeouts with process-group isolation
//! (`CommandExt::process_group(0)`), Linux `PR_SET_CHILD_SUBREAPER` grandchild
//! adoption, and synchronous `-pgid` reaping until `ECHILD`. Every executed
//! command writes `invocation.json`, `outcome.json`, `stdout`, `stderr`, and a
//! signal-forwarding `reproduce.py` wrapper into a fresh log slot.
use std::fs;
use std::path::{Component, Path};
use std::process::Child;
#[cfg(all(test, target_os = "linux"))]
use std::process::Command;
use std::sync::OnceLock;
use std::time::{Duration, Instant};
use serde_json::Value;
#[cfg(all(test, target_os = "linux"))]
use serde_json::json;
use crate::TestResult;
mod files;
mod reproduce;
mod supervise;
#[path = "process/supervisor.rs"]
mod supervisor;
#[cfg(all(test, target_os = "linux"))]
mod tests;
#[path = "process/types.rs"]
mod types;
pub(crate) use self::files::validate_single_component;
pub use self::{
files::{reject_symlink_components, write_json, write_regular_file},
supervisor::ProcessSupervisor,
types::{Invocation, ProcessOutcome, Termination},
};
/// Enable Linux `PR_SET_CHILD_SUBREAPER` once per test process so reparented
/// grandchildren (`lean`, `rustc`, `bwrap`, `ld`) stay waitable children of
/// the test runner rather than escaping to PID 1.
pub fn ensure_child_subreaper() -> TestResult {
static RESULT: OnceLock<Result<(), String>> = OnceLock::new();
RESULT
.get_or_init(|| {
#[cfg(target_os = "linux")]
{
// SAFETY: `prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0)` sets a
// process-wide kernel attribute with no pointer dereferences.
let rc = unsafe { libc::prctl(libc::PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) };
if rc != 0 {
return Err(format!(
"prctl(PR_SET_CHILD_SUBREAPER) failed: {}",
std::io::Error::last_os_error()
));
}
}
Ok(())
})
.clone()
}