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): every unsafe block here
is a single foreign call with its preconditions stated beside it, and nothing
unsafe is exported.
Today it answers one 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 the crate’s only unsafe code. macOS has no pidfd, so a signal is a plainkillsent right after the checks; seeProcess::signal. - Anywhere else:
Process::openreportsstd::io::ErrorKind::Unsupported.
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.
Structs§
- 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.
- Resource
Usage - One reading of a process’s memory and cumulative CPU time.
Enums§
- Memory
Kind - What
ResourceUsage::memory_bytesmeasures. The platforms offer different figures, and they are not interchangeable. - Signal
- A signal
Process::signalcan send.
Constants§
- PROCESS_
IDENTITY_ SUPPORTED - True where
Processcan identify and signal 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§
- 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. - resource_
usage - Memory and cumulative CPU time of the process holding
pid, read now. - 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.