proc_connector/consts.rs
1//! Kernel constants for the Linux Process Event Connector.
2//!
3//! All values are derived from Linux kernel headers:
4//! - `<linux/netlink.h>`
5//! - `<linux/connector.h>`
6//! - `<linux/cn_proc.h>`
7
8// ---------------------------------------------------------------------------
9// Netlink protocol constants
10// ---------------------------------------------------------------------------
11
12/// Netlink protocol family for the Connector.
13pub const NETLINK_CONNECTOR: i32 = 11;
14
15// NLMSG_* message types
16/// No-operation message type (ignored by netlink).
17pub const NLMSG_NOOP: u16 = 1;
18/// Error message type: contains `nlmsgerr` struct.
19pub const NLMSG_ERROR: u16 = 2;
20/// Done message type: marks the end of a multi-part message.
21pub const NLMSG_DONE: u16 = 3;
22/// Overrun message type: data was lost due to buffer overflow.
23pub const NLMSG_OVERRUN: u16 = 4;
24/// Minimum valid message type for application-specific messages.
25pub const NLMSG_MIN_TYPE: u16 = 16;
26
27/// Alignment boundary for netlink message headers (4 bytes).
28pub const NLMSG_ALIGNTO: usize = 4;
29
30/// Round `len` up to the nearest multiple of `NLMSG_ALIGNTO`.
31#[inline]
32pub const fn nlmsg_align(len: usize) -> usize {
33 (len + NLMSG_ALIGNTO - 1) & !(NLMSG_ALIGNTO - 1)
34}
35
36/// Total header length of `nlmsghdr` after alignment.
37#[inline]
38pub const fn nlmsg_hdrlen() -> usize {
39 nlmsg_align(size_of_nlmsghdr())
40}
41
42/// Full message length: `len` bytes of payload plus aligned header.
43#[inline]
44pub const fn nlmsg_length(len: usize) -> usize {
45 len + nlmsg_hdrlen()
46}
47
48/// Size of `struct nlmsghdr` in bytes (without alignment).
49pub const SIZE_NLMSGHDR: usize = 16;
50
51/// Minimum size of `struct nlmsgerr` (error code + original header).
52pub const SIZE_NLMSGERR: usize = 20;
53
54#[inline]
55const fn size_of_nlmsghdr() -> usize {
56 SIZE_NLMSGHDR
57}
58
59// Netlink socket options
60/// Socket option to disable `ENOBUFS` errors on recv.
61pub const NETLINK_NO_ENOBUFS: i32 = 5;
62
63// NLM_F flags
64/// Netlink message flag: this is a request message.
65pub const NLM_F_REQUEST: u16 = 1;
66
67// ---------------------------------------------------------------------------
68// Connector constants
69// ---------------------------------------------------------------------------
70
71/// Connector index for process events.
72pub const CN_IDX_PROC: u32 = 1;
73/// Connector value for process events.
74pub const CN_VAL_PROC: u32 = 1;
75
76/// Multicast operation: start listening.
77pub const PROC_CN_MCAST_LISTEN: u32 = 1;
78/// Multicast operation: stop listening.
79pub const PROC_CN_MCAST_IGNORE: u32 = 2;
80
81/// Size of `struct cn_msg` header (excluding flexible `data` array).
82pub const SIZE_CN_MSG: usize = 20;
83
84/// Maximum message size for the connector protocol.
85pub const CONNECTOR_MAX_MSG_SIZE: usize = 16384;
86
87// ---------------------------------------------------------------------------
88// Process event constants
89// ---------------------------------------------------------------------------
90
91/// No event / ACK (used for subscription acknowledgement).
92pub const PROC_EVENT_NONE: u32 = 0x00000000;
93/// A process was forked.
94pub const PROC_EVENT_FORK: u32 = 0x00000001;
95/// A process executed a new program (exec).
96pub const PROC_EVENT_EXEC: u32 = 0x00000002;
97/// Real/effective UID changed.
98pub const PROC_EVENT_UID: u32 = 0x00000004;
99/// Real/effective GID changed.
100pub const PROC_EVENT_GID: u32 = 0x00000040;
101/// Session ID changed.
102pub const PROC_EVENT_SID: u32 = 0x00000080;
103/// ptrace attach/detach.
104pub const PROC_EVENT_PTRACE: u32 = 0x00000100;
105/// Process name (comm) changed.
106pub const PROC_EVENT_COMM: u32 = 0x00000200;
107/// Process dumped core.
108pub const PROC_EVENT_COREDUMP: u32 = 0x40000000;
109/// Process exited.
110pub const PROC_EVENT_EXIT: u32 = 0x80000000;
111
112// ---------------------------------------------------------------------------
113// proc_event struct layout helpers
114// ---------------------------------------------------------------------------
115
116/// Offset from `proc_event` base to `event_data` union.
117///
118/// `proc_event` layout:
119/// - `what` (u32, 4 bytes)
120/// - `cpu` (u32, 4 bytes)
121/// - `timestamp_ns` (u64, 8 bytes)
122/// - `event_data` (union, varies)
123pub const PROC_EVENT_HEADER_SIZE: usize = 16;
124
125/// Per-event sub-structure sizes (all within the `event_data` union):
126/// Size of fork event data (4 × i32: parent_pid, parent_tgid, child_pid, child_tgid).
127pub const SIZE_FORK_EVENT: usize = 16;
128/// Size of exec event data (2 × i32: pid, tgid).
129pub const SIZE_EXEC_EVENT: usize = 8;
130/// Size of uid/gid event data (2 × i32 + ruid/rgid union + euid/egid union).
131pub const SIZE_ID_EVENT: usize = 16;
132/// Size of session ID event data (2 × i32: pid, tgid).
133pub const SIZE_SID_EVENT: usize = 8;
134/// Size of ptrace event data (4 × i32: pid, tgid, tracer_pid, tracer_tgid).
135pub const SIZE_PTRACE_EVENT: usize = 16;
136/// Size of comm event data (2 × i32 + char\[16\]: pid, tgid, comm).
137pub const SIZE_COMM_EVENT: usize = 24;
138/// Size of coredump event data (4 × i32: pid, tgid, parent_pid, parent_tgid).
139pub const SIZE_COREDUMP_EVENT: usize = 16;
140/// Size of exit event data (4 × i32 + u32 + u32: pid, tgid, exit_code, exit_signal, parent_pid, parent_tgid).
141pub const SIZE_EXIT_EVENT: usize = 24;
142
143// ---------------------------------------------------------------------------
144// proc_event sub-struct field offsets (relative to event_data union base)
145// ---------------------------------------------------------------------------
146
147// --- fork ---
148/// Offset to parent PID in fork event data.
149pub const FORK_PARENT_PID: usize = 0;
150/// Offset to parent TGID in fork event data.
151pub const FORK_PARENT_TGID: usize = 4;
152/// Offset to child PID in fork event data.
153pub const FORK_CHILD_PID: usize = 8;
154/// Offset to child TGID in fork event data.
155pub const FORK_CHILD_TGID: usize = 12;
156
157// --- exec ---
158/// Offset to PID in exec event data.
159pub const EXEC_PID: usize = 0;
160/// Offset to TGID in exec event data.
161pub const EXEC_TGID: usize = 4;
162
163// --- id (uid/gid share same layout) ---
164/// Offset to PID in uid/gid event data.
165pub const ID_PID: usize = 0;
166/// Offset to TGID in uid/gid event data.
167pub const ID_TGID: usize = 4;
168/// Offset to real UID/GID in uid/gid event data.
169pub const ID_RUID_RGID: usize = 8;
170/// Offset to effective UID/GID in uid/gid event data.
171pub const ID_EUID_EGID: usize = 12;
172
173// --- sid ---
174/// Offset to PID in session ID event data.
175pub const SID_PID: usize = 0;
176/// Offset to TGID in session ID event data.
177pub const SID_TGID: usize = 4;
178
179// --- ptrace ---
180/// Offset to PID in ptrace event data.
181pub const PTRACE_PID: usize = 0;
182/// Offset to TGID in ptrace event data.
183pub const PTRACE_TGID: usize = 4;
184/// Offset to tracer PID in ptrace event data.
185pub const PTRACE_TRACER_PID: usize = 8;
186/// Offset to tracer TGID in ptrace event data.
187pub const PTRACE_TRACER_TGID: usize = 12;
188
189// --- comm ---
190/// Offset to PID in comm event data.
191pub const COMM_PID: usize = 0;
192/// Offset to TGID in comm event data.
193pub const COMM_TGID: usize = 4;
194/// Offset to comm string (16 bytes) in comm event data.
195pub const COMM_DATA: usize = 8;
196
197// --- coredump ---
198/// Offset to PID in coredump event data.
199pub const COREDUMP_PID: usize = 0;
200/// Offset to TGID in coredump event data.
201pub const COREDUMP_TGID: usize = 4;
202/// Offset to parent PID in coredump event data.
203pub const COREDUMP_PARENT_PID: usize = 8;
204/// Offset to parent TGID in coredump event data.
205pub const COREDUMP_PARENT_TGID: usize = 12;
206
207// --- exit ---
208/// Offset to PID in exit event data.
209pub const EXIT_PID: usize = 0;
210/// Offset to TGID in exit event data.
211pub const EXIT_TGID: usize = 4;
212/// Offset to exit code in exit event data.
213pub const EXIT_CODE: usize = 8;
214/// Offset to exit signal in exit event data.
215pub const EXIT_SIGNAL: usize = 12;
216/// Offset to parent PID in exit event data.
217pub const EXIT_PARENT_PID: usize = 16;
218/// Offset to parent TGID in exit event data.
219pub const EXIT_PARENT_TGID: usize = 20;