Skip to main content

proc_tree/
lib.rs

1//! # proc-tree
2//!
3//! Linux process tree: snapshot, incremental maintenance via fork/exec events,
4//! ancestry chain queries, and PID reuse detection.
5//!
6//! ## Quick Start
7//!
8//! ```rust
9//! use proc_tree::{ProcessStore, ProcessInfo, ProcEvent};
10//! use proc_tree::{snapshot, resolve, handle_events, build_chain_string};
11//!
12//! // Implement your own storage (or use a provided example)
13//! # struct MyStore;
14//! # impl ProcessStore for MyStore {
15//! #     fn get_process(&self, pid: u32) -> Option<ProcessInfo> { None }
16//! #     fn insert_process(&self, pid: u32, info: ProcessInfo) {}
17//! #     fn remove_process(&self, pid: u32) -> Option<ProcessInfo> { None }
18//! #     fn all_pids(&self) -> Vec<u32> { vec![] }
19//! #     fn children_of(&self, pid: u32) -> Vec<u32> { vec![] }
20//! # }
21//!
22//! let store = MyStore;
23//!
24//! // Seed from /proc
25//! snapshot(&store);
26//!
27//! // Resolve a PID
28//! if let Some(info) = resolve(&store, 1) {
29//!     println!("PID 1: cmd={}, user={}", info.cmd, info.user);
30//! }
31//!
32//! // Build ancestry chain
33//! let s = build_chain_string(&store, 1234);
34//! println!("Chain: {}", s);
35//!
36//! // Handle events (returns ExitedProcess handles)
37//! let exited = handle_events(&store, &[
38//!     ProcEvent::Fork { child_pid: 200, parent_pid: 100, timestamp_ns: 0 },
39//! ]);
40//! // Caller decides when to remove exited processes
41//! for ep in exited {
42//!     ep.remove(&store);
43//! }
44//! ```
45//!
46//! ## PID Reuse Detection
47//!
48//! When a process exits and its PID is reused by a new process, cached data
49//! becomes stale. `ProcessStore` implementations should compare `start_time_ns`
50//! with the current `/proc` value to detect reuse.
51
52mod default_store;
53mod ops;
54mod proc;
55mod traits;
56mod tree;
57mod types;
58
59// Public API — types
60pub use types::ProcessInfo;
61
62// Public API — traits
63pub use traits::ProcessStore;
64
65// Public API — default implementations
66pub use default_store::DefaultStore;
67
68// Public API — tree types
69pub use tree::{ExitedProcess, ProcEvent, ProcessLink};
70
71// Public API — operations
72pub use ops::{
73    build_chain_links, build_chain_string, children, descendants, display, find_by_cmd,
74    find_by_user, handle_event, handle_events, is_descendant, resolve, siblings, snapshot,
75    tree_len,
76};
77
78// Public API — proc utilities
79pub use proc::{parse_proc_entry, read_proc_comm, read_proc_start_time_ns, uid_to_username};