Skip to main content

Crate subc_os

Crate subc_os 

Source
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 is stat through /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_starttime from sysctl KERN_PROC_PID (microseconds since the epoch), and the executable is the path proc_pidpath reports, then stat on that path. These two calls are unsafe. macOS has no pidfd, so a signal is a plain kill sent right after the checks; see Process::signal.
  • Windows: creation time and forced stops use one retained process handle. ExecutableCapture pins the resolved executable until a suspended spawn binds its volume and 128-bit file ID to that handle and creation time.
  • Anywhere else: Process::open reports std::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-subc re-executed with a hidden first argument. The trampoline calls posix_spawnp with POSIX_SPAWN_SETEXEC, a Darwin flag that replaces the calling process’s image in place (like execve) 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. supervised caches the validation decision once, independently of whether the connection has been taken. take_registration_channel transfers ownership exactly once; a second take is an error, never permission to connect as direct. Refused descriptors are left untouched and reported by registration_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§

DurableReplaceError
A replacement can be visible even when flushing its publication fails.
FileIdentity
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.
ProcessGroup
A live program group. CPU includes its members’ reaped children on Unix.
ProcessMember
A process identity and its observed parent, session, program and resources.
ProcessTable
One process-table walk, reusable for any number of roots.
ResourceUsage
One reading of a process’s memory and cumulative CPU time.
TreeUsage
Cumulative CPU and current memory of the observed members of a tree.

Enums§

ExecutableAccess
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.
MemoryKind
What ResourceUsage::memory_bytes measures. The platforms offer different figures, and they are not interchangeable.
OpenRegularError
Signal
A signal Process::signal can send.

Constants§

PROCESS_IDENTITY_SUPPORTED
True where Process can identify and stop a process by pid.
RESOURCE_USAGE_SUPPORTED
True where resource_usage can read a live process. Elsewhere it always answers None, 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. None if 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 None when 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 than HOME or 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, or None if there is none, it has already exited (a zombie waiting to be reaped counts as exited), or the platform has no source.

Type Aliases§

RetainedFileIdentity