aboutsummaryrefslogtreecommitdiff
path: root/src/crosvm/sys/windows/broker.rs
blob: 8a93be8b51a35d0b540657e9d9edcf11310ff408 (plain)
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
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Contains the multi-process broker for crosvm. This is a work in progress, and some example
//! structs here are dead code.
#![allow(dead_code)]
use std::boxed::Box;
use std::collections::HashMap;
use std::env::current_exe;
use std::ffi::OsStr;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fs::OpenOptions;
use std::os::windows::io::AsRawHandle;
use std::os::windows::io::RawHandle;
use std::path::Path;
use std::path::PathBuf;
use std::process;
use std::process::Command;
use std::time::Duration;

use anyhow::anyhow;
use anyhow::Context;
use anyhow::Result;
use base::enable_high_res_timers;
use base::error;
#[cfg(feature = "crash-report")]
use base::generate_uuid;
use base::info;
use base::named_pipes;
use base::syslog;
use base::syslog::LogArgs;
use base::syslog::LogConfig;
use base::warn;
use base::AsRawDescriptor;
use base::BlockingMode;
use base::Descriptor;
use base::DuplicateHandleRequest;
use base::DuplicateHandleResponse;
use base::Event;
use base::EventToken;
use base::FramingMode;
use base::RawDescriptor;
use base::ReadNotifier;
use base::RecvTube;
use base::SafeDescriptor;
use base::SendTube;
#[cfg(feature = "gpu")]
use base::StreamChannel;
use base::Timer;
use base::TimerTrait;
use base::Tube;
use base::WaitContext;
#[cfg(feature = "process-invariants")]
use broker_ipc::init_broker_process_invariants;
use broker_ipc::CommonChildStartupArgs;
#[cfg(feature = "process-invariants")]
use broker_ipc::EmulatorProcessInvariants;
#[cfg(feature = "crash-report")]
use crash_report::product_type;
#[cfg(feature = "crash-report")]
use crash_report::CrashReportAttributes;
use crosvm_cli::bail_exit_code;
use crosvm_cli::ensure_exit_code;
use crosvm_cli::sys::windows::exit::to_process_type_error;
use crosvm_cli::sys::windows::exit::Exit;
use crosvm_cli::sys::windows::exit::ExitCode;
use crosvm_cli::sys::windows::exit::ExitCodeWrapper;
use crosvm_cli::sys::windows::exit::ExitContext;
use crosvm_cli::sys::windows::exit::ExitContextAnyhow;
#[cfg(feature = "audio")]
use devices::virtio::snd::parameters::Parameters as SndParameters;
#[cfg(feature = "gpu")]
use devices::virtio::vhost::user::device::gpu::sys::windows::GpuBackendConfig;
#[cfg(feature = "gpu")]
use devices::virtio::vhost::user::device::gpu::sys::windows::GpuVmmConfig;
#[cfg(feature = "gpu")]
use devices::virtio::vhost::user::device::gpu::sys::windows::InputEventBackendConfig;
#[cfg(feature = "gpu")]
use devices::virtio::vhost::user::device::gpu::sys::windows::InputEventSplitConfig;
#[cfg(feature = "gpu")]
use devices::virtio::vhost::user::device::gpu::sys::windows::InputEventVmmConfig;
#[cfg(feature = "gpu")]
use devices::virtio::vhost::user::device::gpu::sys::windows::WindowProcedureThreadSplitConfig;
#[cfg(feature = "gpu")]
use devices::virtio::vhost::user::device::gpu::sys::windows::WindowProcedureThreadVmmConfig;
#[cfg(feature = "audio")]
use devices::virtio::vhost::user::device::snd::sys::windows::SndBackendConfig;
#[cfg(feature = "audio")]
use devices::virtio::vhost::user::device::snd::sys::windows::SndSplitConfig;
#[cfg(feature = "audio")]
use devices::virtio::vhost::user::device::snd::sys::windows::SndVmmConfig;
#[cfg(feature = "net")]
use devices::virtio::vhost::user::device::NetBackendConfig;
use devices::virtio::DeviceType;
#[cfg(feature = "gpu")]
use gpu_display::EventDevice;
#[cfg(feature = "gpu")]
use gpu_display::WindowProcedureThread;
#[cfg(feature = "gpu")]
use gpu_display::WindowProcedureThreadBuilder;
use metrics::protos::event_details::EmulatorChildProcessExitDetails;
use metrics::protos::event_details::RecordDetails;
use metrics::MetricEventType;
#[cfg(all(feature = "net", feature = "slirp"))]
use net_util::slirp::sys::windows::SlirpStartupConfig;
#[cfg(all(feature = "net", feature = "slirp"))]
use net_util::slirp::sys::windows::SLIRP_BUFFER_SIZE;
use serde::Deserialize;
use serde::Serialize;
use tube_transporter::TubeToken;
use tube_transporter::TubeTransferData;
use tube_transporter::TubeTransporter;
use win_util::get_exit_code_process;
use win_util::ProcessType;
use winapi::shared::winerror::ERROR_ACCESS_DENIED;
use winapi::um::processthreadsapi::TerminateProcess;

#[cfg(feature = "gpu")]
use crate::sys::windows::get_gpu_product_configs;
#[cfg(feature = "audio")]
use crate::sys::windows::get_snd_product_configs;
#[cfg(feature = "gpu")]
use crate::sys::windows::get_window_procedure_thread_product_configs;
#[cfg(feature = "audio")]
use crate::sys::windows::num_input_sound_devices;
#[cfg(feature = "audio")]
use crate::sys::windows::num_input_sound_streams;
use crate::Config;

const KILL_CHILD_EXIT_CODE: u32 = 1;

/// Tubes created by the broker and sent to child processes via the bootstrap tube.
#[derive(Serialize, Deserialize)]
pub struct BrokerTubes {
    pub vm_evt_wrtube: SendTube,
    pub vm_evt_rdtube: RecvTube,
}

/// This struct represents a configured "disk" device as returned by the platform's API. There will
/// be two instances of it for each disk device, with the Tubes connected appropriately. The broker
/// will send one of these to the main process, and the other to the vhost user disk backend.
struct DiskDeviceEnd {
    bootstrap_tube: Tube,
    vhost_user: Tube,
}

/// Example of the function that would be in linux.rs.
fn platform_create_disks(_cfg: Config) -> Vec<(DiskDeviceEnd, DiskDeviceEnd)> {
    unimplemented!()
}

/// Time to wait after a process failure for the remaining processes to exit. When exceeded, all
/// remaining processes, except metrics, will be terminated.
const EXIT_TIMEOUT: Duration = Duration::from_secs(3);
/// Time to wait for the metrics process to flush and upload all logs.
const METRICS_TIMEOUT: Duration = Duration::from_secs(3);

/// DLLs that are known to interfere with crosvm.
#[cfg(feature = "sandbox")]
const BLOCKLIST_DLLS: &[&str] = &[
    "action_x64.dll",
    "AudioDevProps2.dll",
    "GridWndHook.dll",
    "Nahimic2OSD.dll",
    "NahimicOSD.dll",
    "TwitchNativeOverlay64.dll",
    "XSplitGameSource64.dll",
    "SS2OSD.dll",
    "nhAsusStrixOSD.dll",
];

/// Maps a process type to its sandbox policy configuration.
#[cfg(feature = "sandbox")]
fn process_policy(process_type: ProcessType, cfg: &Config) -> sandbox::policy::Policy {
    #[allow(unused_mut)]
    let mut policy = match process_type {
        ProcessType::Block => sandbox::policy::BLOCK,
        ProcessType::Main => main_process_policy(cfg),
        ProcessType::Metrics => sandbox::policy::METRICS,
        ProcessType::Net => sandbox::policy::NET,
        ProcessType::Slirp => slirp_process_policy(cfg),
        ProcessType::Gpu => sandbox::policy::GPU,
        ProcessType::Snd => sandbox::policy::SND,
        ProcessType::Broker => unimplemented!("No broker policy"),
        ProcessType::Spu => unimplemented!("No SPU policy"),
    };

    for dll in BLOCKLIST_DLLS.iter() {
        policy.dll_blocklist.push(dll.to_string());
    }

    #[cfg(feature = "asan")]
    adjust_asan_policy(&mut policy);
    #[cfg(feature = "cperfetto")]
    adjust_perfetto_policy(&mut policy);
    policy
}

/// Dynamically appends rules to the main process's policy.
#[cfg(feature = "sandbox")]
fn main_process_policy(cfg: &Config) -> sandbox::policy::Policy {
    let mut policy = sandbox::policy::MAIN;
    if let Some(host_guid) = &cfg.host_guid {
        let rule = sandbox::policy::Rule {
            subsystem: sandbox::SubSystem::SUBSYS_FILES,
            semantics: sandbox::Semantics::FILES_ALLOW_ANY,
            pattern: format!("\\??\\pipe\\{}\\vsock-*", host_guid),
        };
        policy.exceptions.push(rule);
    }
    policy
}

#[cfg(feature = "sandbox")]
fn slirp_process_policy(#[allow(unused)] cfg: &Config) -> sandbox::policy::Policy {
    #[allow(unused_mut)]
    let mut policy = sandbox::policy::SLIRP;

    #[cfg(any(feature = "slirp-ring-capture", feature = "slirp-debug"))]
    if let Some(path) = &cfg.slirp_capture_file {
        policy.exceptions.push(sandbox::policy::Rule {
            subsystem: sandbox::SubSystem::SUBSYS_FILES,
            semantics: sandbox::Semantics::FILES_ALLOW_ANY,
            pattern: path.to_owned(),
        });
    }

    policy
}

/// Adjust a policy to allow ASAN builds to write output files.
#[cfg(feature = "sandbox")]
fn adjust_asan_policy(policy: &mut sandbox::policy::Policy) {
    if (policy.initial_token_level as i32) < (sandbox::TokenLevel::USER_RESTRICTED_NON_ADMIN as i32)
    {
        policy.initial_token_level = sandbox::TokenLevel::USER_RESTRICTED_NON_ADMIN;
    }
    if (policy.integrity_level as i32) > (sandbox::IntegrityLevel::INTEGRITY_LEVEL_MEDIUM as i32) {
        policy.integrity_level = sandbox::IntegrityLevel::INTEGRITY_LEVEL_MEDIUM;
    }
}

/// Adjust a policy to allow perfetto tracing to open shared memory and use WASAPI.
#[cfg(feature = "sandbox")]
fn adjust_perfetto_policy(policy: &mut sandbox::policy::Policy) {
    if (policy.initial_token_level as i32)
        < (sandbox::TokenLevel::USER_RESTRICTED_SAME_ACCESS as i32)
    {
        policy.initial_token_level = sandbox::TokenLevel::USER_RESTRICTED_SAME_ACCESS;
    }

    if (policy.lockdown_token_level as i32)
        < (sandbox::TokenLevel::USER_RESTRICTED_SAME_ACCESS as i32)
    {
        policy.lockdown_token_level = sandbox::TokenLevel::USER_RESTRICTED_SAME_ACCESS;
    }

    if (policy.integrity_level as i32) > (sandbox::IntegrityLevel::INTEGRITY_LEVEL_MEDIUM as i32) {
        policy.integrity_level = sandbox::IntegrityLevel::INTEGRITY_LEVEL_MEDIUM;
    }

    if (policy.delayed_integrity_level as i32)
        > (sandbox::IntegrityLevel::INTEGRITY_LEVEL_MEDIUM as i32)
    {
        policy.delayed_integrity_level = sandbox::IntegrityLevel::INTEGRITY_LEVEL_MEDIUM;
    }
}

/// Wrapper that terminates a child process (if running) when dropped.
struct ChildCleanup {
    process_type: ProcessType,
    child: Box<dyn Child>,
    dh_tube: Option<Tube>,
}

#[derive(Debug)]
struct UnsandboxedChild(process::Child);
#[derive(Debug)]
struct SandboxedChild(SafeDescriptor);

impl AsRawDescriptor for UnsandboxedChild {
    fn as_raw_descriptor(&self) -> RawDescriptor {
        self.0.as_raw_handle()
    }
}

impl AsRawDescriptor for SandboxedChild {
    fn as_raw_descriptor(&self) -> RawDescriptor {
        self.0.as_raw_descriptor()
    }
}

impl Display for ChildCleanup {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{:?} {:?}", self.process_type, self.child)
    }
}

trait Child: std::fmt::Debug + AsRawDescriptor {
    fn wait(&mut self) -> std::io::Result<Option<ExitCode>>;
    fn try_wait(&mut self) -> std::io::Result<Option<ExitCode>>;
    fn kill(&mut self) -> std::io::Result<()>;
    // Necessary to upcast dyn Child to dyn AsRawDescriptor
    fn as_descriptor(&self) -> &dyn AsRawDescriptor;
}

impl Child for UnsandboxedChild {
    fn wait(&mut self) -> std::io::Result<Option<ExitCode>> {
        Ok(self.0.wait()?.code())
    }

    fn try_wait(&mut self) -> std::io::Result<Option<ExitCode>> {
        if let Some(status) = self.0.try_wait()? {
            Ok(status.code())
        } else {
            Ok(None)
        }
    }

    fn kill(&mut self) -> std::io::Result<()> {
        self.0.kill()
    }

    fn as_descriptor(&self) -> &dyn AsRawDescriptor {
        self
    }
}

impl Child for SandboxedChild {
    fn wait(&mut self) -> std::io::Result<Option<ExitCode>> {
        let wait_ctx = WaitContext::<u32>::new()?;
        wait_ctx.add(&self.0, 0)?;
        let _events = wait_ctx.wait()?;
        self.try_wait()
    }

    fn try_wait(&mut self) -> std::io::Result<Option<ExitCode>> {
        get_exit_code_process(self.0.as_raw_descriptor()).map(|code| code.map(|c| c as i32))
    }

    fn kill(&mut self) -> std::io::Result<()> {
        // TODO(b/315998194): Add safety comment
        #[allow(clippy::undocumented_unsafe_blocks)]
        if unsafe { TerminateProcess(self.0.as_raw_descriptor(), KILL_CHILD_EXIT_CODE) == 0 } {
            Err(std::io::Error::last_os_error())
        } else {
            Ok(())
        }
    }

    fn as_descriptor(&self) -> &dyn AsRawDescriptor {
        self
    }
}

impl Drop for ChildCleanup {
    fn drop(&mut self) {
        let kill_process = match self.child.try_wait() {
            Ok(None) => true,
            Ok(_) => false,
            Err(_) => true,
        };
        if kill_process {
            if let Err(e) = self.child.kill() {
                const ACCESS_DENIED: Option<i32> = Some(ERROR_ACCESS_DENIED as i32);
                if !matches!(e.raw_os_error(), ACCESS_DENIED) {
                    error!("Failed to clean up child process {}: {}", self, e);
                }
            }

            // Sending a kill signal does NOT imply the process has exited. Wait for it to exit.
            let wait_res = self.child.wait();
            if let Ok(Some(code)) = wait_res.as_ref() {
                warn!(
                    "child process {} killed, exited {}",
                    self,
                    ExitCodeWrapper(*code)
                );
            } else {
                error!(
                    "failed to wait for child process {} that was terminated: {:?}",
                    self, wait_res
                );
            }
        } else {
            info!("child process {} already terminated", self);
        }

        // Log child exit code regardless of whether we killed it or it exited
        // on its own.
        {
            // Don't even attempt to log metrics process, it doesn't exist to log
            // itself.
            if self.process_type != ProcessType::Metrics {
                let exit_code = self.child.wait();
                if let Ok(Some(exit_code)) = exit_code {
                    let mut details = RecordDetails::new();
                    let mut exit_details = EmulatorChildProcessExitDetails::new();
                    exit_details.set_exit_code(exit_code as u32);
                    exit_details.set_process_type(self.process_type.into());
                    details.emulator_child_process_exit_details = Some(exit_details).into();
                    metrics::log_event_with_details(MetricEventType::ChildProcessExit, &details);
                } else {
                    error!(
                        "Failed to log exit code for process: {:?}, couldn't get exit code",
                        self.process_type
                    );
                }
            }
        }
    }
}

/// Represents a child process spawned by the broker.
struct ChildProcess {
    // This is unused, but we hold it open to avoid an EPIPE in the child if it doesn't
    // immediately read its startup information. We don't use FlushFileBuffers to avoid this because
    // that would require blocking the startup sequence.
    tube_transporter: TubeTransporter,

    // Used to set up the child process. Unused in steady state.
    bootstrap_tube: Tube,
    // Child process PID.
    process_id: u32,
    alias_pid: u32,
}

/// Wrapper to start the broker.
pub fn run(cfg: Config, log_args: LogArgs) -> Result<()> {
    // This wrapper exists because errors that are returned up to the caller aren't logged, though
    // they are used to generate the return code. For practical debugging though, we want to log the
    // errors.
    let res = run_internal(cfg, log_args);
    if let Err(e) = &res {
        error!("Broker encountered an error: {}", e);
    }
    res
}

#[derive(EventToken)]
enum Token {
    Sigterm,
    Process(u32),
    MainExitTimeout,
    DeviceExitTimeout,
    MetricsExitTimeout,
    SigtermTimeout,
    DuplicateHandle(u32),
}

fn get_log_path(cfg: &Config, file_name: &str) -> Option<PathBuf> {
    cfg.logs_directory
        .as_ref()
        .map(|dir| Path::new(dir).join(file_name))
}

/// Creates a metrics tube pair for communication with the metrics process.
/// The returned Tube will be used by the process producing logs, while
/// the metric_tubes list is sent to the metrics process to receive logs.
///
/// IMPORTANT NOTE: The metrics process must receive the client (second) end
/// of the Tube pair in order to allow the connection to be properly shut
/// down without data loss.
fn metrics_tube_pair(metric_tubes: &mut Vec<Tube>) -> Result<Tube> {
    // TODO(nkgold): as written, this Tube pair won't handle ancillary data properly because the
    // PIDs are not set properly at each end; however, we don't plan to send ancillary data.
    let (t1, t2) = Tube::pair().exit_context(Exit::CreateTube, "failed to create tube")?;
    metric_tubes.push(t2);
    Ok(t1)
}

#[cfg(feature = "crash-report")]
pub fn create_crash_report_attrs(cfg: &Config, product_type: &str) -> CrashReportAttributes {
    crash_report::CrashReportAttributes {
        product_type: product_type.to_owned(),
        pipe_name: cfg.crash_pipe_name.clone(),
        report_uuid: cfg.crash_report_uuid.clone(),
        product_name: cfg.product_name.clone(),
        product_version: cfg.product_version.clone(),
    }
}

/// Setup crash reporting for a process. Each process MUST provide a unique `product_type` to avoid
/// making crash reports incomprehensible.
#[cfg(feature = "crash-report")]
pub fn setup_emulator_crash_reporting(cfg: &Config) -> Result<String> {
    crash_report::setup_crash_reporting(create_crash_report_attrs(
        cfg,
        crash_report::product_type::EMULATOR,
    ))
    .exit_context(
        Exit::CrashReportingInit,
        "failed to initialize crash reporting",
    )
}

/// Starts the broker, which in turn spawns the main process & vhost user devices.
/// General data flow for device & main process spawning:
///   Each platform (e.g. linux.rs) will provide create_inputs/gpus/nets.
///
///   Those functions will return a list of pairs of structs (containing the pipes and other
///   process specific configuration) for the VMM & backend sides of the device. These structs
///   should be minimal, and not duplicate information that is otherwise available in the Config
///   struct. There MAY be two different types per device, one for the VMM side, and another for
///   the backend.
///
///   The broker will send all the VMM structs to the main process, and the other structs
///   to the vhost user backends. Every process will get a copy of the Config struct.
///
///   Finally, the broker will wait on the child processes to exit, and handle errors.
///
/// Refrain from using platform specific code within this function. It will eventually be cross
/// platform.
fn run_internal(mut cfg: Config, log_args: LogArgs) -> Result<()> {
    #[cfg(feature = "sandbox")]
    if sandbox::is_sandbox_broker() {
        // Get the BrokerServices pointer so that it gets initialized.
        sandbox::BrokerServices::get()
            .exit_context(Exit::SandboxError, "sandbox operation failed")?;
    }
    // Note that parsing args causes syslog's log file to be set to the log file for the "main"
    // process. We don't want broker logs going there, so we fetch our own log file and set it here.
    let mut log_cfg = LogConfig {
        log_args: log_args.clone(),
        ..Default::default()
    };

    if let Some(log_path) = get_log_path(&cfg, "broker_syslog.log") {
        log_cfg.pipe = Some(Box::new(
            OpenOptions::new()
                .append(true)
                .create(true)
                .open(log_path.as_path())
                .with_exit_context(Exit::LogFile, || {
                    format!("failed to open log file {}", log_path.display())
                })?,
        ));
        log_cfg.log_args.stderr = false;
    } else {
        log_cfg.log_args.stderr = true;
    }
    syslog::init_with(log_cfg)?;

    #[cfg(feature = "process-invariants")]
    let process_invariants = init_broker_process_invariants(
        &cfg.process_invariants_data_handle,
        &cfg.process_invariants_data_size,
    )
    .exit_context(
        Exit::ProcessInvariantsInit,
        "failed to initialize process invariants",
    )?;

    #[cfg(feature = "crash-report")]
    init_broker_crash_reporting(&mut cfg)?;

    let _raise_timer_resolution = enable_high_res_timers()
        .exit_context(Exit::EnableHighResTimer, "failed to enable high res timers")?;

    // Note: in case of an error / scope exit, any children still in this map will be automatically
    // closed.
    let mut children: HashMap<u32, ChildCleanup> = HashMap::new();

    let mut exit_events = Vec::new();
    let mut wait_ctx: WaitContext<Token> = WaitContext::new()
        .exit_context(Exit::CreateWaitContext, "failed to create event context")?;

    // Hook ^C / SIGTERM so we can handle it gracefully.
    let sigterm_event = Event::new().exit_context(Exit::CreateEvent, "failed to create event")?;
    let sigterm_event_ctrlc = sigterm_event
        .try_clone()
        .exit_context(Exit::CloneEvent, "failed to clone event")?;
    ctrlc::set_handler(move || {
        sigterm_event_ctrlc.signal().unwrap();
    })
    .exit_context(Exit::SetSigintHandler, "failed to set sigint handler")?;
    wait_ctx.add(&sigterm_event, Token::Sigterm).exit_context(
        Exit::WaitContextAdd,
        "failed to add trigger to event context",
    )?;

    let mut metric_tubes = Vec::new();
    let metrics_controller = spawn_child(
        current_exe().unwrap().to_str().unwrap(),
        ["run-metrics"],
        get_log_path(&cfg, "metrics_stdout.log"),
        get_log_path(&cfg, "metrics_stderr.log"),
        ProcessType::Metrics,
        &mut children,
        &mut wait_ctx,
        /* skip_bootstrap= */
        #[cfg(test)]
        false,
        /* use_sandbox= */
        cfg.jail_config.is_some(),
        Vec::new(),
        &cfg,
    )?;
    metrics_controller
        .tube_transporter
        .serialize_and_transport(metrics_controller.process_id)
        .exit_context(Exit::TubeTransporterInit, "failed to initialize tube")?;

    let mut main_child = spawn_child(
        current_exe().unwrap().to_str().unwrap(),
        ["run-main"],
        get_log_path(&cfg, "main_stdout.log"),
        get_log_path(&cfg, "main_stderr.log"),
        ProcessType::Main,
        &mut children,
        &mut wait_ctx,
        /* skip_bootstrap= */
        #[cfg(test)]
        false,
        /* use_sandbox= */
        cfg.jail_config.is_some(),
        Vec::new(),
        &cfg,
    )?;

    // Save block children `ChildProcess` so TubeTransporter and Tubes don't get closed.
    let _block_children = start_up_block_backends(
        &mut cfg,
        &log_args,
        &mut children,
        &mut exit_events,
        &mut wait_ctx,
        &mut main_child,
        &mut metric_tubes,
        #[cfg(feature = "process-invariants")]
        &process_invariants,
    )?;

    #[cfg(all(feature = "net", feature = "slirp"))]
    let (_slirp_child, _net_children) = start_up_net_backend(
        &mut main_child,
        &mut children,
        &mut exit_events,
        &mut wait_ctx,
        &mut cfg,
        &log_args,
        &mut metric_tubes,
        #[cfg(feature = "process-invariants")]
        &process_invariants,
    )?;

    #[cfg(feature = "audio")]
    let snd_cfg = platform_create_snd(&cfg, &mut main_child, &mut exit_events)?;

    #[cfg(feature = "audio")]
    let _snd_child = if !cfg
        .vhost_user
        .iter()
        .any(|opt| opt.type_ == DeviceType::Sound)
    {
        // Pass both backend and frontend configs to main process.
        cfg.snd_split_config = Some(snd_cfg);
        None
    } else {
        Some(start_up_snd(
            &mut cfg,
            &log_args,
            snd_cfg,
            &mut main_child,
            &mut children,
            &mut wait_ctx,
            &mut metric_tubes,
            #[cfg(feature = "process-invariants")]
            &process_invariants,
        )?)
    };

    let (vm_evt_wrtube, vm_evt_rdtube) =
        Tube::directional_pair().context("failed to create vm event tube")?;

    #[cfg(feature = "gpu")]
    let (gpu_control_host_tube, gpu_control_device_tube) =
        Tube::pair().exit_context(Exit::CreateTube, "failed to create tube")?;

    #[cfg(feature = "gpu")]
    let mut input_event_split_config = platform_create_input_event_config(&cfg)
        .context("create input event devices for virtio-gpu device")?;

    #[cfg(feature = "gpu")]
    let mut window_procedure_thread_builder = Some(WindowProcedureThread::builder());

    #[cfg(feature = "gpu")]
    let gpu_cfg = platform_create_gpu(
        &cfg,
        &mut main_child,
        &mut exit_events,
        vm_evt_wrtube
            .try_clone()
            .exit_context(Exit::CloneEvent, "failed to clone event")?,
        gpu_control_host_tube,
        gpu_control_device_tube,
    )?;

    #[cfg(feature = "gpu")]
    let _gpu_child = if !cfg
        .vhost_user
        .iter()
        .any(|opt| opt.type_ == DeviceType::Gpu)
    {
        // Pass both backend and frontend configs to main process.
        cfg.gpu_backend_config = Some(gpu_cfg.0);
        cfg.gpu_vmm_config = Some(gpu_cfg.1);
        None
    } else {
        Some(start_up_gpu(
            &mut cfg,
            &log_args,
            gpu_cfg,
            &mut input_event_split_config,
            &mut main_child,
            &mut children,
            &mut wait_ctx,
            &mut metric_tubes,
            window_procedure_thread_builder
                .take()
                .ok_or_else(|| anyhow!("window_procedure_thread_builder is missing."))?,
            #[cfg(feature = "process-invariants")]
            &process_invariants,
        )?)
    };

    #[cfg(feature = "gpu")]
    {
        cfg.input_event_split_config = Some(input_event_split_config);
        if let Some(window_procedure_thread_builder) = window_procedure_thread_builder {
            cfg.window_procedure_thread_split_config = Some(
                platform_create_window_procedure_thread_configs(
                    &cfg,
                    window_procedure_thread_builder,
                    main_child.alias_pid,
                    main_child.alias_pid,
                )
                .context("Failed to create window procedure thread configs")?,
            );
        }
    }

    // Wait until all device processes are spun up so main TubeTransporter will have all the
    // device control and Vhost tubes.
    main_child
        .tube_transporter
        .serialize_and_transport(main_child.process_id)
        .exit_context(Exit::TubeTransporterInit, "failed to initialize tube")?;
    main_child.bootstrap_tube.send(&cfg).unwrap();

    let main_startup_args = CommonChildStartupArgs::new(
        &log_args,
        get_log_path(&cfg, "main_syslog.log"),
        #[cfg(feature = "crash-report")]
        create_crash_report_attrs(&cfg, product_type::EMULATOR),
        #[cfg(feature = "process-invariants")]
        process_invariants.clone(),
        Some(metrics_tube_pair(&mut metric_tubes)?),
    )?;
    main_child.bootstrap_tube.send(&main_startup_args).unwrap();

    let exit_event = Event::new().exit_context(Exit::CreateEvent, "failed to create event")?;
    main_child.bootstrap_tube.send(&exit_event).unwrap();
    exit_events.push(exit_event);

    let broker_tubes = BrokerTubes {
        vm_evt_wrtube,
        vm_evt_rdtube,
    };
    main_child.bootstrap_tube.send(&broker_tubes).unwrap();

    // Setup our own metrics agent
    {
        let broker_metrics = metrics_tube_pair(&mut metric_tubes)?;
        metrics::initialize(broker_metrics);

        #[cfg(feature = "gpu")]
        let use_vulkan = match &cfg.gpu_parameters {
            Some(params) => Some(params.use_vulkan),
            None => {
                warn!("No GPU parameters set on crosvm config.");
                None
            }
        };
        #[cfg(not(feature = "gpu"))]
        let use_vulkan = None;

        anti_tamper::setup_common_metric_invariants(
            &cfg.product_version,
            &cfg.product_channel,
            &use_vulkan.unwrap_or_default(),
        );
    }

    // We have all the metrics tubes from other children, so give them to the metrics controller
    // along with a startup configuration.
    let metrics_startup_args = CommonChildStartupArgs::new(
        &log_args,
        get_log_path(&cfg, "metrics_syslog.log"),
        #[cfg(feature = "crash-report")]
        create_crash_report_attrs(&cfg, product_type::METRICS),
        #[cfg(feature = "process-invariants")]
        process_invariants.clone(),
        None,
    )?;
    metrics_controller
        .bootstrap_tube
        .send(&metrics_startup_args)
        .unwrap();

    metrics_controller
        .bootstrap_tube
        .send(&metric_tubes)
        .unwrap();

    Supervisor::broker_supervise_loop(children, wait_ctx, exit_events)
}

/// Shuts down the metrics process, waiting for it to close to ensure
/// all logs are flushed.
fn clean_up_metrics(metrics_child: ChildCleanup) -> Result<()> {
    // This will close the final metrics connection, triggering a metrics
    // process shutdown.
    metrics::get_destructor().cleanup();

    // However, we still want to wait for the metrics process to finish
    // flushing any pending logs before exiting.
    let metrics_cleanup_wait = WaitContext::<u32>::new().exit_context(
        Exit::CreateWaitContext,
        "failed to create metrics wait context",
    )?;
    let mut metrics_timeout =
        Timer::new().exit_context(Exit::CreateTimer, "failed to create metrics timeout timer")?;
    metrics_timeout
        .reset(EXIT_TIMEOUT, None)
        .exit_context(Exit::ResetTimer, "failed to reset timer")?;
    metrics_cleanup_wait.add(&metrics_timeout, 0).exit_context(
        Exit::WaitContextAdd,
        "failed to add metrics timout to wait context",
    )?;
    metrics_cleanup_wait
        .add(metrics_child.child.as_descriptor(), 1)
        .exit_context(
            Exit::WaitContextAdd,
            "failed to add metrics process to wait context",
        )?;
    let events = metrics_cleanup_wait
        .wait()
        .context("failed to wait for metrics context")?;

    let mut process_exited = false;
    if events.iter().any(|e| e.is_readable && e.token == 1) {
        process_exited = true;
    }

    if !process_exited {
        warn!(
            "broker: Metrics process timed out before cleanly exiting.
            This may indicate some logs remain unsent."
        );
        // Process will be force-killed on drop
    }

    Ok(())
}

#[cfg(feature = "crash-report")]
fn init_broker_crash_reporting(cfg: &mut Config) -> Result<()> {
    cfg.crash_report_uuid = Some(generate_uuid());
    if cfg.crash_pipe_name.is_none() {
        // We weren't started by the service. Spin up a crash reporter to be shared with all
        // children.
        cfg.crash_pipe_name = Some(
            crash_report::setup_crash_reporting(create_crash_report_attrs(
                cfg,
                product_type::BROKER,
            ))
            .exit_context(Exit::CrashReportingInit, "failed to init crash reporting")?,
        );
    } else {
        crash_report::setup_crash_reporting(create_crash_report_attrs(cfg, product_type::BROKER))
            .exit_context(Exit::CrashReportingInit, "failed to init crash reporting")?;
    }

    Ok(())
}

struct Supervisor {
    children: HashMap<u32, ChildCleanup>,
    wait_ctx: WaitContext<Token>,
    exit_events: Vec<Event>,
    exit_timer: Option<Timer>,
}

impl Supervisor {
    pub fn broker_supervise_loop(
        children: HashMap<u32, ChildCleanup>,
        wait_ctx: WaitContext<Token>,
        exit_events: Vec<Event>,
    ) -> Result<()> {
        let mut supervisor = Supervisor {
            children,
            wait_ctx,
            exit_events,
            exit_timer: None,
        };
        let result = supervisor.broker_loop();

        // Once supervise loop exits, we are exiting and just need to clean
        // up. In error cases, there could still be children processes, so we close
        // those first, and finally drop the metrics process.
        supervisor.children.retain(|_, child| {
            match child.process_type {
                ProcessType::Metrics => true,
                _ => {
                    warn!(
                        "broker: Forcibly closing child (type: {:?}). This often means
                        the child was unable to close within the normal timeout window,
                        or the broker itself failed with an error.",
                        child.process_type
                    );
                    // Child killed on drop
                    false
                }
            }
        });

        {
            if supervisor.is_only_metrics_process_running() {
                clean_up_metrics(supervisor.children.into_values().next().unwrap())?;
            } else {
                warn!(
                    "broker: Metrics process not running after cleanup.
                    This may indicate some exit logs have been dropped."
                );
            }
        }

        result
    }

    /// We require exactly one main process.
    fn assert_children_sane(&mut self) {
        let main_processes = self
            .children
            .iter()
            .filter(|(_, child)| child.process_type == ProcessType::Main)
            .count();
        if main_processes != 1 {
            // Why do we have to clear children? Well, panic *can* cause destructors not to run,
            // which means these children won't run. The exact explanation for this isn't clear, but
            // it reproduced consistently. So since we're panicking, we'll be careful.
            self.children.clear();
            panic!(
                "Broker must supervise exactly one main process. Got {} main process(es).",
                main_processes,
            )
        }
    }

    fn is_only_metrics_process_running(&self) -> bool {
        self.children.len() == 1
            && self.children.values().next().unwrap().process_type == ProcessType::Metrics
    }

    fn all_non_metrics_processes_exited(&self) -> bool {
        self.children.is_empty() || self.is_only_metrics_process_running()
    }

    fn start_exit_timer(&mut self, timeout_token: Token) -> Result<()> {
        if self.exit_timer.is_some() {
            return Ok(());
        }

        let mut et = Timer::new().exit_context(Exit::CreateTimer, "failed to create timer")?;
        et.reset(EXIT_TIMEOUT, None)
            .exit_context(Exit::ResetTimer, "failed to reset timer")?;
        self.wait_ctx.add(&et, timeout_token).exit_context(
            Exit::WaitContextAdd,
            "failed to add trigger to wait context",
        )?;
        self.exit_timer = Some(et);

        Ok(())
    }

    /// Once children have been spawned, this function is called to run the supervision loop, which
    /// waits for processes to exit and handles errors.
    fn broker_loop(&mut self) -> Result<()> {
        const KILLED_BY_SIGNAL: ExitCode = Exit::KilledBySignal as ExitCode;
        self.assert_children_sane();
        let mut first_nonzero_exitcode = None;

        while !self.all_non_metrics_processes_exited() {
            let events = self
                .wait_ctx
                .wait()
                .context("failed to wait for event context")?;

            for event in events.iter().filter(|e| e.is_readable) {
                match event.token {
                    Token::Sigterm => {
                        // Signal all children other than metrics to exit.
                        for exit_event in &self.exit_events {
                            if let Err(e) = exit_event.signal() {
                                error!("failed to signal exit event to child: {}", e);
                            }
                        }
                        first_nonzero_exitcode.get_or_insert(KILLED_BY_SIGNAL);
                        self.start_exit_timer(Token::SigtermTimeout)?;
                    }
                    Token::Process(child_id) => {
                        let mut child = self.children.remove(&child_id).unwrap();
                        let process_handle = Descriptor(child.child.as_raw_descriptor());
                        self.wait_ctx.delete(&process_handle).exit_context(
                            Exit::WaitContextDelete,
                            "failed to remove trigger from event context",
                        )?;
                        if let Some(dh_tube) = child.dh_tube.as_ref() {
                            self.wait_ctx
                                .delete(dh_tube.get_read_notifier())
                                .exit_context(
                                    Exit::WaitContextDelete,
                                    "failed to remove trigger from event context",
                                )?;
                        }

                        let exit_code = child.child.wait().unwrap().unwrap();
                        info!(
                            "broker: child (type {:?}) exited {}",
                            child.process_type,
                            ExitCodeWrapper(exit_code),
                        );

                        // Save the child's exit code (to pass through to the broker's exit code) if
                        // none has been saved or if the previously saved exit code was
                        // KilledBySignal.  We overwrite KilledBySignal because the child exit may
                        // race with the sigterm from the service, esp if child exit is slowed by a Crashpad
                        // dump, and we don't want to lose the child's exit code if it was the
                        // initial cause of the emulator failing.
                        if exit_code != 0
                            && (first_nonzero_exitcode.is_none()
                                || matches!(first_nonzero_exitcode, Some(KILLED_BY_SIGNAL)))
                        {
                            info!(
                                "setting first_nonzero_exitcode {:?} -> {}",
                                first_nonzero_exitcode, exit_code,
                            );
                            first_nonzero_exitcode =
                                Some(to_process_type_error(exit_code as u32, child.process_type)
                                    as i32);
                        }

                        let timeout_token = match child.process_type {
                            ProcessType::Main => Token::MainExitTimeout,
                            ProcessType::Metrics => Token::MetricsExitTimeout,
                            _ => Token::DeviceExitTimeout,
                        };
                        self.start_exit_timer(timeout_token)?;
                    }
                    Token::SigtermTimeout => {
                        if let Some(exit_code) = first_nonzero_exitcode {
                            if exit_code != KILLED_BY_SIGNAL {
                                bail_exit_code!(
                                    exit_code,
                                    "broker got sigterm, but a child exited with an error.",
                                );
                            }
                        }
                        ensure_exit_code!(
                            self.all_non_metrics_processes_exited(),
                            Exit::BrokerSigtermTimeout,
                            "broker got sigterm, but other broker children did not exit within the \
                            timeout",
                        );
                    }
                    Token::MainExitTimeout => {
                        if let Some(exit_code) = first_nonzero_exitcode {
                            bail_exit_code!(
                                exit_code,
                                "main exited, but a child exited with an error.",
                            );
                        }
                        ensure_exit_code!(
                            self.all_non_metrics_processes_exited(),
                            Exit::BrokerMainExitedTimeout,
                            "main exited, but other broker children did not exit within the \
                            timeout",
                        );
                    }
                    Token::DeviceExitTimeout => {
                        // A device process exited, but there are still other processes running.
                        if let Some(exit_code) = first_nonzero_exitcode {
                            bail_exit_code!(
                                exit_code,
                                "a device exited, and either it or another child exited with an \
                                error.",
                            );
                        }
                        ensure_exit_code!(
                            self.all_non_metrics_processes_exited(),
                            Exit::BrokerDeviceExitedTimeout,
                            "device exited, but other broker children did not exit within the \
                            timeout",
                        );
                    }
                    Token::MetricsExitTimeout => {
                        // The metrics server exited, but there are still other processes running.
                        if let Some(exit_code) = first_nonzero_exitcode {
                            bail_exit_code!(
                                exit_code,
                                "metrics server exited, and either it or another child exited with \
                                an error.",
                            );
                        }
                        ensure_exit_code!(
                            self.children.is_empty(),
                            Exit::BrokerMetricsExitedTimeout,
                            "metrics exited, but other broker children did not exit within the \
                            timeout",
                        );
                    }
                    Token::DuplicateHandle(child_id) => {
                        if let Some(tube) = &self.children[&child_id].dh_tube {
                            let req: DuplicateHandleRequest = tube
                                .recv()
                                .exit_context(Exit::TubeFailure, "failed operation on tube")?;
                            if !self.children.contains_key(&req.target_alias_pid) {
                                error!(
                                    "DuplicateHandleRequest contained invalid alias pid: {}",
                                    req.target_alias_pid
                                );
                                tube.send(&DuplicateHandleResponse { handle: None })
                                    .exit_context(Exit::TubeFailure, "failed operation on tube")?;
                            } else {
                                let target = &self.children[&req.target_alias_pid].child;
                                let handle = win_util::duplicate_handle_from_source_process(
                                    self.children[&child_id].child.as_raw_descriptor(),
                                    req.handle as RawHandle,
                                    target.as_raw_descriptor(),
                                );
                                match handle {
                                    Ok(handle) => tube
                                        .send(&DuplicateHandleResponse {
                                            handle: Some(handle as usize),
                                        })
                                        .exit_context(
                                            Exit::TubeFailure,
                                            "failed operation on tube",
                                        )?,
                                    Err(e) => {
                                        error!("Failed to duplicate handle: {}", e);
                                        tube.send(&DuplicateHandleResponse { handle: None })
                                            .exit_context(
                                                Exit::TubeFailure,
                                                "failed operation on tube",
                                            )?
                                    }
                                };
                            }
                        }
                    }
                }
            }
        }

        if let Some(exit_code) = first_nonzero_exitcode {
            bail_exit_code!(
                exit_code,
                if exit_code == KILLED_BY_SIGNAL {
                    "broker got sigterm, and all children exited zero from shutdown event."
                } else {
                    "all processes exited, but at least one encountered an error."
                },
            );
        }

        Ok(())
    }
}

fn start_up_block_backends(
    cfg: &mut Config,
    log_args: &LogArgs,
    children: &mut HashMap<u32, ChildCleanup>,
    exit_events: &mut Vec<Event>,
    wait_ctx: &mut WaitContext<Token>,
    main_child: &mut ChildProcess,
    metric_tubes: &mut Vec<Tube>,
    #[cfg(feature = "process-invariants")] process_invariants: &EmulatorProcessInvariants,
) -> Result<Vec<ChildProcess>> {
    let mut block_children = Vec::new();
    let disk_options = cfg.disks.clone();
    for (index, disk_option) in disk_options.iter().enumerate() {
        let block_child = spawn_block_backend(index, main_child, children, wait_ctx, cfg)?;

        let startup_args = CommonChildStartupArgs::new(
            log_args,
            get_log_path(cfg, &format!("disk_{}_syslog.log", index)),
            #[cfg(feature = "crash-report")]
            create_crash_report_attrs(cfg, &format!("{}_{}", product_type::DISK, index)),
            #[cfg(feature = "process-invariants")]
            process_invariants.clone(),
            Some(metrics_tube_pair(metric_tubes)?),
        )?;
        block_child.bootstrap_tube.send(&startup_args).unwrap();

        block_child.bootstrap_tube.send(&disk_option).unwrap();

        let exit_event = Event::new().exit_context(Exit::CreateEvent, "failed to create event")?;
        block_child.bootstrap_tube.send(&exit_event).unwrap();
        exit_events.push(exit_event);
        block_children.push(block_child);
    }

    Ok(block_children)
}

fn spawn_block_backend(
    log_index: usize,
    main_child: &mut ChildProcess,
    children: &mut HashMap<u32, ChildCleanup>,
    wait_ctx: &mut WaitContext<Token>,
    cfg: &mut Config,
) -> Result<ChildProcess> {
    let (mut vhost_user_main_tube, mut vhost_user_device_tube) =
        Tube::pair().exit_context(Exit::CreateTube, "failed to create tube")?;

    let (mut disk_host_tube, mut disk_device_tube) =
        Tube::pair().exit_context(Exit::CreateTube, "failed to create tube")?;

    disk_device_tube.set_target_pid(main_child.alias_pid);
    vhost_user_device_tube.set_target_pid(main_child.alias_pid);
    let block_child = spawn_child(
        current_exe().unwrap().to_str().unwrap(),
        ["device", "block"],
        get_log_path(cfg, &format!("disk_{}_stdout.log", log_index)),
        get_log_path(cfg, &format!("disk_{}_stderr.log", log_index)),
        ProcessType::Block,
        children,
        wait_ctx,
        /* skip_bootstrap= */
        #[cfg(test)]
        false,
        /* use_sandbox= */
        cfg.jail_config.is_some(),
        vec![
            TubeTransferData {
                tube: disk_device_tube,
                tube_token: TubeToken::Control,
            },
            TubeTransferData {
                tube: vhost_user_device_tube,
                tube_token: TubeToken::VhostUser,
            },
        ],
        cfg,
    )?;

    block_child
        .tube_transporter
        .serialize_and_transport(block_child.process_id)
        .exit_context(Exit::TubeTransporterInit, "failed to initialize tube")?;

    vhost_user_main_tube.set_target_pid(block_child.alias_pid);
    disk_host_tube.set_target_pid(block_child.alias_pid);
    cfg.block_control_tube.push(disk_host_tube);
    cfg.block_vhost_user_tube.push(vhost_user_main_tube);

    Ok(block_child)
}

#[cfg(feature = "sandbox")]
fn spawn_sandboxed_child<I, S>(
    program: &str,
    args: I,
    stdout_file: Option<std::fs::File>,
    stderr_file: Option<std::fs::File>,
    handles_to_inherit: Vec<&dyn AsRawDescriptor>,
    process_policy: sandbox::policy::Policy,
) -> Result<(u32, Box<dyn Child>)>
where
    I: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
{
    let mut broker = sandbox::BrokerServices::get()
        .exit_context(Exit::SandboxError, "sandbox operation failed")?
        .unwrap();
    let mut policy = broker.create_policy();
    policy
        .set_token_level(
            process_policy.initial_token_level,
            process_policy.lockdown_token_level,
        )
        .exit_context(Exit::SandboxError, "sandbox operation failed")?;
    policy
        .set_job_level(process_policy.job_level, process_policy.ui_exceptions)
        .exit_context(Exit::SandboxError, "sandbox operation failed")?;
    policy
        .set_integrity_level(process_policy.integrity_level)
        .exit_context(Exit::SandboxError, "sandbox operation failed")?;
    policy
        .set_delayed_integrity_level(process_policy.delayed_integrity_level)
        .exit_context(Exit::SandboxError, "sandbox operation failed")?;

    if process_policy.alternate_desktop {
        policy
            .set_alternate_desktop(process_policy.alternate_winstation)
            .exit_context(Exit::SandboxError, "sandbox operation failed")?;
    }

    for rule in process_policy.exceptions {
        policy
            .add_rule(rule.subsystem, rule.semantics, rule.pattern)
            .exit_context(Exit::SandboxError, "sandbox operation failed")?;
    }

    policy.set_lockdown_default_dacl();

    if let Some(file) = stdout_file.as_ref() {
        policy
            .set_stdout_from_file(file)
            .exit_context(Exit::SandboxError, "sandbox operation failed")?;
    }

    if let Some(file) = stderr_file.as_ref() {
        policy
            .set_stderr_from_file(file)
            .exit_context(Exit::SandboxError, "sandbox operation failed")?;
    }

    for handle in handles_to_inherit.into_iter() {
        policy.add_handle_to_share(handle);
    }

    for dll in process_policy.dll_blocklist.into_iter() {
        policy
            .add_dll_to_unload(&dll)
            .exit_context(Exit::SandboxError, "sandbox operation failed")?;
    }

    // spawn_target uses CreateProcessW to create a new process, which will pass
    // the command line arguments verbatim to the new process. Most processes
    // expect that argv[0] will be the program name, so provide that before the
    // rest of the args.
    let command_line = args
        .into_iter()
        .fold(format!("\"{}\"", program), |mut args, arg| {
            args.push(' ');
            args.push_str(OsStr::new(&arg).to_str().unwrap());
            args
        });

    let (target, warning) = broker
        .spawn_target(program, &command_line, &policy)
        .exit_context(Exit::SandboxError, "sandbox operation failed")?;
    if let Some(w) = warning {
        warn!("sandbox: got warning spawning target: {}", w);
    }
    win_util::resume_thread(target.thread.as_raw_descriptor())
        .exit_context(Exit::ProcessSpawnFailed, "failed to spawn child process")?;

    Ok((target.process_id, Box::new(SandboxedChild(target.process))))
}

fn spawn_unsandboxed_child<I, S>(
    program: &str,
    args: I,
    stdout_file: Option<std::fs::File>,
    stderr_file: Option<std::fs::File>,
    handles_to_inherit: Vec<&dyn AsRawDescriptor>,
) -> Result<(u32, Box<dyn Child>)>
where
    I: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
{
    let mut proc = Command::new(program);

    let proc = proc.args(args);

    for handle in handles_to_inherit.iter() {
        win_util::set_handle_inheritance(handle.as_raw_descriptor(), /* inheritable= */ true)
            .exit_context(Exit::CreateSocket, "failed to create socket")?;
    }

    if let Some(file) = stdout_file {
        proc.stdout(file);
    }

    if let Some(file) = stderr_file {
        proc.stderr(file);
    }

    info!("spawning process: {:?}", proc);
    let proc = proc
        .spawn()
        .exit_context(Exit::ProcessSpawnFailed, "failed to spawn child process")?;

    for handle in handles_to_inherit.iter() {
        win_util::set_handle_inheritance(handle.as_raw_descriptor(), /* inheritable= */ false)
            .exit_context(Exit::CreateSocket, "failed to create socket")?;
    }

    let process_id = proc.id();

    Ok((process_id, Box::new(UnsandboxedChild(proc))))
}

#[cfg(all(feature = "net", feature = "slirp"))]
fn start_up_net_backend(
    main_child: &mut ChildProcess,
    children: &mut HashMap<u32, ChildCleanup>,
    exit_events: &mut Vec<Event>,
    wait_ctx: &mut WaitContext<Token>,
    cfg: &mut Config,
    log_args: &LogArgs,
    metric_tubes: &mut Vec<Tube>,
    #[cfg(feature = "process-invariants")] process_invariants: &EmulatorProcessInvariants,
) -> Result<(ChildProcess, ChildProcess)> {
    let (host_pipe, guest_pipe) = named_pipes::pair_with_buffer_size(
        &FramingMode::Message.into(),
        &BlockingMode::Blocking.into(),
        /* timeout= */ 0,
        /* buffer_size= */ SLIRP_BUFFER_SIZE,
        /* overlapped= */ true,
    )
    .expect("Failed to create named pipe pair.");
    let slirp_kill_event = Event::new().expect("Failed to create slirp kill event.");

    let slirp_child = spawn_slirp(children, wait_ctx, cfg)?;

    let slirp_child_startup_args = CommonChildStartupArgs::new(
        log_args,
        get_log_path(cfg, "slirp_syslog.log"),
        #[cfg(feature = "crash-report")]
        create_crash_report_attrs(cfg, product_type::SLIRP),
        #[cfg(feature = "process-invariants")]
        process_invariants.clone(),
        Some(metrics_tube_pair(metric_tubes)?),
    )?;
    slirp_child
        .bootstrap_tube
        .send(&slirp_child_startup_args)
        .unwrap();

    let slirp_config = SlirpStartupConfig {
        slirp_pipe: host_pipe,
        shutdown_event: slirp_kill_event
            .try_clone()
            .expect("Failed to clone slirp kill event."),
        #[cfg(any(feature = "slirp-ring-capture", feature = "slirp-debug"))]
        slirp_capture_file: cfg.slirp_capture_file.take(),
    };
    slirp_child.bootstrap_tube.send(&slirp_config).unwrap();

    let net_child = spawn_net_backend(main_child, children, wait_ctx, cfg)?;

    let net_child_startup_args = CommonChildStartupArgs::new(
        log_args,
        get_log_path(cfg, "net_syslog.log"),
        #[cfg(feature = "crash-report")]
        create_crash_report_attrs(cfg, product_type::SLIRP),
        #[cfg(feature = "process-invariants")]
        process_invariants.clone(),
        Some(metrics_tube_pair(metric_tubes)?),
    )?;
    net_child
        .bootstrap_tube
        .send(&net_child_startup_args)
        .unwrap();

    let net_backend_config = NetBackendConfig {
        guest_pipe,
        slirp_kill_event,
    };
    net_child.bootstrap_tube.send(&net_backend_config).unwrap();
    let exit_event = Event::new().exit_context(Exit::CreateEvent, "failed to create event")?;
    net_child.bootstrap_tube.send(&exit_event).unwrap();
    exit_events.push(exit_event);

    Ok((slirp_child, net_child))
}

fn spawn_slirp(
    children: &mut HashMap<u32, ChildCleanup>,
    wait_ctx: &mut WaitContext<Token>,
    cfg: &mut Config,
) -> Result<ChildProcess> {
    let slirp_child = spawn_child(
        current_exe().unwrap().to_str().unwrap(),
        ["run-slirp"],
        get_log_path(cfg, "slirp_stdout.log"),
        get_log_path(cfg, "slirp_stderr.log"),
        ProcessType::Slirp,
        children,
        wait_ctx,
        /* skip_bootstrap= */
        #[cfg(test)]
        false,
        /* use_sandbox= */ cfg.jail_config.is_some(),
        vec![],
        cfg,
    )?;

    slirp_child
        .tube_transporter
        .serialize_and_transport(slirp_child.process_id)
        .exit_context(Exit::TubeTransporterInit, "failed to initialize tube")?;

    Ok(slirp_child)
}

fn spawn_net_backend(
    main_child: &mut ChildProcess,
    children: &mut HashMap<u32, ChildCleanup>,
    wait_ctx: &mut WaitContext<Token>,
    cfg: &mut Config,
) -> Result<ChildProcess> {
    let (mut vhost_user_main_tube, mut vhost_user_device_tube) =
        Tube::pair().exit_context(Exit::CreateTube, "failed to create tube")?;

    vhost_user_device_tube.set_target_pid(main_child.alias_pid);

    let net_child = spawn_child(
        current_exe().unwrap().to_str().unwrap(),
        ["device", "net"],
        get_log_path(cfg, "net_stdout.log"),
        get_log_path(cfg, "net_stderr.log"),
        ProcessType::Net,
        children,
        wait_ctx,
        /* skip_bootstrap= */
        #[cfg(test)]
        false,
        /* use_sandbox= */ cfg.jail_config.is_some(),
        vec![TubeTransferData {
            tube: vhost_user_device_tube,
            tube_token: TubeToken::VhostUser,
        }],
        cfg,
    )?;

    net_child
        .tube_transporter
        .serialize_and_transport(net_child.process_id)
        .exit_context(Exit::TubeTransporterInit, "failed to initialize tube")?;

    vhost_user_main_tube.set_target_pid(net_child.alias_pid);
    cfg.net_vhost_user_tube = Some(vhost_user_main_tube);

    Ok(net_child)
}

/// Create backend and VMM configurations for the sound device.
#[cfg(feature = "audio")]
fn platform_create_snd(
    cfg: &Config,
    main_child: &mut ChildProcess,
    exit_events: &mut Vec<Event>,
) -> Result<SndSplitConfig> {
    let exit_event = Event::new().exit_context(Exit::CreateEvent, "failed to create exit event")?;
    exit_events.push(
        exit_event
            .try_clone()
            .exit_context(Exit::CloneEvent, "failed to clone event")?,
    );

    let (backend_config_product, vmm_config_product) =
        get_snd_product_configs(cfg, main_child.alias_pid)?;

    let parameters = SndParameters {
        backend: "winaudio".try_into().unwrap(),
        num_input_devices: num_input_sound_devices(cfg),
        num_input_streams: num_input_sound_streams(cfg),
        ..Default::default()
    };

    let backend_config = Some(SndBackendConfig {
        device_vhost_user_tube: None,
        exit_event,
        parameters,
        product_config: backend_config_product,
    });

    let vmm_config = Some(SndVmmConfig {
        main_vhost_user_tube: None,
        product_config: vmm_config_product,
    });

    Ok(SndSplitConfig {
        backend_config,
        vmm_config,
    })
}

/// Returns a snd child process for vhost-user sound.
#[cfg(feature = "audio")]
fn start_up_snd(
    cfg: &mut Config,
    log_args: &LogArgs,
    mut snd_cfg: SndSplitConfig,
    main_child: &mut ChildProcess,
    children: &mut HashMap<u32, ChildCleanup>,
    wait_ctx: &mut WaitContext<Token>,
    metric_tubes: &mut Vec<Tube>,
    #[cfg(feature = "process-invariants")] process_invariants: &EmulatorProcessInvariants,
) -> Result<ChildProcess> {
    // Extract the backend config from the sound config, so it can run elsewhere.
    let mut backend_cfg = snd_cfg
        .backend_config
        .take()
        .expect("snd backend config must be set");

    let (mut main_vhost_user_tube, mut device_host_user_tube) =
        Tube::pair().exit_context(Exit::CreateTube, "failed to create tube")?;

    let snd_child = spawn_child(
        current_exe().unwrap().to_str().unwrap(),
        ["device", "snd"],
        get_log_path(cfg, "snd_stdout.log"),
        get_log_path(cfg, "snd_stderr.log"),
        ProcessType::Snd,
        children,
        wait_ctx,
        /* skip_bootstrap= */
        #[cfg(test)]
        false,
        /* use_sandbox= */
        cfg.jail_config.is_some(),
        vec![],
        cfg,
    )?;

    snd_child
        .tube_transporter
        .serialize_and_transport(snd_child.process_id)
        .exit_context(Exit::TubeTransporterInit, "failed to initialize tube")?;

    // Update target PIDs to new child.
    device_host_user_tube.set_target_pid(main_child.alias_pid);
    main_vhost_user_tube.set_target_pid(snd_child.alias_pid);

    // Insert vhost-user tube to backend / frontend configs.
    backend_cfg.device_vhost_user_tube = Some(device_host_user_tube);
    if let Some(vmm_config) = snd_cfg.vmm_config.as_mut() {
        vmm_config.main_vhost_user_tube = Some(main_vhost_user_tube);
    }

    // Send VMM config to main process.
    cfg.snd_split_config = Some(snd_cfg);

    let startup_args = CommonChildStartupArgs::new(
        log_args,
        get_log_path(cfg, "snd_syslog.log"),
        #[cfg(feature = "crash-report")]
        create_crash_report_attrs(cfg, product_type::SND),
        #[cfg(feature = "process-invariants")]
        process_invariants.clone(),
        Some(metrics_tube_pair(metric_tubes)?),
    )?;
    snd_child.bootstrap_tube.send(&startup_args).unwrap();

    // Send backend config to Snd child.
    snd_child.bootstrap_tube.send(&backend_cfg).unwrap();

    Ok(snd_child)
}

#[cfg(feature = "gpu")]
fn platform_create_input_event_config(cfg: &Config) -> Result<InputEventSplitConfig> {
    let mut event_devices = vec![];
    let mut multi_touch_pipes = vec![];
    let mut mouse_pipes = vec![];
    let mut keyboard_pipes = vec![];

    for _ in cfg.virtio_multi_touch.iter() {
        let (event_device_pipe, virtio_input_pipe) =
            StreamChannel::pair(BlockingMode::Nonblocking, FramingMode::Byte)
                .exit_context(Exit::EventDeviceSetup, "failed to set up EventDevice")?;
        event_devices.push(EventDevice::touchscreen(event_device_pipe));
        multi_touch_pipes.push(virtio_input_pipe);
    }

    for _ in cfg.virtio_mice.iter() {
        let (event_device_pipe, virtio_input_pipe) =
            StreamChannel::pair(BlockingMode::Nonblocking, FramingMode::Byte)
                .exit_context(Exit::EventDeviceSetup, "failed to set up EventDevice")?;
        event_devices.push(EventDevice::mouse(event_device_pipe));
        mouse_pipes.push(virtio_input_pipe);
    }

    // One keyboard
    let (event_device_pipe, virtio_input_pipe) =
        StreamChannel::pair(BlockingMode::Nonblocking, FramingMode::Byte)
            .exit_context(Exit::EventDeviceSetup, "failed to set up EventDevice")?;
    event_devices.push(EventDevice::keyboard(event_device_pipe));
    keyboard_pipes.push(virtio_input_pipe);

    Ok(InputEventSplitConfig {
        backend_config: Some(InputEventBackendConfig { event_devices }),
        vmm_config: InputEventVmmConfig {
            multi_touch_pipes,
            mouse_pipes,
            keyboard_pipes,
        },
    })
}

#[cfg(feature = "gpu")]
/// Create Window procedure thread configurations.
fn platform_create_window_procedure_thread_configs(
    cfg: &Config,
    mut wndproc_thread_builder: WindowProcedureThreadBuilder,
    main_alias_pid: u32,
    device_alias_pid: u32,
) -> Result<WindowProcedureThreadSplitConfig> {
    let product_config = get_window_procedure_thread_product_configs(
        cfg,
        &mut wndproc_thread_builder,
        main_alias_pid,
        device_alias_pid,
    )
    .context("create product window procedure thread configs")?;
    Ok(WindowProcedureThreadSplitConfig {
        wndproc_thread_builder: Some(wndproc_thread_builder),
        vmm_config: WindowProcedureThreadVmmConfig { product_config },
    })
}

#[cfg(feature = "gpu")]
/// Create backend and VMM configurations for the GPU device.
fn platform_create_gpu(
    cfg: &Config,
    #[allow(unused_variables)] main_child: &mut ChildProcess,
    exit_events: &mut Vec<Event>,
    exit_evt_wrtube: SendTube,
    gpu_control_host_tube: Tube,
    gpu_control_device_tube: Tube,
) -> Result<(GpuBackendConfig, GpuVmmConfig)> {
    let exit_event = Event::new().exit_context(Exit::CreateEvent, "failed to create exit event")?;
    exit_events.push(
        exit_event
            .try_clone()
            .exit_context(Exit::CloneEvent, "failed to clone event")?,
    );

    let (backend_config_product, vmm_config_product) =
        get_gpu_product_configs(cfg, main_child.alias_pid)?;

    let backend_config = GpuBackendConfig {
        device_vhost_user_tube: None,
        exit_event,
        exit_evt_wrtube,
        gpu_control_device_tube,
        params: cfg
            .gpu_parameters
            .as_ref()
            .expect("missing GpuParameters in config")
            .clone(),
        product_config: backend_config_product,
    };

    let vmm_config = GpuVmmConfig {
        main_vhost_user_tube: None,
        gpu_control_host_tube: Some(gpu_control_host_tube),
        product_config: vmm_config_product,
    };

    Ok((backend_config, vmm_config))
}

#[cfg(feature = "gpu")]
/// Returns a gpu child process for vhost-user GPU.
fn start_up_gpu(
    cfg: &mut Config,
    log_args: &LogArgs,
    gpu_cfg: (GpuBackendConfig, GpuVmmConfig),
    input_event_cfg: &mut InputEventSplitConfig,
    main_child: &mut ChildProcess,
    children: &mut HashMap<u32, ChildCleanup>,
    wait_ctx: &mut WaitContext<Token>,
    metric_tubes: &mut Vec<Tube>,
    wndproc_thread_builder: WindowProcedureThreadBuilder,
    #[cfg(feature = "process-invariants")] process_invariants: &EmulatorProcessInvariants,
) -> Result<ChildProcess> {
    let (mut backend_cfg, mut vmm_cfg) = gpu_cfg;

    let (mut main_vhost_user_tube, mut device_host_user_tube) =
        Tube::pair().exit_context(Exit::CreateTube, "failed to create tube")?;

    let gpu_child = spawn_child(
        current_exe().unwrap().to_str().unwrap(),
        ["device", "gpu"],
        get_log_path(cfg, "gpu_stdout.log"),
        get_log_path(cfg, "gpu_stderr.log"),
        ProcessType::Gpu,
        children,
        wait_ctx,
        /* skip_bootstrap= */
        #[cfg(test)]
        false,
        /* use_sandbox= */
        cfg.jail_config.is_some(),
        vec![],
        cfg,
    )?;

    gpu_child
        .tube_transporter
        .serialize_and_transport(gpu_child.process_id)
        .exit_context(Exit::TubeTransporterInit, "failed to initialize tube")?;

    let mut wndproc_thread_cfg = platform_create_window_procedure_thread_configs(
        cfg,
        wndproc_thread_builder,
        main_child.alias_pid,
        gpu_child.alias_pid,
    )
    .context("failed to create window procedure thread configs")?;
    let wndproc_thread_builder = wndproc_thread_cfg
        .wndproc_thread_builder
        .take()
        .expect("The window procedure thread builder is missing");
    cfg.window_procedure_thread_split_config = Some(wndproc_thread_cfg);
    // Update target PIDs to new child.
    device_host_user_tube.set_target_pid(main_child.alias_pid);
    main_vhost_user_tube.set_target_pid(gpu_child.alias_pid);
    backend_cfg
        .gpu_control_device_tube
        .set_target_pid(main_child.alias_pid);
    vmm_cfg
        .gpu_control_host_tube
        .as_mut()
        .unwrap()
        .set_target_pid(gpu_child.alias_pid);

    // Insert vhost-user tube to backend / frontend configs.
    backend_cfg.device_vhost_user_tube = Some(device_host_user_tube);
    vmm_cfg.main_vhost_user_tube = Some(main_vhost_user_tube);

    // Send VMM config to main process. Note we don't set gpu_backend_config and
    // input_event_backend_config, since it is passed to the child.
    cfg.gpu_vmm_config = Some(vmm_cfg);
    let input_event_backend_config = input_event_cfg
        .backend_config
        .take()
        .context("input event backend config is missing.")?;

    let startup_args = CommonChildStartupArgs::new(
        log_args,
        get_log_path(cfg, "gpu_syslog.log"),
        #[cfg(feature = "crash-report")]
        create_crash_report_attrs(cfg, product_type::GPU),
        #[cfg(feature = "process-invariants")]
        process_invariants.clone(),
        Some(metrics_tube_pair(metric_tubes)?),
    )?;
    gpu_child.bootstrap_tube.send(&startup_args).unwrap();

    // Send backend config to GPU child.
    gpu_child
        .bootstrap_tube
        .send(&(
            backend_cfg,
            input_event_backend_config,
            wndproc_thread_builder,
        ))
        .unwrap();

    Ok(gpu_child)
}

/// Spawns a child process, sending it a control tube as the --bootstrap=HANDLE_NUMBER argument.
/// stdout & stderr are redirected to the provided file paths.
fn spawn_child<I, S>(
    program: &str,
    args: I,
    stdout_path: Option<PathBuf>,
    stderr_path: Option<PathBuf>,
    process_type: ProcessType,
    children: &mut HashMap<u32, ChildCleanup>,
    wait_ctx: &mut WaitContext<Token>,
    #[cfg(test)] skip_bootstrap: bool,
    use_sandbox: bool,
    mut tubes: Vec<TubeTransferData>,
    #[allow(unused_variables)] cfg: &Config,
) -> Result<ChildProcess>
where
    I: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
{
    let (tube_transport_pipe, tube_transport_main_child) = named_pipes::pair(
        &FramingMode::Message.into(),
        &BlockingMode::Blocking.into(),
        /* timeout= */ 0,
    )
    .exit_context(Exit::CreateSocket, "failed to create socket")?;

    let stdout_file = if let Some(path) = stdout_path {
        Some(
            OpenOptions::new()
                .append(true)
                .create(true)
                .open(path.as_path())
                .with_exit_context(Exit::LogFile, || {
                    format!("failed to open log file {}", path.display())
                })?,
        )
    } else {
        None
    };

    let stderr_file = if let Some(path) = stderr_path {
        Some(
            OpenOptions::new()
                .append(true)
                .create(true)
                .open(path.as_path())
                .with_exit_context(Exit::LogFile, || {
                    format!("failed to open log file {}", path.display())
                })?,
        )
    } else {
        None
    };

    #[cfg(test)]
    let bootstrap = if !skip_bootstrap {
        vec![
            "--bootstrap".to_string(),
            (tube_transport_main_child.as_raw_descriptor() as usize).to_string(),
        ]
    } else {
        vec![]
    };
    #[cfg(not(test))]
    let bootstrap = vec![
        "--bootstrap".to_string(),
        (tube_transport_main_child.as_raw_descriptor() as usize).to_string(),
    ];

    let input_args: Vec<S> = args.into_iter().collect();
    let args = input_args
        .iter()
        .map(|arg| arg.as_ref())
        .chain(bootstrap.iter().map(|arg| arg.as_ref()));

    #[cfg(feature = "sandbox")]
    let (process_id, child) = if use_sandbox {
        spawn_sandboxed_child(
            program,
            args,
            stdout_file,
            stderr_file,
            vec![&tube_transport_main_child],
            process_policy(process_type, cfg),
        )?
    } else {
        spawn_unsandboxed_child(
            program,
            args,
            stdout_file,
            stderr_file,
            vec![&tube_transport_main_child],
        )?
    };
    #[cfg(not(feature = "sandbox"))]
    let (process_id, child) = spawn_unsandboxed_child(
        program,
        args,
        stdout_file,
        stderr_file,
        vec![&tube_transport_main_child],
    )?;

    let (mut bootstrap_tube, bootstrap_tube_child) =
        Tube::pair().exit_context(Exit::CreateTube, "failed to create tube")?;

    // Make sure our end of the Tube knows the PID of the child end.
    bootstrap_tube.set_target_pid(process_id);

    tubes.push(TubeTransferData {
        tube: bootstrap_tube_child,
        tube_token: TubeToken::Bootstrap,
    });

    let (dh_tube, dh_tube_child, alias_pid) = if use_sandbox {
        let (broker, child) =
            Tube::pair().exit_context(Exit::CreateTube, "failed to create tube")?;
        (Some(broker), Some(child), rand::random())
    } else {
        (None, None, process_id)
    };

    let tube_transporter =
        TubeTransporter::new(tube_transport_pipe, tubes, Some(alias_pid), dh_tube_child);

    // Register this child to be waited upon.
    let process_handle = Descriptor(child.as_raw_descriptor());
    wait_ctx
        .add(&process_handle, Token::Process(alias_pid))
        .exit_context(
            Exit::WaitContextAdd,
            "failed to add trigger to event context",
        )?;

    children.insert(
        alias_pid,
        ChildCleanup {
            process_type,
            child,
            dh_tube,
        },
    );

    if use_sandbox {
        wait_ctx
            .add(
                children[&alias_pid]
                    .dh_tube
                    .as_ref()
                    .unwrap()
                    .get_read_notifier(),
                Token::DuplicateHandle(alias_pid),
            )
            .exit_context(
                Exit::WaitContextAdd,
                "failed to add trigger to event context",
            )?;
    }

    Ok(ChildProcess {
        bootstrap_tube,
        tube_transporter,
        process_id,
        alias_pid,
    })
}

#[cfg(test)]
mod tests {
    use base::thread::spawn_with_timeout;

    use super::*;

    /// Verifies that the supervisor loop exits normally with a single child that exits.
    #[test]
    fn smoke_test() {
        spawn_with_timeout(|| {
            let mut children: HashMap<u32, ChildCleanup> = HashMap::new();
            let mut wait_ctx: WaitContext<Token> = WaitContext::new().unwrap();
            let exit_events = vec![Event::new().unwrap()];
            let _child_main = spawn_child(
                "ping",
                ["127.0.0.1", "-n", "2"],
                None,
                None,
                ProcessType::Main,
                &mut children,
                &mut wait_ctx,
                /* skip_bootstrap= */ true,
                /* use_sandbox= */ false,
                Vec::new(),
                &Config::default(),
            );

            Supervisor::broker_supervise_loop(children, wait_ctx, exit_events).unwrap();
        })
        .try_join(Duration::from_secs(5))
        .unwrap();
    }

    /// Verifies that the supervisor loop exits normally when a device exits first, and then
    /// the main loop exits.
    #[test]
    fn main_and_device_clean_exit() {
        spawn_with_timeout(|| {
            let mut children: HashMap<u32, ChildCleanup> = HashMap::new();
            let mut wait_ctx: WaitContext<Token> = WaitContext::new().unwrap();
            let exit_events = vec![Event::new().unwrap()];
            let _child_main = spawn_child(
                "ping",
                ["127.0.0.1", "-n", "4"],
                None,
                None,
                ProcessType::Main,
                &mut children,
                &mut wait_ctx,
                /* skip_bootstrap= */ true,
                /* use_sandbox= */ false,
                Vec::new(),
                &Config::default(),
            );
            let _child_device = spawn_child(
                "ping",
                ["127.0.0.1", "-n", "2"],
                None,
                None,
                ProcessType::Block,
                &mut children,
                &mut wait_ctx,
                /* skip_bootstrap= */ true,
                /* use_sandbox= */ false,
                Vec::new(),
                &Config::default(),
            );

            Supervisor::broker_supervise_loop(children, wait_ctx, exit_events).unwrap();
        })
        .try_join(Duration::from_secs(5))
        .unwrap();
    }

    /// Verifies that the supervisor loop ends even if a device takes too long to exit.
    #[test]
    fn device_takes_too_long_to_exit() {
        spawn_with_timeout(|| {
            let mut children: HashMap<u32, ChildCleanup> = HashMap::new();
            let mut wait_ctx: WaitContext<Token> = WaitContext::new().unwrap();
            let exit_events = vec![Event::new().unwrap()];
            let _child_main = spawn_child(
                "ping",
                ["127.0.0.1", "-n", "2"],
                None,
                None,
                ProcessType::Main,
                &mut children,
                &mut wait_ctx,
                /* skip_bootstrap= */ true,
                /* use_sandbox= */ false,
                Vec::new(),
                &Config::default(),
            );
            let _child_device = spawn_child(
                "ping",
                ["127.0.0.1", "-n", "11"],
                None,
                None,
                ProcessType::Block,
                &mut children,
                &mut wait_ctx,
                /* skip_bootstrap= */ true,
                /* use_sandbox= */ false,
                Vec::new(),
                &Config::default(),
            );

            assert_eq!(
                Supervisor::broker_supervise_loop(children, wait_ctx, exit_events)
                    .to_exit_code()
                    .unwrap(),
                ExitCode::from(Exit::BrokerMainExitedTimeout),
            );
        })
        .try_join(Duration::from_secs(10))
        .unwrap();
    }

    /// Verifies that the supervisor loop ends even if the main process takes too long to exit.
    #[test]
    fn main_takes_too_long_to_exit() {
        spawn_with_timeout(|| {
            let mut children: HashMap<u32, ChildCleanup> = HashMap::new();
            let mut wait_ctx: WaitContext<Token> = WaitContext::new().unwrap();
            let exit_events = vec![Event::new().unwrap()];
            let _child_main = spawn_child(
                "ping",
                ["127.0.0.1", "-n", "11"],
                None,
                None,
                ProcessType::Main,
                &mut children,
                &mut wait_ctx,
                /* skip_bootstrap= */ true,
                /* use_sandbox= */ false,
                Vec::new(),
                &Config::default(),
            );
            let _child_device = spawn_child(
                "ping",
                ["127.0.0.1", "-n", "2"],
                None,
                None,
                ProcessType::Block,
                &mut children,
                &mut wait_ctx,
                /* skip_bootstrap= */ true,
                /* use_sandbox= */ false,
                Vec::new(),
                &Config::default(),
            );

            assert_eq!(
                Supervisor::broker_supervise_loop(children, wait_ctx, exit_events)
                    .to_exit_code()
                    .unwrap(),
                ExitCode::from(Exit::BrokerDeviceExitedTimeout),
            );
        })
        .try_join(Duration::from_secs(10))
        .unwrap();
    }

    /// Verifies that the supervisor loop ends even if a device takes too long to exit.
    #[test]
    fn device_crash_returns_child_error() {
        spawn_with_timeout(|| {
            let mut children: HashMap<u32, ChildCleanup> = HashMap::new();
            let mut wait_ctx: WaitContext<Token> = WaitContext::new().unwrap();
            let exit_events = vec![Event::new().unwrap()];
            let _child_main = spawn_child(
                "ping",
                ["127.0.0.1", "-n", "2"],
                None,
                None,
                ProcessType::Main,
                &mut children,
                &mut wait_ctx,
                /* skip_bootstrap= */ true,
                /* use_sandbox= */ false,
                Vec::new(),
                &Config::default(),
            );
            let _child_device = spawn_child(
                "cmd",
                ["/c", "exit -1"],
                None,
                None,
                ProcessType::Block,
                &mut children,
                &mut wait_ctx,
                /* skip_bootstrap= */ true,
                /* use_sandbox= */ false,
                Vec::new(),
                &Config::default(),
            );

            assert_eq!(
                Supervisor::broker_supervise_loop(children, wait_ctx, exit_events)
                    .to_exit_code()
                    .unwrap(),
                (to_process_type_error(-1i32 as u32, ProcessType::Block) as i32),
            );
        })
        .try_join(Duration::from_secs(10))
        .unwrap();
    }

    /// Verifies that sigterm makes the supervisor loop signal the exit event.
    #[test]
    fn sigterm_signals_exit_event() {
        let exit_event = Event::new().unwrap();
        let exit_event_copy = exit_event.try_clone().unwrap();

        spawn_with_timeout(move || {
            let sigterm_event = Event::new().unwrap();
            let mut wait_ctx: WaitContext<Token> = WaitContext::new().unwrap();
            let mut children: HashMap<u32, ChildCleanup> = HashMap::new();
            let _child_main = spawn_child(
                "ping",
                ["127.0.0.1", "-n", "3"],
                None,
                None,
                ProcessType::Main,
                &mut children,
                &mut wait_ctx,
                /* skip_bootstrap= */ true,
                /* use_sandbox= */ false,
                Vec::new(),
                &Config::default(),
            );
            wait_ctx.add(&sigterm_event, Token::Sigterm).unwrap();
            sigterm_event.signal().unwrap();

            assert_eq!(
                Supervisor::broker_supervise_loop(children, wait_ctx, vec![exit_event_copy])
                    .to_exit_code()
                    .unwrap(),
                ExitCode::from(Exit::KilledBySignal),
            );
        })
        .try_join(Duration::from_secs(10))
        .unwrap();

        exit_event.wait_timeout(Duration::from_secs(0)).unwrap();
    }
}