cosca 0.10.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! Unix: reading a descriptor's readiness through `poll(2)`, for the descriptors that stand for a
//! process's exit: a pidfd on Linux, a kqueue armed for the exit on macOS. Both poll readable
//! once the process has exited, and neither read changes what the descriptor reports.

use std::os::fd::BorrowedFd;
use std::time::{Duration, Instant};

use rustix::event::{poll, PollFd, PollFlags, Timespec};

/// One look at `fd` with a zero timeout: `Ok(true)` once it is readable. Never blocks.
pub(crate) fn is_readable(fd: BorrowedFd<'_>) -> std::io::Result<bool> {
    look_readable(fd).map_err(std::io::Error::from)
}

/// How a look at a descriptor failed.
#[derive(Debug)]
pub(crate) enum PollFailure {
    /// The `poll` answered an errno.
    Call(rustix::io::Errno),
    /// The `poll` reported `POLLERR` or `POLLNVAL`, which carry no errno.
    Revents(std::io::Error),
}

impl From<PollFailure> for std::io::Error {
    fn from(failure: PollFailure) -> std::io::Error {
        match failure {
            PollFailure::Call(errno) => errno.into(),
            PollFailure::Revents(error) => error,
        }
    }
}

/// [`is_readable`], with a failure that keeps the errno of the `poll`.
pub(crate) fn look_readable(fd: BorrowedFd<'_>) -> Result<bool, PollFailure> {
    let mut fds = [PollFd::new(&fd, PollFlags::IN)];
    if !crate::wait::any_ready_now(&mut fds).map_err(PollFailure::Call)? {
        return Ok(false);
    }
    readable(fds[0].revents()).map(|()| true).map_err(PollFailure::Revents)
}

/// `poll(2)` takes its timeout as an `int` of milliseconds; where there is no `ppoll` (macOS) rustix
/// turns a longer `Timespec` into `EINVAL` before the call.
const MAX_POLL_BLOCK: Duration = Duration::from_millis(i32::MAX as u64);

/// Polls `fd` until it is readable or `deadline` passes, and says which. `None` is unbounded: one
/// [`poll_ready`](crate::wait::poll_ready), always `true`. Reads no state and reaps nothing: a pidfd
/// names its process for good, so a reused pid cannot answer it.
pub(crate) fn poll_readable(fd: BorrowedFd<'_>, deadline: Option<Instant>) -> std::io::Result<bool> {
    let Some(deadline) = deadline else {
        #[cfg(test)]
        crate::wait::block_probe::blocking(None, crate::wait::block_probe::WaitObject::Fd(fd));
        let mut fds = [PollFd::new(&fd, PollFlags::IN)];
        crate::wait::poll_ready(&mut fds)?;
        return readable(fds[0].revents()).map(|()| true);
    };
    let seen = crate::wait::rearm_until(Some(deadline), |remaining| {
        let remaining = remaining.map(|d| d.min(MAX_POLL_BLOCK));
        #[cfg(test)]
        crate::wait::block_probe::blocking(remaining, crate::wait::block_probe::WaitObject::Fd(fd));
        let ts = remaining.map(|d| Timespec {
            tv_sec: d.as_secs() as i64,
            tv_nsec: d.subsec_nanos() as _,
        });
        debug_assert!(ts.is_some(), "a deadline wait armed an unbounded poll");
        let mut fds = [PollFd::new(&fd, PollFlags::IN)];
        match poll(&mut fds, ts.as_ref()) {
            Ok(0) => Ok(None),
            Ok(_) => readable(fds[0].revents()).map(Some),
            // A signal: go round again against the recomputed remaining time.
            Err(rustix::io::Errno::INTR) => Ok(None),
            Err(e) => Err(std::io::Error::from(e)),
        }
    })?;
    Ok(seen.is_some())
}

/// What a `poll` that reported events found: `Ok(())` for `POLLIN` (a zombie, or an armed kqueue
/// holding the exit) or `POLLHUP` (a reaped pidfd), both an exit; `POLLERR` and `POLLNVAL` are errors.
fn readable(revents: PollFlags) -> std::io::Result<()> {
    // POLLNVAL on an fd we own and hold alive is a contract violation.
    debug_assert!(
        !revents.contains(PollFlags::NVAL),
        "poll reported POLLNVAL: owned-fd contract violation"
    );
    if revents.contains(PollFlags::NVAL) {
        return Err(std::io::Error::other("poll reported POLLNVAL on an owned descriptor"));
    }
    if revents.contains(PollFlags::ERR) {
        return Err(std::io::Error::other("poll returned POLLERR"));
    }
    Ok(())
}

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