cosca 0.7.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! Linux process-identity backend. A process's [`Key`] is the inode of its pidfd on pidfs (6.9+),
//! else the raw field-22 `starttime` (jiffies) from `/proc/<pid>/stat`. Opening the pid ([`prove`])
//! gives a proven key. Reading the pidfd the caller holds gives a key that the caller's later peek
//! through that pidfd checks. `is_running` polls the pidfd, or reads the process state before 6.9;
//! `created_at` reads the `starttime` (bracketed for a pidfs id) and uses `/proc/stat` `btime` and
//! `_SC_CLK_TCK`.

#[path = "linux/kernel.rs"]
pub(crate) mod kernel;
#[path = "linux/pid_stat.rs"]
pub(crate) mod pid_stat;
#[path = "linux/proc_view.rs"]
pub(crate) mod proc_view;
#[path = "linux/prove.rs"]
pub(crate) mod prove;

#[cfg(test)]
#[path = "linux/bracket_tests.rs"]
mod bracket_tests;

#[cfg(test)]
#[path = "linux/look_tests.rs"]
mod look_tests;

#[cfg(test)]
#[path = "linux/read_tests.rs"]
mod read_tests;

#[cfg(test)]
#[path = "linux/read_namespace_tests.rs"]
mod read_namespace_tests;

use std::os::fd::AsFd as _;
use std::time::{Duration, SystemTime};

use rustix::io::Errno;
use rustix::process::{Pid, PidfdFlags};

use self::kernel::Kernel;
use self::proc_view::ProcDir;
use self::prove::Proof;
use super::probe::{classify_unreadable, SignalProbe};
use super::stat_parse::{parse_ppid, parse_starttime_jiffies};
use super::{Existence, Liveness, OrderedRead, ParentRead, RawPid, Resolved, TokenKind, UnknownCause};
use crate::error::Error;

/// `kill(pid, 0)` — existence/permission check only, no signal delivered. The target
/// validation lives in the pure `probe` module so it is executed on every host.
fn signal_probe(kernel: &dyn Kernel, pid: RawPid) -> SignalProbe {
    let Some(target) = super::probe::signal_target(pid).and_then(Pid::from_raw) else {
        return SignalProbe::NotAPid;
    };
    match kernel.signal_probe(target) {
        Ok(()) => SignalProbe::Signalable,
        Err(Errno::SRCH) => SignalProbe::NoSuchProcess,
        Err(_) => SignalProbe::Denied,
    }
}

/// `pid`'s `stat` through a `/proc` that is shown to describe this process's own pid namespace,
/// else `Unknown` unless `kill(pid, 0)` says the pid is gone: an outer namespace's `/proc/<pid>`
/// names another process, so what it says is no answer about `pid`.
fn read_stat(kernel: &dyn Kernel, pid: RawPid) -> Resolved<Vec<u8>> {
    read_stat_explained(kernel, pid).0
}

/// [`read_stat`], and the unusable view that made it `Unknown`, when one did.
fn read_stat_explained(kernel: &dyn Kernel, pid: RawPid) -> (Resolved<Vec<u8>>, Option<proc_view::ViewUnreadable>) {
    match proc_view::proc_view_with(kernel).into_dir() {
        Ok(dir) => (stat_by_pid(kernel, &dir, pid), None),
        Err(why) => (
            resolve_unreadable(
                kernel,
                pid,
                format_args!("the /proc view could not be established: {why}"),
            ),
            Some(why),
        ),
    }
}

/// [`read_stat`] via `openat` on `proc_dir`, not a `/proc` path lookup. A failed read is `Gone`
/// only when `kill(pid, 0)` says so ([`resolve_unreadable`]); a whole-table scan has the other
/// policy (`pid_stat::read_in_scan`).
fn stat_by_pid(kernel: &dyn Kernel, proc_dir: &ProcDir, pid: RawPid) -> Resolved<Vec<u8>> {
    match pid_stat::read_stat_file(kernel, proc_dir, pid) {
        Ok(bytes) => Resolved::Found(bytes),
        Err(e) => resolve_unreadable(kernel, pid, e),
    }
}

/// What `pid`'s `stat` being unavailable (`why`) says about it: `Gone` only if `kill(pid, 0)`
/// says `ESRCH`, else `Unknown`. `ErrorKind` alone is not enough: under a `hidepid` mount
/// another user's `/proc/<pid>` is invisible, so a LIVE process yields `ENOENT`; and a task
/// that exits mid-read yields `ESRCH`, which has no `ErrorKind`.
fn resolve_unreadable(kernel: &dyn Kernel, pid: RawPid, why: impl std::fmt::Display) -> Resolved<Vec<u8>> {
    match classify_unreadable(signal_probe(kernel, pid)) {
        Resolved::Gone => Resolved::Gone,
        _ => {
            // `debug`, not `warn`: this is a per-pid probe the tree-walk calls once per
            // process per sweep. The decision made from it warns.
            log::debug!("/proc/{pid}/stat not read ({why}) but the pid is not provably gone");
            Resolved::Unknown
        }
    }
}

/// Whether `kill(pid, 0)` answers `ESRCH`. It resolves `pid` in THIS process's pid namespace,
/// which is the one a caller's pid lives in, so unlike a `/proc` read this answer does not
/// depend on which namespace `/proc` describes.
pub(super) fn signal_says_no_such_process(kernel: &dyn Kernel, pid: RawPid) -> bool {
    signal_probe(kernel, pid) == SignalProbe::NoSuchProcess
}

/// What makes a Linux process: `pidfs`, the inode of its pidfd, or, below Linux 6.9 where pidfds
/// have no inode of their own, the `starttime` ticks `/proc` shows. Two ids of different kinds never
/// compare equal.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum Key {
    Pidfs(u64),
    Ticks(u64),
}

impl Key {
    /// The value, whichever the kind.
    pub(super) fn raw(self) -> u64 {
        match self {
            Key::Pidfs(v) | Key::Ticks(v) => v,
        }
    }

    /// A key restored from a record's `token` of the kind the record names. The record was
    /// validated against this host's kind first, so the kind is one of the two.
    pub(super) fn restored(token: u64, kind: Option<&TokenKind>) -> Key {
        debug_assert!(
            matches!(kind, Some(TokenKind::Pidfs | TokenKind::Ticks)),
            "a validated record names a kind of token, not {kind:?}"
        );
        match kind {
            Some(TokenKind::Pidfs) => Key::Pidfs(token),
            _ => Key::Ticks(token),
        }
    }

    /// The kind a record names for this key.
    pub(super) fn wire_kind(self) -> Option<TokenKind> {
        Some(match self {
            Key::Pidfs(_) => TokenKind::Pidfs,
            Key::Ticks(_) => TokenKind::Ticks,
        })
    }

    /// The `Ticks` key of `v`: this backend's default kind, for tests that run on every platform.
    #[cfg(test)]
    pub(crate) fn for_test(v: u64) -> Key {
        Key::Ticks(v)
    }

    /// The ticks of a `Ticks` key, for a test that states them in a `stat` it fakes.
    #[cfg(test)]
    pub(crate) fn ticks(self) -> u64 {
        match self {
            Key::Ticks(v) => v,
            Key::Pidfs(_) => panic!("{self:?} has no ticks"),
        }
    }

    /// The same kind with another value.
    #[cfg(test)]
    pub(super) fn other_value(self) -> Key {
        match self {
            Key::Pidfs(v) => Key::Pidfs(v.wrapping_add(1)),
            Key::Ticks(v) => Key::Ticks(v.wrapping_add(1)),
        }
    }
}

/// What a pidfd opened for a pid says about which kind of [`Key`] it has.
enum Look {
    /// The process's pidfd has this inode on pidfs.
    Pidfs(u64),
    /// This host's pidfds have no inode of their own (before Linux 6.9): `/proc` says.
    Ticks,
    /// No process holds the pid.
    Gone,
    /// The open or the inode read failed in a way that says nothing about the pid.
    Unknown(UnknownCause),
}

/// Opens `pid` and asks its pidfd which kind of key the process has.
///
/// A pid that neither open takes with `EINVAL` is a reaped group leader whose number lives on as a
/// process-group id (6.12), or a kernel with no `PIDFD_THREAD` (before 6.9). Which one is a fact
/// about the host, so the host's own pidfd is asked: on pidfs the pid is no task (`Gone`, and
/// `/proc` is not read, so a process that takes the number meanwhile is not misread as ticks), and
/// elsewhere `/proc` says.
fn look(kernel: &dyn Kernel, pid: RawPid) -> Look {
    let Some(target) = prove::target(pid) else {
        return Look::Gone;
    };
    match prove::open(kernel, target) {
        Ok(opened) => look_at(kernel, opened.fd.as_fd()),
        // `ENOENT` is the retry's: `open` retries only after `INVAL` or `ENOENT`, and `prove` takes
        // it as `Lost` too.
        Err(Errno::SRCH | Errno::NOENT) => Look::Gone,
        Err(Errno::INVAL) => match own_pidfs_ino(kernel) {
            Ok(Some(_)) => Look::Gone,
            Ok(None) => Look::Ticks,
            Err(failed) => Look::Unknown(UnknownCause::of_call(failed)),
        },
        Err(e) => Look::Unknown(UnknownCause::of_call(prove::FailedCall::pidfd_open(e))),
    }
}

/// What the pidfd `fd` says about which kind of key its process has.
fn look_at(kernel: &dyn Kernel, fd: std::os::fd::BorrowedFd<'_>) -> Look {
    match kernel.pidfs_ino(fd) {
        Ok(Some(ino)) => Look::Pidfs(ino),
        Ok(None) => Look::Ticks,
        Err(e) => Look::Unknown(UnknownCause::of_call(prove::FailedCall::pidfs_ino(e))),
    }
}

/// The identity of `pid`: its pidfd's inode on pidfs, else its `starttime` ticks; and what made it
/// `Unknown`, when the read knows.
pub(super) fn of(kernel: &dyn Kernel, pid: RawPid) -> (Resolved<Key>, UnknownCause) {
    key_of(kernel, pid, look(kernel, pid))
}

/// [`of`] through `held`, a pidfd the caller already holds for `pid`, instead of opening the pid
/// again. A pidfd that has no inode of its own (before Linux 6.9) falls back to `/proc` by `pid`.
pub(super) fn of_held(
    kernel: &dyn Kernel,
    pid: RawPid,
    held: std::os::fd::BorrowedFd<'_>,
) -> (Resolved<Key>, UnknownCause) {
    debug_assert!(
        !held_names_another_pid(kernel, pid, held),
        "`held` is a pidfd of another process than {pid}"
    );
    key_of(kernel, pid, look_at(kernel, held))
}

/// Whether the fdinfo of `held` shows a pid other than `pid`. A reaped target, a target the `/proc`
/// view does not number, and an fdinfo or view that cannot be read show none.
fn held_names_another_pid(kernel: &dyn Kernel, pid: RawPid, held: std::os::fd::BorrowedFd<'_>) -> bool {
    let Ok(dir) = proc_view::proc_view_with(kernel).into_dir() else {
        return false;
    };
    matches!(
        proc_view::pidfd_pid_in(kernel, &dir, held),
        Ok(proc_view::PidfdTarget::Pid(shown)) if shown != 0 && shown != pid
    )
}

fn key_of(kernel: &dyn Kernel, pid: RawPid, look: Look) -> (Resolved<Key>, UnknownCause) {
    match look {
        Look::Pidfs(ino) => (Resolved::Found(Key::Pidfs(ino)), UnknownCause::default()),
        Look::Gone => (Resolved::Gone, UnknownCause::default()),
        Look::Unknown(cause) => (Resolved::Unknown, cause),
        Look::Ticks => {
            let (stat, view) = read_stat_explained(kernel, pid);
            (ticks_from(pid, stat).map(Key::Ticks), UnknownCause::of_view(view))
        }
    }
}

/// [`of`], and a `stat` record that is that process's own: for a `Pidfs` key the bracketed one, and
/// for ticks the very record the ticks were read from. `expected`: see
/// [`ProcessId::of_ordered_with`](super::ProcessId::of_ordered_with). A pidfd that shows another
/// kind of key or another inode is `Gone` before the `stat` is read.
pub(super) fn of_stat(
    kernel: &dyn Kernel,
    pid: RawPid,
    expected: Option<Key>,
) -> (Resolved<(Key, Vec<u8>)>, UnknownCause) {
    match look(kernel, pid) {
        Look::Pidfs(ino) if expected.is_some_and(|expected| expected != Key::Pidfs(ino)) => {
            (Resolved::Gone, UnknownCause::default())
        }
        Look::Ticks if matches!(expected, Some(Key::Pidfs(_))) => (Resolved::Gone, UnknownCause::default()),
        Look::Pidfs(ino) => {
            let (stat, cause) = bracketed_stat(kernel, pid, ino);
            (stat.map(|stat| (Key::Pidfs(ino), stat)), cause)
        }
        Look::Gone => (Resolved::Gone, UnknownCause::default()),
        Look::Unknown(cause) => (Resolved::Unknown, cause),
        Look::Ticks => {
            let (stat, view) = read_stat_explained(kernel, pid);
            let bytes = match &stat {
                Resolved::Found(bytes) => bytes.clone(),
                _ => Vec::new(),
            };
            (
                ticks_from(pid, stat).map(|ticks| (Key::Ticks(ticks), bytes)),
                UnknownCause::of_view(view),
            )
        }
    }
}

/// The key and the parent number of `pid`, the latter from the `stat` that [`of_stat`] pairs with
/// the key, with no order key. The ppid is parsed (and an unreadable one logged) only when
/// `ask_parent`, and is then unreadable, not a parent number.
pub(super) fn ordered_read(kernel: &dyn Kernel, pid: RawPid, expected: Option<Key>, ask_parent: bool) -> OrderedRead {
    let (read, cause) = of_stat(kernel, pid, expected);
    let (key, stat) = match read {
        Resolved::Found(found) => found,
        Resolved::Gone => return OrderedRead::missing(Resolved::Gone, cause, ask_parent),
        Resolved::Unknown => return OrderedRead::missing(Resolved::Unknown, cause, ask_parent),
    };
    let parent = if !ask_parent {
        ParentRead::NotAsked
    } else if let Some(ppid) = parse_ppid(&stat) {
        ParentRead::Is(ppid)
    } else {
        log::debug!("identity: the stat of pid {pid} has no readable ppid");
        ParentRead::Unreadable
    };
    // No order key: nothing on Linux orders by the start time, so a `stat` without one still gives
    // its ppid. A `ticks` key is known only from the record just read.
    OrderedRead {
        read: Resolved::Found((key, None)),
        parent,
        cause,
    }
    .against(expected)
}

/// Whether the process `key` names still holds `pid`.
pub(super) fn exists(kernel: &dyn Kernel, pid: RawPid, key: Key) -> (Existence, UnknownCause) {
    match key {
        Key::Ticks(ticks) => {
            let (stat, view) = read_stat_explained(kernel, pid);
            (
                existence_of_ticks(ticks_from(pid, stat), ticks),
                UnknownCause::of_view(view),
            )
        }
        Key::Pidfs(ino) => match prove::prove(kernel, pid, ino) {
            Proof::Holds { .. } => (Existence::Present, UnknownCause::default()),
            Proof::Lost => (Existence::Gone, UnknownCause::default()),
            Proof::Unknown(call) => (Existence::Unknown, UnknownCause::of_call(call)),
        },
    }
}

/// [`exists`], reading through the `/proc` at `proc_dir`: a `Pidfs` id needs no `/proc`.
pub(super) fn exists_in(kernel: &dyn Kernel, proc_dir: &ProcDir, pid: RawPid, key: Key) -> Existence {
    match key {
        Key::Ticks(ticks) => existence_of_ticks(ticks_from(pid, stat_by_pid(kernel, proc_dir, pid)), ticks),
        Key::Pidfs(_) => exists(kernel, pid, key).0,
    }
}

/// What `read`, the ticks now at the pid, says about the process that had `ticks`.
fn existence_of_ticks(read: Resolved<u64>, ticks: u64) -> Existence {
    match read {
        Resolved::Found(now) if now == ticks => Existence::Present,
        Resolved::Found(_) | Resolved::Gone => Existence::Gone,
        Resolved::Unknown => Existence::Unknown,
    }
}

/// `pid`'s `starttime` ticks from `stat`: RAW jiffies, NOT converted to wall-clock.
fn ticks_from(pid: RawPid, stat: Resolved<Vec<u8>>) -> Resolved<u64> {
    match stat {
        Resolved::Found(stat) => match parse_starttime_jiffies(&stat) {
            Some(j) => Resolved::Found(j),
            // Reachable when a task exits mid-read and the kernel hands back a truncated
            // buffer, so this must not be an assertion.
            None => {
                log::debug!("/proc/{pid}/stat has no parseable starttime");
                Resolved::Unknown
            }
        },
        Resolved::Gone => Resolved::Gone,
        Resolved::Unknown => Resolved::Unknown,
    }
}

/// `pid`'s `stat` while the process with `pidfs` inode `ino` holds the pid: read, then proved again.
/// A process that holds the pid when the proof is made held it from when the id was made, so the
/// `stat` read between is its own; the other order would let a reuser's `stat` through. `Gone` when
/// the proof finds another process.
fn bracketed_stat(kernel: &dyn Kernel, pid: RawPid, ino: u64) -> (Resolved<Vec<u8>>, UnknownCause) {
    let (stat, view) = read_stat_explained(kernel, pid);
    let stat = match stat {
        Resolved::Found(stat) => stat,
        Resolved::Gone => return (Resolved::Gone, UnknownCause::of_view(view)),
        Resolved::Unknown => return (Resolved::Unknown, UnknownCause::of_view(view)),
    };
    match prove::prove(kernel, pid, ino) {
        Proof::Holds { .. } => (Resolved::Found(stat), UnknownCause::default()),
        Proof::Lost => (Resolved::Gone, UnknownCause::default()),
        Proof::Unknown(call) => (Resolved::Unknown, UnknownCause::of_call(call)),
    }
}

/// When the process `key` names started, in ticks: a `Ticks` key is that, and a `Pidfs` key's
/// process shows it in its bracketed `stat`.
fn start_ticks(kernel: &dyn Kernel, pid: RawPid, key: Key) -> (Resolved<u64>, UnknownCause) {
    match key {
        Key::Ticks(ticks) => (Resolved::Found(ticks), UnknownCause::default()),
        Key::Pidfs(ino) => {
            let (stat, cause) = bracketed_stat(kernel, pid, ino);
            (ticks_from(pid, stat), cause)
        }
    }
}

pub(super) fn is_running(kernel: &dyn Kernel, pid: RawPid, key: Key) -> Liveness {
    match key {
        Key::Ticks(ticks) => liveness_of(read_stat(kernel, pid), ticks),
        // The pidfd polls readable once the process has exited, a zombie included, and not before:
        // `/proc` shows `Z` for a live process whose leader thread exited, its pidfd does not.
        Key::Pidfs(ino) => match prove::prove(kernel, pid, ino) {
            Proof::Holds { fd, .. } => match crate::os_process::poll::is_readable(fd.as_fd()) {
                Ok(true) => Liveness::Dead,
                Ok(false) => Liveness::Alive,
                Err(e) => {
                    log::debug!("the pidfd of pid {pid} could not be polled: {e}");
                    Liveness::Unknown
                }
            },
            Proof::Lost => Liveness::Dead,
            Proof::Unknown(call) => {
                log::debug!("the pidfd of pid {pid} could not be opened or proved: {call:?}");
                Liveness::Unknown
            }
        },
    }
}

/// [`is_running`], read through the `/proc` at `proc_dir`: a `Pidfs` id needs no `/proc`.
pub(super) fn is_running_in(kernel: &dyn Kernel, proc_dir: &ProcDir, pid: RawPid, key: Key) -> Liveness {
    match key {
        Key::Ticks(ticks) => liveness_of(stat_by_pid(kernel, proc_dir, pid), ticks),
        Key::Pidfs(_) => is_running(kernel, pid, key),
    }
}

fn liveness_of(stat: Resolved<Vec<u8>>, ticks: u64) -> Liveness {
    match stat {
        Resolved::Found(stat) => super::stat_parse::running_from_stat(&stat, ticks),
        Resolved::Gone => Liveness::Dead,
        Resolved::Unknown => Liveness::Unknown,
    }
}

/// Our own key.
///
/// `ProcessId` is the pair `(std::process::id(), key)`. On pidfs it is the inode of the process's
/// own pidfd, which names the process whatever `/proc` the caller sees. Where pidfds have none, it
/// is read from `/proc/self/stat`: under an outer namespace's `/proc` (`unshare --pid --fork`
/// without `--mount-proc`: `getpid()` is 1, `/proc/1` is another process) `/proc/<getpid()>` names
/// someone else, but the kernel resolves `self` for the reader. A plain read, so it needs neither
/// `openat2` nor a `/proc` view; the by-pid re-reads (`exists()`/`is_alive()`) then answer
/// `Unknown`, never `Gone`/`Dead` for the running caller.
///
/// `hidepid` never hides a task from itself, so a failure here is not a foreign-pid outcome:
/// [`ProcessId::current`](super::ProcessId::current) panics on it. That includes a kernel or filter
/// that refuses `pidfd_open` to the process itself, which is the error [`Process::open`] gives (an
/// `Unsupported` for a refusal).
pub(super) fn current_key(kernel: &dyn Kernel) -> Result<Key, Error> {
    match own_pidfs_ino(kernel) {
        Ok(Some(ino)) => Ok(Key::Pidfs(ino)),
        Ok(None) => {
            let stat = std::fs::read("/proc/self/stat").map_err(Error::Io)?;
            match ticks_from(std::process::id(), Resolved::Found(stat)) {
                Resolved::Found(ticks) => Ok(Key::Ticks(ticks)),
                _ => Err(Error::Io(std::io::Error::other(
                    "/proc/self/stat has no parseable starttime",
                ))),
            }
        }
        Err(failed) => Err(crate::os_process::linux::open::open_error(failed)),
    }
}

/// The error for a by-pid identity read of `subject` that `why` (an unusable view) made `Unknown`:
/// [`Error::Unsupported`] naming `openat2` when that is missing, else [`Error::Unassessable`]
/// carrying the view's reason.
pub(crate) fn view_error(subject: &str, why: proc_view::ViewUnreadable) -> Error {
    why.unsupported(format!("identifying {subject}"))
        .unwrap_or_else(|| unassessable_view(subject, &why.reason, why.source))
}

/// The error for a by-pid identity read of `subject` that a failed pidfd call made `Unknown`:
/// [`Error::Unsupported`] when the call was refused, else [`Error::Unassessable`] naming the call
/// and carrying the errno.
pub(crate) fn call_error(subject: &str, call: prove::FailedCall) -> Error {
    match call.refusal() {
        Some(name) => crate::os_process::linux::open::pidfd_open_unsupported(&format!("identifying {subject}"), name),
        None => Error::Unassessable {
            detail: format!(
                "{subject} identity could not be read: {}: {}",
                call.name(),
                call.errno()
            ),
            source: Some(std::io::Error::from(call.errno())),
        },
    }
}

fn unassessable_view(subject: &str, reason: &str, source: Option<std::io::Error>) -> Error {
    let mut detail = format!("{subject} identity could not be read: {reason}");
    if let Some(source) = &source {
        detail.push_str(&format!(": {source}"));
    }
    Error::Unassessable { detail, source }
}

pub(super) fn created_at(kernel: &dyn Kernel, pid: RawPid, key: Key) -> Option<SystemTime> {
    let jiffies = start_ticks(kernel, pid, key).0.found()?;
    let hz = clock_ticks_per_sec()?;
    let btime = boot_time_secs()?;
    let secs = btime + jiffies / hz;
    let nanos = ((jiffies % hz) * 1_000_000_000 / hz) as u32;
    Some(SystemTime::UNIX_EPOCH + Duration::new(secs, nanos))
}

fn clock_ticks_per_sec() -> Option<u64> {
    // SAFETY: sysconf with a constant name is always safe.
    let hz = unsafe { libc::sysconf(libc::_SC_CLK_TCK) };
    (hz > 0).then_some(hz as u64)
}

fn boot_time_secs() -> Option<u64> {
    std::fs::read_to_string("/proc/stat")
        .ok()?
        .lines()
        .find_map(|l| l.strip_prefix("btime "))
        .and_then(|v| v.trim().parse::<u64>().ok())
}

// Persisted-identity session scope ====================================================

use super::persist::{Scope, ScopeReadError};

const BOOT_ID_PATH: &str = "/proc/sys/kernel/random/boot_id";
const PID_NS_PATH: &str = "/proc/self/ns/pid";

/// This host's boot session, as the things a saved token is relative to.
///
/// `boot_id` scopes the token, which restarts at every boot (the jiffy counter, the pidfs inode
/// counter); the `/proc/self/ns/pid` inode scopes the PID, because a container shares the host's
/// `boot_id` (it is not namespaced) while numbering its processes independently; and the kind of
/// token says whether the host's tokens are `pidfs` inodes or `starttime` ticks, which are not
/// comparable. Any one alone would let a saved token be compared against an unrelated process.
///
/// The first two reads are of the caller's own `/proc` entries, which no `hidepid` mount hides
/// from the task itself; a failure there means `/proc` is not mounted at all. The kind is read from
/// the caller's own pidfd, so a host that refuses `pidfd_open` to it has no scope either.
pub(super) fn session_scope() -> Result<Scope, ScopeReadError> {
    session_scope_at(
        &kernel::HostKernel,
        std::path::Path::new(BOOT_ID_PATH),
        std::path::Path::new(PID_NS_PATH),
    )
}

/// [`session_scope`] through `kernel`, with the two `/proc` paths as parameters, so a test can
/// point them at paths that really do not exist and exercise the failure without mocking a syscall.
pub(super) fn session_scope_at(
    kernel: &dyn Kernel,
    boot_id_path: &std::path::Path,
    pid_ns_path: &std::path::Path,
) -> Result<Scope, ScopeReadError> {
    use std::os::unix::fs::MetadataExt;

    let boot_id = std::fs::read_to_string(boot_id_path).map_err(|source| ScopeReadError {
        path: boot_id_path.display().to_string(),
        source,
    })?;
    // Trimmed: the kernel appends a newline, and an untrimmed value would never match a
    // record written by anything else that reads this file.
    let boot_id = boot_id.trim();
    // A BLANK value is refused, not stored. A container runtime that masks this file by
    // bind-mounting /dev/null over it makes the read SUCCEED and return "", which would be
    // stored as `Some("")` and then compare equal to the next boot's `Some("")` — silently
    // turning the boot-session check into a no-op, which is the exact aliasing this record
    // exists to prevent. Failing here surfaces it as `ScopeUnreadable` instead.
    if boot_id.is_empty() {
        return Err(ScopeReadError {
            path: boot_id_path.display().to_string(),
            source: std::io::Error::new(std::io::ErrorKind::InvalidData, "boot_id is empty"),
        });
    }
    let pid_ns = std::fs::metadata(pid_ns_path)
        .map_err(|source| ScopeReadError {
            path: pid_ns_path.display().to_string(),
            source,
        })?
        .ino();
    let token_kind = own_token_kind(kernel).map_err(|failed| ScopeReadError {
        path: format!("{} of this process", failed.name()),
        source: std::io::Error::from(failed.errno()),
    })?;
    Ok(Scope {
        boot_id: Some(boot_id.to_owned()),
        pid_ns: Some(pid_ns),
        token_kind: Some(token_kind),
    })
}

/// The inode of this process's own pidfd: `Some` on pidfs, `None` where pidfds have none.
fn own_pidfs_ino(kernel: &dyn Kernel) -> Result<Option<u64>, prove::FailedCall> {
    let pidfd = kernel
        .pidfd_open(rustix::process::getpid(), PidfdFlags::empty())
        .map_err(prove::FailedCall::pidfd_open)?;
    kernel.pidfs_ino(pidfd.as_fd()).map_err(prove::FailedCall::pidfs_ino)
}

/// The kind of [`Key`] this host's processes have, as a record names it.
fn own_token_kind(kernel: &dyn Kernel) -> Result<TokenKind, prove::FailedCall> {
    Ok(match own_pidfs_ino(kernel)? {
        Some(_) => TokenKind::Pidfs,
        None => TokenKind::Ticks,
    })
}