use super::Console;
pub(crate) const REAPED_ELSEWHERE: &str = "was reaped elsewhere";
#[track_caller]
pub(crate) fn assert_one_reaped_elsewhere_warn(mark: usize, pgid: u32) {
let warns = crate::log_capture::records_since_on_current_thread(mark, REAPED_ELSEWHERE);
assert_eq!(warns.len(), 1, "exactly one record, got {warns:?}");
assert_eq!(warns[0].0, log::Level::Warn, "{warns:?}");
assert!(
warns[0].1.contains(&format!("pgid {pgid}")),
"it names the group: {warns:?}"
);
}
#[cfg(target_os = "macos")]
#[track_caller]
pub(crate) fn assert_one_reaped_elsewhere_walk_warn(mark: usize, pid: u32) {
let warns = crate::log_capture::records_since_on_current_thread(mark, REAPED_ELSEWHERE);
assert_eq!(warns.len(), 1, "exactly one record, got {warns:?}");
assert_eq!(warns[0].0, log::Level::Warn, "{warns:?}");
assert!(
warns[0]
.1
.contains(&format!("walk the process table from the root's pid (root pid {pid})")),
"it names the walk: {warns:?}"
);
}
pub(crate) const UNTRUSTED_NUMBER: &str = "may name another process";
#[cfg(target_os = "linux")]
pub(crate) const ROOT_KILLED_BY_PIDFD: &str = "killed the root through its pidfd";
#[cfg(target_os = "linux")]
pub(crate) const ROOT_TERMED_BY_PIDFD: &str = "SIGTERM to the root through its pidfd";
#[track_caller]
pub(crate) fn assert_one_untrusted_number_warn(mark: usize, pgid: u32) {
let warns = crate::log_capture::records_since_on_current_thread(mark, UNTRUSTED_NUMBER);
assert_eq!(warns.len(), 1, "exactly one record, got {warns:?}");
assert_eq!(warns[0].0, log::Level::Warn, "{warns:?}");
assert!(
warns[0].1.contains(&format!("pgid {pgid}")),
"it names the group: {warns:?}"
);
}
pub(crate) fn session_sleep() -> crate::Command {
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(["sleep", "300"]);
cmd.contain_with(crate::ContainMode::Session);
cmd
}
#[cfg(target_os = "linux")]
pub(crate) fn session_cat() -> crate::Command {
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(["cat"]);
cmd.contain_with(crate::ContainMode::Session);
cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
cmd
}
#[cfg(target_os = "linux")]
pub(crate) fn term_ignoring_session_cat() -> crate::Command {
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(["sh", "-c", r#"trap "" TERM; echo ready; exec cat"#]);
cmd.contain_with(crate::ContainMode::Session);
cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
cmd.stdout(crate::Stdio::pipe_out()).expect("stdout pipe");
cmd
}
pub(crate) fn kill_own_child(pid: u32) {
#[cfg(debug_assertions)]
{
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
#[cfg(target_os = "linux")]
let all_kinds = libc::__WALL;
#[cfg(not(target_os = "linux"))]
let all_kinds = 0;
let peeked = unsafe {
libc::waitid(
libc::P_PID,
pid as libc::id_t,
&mut info,
libc::WEXITED | libc::WNOHANG | libc::WNOWAIT | all_kinds,
)
};
assert_eq!(
peeked,
0,
"pid {pid} is no unreaped child of this process: {}",
std::io::Error::last_os_error()
);
}
let sent = unsafe { libc::kill(pid as libc::pid_t, libc::SIGKILL) };
assert_eq!(sent, 0, "{}", std::io::Error::last_os_error());
}
pub(crate) fn reap_behind(pid: u32) {
reap_behind_status(pid);
}
pub(crate) fn reap_behind_status(pid: u32) -> std::process::ExitStatus {
use std::os::unix::process::ExitStatusExt as _;
super::wait_until_zombie(pid);
let mut status = 0;
let reaped = unsafe { libc::waitpid(pid as libc::pid_t, &mut status, 0) };
assert_eq!(reaped, pid as libc::pid_t, "{}", std::io::Error::last_os_error());
std::process::ExitStatus::from_raw(status)
}
pub(crate) fn foreign_reaped_group_root() -> (crate::Child, u32) {
let child = session_sleep().spawn().expect("spawn");
let pid = child.pid();
kill_own_child(pid);
reap_behind(pid);
(child, pid)
}
#[cfg(feature = "tokio")]
pub(crate) const MAY_STILL_RUN: &str = "may still be running";
#[cfg(target_os = "macos")]
pub(crate) fn escaped_holder_script(ignore_term: bool) -> String {
let ignore = if ignore_term { r#"$SIG{TERM} = "IGNORE"; "# } else { "" };
format!(
r#"exec 3<&0
perl -MPOSIX -e '{ignore}POSIX::setsid() > 0 or die "setsid: $!"; $| = 1; print "$$\n"; open(STDOUT, ">", "/dev/null") or die; exec("cat") or die' <&3 3<&- &
exec 3<&-
exec sleep 300"#
)
}
#[cfg(target_os = "macos")]
#[track_caller]
pub(crate) fn assert_left_the_group(holder: u32, root: u32) {
let pgid = unsafe { libc::getpgid(holder as libc::pid_t) };
assert_eq!(
pgid,
holder as libc::pid_t,
"the holder must lead its own group, not the root's ({root}): {}",
std::io::Error::last_os_error()
);
}
#[cfg(target_os = "macos")]
pub(crate) struct EscapedHolder {
pub(crate) pid: u32,
_stdin: crate::PipeWriter,
stderr: crate::PipeReader,
}
#[cfg(target_os = "macos")]
impl EscapedHolder {
pub(crate) fn assert_dies(mut self) {
let mut rest = Vec::new();
std::io::Read::read_to_end(&mut self.stderr, &mut rest).expect("the holder's stderr reaches EOF");
assert!(rest.is_empty(), "unexpected stderr from the holder: {rest:?}");
}
}
#[cfg(target_os = "macos")]
pub(crate) fn escaped_holder_tree(mode: crate::ContainMode, ignore_term: bool) -> (crate::Child, EscapedHolder) {
use std::io::BufRead as _;
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(["sh", "-c", &escaped_holder_script(ignore_term)]);
cmd.contain_with(mode);
cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
cmd.stdout(crate::Stdio::pipe_out()).expect("stdout pipe");
cmd.stderr(crate::Stdio::pipe_out()).expect("stderr pipe");
let mut child = cmd.spawn().expect("spawn");
assert_eq!(child.containment(), crate::Containment::FdMarker);
let mut line = String::new();
std::io::BufReader::new(child.stdout().expect("stdout pipe"))
.read_line(&mut line)
.expect("read the holder's pid");
let pid: u32 = line.trim().parse().expect("the holder's pid");
assert_left_the_group(pid, child.pid());
let holder = EscapedHolder {
pid,
_stdin: child.stdin().expect("stdin pipe"),
stderr: child.stderr().expect("stderr pipe"),
};
(child, holder)
}