proc-tree
Linux process tree — snapshot from /proc, incremental maintenance via fork/exec/exit events, ancestry chain queries, and pstree-style display.
Overview
proc-tree provides a unified interface for maintaining a Linux process tree with O(1) child lookups, incremental updates from process events, and ancestry chain queries. It supports both one-shot snapshots from /proc and real-time updates via fork/exec/exit events, with PID reuse detection and thread-safe storage.
Why proc-tree?
Unlike simple process listing tools that only show a point-in-time snapshot, proc-tree maintains an in-memory process tree that can be incrementally updated as processes fork, exec, and exit. This makes it ideal for tools that need to track process hierarchies over time, build ancestry chains, or find processes by name or user. The library's unified storage interface and zero-allocation iteration patterns make it suitable for high-performance system monitoring applications.
Usage
Add to your Cargo.toml:
[]
= "0.5.0"
Requirements
- Linux with
/procfilesystem - No special capabilities required for snapshot mode
CAP_NET_ADMINfor real-time event updates via proc-connector
Quick start
use ;
let store = new; // TTL in seconds
snapshot.expect;
// Resolve any PID
let info = resolve.unwrap;
println!; // "systemd" (binary name)
println!; // "/usr/lib/systemd/systemd --user" (full cmdline)
// Render pstree-style tree
println!;
Process matching
Use comm() for process tree matching — it returns the binary name from /proc/pid/comm:
use ;
let store = new;
snapshot.expect;
// Check if current process is a descendant of "bash"
let my_pid = id;
if is_descendant
// Find all bash processes
let bash_pids = find_by_cmd;
Ancestry chains
build_chain_string() returns a JSON array, build_chain_links() returns Vec<ProcessLink>:
use ;
let store = new;
snapshot.expect;
let my_pid = id;
// JSON string (for logging / serialization)
let json = build_chain_string;
println!;
// [{"pid":1234,"comm":"touch","cmd":"touch /tmp/foo","user":"root"}, ...]
// Structured data (for programmatic access)
let links = build_chain_links;
for link in &links
Serde support
serde feature is enabled by default — ProcessInfo and ProcessLink support Serialize/Deserialize:
[]
= "0.5.0"
To disable:
[]
= { = "0.5.0", = false }