cosca 0.7.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! The macOS wait for a child's exit, by number: a kqueue holding `EVFILT_PROC` (`NOTE_EXIT |
//! NOTE_REAP | NOTE_SIGNAL`) and `EVFILT_SIGNAL` (`SIGCHLD`), plus a peek. It never reaps: the
//! caller reaps afterwards through the handle it holds, and for a zombie that returns at once.
//!
//! - A peek that finds an exit record is [`Waited::Reapable`]. A still-running child blocks on
//!   the kqueue; a spurious wake, such as a sibling's `SIGCHLD`, only re-peeks.
//! - `NOTE_REAP` on the wait's own knote is [`Waited::Gone`]: only a real reap fires it (the
//!   tracer's hand-back re-sends `NOTE_EXIT` and returns before `proc_knote(child, NOTE_REAP)`,
//!   `kern_exit.c:2721-2773` against `:2787`), and every caller reaps only after this returns,
//!   so a `NOTE_REAP` during the wait means something else reaped the child. It never peeks
//!   again by a pid that may since name another child. An `ECHILD` from a peek is `Gone` in three
//!   cases, all decided in `exit_only::macos::peek`: the unique id says the pid names another
//!   process, `arg = 1` says it is reaped (`ESRCH`: a process resolves from `P_REF_DEAD` until
//!   its reap), or the read was refused. If launchd holds it (a zombie whose tracer died), the
//!   verdict is [`Waited::Orphaned`], which is not a reap. While the pid names the child and a
//!   live process other than launchd holds it, a tracer holds the child, and the wait goes on. A
//!   traced child's `NOTE_EXIT` reaches this process only at its tracer's hand-back; see
//!   [`TracerWatch`] for how the wait watches the tracer chain. Any signal to the child only makes
//!   the wait peek again.
//! - Once `NOTE_EXIT` has come, or the registration's receipt says `ESRCH` (the child is past
//!   `P_REF_DEAD`, where `proc_find` fails, but `waitid` may not see the zombie yet), it re-peeks
//!   with a backoff until the peek finds the zombie or the child is gone. That is XNU finishing
//!   the exit, or a tracer holding the zombie until its hand-back; the backoff re-checks that
//!   real condition. 1 ms doubling to at most 50 ms, no iteration cap; elapsed time decides
//!   nothing.
//! - `EV_CLEAR` on the `EVFILT_PROC` knote: with `NOTE_REAP` requested `NOTE_EXIT` is not
//!   one-shot and the knote's `fflags` are never cleared, so without it the knote re-activates
//!   and every backoff `kevent` returns at once (`kern_event.c:1222-1224`, `:993-995`,
//!   `:4442-4445`, `:1314`): a CPU spin between exit and `SZOMB`.
//! - Under `SIG_IGN` or `SA_NOCLDWAIT`, XNU reaps the child itself and sends no `SIGCHLD`
//!   (`kern_exit.c:2577-2601`). The wait registers `EVFILT_PROC` itself, so a caller whose own
//!   registration got `ESRCH` still has a `NOTE_EXIT` to wait for.
//! - `deadline` is the caller's bound: never early, and never late by the wait's own choice.
//!   Every blocking `kevent`, the backoff's included, is timed to the time remaining, recomputed
//!   before each call (an `EINTR` retry too) and capped by `kevent_timeout`, so a far deadline is
//!   re-armed, not an `EINVAL`. When the time runs out, one final peek decides: a
//!   zombie found is `Reapable`, else `DeadlinePassed`. `None` is unbounded.

use std::io;
use std::time::{Duration, Instant};

use nix::sys::event::{EvFlags, EventFilter, FilterFlag, KEvent};

use super::{OnTracer, TracerWatch};
use crate::os_process::child::{Foreign, PeekOutcome};
#[cfg(test)]
use crate::os_process::macos::kqueue::test_hooks;
use crate::os_process::macos::kqueue::{kevent_timeout, Event, Kqueue, Subscription};
use crate::wait::exit_only::{self, Target};

/// The wait's verdict.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Waited {
    /// The child is a zombie, and reapable.
    Reapable,
    /// The deadline passed with the child still running, after one final peek.
    DeadlinePassed,
    /// Something else reaped the child.
    Gone,
    /// The pid names the child, but launchd holds its zombie because its tracer died: not this
    /// process's to reap.
    Orphaned,
}

/// Slots in one `kevent` batch.
const BATCH: usize = 8;

pub(crate) const BACKOFF_START: Duration = Duration::from_millis(1);
pub(crate) const BACKOFF_CAP: Duration = Duration::from_millis(50);

fn blank() -> KEvent {
    KEvent::new(0, EventFilter::EVFILT_PROC, EvFlags::empty(), FilterFlag::empty(), 0, 0)
}

/// Register `EVFILT_SIGNAL` for `SIGCHLD` and `EVFILT_PROC` for `pid`. `Ok(true)` when the
/// `EVFILT_PROC` receipt says `ESRCH` (the child is already past `P_REF_DEAD`).
fn register(kq: &Kqueue, pid: u32) -> io::Result<bool> {
    kq.subscribe(Subscription::Signal(libc::SIGCHLD))
        .map_err(crate::error::Error::into_io)?;
    #[cfg(test)]
    if test_hooks::take_forced_esrch_registration() {
        return Ok(true);
    }
    let child = Subscription::Process {
        pid,
        signals: true,
        reap: true,
    };
    match kq.subscribe(child) {
        Ok(()) => Ok(false),
        Err(e) if e.raw_os_error() == Some(libc::ESRCH) => Ok(true),
        Err(e) => Err(e.into_io()),
    }
}

/// One non-consuming look, by number and checked against `unique` when there is one, and the tracer
/// that holds a running child, if one does.
fn peek(pid: u32, unique: Option<u64>) -> io::Result<(PeekOutcome, Option<u32>)> {
    exit_only::peek_traced(&Target::pid(pid, unique))
}

/// The peek's verdict as a wait verdict, or `None` when the child is still running.
fn settle(peeked: PeekOutcome) -> Option<Waited> {
    match peeked {
        PeekOutcome::Exit(_) => Some(Waited::Reapable),
        PeekOutcome::Running => None,
        PeekOutcome::Foreign(Foreign::Gone | Foreign::Other) => Some(Waited::Gone),
        PeekOutcome::Foreign(Foreign::Orphaned) => Some(Waited::Orphaned),
    }
}

/// Read every event already pending without blocking, until a round returns fewer events than
/// the buffer holds; `true` if any is the child's `NOTE_REAP`.
fn drain_for_reap(kq: &Kqueue, events: &mut [KEvent; BATCH], pid: u32, tracer: &mut TracerWatch) -> io::Result<bool> {
    loop {
        let n = kevent_round(kq, events, Block::Poll)?;
        if scan(&events[..n], pid, tracer).0 {
            return Ok(true);
        }
        if n < BATCH {
            return Ok(false);
        }
    }
}

/// `(saw NOTE_REAP, saw NOTE_EXIT)` of the child `pid` in `events`. A signal, to the child or to a
/// tracer, and a tracer's exit are neither: they only wake the wait to peek again.
fn scan(events: &[KEvent], pid: u32, tracer: &mut TracerWatch) -> (bool, bool) {
    let (mut reap, mut exit) = (false, false);
    for raw in events {
        #[cfg(test)]
        test_hooks::record_event(raw.filter().map_or(0, |f| f as i16), raw.fflags().bits());
        // The backoff timer is this module's own registration, not a `Kqueue` subscription.
        if matches!(raw.filter(), Ok(EventFilter::EVFILT_TIMER)) {
            continue;
        }
        let Some(event) = Event::from_kevent(raw) else {
            continue;
        };
        if tracer.fired(&event, pid) {
            continue;
        }
        if let Event::Process { exited, reaped, .. } = event {
            reap |= reaped;
            exit |= exited;
        }
    }
    (reap, exit)
}

/// How long one `kevent` call may block.
#[derive(Clone, Copy)]
enum Block {
    /// Not at all: read what is pending.
    Poll,
    /// Until `deadline`, or `interval` if that is sooner (`None`: the deadline alone; both
    /// `None`: unbounded).
    Until {
        interval: Option<Duration>,
        deadline: Option<Instant>,
    },
}

impl Block {
    /// The time this call may block, recomputed from the clock on every call. It is uncapped:
    /// [`kevent_timeout`](crate::os_process::macos::kqueue::kevent_timeout) caps what reaches the syscall, and a capped
    /// call that times out is just another round against the real deadline.
    fn timeout(self) -> Option<Duration> {
        match self {
            Block::Poll => Some(Duration::ZERO),
            Block::Until { interval, deadline } => {
                let armed = match (interval, crate::wait::remaining(deadline)) {
                    (Some(interval), Some(left)) => Some(interval.min(left)),
                    (Some(interval), None) => Some(interval),
                    (None, left) => left,
                };
                // A deadline wait never arms an unbounded `kevent`.
                debug_assert!(
                    deadline.is_none() || armed.is_some(),
                    "a deadline wait armed an unbounded kevent"
                );
                armed
            }
        }
    }
}

/// One `kevent` call armed by `block`. After an `EINTR` the timeout is recomputed, not reused:
/// the interrupted call spent time.
fn kevent_round(kq: &Kqueue, events: &mut [KEvent; BATCH], block: Block) -> io::Result<usize> {
    loop {
        let timeout = block.timeout().map(kevent_timeout);
        #[cfg(test)]
        {
            let armed = timeout.map(|t| Duration::new(t.tv_sec as u64, t.tv_nsec as u32));
            test_hooks::record_await_kevent(armed);
            if armed != Some(Duration::ZERO) {
                if let Some(elapsed) = test_hooks::take_forced_eintr() {
                    crate::wait::test_clock::advance_by_elapsed_if_frozen(elapsed);
                    continue;
                }
            }
        }
        #[cfg(test)]
        let started = Instant::now();
        let result = kq.native().kevent(&[], events, timeout);
        #[cfg(test)]
        crate::wait::test_clock::advance_by_elapsed_if_frozen(started.elapsed());
        match result {
            Ok(n) => return Ok(n),
            Err(nix::errno::Errno::EINTR) => continue,
            Err(e) => return Err(e.into()),
        }
    }
}

/// Wait until `pid`'s exit is reapable, on `kq`; see the module docs.
pub(crate) fn await_reapable_on(
    kq: &Kqueue,
    pid: u32,
    unique: Option<u64>,
    deadline: Option<Instant>,
) -> io::Result<Waited> {
    let mut backoff = register(kq, pid)?;
    let mut interval = BACKOFF_START;
    let mut events = [blank(); BATCH];
    let mut tracer = TracerWatch::default();
    #[cfg(test)]
    let mut round: u32 = 0;
    #[cfg(test)]
    let mut check = crate::wait::test_clock::RoundCheck::new("await_reapable_on");
    let mut repeek = false;
    loop {
        #[cfg(test)]
        check.round();
        if repeek {
            #[cfg(test)]
            {
                test_hooks::fire_before_repeek();
                if backoff {
                    test_hooks::fire_esrch_repeek();
                }
            }
        }
        repeek = true;
        // Peeks until the wait may block: once more at once after a tracer's watch is registered.
        let mut tracer_wait = None;
        loop {
            // A `NOTE_REAP` anywhere before a peek means something else reaped the child: never
            // peek again by a pid that may since name another child.
            if drain_for_reap(kq, &mut events, pid, &mut tracer)? {
                return Ok(Waited::Gone);
            }
            let (peeked, holder) = peek(pid, unique)?;
            if let Some(verdict) = settle(peeked) {
                return Ok(verdict);
            }
            let Some(holder) = holder else { break };
            match tracer.held_by(kq, holder)? {
                OnTracer::Block => break,
                OnTracer::Recheck(after) if after.is_zero() && !expired(deadline) => {}
                OnTracer::Recheck(after) => {
                    tracer_wait = Some(after);
                    break;
                }
            }
        }
        if expired(deadline) {
            return final_peek(pid, unique);
        }
        // Block: on the kqueue alone until `NOTE_EXIT`, then per backoff interval.
        #[cfg(test)]
        test_hooks::fire_kevent_round(round);
        #[cfg(test)]
        {
            round += 1;
        }
        let block = Block::Until {
            interval: match (backoff.then_some(interval), tracer_wait) {
                (Some(backoff), Some(tracer)) => Some(backoff.min(tracer)),
                (backoff, tracer) => backoff.or(tracer),
            },
            deadline,
        };
        let n = kevent_round(kq, &mut events, block)?;
        let (reaped, exited) = scan(&events[..n], pid, &mut tracer);
        if reaped {
            return Ok(Waited::Gone);
        }
        if exited {
            backoff = true;
        } else if backoff && n == 0 {
            interval = (interval * 2).min(BACKOFF_CAP);
        }
        if expired(deadline) {
            // No new round: the drain, then the one final peek.
            if drain_for_reap(kq, &mut events, pid, &mut tracer)? {
                return Ok(Waited::Gone);
            }
            return final_peek(pid, unique);
        }
    }
}

fn expired(deadline: Option<Instant>) -> bool {
    crate::wait::remaining(deadline) == Some(Duration::ZERO)
}

/// The one non-blocking look at expiry.
fn final_peek(pid: u32, unique: Option<u64>) -> io::Result<Waited> {
    #[cfg(test)]
    exit_only::seams::step(exit_only::seams::HolderStep::FinalPeek);
    Ok(settle(peek(pid, unique)?.0).unwrap_or(Waited::DeadlinePassed))
}

// For a waiter that drives the kqueue itself (the sync and the tokio child's reap) =====

/// Register on `kq` what [`await_reapable_on`] waits on. `Ok(true)` when the `EVFILT_PROC` receipt
/// says `ESRCH`, so no `NOTE_EXIT` or `NOTE_REAP` will come.
pub(crate) fn register_exit_events(kq: &Kqueue, pid: u32) -> io::Result<bool> {
    register(kq, pid)
}

/// Read every event pending on `kq` without blocking: whether one was the child `pid`'s
/// `NOTE_REAP`, and whether one was its `NOTE_EXIT`. The events of the tracers `tracer` watches are
/// neither.
pub(crate) fn drain_exit_events(kq: &Kqueue, pid: u32, tracer: &mut TracerWatch) -> io::Result<(bool, bool)> {
    let mut events = [blank(); BATCH];
    let (mut reaped, mut exited) = (false, false);
    loop {
        // A verdict from an earlier round stands over a later failure: nothing reports it again.
        let n = match kevent_round(kq, &mut events, Block::Poll) {
            Ok(n) => n,
            Err(e) if reaped || exited => {
                log::warn!("wait: reading pid {pid}'s kqueue failed after its exit was read: {e}");
                return Ok((reaped, exited));
            }
            Err(e) => return Err(e),
        };
        let (reap, exit) = scan(&events[..n], pid, tracer);
        reaped |= reap;
        exited |= exit;
        if n < BATCH {
            return Ok((reaped, exited));
        }
    }
}

/// Arm `kq`'s one-shot backoff timer: one `EVFILT_TIMER` event after `interval`, replacing a timer
/// still pending.
#[cfg_attr(
    not(feature = "tokio"),
    allow(dead_code, reason = "the only consumer is the tokio backend's reap")
)]
pub(crate) fn arm_backoff_timer(kq: &Kqueue, interval: Duration) -> io::Result<()> {
    #[cfg(test)]
    test_hooks::record_backoff_timer(interval);
    let timer = KEvent::new(
        0,
        EventFilter::EVFILT_TIMER,
        EvFlags::EV_ADD | EvFlags::EV_ONESHOT,
        FilterFlag::empty(),
        interval.as_millis().max(1) as isize,
        0,
    );
    let mut none: [KEvent; 0] = [];
    kq.native()
        .kevent(&[timer], &mut none, None)
        .map(drop)
        .map_err(io::Error::from)
}

/// [`await_reapable_on`] on a kqueue of its own.
pub(crate) fn await_reapable(pid: u32, unique: Option<u64>, deadline: Option<Instant>) -> io::Result<Waited> {
    let kq = Kqueue::new().map_err(crate::error::Error::into_io)?;
    await_reapable_on(&kq, pid, unique, deadline)
}

#[cfg(test)]
#[path = "await_reapable_tests.rs"]
mod await_reapable_tests;