Expand description
Operating-system primitives the subc daemon needs and cannot reach without unsafe code, each behind a small safe API.
The daemon crates forbid unsafe code. This crate is the one deliberate
exception (like subc-uptime and subc-cgroup): each unsafe block here is
a foreign call, or a read of a buffer such a call returned, with its
preconditions stated beside it, and nothing unsafe is exported.
Its central question: is the process now holding pid N the same
process the daemon spawned earlier? A pid alone cannot say, because the
kernel reuses pids once a process has been reaped. Process reads the two
facts that tell processes apart, the kernel’s start time for the pid and the
file identity (device and inode) of the executable image it runs, and sends
signals to it.
Sources, per platform:
- Linux: the start time is field 22 of
/proc/<pid>/stat(clock ticks since boot), and the executable isstatthrough/proc/<pid>/exe, which resolves to the running image even if its file has since been replaced or deleted. A pidfd is opened before either is read and signals go through it (pidfd_send_signal), so the process that was checked is the process that is signalled. No unsafe code is needed: rustix wraps both calls. - macOS: the start time is
kp_proc.p_starttimefromsysctlKERN_PROC_PID(microseconds since the epoch), and the executable is the pathproc_pidpathreports, thenstaton that path. These two calls are unsafe. macOS has no pidfd, so a signal is a plainkillsent right after the checks; seeProcess::signal. - Windows: creation time and forced stops use one retained process handle.
ExecutableCapturepins the resolved executable until a suspended spawn binds its volume and 128-bit file ID to that handle and creation time. - Anywhere else:
Process::openreportsstd::io::ErrorKind::Unsupported.
For persisted PID owners, process_identity reads versioned kernel start
identities and distinguishes alive, dead and unknown without spawning a
process. Its foreign calls are signal-zero kill on Unix and proc_pidinfo
on macOS. Only dead owners may be reclaimed; unknown owners stay protected.
It also reads how much memory and CPU time one process is using, for
reporting only; see resource_usage. On Linux that is procfs again; on
macOS it is proc_pid_rusage, plus mach_timebase_info to convert its CPU
times to nanoseconds, the other two unsafe calls in the crate.
For the machine as a whole, host reads total and available memory, the
load averages and the space on a filesystem, again for reporting only.
It also resolves per-user locations from account records rather than the
environment: the password-database home and effective uid on Unix, Darwin’s
per-user temporary directory on macOS, and the recorded profile folder on
Windows (profile_dir).
For the peer of an accepted local-endpoint connection (a Unix-domain
socket, or a named-pipe client on Windows), local_peer reads the
kernel’s record of the connecting process: its user, pid and executable,
and on macOS its code-signing identifier.
And it carries the launch nonce from the daemon to each module it spawns
over an inherited Unix pipe or a PID-authenticated Windows named pipe:
[launch_nonce] is the one cached reader every module uses. Windows keeps
an environment copy for old Windows readers until live source reports show
every module consuming the named pipe.
Re-exports§
pub use launch_nonce::LaunchNonceHandoff;pub use launch_nonce::launch_nonce;pub use launch_nonce::LaunchNonce;pub use launch_nonce::LaunchNonceError;pub use launch_nonce::LaunchNonceSource;pub use launch_nonce::LAUNCH_NONCE_ENV;pub use launch_nonce::LAUNCH_NONCE_FD;pub use launch_nonce::LAUNCH_NONCE_FD_ENV;pub use launch_nonce::LAUNCH_NONCE_PIPE_ENV;pub use launch_nonce::LAUNCH_NONCE_PIPE_FALLBACK_ENV;pub use registration_channel::registration_refusal;pub use registration_channel::supervised;pub use registration_channel::take_registration_channel;pub use registration_channel::RegistrationChannel;pub use registration_channel::RegistrationChannelError;pub use registration_channel::RegistrationRefusal;
Modules§
- admission_
gate - Keep a forked child before exec until its parent has recorded its identity.
- boot_id
- Kernel boot identity for persisted process containers.
- child_
exit - Non-reaping exit observation for supervisors that must decide the lifetime of a process container before releasing the leader’s pid.
- child_
spawn - One fork boundary for daemon and OS helpers. Spawns share the lock, while Unix socketpair creation holds it exclusively until both ends are CLOEXEC. A guard never survives into an await or a wait on a running process.
- config_
keychain - The configuration store’s sealing key and revision anchor in one keychain item. Only a valid team-signed macOS image can open the native store.
- host
- Facts about the machine as a whole: its memory, its load and the space on a filesystem, read now, for reporting only.
- launch_
nonce - The launch nonce: the secret the daemon gives each module it spawns, which the module presents to be admitted as itself.
- local_
peer - Who is at the other end of an accepted local-endpoint connection: a Unix-domain socket on macOS and Linux, a named-pipe client on Windows.
- named_
pipe - Owner-only, local-only Windows named pipes, behind a safe API.
- privacy_
identity - macOS responsibility isolation at exec. macOS charges privacy permissions
(Screen Recording, Accessibility, Files and Folders) to a process’s
“responsible process”, which is normally the program that launched it, so a
supervised module would otherwise borrow the daemon’s grants. The daemon
therefore launches each module through a trampoline:
ck-subcre-executed with a hidden first argument. The trampoline callsposix_spawnpwithPOSIX_SPAWN_SETEXEC, a Darwin flag that replaces the calling process’s image in place (likeexecve) instead of creating a child, after marking the spawn as disclaiming responsibility. The module keeps the trampoline’s pid, process group, pipes and inherited registration descriptors, and becomes its own responsible process. - process_
group - Process-group facts for recovery after a daemon no longer keeps its original group leader as an unreaped child to reserve the group’s numeric identity.
- process_
identity - PID-reuse-safe liveness checks using versioned kernel start-time identities.
- registration_
channel - The process’s single registration connection.
supervisedcaches the validation decision once, independently of whether the connection has been taken.take_registration_channeltransfers ownership exactly once; a second take is an error, never permission to connect asdirect. Refused descriptors are left untouched and reported byregistration_refusal. Errors after validation are fatal adoption errors, not refusals. - script_
pipe - Identity-bearing Unix script transport. The receiver validates before adopting a descriptor because runtimes may reuse inherited descriptor numbers for IPC.
- socket_
peer - Best-effort process descriptions for the peer of an accepted loopback TCP connection. These snapshots are not credentials: processes can exit, exec, or share a socket while a lookup is running. macOS libproc and Linux procfs enumerate socket holders; if more than one visible process holds the peer socket (for example after fork), the caller is ambiguous and unresolved. Windows’ TCP owner table reports one owning PID per connection, not every process with an inherited or duplicated socket handle, so it cannot detect that kind of sharing. None of these OS snapshots proves exclusive ownership.
- windows_
acl - Windows process hardening, with a no-op on other operating systems.
Structs§
- Durable
Replace Error - A replacement can be visible even when flushing its publication fails.
- File
Identity - Device and inode of a file: which file, independent of the name used to reach it.
- Observation
- What a live process looks like right now.
- Process
- A handle on the process holding one pid at the moment it was opened.
- Process
Group - A live program group. CPU includes its members’ reaped children on Unix.
- Process
Member - A process identity and its observed parent, session, program and resources.
- Process
Table - One process-table walk, reusable for any number of roots.
- Resource
Usage - One reading of a process’s memory and cumulative CPU time.
- Tree
Usage - Cumulative CPU and current memory of the observed members of a tree.
Enums§
- Executable
Access - Whether the running executable’s identity could be read. Permission denial is distinct on Linux because a non-dumpable process hides its executable while leaving its start time readable.
- Memory
Kind - What
ResourceUsage::memory_bytesmeasures. The platforms offer different figures, and they are not interchangeable. - Open
Regular Error - Signal
- A signal
Process::signalcan send.
Constants§
- PROCESS_
IDENTITY_ SUPPORTED - True where
Processcan identify and stop a process by pid. - RESOURCE_
USAGE_ SUPPORTED - True where
resource_usagecan read a live process. Elsewhere it always answersNone, and a caller can use this to say “not supported here” rather than “could not read”.
Functions§
- durable_
replace - Atomically replace
to, then flush the publication before returning. The caller must sync the source file’s contents first. Neither path may be on a different filesystem; there is deliberately no copy/delete fallback. An error retains whether publication occurred before durability failed. - effective_
uid - Read the process’s effective Unix uid from the kernel, without environment input.
- file_
identity - The device and inode of the file at
path, following symlinks.Noneif it cannot be read or the platform has no inode numbers. - open_
directory_ nofollow_ all - Open an absolute directory without following symlinks in any component.
- open_
regular_ nofollow - Open a regular input without following its leaf. Parent directories remain the caller’s responsibility. Nonblocking open prevents a FIFO substitution from hanging before the descriptor’s type can be checked.
- open_
regular_ nofollow_ all - Open an absolute regular file without following a symlink in any component. Directory descriptors anchor each lookup even if an ancestor is renamed.
- parent_
pid - The parent pid reported by the kernel, or
Nonewhen it cannot be read. On Windows a reused parent pid (created after the child) is rejected. - process_
start_ time_ ms - Kernel process creation time, in milliseconds since the Unix epoch.
- process_
start_ time_ precision_ ms - Resolution used by
process_start_time_ms, rounded up to milliseconds. - real_
user_ home_ dir - Read the real user’s home directory from the password database, using
getpwuid_r(getuid())rather thanHOMEor any other environment variable. Returns an error if the user has no entry or the home directory is not absolute. - resource_
usage - Memory and cumulative CPU time of the process holding
pid, read now. - retained_
file_ identity - Identify an already-open file, without resolving its mutable pathname again. Unix identities contain device and inode; Windows identities contain the volume serial and all sixteen file-id bytes. Content hashes remain separate.
- start_
time - The kernel start time of the process holding
pid, orNoneif there is none, it has already exited (a zombie waiting to be reaped counts as exited), or the platform has no source.