use std::io::Read as _;
use std::os::unix::process::ExitStatusExt as _;
use std::process::Stdio;
use std::time::{Duration, Instant};
use rustix::process::{Pid, getpgid, getpgrp};
use super::super::{Group, Scope, Signal};
use crate::testing::{gone, kill, pid_in, sh};
fn start(script: &str, scope: Scope) -> Group {
let mut command = sh(script);
command.stdout(Stdio::piped()).stderr(Stdio::null());
Group::start(command, scope).unwrap()
}
fn first_line(group: &mut Group) -> Vec<u8> {
let mut stdout = group.take_stdout().unwrap();
let mut line = Vec::new();
let mut byte = [0];
while stdout.read(&mut byte).unwrap() == 1 && byte[0] != b'\n' {
line.push(byte[0]);
}
line
}
#[test]
fn a_child_leads_a_process_group_of_its_own_and_stays_unreaped_until_the_reap() {
let mut command = sh("read line; exit 3");
command.stdin(Stdio::piped());
let mut group = Group::start(command, Scope::Group).unwrap();
let pid = Pid::from_raw(group.id().try_into().unwrap()).unwrap();
assert_eq!(getpgid(Some(pid)).ok(), Some(pid));
assert!(!group.has_exited().unwrap());
drop(group.take_stdin());
group.wait_exit().unwrap();
assert!(group.has_exited().unwrap());
assert!(
rustix::process::test_kill_process(pid).is_ok(),
"the child was reaped"
);
assert_eq!(group.reap().unwrap().code(), Some(3));
assert_eq!(group.reap().unwrap().code(), Some(3));
}
#[test]
fn the_reap_kills_a_background_grandchild_that_the_child_left_running() {
let mut group = start("sleep 30 & echo $!", Scope::Group);
let sleeper = pid_in(&first_line(&mut group));
group.reap().unwrap();
assert!(
gone(sleeper),
"the background sleep {sleeper} outlived the reap"
);
}
#[test]
fn a_signal_reaches_the_whole_group_and_a_reaped_group_refuses_another() {
let mut group = start(
"trap 'exit 9' TERM; sh -c 'echo $$; exec sleep 30' & wait",
Scope::Group,
);
let sleeper = pid_in(&first_line(&mut group));
group.signal(Signal::Terminate).unwrap();
assert!(
gone(sleeper),
"the background sleep {sleeper} outlived the signal"
);
assert_eq!(group.reap().unwrap().code(), Some(9));
assert!(group.signal(Signal::Kill).is_err());
}
#[test]
fn a_child_that_moved_to_another_group_still_gets_each_signal() {
let mut group = start(
"exec perl -e 'setpgrp(0, getpgrp(getppid())) or die; $| = 1; print qq(moved\\n); sleep 30'",
Scope::Group,
);
assert_eq!(first_line(&mut group), b"moved");
let pid = Pid::from_raw(group.id().try_into().unwrap()).unwrap();
assert_ne!(
getpgid(Some(pid)).ok(),
Some(pid),
"the child still leads its group"
);
group.signal(Signal::Kill).unwrap();
assert_eq!(group.reap().unwrap().signal(), Some(9));
}
#[test]
fn a_child_that_moved_to_another_group_gets_the_signal_when_its_old_group_refuses_it() {
let mut group = start(
"exec perl -e 'pipe(R, W); fork() or exit 0; close W; <R>; \
setpgrp(0, getpgrp(getppid())) or die; $SIG{TERM} = sub { exit 9 }; \
$| = 1; print qq(moved\\n); sleep 30'",
Scope::Group,
);
assert_eq!(first_line(&mut group), b"moved");
let _ = group.signal(Signal::Terminate);
let deadline = Instant::now() + Duration::from_secs(5);
while !group.has_exited().unwrap() && Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(20));
}
let exited = group.has_exited().unwrap();
if !exited {
let _ = group.signal(Signal::Kill);
}
assert!(exited, "the moved child did not get the signal");
assert_eq!(group.reap().unwrap().code(), Some(9));
}
#[test]
fn a_dropped_group_kills_its_child_and_its_group() {
let mut group = start("sleep 30 & echo $!; sleep 30", Scope::Group);
let sleeper = pid_in(&first_line(&mut group));
let started = Instant::now();
drop(group);
assert!(started.elapsed() < Duration::from_secs(5));
assert!(
gone(sleeper),
"the background sleep {sleeper} outlived the drop"
);
}
#[test]
fn a_child_in_the_child_scope_stays_in_the_callers_group_and_only_it_is_signalled() {
let mut group = start("sleep 30 & echo $!; wait", Scope::Child);
let sleeper = pid_in(&first_line(&mut group));
let pid = Pid::from_raw(group.id().try_into().unwrap()).unwrap();
assert_eq!(getpgid(Some(pid)).ok(), Some(getpgrp()));
group.signal(Signal::Kill).unwrap();
group.reap().unwrap();
assert!(!gone_soon(sleeper), "the background sleep was stopped");
kill(sleeper);
}
fn gone_soon(pid: i32) -> bool {
let deadline = Instant::now() + Duration::from_millis(500);
while Instant::now() < deadline {
if rustix::process::test_kill_process(Pid::from_raw(pid).unwrap()).is_err() {
return true;
}
std::thread::sleep(Duration::from_millis(20));
}
false
}
fn reap_outside(group: &Group) {
let pid = Pid::from_raw(group.id().try_into().unwrap()).unwrap();
group.wait_exit().unwrap();
rustix::process::waitpid(Some(pid), rustix::process::WaitOptions::empty()).unwrap();
}
#[test]
fn a_child_reaped_elsewhere_makes_the_reap_fail_and_the_group_refuse_every_later_step() {
let mut group = start("exit 0", Scope::Group);
reap_outside(&group);
let error = group.reap().unwrap_err();
assert_eq!(
error.raw_os_error(),
Some(rustix::io::Errno::CHILD.raw_os_error())
);
assert!(group.signal(Signal::Kill).is_err());
assert!(group.has_exited().unwrap());
let started = Instant::now();
assert_eq!(
group.reap().unwrap_err().kind(),
std::io::ErrorKind::NotFound
);
assert!(started.elapsed() < Duration::from_secs(1));
}
#[test]
fn a_group_whose_child_was_reaped_elsewhere_sends_no_signal_from_a_signal_or_the_drop() {
let mut group = start("sleep 30 & echo $!", Scope::Group);
let sleeper = pid_in(&first_line(&mut group));
reap_outside(&group);
group.reap().unwrap_err();
assert!(group.signal(Signal::Kill).is_err());
drop(group);
std::thread::sleep(Duration::from_millis(500));
let alive = rustix::process::test_kill_process(Pid::from_raw(sleeper).unwrap()).is_ok();
kill(sleeper);
assert!(
alive,
"a signal or the drop killed the background sleep {sleeper}"
);
}