cosca 0.10.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! A shell root with a background member, each held by a pipe of the test's own: the member ends at
//! its stdin's end of file and says `survived` unless a fatal signal reached it first. A test ends
//! the member's stdin or the root's release pipe itself, so a kill that sent nothing fails an
//! assertion instead of leaving a wait unbounded.

use std::io::Read as _;

/// A session root `sh` that leaves a member in its group, says `started`, and blocks holding neither
/// the member's stdin nor stderr. The member reads stdin through fd 3 (`sh` points a background
/// job's stdin at `/dev/null` otherwise) and, at its end of file, says `survived` on stderr. The
/// root reads fd 4 ([`Member::pipes`]'s release pipe) and exits at its end of file, so a test can end
/// it without a signal.
#[cfg(target_os = "linux")]
pub(crate) const ROOT_WITH_A_MEMBER: [&str; 3] = [
    "sh",
    "-c",
    "exec 3<&0; (cat <&3 3<&- 4<&- >/dev/null && echo survived >&2) & echo started; exec cat <&4 3<&- 4<&- >/dev/null 2>/dev/null",
];

/// The descriptor the root of [`ROOT_WITH_A_MEMBER`] reads its release from.
#[cfg(target_os = "linux")]
pub(crate) const RELEASE_FD: i32 = 4;

/// A test's ends of the pipes of [`ROOT_WITH_A_MEMBER`]'s member and root.
pub(crate) struct Member {
    /// The write end of the member's stdin. Dropped, the member ends at its end of file.
    stdin: std::rc::Rc<std::cell::Cell<Option<crate::PipeWriter>>>,
    stderr: crate::PipeReader,
    /// The write end of the root's release pipe. Dropped, the root exits by itself.
    root_release: std::rc::Rc<std::cell::Cell<Option<crate::PipeWriter>>>,
}

impl Member {
    /// The pipes of a [`ROOT_WITH_A_MEMBER`] spawn, to give its stdin, stdout, stderr and
    /// [`RELEASE_FD`], and the read end of its stdout.
    pub(crate) fn pipes() -> (MemberStdio, crate::PipeReader, Member) {
        let (stdin_read, stdin) = std::io::pipe().expect("stdin pipe");
        let (started, stdout_write) = std::io::pipe().expect("stdout pipe");
        let (stderr, stderr_write) = std::io::pipe().expect("stderr pipe");
        let (release_read, root_release) = std::io::pipe().expect("release pipe");
        let stdio = |end: std::os::fd::OwnedFd| crate::Stdio::from_file(std::fs::File::from(end));
        let child_ends = MemberStdio {
            stdin: stdio(stdin_read.into()),
            stdout: stdio(stdout_write.into()),
            stderr: stdio(stderr_write.into()),
            release: stdio(release_read.into()),
        };
        let member = Member {
            stdin: std::rc::Rc::new(std::cell::Cell::new(Some(stdin.into()))),
            stderr: stderr.into(),
            root_release: std::rc::Rc::new(std::cell::Cell::new(Some(root_release.into()))),
        };
        (child_ends, started.into(), member)
    }

    /// Releases the root once a teardown's kill returned
    /// ([`release_when_a_teardown_waits`](crate::child::spawn::fault::release_when_a_teardown_waits)),
    /// so a kill that sent nothing fails the member assertion instead of hanging the wait. Keep the
    /// guard through the teardown; it never releases the member.
    #[cfg(target_os = "linux")]
    pub(crate) fn release_root_when_a_teardown_waits(&self) -> Box<dyn std::any::Any> {
        crate::child::spawn::fault::release_when_a_teardown_waits(&self.root_release)
    }

    /// Closes the member's stdin and reads its stderr to the end. A member still running ends at that
    /// end of file and says `survived`; one already sent a fatal signal never runs again, and says
    /// nothing. Neither waits on a signal's delivery. The root's writer is released too, whatever
    /// guards of [`Member::release_root_when_a_teardown_waits`] still hold the cell, so a failed
    /// assertion on what this returns unwinds without a drop that waits on a root nothing ended.
    pub(crate) fn end(self) -> String {
        let Member {
            stdin,
            mut stderr,
            root_release,
        } = self;
        drop((stdin.take(), root_release.take()));
        let mut said = String::new();
        stderr
            .read_to_string(&mut said)
            .expect("read the member's stderr to its end");
        said
    }

    /// Asserts the member was sent a fatal signal before this call.
    #[track_caller]
    pub(crate) fn assert_killed(self) {
        let said = self.end();
        assert!(
            !said.contains("survived"),
            "the group's member was not killed: {said:?}"
        );
    }

    /// Asserts the member was sent no fatal signal.
    #[cfg(target_os = "linux")]
    #[track_caller]
    pub(crate) fn assert_survived(self) {
        let said = self.end();
        assert!(said.contains("survived"), "the group's member was killed: {said:?}");
    }
}

/// The child's ends of [`Member::pipes`].
pub(crate) struct MemberStdio {
    pub(crate) stdin: crate::Stdio,
    pub(crate) stdout: crate::Stdio,
    pub(crate) stderr: crate::Stdio,
    pub(crate) release: crate::Stdio,
}

/// Reads [`ROOT_WITH_A_MEMBER`]'s `started` from `stdout`, once its member exists.
pub(crate) fn await_started(mut stdout: crate::PipeReader) {
    let mut started = [0u8; 8];
    stdout.read_exact(&mut started).expect("read `started`");
    assert_eq!(&started, b"started\n");
}