aither-sandbox 0.28.0

Terminal-first command execution with heel sandboxing
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
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
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
//! A command's process tree: killing it, and finding what outlives the
//! command's own shell.
//!
//! A job's lifetime is its whole tree, not its top-level shell. A `&` child
//! outlives the shell that started it, and a `setsid` child also leaves the
//! shell's process group, so no group signal reaches it. [`JobTree`] finds the
//! descendants of one job from a snapshot of the process table:
//!
//! - every process in the job's process group, and every process whose
//!   ancestry leads to one of the found processes or to the job's root;
//! - every process of the same user whose environment carries the job's
//!   marker, [`JobMarker`] — the link that survives `setsid` and the
//!   reparenting that follows when the shell exits;
//! - on macOS, every process whose parent's unique id (`p_puniqueid`) is that
//!   of a process the job has seen, the root included, even one that has
//!   exited since. The kernel records that id when the process is forked and
//!   resets it to `launchd`'s (1) at the first `exec` the process makes after
//!   it was reparented, so the link holds only until that `exec`.
//!
//! The job's task therefore watches the root from the moment it is spawned
//! (kqueue `NOTE_FORK | NOTE_EXEC | NOTE_EXIT` on macOS) and scans on every
//! event, so children enter the member set while their lineage is readable,
//! and it keeps the unique ids of exited members for lineage.
//!
//! A process is identified by more than its pid, so a reused pid is never
//! mistaken for a member: Linux pairs the pid with the start time in
//! `/proc/<pid>/stat`, macOS uses the 64-bit unique id (see
//! `platform::Spi` for the SPI it comes from). The identity is checked again
//! right before every signal.
//!
//! The job's own task owns its member set; [`JobTree`] is the handle that
//! sends it messages. The job's root is kept unreaped until the job ends
//! ([`wait_for_exit`]), so its pid and process group stay reserved. A kill
//! freezes the members with `SIGSTOP` until a pass finds nothing new, then
//! sends `SIGKILL`, and a tracked member that survives is an error.
//!
//! What cannot be found. This paragraph covers every job until sandboxed jobs
//! are attributed by the sandbox itself (lexoliu/heel#134); after that it
//! describes unsafe jobs only, whose membership stays an approximation.
//! A descendant is found by a scan that runs while it is still linkable: it
//! is in the job's process group, its parent is the root or a process already
//! seen, or its `p_puniqueid` is in the lineage. It escapes when it has left
//! the group and every link it has leads only to processes that no scan saw
//! alive, and it has no readable marker: it cleared it from its environment
//! (`env -i`, a daemon that scrubs its environment), or its environment is
//! unreadable (Apple's own binaries on macOS, another user's processes). Such
//! a link leads nowhere when the process it names lived for less than one
//! scan latency, or when an `exec` after a reparent reset it. A scan follows
//! an event within about 5 ms on the development Mac (event delivery, the
//! blocking-pool hand-off and a 1-3 ms lineage-only read), so a process that
//! sets its session up faster than that after its fork, or whose every
//! intermediate parent exits faster, can escape. The irreducible case is the
//! double fork whose intermediates live for microseconds: no scan rate sees
//! them. Such a process is invisible to the job; it is not reported as
//! covered.
//! Windows has no such tracking: `taskkill /T` ends the tree, and nothing
//! finds a descendant that outlives its shell.
//!
//! Linux membership is exact only once heel provides a per-job subreaper
//! (lexoliu/heel#129); until then Linux uses the marker and the ancestry scan.

#[cfg(unix)]
use std::time::Duration;

/// The environment variable that marks every process of a job; its value is
/// the job's [`JobMarker`].
pub const JOB_MARKER_VAR: &str = "AITHER_JOB";

/// How often a job whose shell has exited is checked for remaining
/// descendants where the platform cannot signal exits, and how long a shared
/// process-table scan may be reused meanwhile.
#[cfg(target_os = "linux")]
const DESCENDANT_POLL: Duration = Duration::from_millis(250);

/// How many times a kill re-reads the process table to freeze a process that
/// forked while the others were being stopped.
#[cfg(unix)]
const FREEZE_ROUNDS: u32 = 50;

/// How many times a kill re-reads the process table to catch a process that
/// forked while it was being killed.
#[cfg(unix)]
const KILL_ROUNDS: u32 = 50;

/// The pause between two such reads.
#[cfg(unix)]
const KILL_ROUND_PAUSE: Duration = Duration::from_millis(10);

/// Why a process tree could not be killed.
#[derive(Debug, thiserror::Error)]
pub enum KillTreeError {
    /// The signal to the process group failed.
    #[cfg(unix)]
    #[error("failed to signal the process group: {0}")]
    Signal(#[source] std::io::Error),
    /// The descendants of the job could not be found or killed.
    #[error(transparent)]
    Descendants(#[from] ProcessTableError),
    /// The job's process tree had already ended, so nothing was killed.
    #[cfg(unix)]
    #[error("the job's process tree had already ended")]
    Ended,
    /// `taskkill` could not be started.
    #[cfg(windows)]
    #[error("failed to run taskkill: {0}")]
    Taskkill(#[source] std::io::Error),
    /// `taskkill` ran and reported failure.
    #[cfg(windows)]
    #[error("taskkill reported failure ({status}): {stderr}")]
    TaskkillFailed {
        /// The exit status `taskkill` returned.
        status: std::process::ExitStatus,
        /// What `taskkill` wrote to its standard error.
        stderr: String,
    },
    /// The job's supervisor could not end its tree; its report names any
    /// process that survived.
    #[cfg(target_os = "linux")]
    #[error("the job's supervisor could not end its tree: {0}")]
    Supervised(#[source] heel::supervisor::TreeKillError),
}

/// Why a job's descendants could not be found or killed.
#[derive(Debug, thiserror::Error)]
pub enum ProcessTableError {
    /// The platform's process table could not be read.
    #[error("cannot read the process table: {0}")]
    Table(#[source] std::io::Error),
    /// A process of the job could not be signalled, for example for lack of
    /// permission.
    #[error("cannot signal {} (pid {}): {source}", process.name, process.pid)]
    Signal {
        /// The process the signal was for.
        process: JobProcess,
        /// What `kill(2)` reported.
        #[source]
        source: std::io::Error,
    },
    /// Processes of the job were still alive after repeated `SIGKILL`.
    #[error("processes of the job survived SIGKILL: {}", names(.0))]
    Survived(Vec<JobProcess>),
    /// The task that owns the job's processes ended before it answered.
    #[error("the task tracking the job's processes ended")]
    TaskEnded,
}

fn names(processes: &[JobProcess]) -> String {
    processes
        .iter()
        .map(|process| format!("{} ({})", process.name, process.pid))
        .collect::<Vec<_>>()
        .join(", ")
}

/// A process of a job other than its top-level shell.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JobProcess {
    /// The process id.
    pub pid: u32,
    /// The command name the platform reports for the process.
    pub name: String,
}

/// The value of [`JOB_MARKER_VAR`] in every process of one job: a fresh id,
/// unrelated to the job's display name, so no other job or host shares it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JobMarker {
    value: String,
}

impl JobMarker {
    /// A marker no other job has.
    #[must_use]
    pub fn new() -> Self {
        Self {
            value: uuid::Uuid::new_v4().to_string(),
        }
    }

    /// The environment entry every process of the job must be started with.
    #[must_use]
    pub fn entry(&self) -> (String, String) {
        (JOB_MARKER_VAR.to_string(), self.value.clone())
    }
}

impl Default for JobMarker {
    fn default() -> Self {
        Self::new()
    }
}

/// Kills the process `pid` and every process in its process group.
///
/// On Unix `pid` must lead its own process group (spawned with
/// `process_group(0)`), and the group receives `SIGKILL`; descendants that
/// left the group are [`JobTree`]'s business. On Windows `taskkill /F /T` ends
/// the tree.
///
/// # Errors
///
/// Returns [`KillTreeError`] when the kill could not be delivered.
pub async fn kill_process_tree(pid: u32) -> Result<(), KillTreeError> {
    #[cfg(unix)]
    {
        kill_group(pid)
    }

    #[cfg(windows)]
    {
        let output = async_process::Command::new("taskkill")
            .args(["/F", "/T", "/PID", &pid.to_string()])
            .output()
            .await
            .map_err(KillTreeError::Taskkill)?;
        if output.status.success() {
            Ok(())
        } else {
            Err(KillTreeError::TaskkillFailed {
                status: output.status,
                stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
            })
        }
    }
}

/// The processes of each job rooted at `roots`, the root included, from one
/// read of the process table: those of the root's process group and
/// everything descending from them. A root that has exited yields no pids.
///
/// # Errors
///
/// Returns the error of reading the process table.
#[cfg(unix)]
pub fn tree_pids(roots: &[u32]) -> std::io::Result<Vec<Vec<u32>>> {
    let (mode, _) = platform::identity_mode();
    let table = platform::snapshot(mode, false)?;
    let host = std::process::id();
    let none = std::collections::HashSet::new();
    let no_lineage = std::collections::HashSet::new();
    Ok(roots
        .iter()
        .map(|&root| {
            let alive = table.procs.iter().any(|process| process.pid() == root);
            let members = unix::members(&table.procs, root, host, "", &none, &no_lineage);
            alive
                .then_some(root)
                .into_iter()
                .chain(members.into_iter().map(unix::Proc::pid))
                .collect()
        })
        .collect())
}

/// Sends `SIGKILL` to the process group `pid` leads.
#[cfg(unix)]
fn kill_group(pid: u32) -> Result<(), KillTreeError> {
    // SAFETY: `kill` takes plain integers and touches no memory.
    let result = unsafe { libc::kill(-pid.cast_signed(), libc::SIGKILL) };
    if result == 0 {
        Ok(())
    } else {
        Err(KillTreeError::Signal(std::io::Error::last_os_error()))
    }
}

/// Returns once the process `pid`, a child of this process, has exited, and
/// leaves it unreaped: it stays a zombie, so its pid and process group stay
/// reserved until the caller waits for it.
///
/// The exit is signalled by the kernel (a kqueue `NOTE_EXIT` on macOS, a
/// pidfd on Linux, 5.3 or later), so a long-lived root holds no thread.
///
/// # Errors
///
/// Returns the OS error when `pid` is not a child of this process.
#[cfg(unix)]
pub async fn wait_for_exit(pid: u32) -> std::io::Result<()> {
    // Registered before the check, so an exit between them is not missed.
    let watch = platform::ExitWatch::new(pid)?;
    if exited_unreaped(pid)? {
        return Ok(());
    }
    watch.exited().await
}

/// Whether the child `pid` has exited, without reaping it.
#[cfg(unix)]
fn exited_unreaped(pid: u32) -> std::io::Result<bool> {
    wait_unreaped(pid, libc::WNOHANG)
}

/// Blocks until the child `pid` has exited, without reaping it.
///
/// Called only once the kernel reported the child exiting, so it returns
/// within the kernel's own exit teardown.
#[cfg(target_os = "macos")]
fn block_until_exited_unreaped(pid: u32) -> std::io::Result<()> {
    wait_unreaped(pid, 0).map(drop)
}

/// `waitid` for the exit of the child `pid` with `WNOWAIT` and `flags`;
/// whether it reported the exit.
#[cfg(unix)]
fn wait_unreaped(pid: u32, flags: libc::c_int) -> std::io::Result<bool> {
    loop {
        // SAFETY: `siginfo_t` is plain data for which all-zero bytes are valid.
        let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
        // SAFETY: `info` is live memory of the type `waitid` writes; `WNOWAIT`
        // leaves the child waitable.
        let result = unsafe {
            libc::waitid(
                libc::P_PID,
                pid,
                &raw mut info,
                libc::WEXITED | libc::WNOWAIT | flags,
            )
        };
        if result == 0 {
            // `waitid` leaves `si_pid` zero when no child has exited yet.
            #[cfg(target_os = "linux")]
            // SAFETY: `waitid` filled `info`.
            let reported = unsafe { info.si_pid() };
            #[cfg(not(target_os = "linux"))]
            let reported = info.si_pid;
            return Ok(reported != 0);
        }
        let error = std::io::Error::last_os_error();
        if error.kind() != std::io::ErrorKind::Interrupted {
            return Err(error);
        }
    }
}

/// Every scan of the process table may share one cache where reading is dear
/// (Linux), so jobs checking in the same tick read the table once between
/// them. A read never holds the cache's lock.
///
/// One scanner belongs to one registry, and clones share it.
///
/// On Linux the supervisor owns the trees it watches, so it is kept for its
/// tests and the other Unixes.
#[cfg_attr(target_os = "linux", allow(dead_code))]
#[derive(Debug, Clone, Default)]
pub struct ProcessScanner {
    #[cfg(target_os = "linux")]
    cache: std::sync::Arc<std::sync::Mutex<Option<CachedTable>>>,
}

/// A process table and when it was read.
#[cfg(target_os = "linux")]
type CachedTable = (std::time::Instant, std::sync::Arc<unix::Table>);

#[cfg(unix)]
impl ProcessScanner {
    /// The process table as it was at most `max_age` ago; a zero age always
    /// reads it afresh. `markers` asks for the environment markers, which
    /// are most of a read's cost; a table without them still links processes
    /// by group, ancestry and lineage.
    fn table(
        &self,
        max_age: Duration,
        mode: platform::IdentityMode,
        markers: bool,
    ) -> std::io::Result<std::sync::Arc<unix::Table>> {
        self.table_with(max_age, || platform::snapshot(mode, markers))
    }

    /// [`Self::table`] with the read `read` performs. The cache is checked
    /// and filled under its lock, and `read` runs outside it, so concurrent
    /// reads proceed together.
    #[cfg_attr(not(target_os = "linux"), allow(clippy::unused_self))]
    fn table_with(
        &self,
        max_age: Duration,
        read: impl FnOnce() -> std::io::Result<unix::Table>,
    ) -> std::io::Result<std::sync::Arc<unix::Table>> {
        #[cfg(target_os = "linux")]
        if !max_age.is_zero()
            && let Some((at, table)) = self
                .cache
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .as_ref()
            && at.elapsed() < max_age
        {
            return Ok(std::sync::Arc::clone(table));
        }
        #[cfg(not(target_os = "linux"))]
        let _ = max_age;
        let table = std::sync::Arc::new(read()?);
        #[cfg(target_os = "linux")]
        {
            *self
                .cache
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner) =
                Some((std::time::Instant::now(), std::sync::Arc::clone(&table)));
        }
        Ok(table)
    }
}

/// The process tree of one local job: its root shell and everything that
/// descends from it, wherever the descendants have gone.
///
/// On Linux the tree is owned by the job's supervisor
/// ([`TrackedJob::Supervised`]), which is a child subreaper inside the
/// sandbox, so a descendant that empties its environment, double-forks or
/// leaves its session is still attributed and still dies with the job.
/// Everywhere else it is the job's own task watching the process table.
#[derive(Debug, Clone)]
pub struct JobTree {
    #[cfg(windows)]
    root: u32,
    #[cfg(unix)]
    tracked: TrackedJob,
}

/// How a Unix job's tree is owned.
#[cfg(unix)]
#[derive(Debug, Clone)]
enum TrackedJob {
    /// A handle to the job's own task, which owns the set of processes found
    /// so far. Cloning keeps a handle to the same task.
    ///
    /// Only the other Unixes and the tests use it: on Linux the supervisor
    /// owns the tree.
    #[cfg_attr(target_os = "linux", allow(dead_code))]
    Watched(async_channel::Sender<unix::Command>),
    /// A handle to the job's `heel supervise` supervisor: the tree is ended
    /// through it, not by signalling the process group.
    #[cfg(target_os = "linux")]
    Supervised(heel::supervisor::TreeControl),
}

#[cfg(unix)]
#[cfg_attr(target_os = "linux", allow(dead_code))]
impl JobTree {
    /// Starts the task of the job whose root shell is `root`, a process group
    /// leader the caller keeps unreaped until [`JobTree::finish`], and whose
    /// processes carry `marker` in [`JOB_MARKER_VAR`].
    ///
    /// Call it right after the root was spawned: it reads the root's unique
    /// id and starts watching the root before returning, so the children the
    /// root forks are seen while their lineage can still be read.
    pub fn spawn<E: executor_core::Executor>(
        executor: &E,
        scanner: ProcessScanner,
        root: u32,
        marker: &JobMarker,
    ) -> Self {
        let (commands, inbox) = async_channel::unbounded();
        let task = unix::Task::new(root, marker.value.clone(), scanner);
        executor_core::Task::detach(executor.spawn(task.run(inbox)));
        Self {
            tracked: TrackedJob::Watched(commands),
        }
    }

    /// The tree of a job whose command runs under `heel supervise`.
    ///
    /// The supervisor already owns membership — every orphan of the job
    /// reparents to it — so the job asks it to end the tree and trusts its
    /// report rather than scanning the process table.
    #[cfg(target_os = "linux")]
    #[must_use]
    pub const fn supervised(control: heel::supervisor::TreeControl) -> Self {
        Self {
            tracked: TrackedJob::Supervised(control),
        }
    }

    /// Sends `command` to the job's task and returns its answer; `None` when
    /// the task has ended, which it does only once the job did.
    async fn ask_at<T>(
        &self,
        commands: &async_channel::Sender<unix::Command>,
        command: impl FnOnce(async_channel::Sender<T>) -> unix::Command,
    ) -> Result<Option<T>, ProcessTableError> {
        let (reply, answer) = async_channel::bounded(1);
        if commands.send(command(reply)).await.is_err() {
            return Ok(None);
        }
        answer
            .recv()
            .await
            .map(Some)
            .map_err(|_| ProcessTableError::TaskEnded)
    }

    /// Kills the whole job: its process group, then every descendant that is
    /// still alive.
    ///
    /// # Errors
    ///
    /// Returns [`KillTreeError`] when a process cannot be signalled, when the
    /// process table cannot be read, when a process the job tracks is still
    /// alive after repeated kills, or when the tree had already ended
    /// ([`KillTreeError::Ended`]): nothing was killed then.
    pub async fn kill(&self) -> Result<(), KillTreeError> {
        match &self.tracked {
            TrackedJob::Watched(commands) => {
                self.ask_at(commands, |reply| unix::Command::Kill { group: true, reply })
                    .await?
                    .ok_or(KillTreeError::Ended)??;
            }
            #[cfg(target_os = "linux")]
            TrackedJob::Supervised(control) => {
                control.kill().await.map_err(KillTreeError::Supervised)?;
            }
        }
        Ok(())
    }

    /// Kills every descendant of the job that is alive and returns what it
    /// killed; empty when none was.
    ///
    /// # Errors
    ///
    /// Returns [`ProcessTableError`] when the process table cannot be read, a
    /// process cannot be signalled, or a tracked descendant survives repeated
    /// `SIGKILL`.
    pub async fn kill_descendants(&self) -> Result<Vec<JobProcess>, ProcessTableError> {
        match &self.tracked {
            TrackedJob::Watched(commands) => Ok(self
                .ask_at(commands, |reply| unix::Command::Kill {
                    group: false,
                    reply,
                })
                .await?
                .transpose()?
                .unwrap_or_default()),
            // A supervised job's supervisor has already torn its tree down
            // by the time descendants are asked for; what it killed arrives
            // in the wait's report.
            #[cfg(target_os = "linux")]
            TrackedJob::Supervised(_) => Ok(Vec::new()),
        }
    }

    /// Returns once no descendant of the job is alive.
    ///
    /// Ends only when the tree is empty, so the caller bounds it with the
    /// job's own timeout and kill.
    ///
    /// # Errors
    ///
    /// Returns [`ProcessTableError`] when the process table cannot be read.
    pub async fn wait_descendants_exit(&self) -> Result<(), ProcessTableError> {
        match &self.tracked {
            TrackedJob::Watched(commands) => {
                self.ask_at(commands, unix::Command::Wait)
                    .await?
                    .transpose()?;
            }
            // A supervised background job's supervisor exits only once the
            // last descendant has, which the caller's own wait on the child
            // already covers; a supervised foreground job's teardown is
            // reported by that same wait.
            #[cfg(target_os = "linux")]
            TrackedJob::Supervised(_) => {}
        }
        Ok(())
    }

    /// Ends the job's task. Called before the root is reaped: afterwards the
    /// root's pid may belong to any process, so every later kill does nothing.
    pub async fn finish(&self) {
        match &self.tracked {
            TrackedJob::Watched(commands) => {
                let _ = self.ask_at(commands, unix::Command::Finish).await;
            }
            #[cfg(target_os = "linux")]
            TrackedJob::Supervised(_) => {}
        }
    }
}

#[cfg(windows)]
impl JobTree {
    /// The tree of the job whose root shell is `root`.
    pub const fn spawn<E: executor_core::Executor>(
        _executor: &E,
        _scanner: ProcessScanner,
        root: u32,
        _marker: &JobMarker,
    ) -> Self {
        Self { root }
    }

    /// Kills the root and its tree with `taskkill /T`.
    ///
    /// # Errors
    ///
    /// Returns [`KillTreeError`] when `taskkill` fails.
    pub async fn kill(&self) -> Result<(), KillTreeError> {
        kill_process_tree(self.root).await
    }

    /// Windows finds no descendants beyond `taskkill /T`.
    ///
    /// # Errors
    ///
    /// Never fails.
    #[allow(clippy::unused_async)]
    pub async fn kill_descendants(&self) -> Result<Vec<JobProcess>, ProcessTableError> {
        Ok(Vec::new())
    }

    /// Windows finds no descendants beyond `taskkill /T`.
    ///
    /// # Errors
    ///
    /// Never fails.
    #[allow(clippy::unused_async)]
    pub async fn wait_descendants_exit(&self) -> Result<(), ProcessTableError> {
        Ok(())
    }

    /// Nothing to end.
    #[allow(clippy::unused_async)]
    pub async fn finish(&self) {}
}

/// The watched-tree machinery a Linux build keeps for the tests and the
/// other Unixes only: the supervisor owns the trees there.
#[cfg(unix)]
#[cfg_attr(target_os = "linux", allow(dead_code))]
mod unix {
    use std::{collections::HashSet, io, time::Duration};

    use async_channel::{Receiver, Sender};

    use super::{
        FREEZE_ROUNDS, JobProcess, KILL_ROUND_PAUSE, KILL_ROUNDS, ProcessScanner,
        ProcessTableError, platform,
    };

    /// What identifies one process for as long as the kernel runs: the pid
    /// alone is reused.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub(super) struct Identity {
        pub(super) pid: u32,
        pub(super) birth: Birth,
    }

    /// What distinguishes the process that has a pid now from any other that
    /// had it before.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub(super) enum Birth {
        /// The start time in clock ticks since boot, `/proc/<pid>/stat`
        /// field 22 (Linux).
        #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
        Ticks(u64),
        /// The kernel's 64-bit unique process id, never reused (macOS).
        #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
        Unique(u64),
        /// The start time `proc_bsdinfo` reports (macOS fallback).
        #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
        Started { seconds: u64, micros: u64 },
    }

    impl Birth {
        /// The unique id, when that is what identifies the process.
        pub(super) const fn unique(self) -> Option<u64> {
            match self {
                Self::Unique(id) => Some(id),
                Self::Ticks(_) | Self::Started { .. } => None,
            }
        }
    }

    /// One row of the process table.
    #[derive(Debug, Clone)]
    pub(super) struct Proc {
        pub(super) identity: Identity,
        pub(super) ppid: u32,
        /// The parent's unique id as it was when the process was forked
        /// (macOS). The kernel resets it to `launchd`'s at the first `exec`
        /// after the process was reparented.
        pub(super) parent_unique: Option<u64>,
        pub(super) pgid: u32,
        pub(super) zombie: bool,
        pub(super) name: String,
        /// The value of the job marker in the environment, read for live
        /// processes of this user only.
        pub(super) marker: Option<String>,
    }

    impl Proc {
        pub(super) const fn pid(&self) -> u32 {
            self.identity.pid
        }

        const fn unique(&self) -> Option<u64> {
            self.identity.birth.unique()
        }
    }

    /// The process table at one moment.
    #[derive(Debug)]
    pub(super) struct Table {
        pub(super) procs: Vec<Proc>,
    }

    /// A process of the job found in one scan.
    #[derive(Debug, Clone)]
    pub(super) struct Member {
        pub(super) identity: Identity,
        pub(super) name: String,
        pub(super) zombie: bool,
    }

    impl Member {
        fn process(&self) -> JobProcess {
            JobProcess {
                pid: self.identity.pid,
                name: self.name.clone(),
            }
        }
    }

    /// The members of the job rooted at `root`: processes of `known`, of the
    /// root's process group, carrying `marker`, and everything whose ancestry
    /// or lineage reaches one of those or the root. `lineage` holds the unique
    /// ids of every process the job has seen, the root and the exited ones
    /// included. The root and the host are never members.
    pub(super) fn members<'a>(
        table: &'a [Proc],
        root: u32,
        host: u32,
        marker: &str,
        known: &HashSet<Identity>,
        lineage: &HashSet<u64>,
    ) -> Vec<&'a Proc> {
        let eligible =
            |process: &Proc| process.pid() > 1 && process.pid() != root && process.pid() != host;
        let root_unique = table
            .iter()
            .find(|process| process.pid() == root)
            .and_then(Proc::unique);
        let mut found: HashSet<u32> = HashSet::new();
        let mut found_unique: HashSet<u64> = HashSet::new();
        for process in table.iter().filter(|process| eligible(process)) {
            if process.pgid == root
                || known.contains(&process.identity)
                || process.marker.as_deref() == Some(marker)
            {
                found.insert(process.pid());
                found_unique.extend(process.unique());
            }
        }
        loop {
            let before = found.len();
            for process in table.iter().filter(|process| eligible(process)) {
                let linked = process.ppid == root
                    || found.contains(&process.ppid)
                    || process.parent_unique.is_some_and(|parent| {
                        root_unique == Some(parent)
                            || lineage.contains(&parent)
                            || found_unique.contains(&parent)
                    });
                if linked {
                    found.insert(process.pid());
                    found_unique.extend(process.unique());
                }
            }
            if found.len() == before {
                break;
            }
        }
        table
            .iter()
            .filter(|process| found.contains(&process.pid()))
            .collect()
    }

    /// Signals `member` unless the pid now belongs to another process. A
    /// process that is gone is no error; one that cannot be signalled is.
    pub(super) fn signal(member: &Member, signal: libc::c_int) -> Result<(), ProcessTableError> {
        if !platform::is_same(member.identity).map_err(ProcessTableError::Table)? {
            return Ok(());
        }
        // SAFETY: `kill` takes plain integers and touches no memory.
        if unsafe { libc::kill(member.identity.pid.cast_signed(), signal) } == 0 {
            return Ok(());
        }
        let source = io::Error::last_os_error();
        if source.raw_os_error() == Some(libc::ESRCH) {
            return Ok(());
        }
        Err(ProcessTableError::Signal {
            process: member.process(),
            source,
        })
    }

    /// Signals the first listed process this user may not signal, as a member
    /// would be; `None` when there is none.
    #[cfg(test)]
    pub(super) fn signal_foreign_for_test(
        number: libc::c_int,
    ) -> Option<Result<(), ProcessTableError>> {
        let table =
            platform::snapshot(platform::identity_mode().0, true).expect("the table is readable");
        table
            .procs
            .iter()
            .filter(|process| !process.zombie)
            .find(|process| {
                // SAFETY: signal 0 probes whether a signal could be sent.
                let refused = unsafe { libc::kill(process.pid().cast_signed(), 0) } != 0;
                refused && io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)
            })
            .map(|row| {
                signal(
                    &Member {
                        identity: row.identity,
                        name: row.name.clone(),
                        zombie: row.zombie,
                    },
                    number,
                )
            })
    }

    /// Signals the job's process group; a group that is gone is no error.
    fn signal_group(root: &JobProcess, signal: libc::c_int) -> Result<(), ProcessTableError> {
        // SAFETY: `kill` takes plain integers and touches no memory.
        if unsafe { libc::kill(-root.pid.cast_signed(), signal) } == 0 {
            return Ok(());
        }
        let source = io::Error::last_os_error();
        if source.raw_os_error() == Some(libc::ESRCH) {
            return Ok(());
        }
        Err(ProcessTableError::Signal {
            process: root.clone(),
            source,
        })
    }

    /// How a task delivers signals: the operating system's, or a test's
    /// stand-in for a process that does not die.
    pub(super) trait Signals {
        /// Signals one member, as [`signal`] does.
        fn member(&self, member: &Member, signal: libc::c_int) -> Result<(), ProcessTableError>;

        /// Signals the job's process group.
        fn group(&self, root: &JobProcess, signal: libc::c_int) -> Result<(), ProcessTableError>;
    }

    /// The operating system's `kill(2)`.
    pub(super) struct Os;

    impl Signals for Os {
        fn member(&self, member: &Member, number: libc::c_int) -> Result<(), ProcessTableError> {
            signal(member, number)
        }

        fn group(&self, root: &JobProcess, number: libc::c_int) -> Result<(), ProcessTableError> {
            signal_group(root, number)
        }
    }

    /// A message to the job's task.
    #[derive(Debug)]
    pub(super) enum Command {
        /// Answer once no descendant is alive.
        Wait(Sender<Result<(), ProcessTableError>>),
        /// Kill the descendants, and the process group when `group` is set;
        /// answers with what was killed.
        Kill {
            group: bool,
            reply: Sender<Result<Vec<JobProcess>, ProcessTableError>>,
        },
        /// End the task.
        Finish(Sender<()>),
    }

    /// The task's end of its command channel. Dropping it closes the channel
    /// and drops every command still queued, whose asker then learns that the
    /// task ended: async-channel keeps a queued message, and the reply sender
    /// inside it, alive for as long as any sender does, so a command left in
    /// the queue would otherwise wait for its answer forever.
    struct Inbox(Receiver<Command>);

    impl Drop for Inbox {
        fn drop(&mut self) {
            self.0.close();
            while self.0.try_recv().is_ok() {}
        }
    }

    enum Event {
        Command(Command),
        Changed(io::Result<()>),
        Closed,
    }

    /// The task that owns one job's member set.
    pub(super) struct Task<S> {
        root: u32,
        root_name: String,
        /// Whether the root has exited and waits to be reaped. A process
        /// group that holds only that zombie cannot be signalled on macOS.
        root_zombie: bool,
        marker: String,
        scanner: ProcessScanner,
        /// How processes are identified, decided once when the job starts.
        mode: platform::IdentityMode,
        /// Watches the root from the spawn on, and every member found.
        watch: Option<platform::Watch>,
        /// Why there is no watch, when the kernel refused it.
        watch_error: Option<String>,
        /// Members the last scan found; a member that exited is not in it.
        known: HashSet<Identity>,
        /// The unique ids of the root and of every member seen so far, the
        /// exited ones included: a child of any of them belongs to the job.
        lineage: HashSet<u64>,
        waiters: Vec<Sender<Result<(), ProcessTableError>>>,
        signals: S,
    }

    impl Task<Os> {
        /// The task of the job whose root is `root`. It reads the root's
        /// unique id and starts watching the root before it returns.
        pub(super) fn new(root: u32, marker: String, scanner: ProcessScanner) -> Self {
            Self::with_signals(Os, root, marker, scanner)
        }
    }

    impl<S: Signals + Send + 'static> Task<S> {
        pub(super) fn with_signals(
            signals: S,
            root: u32,
            marker: String,
            scanner: ProcessScanner,
        ) -> Self {
            let (mode, degraded) = platform::identity_mode();
            if let Some(reason) = degraded {
                tracing::warn!(
                    root,
                    %reason,
                    "process identity falls back to pid plus start time; \
                     descendants are found by group, ancestry and the environment marker only"
                );
            }
            let (watch, watch_error) = match platform::Watch::new(root) {
                Ok(watch) => (Some(watch), None),
                Err(error) => {
                    tracing::warn!(root, %error, "cannot watch the job's processes");
                    (None, Some(error.to_string()))
                }
            };
            Self {
                root,
                root_name: "the job's shell".to_string(),
                root_zombie: false,
                marker,
                scanner,
                mode,
                watch,
                watch_error,
                known: HashSet::new(),
                lineage: platform::unique_of(mode, root).into_iter().collect(),
                waiters: Vec::new(),
                signals,
            }
        }

        /// The task's loop. `inbox` is wrapped before the returned future is
        /// first polled, so even a task dropped unpolled leaves no command
        /// waiting for an answer.
        pub(super) fn run(
            self,
            inbox: Receiver<Command>,
        ) -> impl std::future::Future<Output = ()> + Send + 'static {
            self.serve(Inbox(inbox))
        }

        async fn serve(mut self, inbox: Inbox) {
            let inbox = &inbox.0;
            let mut finished = None;
            loop {
                let next = async { inbox.recv().await.map_or(Event::Closed, Event::Command) };
                let event = if self.watching() {
                    futures_lite::future::or(next, async { Event::Changed(self.changed().await) })
                        .await
                } else {
                    next.await
                };
                let mut scan = false;
                match event {
                    Event::Closed => break,
                    Event::Command(Command::Wait(reply)) => match &self.watch_error {
                        Some(reason) => {
                            let error = ProcessTableError::Table(io::Error::other(reason.clone()));
                            let _ = reply.send(Err(error)).await;
                        }
                        None => self.waiters.push(reply),
                    },
                    Event::Command(Command::Kill { group, reply }) => {
                        let result = self.kill(group).await;
                        let _ = reply.send(result).await;
                    }
                    Event::Command(Command::Finish(ack)) => {
                        // Closed before the finish is acknowledged, so a
                        // command sent once `finish` returned is refused and
                        // reads as the job having ended.
                        inbox.close();
                        finished = Some(ack);
                        break;
                    }
                    Event::Changed(Err(error)) => {
                        tracing::warn!(root = self.root, %error, "the job's watch failed");
                        self.watch = None;
                        self.watch_error = Some(error.to_string());
                        self.fail_waiters(ProcessTableError::Table(error)).await;
                    }
                    Event::Changed(Ok(())) => scan = true,
                }
                let started = std::time::Instant::now();
                self.settle(scan).await;
                if scan && let Some(watch) = self.watch.as_mut() {
                    // A fork storm cannot make the job scan without a break:
                    // the watch rests as long as this scan took, which caps
                    // the share of time spent scanning at one half. The scan
                    // that answers an event never waits.
                    watch.rest(started.elapsed());
                }
            }
            if let Some(ack) = finished {
                let _ = ack.send(()).await;
            }
        }

        /// Whether the next event may come from the watch: always where the
        /// platform signals forks and exits, so children are found while
        /// their lineage can be read; otherwise only while a wait runs.
        const fn watching(&self) -> bool {
            self.watch.is_some() && (platform::WATCH_FROM_SPAWN || !self.waiters.is_empty())
        }

        async fn changed(&mut self) -> io::Result<()> {
            match self.watch.as_mut() {
                Some(watch) => watch.changed().await,
                None => std::future::pending().await,
            }
        }

        /// Reads the table, at most `max_age` old, and attributes it to the
        /// job; `markers` says whether the environment markers are read too.
        /// What it finds becomes the job's member set, and its unique ids join
        /// the lineage.
        async fn scan(
            &mut self,
            max_age: Duration,
            markers: bool,
        ) -> Result<Vec<Member>, ProcessTableError> {
            let scanner = self.scanner.clone();
            let (root, mode) = (self.root, self.mode);
            // The seen sets move into the read and back, so a long job's
            // history is never copied per scan.
            let (marker, known, lineage) = (
                self.marker.clone(),
                std::mem::take(&mut self.known),
                std::mem::take(&mut self.lineage),
            );
            let (known, lineage, outcome) = blocking::unblock(move || {
                let outcome = scanner
                    .table(max_age, mode, markers)
                    .map_err(ProcessTableError::Table)
                    .map(|table| {
                        let host = std::process::id();
                        let members = members(&table.procs, root, host, &marker, &known, &lineage)
                            .into_iter()
                            .map(|process| Member {
                                identity: process.identity,
                                name: process.name.clone(),
                                zombie: process.zombie,
                            })
                            .collect::<Vec<_>>();
                        let root = table
                            .procs
                            .iter()
                            .find(|process| process.pid() == root)
                            .map(|process| {
                                (process.name.clone(), process.zombie, process.unique())
                            });
                        (members, root)
                    });
                (known, lineage, outcome)
            })
            .await;
            self.known = known;
            self.lineage = lineage;
            let (members, root) = outcome?;
            // An unreaped root that the table does not list is a zombie.
            self.root_zombie = root.as_ref().is_none_or(|(_, zombie, _)| *zombie);
            if let Some((name, _, unique)) = root {
                self.root_name = name;
                self.lineage.extend(unique);
            }
            self.lineage.extend(
                members
                    .iter()
                    .filter_map(|member| member.identity.birth.unique()),
            );
            self.known = members.iter().map(|member| member.identity).collect();
            Ok(members)
        }

        /// Whether no descendant is alive, watching the live ones so the next
        /// change is signalled.
        ///
        /// `markers` says whether the scan reads the environment markers.
        /// Without them a scan is several times faster, which is what an
        /// event needs: the child that forked has little time before its
        /// parent exits. The markers are read again by the next scan that
        /// needs them.
        async fn poll_empty(&mut self, markers: bool) -> Result<bool, ProcessTableError> {
            for _ in 0..2 {
                let members = self.scan(platform::SCAN_SHARING, markers).await?;
                let live: Vec<u32> = members
                    .iter()
                    .filter(|member| !member.zombie)
                    .map(|member| member.identity.pid)
                    .collect();
                if live.is_empty() {
                    return Ok(true);
                }
                // A process registered now may have forked before it was
                // watched, so a new registration is followed by another scan.
                let root = self.root;
                let watched = self
                    .watch
                    .as_mut()
                    .map_or(Ok(false), |watch| watch.follow(root, &live));
                if !watched.map_err(ProcessTableError::Table)? {
                    break;
                }
            }
            Ok(false)
        }

        /// Scans after a watch event, or while a wait runs, and ends the
        /// waits when nothing is alive.
        async fn settle(&mut self, event: bool) {
            if !event && self.waiters.is_empty() {
                return;
            }
            let markers = !self.waiters.is_empty();
            match self.poll_empty(markers).await {
                Ok(false) => {}
                Ok(true) => {
                    for waiter in self.waiters.drain(..) {
                        let _ = waiter.send(Ok(())).await;
                    }
                }
                Err(error) if self.waiters.is_empty() => {
                    tracing::warn!(root = self.root, %error, "cannot scan the job's processes");
                }
                Err(error) => self.fail_waiters(error).await,
            }
        }

        async fn fail_waiters(&mut self, error: ProcessTableError) {
            let message = error.to_string();
            let mut error = Some(error);
            for waiter in self.waiters.drain(..) {
                let reply = error
                    .take()
                    .unwrap_or_else(|| ProcessTableError::Table(io::Error::other(message.clone())));
                let _ = waiter.send(Err(reply)).await;
            }
        }

        /// Freezes the job's processes until a pass finds no new one, then
        /// kills them, and fails if a tracked member is still alive.
        async fn kill(&mut self, group: bool) -> Result<Vec<JobProcess>, ProcessTableError> {
            let mut frozen: Vec<Member> = Vec::new();
            let result = self.freeze_and_kill(group, &mut frozen).await;
            if result.is_err() {
                // Do not leave stopped processes behind a failed kill.
                for member in &frozen {
                    let _ = self.signals.member(member, libc::SIGCONT);
                }
                if group && !self.root_zombie {
                    let _ = self.signals.group(&self.root_process(), libc::SIGCONT);
                }
            }
            result
        }

        fn root_process(&self) -> JobProcess {
            JobProcess {
                pid: self.root,
                name: self.root_name.clone(),
            }
        }

        async fn freeze_and_kill(
            &mut self,
            group: bool,
            frozen: &mut Vec<Member>,
        ) -> Result<Vec<JobProcess>, ProcessTableError> {
            // Names are read from the last pass of the freeze, after the
            // processes stopped, so a just-forked child has usually exec'd.
            let mut killed: Vec<Member> = Vec::new();
            if group {
                // Learns whether the root is alive before the group is signalled.
                self.scan(Duration::ZERO, true).await?;
            }
            for _ in 0..FREEZE_ROUNDS {
                if group && !self.root_zombie {
                    self.signals.group(&self.root_process(), libc::SIGSTOP)?;
                }
                let members = self.scan(Duration::ZERO, true).await?;
                let live: Vec<Member> = members
                    .into_iter()
                    .filter(|member| !member.zombie)
                    .collect();
                let fresh: Vec<Member> = live
                    .iter()
                    .filter(|member| frozen.iter().all(|seen| seen.identity != member.identity))
                    .cloned()
                    .collect();
                killed = live;
                if fresh.is_empty() {
                    break;
                }
                for member in fresh {
                    self.signals.member(&member, libc::SIGSTOP)?;
                    frozen.push(member);
                }
            }
            if group && !self.root_zombie {
                self.signals.group(&self.root_process(), libc::SIGKILL)?;
            }
            for _ in 0..KILL_ROUNDS {
                let live: Vec<Member> = self
                    .scan(Duration::ZERO, true)
                    .await?
                    .into_iter()
                    .filter(|member| !member.zombie)
                    .collect();
                if live.is_empty() {
                    return Ok(killed.iter().map(Member::process).collect());
                }
                for member in live {
                    self.signals.member(&member, libc::SIGKILL)?;
                    if killed.iter().all(|seen| seen.identity != member.identity) {
                        killed.push(member);
                    }
                }
                async_io::Timer::after(KILL_ROUND_PAUSE).await;
            }
            let survivors: Vec<JobProcess> = self
                .scan(Duration::ZERO, true)
                .await?
                .iter()
                .filter(|member| !member.zombie)
                .map(Member::process)
                .collect();
            if survivors.is_empty() {
                return Ok(killed.iter().map(Member::process).collect());
            }
            Err(ProcessTableError::Survived(survivors))
        }
    }
}

/// Parts of the process-table reads are reached only through the watched
/// tree, which the supervisor replaces on Linux.
#[cfg(target_os = "linux")]
#[cfg_attr(target_os = "linux", allow(dead_code))]
mod platform {
    use std::{
        fs, io,
        os::{
            fd::{FromRawFd, OwnedFd},
            unix::fs::MetadataExt,
        },
        time::Duration,
    };

    use async_io::Async;

    use super::unix::{Birth, Identity, Proc, Table};

    /// How long a table read may be reused by another job's check.
    pub(super) const SCAN_SHARING: Duration = super::DESCENDANT_POLL;

    /// Linux has no exit events for arbitrary processes, so a job is checked
    /// only while a wait runs.
    pub(super) const WATCH_FROM_SPAWN: bool = false;

    /// How processes are identified: on Linux always by pid and start time.
    #[derive(Debug, Clone, Copy)]
    pub(super) struct IdentityMode;

    /// Linux identities never fall back.
    pub(super) const fn identity_mode() -> (IdentityMode, Option<String>) {
        (IdentityMode, None)
    }

    fn gone(error: &io::Error) -> bool {
        error.kind() == io::ErrorKind::NotFound || error.raw_os_error() == Some(libc::ESRCH)
    }

    /// Fields of a `/proc/<pid>/stat` dump: the command name sits in
    /// parentheses and may itself contain spaces and parentheses, so the other
    /// fields are read after the last `") "`.
    #[allow(clippy::similar_names)] // `ppid` and `pgid` are the kernel's names
    pub(super) fn parse_stat(pid: u32, line: &str) -> Option<Proc> {
        let (head, rest) = line.rsplit_once(") ")?;
        let name = head.split_once('(')?.1.to_string();
        let mut fields = rest.split_whitespace();
        let state = fields.next()?;
        let ppid = fields.next()?.parse().ok()?;
        let pgid = fields.next()?.parse().ok()?;
        // Field 22, `starttime`, is the 20th after the state (field 3).
        let starttime = fields.nth(16)?.parse().ok()?;
        Some(Proc {
            identity: Identity {
                pid,
                birth: Birth::Ticks(starttime),
            },
            ppid,
            parent_unique: None,
            pgid,
            zombie: matches!(state, "Z" | "X"),
            name,
            marker: None,
        })
    }

    /// The value of the job marker in the environment of `pid`. A process
    /// whose environment cannot be read (it exited, or disabled dumping) is
    /// not attributable this way.
    fn marker_of(pid: u32) -> Option<String> {
        let prefix = format!("{}=", super::JOB_MARKER_VAR);
        let environment = fs::read(format!("/proc/{pid}/environ")).ok()?;
        environment
            .split(|byte| *byte == 0)
            .find_map(|entry| entry.strip_prefix(prefix.as_bytes()))
            .map(|value| String::from_utf8_lossy(value).into_owned())
    }

    /// Linux reads the markers whatever `_markers` says: only macOS scans
    /// without them.
    pub(super) fn snapshot(_mode: IdentityMode, _markers: bool) -> io::Result<Table> {
        // SAFETY: `geteuid` has no preconditions.
        let me = unsafe { libc::geteuid() };
        let mut procs = 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().ok())
            else {
                continue;
            };
            let uid = match entry.metadata() {
                Ok(metadata) => metadata.uid(),
                Err(error) if gone(&error) => continue,
                Err(error) => return Err(error),
            };
            let line = match fs::read_to_string(entry.path().join("stat")) {
                Ok(line) => line,
                Err(error) if gone(&error) => continue,
                Err(error) => return Err(error),
            };
            let mut process = parse_stat(pid, &line)
                .ok_or_else(|| io::Error::other(format!("malformed /proc/{pid}/stat: {line:?}")))?;
            if uid == me && !process.zombie {
                process.marker = marker_of(pid);
            }
            procs.push(process);
        }
        Ok(Table { procs })
    }

    /// Linux identifies by start time and has no lineage ids.
    pub(super) const fn unique_of(_mode: IdentityMode, _pid: u32) -> Option<u64> {
        None
    }

    /// Whether `identity` is the process that has its pid now.
    pub(super) fn is_same(identity: Identity) -> io::Result<bool> {
        match fs::read_to_string(format!("/proc/{}/stat", identity.pid)) {
            Ok(line) => {
                Ok(parse_stat(identity.pid, &line)
                    .is_some_and(|process| process.identity == identity))
            }
            Err(error) if gone(&error) => Ok(false),
            Err(error) => Err(error),
        }
    }

    /// Linux has no exit events for arbitrary processes, so a job waiting on
    /// its tree checks it once per [`SCAN_SHARING`].
    pub(super) struct Watch;

    // Linux has nothing to register or fail on, but the callers see the same
    // fallible shape as on macOS.
    #[allow(
        clippy::unnecessary_wraps,
        clippy::unused_self,
        clippy::needless_pass_by_ref_mut
    )]
    impl Watch {
        pub(super) const fn new(_root: u32) -> io::Result<Self> {
            Ok(Self)
        }

        /// Nothing to register: reports no new watch.
        pub(super) const fn follow(&mut self, _root: u32, _live: &[u32]) -> io::Result<bool> {
            Ok(false)
        }

        /// Polling already spaces the scans.
        pub(super) const fn rest(&mut self, _duration: Duration) {}

        pub(super) async fn changed(&mut self) -> io::Result<()> {
            async_io::Timer::after(super::DESCENDANT_POLL).await;
            Ok(())
        }
    }

    /// A pidfd of a child, readable once the child has exited.
    pub(super) struct ExitWatch {
        pid: u32,
        pidfd: Async<OwnedFd>,
    }

    impl ExitWatch {
        pub(super) fn new(pid: u32) -> io::Result<Self> {
            // SAFETY: `pidfd_open` takes plain integers.
            let fd = unsafe { libc::syscall(libc::SYS_pidfd_open, pid.cast_signed(), 0) };
            if fd < 0 {
                return Err(io::Error::last_os_error());
            }
            let fd = libc::c_int::try_from(fd).expect("a descriptor fits c_int");
            // SAFETY: `fd` is a new descriptor nothing else owns.
            let pidfd = unsafe { OwnedFd::from_raw_fd(fd) };
            Ok(Self {
                pid,
                pidfd: Async::new(pidfd)?,
            })
        }

        /// Returns once the child has exited and can be waited for. The
        /// kernel makes a pidfd readable once the process is a zombie or
        /// reaped, so the check that follows fails only for a child that
        /// something else reaped.
        pub(super) async fn exited(self) -> io::Result<()> {
            self.pidfd.readable().await?;
            if super::exited_unreaped(self.pid)? {
                Ok(())
            } else {
                Err(io::Error::other(format!(
                    "the pidfd of {} is readable but waitid reports no exit",
                    self.pid
                )))
            }
        }
    }

    #[cfg(test)]
    mod tests {
        use super::{Birth, parse_stat};

        /// The name may hold spaces and parentheses; the identity is field 22,
        /// the start time.
        #[test]
        fn stat_lines_yield_group_state_and_start_time() {
            let line = "4242 (my (odd) cmd) Z 1 4243 4242 0 -1 4194560 1000 0 0 0 5 6 0 0 20 0 3 0 \
                        987654 12345678 100 18446744073709551615";
            let process = parse_stat(4242, line).expect("a valid stat line");
            assert_eq!(process.name, "my (odd) cmd");
            assert_eq!((process.ppid, process.pgid), (1, 4243));
            assert!(process.zombie);
            assert_eq!(process.identity.birth, Birth::Ticks(987_654));
            assert!(parse_stat(1, "1 (init) S 0").is_none());
        }
    }
}

#[cfg(target_os = "macos")]
mod platform {
    use std::{
        collections::HashSet,
        ffi::c_int,
        io,
        mem::{size_of, zeroed},
        os::fd::{AsRawFd, FromRawFd, OwnedFd},
        time::{Duration, Instant},
    };

    use async_io::Async;

    use super::unix::{Birth, Identity, Proc, Table};

    /// A table read is never reused: an exit is signalled by the kernel, and
    /// a scan answers it.
    pub(super) const SCAN_SHARING: Duration = Duration::ZERO;

    /// The kernel signals a job's forks, execs and exits, so its task watches
    /// it from the spawn on.
    pub(super) const WATCH_FROM_SPAWN: bool = true;

    /// `proc_pidinfo` flavor `PROC_PIDUNIQIDENTIFIERINFO`.
    ///
    /// Apple SPI: this flavor and [`ProcUniqIdentifierInfo`] sit under
    /// `#ifdef PRIVATE` in `sys/proc_info.h`, so the public SDK and the `libc`
    /// crate do not declare them, and they are declared here from xnu's
    /// header. Verified on macOS 27.0.1 (build 26A434, Apple silicon): the
    /// call returns 56 bytes and `p_uniqueid` is never reused. `p_puniqueid`
    /// survives the parent's exit and the reparenting to `launchd`, but the
    /// kernel resets it to `launchd`'s (1) at the first `exec` the process
    /// makes after it was reparented; a process that execs before its parent
    /// exits keeps it. Apple may change all of it without notice: when the
    /// call fails or returns another size at the start of a job, that job's
    /// identity is the pid plus the public start time
    /// (`pbi_start_tvsec`/`pbi_start_tvusec` from `PROC_PIDTBSDINFO`) for its
    /// whole life, lineage is dropped, attribution keeps the environment
    /// marker, and the job logs one warning naming the failure. A failure
    /// later in a job's life fails the scan; it never changes the identities.
    const PROC_PIDUNIQIDENTIFIERINFO: c_int = 17;

    /// `struct proc_uniqidentifierinfo` of `sys/proc_info.h` (SPI, see
    /// [`PROC_PIDUNIQIDENTIFIERINFO`]).
    #[repr(C)]
    #[allow(clippy::struct_field_names)] // the C names, kept to match the header
    struct ProcUniqIdentifierInfo {
        /// UUID of the main executable.
        p_uuid: [u8; 16],
        /// The 64-bit unique id of the process.
        p_uniqueid: u64,
        /// The unique id of the process's parent when it was forked.
        p_puniqueid: u64,
        p_idversion: i32,
        p_orig_ppidversion: i32,
        p_reserve2: u64,
        p_reserve3: u64,
    }

    /// A process's unique id and its parent's.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub(super) struct Uniq {
        pub(super) id: u64,
        pub(super) parent: u64,
    }

    /// Why unique ids could not be read for a process.
    #[derive(Debug)]
    pub(super) enum UniqError {
        /// The process exited.
        Gone,
        /// The SPI failed or answered unexpectedly.
        Unavailable(String),
    }

    /// Where unique ids come from: the SPI, or a test's stand-in.
    pub(super) trait IdentitySource {
        fn unique(&self, pid: i32) -> Result<Uniq, UniqError>;
    }

    /// The `PROC_PIDUNIQIDENTIFIERINFO` SPI.
    pub(super) struct Spi;

    impl IdentitySource for Spi {
        fn unique(&self, pid: i32) -> Result<Uniq, UniqError> {
            // SAFETY: the struct is plain data for which all-zero bytes are valid.
            let mut info: ProcUniqIdentifierInfo = unsafe { zeroed() };
            let size = c_int::try_from(size_of::<ProcUniqIdentifierInfo>()).expect("fits c_int");
            // SAFETY: `info` is `size` bytes of live memory.
            let written = unsafe {
                libc::proc_pidinfo(
                    pid,
                    PROC_PIDUNIQIDENTIFIERINFO,
                    0,
                    (&raw mut info).cast(),
                    size,
                )
            };
            if written == size {
                return Ok(Uniq {
                    id: info.p_uniqueid,
                    parent: info.p_puniqueid,
                });
            }
            let error = io::Error::last_os_error();
            if written <= 0 && error.raw_os_error() == Some(libc::ESRCH) {
                return Err(UniqError::Gone);
            }
            Err(UniqError::Unavailable(if written <= 0 {
                format!("proc_pidinfo(PROC_PIDUNIQIDENTIFIERINFO) for pid {pid} failed: {error}")
            } else {
                format!(
                    "proc_pidinfo(PROC_PIDUNIQIDENTIFIERINFO) for pid {pid} returned {written} \
                     bytes, expected {size}"
                )
            }))
        }
    }

    /// The public part of a process's row.
    struct Bsd {
        pid: u32,
        ppid: u32,
        pgid: u32,
        uid: u32,
        zombie: bool,
        name: String,
        started_seconds: u64,
        started_micros: u64,
    }

    fn list() -> io::Result<Vec<Bsd>> {
        // `proc_listallpids` reports a count of pids, and a null buffer asks
        // for the current count; processes start meanwhile, so the buffer
        // grows until the call does not fill it.
        // SAFETY: a null buffer of size 0 is the documented size query.
        let mut capacity =
            usize::try_from(unsafe { libc::proc_listallpids(std::ptr::null_mut(), 0) })
                .map_err(|_| io::Error::last_os_error())?
                + 64;
        let pids = loop {
            let mut pids = vec![0 as c_int; capacity];
            let bytes = c_int::try_from(capacity * size_of::<c_int>()).expect("fits c_int");
            // SAFETY: the buffer holds `bytes` bytes of live memory.
            let count = unsafe { libc::proc_listallpids(pids.as_mut_ptr().cast(), bytes) };
            let count = usize::try_from(count).map_err(|_| io::Error::last_os_error())?;
            if count < capacity {
                pids.truncate(count);
                break pids;
            }
            capacity *= 2;
        };
        let mut rows = Vec::with_capacity(pids.len());
        for pid in pids.into_iter().filter(|pid| *pid > 0) {
            if let Some(info) = bsd_info(pid) {
                rows.push(Bsd {
                    pid: info.pbi_pid,
                    ppid: info.pbi_ppid,
                    pgid: info.pbi_pgid,
                    uid: info.pbi_uid,
                    zombie: info.pbi_status == libc::SZOMB,
                    name: c_name(&info.pbi_name)
                        .filter(|name| !name.is_empty())
                        .unwrap_or_else(|| c_name(&info.pbi_comm).unwrap_or_default()),
                    started_seconds: info.pbi_start_tvsec,
                    started_micros: info.pbi_start_tvusec,
                });
            }
        }
        Ok(rows)
    }

    /// `PROC_PIDTBSDINFO` of `pid`; `None` when the process exited.
    fn bsd_info(pid: c_int) -> Option<libc::proc_bsdinfo> {
        // SAFETY: `proc_bsdinfo` is plain data for which all-zero bytes are valid.
        let mut info: libc::proc_bsdinfo = unsafe { zeroed() };
        let size = c_int::try_from(size_of::<libc::proc_bsdinfo>()).expect("fits c_int");
        // SAFETY: `info` is `size` bytes of live memory.
        let written = unsafe {
            libc::proc_pidinfo(pid, libc::PROC_PIDTBSDINFO, 0, (&raw mut info).cast(), size)
        };
        (written == size).then_some(info)
    }

    fn c_name(raw: &[libc::c_char]) -> Option<String> {
        let bytes: Vec<u8> = raw
            .iter()
            .take_while(|byte| **byte != 0)
            .map(|byte| byte.cast_unsigned())
            .collect();
        String::from_utf8(bytes).ok()
    }

    /// The raw `KERN_PROCARGS2` block of `pid`: argument count, executable
    /// path, arguments, then the environment, all NUL-separated. `None` when
    /// the process exited or refuses the read.
    fn process_arguments(pid: u32) -> Option<Vec<u8>> {
        let mut mib = [libc::CTL_KERN, libc::KERN_PROCARGS2, pid.cast_signed()];
        let mut size = 0usize;
        // SAFETY: a null `oldp` asks for the needed size; `mib` has three entries.
        let sized = unsafe {
            libc::sysctl(
                mib.as_mut_ptr(),
                3,
                std::ptr::null_mut(),
                &raw mut size,
                std::ptr::null_mut(),
                0,
            )
        };
        if sized != 0 {
            return None;
        }
        let mut buffer = vec![0u8; size];
        // SAFETY: `buffer` holds `size` writable bytes.
        let read = unsafe {
            libc::sysctl(
                mib.as_mut_ptr(),
                3,
                buffer.as_mut_ptr().cast(),
                &raw mut size,
                std::ptr::null_mut(),
                0,
            )
        };
        if read != 0 {
            return None;
        }
        buffer.truncate(size);
        Some(buffer)
    }

    /// The value of the job marker in the environment of `pid`. A process
    /// whose arguments cannot be read is not attributable this way.
    fn marker_of(pid: u32) -> Option<String> {
        let prefix = format!("{}=", super::JOB_MARKER_VAR);
        process_arguments(pid)?
            .split(|byte| *byte == 0)
            .find_map(|entry| entry.strip_prefix(prefix.as_bytes()))
            .map(|value| String::from_utf8_lossy(value).into_owned())
    }

    /// How processes are identified, decided once per job.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub(super) enum IdentityMode {
        /// By the SPI's unique id, with lineage.
        Unique,
        /// By pid plus the public start time, without lineage.
        Started,
    }

    /// The identity mode this macOS supports, and why it is not the unique
    /// id when it is not.
    pub(super) fn identity_mode() -> (IdentityMode, Option<String>) {
        identity_mode_with(&Spi)
    }

    /// [`identity_mode`] asking `source` about this process.
    pub(super) fn identity_mode_with(
        source: &impl IdentitySource,
    ) -> (IdentityMode, Option<String>) {
        match source.unique(std::process::id().cast_signed()) {
            Ok(_) => (IdentityMode::Unique, None),
            Err(UniqError::Gone) => (
                IdentityMode::Started,
                Some("the SPI does not know this process".to_string()),
            ),
            Err(UniqError::Unavailable(reason)) => (IdentityMode::Started, Some(reason)),
        }
    }

    pub(super) fn snapshot(mode: IdentityMode, markers: bool) -> io::Result<Table> {
        snapshot_with(&Spi, mode, markers)
    }

    /// The table with identities from `source` in `mode`. A process that
    /// exited during the read is dropped; any other failure of the source in
    /// [`IdentityMode::Unique`] fails the read, so identities never change
    /// kind within a job. `markers` says whether to read the environment of
    /// each of this user's processes.
    pub(super) fn snapshot_with(
        source: &impl IdentitySource,
        mode: IdentityMode,
        markers: bool,
    ) -> io::Result<Table> {
        // SAFETY: `geteuid` has no preconditions.
        let me = unsafe { libc::geteuid() };
        let mut procs = Vec::new();
        for row in list()? {
            let (birth, parent_unique) = match mode {
                IdentityMode::Started => (
                    Birth::Started {
                        seconds: row.started_seconds,
                        micros: row.started_micros,
                    },
                    None,
                ),
                IdentityMode::Unique => match source.unique(row.pid.cast_signed()) {
                    Ok(unique) => (Birth::Unique(unique.id), Some(unique.parent)),
                    // The process exited between the listing and the query.
                    Err(UniqError::Gone) => continue,
                    Err(UniqError::Unavailable(reason)) => return Err(io::Error::other(reason)),
                },
            };
            let marker = (markers && row.uid == me && !row.zombie)
                .then(|| marker_of(row.pid))
                .flatten();
            procs.push(Proc {
                identity: Identity {
                    pid: row.pid,
                    birth,
                },
                ppid: row.ppid,
                parent_unique,
                pgid: row.pgid,
                zombie: row.zombie,
                name: row.name,
                marker,
            });
        }
        Ok(Table { procs })
    }

    /// The unique id of `pid`; `None` in [`IdentityMode::Started`] or when it
    /// cannot be read (the process is a zombie or gone).
    pub(super) fn unique_of(mode: IdentityMode, pid: u32) -> Option<u64> {
        match mode {
            IdentityMode::Unique => Spi.unique(pid.cast_signed()).ok().map(|unique| unique.id),
            IdentityMode::Started => None,
        }
    }

    /// Whether `identity` is the process that has its pid now.
    pub(super) fn is_same(identity: Identity) -> io::Result<bool> {
        let pid = identity.pid.cast_signed();
        match identity.birth {
            Birth::Unique(id) => match Spi.unique(pid) {
                Ok(unique) => Ok(unique.id == id),
                Err(UniqError::Gone) => Ok(false),
                Err(UniqError::Unavailable(reason)) => Err(io::Error::other(reason)),
            },
            Birth::Started { seconds, micros } => Ok(bsd_info(pid).is_some_and(|info| {
                info.pbi_start_tvsec == seconds && info.pbi_start_tvusec == micros
            })),
            Birth::Ticks(_) => unreachable!("macOS identities are never start ticks"),
        }
    }

    /// A kqueue descriptor. It rejects `FIONBIO`, so it is registered with
    /// the reactor as it is, and every read of it is a `kevent` with a zero
    /// timeout.
    fn new_queue() -> io::Result<Async<OwnedFd>> {
        // SAFETY: `kqueue` takes no arguments.
        let fd = unsafe { libc::kqueue() };
        if fd < 0 {
            return Err(io::Error::last_os_error());
        }
        // SAFETY: `fd` is a new descriptor nothing else owns.
        let queue = unsafe { OwnedFd::from_raw_fd(fd) };
        // SAFETY: plain integers.
        if unsafe { libc::fcntl(fd, libc::F_SETFD, libc::FD_CLOEXEC) } < 0 {
            return Err(io::Error::last_os_error());
        }
        Async::new_nonblocking(queue)
    }

    /// Registers `pid` on `queue` for `fflags`; `false` when the process is
    /// gone or a zombie, which the kernel does not watch.
    fn register(queue: &Async<OwnedFd>, pid: u32, fflags: u32) -> io::Result<bool> {
        // SAFETY: `kevent` is plain data for which all-zero bytes are valid.
        let mut change: libc::kevent = unsafe { zeroed() };
        change.ident = pid as usize;
        change.filter = libc::EVFILT_PROC;
        change.flags = libc::EV_ADD | libc::EV_CLEAR;
        change.fflags = fflags;
        // SAFETY: one change is passed and no event is requested.
        let result = unsafe {
            libc::kevent(
                queue.as_raw_fd(),
                &raw const change,
                1,
                std::ptr::null_mut(),
                0,
                std::ptr::null(),
            )
        };
        if result >= 0 {
            return Ok(true);
        }
        let error = io::Error::last_os_error();
        if error.raw_os_error() == Some(libc::ESRCH) {
            Ok(false)
        } else {
            Err(error)
        }
    }

    /// Drains pending events; whether there was any.
    fn drain(queue: &Async<OwnedFd>) -> io::Result<bool> {
        let mut any = false;
        loop {
            // SAFETY: `kevent` is plain data for which all-zero bytes are valid.
            let mut events: [libc::kevent; 16] = unsafe { zeroed() };
            let timeout = libc::timespec {
                tv_sec: 0,
                tv_nsec: 0,
            };
            // SAFETY: `events` has room for the 16 events asked for.
            let count = unsafe {
                libc::kevent(
                    queue.as_raw_fd(),
                    std::ptr::null(),
                    0,
                    events.as_mut_ptr(),
                    16,
                    &raw const timeout,
                )
            };
            if count < 0 {
                return Err(io::Error::last_os_error());
            }
            any |= count > 0;
            if count < 16 {
                return Ok(any);
            }
        }
    }

    /// What a job's processes are followed for.
    const FOLLOWED: u32 = libc::NOTE_FORK | libc::NOTE_EXEC | libc::NOTE_EXIT;

    /// A kqueue that signals when a watched process forks, execs or exits.
    pub(super) struct Watch {
        queue: Async<OwnedFd>,
        followed: HashSet<u32>,
        /// Until when the watch rests after a scan.
        rest_until: Option<Instant>,
    }

    impl Watch {
        /// A watch that already follows `root`: created right after the root
        /// was spawned, it misses none of its forks.
        pub(super) fn new(root: u32) -> io::Result<Self> {
            let queue = new_queue()?;
            let mut followed = HashSet::new();
            if register(&queue, root, FOLLOWED)? {
                followed.insert(root);
            }
            Ok(Self {
                queue,
                followed,
                rest_until: None,
            })
        }

        /// Watches the root and the `live` pids; returns whether any process
        /// was newly watched.
        pub(super) fn follow(&mut self, root: u32, live: &[u32]) -> io::Result<bool> {
            let wanted: HashSet<u32> = live.iter().copied().chain([root]).collect();
            self.followed.retain(|pid| wanted.contains(pid));
            let mut added = false;
            for pid in wanted {
                if self.followed.contains(&pid) {
                    continue;
                }
                // A process that exited already is seen by the scan that follows.
                if register(&self.queue, pid, FOLLOWED)? {
                    self.followed.insert(pid);
                }
                added = true;
            }
            Ok(added)
        }

        /// Rests for `duration` before the next event is taken, so the
        /// scans a fork storm causes use at most half the time.
        pub(super) fn rest(&mut self, duration: Duration) {
            self.rest_until = Some(Instant::now() + duration);
        }

        /// Returns after a watched process forked, exec'd or exited. An event
        /// is answered at once unless it comes within the previous scan's own
        /// duration after that scan: a child's lineage lasts only until its
        /// first `exec` after the parent exited, so no fixed delay is
        /// acceptable.
        pub(super) async fn changed(&mut self) -> io::Result<()> {
            loop {
                if let Some(until) = self.rest_until.take() {
                    async_io::Timer::at(until).await;
                }
                self.queue.readable().await?;
                if drain(&self.queue)? {
                    return Ok(());
                }
            }
        }
    }

    /// A kqueue that signals when one child exits.
    pub(super) struct ExitWatch {
        pid: u32,
        queue: Async<OwnedFd>,
        watched: bool,
    }

    impl ExitWatch {
        pub(super) fn new(pid: u32) -> io::Result<Self> {
            let queue = new_queue()?;
            let watched = register(&queue, pid, libc::NOTE_EXIT)?;
            Ok(Self {
                pid,
                queue,
                watched,
            })
        }

        /// Returns once the child has exited and can be waited for.
        ///
        /// xnu refuses a new watch (`P_REF_DEAD`, `proc_prepareexit`) and then
        /// posts `NOTE_EXIT` (`proc_exit`) before it marks the process a
        /// zombie, so neither an exit event nor a refused watch means the
        /// child is waitable yet, and no further event follows. The wait for
        /// that last step blocks, which takes a pool thread only for the
        /// remainder of the kernel's exit teardown. A pid that is no child
        /// of this process fails that wait.
        pub(super) async fn exited(self) -> io::Result<()> {
            if self.watched {
                loop {
                    self.queue.readable().await?;
                    if drain(&self.queue)? {
                        break;
                    }
                }
            }
            let pid = self.pid;
            blocking::unblock(move || super::block_until_exited_unreaped(pid)).await
        }
    }

    #[cfg(test)]
    pub(super) mod tests {
        use super::{
            Birth, IdentityMode, IdentitySource, ProcUniqIdentifierInfo, Spi, Uniq, UniqError,
            identity_mode, identity_mode_with, size_of, snapshot_with, unique_of,
        };

        /// A source whose every query fails the way a changed SPI would.
        struct Broken(UniqError);

        impl Broken {
            fn failing() -> Self {
                Self(UniqError::Unavailable(
                    "proc_pidinfo(PROC_PIDUNIQIDENTIFIERINFO) failed: test".to_string(),
                ))
            }
        }

        impl IdentitySource for Broken {
            fn unique(&self, _pid: i32) -> Result<Uniq, UniqError> {
                match &self.0 {
                    UniqError::Gone => Err(UniqError::Gone),
                    UniqError::Unavailable(reason) => Err(UniqError::Unavailable(reason.clone())),
                }
            }
        }

        /// A source that answers with another struct size than the SPI's.
        struct WrongSize;

        impl IdentitySource for WrongSize {
            fn unique(&self, pid: i32) -> Result<Uniq, UniqError> {
                Err(UniqError::Unavailable(format!(
                    "returned 24 bytes for pid {pid}, expected {}",
                    size_of::<ProcUniqIdentifierInfo>()
                )))
            }
        }

        fn own_row(table: &super::Table) -> &super::Proc {
            let me = std::process::id();
            table
                .procs
                .iter()
                .find(|process| process.pid() == me)
                .expect("the table lists this process")
        }

        /// The SPI answers with the size declared here, and the unique id of
        /// a child links to its parent's.
        #[test]
        fn the_spi_answers_with_unique_ids_that_link_children_to_parents() {
            let (mode, degraded) = identity_mode();
            assert_eq!(degraded, None, "the SPI should work on this macOS");
            assert_eq!(mode, IdentityMode::Unique);
            let table = snapshot_with(&Spi, mode, true).expect("the table is readable");
            let me = own_row(&table);
            let Birth::Unique(my_id) = me.identity.birth else {
                panic!("expected a unique id, got {:?}", me.identity.birth);
            };
            let mut child = std::process::Command::new("/bin/sleep")
                .arg("5")
                .spawn()
                .expect("sleep should start");
            let table = snapshot_with(&Spi, mode, true).expect("the table is readable");
            let row = table
                .procs
                .iter()
                .find(|process| process.pid() == child.id())
                .expect("the table lists the child");
            assert_eq!(row.parent_unique, Some(my_id));
            child.kill().expect("child should be killed");
            child.wait().expect("child should be reaped");
        }

        /// When the SPI fails, the job's identity is pid plus start time for
        /// good and lineage is dropped, and it says why.
        #[test]
        fn a_failing_spi_falls_back_to_pid_plus_start_time() {
            let (mode, degraded) = identity_mode_with(&Broken::failing());
            assert_eq!(mode, IdentityMode::Started);
            assert!(
                degraded
                    .as_deref()
                    .is_some_and(|reason| reason.contains("PROC_PIDUNIQIDENTIFIERINFO")),
                "{degraded:?}"
            );
            // The fallback never asks the SPI again.
            let table =
                snapshot_with(&Broken::failing(), mode, true).expect("the table is readable");
            assert!(!table.procs.is_empty());
            for process in &table.procs {
                assert!(matches!(process.identity.birth, Birth::Started { .. }));
                assert_eq!(process.parent_unique, None);
            }
            assert_eq!(own_row(&table).pid(), std::process::id());
            assert_eq!(unique_of(mode, std::process::id()), None);
        }

        /// An unexpected size is treated like a failure.
        #[test]
        fn an_spi_answering_another_size_falls_back_too() {
            let (mode, degraded) = identity_mode_with(&WrongSize);
            assert_eq!(mode, IdentityMode::Started);
            assert!(
                degraded
                    .as_deref()
                    .is_some_and(|reason| reason.contains("expected")),
                "{degraded:?}"
            );
        }

        /// A job that started with unique ids does not slip into start times
        /// when the SPI fails later: the scan fails instead.
        #[test]
        fn an_spi_failing_mid_job_fails_the_read() {
            let error = snapshot_with(&Broken::failing(), IdentityMode::Unique, true)
                .expect_err("identities never change kind");
            assert!(error.to_string().contains("PROC_PIDUNIQIDENTIFIERINFO"));
        }

        /// The kqueue reports a watched process's forks, execs and exit, the
        /// first at once; nothing else wakes it on macOS, so this is the
        /// event-driven path itself.
        #[tokio::test]
        async fn the_watch_reports_a_fork_and_an_exit() {
            let mut shell = std::process::Command::new("/bin/sh")
                .args(["-c", "/bin/sleep 0.4 & /bin/sleep 0.6"])
                .spawn()
                .expect("sh should start");
            let mut watch = super::Watch::new(shell.id()).expect("kqueue should open");
            assert!(
                !watch.follow(shell.id(), &[]).expect("watch registers"),
                "the root is watched from the start"
            );
            // Events keep coming until the shell has exited and been seen.
            loop {
                tokio::time::timeout(std::time::Duration::from_secs(5), watch.changed())
                    .await
                    .expect("no event before the shell exited")
                    .expect("the watch reads events");
                if shell.try_wait().expect("sh is polled").is_some() {
                    break;
                }
            }
        }

        /// The exit of a child is signalled to a watch made before it.
        #[tokio::test]
        async fn the_exit_watch_signals_the_exit_of_a_child() {
            let mut child = std::process::Command::new("/bin/sh")
                .args(["-c", "/bin/sleep 0.3"])
                .spawn()
                .expect("sh should start");
            let watch = super::ExitWatch::new(child.id()).expect("kqueue should open");
            tokio::time::timeout(std::time::Duration::from_secs(5), watch.exited())
                .await
                .expect("no event for the exit")
                .expect("the watch reads events");
            child.wait().expect("sh is reaped");
        }

        /// A watch made after the child exited is refused by the kernel; it
        /// still ends once the child is waitable rather than failing.
        #[tokio::test]
        async fn a_watch_made_after_the_exit_ends_with_the_waitable_child() {
            let mut child = std::process::Command::new("/bin/sh")
                .args(["-c", "exit 0"])
                .spawn()
                .expect("sh should start");
            super::super::wait_for_exit(child.id())
                .await
                .expect("the exit is observed");
            let watch = super::ExitWatch::new(child.id()).expect("kqueue should open");
            assert!(!watch.watched, "the kernel watches no zombie");
            tokio::time::timeout(std::time::Duration::from_secs(5), watch.exited())
                .await
                .expect("the exited child is waitable")
                .expect("the zombie is this process's child");
            child.wait().expect("sh is reaped");
        }

        /// A process that exits while the table is read is dropped, not a
        /// reason to fall back.
        #[test]
        fn a_process_that_exited_during_the_read_is_no_degradation() {
            let table = snapshot_with(&Broken(UniqError::Gone), IdentityMode::Unique, true)
                .expect("the table is readable");
            assert!(table.procs.is_empty());
        }
    }
}

#[cfg(all(unix, not(any(target_os = "linux", target_os = "macos"))))]
compile_error!("descendant tracking needs /proc (Linux) or libproc (macOS); this Unix has neither");

#[cfg(all(test, unix))]
mod tests {
    use std::{collections::HashSet, os::unix::process::CommandExt, time::Duration};

    use executor_core::tokio::TokioGlobal;

    use super::{
        JobMarker, JobProcess, JobTree, KillTreeError, ProcessScanner, ProcessTableError, platform,
        unix::{
            Birth, Command, Identity, Member, Proc, Signals, Table, Task, members, signal,
            signal_foreign_for_test,
        },
        wait_for_exit,
    };

    const MARKER: &str = "job-marker";

    fn proc(id: u32, parent: u32, group: u32) -> Proc {
        Proc {
            identity: Identity {
                pid: id,
                birth: Birth::Ticks(u64::from(id) * 10),
            },
            ppid: parent,
            parent_unique: None,
            pgid: group,
            zombie: false,
            name: format!("p{id}"),
            marker: None,
        }
    }

    fn marked(mut process: Proc) -> Proc {
        process.marker = Some(MARKER.to_string());
        process
    }

    fn pids(found: &[&Proc]) -> Vec<u32> {
        let mut pids: Vec<u32> = found.iter().map(|process| process.pid()).collect();
        pids.sort_unstable();
        pids
    }

    /// Members come from the root's group, from ancestry below any member,
    /// from the marker and from earlier finds; the root and the host never do.
    #[test]
    fn members_follow_group_ancestry_and_marker() {
        let table = [
            proc(10, 1, 10),         // the root shell
            proc(11, 10, 10),        // a `&` child in the group
            proc(12, 11, 12),        // its child that left the group
            marked(proc(20, 1, 20)), // a setsid orphan with the marker
            proc(21, 20, 21),        // its child
            proc(30, 1, 30),         // unrelated
            proc(99, 1, 99),         // the host
        ];
        let found = members(&table, 10, 99, MARKER, &HashSet::new(), &HashSet::new());
        assert_eq!(pids(&found), [11, 12, 20, 21]);
    }

    /// With nothing to link it an orphan that left the group is not a member:
    /// the limit the module documents.
    #[test]
    fn an_unmarked_session_leader_is_not_a_member() {
        let table = [proc(10, 1, 10), proc(20, 1, 20)];
        assert!(members(&table, 10, 99, MARKER, &HashSet::new(), &HashSet::new()).is_empty());
    }

    /// A marker of another job does not attribute a process to this one.
    #[test]
    fn another_jobs_marker_is_no_member() {
        let mut other = proc(20, 1, 20);
        other.marker = Some("another-job".to_string());
        let table = [proc(10, 1, 10), other];
        assert!(members(&table, 10, 99, MARKER, &HashSet::new(), &HashSet::new()).is_empty());
    }

    /// An earlier find stays a member while it is the same process, and a pid
    /// that now belongs to another process is not one.
    #[test]
    fn a_known_pid_reused_by_another_process_is_no_member() {
        let table = [proc(10, 1, 10), proc(20, 1, 20)];
        let same = HashSet::from([table[1].identity]);
        assert_eq!(
            pids(&members(&table, 10, 99, MARKER, &same, &HashSet::new())),
            [20]
        );
        let earlier = Identity {
            pid: 20,
            birth: Birth::Ticks(1),
        };
        let reused = HashSet::from([earlier]);
        assert!(members(&table, 10, 99, MARKER, &reused, &HashSet::new()).is_empty());
    }

    /// Lineage by unique id survives reparenting: the orphan's `ppid` is 1,
    /// but its parent's unique id is the root's.
    #[test]
    fn lineage_links_an_orphan_to_the_root_and_to_found_members() {
        let unique = |mut process: Proc, id: u64, parent: u64| {
            process.identity.birth = Birth::Unique(id);
            process.parent_unique = Some(parent);
            process
        };
        let table = [
            unique(proc(10, 1, 10), 100, 1),
            unique(proc(20, 1, 20), 101, 100), // reparented child of the root
            unique(proc(21, 1, 21), 102, 101), // reparented child of that child
            unique(proc(30, 1, 30), 103, 1),   // unrelated
        ];
        let found = members(&table, 10, 99, MARKER, &HashSet::new(), &HashSet::new());
        assert_eq!(pids(&found), [20, 21]);
        // A zombie root is not listed on macOS: the id read earlier serves.
        let unlisted = &table[1..];
        let found = members(
            unlisted,
            10,
            99,
            MARKER,
            &HashSet::new(),
            &HashSet::from([100]),
        );
        assert_eq!(pids(&found), [20, 21]);
        // The unique ids of members that exited since still link their
        // children, and only those do.
        let exited = [
            unique(proc(10, 1, 10), 100, 1),
            unique(proc(21, 1, 21), 102, 101),
        ];
        let seen = HashSet::from([101]);
        let found = members(&exited, 10, 99, MARKER, &HashSet::new(), &seen);
        assert_eq!(pids(&found), [21]);
        let found = members(&exited, 10, 99, MARKER, &HashSet::new(), &HashSet::new());
        assert!(found.is_empty());
    }

    fn spawn_group_leader(script: &str, marker: &JobMarker) -> std::process::Child {
        let (name, value) = marker.entry();
        std::process::Command::new("/bin/sh")
            .arg("-c")
            .arg(script)
            .env(name, value)
            .process_group(0)
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .spawn()
            .expect("sh should start")
    }

    fn alive(pid: u32) -> bool {
        // SAFETY: signal 0 probes process existence.
        unsafe { libc::kill(pid.cast_signed(), 0) == 0 }
    }

    async fn gone_within(pid: u32, wait: Duration) -> bool {
        let deadline = std::time::Instant::now() + wait;
        while std::time::Instant::now() < deadline {
            if !alive(pid) {
                return true;
            }
            async_io::Timer::after(Duration::from_millis(20)).await;
        }
        false
    }

    /// The root stays a zombie, pid and group reserved, until it is waited
    /// for, and its status is still there.
    #[tokio::test(flavor = "multi_thread")]
    async fn the_root_stays_unreaped_until_it_is_waited_for() {
        let mut root = std::process::Command::new("/bin/sh")
            .args(["-c", "exit 3"])
            .process_group(0)
            .spawn()
            .expect("sh should start");
        wait_for_exit(root.id())
            .await
            .expect("the exit is observed");
        assert!(alive(root.id()), "the unreaped root still holds its pid");
        let status = root.wait().expect("the root is reaped afterwards");
        assert_eq!(status.code(), Some(3));
        assert!(!alive(root.id()));
    }

    /// The failure path: a pid that is no child of this process.
    #[tokio::test(flavor = "multi_thread")]
    async fn waiting_for_a_process_that_is_no_child_fails() {
        assert!(wait_for_exit(1).await.is_err());
    }

    /// Regression for the review's reproduction: a `setsid` Apple binary that
    /// the environment cannot attribute, found once through ancestry and then
    /// orphaned, is killed by a kill raced against the wait, and the wait ends.
    #[cfg(target_os = "macos")]
    #[tokio::test(flavor = "multi_thread")]
    async fn a_kill_raced_against_the_wait_kills_an_orphaned_apple_binary() {
        let marker = JobMarker::new();
        let dir = tempfile::tempdir().expect("temp dir");
        let pid_file = dir.path().join("perl.pid");
        let script = format!(
            "/bin/sh -c \"/usr/bin/perl -e 'use POSIX; POSIX::setsid(); \
             open(F, \\\">{}\\\"); print F \\$\\$; close F; sleep 30' & sleep 2\" &",
            pid_file.display()
        );
        let mut root = spawn_group_leader(&script, &marker);
        let tree = JobTree::spawn(&TokioGlobal, ProcessScanner::default(), root.id(), &marker);
        wait_for_exit(root.id()).await.expect("root exit observed");
        let waiter = {
            let tree = tree.clone();
            tokio::spawn(async move { tree.wait_descendants_exit().await })
        };
        // Past the inner shell's exit: perl is a session leader with ppid 1
        // and no readable environment.
        tokio::time::sleep(Duration::from_millis(3500)).await;
        let perl: u32 = std::fs::read_to_string(&pid_file)
            .expect("perl wrote its pid")
            .trim()
            .parse()
            .expect("pid");
        assert!(alive(perl), "perl must outlive the shells");
        tree.kill()
            .await
            .expect("the kill reports success only if it worked");
        assert!(
            gone_within(perl, Duration::from_secs(2)).await,
            "perl {perl} survived a kill that returned Ok"
        );
        tokio::time::timeout(Duration::from_secs(5), waiter)
            .await
            .expect("the wait ends after the kill")
            .expect("the wait task joins")
            .expect("the wait succeeds");
        tree.finish().await;
        root.wait().expect("root is reaped");
    }

    /// A descendant that empties its environment loses the marker, and one
    /// that also leaves its session can no longer be found through its
    /// group either: only its ancestry — or, on Linux, the job's supervisor —
    /// still attributes it to the job. The kill must reach it anyway.
    #[cfg(all(unix, not(target_os = "linux")))]
    #[tokio::test(flavor = "multi_thread")]
    async fn an_env_cleaned_setsid_descendant_is_still_killed() {
        let marker = JobMarker::new();
        #[cfg(target_os = "macos")]
        let script = "/usr/bin/env -i /usr/bin/perl -e \
            'use POSIX; POSIX::setsid(); exec \"sleep\", \"300\"' & exit 0";
        #[cfg(not(target_os = "macos"))]
        let script = "/usr/bin/env -i sleep 300 & exit 0";
        let mut root = spawn_group_leader(script, &marker);
        let tree = JobTree::spawn(&TokioGlobal, ProcessScanner::default(), root.id(), &marker);
        wait_for_exit(root.id()).await.expect("root exit observed");
        let killed = tree.kill_descendants().await.expect("kill succeeds");
        assert_eq!(killed.len(), 1, "{killed:?}");
        assert!(
            gone_within(killed[0].pid, Duration::from_secs(2)).await,
            "the env-cleaned setsid child survived its job's kill"
        );
        tree.finish().await;
        root.wait().expect("root is reaped");
    }

    /// On Linux the same escape runs under the job's supervisor, which owns
    /// the descendants itself: neither marker nor ancestry is needed to
    /// name the leftover it killed.
    #[cfg(target_os = "linux")]
    #[tokio::test(flavor = "multi_thread")]
    async fn an_env_cleaned_setsid_descendant_is_still_killed() {
        let config = heel::SandboxConfig::builder()
            .heel_binary(crate::test_heel::pinned().await.unwrap())
            .network(heel::DenyAll)
            .security(heel::SecurityConfig::interactive())
            .supervised_spawn(true)
            .build();
        let sandbox = heel::Sandbox::with_config_and_executor(config, TokioGlobal)
            .await
            .expect("the sandbox builds");
        let mut child = sandbox
            .command("sh")
            .args(["-c", "/usr/bin/env -i setsid sleep 300 & exit 0"])
            .spawn_supervised(heel::supervisor::Foreground {
                reap_timeout: Duration::from_secs(5),
            })
            .await
            .expect("the supervised command spawns");
        let exit = child.wait().await.expect("the supervisor tears down");
        assert_eq!(exit.report.killed.len(), 1, "{:?}", exit.report.killed);
        // The name is read at kill time, and the background command passes
        // through `sh` (forked, before its exec), `env`, `setsid` and `sleep`.
        assert!(
            matches!(
                exit.report.killed[0].name.as_str(),
                "sh" | "env" | "setsid" | "sleep"
            ),
            "{:?}",
            exit.report.killed[0]
        );
        assert!(
            gone_within(exit.report.killed[0].pid, Duration::from_secs(2)).await,
            "the env-cleaned setsid child survived its job's teardown"
        );
    }

    /// A kill of a plain tree freezes and kills the group's members and the
    /// descendants that left it, and names them.
    #[tokio::test(flavor = "multi_thread")]
    async fn kill_descendants_names_what_it_killed() {
        let marker = JobMarker::new();
        let mut root = spawn_group_leader("sleep 300 & sleep 300 & exit 0", &marker);
        let tree = JobTree::spawn(&TokioGlobal, ProcessScanner::default(), root.id(), &marker);
        wait_for_exit(root.id()).await.expect("root exit observed");
        let killed = tree.kill_descendants().await.expect("kill succeeds");
        assert_eq!(killed.len(), 2, "{killed:?}");
        for JobProcess { pid, name } in &killed {
            assert_eq!(name, "sleep", "the name is read after the freeze");
            assert!(gone_within(*pid, Duration::from_secs(2)).await);
        }
        assert_eq!(tree.kill_descendants().await.expect("again"), []);
        tree.finish().await;
        root.wait().expect("root is reaped");
    }

    /// After the job's task ended, a kill does nothing and says so: the
    /// root's pid may be anyone's.
    #[tokio::test(flavor = "multi_thread")]
    async fn a_finished_tree_kills_nothing() {
        let marker = JobMarker::new();
        let mut root = spawn_group_leader("exit 0", &marker);
        let tree = JobTree::spawn(&TokioGlobal, ProcessScanner::default(), root.id(), &marker);
        wait_for_exit(root.id()).await.expect("root exit observed");
        tree.wait_descendants_exit().await.expect("nothing left");
        tree.finish().await;
        root.wait().expect("root is reaped");
        assert!(
            matches!(tree.kill().await, Err(KillTreeError::Ended)),
            "a kill after the tree ended is not a success"
        );
        assert_eq!(tree.kill_descendants().await.expect("nothing"), []);
    }

    /// A command still queued when the task ends is dropped with the task,
    /// so its asker learns the task ended instead of waiting forever, and a
    /// command sent afterwards is refused.
    #[tokio::test(flavor = "multi_thread")]
    async fn a_command_queued_behind_the_finish_is_not_left_unanswered() {
        let marker = JobMarker::new();
        let mut root = spawn_group_leader("exit 0", &marker);
        let (commands, inbox) = async_channel::unbounded();
        let task = Task::new(root.id(), marker.entry().1, ProcessScanner::default());
        let (ack, finished) = async_channel::bounded(1);
        let (reply, answer) = async_channel::bounded(1);
        commands
            .send(Command::Finish(ack))
            .await
            .expect("the task's inbox is open");
        commands
            .send(Command::Kill { group: true, reply })
            .await
            .expect("the task's inbox is open");
        task.run(inbox).await;
        finished.try_recv().expect("the finish is acknowledged");
        assert!(
            matches!(answer.try_recv(), Err(async_channel::TryRecvError::Closed)),
            "the queued kill's reply must be dropped with the task"
        );
        let (late, _) = async_channel::bounded(1);
        assert!(
            commands.send(Command::Finish(late)).await.is_err(),
            "a command sent after the task ended must be refused"
        );
        wait_for_exit(root.id()).await.expect("root exit observed");
        root.wait().expect("root is reaped");
    }

    /// A process that cannot be signalled is an error naming it, not a
    /// success. Signal 0 delivers nothing.
    #[test]
    fn a_process_that_cannot_be_signalled_is_an_error_naming_it() {
        let Some(result) = signal_foreign_for_test(0) else {
            panic!("no listed process belongs to another user");
        };
        match result.expect_err("the process is not ours to signal") {
            ProcessTableError::Signal { process, source } => {
                assert!(process.pid > 0);
                assert_ne!(process.name, "");
                assert_eq!(source.raw_os_error(), Some(libc::EPERM));
            }
            other => panic!("expected a signal error, got {other}"),
        }
    }

    /// Delivers every signal but `SIGKILL`: a process that does not die.
    struct IgnoresKill;

    impl Signals for IgnoresKill {
        fn member(&self, member: &Member, number: libc::c_int) -> Result<(), ProcessTableError> {
            if number == libc::SIGKILL {
                Ok(())
            } else {
                signal(member, number)
            }
        }

        fn group(&self, _root: &JobProcess, _number: libc::c_int) -> Result<(), ProcessTableError> {
            Ok(())
        }
    }

    /// A kill that leaves a tracked process alive is an error naming it,
    /// never a success.
    #[tokio::test(flavor = "multi_thread")]
    async fn a_kill_that_leaves_survivors_is_not_ok() {
        let marker = JobMarker::new();
        let mut root = spawn_group_leader("sleep 300 & exit 0", &marker);
        let (commands, inbox) = async_channel::unbounded();
        let task = Task::with_signals(
            IgnoresKill,
            root.id(),
            marker.entry().1,
            ProcessScanner::default(),
        );
        executor_core::Task::detach(executor_core::Executor::spawn(
            &TokioGlobal,
            task.run(inbox),
        ));
        let tree = JobTree {
            tracked: super::TrackedJob::Watched(commands),
        };
        wait_for_exit(root.id()).await.expect("root exit observed");
        let result = tree.kill_descendants().await;
        let survivors = match result {
            Err(ProcessTableError::Survived(survivors)) => survivors,
            other => panic!("expected the survivors as an error, got {other:?}"),
        };
        assert_eq!(survivors.len(), 1, "{survivors:?}");
        assert_eq!(survivors[0].name, "sleep");
        // SAFETY: the pid is a live child's of this test.
        unsafe { libc::kill(survivors[0].pid.cast_signed(), libc::SIGKILL) };
        assert!(gone_within(survivors[0].pid, Duration::from_secs(2)).await);
        tree.finish().await;
        root.wait().expect("root is reaped");
    }

    /// A start time or unique id that is not the process's own.
    const fn stale(birth: Birth) -> Birth {
        match birth {
            Birth::Ticks(ticks) => Birth::Ticks(ticks + 1),
            Birth::Unique(id) => Birth::Unique(id + 1),
            Birth::Started { seconds, micros } => Birth::Started {
                seconds: seconds + 1,
                micros,
            },
        }
    }

    /// The identity is checked right before a signal: a pid that now belongs
    /// to another process is not signalled.
    #[test]
    fn a_signal_for_a_reused_pid_reaches_nobody() {
        use std::os::unix::process::ExitStatusExt;

        let mut child = std::process::Command::new("/bin/sleep")
            .arg("30")
            .spawn()
            .expect("sleep should start");
        let table =
            platform::snapshot(platform::identity_mode().0, true).expect("the table is readable");
        let row = table
            .procs
            .iter()
            .find(|process| process.pid() == child.id())
            .expect("the table lists the child");
        let member = |birth| Member {
            identity: Identity {
                pid: row.pid(),
                birth,
            },
            name: row.name.clone(),
            zombie: false,
        };
        signal(&member(stale(row.identity.birth)), libc::SIGKILL)
            .expect("a pid that changed hands is no error");
        // A delivered SIGKILL ends a sleeping process at once; the pause is
        // only for the kernel to act on one.
        std::thread::sleep(Duration::from_millis(200));
        assert!(
            child.try_wait().expect("child is polled").is_none(),
            "the stale identity must not be signalled"
        );
        signal(&member(row.identity.birth), libc::SIGKILL).expect("the real one is signalled");
        let status = child.wait().expect("child is reaped");
        assert_eq!(status.signal(), Some(libc::SIGKILL));
    }

    /// Two reads of the table run together: neither waits for the other to
    /// finish, so the kills of several jobs are really concurrent.
    #[test]
    fn reads_of_the_process_table_do_not_wait_for_each_other() {
        let scanner = ProcessScanner::default();
        let (first_started, second_waits) = std::sync::mpsc::channel::<()>();
        let (second_started, first_waits) = std::sync::mpsc::channel::<()>();
        let patience = Duration::from_secs(5);
        std::thread::scope(|scope| {
            let scanner = &scanner;
            let first = scope.spawn(move || {
                scanner.table_with(Duration::ZERO, || {
                    first_started.send(()).expect("the other read listens");
                    first_waits
                        .recv_timeout(patience)
                        .expect("the other read must start while this one runs");
                    Ok(Table { procs: Vec::new() })
                })
            });
            let second = scope.spawn(move || {
                scanner.table_with(Duration::ZERO, || {
                    second_started.send(()).expect("the other read listens");
                    second_waits
                        .recv_timeout(patience)
                        .expect("the other read must start while this one runs");
                    Ok(Table { procs: Vec::new() })
                })
            });
            first.join().expect("first read").expect("table");
            second.join().expect("second read").expect("table");
        });
    }

    /// Reads the pid a helper wrote to `file`, waiting for it to appear.
    #[cfg(target_os = "macos")]
    async fn pid_from(file: &std::path::Path) -> u32 {
        let deadline = std::time::Instant::now() + Duration::from_secs(5);
        loop {
            if let Ok(text) = std::fs::read_to_string(file)
                && let Ok(pid) = text.trim().parse()
            {
                return pid;
            }
            assert!(
                std::time::Instant::now() < deadline,
                "{} was never written",
                file.display()
            );
            async_io::Timer::after(Duration::from_millis(20)).await;
        }
    }

    /// Kills `pid` whatever a test found, so a failed assertion leaks nothing.
    #[cfg(target_os = "macos")]
    fn kill_leftover(pid: u32) {
        // SAFETY: plain integers; a pid that is gone is no harm.
        unsafe { libc::kill(pid.cast_signed(), libc::SIGKILL) };
    }

    /// R3b: an Apple binary that calls `setsid`, started in the background as
    /// the shell's last command, while the shell exits at once. Watching the
    /// root from its spawn finds it before it leaves the group, and the job's
    /// kill ends it.
    #[cfg(target_os = "macos")]
    #[tokio::test(flavor = "multi_thread")]
    async fn a_setsid_apple_binary_started_as_the_last_command_is_killed() {
        let marker = JobMarker::new();
        let dir = tempfile::tempdir().expect("temp dir");
        let pid_file = dir.path().join("perl.pid");
        let script = format!(
            "/usr/bin/perl -e 'use POSIX; POSIX::setsid(); open(F, \">{}\"); \
             print F $$; close F; sleep 30' &",
            pid_file.display()
        );
        let mut root = spawn_group_leader(&script, &marker);
        let tree = JobTree::spawn(&TokioGlobal, ProcessScanner::default(), root.id(), &marker);
        wait_for_exit(root.id()).await.expect("root exit observed");
        let perl = pid_from(&pid_file).await;
        let killed = tree.kill_descendants().await;
        let gone = gone_within(perl, Duration::from_secs(2)).await;
        kill_leftover(perl);
        tree.finish().await;
        root.wait().expect("root is reaped");
        let killed = killed.expect("the kill succeeds");
        assert!(
            killed.iter().any(|process| process.pid == perl),
            "perl {perl} was not found: {killed:?}"
        );
        assert!(gone, "perl {perl} survived the kill");
    }

    /// R2b: a program that daemonizes the way `ssh -f` does (fork, the parent
    /// exits, the child calls `setsid`) while the shell goes on for a while.
    /// The job is not over while the daemon lives, and its kill ends it.
    #[cfg(target_os = "macos")]
    #[ignore = "approximation test: passed 199 of 200 runs with a scan on every event; the one failure came while other builds ran and its cause was not captured, so it stays out of the default run until a failure is captured"]
    #[tokio::test(flavor = "multi_thread")]
    async fn a_daemonizing_apple_binary_keeps_the_job_alive_and_is_killed() {
        let marker = JobMarker::new();
        let dir = tempfile::tempdir().expect("temp dir");
        let pid_file = dir.path().join("daemon.pid");
        let script = format!(
            "/usr/bin/perl -e 'use POSIX; fork and exit; POSIX::setsid(); \
             open(F, \">{}\"); print F $$; close F; sleep 30'; sleep 1",
            pid_file.display()
        );
        let mut root = spawn_group_leader(&script, &marker);
        let tree = JobTree::spawn(&TokioGlobal, ProcessScanner::default(), root.id(), &marker);
        wait_for_exit(root.id()).await.expect("root exit observed");
        let daemon = pid_from(&pid_file).await;
        let mut waiter = {
            let tree = tree.clone();
            tokio::spawn(async move { tree.wait_descendants_exit().await })
        };
        let early = tokio::time::timeout(Duration::from_secs(2), &mut waiter).await;
        let still_waiting = early.is_err();
        let killed = tree.kill().await;
        let gone = gone_within(daemon, Duration::from_secs(2)).await;
        kill_leftover(daemon);
        let finished = match early {
            Ok(done) => done,
            Err(_) => tokio::time::timeout(Duration::from_secs(5), &mut waiter)
                .await
                .expect("the wait ends after the kill"),
        };
        tree.finish().await;
        root.wait().expect("root is reaped");
        assert!(
            still_waiting,
            "the wait ended while the daemon {daemon} lived"
        );
        killed.expect("the kill succeeds");
        assert!(gone, "the daemon {daemon} survived the kill");
        finished
            .expect("the wait task joins")
            .expect("the wait succeeds");
    }
}