#[cfg(target_arch = "x86_64")]
mod group_exit_status {
use std::io::Read;
use std::io::Write;
use std::os::unix::process::CommandExt;
const NAME: &str =
"notifier::test::group_exit_status::reaped_status_follows_a_later_group_exit";
const ROLE: &str = "SAFEPTRACE_GROUP_EXIT_STATUS_GUEST";
const TIDS: &str = "SAFEPTRACE_GROUP_EXIT_TIDS";
fn guest() -> ! {
let helper = std::thread::spawn(|| unsafe { libc::gettid() })
.join()
.unwrap();
let caller = unsafe { libc::gettid() };
let mut stdout = std::io::stdout();
writeln!(stdout, "{TIDS} {helper} {caller}").unwrap();
stdout.flush().unwrap();
let mut byte = [0u8; 1];
let _ = std::io::stdin().read(&mut byte);
unsafe { libc::_exit(11) }
}
fn waitid(id: libc::id_t, idtype: libc::idtype_t, flags: libc::c_int) -> libc::siginfo_t {
loop {
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
if unsafe { libc::waitid(idtype, id, &mut info, flags) } == 0 {
return info;
}
let errno = std::io::Error::last_os_error();
assert_eq!(errno.raw_os_error(), Some(libc::EINTR), "waitid: {errno}");
}
}
fn exit_code(info: &libc::siginfo_t) -> Option<i32> {
(info.si_code == libc::CLD_EXITED).then(|| unsafe { info.si_status() })
}
fn resume_stop(pid: libc::pid_t) {
let mut status = 0;
assert_eq!(
unsafe { libc::waitpid(pid, &mut status, libc::__WALL) },
pid
);
assert!(libc::WIFSTOPPED(status), "{pid}: {status:#x}");
let signal = libc::WSTOPSIG(status);
let deliver = if status >> 16 != 0 || signal == libc::SIGSTOP || signal == libc::SIGTRAP {
0
} else {
signal
};
assert_eq!(
unsafe { libc::ptrace(libc::PTRACE_CONT, pid, 0, deliver) },
0
);
}
#[test]
fn reaped_status_follows_a_later_group_exit() {
if std::env::var(ROLE).as_deref() == Ok(NAME) {
guest();
}
let mut child = unsafe {
crate::test_fork::spawn(
std::process::Command::new(std::env::current_exe().unwrap())
.args(["--exact", NAME, "--test-threads=1", "--nocapture"])
.env(ROLE, NAME)
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.pre_exec(|| {
if libc::setpgid(0, 0) == 0
&& libc::ptrace(libc::PTRACE_TRACEME, 0, 0, 0) == 0
{
Ok(())
} else {
Err(std::io::Error::last_os_error())
}
}),
)
.unwrap()
};
let leader = child.id() as libc::pid_t;
let group = leader as libc::id_t;
let mut status = 0;
assert_eq!(unsafe { libc::waitpid(leader, &mut status, 0) }, leader);
assert!(libc::WIFSTOPPED(status) && libc::WSTOPSIG(status) == libc::SIGTRAP);
let options = libc::PTRACE_O_TRACECLONE | libc::PTRACE_O_TRACEEXIT | libc::PTRACE_O_EXITKILL;
assert_eq!(
unsafe { libc::ptrace(libc::PTRACE_SETOPTIONS, leader, 0, options) },
0
);
assert_eq!(unsafe { libc::ptrace(libc::PTRACE_CONT, leader, 0, 0) }, 0);
let mut stdout = std::io::BufReader::new(child.stdout.take().unwrap());
std::thread::scope(|scope| {
let reader = scope.spawn(move || loop {
let mut line = String::new();
assert_ne!(
std::io::BufRead::read_line(&mut stdout, &mut line).unwrap(),
0,
"the guest wrote no TIDs"
);
if let Some((_, ids)) = line.split_once(TIDS) {
break ids.trim().to_owned();
}
});
let observed = loop {
let info = waitid(
group,
libc::P_PGID,
libc::WEXITED | libc::WSTOPPED | libc::WNOWAIT | libc::__WALL,
);
let pid = unsafe { info.si_pid() };
if info.si_code == libc::CLD_TRAPPED || info.si_code == libc::CLD_STOPPED {
resume_stop(pid);
continue;
}
assert_ne!(pid, leader, "the leader exited first: {}", info.si_code);
break (pid, exit_code(&info));
};
let line = reader.join().unwrap();
let (helper, caller) = line.trim().split_once(' ').unwrap();
let (helper, caller): (libc::pid_t, libc::pid_t) =
(helper.parse().unwrap(), caller.parse().unwrap());
assert_eq!(observed, (helper, Some(0)), "the helper's own exit status");
child.stdin.take().unwrap().write_all(b"x").unwrap();
loop {
let info = waitid(group, libc::P_PGID, libc::WSTOPPED | libc::WNOWAIT | libc::__WALL);
let pid = unsafe { info.si_pid() };
let event = unsafe { info.si_status() } >> 8;
if pid == caller && event == libc::PTRACE_EVENT_EXIT {
break;
}
resume_stop(pid);
}
let reaped = waitid(helper as libc::id_t, libc::P_PID, libc::WEXITED | libc::__WALL);
assert_eq!(
exit_code(&reaped),
Some(11),
"the reap reports the group's exit code, not the observed 0"
);
});
assert_eq!(unsafe { libc::kill(leader, libc::SIGKILL) }, 0);
loop {
let mut status = 0;
let pid = unsafe { libc::waitpid(-leader, &mut status, libc::__WALL) };
if pid == leader && (libc::WIFEXITED(status) || libc::WIFSIGNALED(status)) {
break;
}
if pid > 0 && libc::WIFSTOPPED(status) {
unsafe { libc::ptrace(libc::PTRACE_CONT, pid, 0, 0) };
}
}
}
}