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::{TreeStore, CacheStore, PidNode, ProcInfo, 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 MyTree;
14//! # impl TreeStore for MyTree {
15//! #     fn get_node(&self, pid: u32) -> Option<PidNode> { None }
16//! #     fn insert_node(&self, pid: u32, node: PidNode) {}
17//! #     fn all_pids(&self) -> Vec<u32> { vec![] }
18//! # }
19//! # struct MyCache;
20//! # impl CacheStore for MyCache {
21//! #     fn get_info(&self, pid: u32) -> Option<ProcInfo> { None }
22//! #     fn insert_info(&self, pid: u32, info: ProcInfo) {}
23//! }
24//!
25//! let tree = MyTree;
26//! let cache = MyCache;
27//!
28//! // Seed from /proc
29//! snapshot(&tree, &cache);
30//!
31//! // Resolve a PID
32//! if let Some(info) = resolve(&cache, 1) {
33//!     println!("PID 1: cmd={}, user={}", info.cmd, info.user);
34//! }
35//!
36//! // Build ancestry chain
37//! let s = build_chain_string(&tree, &cache, 1234);
38//! println!("Chain: {}", s);
39//!
40//! // Handle events
41//! handle_events(&tree, &cache, &[
42//!     ProcEvent::Fork { child_pid: 200, parent_pid: 100, timestamp_ns: 0 },
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. `CacheStore` implementations should compare `start_time_ns`
50//! with the current `/proc` value to detect reuse.
51
52mod default_store;
53mod ops;
54pub mod proc;
55mod traits;
56mod tree;
57mod types;
58
59// Public API — types
60pub use types::{PidNode, ProcInfo};
61
62// Public API — traits
63pub use traits::{CacheStore, TreeStore};
64
65// Public API — default implementations
66pub use default_store::{DefaultCache, DefaultStore, DefaultTree};
67
68// Public API — tree types
69pub use tree::{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_start_time_ns};