subc-os 0.1.55

Operating-system primitives for the subc daemon that need unsafe code, behind a safe API: per-process identity, signalling, memory and CPU usage, and the launch-nonce descriptor handoff.
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
//! Operating-system primitives the subc daemon needs and cannot reach without
//! unsafe code, each behind a small safe API.
//!
//! The daemon crates forbid unsafe code. This crate is the one deliberate
//! exception (like `subc-uptime` and `subc-cgroup`): each `unsafe` block here is
//! a foreign call, or a read of a buffer such a call returned, with its
//! preconditions stated beside it, and nothing unsafe is exported.
//!
//! Its central question: is the process now holding pid N the same
//! process the daemon spawned earlier? A pid alone cannot say, because the
//! kernel reuses pids once a process has been reaped. [`Process`] reads the two
//! facts that tell processes apart, the kernel's start time for the pid and the
//! file identity (device and inode) of the executable image it runs, and sends
//! signals to it.
//!
//! Sources, per platform:
//!
//! - Linux: the start time is field 22 of `/proc/<pid>/stat` (clock ticks since
//!   boot), and the executable is `stat` through `/proc/<pid>/exe`, which
//!   resolves to the running image even if its file has since been replaced or
//!   deleted. A pidfd is opened before either is read and signals go through it
//!   (`pidfd_send_signal`), so the process that was checked is the process that
//!   is signalled. No unsafe code is needed: rustix wraps both calls.
//! - macOS: the start time is `kp_proc.p_starttime` from `sysctl`
//!   `KERN_PROC_PID` (microseconds since the epoch), and the executable is the
//!   path `proc_pidpath` reports, then `stat` on that path. These two calls are
//!   unsafe. macOS has no pidfd, so a signal is a plain
//!   `kill` sent right after the checks; see [`Process::signal`].
//! - Windows: creation time and forced stops use one retained process handle.
//!   `ExecutableCapture` pins the resolved executable until a suspended spawn
//!   binds its volume and 128-bit file ID to that handle and creation time.
//! - Anywhere else: [`Process::open`] reports [`std::io::ErrorKind::Unsupported`].
//!
//! For persisted PID owners, [`process_identity`] reads versioned kernel start
//! identities and distinguishes alive, dead and unknown without spawning a
//! process. Its foreign calls are signal-zero `kill` on Unix and `proc_pidinfo`
//! on macOS. Only dead owners may be reclaimed; unknown owners stay protected.
//!
//! It also reads how much memory and CPU time one process is using, for
//! reporting only; see [`resource_usage`]. On Linux that is procfs again; on
//! macOS it is `proc_pid_rusage`, plus `mach_timebase_info` to convert its CPU
//! times to nanoseconds, the other two unsafe calls in the crate.
//!
//! For the machine as a whole, [`host`] reads total and available memory, the
//! load averages and the space on a filesystem, again for reporting only.
//!
//! It also resolves per-user locations from account records rather than the
//! environment: the password-database home and effective uid on Unix, Darwin's
//! per-user temporary directory on macOS, and the recorded profile folder on
//! Windows (`profile_dir`).
//!
//! For the peer of an accepted local-endpoint connection (a Unix-domain
//! socket, or a named-pipe client on Windows), [`local_peer`] reads the
//! kernel's record of the connecting process: its user, pid and executable,
//! and on macOS its code-signing identifier.
//!
//! And it carries the launch nonce from the daemon to each module it spawns
//! over an inherited Unix pipe or a PID-authenticated Windows named pipe:
//! [`launch_nonce`] is the one cached reader every module uses. Windows keeps
//! an environment copy for old Windows readers until live source reports show
//! every module consuming the named pipe.

#![deny(unsafe_code)]

#[cfg(target_os = "macos")]
mod darwin_user_temp;
#[cfg(target_os = "macos")]
pub use darwin_user_temp::darwin_user_temp_dir;

#[cfg(windows)]
mod profile;
#[cfg(windows)]
pub use profile::profile_dir;

#[cfg(unix)]
mod user_home;
#[cfg(unix)]
pub use user_home::{effective_uid, real_user_home_dir};

#[cfg(unix)]
pub mod admission_gate;
pub mod boot_id;
#[cfg(unix)]
pub mod child_exit;
pub mod child_spawn;
#[cfg(target_os = "macos")]
pub mod code_signature;
pub mod config_keychain;
mod durable_replace;
#[cfg(any(unix, windows))]
mod retained_file;
#[cfg(any(target_os = "linux", target_os = "macos", windows))]
mod retained_replace;
#[cfg(windows)]
pub use durable_replace::durable_move_to_new_name;
#[cfg(feature = "test-support")]
pub use durable_replace::durable_replace_with_rename_hook;
pub use durable_replace::{durable_replace, DurableReplaceError};
#[cfg(any(unix, windows))]
pub use retained_file::{
    open_directory_nofollow_all, open_regular_nofollow, open_regular_nofollow_all,
    retained_file_identity, OpenRegularError, RetainedFileIdentity,
};
#[cfg(windows)]
pub use retained_file::{retained_file_index, WindowsFileIndexIdentity};
#[cfg(any(target_os = "linux", target_os = "macos", windows))]
pub use retained_replace::{
    create_directory_nofollow_at, move_file_aside_nofollow_at, move_file_to_noreplace_at,
    open_directory_nofollow_at, open_or_create_private_directory_at,
    open_single_link_file_nofollow_at, probe_retained_directory_write,
    probe_retained_regular_file_at, retained_directory_path, retained_regular_file_at,
    OpenRetainedError, RetainedMovedFile, RetainedTempFile,
};
#[cfg(all(unix, feature = "test-support"))]
pub mod fork_exec_test;
pub mod host;
pub mod launch_nonce;
pub mod local_peer;
#[cfg(windows)]
mod loopback_connect;
#[cfg(windows)]
pub use loopback_connect::connect_loopback;
pub mod named_pipe;
pub mod privacy_identity;
mod private_directory;
#[cfg(unix)]
pub mod process_group;
pub mod process_identity;
pub use private_directory::create_private_directory_exclusive;
pub mod registration_channel;
pub mod script_pipe;
pub mod socket_peer;
#[cfg(windows)]
pub mod user_path;
#[cfg_attr(not(windows), path = "windows_acl_noop.rs")]
pub mod windows_acl;
#[cfg(unix)]
pub use launch_nonce::LaunchNonceHandoff;
pub use launch_nonce::{
    launch_nonce, LaunchNonce, LaunchNonceError, LaunchNonceSource, LAUNCH_NONCE_ENV,
    LAUNCH_NONCE_FD, LAUNCH_NONCE_FD_ENV, LAUNCH_NONCE_PIPE_ENV, LAUNCH_NONCE_PIPE_FALLBACK_ENV,
};

#[cfg(windows)]
pub use launch_nonce::{LaunchNoncePipeDelivery, LaunchNoncePipeHandoff};

pub use registration_channel::{
    registration_refusal, supervised, take_registration_channel, RegistrationChannel,
    RegistrationChannelError, RegistrationRefusal,
};

#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;

#[cfg(target_os = "macos")]
pub use macos::owned_child_start_time;
#[cfg(windows)]
mod windows;
#[cfg(all(test, windows))]
mod windows_tests;
#[cfg(windows)]
pub use windows::{
    ExecutableCapture, ImageAgreement, ImageUnavailable, SpawnedImage, WindowsFileIdentity,
};

#[cfg(target_os = "linux")]
use linux as platform;
#[cfg(target_os = "macos")]
use macos as platform;

use std::{io, path::Path};

/// True where [`Process`] can identify and stop a process by pid.
pub const PROCESS_IDENTITY_SUPPORTED: bool =
    cfg!(any(target_os = "linux", target_os = "macos", windows));

/// Device and inode of a file: which file, independent of the name used to
/// reach it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FileIdentity {
    pub device: u64,
    pub inode: u64,
}

/// The device and inode of the file at `path`, following symlinks. `None` if it
/// cannot be read or the platform has no inode numbers.
pub fn file_identity(path: &Path) -> Option<FileIdentity> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;

        std::fs::metadata(path).ok().map(|metadata| FileIdentity {
            device: metadata.dev(),
            inode: metadata.ino(),
        })
    }
    #[cfg(not(unix))]
    {
        let _ = path;
        None
    }
}

/// What a live process looks like right now.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Observation {
    /// The kernel's start time for the process. Opaque: compare it only with a
    /// value read on the same host by this crate. Linux counts clock ticks since
    /// boot; macOS counts microseconds since the epoch; Windows counts 100 ns
    /// intervals since the Windows epoch.
    pub start_time: u64,
    /// The file the process is executing, or `None` if it could not be read
    /// (for example, a process owned by another user).
    /// Windows uses the full file ID in `SpawnedImage` instead of a Unix inode;
    /// this field is always `None` there.
    pub executable: Option<FileIdentity>,
}

/// Whether the running executable's identity could be read. Permission denial
/// is distinct on Linux because a non-dumpable process hides its executable
/// while leaving its start time readable.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExecutableAccess {
    Readable,
    PermissionDenied,
    Unavailable,
}

/// A signal [`Process::signal`] can send.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Signal {
    /// SIGTERM: a request to exit, which the process may handle or ignore.
    Terminate,
    /// SIGKILL: ends the process; it cannot be handled or ignored.
    Kill,
}

/// The kernel start time of the process holding `pid`, or `None` if there is
/// none, it has already exited (a zombie waiting to be reaped counts as exited),
/// or the platform has no source.
pub fn start_time(pid: u32) -> Option<u64> {
    #[cfg(any(target_os = "linux", target_os = "macos"))]
    {
        platform::start_time(pid)
    }
    #[cfg(windows)]
    {
        Process::open(pid)
            .ok()??
            .observe()
            .map(|observation| observation.start_time)
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
    {
        let _ = pid;
        None
    }
}

/// Snapshot probes distinguish an exited process from a failed identity read.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum IdentityRead {
    Alive(u64),
    #[cfg_attr(windows, allow(dead_code))]
    // Windows retains its existing unknown-on-observe-failure policy.
    Gone,
    Unknown,
}

#[cfg(all(test, any(target_os = "linux", target_os = "macos")))]
mod identity_read_tests {
    use super::*;

    #[test]
    fn identity_probe_distinguishes_alive_gone_and_invalid() {
        let mut child = std::process::Command::new("/bin/sh")
            .args(["-c", "exit 0"])
            .spawn()
            .unwrap();
        let pid = child.id();
        let until = std::time::Instant::now() + std::time::Duration::from_secs(5);
        while read_identity(pid) != IdentityRead::Gone {
            assert!(
                std::time::Instant::now() < until,
                "an exited owned child must become gone before reaping"
            );
            std::thread::yield_now();
        }
        child.wait().unwrap();
        assert_eq!(
            read_identity(pid),
            IdentityRead::Gone,
            "a missing owned PID is gone"
        );
        assert_eq!(read_identity(0), IdentityRead::Unknown);
        assert!(matches!(
            read_identity(std::process::id()),
            IdentityRead::Alive(_)
        ));
    }
}

#[cfg(not(windows))]
fn read_identity(pid: u32) -> IdentityRead {
    #[cfg(any(target_os = "linux", target_os = "macos"))]
    {
        platform::read_identity(pid)
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos")))]
    {
        start_time(pid).map_or(IdentityRead::Unknown, IdentityRead::Alive)
    }
}

mod process_tree;
pub use process_tree::{BirthBaseline, ProcessGroup, ProcessMember, ProcessTable, TreeUsage};

/// Kernel process creation time, in milliseconds since the Unix epoch.
///
/// Linux combines procfs boot time (whole seconds) with start ticks (usually
/// 10 ms precision). macOS truncates microseconds and Windows truncates 100 ns
/// FILETIME ticks to milliseconds. Use [`process_start_time_precision_ms`] when
/// comparing a declaration made by another process; this is not a unique ID
/// for two starts within the kernel's timestamp resolution.
pub fn process_start_time_ms(pid: u32) -> Option<u64> {
    start_time_ms_from_native(start_time(pid)?)
}

fn start_time_ms_from_native(raw: u64) -> Option<u64> {
    #[cfg(target_os = "linux")]
    {
        linux_start_time_ms(raw, || std::fs::read_to_string("/proc/stat").ok())
    }
    #[cfg(target_os = "macos")]
    {
        Some(raw / 1_000)
    }
    #[cfg(windows)]
    {
        (raw / 10_000).checked_sub(11_644_473_600_000)
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
    {
        let _ = raw;
        None
    }
}

#[cfg(target_os = "linux")]
fn linux_start_time_ms(raw: u64, read_boot: impl FnOnce() -> Option<String>) -> Option<u64> {
    // Realtime clock steps change the boot-time value in the `btime` line.
    // The daemon and a newly started client
    // must both convert against the current value, not a per-process cache.
    let boot = read_boot()?
        .lines()
        .find_map(|line| line.strip_prefix("btime ")?.parse::<u64>().ok())?;
    boot.checked_mul(1_000)?
        .checked_add(raw.checked_mul(1_000)? / rustix::param::clock_ticks_per_second().max(1))
}

#[cfg(all(test, target_os = "linux"))]
#[test]
fn linux_start_identity_uses_fresh_boot_time_after_clock_step() {
    let before = linux_start_time_ms(100, || Some("btime 1000\n".into())).unwrap();
    let after = linux_start_time_ms(100, || Some("btime 1002\n".into())).unwrap();
    assert_eq!(after - before, 2_000);
}

/// Resolution used by [`process_start_time_ms`], rounded up to milliseconds.
pub fn process_start_time_precision_ms() -> u64 {
    #[cfg(target_os = "linux")]
    {
        1_000_u64.div_ceil(rustix::param::clock_ticks_per_second().max(1))
    }
    #[cfg(not(target_os = "linux"))]
    {
        1
    }
}

/// The parent pid reported by the kernel, or `None` when it cannot be read.
/// On Windows a reused parent pid (created after the child) is rejected.
pub fn parent_pid(pid: u32) -> Option<u32> {
    #[cfg(any(target_os = "linux", target_os = "macos"))]
    {
        platform::parent_pid(pid)
    }
    #[cfg(windows)]
    {
        windows::parent_pid(pid)
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
    {
        let _ = pid;
        None
    }
}

/// True where [`resource_usage`] can read a live process. Elsewhere it always
/// answers `None`, and a caller can use this to say "not supported here"
/// rather than "could not read".
pub const RESOURCE_USAGE_SUPPORTED: bool =
    cfg!(any(target_os = "linux", target_os = "macos", windows));

/// What [`ResourceUsage::memory_bytes`] measures. The platforms offer
/// different figures, and they are not interchangeable.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryKind {
    /// macOS `phys_footprint`: the memory the kernel charges to the process
    /// (dirty and compressed pages, among others), which is also what jetsam
    /// acts on. Pages an allocator has released with `MADV_FREE` do not count.
    PhysFootprint,
    /// Linux `VmRSS`: pages of the process resident in RAM, including shared
    /// file-backed pages. Swapped-out pages are not included; see
    /// [`ResourceUsage::swap_bytes`].
    ResidentSet,
    /// Windows `WorkingSetSize`: pageable memory currently resident in RAM,
    /// including shared pages. This is not Unix RSS or private committed memory.
    WindowsWorkingSet,
}

/// One reading of a process's memory and cumulative CPU time.
///
/// It covers the process named by the pid alone: its threads are included,
/// processes it has started are not.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ResourceUsage {
    /// Memory in bytes, measured as [`Self::memory_kind`] says.
    pub memory_bytes: u64,
    pub memory_kind: MemoryKind,
    /// Bytes swapped out (Linux `VmSwap`). `None` where the platform does not
    /// report it for a single process, which is not the same as zero.
    pub swap_bytes: Option<u64>,
    /// CPU time spent in user mode since the process started.
    pub cpu_user: std::time::Duration,
    /// CPU time spent in the kernel on the process's behalf since it started.
    pub cpu_system: std::time::Duration,
}

/// Memory and cumulative CPU time of the process holding `pid`, read now.
///
/// `None` when there is no such process, it has exited (a zombie awaiting its
/// reap counts as exited), it cannot be read (for example, another user's
/// process on macOS), or the platform has no source
/// (see [`RESOURCE_USAGE_SUPPORTED`]). Never a reading of zeros in place of
/// one of those.
///
/// Like any pid-based read, this describes whatever process holds `pid` now;
/// a caller that needs it to be a particular process should confirm that
/// process's [`start_time`] around the call.
pub fn resource_usage(pid: u32) -> Option<ResourceUsage> {
    #[cfg(any(target_os = "linux", target_os = "macos"))]
    {
        platform::resource_usage(pid)
    }
    #[cfg(windows)]
    {
        Process::open(pid).ok()??.resource_usage()
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
    {
        let _ = pid;
        None
    }
}

/// A handle on the process holding one pid at the moment it was opened.
#[derive(Debug)]
pub struct Process {
    pid: u32,
    #[cfg(target_os = "linux")]
    pidfd: Option<std::os::fd::OwnedFd>,
    #[cfg(windows)]
    handle: std::os::windows::io::OwnedHandle,
}

impl Process {
    /// Open a handle on the process now holding `pid`.
    ///
    /// `Ok(None)` means no process holds that pid (on macOS, also a zombie
    /// awaiting its reap; on Linux a zombie opens, and [`Self::observe`] then
    /// reports it as exited). On Linux this opens a pidfd,
    /// which from then on refers to this exact process even if it exits and the
    /// pid is reused; when the kernel cannot open one (older than 5.3, or a
    /// seccomp policy refusing the call) the handle falls back to the pid, as
    /// on macOS.
    pub fn open(pid: u32) -> io::Result<Option<Self>> {
        #[cfg(target_os = "linux")]
        {
            linux::open(pid).map(|opened| opened.map(|pidfd| Self { pid, pidfd }))
        }
        #[cfg(target_os = "macos")]
        {
            Ok(platform::exists(pid).then_some(Self { pid }))
        }
        #[cfg(windows)]
        {
            windows::open(pid).map(|opened| opened.map(|handle| Self { pid, handle }))
        }
        #[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
        {
            let _ = pid;
            Err(io::Error::new(
                io::ErrorKind::Unsupported,
                "process identity is not available on this platform",
            ))
        }
    }

    pub fn pid(&self) -> u32 {
        self.pid
    }

    /// True when signals go through a pidfd, so they cannot reach a different
    /// process that has since reused this pid.
    pub fn signals_through_pidfd(&self) -> bool {
        #[cfg(target_os = "linux")]
        {
            self.pidfd.is_some()
        }
        #[cfg(not(target_os = "linux"))]
        {
            false
        }
    }

    /// The process's start time and executable, or `None` when it has exited
    /// (including as a zombie not yet reaped) or cannot be observed. On Linux,
    /// [`Self::is_alive_but_unobservable`] distinguishes a hidden procfs entry.
    pub fn observe(&self) -> Option<Observation> {
        self.observe_with_executable_access()
            .map(|(observed, _)| observed)
    }

    /// Whether Linux hides the entire procfs entry while the process still
    /// exists. This proves existence, not identity, and never authorizes a signal.
    /// Other platforms return false without changing their observation behavior.
    pub fn is_alive_but_unobservable(&self) -> bool {
        #[cfg(target_os = "linux")]
        {
            linux::alive_but_unobservable(self.pid, self.pidfd.as_ref())
        }
        #[cfg(not(target_os = "linux"))]
        {
            false
        }
    }

    /// Observe the process and distinguish Linux executable permission denial
    /// from other failures, without changing the legacy observation's fields.
    /// Other platforms retain their existing readable/unavailable distinction.
    pub fn observe_with_executable_access(&self) -> Option<(Observation, ExecutableAccess)> {
        #[cfg(any(target_os = "linux", target_os = "macos"))]
        {
            #[cfg(target_os = "linux")]
            if !linux::pidfd_alive(self.pidfd.as_ref()) {
                return None;
            }
            let start_time = platform::start_time(self.pid)?;
            #[cfg(target_os = "linux")]
            let (executable, access) = match linux::executable_identity(self.pid) {
                Ok(identity) => (Some(identity), ExecutableAccess::Readable),
                Err(error) if error.kind() == io::ErrorKind::PermissionDenied => {
                    (None, ExecutableAccess::PermissionDenied)
                }
                Err(_) => (None, ExecutableAccess::Unavailable),
            };
            #[cfg(target_os = "macos")]
            let executable = macos::executable_identity(self.pid);
            #[cfg(target_os = "macos")]
            let access = if executable.is_some() {
                ExecutableAccess::Readable
            } else {
                ExecutableAccess::Unavailable
            };
            Some((
                Observation {
                    start_time,
                    executable,
                },
                access,
            ))
        }
        #[cfg(windows)]
        {
            windows::observe(self).map(|observed| (observed, ExecutableAccess::Unavailable))
        }
        #[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
        {
            None
        }
    }

    /// Wait on the retained Windows handle. `Ok(false)` means the bound elapsed,
    /// not that a reused PID was observed.
    #[cfg(windows)]
    pub fn wait_for_exit(&self, timeout: std::time::Duration) -> io::Result<bool> {
        windows::wait(self, timeout)
    }

    /// Force the confirmed Windows process to stop, then wait on the same handle.
    /// This is not a graceful termination signal. A different creation time
    /// refuses before any action; `Ok(false)` means the wait bound elapsed.
    #[cfg(windows)]
    pub fn force_stop(
        &self,
        expected_start_time: u64,
        timeout: std::time::Duration,
    ) -> io::Result<bool> {
        windows::force_stop(self, expected_start_time, timeout)
    }

    /// Request forced termination on the retained Windows handle with a chosen
    /// exit code. `Ok(true)` means the termination request was accepted;
    /// `Ok(false)` means the process had already exited. This does not wait for
    /// exit: call [`Self::wait_for_exit`] before treating the process as dead.
    #[cfg(windows)]
    pub fn terminate_with_exit_code(&self, exit_code: u32) -> io::Result<bool> {
        windows::terminate_with_exit_code(self, exit_code)
    }

    /// Windows resources read through the retained handle, not by reopening its PID.
    #[cfg(windows)]
    pub fn resource_usage(&self) -> Option<ResourceUsage> {
        windows::resource_usage(self)
    }

    /// Cumulative CPU read from the retained Windows process handle, including
    /// after exit. Unlike working-set observations, final CPU remains available.
    #[cfg(windows)]
    pub fn cumulative_cpu_ms(&self) -> Option<u64> {
        windows::cumulative_cpu_ms(self)
    }

    /// Send `signal` to the process.
    ///
    /// With a pidfd the signal can only reach the process this handle was
    /// opened on: if that process has exited, the call fails with `ESRCH` even
    /// if the pid has been reused. Without one (macOS, or a Linux kernel with no
    /// pidfd) the signal goes to whatever holds the pid now, so callers should
    /// [`Self::observe`] immediately before signalling. What remains is the
    /// time between that check and this call; for a different process to be
    /// hit, the checked one must exit, be reaped, and have its pid handed to a
    /// new process inside that window, and both kernels hand out pids in
    /// increasing order, so a reuse needs the whole pid space to wrap first.
    ///
    /// `Ok(false)` means the process had already exited (`ESRCH`).
    pub fn signal(&self, signal: Signal) -> io::Result<bool> {
        #[cfg(any(target_os = "linux", target_os = "macos"))]
        {
            #[cfg(target_os = "linux")]
            let result = linux::signal(self.pid, self.pidfd.as_ref(), signal);
            #[cfg(target_os = "macos")]
            let result = macos::signal(self.pid, signal);
            match result {
                Ok(()) => Ok(true),
                Err(rustix::io::Errno::SRCH) => Ok(false),
                Err(error) => Err(error.into()),
            }
        }
        #[cfg(not(any(target_os = "linux", target_os = "macos")))]
        {
            let _ = signal;
            Err(io::Error::new(
                io::ErrorKind::Unsupported,
                "process signalling is not available on this platform",
            ))
        }
    }
}

#[cfg(all(test, any(target_os = "linux", target_os = "macos")))]
mod tests {
    use std::{
        io::Read,
        process::{Child, Command, Stdio},
        time::{Duration, Instant},
    };

    use super::*;

    fn spawn_sleep() -> Child {
        Command::new("sleep")
            .arg("60")
            .spawn()
            .expect("spawn sleep")
    }

    /// The executable a spawned `sleep` runs, resolved the way `Command` found it.
    fn sleep_identity() -> FileIdentity {
        let path = ["/bin/sleep", "/usr/bin/sleep"]
            .into_iter()
            .find(|path| Path::new(path).exists())
            .expect("sleep is installed");
        file_identity(Path::new(path)).expect("stat sleep")
    }

    /// Right after `spawn` returns the child may not have finished exec yet,
    /// and until then it still runs the test binary's image.
    fn wait_for_executable(process: &Process, expected: FileIdentity) -> Observation {
        let deadline = Instant::now() + Duration::from_secs(5);
        loop {
            let observation = process.observe().expect("child is alive");
            if observation.executable == Some(expected) || Instant::now() > deadline {
                return observation;
            }
            std::thread::sleep(Duration::from_millis(10));
        }
    }

    #[test]
    fn own_process_is_observable_with_its_own_image() {
        let process = Process::open(std::process::id())
            .expect("open own process")
            .expect("own process exists");
        let observation = process.observe().expect("own process is alive");
        let own_image = file_identity(&std::env::current_exe().unwrap()).unwrap();
        assert_eq!(observation.executable, Some(own_image));
        assert_eq!(start_time(std::process::id()), Some(observation.start_time));
    }

    #[test]
    fn child_start_time_is_stable_and_differs_from_ours() {
        let mut child = spawn_sleep();
        let pid = child.id();
        let process = Process::open(pid).unwrap().unwrap();
        let observation = wait_for_executable(&process, sleep_identity());
        assert_eq!(observation.executable, Some(sleep_identity()));
        assert_eq!(start_time(pid), Some(observation.start_time));
        child.kill().unwrap();
        child.wait().unwrap();
    }

    #[test]
    fn a_signalled_and_unreaped_child_reads_as_exited() {
        let mut child = spawn_sleep();
        let process = Process::open(child.id()).unwrap().unwrap();
        assert!(process.signal(Signal::Terminate).unwrap());
        let deadline = Instant::now() + Duration::from_secs(5);
        while process.observe().is_some() {
            assert!(Instant::now() < deadline, "child still observed as alive");
            std::thread::sleep(Duration::from_millis(10));
        }
        // Not yet reaped: the pid is still a zombie here, and still reads as exited.
        assert_eq!(start_time(child.id()), None);
        child.wait().unwrap();
    }

    #[test]
    fn a_reaped_child_cannot_be_opened_or_observed() {
        let mut child = spawn_sleep();
        let pid = child.id();
        child.kill().unwrap();
        child.wait().unwrap();
        // The pid could in principle be reused by now; either way it is not the child.
        if let Some(process) = Process::open(pid).unwrap() {
            if let Some(observation) = process.observe() {
                assert_ne!(observation.executable, Some(sleep_identity()));
            }
        }
    }

    /// The macOS fields are read at fixed offsets, so check the value is a
    /// plausible start time and not some other field: our own process started
    /// in the past, and not long ago.
    #[cfg(target_os = "macos")]
    #[test]
    fn macos_start_time_is_microseconds_since_the_epoch() {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_micros() as u64;
        let started = start_time(std::process::id()).unwrap();
        assert!(started <= now, "start time {started} is after now {now}");
        assert!(
            now - started < 3_600 * 1_000_000,
            "start time {started} is more than an hour before now {now}"
        );
    }

    /// Keeps one core busy for at least `wall` of wall-clock time.
    /// This thread's CPU time, from the thread CPU clock rather than the
    /// process-usage API under test.
    fn thread_cpu_time() -> Duration {
        let now = rustix::time::clock_gettime(rustix::time::ClockId::ThreadCPUTime);
        Duration::new(now.tv_sec as u64, now.tv_nsec as u32)
    }

    /// Spend `cpu` of this thread's CPU time. Measured on CPU time, not wall
    /// time: on a loaded machine the thread is descheduled for part of any
    /// wall interval, so a wall-timed loop can do far less work than its
    /// duration suggests. A generous wall cap keeps a stalled clock from
    /// hanging the test.
    fn burn_cpu(cpu: Duration) {
        let start = thread_cpu_time();
        let give_up = Instant::now() + Duration::from_secs(60);
        let mut value = 0u64;
        while thread_cpu_time().saturating_sub(start) < cpu {
            assert!(
                Instant::now() < give_up,
                "thread CPU clock stopped advancing"
            );
            for step in 0..10_000u64 {
                value = std::hint::black_box(value.wrapping_mul(31).wrapping_add(step));
            }
        }
        std::hint::black_box(value);
    }

    #[test]
    fn own_resource_usage_is_present_and_plausible() {
        // Clean executable pages need not count toward physical footprint, and
        // nextest runs this case in a fresh process with little private memory.
        // Touch and retain private pages so the byte/unit check has a known
        // lower bound instead of assuming a minimum footprint for the binary.
        let pages = vec![0xa5u8; 8 * 1024 * 1024];
        std::hint::black_box(&pages);
        let usage = resource_usage(std::process::id()).expect("own process is readable");
        assert!(
            usage.memory_bytes >= pages.len() as u64,
            "memory {} bytes cannot account for {} touched private bytes",
            usage.memory_bytes,
            pages.len()
        );
        std::hint::black_box(&pages);
        assert!(
            usage.memory_bytes < 64 * 1024 * 1024 * 1024,
            "memory {} bytes is implausibly large",
            usage.memory_bytes
        );
        #[cfg(target_os = "macos")]
        assert_eq!(usage.memory_kind, MemoryKind::PhysFootprint);
        #[cfg(target_os = "linux")]
        {
            assert_eq!(usage.memory_kind, MemoryKind::ResidentSet);
            assert!(usage.swap_bytes.is_some(), "Linux reports VmSwap");
        }
    }

    /// CPU time must grow with busy work, and by roughly the amount of work
    /// done: a reading in the wrong unit (for example Mach ticks taken as
    /// nanoseconds on Apple silicon, about 24 times too small) grows too, but
    /// not by enough.
    #[test]
    fn own_cpu_time_grows_by_about_the_busy_work_done() {
        let pid = std::process::id();
        let total = |usage: ResourceUsage| usage.cpu_user + usage.cpu_system;
        let before = total(resource_usage(pid).unwrap());
        let busy = Duration::from_millis(400);
        burn_cpu(busy);
        let after = total(resource_usage(pid).unwrap());
        let grown = after.saturating_sub(before);
        // This thread alone spent `busy` of CPU time, so the process total
        // grew by at least that much; other tests' threads only add to it.
        // The 10% allowance covers tick rounding in the reading, and is far
        // tighter than the ~24x a unit error would cause.
        assert!(
            grown >= busy * 9 / 10,
            "cpu time grew by {grown:?} over {busy:?} of busy work"
        );
    }

    #[test]
    fn a_child_reads_its_own_usage_not_ours() {
        struct ChildGuard(Child);
        impl Drop for ChildGuard {
            fn drop(&mut self) {
                let _ = self.0.kill();
                let _ = self.0.wait();
            }
        }

        // An unrelated executable's RSS need not be smaller than ours. Give
        // the child a known amount of touched private memory instead, with a
        // margin above the caller's footprint even while other tests run.
        let ours = resource_usage(std::process::id()).unwrap();
        let bytes = usize::try_from(ours.memory_bytes)
            .unwrap()
            .checked_add(64 * 1024 * 1024)
            .unwrap();
        let scratch = cortexkit_test_support::ScratchDir::new("subc-os-resource-usage");
        let ready = scratch.join("ready");
        let mut child = ChildGuard(
            Command::new(std::env::current_exe().unwrap())
                .args([
                    "tests::resource_usage_child",
                    "--exact",
                    "--ignored",
                    "--nocapture",
                    "--test-threads=1",
                ])
                .env("SUBC_OS_TEST_RESOURCE_BYTES", bytes.to_string())
                .env("SUBC_OS_TEST_RESOURCE_READY", &ready)
                .stdin(Stdio::piped())
                .stdout(Stdio::null())
                .spawn()
                .unwrap(),
        );
        let deadline = Instant::now() + Duration::from_secs(10);
        while !ready.exists() {
            assert!(child.0.try_wait().unwrap().is_none(), "memory child exited");
            assert!(Instant::now() < deadline, "memory child never became ready");
            std::thread::sleep(Duration::from_millis(5));
        }

        let ours = resource_usage(std::process::id()).unwrap();
        let usage = resource_usage(child.0.id()).expect("live child is readable");
        // The caller cannot meet the child's lower bound. Reading our own
        // usage for both pids must therefore fail, not satisfy the test.
        assert!(
            ours.memory_bytes < bytes as u64,
            "caller uses {} bytes, overlapping the child's {bytes} touched bytes",
            ours.memory_bytes,
        );
        assert!(
            usage.memory_bytes >= bytes as u64,
            "child uses {} bytes, fewer than its {bytes} touched bytes (caller uses {})",
            usage.memory_bytes,
            ours.memory_bytes,
        );
    }

    #[test]
    #[ignore = "re-exec fixture retaining touched private memory until its parent closes stdin"]
    fn resource_usage_child() {
        let bytes: usize = std::env::var("SUBC_OS_TEST_RESOURCE_BYTES")
            .unwrap()
            .parse()
            .unwrap();
        let ready = std::env::var_os("SUBC_OS_TEST_RESOURCE_READY").unwrap();
        let pages = vec![0xa5u8; bytes];
        std::hint::black_box(&pages);
        std::fs::write(ready, b"ready").unwrap();
        let _ = std::io::stdin().read(&mut [0]);
        std::hint::black_box(&pages);
    }

    #[test]
    fn an_exited_child_reads_as_unavailable_not_zero() {
        let mut child = spawn_sleep();
        let pid = child.id();
        child.kill().unwrap();
        // Killed but not reaped: a zombie, which still has a pid.
        let deadline = Instant::now() + Duration::from_secs(5);
        while start_time(pid).is_some() {
            assert!(Instant::now() < deadline, "child still observed as alive");
            std::thread::sleep(Duration::from_millis(10));
        }
        assert_eq!(resource_usage(pid), None, "a zombie reads as unavailable");
        child.wait().unwrap();
        // Reaped: the pid names nothing (barring reuse, which would be some
        // other live process and so still not a reading of zeros).
        if let Some(usage) = resource_usage(pid) {
            assert!(usage.memory_bytes > 0, "a reused pid is some live process");
        }
    }

    #[test]
    fn a_pid_with_no_process_reads_as_unavailable() {
        // Above both kernels' pid limits (Linux caps pid_max at 2^22, macOS at
        // 99998), so nothing can hold it.
        assert_eq!(resource_usage(i32::MAX as u32), None);
        // Not a representable pid at all.
        assert_eq!(resource_usage(u32::MAX), None);
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn linux_signals_go_through_a_pidfd() {
        let mut child = spawn_sleep();
        let process = Process::open(child.id()).unwrap().unwrap();
        assert!(process.signals_through_pidfd());
        child.kill().unwrap();
        child.wait().unwrap();
        // The pidfd still names the reaped child, so a signal cannot reach anything else.
        assert!(!process.signal(Signal::Kill).unwrap());
    }
}