use std::io;
use std::process::{ChildStdin, ExitStatus};
use std::sync::mpsc::{Receiver, Sender};
use std::sync::Arc;
use std::thread::JoinHandle;
use crate::child::shared::seams::{self, ForcedWait};
use crate::child::shared::SharedChild;
use crate::identity::ProcessId;
pub(super) struct Blocker {
pub(super) shared: Arc<SharedChild>,
stdin: Option<ChildStdin>,
}
pub(super) fn spawn_std_blocker() -> (std::process::Child, ChildStdin) {
spawn_std_blocker_with(|_| {})
}
pub(super) fn spawn_std_blocker_with(
configure: impl FnOnce(&mut std::process::Command),
) -> (std::process::Child, ChildStdin) {
let mut cmd = crate::test_child::held_std_blocker(std::process::Stdio::null());
configure(&mut cmd);
let mut child = crate::test_spawn::spawn(&mut cmd).expect("spawn the blocker");
let stdin = child.stdin.take().expect("piped stdin");
(child, stdin)
}
pub(super) fn identity_of(child: &std::process::Child) -> ProcessId {
ProcessId::of(child.id())
.found()
.expect("the live child has an identity")
}
impl Blocker {
pub(super) fn spawn() -> Blocker {
Blocker::spawn_with(|_| {})
}
pub(super) fn spawn_with(configure: impl FnOnce(&mut std::process::Command)) -> Blocker {
let (child, stdin) = spawn_std_blocker_with(configure);
let id = identity_of(&child);
let shared = SharedChild::adopt(child, id).unwrap_or_else(|(e, _)| panic!("adopt: {e}"));
Blocker {
shared: Arc::new(shared),
stdin: Some(stdin),
}
}
pub(super) fn end_child(&mut self) {
drop(self.stdin.take());
}
pub(super) fn end_child_and_confirm_exit(&mut self) {
self.end_child();
confirm_exit(&self.shared);
}
}
impl Drop for Blocker {
fn drop(&mut self) {
drop(self.stdin.take());
if std::thread::panicking() {
_ = self.shared.kill();
} else {
_ = self.shared.kill();
_ = self.shared.wait();
}
}
}
pub(super) fn confirm_exit(shared: &SharedChild) {
#[cfg(target_os = "linux")]
{
let target = shared.target().expect("a pidfd");
let peeked = crate::wait::exit_only::wait_visible_exit(&target).expect("wait for the exit");
assert!(
matches!(peeked, crate::wait::exit_only::Peek::Exit(_)),
"the exit must be visible, got {peeked:?}"
);
}
#[cfg(target_os = "macos")]
{
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
let r = unsafe {
libc::waitid(
libc::P_PID,
shared.id() as libc::id_t,
&mut info,
libc::WEXITED | libc::WNOWAIT,
)
};
assert_eq!(r, 0, "waitid(WNOWAIT): {}", io::Error::last_os_error());
}
#[cfg(windows)]
{
use std::os::windows::io::AsRawHandle;
use windows::Win32::Foundation::{HANDLE, WAIT_OBJECT_0};
use windows::Win32::System::Threading::{WaitForSingleObject, INFINITE};
let r = unsafe { WaitForSingleObject(HANDLE(shared.handle.as_raw_handle()), INFINITE) };
assert_eq!(r, WAIT_OBJECT_0);
}
}
#[cfg(target_os = "linux")]
fn is_echild(e: &io::Error) -> bool {
e.raw_os_error() == Some(libc::ECHILD)
}
#[cfg(target_os = "linux")]
fn confirm_exit_of(child: &std::process::Child) {
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
let r = unsafe {
libc::waitid(
libc::P_PID,
child.id() as libc::id_t,
&mut info,
libc::WEXITED | libc::WNOWAIT,
)
};
assert_eq!(r, 0, "waitid(WNOWAIT): {}", io::Error::last_os_error());
}
#[cfg(target_os = "linux")]
pub(super) fn assert_adoption_is_gone<G>(arm: impl FnOnce() -> G) {
let (child, stdin) = spawn_std_blocker();
let pid = child.id();
let id = identity_of(&child);
drop(stdin);
confirm_exit_of(&child);
let forced = arm();
let shared = SharedChild::adopt(child, id).expect("a gone answer is not a failure");
drop(forced);
let far = std::time::Instant::now() + std::time::Duration::from_secs(3600);
assert!(is_echild(&shared.wait().expect_err("wait")));
assert!(is_echild(&shared.try_wait().expect_err("try_wait")));
assert!(is_echild(&shared.wait_deadline(far).expect_err("wait_deadline")));
let log = crate::send_log::Capture::start();
shared
.kill()
.expect("a child that was gone at adoption is already dead: success");
assert_eq!(log.entries(), [], "nothing is sent without a pidfd");
drop(log);
let mut status = 0;
let r = unsafe { libc::waitpid(pid as i32, &mut status, 0) };
assert_eq!(r, pid as i32, "reap the fixture: {}", io::Error::last_os_error());
}
pub(super) fn assert_handed_off(shared: &SharedChild, token: u64) {
use crate::child::shared::{Logged, State};
let mut lock = shared.lock();
let stranded = matches!(lock.state, State::W { token: t } if t == token);
let unnotified = lock
.log
.iter()
.enumerate()
.any(|(i, entry)| matches!(entry, Logged::Write(_)) && lock.log.get(i + 1) != Some(&Logged::Notify));
let unlogged = lock
.log
.iter()
.rev()
.find_map(|entry| match entry {
Logged::Write(state) => Some(*state),
Logged::Notify => None,
})
.is_some_and(|last| last != lock.state);
let mut prev = State::N;
let mut illegal = false;
for entry in &lock.log {
if let Logged::Write(next) = entry {
illegal |= !matches!(
(prev, *next),
(State::N, State::W { .. }) | (State::N | State::W { .. }, State::E(_)) | (State::W { .. }, State::N)
);
prev = *next;
}
}
if stranded || unnotified || unlogged || illegal {
let log = lock.log.clone();
lock.set(State::N);
shared.notify(&mut lock);
drop(lock);
panic!(
"the holder {token} left the handle stranded ({stranded}), unnotified ({unnotified}), \
with a state the log does not show ({unlogged}) or with an illegal write sequence \
({illegal}): {log:?}"
);
}
}
pub(super) struct ParkedHolder {
pub(super) thread: JoinHandle<io::Result<Option<ExitStatus>>>,
release: Sender<()>,
}
impl ParkedHolder {
pub(super) fn release_and_join(self) -> io::Result<Option<ExitStatus>> {
drop(self.release);
self.thread.join().expect("the holder thread must not panic")
}
pub(super) fn release_and_join_thread(self) -> std::thread::Result<io::Result<Option<ExitStatus>>> {
drop(self.release);
self.thread.join()
}
}
pub(super) fn park_holder<G>(
shared: &Arc<SharedChild>,
deadline: Option<std::time::Instant>,
arm: impl FnOnce() -> G + Send + 'static,
) -> ParkedHolder {
let (gate, reached, release) = seams::park_gate();
let shared = Arc::clone(shared);
let thread = std::thread::spawn(move || {
let _armed = arm();
let _park = seams::park_in_unlocked_wait(gate);
match deadline {
None => shared.wait().map(Some),
Some(d) => shared.wait_deadline(d),
}
});
reached.recv().expect("the holder must reach its unlocked wait");
ParkedHolder { thread, release }
}
pub(super) fn spawn_waiter<T: Send + 'static>(
shared: &Arc<SharedChild>,
must_not_hold: bool,
body: impl FnOnce(&SharedChild) -> T + Send + 'static,
) -> (JoinHandle<T>, Receiver<()>) {
let (blocked_tx, blocked_rx) = std::sync::mpsc::channel();
let shared = Arc::clone(shared);
let thread = std::thread::spawn(move || {
let _hook = seams::on_condvar_block(move || {
_ = blocked_tx.send(());
});
let _forced = must_not_hold.then(|| seams::force_unlocked_wait(ForcedWait::Panic));
body(&shared)
});
(thread, blocked_rx)
}
#[cfg(unix)]
pub(super) fn signal_of(status: ExitStatus) -> Option<i32> {
std::os::unix::process::ExitStatusExt::signal(&status)
}