cosca 0.8.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation

cosca

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.

std::process hands you a child and little else. It cannot tell you whether the process you spawned is still the process you think it is, cannot tear down a process tree, cannot address a process it did not spawn, and cannot run one elevated. This crate covers those, with one API across Linux, macOS, and Windows, a tokio mirror behind a feature flag, and identities that can be written to disk and restored after a restart (serde feature).

On Linux, cosca needs openat2 (kernel 5.6 or newer, and not blocked by a seccomp filter or an LSM): it reads /proc only through a checked directory fd. Without it a by-pid identity read fails with Error::Unsupported saying so. On kernels before 6.9, which read a child's identity from /proc, a spawn keeps such a child without an identity (Child::id() is None); a tree-walk spawn needs the identity and fails with that error inside Error::MayHaveStarted, since the child has already started when the spawn reads its identity.

The API is not stable; expect breaking changes in any 0.x release.

Platform requirements, including Linux 5.6 or newer, are in the crate documentation.

The design rules every change follows are in docs/principles.md.

Running tests

Run the suite with cargo nextest (cargo install cargo-nextest): cargo nextest run. It's what CI runs, and nextest isolates each test in its own process.

The suite signals process groups and creates cgroup leaves and Job Objects, so run it in a VM (scripts/devvm), a container or CI, never directly on your machine. See principle 10.

The harness

The lib and every integration test target run on skuld (harness = false in Cargo.toml), not libtest. Each target's main calls libtest_names(), so test names keep libtest's module::path::name shape for nextest filters, and require_known_labels(), so a name in SKULD_LABELS that the binary doesn't declare fails at startup instead of selecting nothing (or, negated, everything). Labels are declared in src/test_harness.rs. SKULD_LABELS=<label> selects a lane's tests, and only skuld binaries honour it: libtest binaries ignore it and would run whole.

Libtest's #[test] never runs in a harness = false target, so .github/libtest-lint fails on any such attribute in a tracked source file, whatever its cfg, and on any target left on the default harness. It parses source and needs no build of the crate, so it runs in about a second as a prek hook and in CI's Lint job.

Doc tests

nextest doesn't run doctests, so CI runs them separately with cargo test --doc. cosca has none today. Claude Code agents in this repo deny plain cargo test (.claude/settings.json), so an agent can't run that step locally either.

Test groups

A test group is declared once in src/test_groups.rs and gates its tests the way principles 9 and 10 describe: COSCA_TEST_<GROUP>=0 turns the group off (its tests report as ignored), and a system-affecting group also fails without COSCA_TEST_<GROUP>_CONSENT=1. To keep a group off on your machine without exporting variables each time, set the =0 switches in the [env] table of ~/.cargo/config.toml.

ELEVATION is an ordinary group of this kind: tests that really elevate with sudo, doas, pkexec, osascript or ShellExecuteEx(runas). COSCA_TEST_ELEVATION=0 turns it off, and it needs COSCA_TEST_ELEVATION_CONSENT=1. Run it only in a sandbox, set up as the elevation_* jobs in .github/workflows/ci.yaml do (passwords, polkit, doas). Those setups change system policy (the polkit rule, the macOS authorization right, the Windows ConsentPromptBehaviorAdmin value), so undo them when you are done: unattended-gui-elevation.py saves the prior state and restores it with --revert (same guard and root check as enabling; on Linux pass the same --user), and elevation-lane-sudoers.sh --cleanup and elevation-lane-linux.sh --cleanup undo the rest.

STRAY_SIGNAL (COSCA_TEST_STRAY_SIGNAL) holds the tests that may signal an unrelated process of yours. It needs COSCA_TEST_STRAY_SIGNAL_CONSENT=1, which CI sets; on your own machine set COSCA_TEST_STRAY_SIGNAL=0 to skip it, or run it in a container or VM.

Tests that need root

Two test groups run only as root (principles 9 and 10 in docs/principles.md):

  • ROOT (COSCA_TEST_ROOT): the library tests that need a DAC bypass (SKULD_LABELS=root).
  • UID_SWITCH (COSCA_TEST_UID_SWITCH): foreign_kill_surfaces_permission_denied in tests/process_root.rs. It runs as real root, spawns children under two other real uids and re-execs itself as one of them.

For each group, =0 turns it off: its tests report as ignored. Otherwise COSCA_TEST_<GROUP>_CONSENT=1 is required, and without it the test fails. An ordinary (unprivileged) run sets both switches to 0. CI does this workflow-wide; the root lanes in .github/workflows/ci.yaml turn ROOT on, and turn UID_SWITCH on only in the lanes that are real root (Linux root, DAC_READ_SEARCH, foreign TMPDIR, and macOS).

Run them in a throwaway container. Two steps, because --network none (below) cannot itself fetch anything: first a networked step populates a named CARGO_HOME volume with cosca's own dependencies and cargo-nextest itself, then the actual test run is fully offline and network-isolated:

( set -e
docker volume create cosca-root-test-cargo-home >/dev/null
docker volume create cosca-root-test-target >/dev/null

# 1. Networked: fetch dependencies and install cargo-nextest into the shared CARGO_HOME.
docker run --rm \
    -v "$PWD":/repo:ro \
    -v cosca-root-test-cargo-home:/usr/local/cargo \
    -v cosca-root-test-target:/target \
    -e CARGO_TARGET_DIR=/target \
    -w /repo \
    rust:1 \
    bash -c 'cargo fetch --locked && cargo install cargo-nextest --locked --quiet'

# 2. Offline and network-isolated: the two groups' tests (`PRIVILEGE_TESTS` in ci.yaml). Each
#    _CONSENT=1 consents to that group inside the sandbox; `=0` on the group would skip it.
docker run --rm --network none \
    -v "$PWD":/repo:ro \
    -v cosca-root-test-cargo-home:/usr/local/cargo:ro \
    -v cosca-root-test-target:/target \
    -e CARGO_TARGET_DIR=/target \
    -e CARGO_NET_OFFLINE=true \
    -e SKULD_LABELS=root \
    -e COSCA_TEST_ROOT_CONSENT=1 \
    -e COSCA_TEST_UID_SWITCH_CONSENT=1 \
    -w /repo \
    rust:1 \
    cargo nextest run --offline --no-tests=fail -E "binary(cosca) | binary(process_root)"
)
docker volume rm cosca-root-test-cargo-home cosca-root-test-target
  • The container's default user is already root, so no sudo is needed inside it; the two _CONSENT=1 variables unlock the tests' real privilege and stay inside the container. The repo is mounted read-only and the build goes to throwaway named volumes, so nothing on the host is touched.
  • Don't lift the inner cargo nextest run out of the container and run it with sudo on the host: the consent variables are real, standing consent to switch uids and spawn/kill processes, and this project's own rule is that system-affecting tests run in a container or VM, never against this machine's own services.

Checks

CI verifies the code in two ways only: prek hooks and tests. The Lint job runs prek run --all-files, minus the platform-dependent hooks (cargo-clippy, cargo-doc: they check the host target); every Test lane runs cargo-doc for its own platform after its tests, and the three lanes marked lint in the matrix run cargo-clippy last. A new check is therefore a hook in prek.toml or a test, and runs on a developer's machine as it does in CI, so each hook has to be fast enough for that. cargo-clippy is a pre-push hook (prek run --hook-stage pre-push cargo-clippy); the rest run on commit. The musl-check hook needs rustup target add x86_64-unknown-linux-musl.

License

Copyright 2026, Anna Zhukova

This project is licensed under the Apache 2.0 license. The license text can be found at LICENSE.

src/quote/posix.rs is a Rust port of the internal/foundation/shlex package from JetBrains qodana-cli, used under that project's Apache 2.0 license.