Expand description
Actually Portable Executable (APE / cosmocc) support.
Cosmopolitan APE binaries start with a shell prologue rather than a native
image header. A host without a binfmt_misc registration for them – stock
NixOS among them – refuses them with ENOEXEC (“Exec format error”), and
only a shell knows to retry.
This crate launches them through a loader: an explicit one
(LOADER_ENV), else the loader embedded in the image (the default
ape-loader feature; installed into a private, exec-capable cache, or a
sealed memfd), else an installed ape, else the host shell.
- Spawns whose program and environment are known plan the loader before
the spawn:
SpawnSpec/AsyncProcess, argvNativeProcesses, andcommand/tokio_commandfor callers building their own command. - A caller-built command keeps every caller setting and is retried once
after a refusal, through
execvp’s POSIXENOEXECshell rule (spawn_std). fork_guard/exclusive_fork_guardare the process-wide fork lock that keeps a freshly written loader out of concurrently forked children (ETXTBSY); a spawner outside this crate should holdfork_guardacross its spawn.
Structs§
- ApeLaunch
- How to run one APE image:
loader image args.... - ApeOptions
- What a launch plan consults, as the child will see it.
Enums§
- Loader
Kind - Which loader a planned launch runs the image through.
Constants§
- CACHE_
DIR_ ENV - Environment variable naming the preferred directory for loaders extracted from APE images. Tried before the host defaults; like them it is used only when owned by the current user, not group/world-writable, and on an exec-capable mount.
- LOADER_
ENV - Environment variable naming an explicit APE loader (an
apebinary or a POSIX shell). Read from the child environment first, then this process’s. - MAGICS
- Leading bytes of every APE image:
MZqFpD='(the standard header, also a valid DOS/PEMZstub),jartsr='(non-Windows),APEDBG='(debug). - NEEDS_
LOADER - An APE image is not a native Linux executable.
Functions§
- command
- A
std::process::Commandforprogramthat runs an APE image through its planned loader, with this process’s environment as the child’s. - embedded_
loader - The host-runnable loader embedded in
image, installed into the first usablecache_dirsentry (or, on Linux, a sealed memfd).Nonewhen the host runs APE natively, the image carries no valid loader for this host, or nothing could be materialized. - exclusive_
fork_ guard - Hold while a file that will be executed is open for writing.
- extract_
loader - Inflate and validate the Linux ELF loader
imageembeds formachine("x86_64"or"aarch64"). - fork_
guard - Hold across a spawn so no executable is being written meanwhile.
- gunzip
- Decode one gzip member (RFC 1952), refusing output over
limitbytes. - is_
ape_ file - Whether the file at
pathis an APE image. Unreadable files are not. - is_
ape_ header - Whether
headerbegins with an APE magic. - is_
exec_ format_ error - The kernel refused the image’s format.
- loader_
blob_ range (skip, count)of the gzip’d Linux loader formachine.- plan_
launch - Plan how to run
programif it resolves to an APE image. - prepare_
std_ retry - Prepare a caller-built command to be spawned again as an APE image.
- resolve_
program - Resolve
programto the file the child’sexecvpwould execute: a path with a separator is taken as-is (relative tocurrent_dirwhen given), a bare name is searched onpath. The result is absolute. - retry_
while_ busy - Run
spawnagain while the host reports the program busy (ETXTBSY). - spawn_
std - Spawn a caller-built command under the fork lock, retrying while the
program is transiently busy (
ETXTBSY) and once if the host refused it as an APE image.