1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//! The macOS pid listing: `proc_listallpids` sized, filled and grown until the kernel reports
//! fewer pids than the buffer holds. [`host_pids`] is `HostKernel`'s read.
//!
//! One thing worth knowing:
//!
//! - `proc_listallpids`' fill path caps its walk at `min(nprocs + 20, our buffer capacity)`,
//! using an UNLOCKED read of `nprocs` taken before the process list locks (confirmed
//! against XNU's `bsd/kern/proc_info.c`). Our buffer capacity is the sizing call's answer
//! (itself `nprocs + 20`, at sizing time) plus `HEADROOM`; the `+ 20` term is on both sides
//! of the comparison and cancels, so our buffer exceeds the kernel's fill-time cap only
//! while fewer than `HEADROOM` net new processes are created between the sizing call and
//! the fill call. Above that threshold OUR buffer is the binding limit instead, the fill
//! saturates, and `collect_pids`'s grow loop doubles and retries — exactly the case it
//! exists for. `written < cap` is therefore not an airtight completeness proof: even once
//! the buffer has grown past being the limit, a narrow, kernel-internal, userspace-invisible
//! race remains on every fill — ≥20 net new processes inside THAT fill's own
//! unlocked-read-to-locked-walk window. Nothing in this module can close it without
//! changing the underlying syscall.
/// Extra pid slots asked for on top of the kernel's sizing answer, so the common case
/// needs exactly one fill: the process set can grow between the sizing call and the fill.
const HEADROOM: usize = 16;
/// Buffer capacity, in PIDS, for a sizing answer of `needed` pids.
///
/// `proc_listallpids` is one of the libproc wrappers that CONVERTS before returning: it
/// calls `proc_listpids` — which answers in bytes — and divides by `sizeof(int)`. Both its
/// sizing answer and its fill answer are therefore PID COUNTS, unlike `proc_listpids` and
/// `proc_listpidspath`, which answer in bytes.
///
/// `saturating_add`, not `+`: `needed` ultimately comes from `interpret_written`'s
/// `libc::c_int`-derived bound today, but `capacity_for` is a private seam a future caller
/// could invoke with an unvalidated `usize`, and a plain `+` would panic in debug or
/// silently wrap in release for `needed` near
/// `usize::MAX`. Saturating is safe here: `collect_pids`'s `allocate` step rejects any
/// capacity whose byte size exceeds `i32::MAX` before ever allocating, so a saturated
/// `usize::MAX` capacity fails loudly one call later, not silently.
/// `proc_listallpids`' buffer-size argument — an `int` of BYTES — for a buffer of
/// `pid_count` pids. An error rather than a wrapped cast: a wrapped size would describe an
/// allocation that does not exist.
///
/// A negative `buffersize` is not rejected by `proc_listallpids` itself (measured: it is
/// silently read as a large unsigned byte count) - so callers of this function's output
/// depend on it never producing one. That contract is asserted below, not just documented:
/// `checked_mul` operates on `usize` (never negative) and `c_int::try_from` only succeeds
/// for values in `0..=c_int::MAX`, so the assertion cannot fire without a bug in this
/// function or in `c_int::try_from` itself - it exists to make that invariant loud rather
/// than assumed.
/// A completed fill. `Debug` so tests can call `.expect_err(..)` on a `Result<Filled, _>`.
/// Interpret `proc_listallpids`' raw return value as a pid count or an error.
///
/// Measured: a genuine kernel failure (a bad buffer pointer, EFAULT) reports as `0`,
/// never negative — so `written <= 0` is the correct failure guard. Narrowing it to
/// `written < 0` would read a real failure as a successful empty pid list. Applies
/// identically to the sizing form's return (also `proc_listallpids`, also measured
/// success-as-positive / failure-as-`0`).
///
/// Must be called immediately after the syscall whose return value it interprets —
/// `last_os_error` reads whatever errno the most recently invoked libc call left behind.
/// One `proc_listallpids` fill into `pids`, answering in PIDS (see [`capacity_for`]).
///
/// Its `size_argument_for` call is unreachable-on-error via the only production call chain
/// (`collect_pids` already validates the identical `cap` immediately before constructing
/// `pids` from it, so `pids.len()` here can never be a size `collect_pids` didn't already
/// accept) - kept anyway as a defensive contract (same reasoning as `capacity_for`'s
/// `saturating_add`, above).
/// Fill a growing buffer until the kernel's answer proves the buffer was not the limit —
/// until it reports FEWER pids than the buffer could hold.
///
/// The kernel fills whatever buffer it is handed and reports how many pids it wrote; it
/// does NOT report truncation. An answer equal to the capacity is therefore ambiguous —
/// the list may have been cut off at the buffer's edge — and the only way to tell is to
/// ask again with more room. This is why a fixed headroom is not enough: it narrows the
/// window in which the answer is a silent truncation instead of closing it.
///
/// Termination is by real conditions, never a counter. Each round doubles the capacity
/// actually produced (`pids.len()`, not the `cap` requested — see below), so either the
/// kernel's answer fits (the process table is bounded by `maxproc`) or the byte size stops
/// fitting in an `int` and `allocate` errors. The production `allocate_pids` checks the size
/// BEFORE allocating, so the error is reachable without first attempting the allocation it
/// would have rejected, and doubling cannot overflow because `size_argument_for` refuses
/// every capacity above `i32::MAX / 4` first.
///
/// `fill` and `allocate` are both injected: `fill` so the doubling logic can be pinned against
/// an exact, host-independent total and its error / over-report branches driven
/// deterministically - a live kernel CAN be forced to saturate on demand by starting from a
/// deliberately small `cap` (see `collect_pids_grows_against_the_live_kernel`), but not to
/// report a specific total, an error, or a bogus over-report; `allocate` so both the
/// doubling-then-refusal path and an allocation failure can be driven deterministically
/// without a real run to the true ~536M-pid boundary or genuine memory pressure. Production
/// passes [`fill_from_kernel`] and [`allocate_pids`]; that exact composition is pinned live in
/// `collect_pids_grows_against_the_live_kernel`.
///
/// `cap == 0` is refused up front as a real `Err`, in every build profile: a zero capacity
/// can never grow (`written < n` is `0 < 0` = false, so the "saturated, retry" branch would
/// spin at `n == 0` forever), and a hang is worse than the truncation this function exists
/// to prevent.
/// The production `allocate` step: validate the size argument, then reserve fallibly.
/// `try_reserve_exact`, not `vec![0; cap]`: a failed reservation of this buffer is an `Err`
/// the listing's caller reports (`process_parents`: `Unassessable`; `snapshot`: a blind
/// pass) rather than the abort `handle_alloc_error` would take. `resize` after a
/// successful reservation cannot reallocate, so it cannot abort either.
/// The two live syscalls wired together. Separate from [`host_pids`] so tests can see the
/// round count and the error.
/// Every pid the real kernel will list, or why it would not: [`HostKernel::pids`](crate::identity::HostKernel)'s read.
pub