cosca 0.10.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! Collecting the child's status through its pidfd, and killing the child through it.

use std::os::fd::AsFd;

use rustix::io::Errno;
use rustix::process::{waitid, WaitId, WaitIdOptions, WaitIdStatus};

use super::child::signal;
use super::log::Log;
use crate::elevation::shim::step::ToChild;
use crate::os_process::linux::fork::NewChild;

/// A wait status in the layout `waitpid` gives: `code << 8` for an exit, the signal for a kill, with
/// `0x80` added for a core dump.
fn wait_status(status: &WaitIdStatus) -> Option<i32> {
    if let Some(code) = status.exit_status() {
        Some((code & 0xff) << 8)
    } else {
        status
            .terminating_signal()
            .map(|signal| signal | if status.dumped() { 0x80 } else { 0 })
    }
}

/// SIGKILL through the child's handle, and the result in the logs. Only `ESRCH`, the child already
/// collected by someone else, is expected to fail; anything else is a bug, and the caller goes on
/// to wait for a child that may not have been killed.
pub(super) fn kill(child: &NewChild, log: &Log) {
    let sent = signal(child, ToChild::Kill);
    log.line(format_args!("kill rc={sent:?}"));
    match sent {
        Ok(()) => {}
        Err(Errno::SRCH) => log::debug!("the program's process is already gone"),
        Err(e) => {
            log.warn(format_args!(
                "cannot kill the program's process: {}",
                crate::error::Report(&e)
            ));
            debug_assert!(false, "pidfd_send_signal(SIGKILL): {}", crate::error::Report(&e));
        }
    }
}

/// The child's wait status, or `None` when someone else collected it.
///
/// `ready` is true when the pidfd has fired, so the child has exited and the first call does not
/// block. If the zombie is still invisible, because another process traces the child and only
/// that tracer can see it until it collects it, the call then blocks until the kernel hands the
/// status on: the program is already dead, so nothing is left to control. Only `ECHILD`, someone
/// else having reaped it, is a lost status. If the tracer never collects, this waits, as a plain
/// parent's `waitpid` would.
pub(super) fn reap(child: &NewChild, ready: bool, log: &Log) -> Option<i32> {
    let mut options = WaitIdOptions::EXITED;
    if ready {
        options |= WaitIdOptions::NOHANG;
    }
    match wait_on(child, options, log) {
        Wait::Status(status) => return Some(status),
        Wait::Gone => return None,
        Wait::NotYet => {}
    }
    log.line(format_args!(
        "exited, but only its tracer can see it yet: waiting for its status"
    ));
    // The one deliberate blocking call after the loop's poll: see above.
    match wait_on(child, WaitIdOptions::EXITED, log) {
        Wait::Status(status) => Some(status),
        Wait::Gone => None,
        Wait::NotYet => {
            debug_assert!(false, "a blocking waitid returned with nothing to collect");
            None
        }
    }
}

enum Wait {
    Status(i32),
    Gone,
    /// `WNOHANG` and nothing to collect yet.
    NotYet,
}

fn wait_on(child: &NewChild, options: WaitIdOptions, log: &Log) -> Wait {
    loop {
        return match waitid(WaitId::PidFd(child.pidfd.as_fd()), options) {
            Ok(Some(status)) => wait_status(&status).map_or(Wait::Gone, Wait::Status),
            Ok(None) => Wait::NotYet,
            Err(Errno::INTR) => continue,
            // Someone else collected the status.
            Err(Errno::CHILD) => Wait::Gone,
            Err(e) => {
                log.warn(format_args!(
                    "waitid on the program's pidfd: {}",
                    crate::error::Report(&e)
                ));
                debug_assert!(false, "waitid on the program's pidfd: {}", crate::error::Report(&e));
                Wait::Gone
            }
        };
    }
}

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