subc-os 0.1.58

Operating-system primitives for the subc daemon that need unsafe code, behind a safe API: per-process identity, signalling, memory and CPU usage, and the launch-nonce descriptor handoff.
Documentation
//! Native signed socket peer for macOS tests. The fixture's stdin selects a
//! new connection, allowing a daemon restart without restarting its peer.

use super::{capture_unix_peer, CapturedPeer};
use std::{
    io::Write,
    os::unix::net::{UnixListener, UnixStream},
    path::{Path, PathBuf},
    process::{Child, Command, Stdio},
    sync::atomic::{AtomicU32, Ordering},
    time::{Duration, Instant},
};

#[non_exhaustive]
pub struct SignedPeerFixture {
    child: Child,
    directory: PathBuf,
}

/// A real signed `.app`, kept separate from its process for static tests.
#[non_exhaustive]
pub struct SignedAppFixture {
    directory: PathBuf,
    bundle: PathBuf,
}

fn run(command: &mut Command) {
    let output = crate::child_spawn::output(command).expect("fixture tool starts");
    assert!(
        output.status.success(),
        "{command:?}: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

impl SignedAppFixture {
    pub fn new(
        directory: &Path,
        identifier: &str,
        version: Option<&str>,
        runtime: bool,
        entitlement: Option<(&str, bool)>,
    ) -> Self {
        std::fs::create_dir_all(directory).unwrap();
        let directory = directory.canonicalize().unwrap();
        let bundle = directory.join("Fixture.app");
        let contents = bundle.join("Contents");
        std::fs::create_dir_all(contents.join("MacOS")).unwrap();
        std::fs::create_dir_all(contents.join("Resources")).unwrap();
        let source = directory.join("peer.c");
        std::fs::write(&source, include_str!("fixture.c")).unwrap();
        let version = version
            .map(|version| {
                format!("<key>CFBundleShortVersionString</key><string>{version}</string>")
            })
            .unwrap_or_default();
        std::fs::write(contents.join("Info.plist"), format!("<?xml version=\"1.0\"?><plist version=\"1.0\"><dict><key>CFBundleIdentifier</key><string>{identifier}</string><key>CFBundleName</key><string>Fixture</string><key>CFBundleExecutable</key><string>Fixture</string><key>CFBundlePackageType</key><string>APPL</string><key>CortexKitBuildTime</key><string>2026-11-29T12:00:00Z</string>{version}</dict></plist>")).unwrap();
        std::fs::write(contents.join("Resources/index.html"), "signed resource").unwrap();
        run(Command::new("cc")
            .arg(&source)
            .arg("-o")
            .arg(contents.join("MacOS/Fixture")));
        sign(&directory, &bundle, identifier, runtime, entitlement);
        Self { directory, bundle }
    }

    pub fn bundle(&self) -> &Path {
        &self.bundle
    }

    pub fn launch(&self) -> SignedPeerFixture {
        SignedPeerFixture::spawn(
            self.directory.clone(),
            self.bundle.join("Contents/MacOS/Fixture"),
        )
    }
}

fn sign(
    directory: &Path,
    code: &Path,
    identifier: &str,
    runtime: bool,
    entitlement: Option<(&str, bool)>,
) {
    let mut sign = Command::new("/usr/bin/codesign");
    sign.args([
        "--force",
        "--sign",
        "-",
        "--timestamp=none",
        "--identifier",
        identifier,
    ]);
    if runtime {
        sign.args(["--options", "runtime"]);
    }
    if let Some((entitlement, value)) = entitlement {
        let entitlements = directory.join("entitlements.plist");
        std::fs::write(&entitlements, format!("<?xml version=\"1.0\"?><plist version=\"1.0\"><dict><key>{entitlement}</key><{value}/></dict></plist>")).unwrap();
        sign.arg("--entitlements").arg(entitlements);
    }
    run(sign.arg(code));
}

impl SignedPeerFixture {
    /// Compile a tiny peer with a sealed, embedded plist, then sign it ad hoc.
    pub fn new(
        directory: &Path,
        identifier: &str,
        version: Option<&str>,
        runtime: bool,
        entitlement: Option<(&str, bool)>,
    ) -> Self {
        std::fs::create_dir_all(directory).unwrap();
        let directory = directory.canonicalize().unwrap();
        let source = directory.join("peer.c");
        let plist = directory.join("Info.plist");
        let binary = directory.join("peer");
        std::fs::write(&source, include_str!("fixture.c")).unwrap();
        std::fs::write(&plist, format!("<?xml version=\"1.0\"?><plist version=\"1.0\"><dict><key>CFBundleIdentifier</key><string>{identifier}</string>{}</dict></plist>",
            version.map(|version| format!("<key>CFBundleShortVersionString</key><string>{version}</string>")).unwrap_or_default())).unwrap();
        run(Command::new("cc")
            .arg(&source)
            .arg("-o")
            .arg(&binary)
            .arg("-Wl,-sectcreate,__TEXT,__info_plist")
            .arg(format!("-Wl,{}", plist.display())));
        sign(&directory, &binary, identifier, runtime, entitlement);
        Self::spawn(directory, binary)
    }

    pub fn app(
        directory: &Path,
        identifier: &str,
        version: Option<&str>,
        runtime: bool,
        entitlement: Option<(&str, bool)>,
    ) -> Self {
        SignedAppFixture::new(directory, identifier, version, runtime, entitlement).launch()
    }

    fn spawn(directory: PathBuf, binary: PathBuf) -> Self {
        let child = crate::child_spawn::spawn(
            Command::new(binary)
                .stdin(Stdio::piped())
                .stdout(Stdio::null()),
        )
        .unwrap();
        Self { child, directory }
    }

    pub fn pid(&self) -> u32 {
        self.child.id()
    }

    /// Accept a fresh connection from the same still-running signed process.
    pub fn connect(&mut self) -> (UnixStream, CapturedPeer) {
        static NEXT: AtomicU32 = AtomicU32::new(0);
        // The short alias keeps sockaddr_un within its limit, while the socket
        // itself lives on the fixture's local filesystem, not a mounted checkout.
        std::fs::create_dir_all("target/scratch").unwrap();
        let alias = format!(
            "target/scratch/code-peer-{}-{}",
            std::process::id(),
            NEXT.fetch_add(1, Ordering::Relaxed)
        );
        std::os::unix::fs::symlink(&self.directory, &alias).unwrap();
        let path = format!("{alias}/s");
        let listener = UnixListener::bind(&path).unwrap();
        listener.set_nonblocking(true).unwrap();
        writeln!(self.child.stdin.as_mut().unwrap(), "{path}").unwrap();
        let deadline = Instant::now() + Duration::from_secs(10);
        let stream = loop {
            match listener.accept() {
                Ok((stream, _)) => break stream,
                Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
                    assert!(
                        self.child.try_wait().unwrap().is_none(),
                        "fixture exited before connect"
                    );
                    assert!(Instant::now() < deadline, "fixture connect timed out");
                    std::thread::sleep(Duration::from_millis(10));
                }
                Err(error) => panic!("accept fixture: {error}"),
            }
        };
        std::fs::remove_file(path).unwrap();
        std::fs::remove_file(alias).unwrap();
        let peer = capture_unix_peer(&stream);
        (stream, peer)
    }
}

impl Drop for SignedPeerFixture {
    fn drop(&mut self) {
        let _ = self.child.kill();
        let _ = self.child.wait();
    }
}