srb_gui 0.1.0

GUI for the Space Robotics Bench
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
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
use std::fmt;
#[cfg(target_os = "linux")]
use std::fs::OpenOptions;
use std::io;
#[cfg(target_os = "linux")]
use std::io::{Read, Write};
#[cfg(target_os = "linux")]
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
#[cfg(target_os = "linux")]
use std::os::unix::net::UnixStream;
#[cfg(target_os = "linux")]
use std::os::unix::process::CommandExt as _;
use std::path::PathBuf;
use std::process::{Child, Command, ExitStatus};
#[cfg(target_os = "linux")]
use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};
#[cfg(target_os = "linux")]
use std::sync::mpsc;

use std::time::{Duration, Instant};
#[cfg(target_os = "linux")]
use std::{fs, mem::MaybeUninit, sync::OnceLock};

const STARTUP_WAIT: Duration = Duration::from_secs(2);
const STOP_GRACE_PERIOD: Duration = Duration::from_millis(250);
const TERM_GRACE_PERIOD: Duration = Duration::from_millis(500);
const KILL_GRACE_PERIOD: Duration = Duration::from_millis(500);
const REAP_GRACE_PERIOD: Duration = Duration::from_millis(500);
// Flushing the captured log may first flush unrelated journal data; 500 ms
// proved too short on a loaded host and misreported clean stops as failures.

const OUTPUT_GRACE_PERIOD: Duration = Duration::from_secs(2);

#[cfg(target_os = "linux")]
const CLEANUP_RECONCILIATION_INTERVAL: Duration = Duration::from_millis(1);
#[cfg(target_os = "linux")]
const STARTUP_GATE_TIMEOUT: Duration = Duration::from_secs(2);
#[cfg(target_os = "linux")]
const STARTUP_GATE_GO: u8 = b'G';
#[cfg(target_os = "linux")]
const STARTUP_GATE_ABORT: u8 = b'A';

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GuiProcessKind {
    Interactive,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GuiProcessStatus {
    Exited,
    TimedOut,
    Interrupted,
    Terminated,
    Killed,
    CleanupFailed,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GuiProcessSignal {
    Int,
    Term,
    Kill,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GuiCleanupErrorKind {
    #[cfg(not(target_os = "linux"))]
    UnsupportedPlatform,
    Ownership,
    Wait,
    Signal,
    Reap,
    Deadline,
    Output,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GuiCleanupError {
    pub kind: GuiCleanupErrorKind,
    pub message: String,
}

impl GuiCleanupError {
    fn new(kind: GuiCleanupErrorKind, message: impl Into<String>) -> Self {
        Self {
            kind,
            message: message.into(),
        }
    }
}

impl fmt::Display for GuiCleanupError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{:?}: {}", self.kind, self.message)
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GuiProcessResult {
    pub argv: Vec<String>,
    pub kind: GuiProcessKind,
    pub pid: Option<u32>,
    pub process_group_id: Option<i32>,
    pub session_id: Option<i32>,
    pub status: GuiProcessStatus,
    pub exit_status: Option<ExitStatus>,
    pub output_path: PathBuf,
    pub error: Option<String>,
    pub cleanup_errors: Vec<GuiCleanupError>,
    pub cause: Option<GuiProcessStatus>,
    pub first_signal: Option<GuiProcessSignal>,
    pub last_signal: Option<GuiProcessSignal>,
    pub term_sent: bool,
    pub kill_sent: bool,
    pub reaped: bool,
    pub descendants_reaped: bool,
    pub group_empty: bool,
    pub session_empty: bool,
    pub output_complete: bool,
    pub cleanup_complete: bool,
}

#[derive(Clone, Default)]
struct GuiSignalEvidence {
    first: Option<GuiProcessSignal>,
    last: Option<GuiProcessSignal>,
    term_sent: bool,
    kill_sent: bool,
}

impl GuiSignalEvidence {
    #[cfg(target_os = "linux")]
    fn record(&mut self, signal: GuiProcessSignal) {
        self.first.get_or_insert(signal);
        self.last = Some(signal);
        match signal {
            GuiProcessSignal::Int => {}
            GuiProcessSignal::Term => self.term_sent = true,
            GuiProcessSignal::Kill => self.kill_sent = true,
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct GuiProcessDeadlines {
    startup: Duration,
    stop: Duration,
    term: Duration,
    kill: Duration,
    reap: Duration,
    output: Duration,
}

impl Default for GuiProcessDeadlines {
    fn default() -> Self {
        Self {
            startup: STARTUP_WAIT,
            stop: STOP_GRACE_PERIOD,
            term: TERM_GRACE_PERIOD,
            kill: KILL_GRACE_PERIOD,
            reap: REAP_GRACE_PERIOD,
            output: OUTPUT_GRACE_PERIOD,
        }
    }
}

#[derive(Debug)]
pub(crate) enum OwnedProcessSpawnError {
    Io(io::Error),
    Supervision(String),
}

impl fmt::Display for OwnedProcessSpawnError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Io(error) => error.fmt(formatter),
            Self::Supervision(message) => formatter.write_str(message),
        }
    }
}

pub(crate) struct OwnedProcess {
    child: Child,
    argv: Vec<String>,
    kind: GuiProcessKind,
    pid: u32,
    process_group_id: Option<i32>,
    session_id: Option<i32>,
    output_path: PathBuf,
    deadlines: GuiProcessDeadlines,
    exit_status: Option<ExitStatus>,
    failed_cleanup_cause: Option<GuiProcessStatus>,
    signal_evidence: GuiSignalEvidence,
    completed_result: Option<GuiProcessResult>,
    #[cfg(target_os = "linux")]
    leader_identity: LinuxLeaderIdentity,
    #[cfg(target_os = "linux")]
    leader_exit_observed: bool,
    #[cfg(target_os = "linux")]
    ownership_discharged: bool,
    #[cfg(target_os = "linux")]
    parent_signal_relay: Option<ScopedParentSignalRelay>,
    #[cfg(target_os = "linux")]
    /// In-flight output sync, tagged with its path to prevent retries from using
    /// a stale result after `output_path` changes.
    output_sync: Option<(PathBuf, mpsc::Receiver<io::Result<()>>)>,
}

impl OwnedProcess {
    pub(crate) fn spawn_bound(
        command: Command,
        argv: Vec<String>,
        kind: GuiProcessKind,
        output_path: PathBuf,
    ) -> Result<Self, OwnedProcessSpawnError> {
        #[cfg(target_os = "linux")]
        {
            Self::spawn_bound_with_probe(
                command,
                argv,
                kind,
                output_path,
                capture_linux_leader_identity,
            )
        }
        #[cfg(not(target_os = "linux"))]
        {
            let mut command = command;
            let child = command.spawn().map_err(OwnedProcessSpawnError::Io)?;
            let pid = child.id();
            #[cfg(all(unix, not(target_os = "linux")))]
            let (process_group_id, session_id) = (Some(pid as i32), Some(pid as i32));
            #[cfg(not(unix))]
            let (process_group_id, session_id) = (None, None);
            Ok(Self {
                child,
                argv,
                kind,
                pid,
                process_group_id,
                session_id,
                output_path,
                deadlines: GuiProcessDeadlines::default(),
                exit_status: None,
                failed_cleanup_cause: None,
                signal_evidence: GuiSignalEvidence::default(),
                completed_result: None,
            })
        }
    }

    #[cfg(target_os = "linux")]
    fn spawn_bound_with_probe(
        command: Command,
        argv: Vec<String>,
        kind: GuiProcessKind,
        output_path: PathBuf,
        identity_probe: fn(i32) -> Result<LinuxLeaderIdentity, String>,
    ) -> Result<Self, OwnedProcessSpawnError> {
        enable_linux_child_subreaper().map_err(OwnedProcessSpawnError::Supervision)?;
        let parent_signal_relay =
            ScopedParentSignalRelay::install().map_err(OwnedProcessSpawnError::Supervision)?;
        let (child, leader_identity) = spawn_linux_identity_gated(command, identity_probe)?;
        let pid = child.id();
        if pid as i32 != leader_identity.pid {
            let mut child = child;
            let _ = child.kill();
            let _ = child.wait();
            return Err(OwnedProcessSpawnError::Supervision(format!(
                "startup identity PID {} did not match child handle PID {pid}",
                leader_identity.pid
            )));
        }
        Ok(Self {
            child,
            argv,
            kind,
            pid,
            process_group_id: Some(leader_identity.process_group_id),
            session_id: Some(leader_identity.session_id),
            output_path,
            deadlines: GuiProcessDeadlines::default(),
            exit_status: None,
            failed_cleanup_cause: None,
            signal_evidence: GuiSignalEvidence::default(),
            completed_result: None,
            leader_identity,
            leader_exit_observed: false,
            ownership_discharged: false,
            parent_signal_relay: Some(parent_signal_relay),
            output_sync: None,
        })
    }

    pub(crate) fn wait_for_startup(&mut self) -> Result<bool, String> {
        self.wait_timeout(self.deadlines.startup)
    }

    pub(crate) fn is_running(&mut self) -> Result<bool, String> {
        if self.completed_result.is_some() {
            return Ok(false);
        }
        self.wait_timeout(Duration::ZERO)
    }

    pub(crate) fn leader_exited(&self) -> bool {
        #[cfg(target_os = "linux")]
        {
            self.leader_exit_observed || self.exit_status.is_some()
        }
        #[cfg(not(target_os = "linux"))]
        {
            self.exit_status.is_some()
        }
    }

    pub(crate) fn stop(&mut self) -> GuiProcessResult {
        if let Some(signal) = self.take_parent_signal() {
            return self.stop_after_parent_signal(signal);
        }
        self.stop_with_cause(None)
    }

    pub(crate) fn stop_with_cause(
        &mut self,
        requested_cause: Option<GuiProcessStatus>,
    ) -> GuiProcessResult {
        self.stop_with_first_signal(requested_cause, GuiProcessSignal::Term)
    }

    pub(crate) fn stop_after_parent_signal(
        &mut self,
        signal: GuiProcessSignal,
    ) -> GuiProcessResult {
        self.stop_with_first_signal(Some(Self::signal_cause(signal)), signal)
    }

    fn signal_cause(signal: GuiProcessSignal) -> GuiProcessStatus {
        match signal {
            GuiProcessSignal::Int => GuiProcessStatus::Interrupted,
            GuiProcessSignal::Term => GuiProcessStatus::Terminated,
            GuiProcessSignal::Kill => GuiProcessStatus::Killed,
        }
    }

    /// Consume parent SIGTERM/SIGINT recorded while this child was owned.
    /// The scoped relay is restored on drop. The first signal starts bounded
    /// cleanup while the GUI stays open to report the result, preventing an
    /// interrupted cleanup from orphaning a session-detached Isaac process.
    /// Later signals advance escalation one phase at a time; they do not
    /// shorten the SIGKILL wait.
    pub(crate) fn take_parent_signal(&self) -> Option<GuiProcessSignal> {
        #[cfg(target_os = "linux")]
        {
            self.parent_signal_relay
                .as_ref()
                .and_then(ScopedParentSignalRelay::take_signal)
        }
        #[cfg(not(target_os = "linux"))]
        {
            None
        }
    }

    fn stop_with_first_signal(
        &mut self,
        requested_cause: Option<GuiProcessStatus>,
        first_signal: GuiProcessSignal,
    ) -> GuiProcessResult {
        if let Some(result) = &self.completed_result {
            return result.clone();
        }

        #[cfg(target_os = "linux")]
        let result = self.stop_linux(requested_cause, GroupSignal::from_gui(first_signal));
        #[cfg(not(target_os = "linux"))]
        let result = self.stop_unsupported(requested_cause);

        if result.cleanup_complete {
            #[cfg(target_os = "linux")]
            drop(self.parent_signal_relay.take());
            self.completed_result = Some(result.clone());
        }
        result
    }

    #[cfg(target_os = "linux")]
    fn stop_linux(
        &mut self,
        requested_cause: Option<GuiProcessStatus>,
        first_signal: GroupSignal,
    ) -> GuiProcessResult {
        self.stop_linux_with_observer(requested_cause, first_signal, |owned| {
            owned.observe_linux_session()
        })
    }

    #[cfg(target_os = "linux")]
    fn stop_linux_with_observer<Observe>(
        &mut self,
        requested_cause: Option<GuiProcessStatus>,
        first_signal: GroupSignal,
        mut observe: Observe,
    ) -> GuiProcessResult
    where
        Observe: FnMut(&mut OwnedProcess) -> io::Result<LinuxSessionState>,
    {
        if self.ownership_discharged {
            return self.retry_linux_output_completion(requested_cause);
        }

        let mut errors = Vec::new();
        let mut proof_resolvable_errors = Vec::new();
        let mut signal_evidence = self.signal_evidence.clone();
        let mut attempt_cause = requested_cause;
        let mut result_cause = requested_cause;

        let mut wait_disappeared = false;
        let mut running = match self.wait_linux(self.deadlines.stop, ParentSignalWait::Escalate) {
            Ok((running, consumed_signal)) => {
                result_cause = consumed_signal.or(result_cause);
                running
            }
            Err(error) => {
                Self::record_io_cause(&error, &mut attempt_cause);
                wait_disappeared = Self::push_wait_error(
                    &error,
                    format!("failed during graceful wait: {error}"),
                    &mut errors,
                    &mut proof_resolvable_errors,
                );
                true
            }
        };

        if running {
            let signal_attempt = self.send_linux_signal(first_signal);
            if signal_attempt.target_signaled {
                signal_evidence.record(first_signal.to_gui());
            }
            if let Some(error) = signal_attempt.error {
                Self::record_io_cause(&error, &mut attempt_cause);
                let cleanup_error = GuiCleanupError::new(
                    GuiCleanupErrorKind::Signal,
                    format!(
                        "failed to send {} to owned process session: {error}",
                        first_signal.name()
                    ),
                );
                if Self::linux_signal_error_is_session_disappearance(&error) {
                    proof_resolvable_errors.push(cleanup_error);
                } else {
                    errors.push(cleanup_error);
                }
            }
            running = match self.wait_linux(self.deadlines.term, ParentSignalWait::Escalate) {
                Ok((running, consumed_signal)) => {
                    result_cause = consumed_signal.or(result_cause);
                    wait_disappeared = false;
                    running
                }
                Err(error) => {
                    Self::record_io_cause(&error, &mut attempt_cause);
                    wait_disappeared = Self::push_wait_error(
                        &error,
                        format!(
                            "failed while waiting after {}: {error}",
                            first_signal.name()
                        ),
                        &mut errors,
                        &mut proof_resolvable_errors,
                    );
                    true
                }
            };
        }

        if running {
            let signal_attempt = self.send_linux_signal(GroupSignal::Kill);
            if signal_attempt.target_signaled {
                signal_evidence.record(GuiProcessSignal::Kill);
            }
            if let Some(error) = signal_attempt.error {
                Self::record_io_cause(&error, &mut attempt_cause);
                let cleanup_error = GuiCleanupError::new(
                    GuiCleanupErrorKind::Signal,
                    format!("failed to send SIGKILL to owned process group: {error}"),
                );
                if Self::linux_signal_error_is_session_disappearance(&error) {
                    proof_resolvable_errors.push(cleanup_error);
                } else {
                    errors.push(cleanup_error);
                }
            }
            running = match self.wait_linux(self.deadlines.kill, ParentSignalWait::Continue) {
                Ok((running, consumed_signal)) => {
                    result_cause = consumed_signal.or(result_cause);
                    wait_disappeared = false;
                    running
                }
                Err(error) => {
                    Self::record_io_cause(&error, &mut attempt_cause);
                    wait_disappeared = Self::push_wait_error(
                        &error,
                        format!("failed while waiting after SIGKILL: {error}"),
                        &mut errors,
                        &mut proof_resolvable_errors,
                    );
                    true
                }
            };
        }

        if running && !wait_disappeared {
            errors.push(GuiCleanupError::new(
                GuiCleanupErrorKind::Deadline,
                "owned process session did not exit within cleanup deadline",
            ));
        }

        let reap_deadline = Instant::now() + self.deadlines.reap;
        let mut proof_errors = Vec::new();
        let mut descendants_reaped = false;
        let mut group_empty = false;
        let mut session_empty = false;
        while Instant::now() < reap_deadline {
            match observe(self) {
                Ok(state) => {
                    descendants_reaped = state.group_empty && state.session_empty;
                    if state.leader_exited && descendants_reaped {
                        match self.reap_linux_leader_until(reap_deadline) {
                            Ok(()) => {
                                let leader_reaped = self.exit_status.is_some();
                                // Group/session emptiness is public only after the retained zombie
                                // leader is reaped.
                                group_empty = leader_reaped && state.group_empty;
                                session_empty = leader_reaped && state.session_empty;
                                if leader_reaped && group_empty && session_empty {
                                    break;
                                }
                            }
                            Err(error) => {
                                Self::record_io_cause(&error, &mut attempt_cause);
                                let cleanup_error = GuiCleanupError::new(
                                    GuiCleanupErrorKind::Reap,
                                    format!("failed to reap owned process leader: {error}"),
                                );
                                if !proof_errors.contains(&cleanup_error) {
                                    proof_errors.push(cleanup_error);
                                }
                            }
                        }
                    }
                }
                Err(error) => {
                    Self::record_io_cause(&error, &mut attempt_cause);
                    let cleanup_error = GuiCleanupError::new(
                        GuiCleanupErrorKind::Reap,
                        format!("failed to prove owned descendant reaping: {error}"),
                    );
                    if !proof_errors.contains(&cleanup_error) {
                        proof_errors.push(cleanup_error);
                    }
                }
            }
            let remaining = reap_deadline.saturating_duration_since(Instant::now());
            if remaining.is_zero() {
                break;
            }
            std::thread::sleep(remaining.min(CLEANUP_RECONCILIATION_INTERVAL));
        }
        let reaped = self.exit_status.is_some();
        if !reaped {
            let cleanup_error = GuiCleanupError::new(
                GuiCleanupErrorKind::Reap,
                "owned subprocess leader was not reaped",
            );
            if !proof_errors.contains(&cleanup_error) {
                proof_errors.push(cleanup_error);
            }
        }
        if !descendants_reaped {
            let cleanup_error = GuiCleanupError::new(
                GuiCleanupErrorKind::Deadline,
                "owned subprocess descendants were not fully reaped within the cleanup deadline",
            );
            if !proof_errors.contains(&cleanup_error) {
                proof_errors.push(cleanup_error);
            }
        }
        let physical_cleanup_proven = reaped && descendants_reaped && group_empty && session_empty;
        if !physical_cleanup_proven {
            errors.extend(proof_resolvable_errors);
        }
        // Once process cleanup is proven, retries only prove output completion;
        // prior attempt errors stay in the result.
        self.ownership_discharged = physical_cleanup_proven;

        let mut output_complete = false;
        if physical_cleanup_proven {
            let output_deadline = Instant::now() + self.deadlines.output;
            while Instant::now() < output_deadline {
                match self.prove_output_complete_until(output_deadline) {
                    Ok(()) => {
                        output_complete = true;
                        break;
                    }
                    Err(error) => {
                        Self::record_io_cause(&error, &mut attempt_cause);
                        let cleanup_error = GuiCleanupError::new(
                            GuiCleanupErrorKind::Output,
                            format!("failed to prove captured output completion: {error}"),
                        );
                        if !proof_errors.contains(&cleanup_error) {
                            proof_errors.push(cleanup_error);
                        }
                    }
                }
                let remaining = output_deadline.saturating_duration_since(Instant::now());
                if remaining.is_zero() {
                    break;
                }
                std::thread::sleep(remaining.min(CLEANUP_RECONCILIATION_INTERVAL));
            }
            if !output_complete
                && !proof_errors
                    .iter()
                    .any(|error| error.kind == GuiCleanupErrorKind::Output)
            {
                proof_errors.push(GuiCleanupError::new(
                    GuiCleanupErrorKind::Output,
                    "captured output completion was not proven within the cleanup deadline",
                ));
            }
        }
        if output_complete {
            proof_errors.clear();
        }
        errors.extend(proof_errors);

        self.build_result(
            result_cause,
            attempt_cause,
            errors,
            signal_evidence,
            reaped,
            descendants_reaped,
            group_empty,
            session_empty,
            output_complete,
        )
    }

    #[cfg(target_os = "linux")]
    fn retry_linux_output_completion(
        &mut self,
        requested_cause: Option<GuiProcessStatus>,
    ) -> GuiProcessResult {
        debug_assert!(self.exit_status.is_some());
        let mut errors = Vec::new();
        let mut attempt_cause = requested_cause;
        let output_deadline = Instant::now() + self.deadlines.output;
        let mut output_complete = false;
        while Instant::now() < output_deadline {
            match self.prove_output_complete_until(output_deadline) {
                Ok(()) => {
                    output_complete = true;
                    break;
                }
                Err(error) => {
                    Self::record_io_cause(&error, &mut attempt_cause);
                    let cleanup_error = GuiCleanupError::new(
                        GuiCleanupErrorKind::Output,
                        format!("failed to prove captured output completion: {error}"),
                    );
                    if !errors.contains(&cleanup_error) {
                        errors.push(cleanup_error);
                    }
                }
            }
            let remaining = output_deadline.saturating_duration_since(Instant::now());
            if remaining.is_zero() {
                break;
            }
            std::thread::sleep(remaining.min(CLEANUP_RECONCILIATION_INTERVAL));
        }
        if !output_complete && errors.is_empty() {
            errors.push(GuiCleanupError::new(
                GuiCleanupErrorKind::Output,
                "captured output completion was not proven within the cleanup deadline",
            ));
        }
        self.build_result(
            requested_cause,
            attempt_cause,
            errors,
            self.signal_evidence.clone(),
            true,
            true,
            true,
            true,
            output_complete,
        )
    }

    #[cfg(not(target_os = "linux"))]
    fn stop_unsupported(&mut self, requested_cause: Option<GuiProcessStatus>) -> GuiProcessResult {
        let mut errors = vec![GuiCleanupError::new(
            GuiCleanupErrorKind::UnsupportedPlatform,
            unsupported_cleanup_reason(),
        )];
        let mut signal_evidence = self.signal_evidence.clone();
        // Dispose only the exact leader represented by Child: Windows uses
        // its stable process handle, while Unix keeps an unreaped direct child
        // PID reserved. No raw process-group signal is issued, and descendant
        // ownership is deliberately never claimed on these platforms.
        match self.child.try_wait() {
            Ok(Some(status)) => self.exit_status = Some(status),
            Ok(None) => match self.child.kill() {
                Ok(()) => {
                    signal_evidence.kill_sent = true;
                    signal_evidence.first = Some(GuiProcessSignal::Kill);
                    signal_evidence.last = Some(GuiProcessSignal::Kill);
                    match self.wait_timeout(self.deadlines.kill) {
                        Ok(false) => {}
                        Ok(true) => errors.push(GuiCleanupError::new(
                            GuiCleanupErrorKind::Deadline,
                            "exact subprocess leader did not exit within cleanup deadline",
                        )),
                        Err(error) => errors.push(GuiCleanupError::new(
                            GuiCleanupErrorKind::Reap,
                            format!("failed to reap exact subprocess leader: {error}"),
                        )),
                    }
                }
                Err(error) => errors.push(GuiCleanupError::new(
                    GuiCleanupErrorKind::Signal,
                    format!("failed to terminate exact subprocess leader: {error}"),
                )),
            },
            Err(error) => errors.push(GuiCleanupError::new(
                GuiCleanupErrorKind::Wait,
                format!("failed to query subprocess leader: {error}"),
            )),
        }
        self.build_result(
            requested_cause,
            requested_cause,
            errors,
            signal_evidence,
            self.exit_status.is_some(),
            false,
            false,
            false,
            false,
        )
    }

    #[allow(clippy::too_many_arguments)]
    fn build_result(
        &mut self,
        requested_cause: Option<GuiProcessStatus>,
        attempt_cause: Option<GuiProcessStatus>,
        cleanup_errors: Vec<GuiCleanupError>,
        signal_evidence: GuiSignalEvidence,
        reaped: bool,
        descendants_reaped: bool,
        group_empty: bool,
        session_empty: bool,
        output_complete: bool,
    ) -> GuiProcessResult {
        self.signal_evidence = signal_evidence.clone();
        let cleanup_complete = reaped
            && descendants_reaped
            && group_empty
            && session_empty
            && output_complete
            && cleanup_errors.is_empty();
        let failure_cause = attempt_cause.or(self.failed_cleanup_cause);
        let cause = if cleanup_complete {
            self.failed_cleanup_cause = None;
            None
        } else {
            self.failed_cleanup_cause = failure_cause;
            failure_cause
        };
        let status = if !cleanup_complete {
            GuiProcessStatus::CleanupFailed
        } else if let Some(status) = requested_cause {
            status
        } else if signal_evidence.kill_sent {
            GuiProcessStatus::Killed
        } else if signal_evidence.term_sent {
            GuiProcessStatus::Terminated
        } else {
            GuiProcessStatus::Exited
        };
        let error = (!cleanup_errors.is_empty()).then(|| {
            cleanup_errors
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>()
                .join("; ")
        });
        GuiProcessResult {
            argv: self.argv.clone(),
            kind: self.kind,
            pid: Some(self.pid),
            process_group_id: self.process_group_id,
            session_id: self.session_id,
            status,
            exit_status: self.exit_status,
            output_path: self.output_path.clone(),
            error,
            cleanup_errors,
            cause,
            first_signal: signal_evidence.first,
            last_signal: signal_evidence.last,
            term_sent: signal_evidence.term_sent,
            kill_sent: signal_evidence.kill_sent,
            reaped,
            descendants_reaped,
            group_empty,
            session_empty,
            output_complete,
            cleanup_complete,
        }
    }

    fn close_child_handles(&mut self) {
        drop(self.child.stdin.take());
        drop(self.child.stdout.take());
        drop(self.child.stderr.take());
    }

    #[cfg(target_os = "linux")]
    fn prove_output_complete_until(&mut self, deadline: Instant) -> io::Result<()> {
        self.close_child_handles();
        // At most one synchronizer thread is in flight per owned process: a
        // retry after a timeout waits on the same outstanding `fsync` instead
        // of stacking another blocked thread on the same file. A synchronizer
        // started for a different `output_path`, or one that already finished
        // with an error, is never reused: its result describes an earlier
        // attempt, not the file this retry has to prove.
        let stale = match self.output_sync.take() {
            Some((path, receiver)) if path == self.output_path => Some(receiver),
            _ => None,
        };
        if let Some(receiver) = stale {
            match receiver.recv_timeout(deadline.saturating_duration_since(Instant::now())) {
                Ok(Ok(())) => return Ok(()),
                Ok(Err(_)) | Err(mpsc::RecvTimeoutError::Disconnected) => {}
                Err(mpsc::RecvTimeoutError::Timeout) => {
                    self.output_sync = Some((self.output_path.clone(), receiver));
                    return Err(io::Error::new(
                        io::ErrorKind::TimedOut,
                        "captured output synchronization exceeded its deadline",
                    ));
                }
            }
        }
        let output_path = self.output_path.clone();
        let (sender, receiver) = mpsc::sync_channel(1);
        std::thread::Builder::new()
            .name("srb-gui-output-sync".to_string())
            .spawn(move || {
                let result = OpenOptions::new()
                    .read(true)
                    .append(true)
                    .open(output_path)
                    .and_then(|file| file.sync_all());
                let _ = sender.send(result);
            })?;
        match receiver.recv_timeout(deadline.saturating_duration_since(Instant::now())) {
            Ok(result) => result,
            Err(mpsc::RecvTimeoutError::Timeout) => {
                self.output_sync = Some((self.output_path.clone(), receiver));
                Err(io::Error::new(
                    io::ErrorKind::TimedOut,
                    "captured output synchronization exceeded its deadline",
                ))
            }
            Err(mpsc::RecvTimeoutError::Disconnected) => Err(io::Error::new(
                io::ErrorKind::BrokenPipe,
                "captured output synchronizer disconnected",
            )),
        }
    }

    #[cfg(target_os = "linux")]
    fn wait_timeout(&mut self, timeout: Duration) -> Result<bool, String> {
        self.wait_linux(timeout, ParentSignalWait::Interrupt)
            .map(|(running, _)| running)
            .map_err(|error| error.to_string())
    }

    #[cfg(not(target_os = "linux"))]
    fn wait_timeout(&mut self, timeout: Duration) -> Result<bool, String> {
        let started = Instant::now();
        loop {
            match self.child.try_wait() {
                Ok(Some(status)) => {
                    self.exit_status = Some(status);
                    return Ok(false);
                }
                Ok(None) if started.elapsed() >= timeout => return Ok(true),
                Ok(None) => std::thread::sleep(Duration::from_millis(1)),
                Err(error) => {
                    return Err(format!("failed to query subprocess leader: {error}"));
                }
            }
        }
    }

    #[cfg(target_os = "linux")]
    fn wait_linux(
        &mut self,
        timeout: Duration,
        on_parent_signal: ParentSignalWait,
    ) -> io::Result<(bool, Option<GuiProcessStatus>)> {
        let started = Instant::now();
        loop {
            if let Some(relay) = self.parent_signal_relay.as_ref() {
                match on_parent_signal {
                    ParentSignalWait::Interrupt if relay.has_signal() => {
                        return Err(io::Error::new(
                            io::ErrorKind::Interrupted,
                            "parent SIGTERM/SIGINT is pending owned cleanup",
                        ));
                    }
                    ParentSignalWait::Escalate => {
                        if let Some(signal) = relay.take_signal() {
                            return Ok((true, Some(Self::signal_cause(signal))));
                        }
                    }
                    _ => {}
                }
            }
            let state = self.observe_linux_session()?;
            if !state.active() {
                return Ok((false, None));
            }
            let remaining = timeout.saturating_sub(started.elapsed());
            if remaining.is_zero() {
                return Ok((true, None));
            }
            std::thread::sleep(remaining.min(Duration::from_millis(10)));
        }
    }

    #[cfg(target_os = "linux")]
    fn observe_linux_session(&mut self) -> io::Result<LinuxSessionState> {
        let identity = self.linux_leader_identity();
        let snapshot = probe_linux_leader(identity.pid)?;
        validate_linux_leader_identity(identity, snapshot)?;
        self.leader_exit_observed |=
            linux_leader_exited_wnowait(identity.pid)? || snapshot.stat.state == b'Z';

        let mut members = scan_linux_owned_members(identity)?;
        let mut reaped_any = false;
        for member in &members {
            if member.state == b'Z' && member.parent_pid == std::process::id() as i32 {
                reaped_any |= reap_linux_owned_descendant(identity, *member)?;
            }
        }
        if reaped_any {
            members = scan_linux_owned_members(identity)?;
        }
        Ok(LinuxSessionState::from_members(
            identity,
            self.leader_exit_observed,
            &members,
        ))
    }

    #[cfg(target_os = "linux")]
    fn linux_leader_identity(&self) -> LinuxLeaderIdentity {
        self.leader_identity
    }

    #[cfg(target_os = "linux")]
    fn send_linux_signal(&self, signal: GroupSignal) -> LinuxSignalAttempt {
        let identity = self.linux_leader_identity();
        signal_linux_owned_members(identity, signal)
    }

    #[cfg(target_os = "linux")]
    fn reap_linux_leader_until(&mut self, deadline: Instant) -> io::Result<()> {
        if self.exit_status.is_some() {
            return Ok(());
        }
        loop {
            match self.child.try_wait()? {
                Some(status) => {
                    self.exit_status = Some(status);
                    return Ok(());
                }
                None if Instant::now() >= deadline => {
                    return Err(io::Error::new(
                        io::ErrorKind::TimedOut,
                        "owned leader reaping exceeded its deadline",
                    ));
                }
                None => {
                    let remaining = deadline.saturating_duration_since(Instant::now());
                    std::thread::sleep(remaining.min(CLEANUP_RECONCILIATION_INTERVAL));
                }
            }
        }
    }

    #[cfg(target_os = "linux")]
    fn linux_signal_error_is_session_disappearance(error: &io::Error) -> bool {
        error.kind() == io::ErrorKind::NotFound
            || matches!(
                error.raw_os_error(),
                Some(code) if code == nix::libc::ESRCH
                    || code == nix::libc::ENOENT
                    || code == nix::libc::ECHILD
            )
    }

    /// Record a wait-phase failure.
    ///
    /// Session disappearance (`ESRCH`/`ENOENT`/`ECHILD`, or a `NotFound`
    /// `/proc` read) means the owned leader vanished while cleanup was
    /// waiting on it. That is only a defect when the later reaping proof
    /// fails, so it is kept proof-resolvable exactly like the equivalent
    /// signal-phase failure; every other wait error is a hard cleanup error.
    #[cfg(target_os = "linux")]
    fn push_wait_error(
        error: &io::Error,
        message: String,
        errors: &mut Vec<GuiCleanupError>,
        proof_resolvable_errors: &mut Vec<GuiCleanupError>,
    ) -> bool {
        let cleanup_error = GuiCleanupError::new(GuiCleanupErrorKind::Wait, message);
        let disappearance = Self::linux_signal_error_is_session_disappearance(error);
        if disappearance {
            proof_resolvable_errors.push(cleanup_error);
        } else {
            errors.push(cleanup_error);
        }
        disappearance
    }

    #[cfg(target_os = "linux")]
    fn record_io_cause(error: &io::Error, cause: &mut Option<GuiProcessStatus>) {
        match error.kind() {
            io::ErrorKind::Interrupted => *cause = Some(GuiProcessStatus::Interrupted),
            io::ErrorKind::TimedOut => *cause = Some(GuiProcessStatus::TimedOut),
            _ => {}
        }
    }
}

impl Drop for OwnedProcess {
    fn drop(&mut self) {
        let already_clean = self
            .completed_result
            .as_ref()
            .is_some_and(|result| result.cleanup_complete);
        if !already_clean {
            let result = self.stop();
            if !result.cleanup_complete {
                let mut exact_leader_reaped = false;
                match self.child.try_wait() {
                    Ok(Some(status)) => {
                        self.exit_status = Some(status);
                        exact_leader_reaped = true;
                    }
                    Ok(None) => {
                        let _ = self.child.kill();
                        let deadline = Instant::now() + self.deadlines.reap;
                        while Instant::now() < deadline {
                            match self.child.try_wait() {
                                Ok(Some(status)) => {
                                    self.exit_status = Some(status);
                                    exact_leader_reaped = true;
                                    break;
                                }
                                Ok(None) => std::thread::sleep(Duration::from_millis(1)),
                                Err(_) => break,
                            }
                        }
                    }
                    Err(_) => {}
                }
                eprintln!(
                    "bounded GUI process cleanup failed during Drop: pid={} \
                     exact_leader_reaped={} error={}",
                    self.pid,
                    exact_leader_reaped,
                    result.error.as_deref().unwrap_or("missing cleanup proof")
                );
            }
        }
        #[cfg(target_os = "linux")]
        drop(self.parent_signal_relay.take());
        self.close_child_handles();
    }
}

#[cfg(all(unix, not(target_os = "linux")))]
fn unsupported_cleanup_reason() -> &'static str {
    "stable session identity and descendant reaping are unsupported on this Unix platform"
}

#[cfg(windows)]
fn unsupported_cleanup_reason() -> &'static str {
    "Windows descendant cleanup requires a Job Object; leader-only cleanup is unsupported"
}

#[cfg(all(not(unix), not(windows)))]
fn unsupported_cleanup_reason() -> &'static str {
    "owned descendant cleanup is unsupported on this platform"
}

#[cfg(target_os = "linux")]
#[derive(Clone, Copy)]
enum GroupSignal {
    Int,
    Term,
    Kill,
}

#[cfg(target_os = "linux")]
struct LinuxSignalAttempt {
    target_signaled: bool,
    error: Option<io::Error>,
}

#[cfg(target_os = "linux")]
impl GroupSignal {
    fn from_gui(signal: GuiProcessSignal) -> Self {
        match signal {
            GuiProcessSignal::Int => Self::Int,
            GuiProcessSignal::Term => Self::Term,
            GuiProcessSignal::Kill => Self::Kill,
        }
    }

    fn to_gui(self) -> GuiProcessSignal {
        match self {
            Self::Int => GuiProcessSignal::Int,
            Self::Term => GuiProcessSignal::Term,
            Self::Kill => GuiProcessSignal::Kill,
        }
    }

    fn name(self) -> &'static str {
        match self {
            Self::Int => "SIGINT",
            Self::Term => "SIGTERM",
            Self::Kill => "SIGKILL",
        }
    }

    fn raw(self) -> nix::libc::c_int {
        match self {
            Self::Int => nix::libc::SIGINT,
            Self::Term => nix::libc::SIGTERM,
            Self::Kill => nix::libc::SIGKILL,
        }
    }
}

/// Set while one `ScopedParentSignalRelay` owns the process-wide handlers.
///
/// An atomic flag (not a held `MutexGuard`) keeps `OwnedProcess: Send`, so a
/// stop can run on a worker thread instead of blocking the UI frame thread.
#[cfg(target_os = "linux")]
static PARENT_SIGNAL_RELAY_ACTIVE: AtomicBool = AtomicBool::new(false);

/// Releases `PARENT_SIGNAL_RELAY_ACTIVE` after the relay restored the
/// previous handlers (struct fields drop after `Drop::drop` runs).
#[cfg(target_os = "linux")]
struct ParentSignalRelayToken;

#[cfg(target_os = "linux")]
impl Drop for ParentSignalRelayToken {
    fn drop(&mut self) {
        PARENT_SIGNAL_RELAY_ACTIVE.store(false, Ordering::SeqCst);
    }
}

/// What a wait does when a parent SIGTERM/SIGINT is pending.
#[cfg(target_os = "linux")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ParentSignalWait {
    /// Report it as `Interrupted` so the caller starts cleanup.
    Interrupt,
    /// Cleanup is running: end the current grace period and escalate.
    Escalate,
    /// Nothing stronger remains to send (after SIGKILL): keep waiting.
    Continue,
}

#[cfg(target_os = "linux")]
static PENDING_PARENT_SIGNAL: AtomicI32 = AtomicI32::new(0);

#[cfg(target_os = "linux")]
extern "C" fn record_parent_signal(signal: nix::libc::c_int) {
    let _ = PENDING_PARENT_SIGNAL.compare_exchange(0, signal, Ordering::SeqCst, Ordering::SeqCst);
}

#[cfg(target_os = "linux")]
struct ScopedParentSignalRelay {
    previous_sigterm: nix::sys::signal::SigAction,
    previous_sigint: nix::sys::signal::SigAction,
    _token: ParentSignalRelayToken,
}

#[cfg(target_os = "linux")]
impl ScopedParentSignalRelay {
    fn install() -> Result<Self, String> {
        use nix::sys::signal::{
            pthread_sigmask, sigaction, SaFlags, SigAction, SigHandler, SigSet, SigmaskHow, Signal,
        };

        if PARENT_SIGNAL_RELAY_ACTIVE
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
            .is_err()
        {
            return Err("another GUI child already owns the process-wide signal relay".to_string());
        }
        let relay_token = ParentSignalRelayToken;
        let mut relay_mask = SigSet::empty();
        relay_mask.add(Signal::SIGTERM);
        relay_mask.add(Signal::SIGINT);
        let mut previous_mask = SigSet::empty();
        pthread_sigmask(
            SigmaskHow::SIG_BLOCK,
            Some(&relay_mask),
            Some(&mut previous_mask),
        )
        .map_err(|error| format!("failed to block parent signals during relay install: {error}"))?;

        PENDING_PARENT_SIGNAL.store(0, Ordering::SeqCst);
        let action = SigAction::new(
            SigHandler::Handler(record_parent_signal),
            SaFlags::SA_RESTART,
            relay_mask,
        );
        // SAFETY: the handler performs only an atomic store and both previous
        // actions are retained for scoped restoration below.
        let previous_sigterm = unsafe { sigaction(Signal::SIGTERM, &action) }.map_err(|error| {
            let _ = pthread_sigmask(SigmaskHow::SIG_SETMASK, Some(&previous_mask), None);
            format!("failed to install SIGTERM relay: {error}")
        })?;
        // SAFETY: same scoped handler and restoration contract as SIGTERM.
        let previous_sigint = match unsafe { sigaction(Signal::SIGINT, &action) } {
            Ok(previous) => previous,
            Err(error) => {
                // SAFETY: restore the action returned by the successful install.
                let _ = unsafe { sigaction(Signal::SIGTERM, &previous_sigterm) };
                let _ = pthread_sigmask(SigmaskHow::SIG_SETMASK, Some(&previous_mask), None);
                return Err(format!("failed to install SIGINT relay: {error}"));
            }
        };
        if let Err(error) = pthread_sigmask(SigmaskHow::SIG_SETMASK, Some(&previous_mask), None) {
            // SAFETY: both values came from successful sigaction calls above.
            let _ = unsafe { sigaction(Signal::SIGINT, &previous_sigint) };
            // SAFETY: both values came from successful sigaction calls above.
            let _ = unsafe { sigaction(Signal::SIGTERM, &previous_sigterm) };
            return Err(format!(
                "failed to restore signal mask after relay install: {error}"
            ));
        }
        Ok(Self {
            previous_sigterm,
            previous_sigint,
            _token: relay_token,
        })
    }

    fn has_signal(&self) -> bool {
        PENDING_PARENT_SIGNAL.load(Ordering::SeqCst) != 0
    }

    fn take_signal(&self) -> Option<GuiProcessSignal> {
        match PENDING_PARENT_SIGNAL.swap(0, Ordering::SeqCst) {
            nix::libc::SIGTERM => Some(GuiProcessSignal::Term),
            nix::libc::SIGINT => Some(GuiProcessSignal::Int),
            _ => None,
        }
    }
}

#[cfg(target_os = "linux")]
impl Drop for ScopedParentSignalRelay {
    fn drop(&mut self) {
        use nix::sys::signal::{pthread_sigmask, sigaction, SigSet, SigmaskHow, Signal};

        let mut relay_mask = SigSet::empty();
        relay_mask.add(Signal::SIGTERM);
        relay_mask.add(Signal::SIGINT);
        let mut previous_mask = SigSet::empty();
        let mask_blocked = match pthread_sigmask(
            SigmaskHow::SIG_BLOCK,
            Some(&relay_mask),
            Some(&mut previous_mask),
        ) {
            Ok(()) => true,
            Err(error) => {
                eprintln!("failed to block signals while restoring GUI relay: {error}");
                false
            }
        };
        // SAFETY: these are the exact actions retained at installation.
        if let Err(error) = unsafe { sigaction(Signal::SIGINT, &self.previous_sigint) } {
            eprintln!("failed to restore previous SIGINT handler: {error}");
        }
        // SAFETY: these are the exact actions retained at installation.
        if let Err(error) = unsafe { sigaction(Signal::SIGTERM, &self.previous_sigterm) } {
            eprintln!("failed to restore previous SIGTERM handler: {error}");
        }
        PENDING_PARENT_SIGNAL.store(0, Ordering::SeqCst);
        if mask_blocked {
            if let Err(error) = pthread_sigmask(SigmaskHow::SIG_SETMASK, Some(&previous_mask), None)
            {
                eprintln!("failed to restore signal mask after GUI relay: {error}");
            }
        }
    }
}

#[cfg(target_os = "linux")]
fn child_startup_gate(gate_fd: nix::libc::c_int) -> io::Result<()> {
    // SAFETY: every operation below is an async-signal-safe syscall between
    // fork and exec; the parent gate thread validates the reported identity.
    if unsafe { nix::libc::setsid() } == -1 {
        return Err(io::Error::last_os_error());
    }
    // SAFETY: scalar identity queries do not dereference caller memory.
    let identity = unsafe {
        [
            nix::libc::getpid(),
            nix::libc::getpgid(0),
            nix::libc::getsid(0),
        ]
    };
    if identity[0] <= 0 || identity[1] != identity[0] || identity[2] != identity[0] {
        return Err(io::Error::from_raw_os_error(nix::libc::EPERM));
    }
    // SAFETY: the fixed-size identity array remains valid for this write loop.
    let bytes = unsafe {
        std::slice::from_raw_parts(
            identity.as_ptr().cast::<u8>(),
            std::mem::size_of_val(&identity),
        )
    };
    let mut written = 0;
    while written < bytes.len() {
        // SAFETY: bytes points to initialized storage and gate_fd is inherited.
        let result = unsafe {
            nix::libc::write(
                gate_fd,
                bytes[written..].as_ptr().cast(),
                bytes.len() - written,
            )
        };
        if result > 0 {
            written += result as usize;
        } else if result == -1 && io::Error::last_os_error().kind() == io::ErrorKind::Interrupted {
            continue;
        } else {
            return Err(io::Error::last_os_error());
        }
    }
    let mut decision = STARTUP_GATE_ABORT;
    loop {
        // SAFETY: decision is one writable byte and gate_fd is inherited.
        let result = unsafe { nix::libc::read(gate_fd, (&mut decision as *mut u8).cast(), 1) };
        if result == 1 {
            break;
        }
        if result == -1 && io::Error::last_os_error().kind() == io::ErrorKind::Interrupted {
            continue;
        }
        return Err(if result == 0 {
            io::Error::from_raw_os_error(nix::libc::ECANCELED)
        } else {
            io::Error::last_os_error()
        });
    }
    if decision == STARTUP_GATE_GO {
        Ok(())
    } else {
        Err(io::Error::from_raw_os_error(nix::libc::ECANCELED))
    }
}

#[cfg(target_os = "linux")]
fn parent_startup_gate(
    mut gate: UnixStream,
    identity_probe: fn(i32) -> Result<LinuxLeaderIdentity, String>,
) -> Result<LinuxLeaderIdentity, String> {
    let mut bytes = [0_u8; std::mem::size_of::<[i32; 3]>()];
    let identity = gate
        .read_exact(&mut bytes)
        .map_err(|error| format!("failed to read child startup identity: {error}"))
        .and_then(|()| {
            let identity: [i32; 3] = std::array::from_fn(|index| {
                let start = index * std::mem::size_of::<i32>();
                let end = start + std::mem::size_of::<i32>();
                let mut fixed_bytes = [0_u8; std::mem::size_of::<i32>()];
                fixed_bytes.copy_from_slice(&bytes[start..end]);
                i32::from_ne_bytes(fixed_bytes)
            });
            let [pid, process_group_id, session_id] = identity;
            if pid <= 0 || process_group_id != pid || session_id != pid {
                return Err(format!(
                    "child startup identity is not a dedicated session: \
                     pid={pid} pgid={process_group_id} sid={session_id}"
                ));
            }
            let identity = identity_probe(pid)?;
            if identity.process_group_id != process_group_id || identity.session_id != session_id {
                return Err("child startup identity changed during binding".to_string());
            }
            Ok(identity)
        });
    let decision = if identity.is_ok() {
        STARTUP_GATE_GO
    } else {
        STARTUP_GATE_ABORT
    };
    if let Err(error) = gate.write_all(&[decision]) {
        return Err(match identity {
            Ok(_) => format!("failed to release identity-bound child: {error}"),
            Err(reason) => format!("{reason}; failed to abort gated child: {error}"),
        });
    }
    identity
}

#[cfg(target_os = "linux")]
fn spawn_linux_identity_gated(
    mut command: Command,
    identity_probe: fn(i32) -> Result<LinuxLeaderIdentity, String>,
) -> Result<(Child, LinuxLeaderIdentity), OwnedProcessSpawnError> {
    let (parent_gate, child_gate) = UnixStream::pair().map_err(OwnedProcessSpawnError::Io)?;
    parent_gate
        .set_read_timeout(Some(STARTUP_GATE_TIMEOUT))
        .map_err(OwnedProcessSpawnError::Io)?;
    parent_gate
        .set_write_timeout(Some(STARTUP_GATE_TIMEOUT))
        .map_err(OwnedProcessSpawnError::Io)?;
    child_gate
        .set_read_timeout(Some(STARTUP_GATE_TIMEOUT))
        .map_err(OwnedProcessSpawnError::Io)?;
    let child_gate_fd = child_gate.as_raw_fd();
    // SAFETY: child_startup_gate uses only async-signal-safe syscalls and the
    // descriptor remains open in both parent and child until spawn resolves.
    unsafe {
        command.pre_exec(move || child_startup_gate(child_gate_fd));
    }
    let gate_thread = std::thread::Builder::new()
        .name("srb-gui-startup-gate".to_string())
        .spawn(move || parent_startup_gate(parent_gate, identity_probe))
        .map_err(|error| OwnedProcessSpawnError::Supervision(error.to_string()))?;
    let child_result = command.spawn();
    drop(child_gate);
    let identity_result = gate_thread.join().map_err(|_| {
        OwnedProcessSpawnError::Supervision("child startup identity gate panicked".to_string())
    })?;
    match (child_result, identity_result) {
        (Ok(child), Ok(identity)) => Ok((child, identity)),
        (Ok(mut child), Err(reason)) => {
            let _ = child.kill();
            let _ = child.wait();
            Err(OwnedProcessSpawnError::Supervision(reason))
        }
        (Err(error), Err(_reason)) => Err(OwnedProcessSpawnError::Io(error)),
        (Err(error), Ok(_)) => Err(OwnedProcessSpawnError::Io(error)),
    }
}

#[cfg(target_os = "linux")]
const PR_SET_CHILD_SUBREAPER: nix::libc::c_int = 36;
#[cfg(target_os = "linux")]
const PR_GET_CHILD_SUBREAPER: nix::libc::c_int = 37;
#[cfg(target_os = "linux")]
static CHILD_SUBREAPER: OnceLock<Result<(), String>> = OnceLock::new();

/// Enable Linux child-subreaper behavior before launch.
///
/// This is intentionally process-wide. Orphaned GUI descendants are adopted
/// by this process, but cleanup only waits exact PIDs whose `/proc` identity is
/// proven to belong to the owned dedicated session or process group.
#[cfg(target_os = "linux")]
pub(crate) fn enable_linux_child_subreaper() -> Result<(), String> {
    CHILD_SUBREAPER
        .get_or_init(|| {
            // SAFETY: prctl receives scalar arguments and enables a documented
            // process-wide Linux attribute without dereferencing user memory.
            if unsafe { nix::libc::prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) } == -1 {
                return Err(io::Error::last_os_error().to_string());
            }
            let mut enabled: nix::libc::c_int = 0;
            // SAFETY: PR_GET_CHILD_SUBREAPER writes one c_int to the valid
            // pointer supplied below; the remaining variadic arguments are 0.
            if unsafe {
                nix::libc::prctl(
                    PR_GET_CHILD_SUBREAPER,
                    &mut enabled as *mut nix::libc::c_int,
                    0,
                    0,
                    0,
                )
            } == -1
            {
                return Err(io::Error::last_os_error().to_string());
            }
            if enabled != 1 {
                return Err("kernel did not retain child-subreaper state".to_string());
            }
            Ok(())
        })
        .clone()
}

#[cfg(target_os = "linux")]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct LinuxLeaderIdentity {
    pid: i32,
    process_group_id: i32,
    session_id: i32,
    start_time_ticks: u64,
}

#[cfg(target_os = "linux")]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct LinuxLeaderSnapshot {
    stat: LinuxProcessStat,
    getpgid_process_group_id: i32,
    getsid_session_id: i32,
}

#[cfg(target_os = "linux")]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct LinuxProcessStat {
    pid: i32,
    state: u8,
    parent_pid: i32,
    process_group_id: i32,
    session_id: i32,
    start_time_ticks: u64,
}

#[cfg(target_os = "linux")]
#[derive(Debug)]
struct LinuxSessionState {
    leader_exited: bool,
    group_empty: bool,
    session_empty: bool,
}

#[cfg(target_os = "linux")]
impl LinuxSessionState {
    fn from_members(
        identity: LinuxLeaderIdentity,
        leader_exited: bool,
        members: &[LinuxProcessStat],
    ) -> Self {
        Self {
            leader_exited,
            group_empty: leader_exited
                && !members
                    .iter()
                    .any(|member| member.process_group_id == identity.process_group_id),
            session_empty: leader_exited
                && !members
                    .iter()
                    .any(|member| member.session_id == identity.session_id),
        }
    }

    fn active(&self) -> bool {
        !self.leader_exited || !self.group_empty || !self.session_empty
    }
}

#[cfg(target_os = "linux")]
fn capture_linux_leader_identity(pid: i32) -> Result<LinuxLeaderIdentity, String> {
    let snapshot = probe_linux_leader(pid).map_err(|error| error.to_string())?;
    if snapshot.stat.pid != pid
        || snapshot.stat.process_group_id != pid
        || snapshot.stat.session_id != pid
        || snapshot.getpgid_process_group_id != pid
        || snapshot.getsid_session_id != pid
    {
        return Err(format!(
            "process {pid} is not a dedicated session leader \
             (stat_pgid={}, stat_sid={}, getpgid={}, getsid={})",
            snapshot.stat.process_group_id,
            snapshot.stat.session_id,
            snapshot.getpgid_process_group_id,
            snapshot.getsid_session_id,
        ));
    }
    Ok(LinuxLeaderIdentity {
        pid,
        process_group_id: pid,
        session_id: pid,
        start_time_ticks: snapshot.stat.start_time_ticks,
    })
}

#[cfg(target_os = "linux")]
fn probe_linux_leader(pid: i32) -> io::Result<LinuxLeaderSnapshot> {
    let stat = read_linux_process_stat(pid)?;
    // SAFETY: getpgid/getsid only read kernel metadata for a numeric PID.
    let process_group_id = unsafe { nix::libc::getpgid(pid) };
    if process_group_id == -1 {
        return Err(io::Error::last_os_error());
    }
    // SAFETY: getsid only reads kernel metadata for a numeric PID.
    let session_id = unsafe { nix::libc::getsid(pid) };
    if session_id == -1 {
        return Err(io::Error::last_os_error());
    }
    Ok(LinuxLeaderSnapshot {
        stat,
        getpgid_process_group_id: process_group_id,
        getsid_session_id: session_id,
    })
}

#[cfg(target_os = "linux")]
fn validate_linux_leader_identity(
    expected: LinuxLeaderIdentity,
    current: LinuxLeaderSnapshot,
) -> io::Result<()> {
    if current.stat.pid != expected.pid
        || current.stat.start_time_ticks != expected.start_time_ticks
        || current.stat.process_group_id != expected.process_group_id
        || current.stat.session_id != expected.session_id
        || current.getpgid_process_group_id != expected.process_group_id
        || current.getsid_session_id != expected.session_id
    {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!(
                "owned session leader identity changed; refusing to signal \
                 (expected pid={} pgid={} sid={} start={}, current pid={} \
                 stat_pgid={} stat_sid={} getpgid={} getsid={} start={})",
                expected.pid,
                expected.process_group_id,
                expected.session_id,
                expected.start_time_ticks,
                current.stat.pid,
                current.stat.process_group_id,
                current.stat.session_id,
                current.getpgid_process_group_id,
                current.getsid_session_id,
                current.stat.start_time_ticks,
            ),
        ));
    }
    Ok(())
}

#[cfg(target_os = "linux")]
fn signal_linux_owned_members(
    identity: LinuxLeaderIdentity,
    signal: GroupSignal,
) -> LinuxSignalAttempt {
    let leader = match probe_linux_leader(identity.pid) {
        Ok(leader) => leader,
        Err(error) => {
            return LinuxSignalAttempt {
                target_signaled: false,
                error: Some(error),
            };
        }
    };
    if let Err(error) = validate_linux_leader_identity(identity, leader) {
        return LinuxSignalAttempt {
            target_signaled: false,
            error: Some(error),
        };
    }
    let mut members = match scan_linux_owned_members(identity) {
        Ok(members) => members,
        Err(error) => {
            return LinuxSignalAttempt {
                target_signaled: false,
                error: Some(error),
            };
        }
    };
    members.push(leader.stat);
    members.sort_unstable_by_key(|member| member.pid);
    members.dedup_by_key(|member| member.pid);

    let mut target_signaled = false;
    for member in members {
        if member.state == b'Z' {
            continue;
        }
        match send_bound_linux_process_signal(identity, member, signal) {
            Ok(sent) => target_signaled |= sent,
            Err(error) => {
                return LinuxSignalAttempt {
                    target_signaled,
                    error: Some(error),
                };
            }
        }
    }
    LinuxSignalAttempt {
        target_signaled,
        error: None,
    }
}

#[cfg(target_os = "linux")]
fn send_bound_linux_process_signal(
    identity: LinuxLeaderIdentity,
    expected: LinuxProcessStat,
    signal: GroupSignal,
) -> io::Result<bool> {
    if expected.pid <= 0
        || (expected.process_group_id != identity.process_group_id
            && expected.session_id != identity.session_id)
    {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("process {} is outside the owned session", expected.pid),
        ));
    }
    // SAFETY: pidfd_open takes only a numeric PID and flags. The returned file
    // descriptor pins that exact kernel process across the validation below.
    let raw_pidfd = unsafe {
        nix::libc::syscall(nix::libc::SYS_pidfd_open, expected.pid, 0) as nix::libc::c_int
    };
    if raw_pidfd == -1 {
        let error = io::Error::last_os_error();
        return if error.kind() == io::ErrorKind::NotFound {
            Ok(false)
        } else {
            Err(error)
        };
    }
    // SAFETY: raw_pidfd is a newly returned owned descriptor.
    let pidfd = unsafe { OwnedFd::from_raw_fd(raw_pidfd) };
    let current = match read_linux_process_stat(expected.pid) {
        Ok(current) => current,
        Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(false),
        Err(error) => return Err(error),
    };
    if current.pid != expected.pid
        || current.start_time_ticks != expected.start_time_ticks
        || current.process_group_id != expected.process_group_id
        || current.session_id != expected.session_id
        || (current.process_group_id != identity.process_group_id
            && current.session_id != identity.session_id)
    {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!(
                "owned process {} identity changed before pidfd signal",
                expected.pid
            ),
        ));
    }
    if current.state == b'Z' {
        return Ok(false);
    }
    // SAFETY: pidfd pins the revalidated process; a null siginfo requests the
    // normal signal semantics and flags must be zero for this syscall.
    let result = unsafe {
        nix::libc::syscall(
            nix::libc::SYS_pidfd_send_signal,
            pidfd.as_raw_fd(),
            signal.raw(),
            std::ptr::null::<nix::libc::siginfo_t>(),
            0,
        )
    };
    if result == 0 {
        Ok(true)
    } else {
        let error = io::Error::last_os_error();
        if error.kind() == io::ErrorKind::NotFound {
            Ok(false)
        } else {
            Err(error)
        }
    }
}

#[cfg(target_os = "linux")]
fn linux_leader_exited_wnowait(pid: i32) -> io::Result<bool> {
    let mut info = MaybeUninit::<nix::libc::siginfo_t>::zeroed();
    // SAFETY: info is valid writable storage. WNOHANG bounds the call and
    // WNOWAIT deliberately keeps the leader waitable as the session anchor.
    let result = unsafe {
        nix::libc::waitid(
            nix::libc::P_PID,
            pid as nix::libc::id_t,
            info.as_mut_ptr(),
            nix::libc::WEXITED | nix::libc::WNOHANG | nix::libc::WNOWAIT,
        )
    };
    if result == -1 {
        return Err(io::Error::last_os_error());
    }
    // SAFETY: successful waitid initializes siginfo_t; si_pid is zero when no
    // exited child was observed.
    let observed_pid = unsafe { info.assume_init().si_pid() };
    match observed_pid {
        0 => Ok(false),
        observed if observed == pid => Ok(true),
        observed => Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("waitid returned child {observed} for owned leader {pid}"),
        )),
    }
}

#[cfg(target_os = "linux")]
fn scan_linux_owned_members(identity: LinuxLeaderIdentity) -> io::Result<Vec<LinuxProcessStat>> {
    let mut members = Vec::new();
    for entry in fs::read_dir("/proc")? {
        let entry = entry?;
        let Some(pid) = entry
            .file_name()
            .to_str()
            .and_then(|name| name.parse::<i32>().ok())
        else {
            continue;
        };
        if pid == identity.pid {
            continue;
        }
        match read_linux_process_stat(pid) {
            Ok(stat)
                if stat.process_group_id == identity.process_group_id
                    || stat.session_id == identity.session_id =>
            {
                members.push(stat);
            }
            Ok(_) => {}
            Err(error)
                if error.kind() == io::ErrorKind::NotFound
                    || error.raw_os_error() == Some(nix::libc::ESRCH) => {}
            Err(error) => return Err(error),
        }
    }
    Ok(members)
}

#[cfg(target_os = "linux")]
fn reap_linux_owned_descendant(
    identity: LinuxLeaderIdentity,
    expected: LinuxProcessStat,
) -> io::Result<bool> {
    let current = read_linux_process_stat(expected.pid)?;
    if current.start_time_ticks != expected.start_time_ticks
        || current.parent_pid != std::process::id() as i32
        || (current.process_group_id != identity.process_group_id
            && current.session_id != identity.session_id)
    {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!(
                "owned descendant {} identity changed before reap",
                expected.pid
            ),
        ));
    }
    let mut status = 0;
    // SAFETY: waitpid is scoped to one exact zombie PID whose start token,
    // parent, group, and session were revalidated immediately above.
    let waited = unsafe { nix::libc::waitpid(expected.pid, &mut status, nix::libc::WNOHANG) };
    match waited {
        pid if pid == expected.pid => Ok(true),
        0 => Ok(false),
        -1 => Err(io::Error::last_os_error()),
        other => Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("waitpid returned unexpected descendant {other}"),
        )),
    }
}

#[cfg(target_os = "linux")]
fn read_linux_process_stat(pid: i32) -> io::Result<LinuxProcessStat> {
    let contents = fs::read(format!("/proc/{pid}/stat"))?;
    parse_linux_process_stat(&contents)
}

#[cfg(target_os = "linux")]
fn parse_linux_process_stat(contents: &[u8]) -> io::Result<LinuxProcessStat> {
    let command_start = contents
        .iter()
        .position(|&byte| byte == b'(')
        .ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "missing /proc stat command start",
            )
        })?;
    let command_end = contents
        .iter()
        .rposition(|&byte| byte == b')')
        .ok_or_else(|| {
            io::Error::new(io::ErrorKind::InvalidData, "missing /proc stat command end")
        })?;
    if command_end <= command_start {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "invalid /proc stat command field",
        ));
    }
    let pid = std::str::from_utf8(&contents[..command_start])
        .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?
        .trim()
        .parse::<i32>()
        .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
    let fields: Vec<_> = contents[command_end + 1..]
        .split(|byte| byte.is_ascii_whitespace())
        .filter(|field| !field.is_empty())
        .collect();
    let parse_field = |index: usize, name: &str| -> io::Result<i32> {
        let field = fields
            .get(index)
            .copied()
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, format!("missing {name}")))?;
        std::str::from_utf8(field)
            .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?
            .parse::<i32>()
            .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))
    };
    let state = fields
        .first()
        .and_then(|field| field.first())
        .copied()
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "missing process state"))?;
    let start_time = fields
        .get(19)
        .copied()
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "missing process start time"))?;
    let start_time_ticks = std::str::from_utf8(start_time)
        .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?
        .parse::<u64>()
        .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
    Ok(LinuxProcessStat {
        pid,
        state,
        parent_pid: parse_field(1, "parent pid")?,
        process_group_id: parse_field(2, "process group")?,
        session_id: parse_field(3, "session id")?,
        start_time_ticks,
    })
}