Skip to main content

proc_tree/
types.rs

1//! Data types for the process tree.
2
3/// Process info for a single PID.
4#[derive(Clone, Debug, PartialEq, Eq)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub struct ProcessInfo {
7    /// Binary name from `/proc/{pid}/comm` (truncated to 15 chars by kernel).
8    /// Used for process tree matching.
9    comm: String,
10    /// Full command line from `/proc/{pid}/cmdline`.
11    /// Falls back to `comm` for kernel threads with empty cmdline.
12    cmd: String,
13    /// Username (from UID lookup via `/etc/passwd`).
14    user: String,
15    /// Parent PID.
16    ppid: u32,
17    /// Thread group ID.
18    tgid: u32,
19    /// Process start time in nanoseconds since boot.
20    /// Used for PID reuse detection.
21    start_time_ns: u64,
22}
23
24impl ProcessInfo {
25    /// Create a new `ProcessInfo`.
26    pub fn new(
27        comm: String,
28        cmd: String,
29        user: String,
30        ppid: u32,
31        tgid: u32,
32        start_time_ns: u64,
33    ) -> Self {
34        Self {
35            comm,
36            cmd,
37            user,
38            ppid,
39            tgid,
40            start_time_ns,
41        }
42    }
43
44    /// Binary name from `/proc/{pid}/comm` (truncated to 15 chars).
45    /// Use this for process tree matching.
46    pub fn comm(&self) -> &str {
47        &self.comm
48    }
49
50    /// Full command line from `/proc/{pid}/cmdline`.
51    /// Use this for display/logging.
52    pub fn cmd(&self) -> &str {
53        &self.cmd
54    }
55
56    /// Username (from UID lookup via `/etc/passwd`).
57    pub fn user(&self) -> &str {
58        &self.user
59    }
60
61    /// Parent PID.
62    pub fn ppid(&self) -> u32 {
63        self.ppid
64    }
65
66    /// Thread group ID.
67    pub fn tgid(&self) -> u32 {
68        self.tgid
69    }
70
71    /// Process start time in nanoseconds since boot.
72    /// Used for PID reuse detection.
73    pub fn start_time_ns(&self) -> u64 {
74        self.start_time_ns
75    }
76}