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
//! `process`: desktop subprocesses owned by one VM, and the VM's process
//! metadata (`platform`, `arch`, `env`, `dataDir`, `exit`). `nsh-js` makes
//! the module's default object the global `process` and adds `nextTick`
//! (a JS job).
//!
//! A child runs under a supervisor thread with a reader per pipe and a
//! writer for stdin; they never touch the VM. Output goes through a channel
//! to a task of the core, which posts it to the app's listeners, or, for
//! `run`, captures it. The VM's children are a [`Local`]: when the VM goes,
//! every child is killed at once and the supervisors are joined together.
use std::{
cell::{Cell, RefCell},
collections::BTreeMap,
rc::Rc,
};
use crate::ui::{
CallCx, Callback, Instance, JsError, JsObject, JsValue, Later, Local, PropEnum, PropError,
System, TsTypes, class, module,
};
/// Subprocesses owned by this app's VM, and its process metadata.
#[module]
pub(crate) struct Process;
#[module(flat, default)]
impl Process {
/// The native operating system's name, as Rust's `std::env::consts::OS`
/// and gpui-shell's say it: `"macos"`, `"windows"`, `"linux"`, `"ios"`,
/// `"android"` (not Node's `"darwin"` / `"win32"`).
#[allow(non_upper_case_globals)]
pub(crate) const platform: &'static str = std::env::consts::OS;
/// The native CPU architecture's name, as Rust's
/// `std::env::consts::ARCH` and gpui-shell's say it: `"aarch64"`,
/// `"x86_64"` (not Node's `"arm64"` / `"x64"`).
#[allow(non_upper_case_globals)]
pub(crate) const arch: &'static str = std::env::consts::ARCH;
/// Runs `command` (granted by capabilities.process.execute; no shell)
/// and captures up to 8 MiB per stream, with a default 30-second
/// deadline. A child's environment is PATH, HOME, USERPROFILE,
/// SystemRoot, TEMP, TMP, TMPDIR, LANG and LC_* from the host, plus the
/// names capabilities.process.env grants; never the whole host
/// environment. A refused command or option throws; a launch failure,
/// a timeout and output past the limit reject.
pub(crate) fn run(
command: String,
args: Option<Vec<String>>,
options: Option<RunOptions>,
sys: System,
cx: CallCx<'_>,
) -> Result<Later<CommandOutput>, JsError> {
let options = options.unwrap_or_default();
let launch = native::Launch::new(
&sys,
command,
args.unwrap_or_default(),
options.cwd,
timeout(options.timeout_ms)?.or(Some(RUN_TIMEOUT)),
)?;
#[cfg(not(target_arch = "wasm32"))]
return Ok(native::run(&sys, launch, cx));
#[cfg(target_arch = "wasm32")]
{
let _ = (launch, cx);
Err(JsError::Type(UNAVAILABLE.into()))
}
}
/// Starts `command` as `run` does and streams its decoded UTF-8 output,
/// or complete lines with `lines: true`; stdin is closed unless
/// `stdin: "pipe"`. Listeners get the event's payload only (gpui-shell's
/// also get a context).
pub(crate) fn spawn(
command: String,
args: Option<Vec<String>>,
options: Option<SpawnOptions>,
sys: System,
cx: CallCx<'_>,
) -> Result<ChildProcess, JsError> {
let options = options.unwrap_or_default();
let launch = native::Launch::new(
&sys,
command,
args.unwrap_or_default(),
options.cwd,
timeout(options.timeout_ms)?,
)?;
#[cfg(not(target_arch = "wasm32"))]
return native::spawn(
&sys,
launch,
options.stdin == Some(Stdin::Pipe),
options.lines.unwrap_or(false),
cx,
);
#[cfg(target_arch = "wasm32")]
{
let _ = (launch, cx);
Err(JsError::Type(UNAVAILABLE.into()))
}
}
/// Asks the host to exit (capabilities.process.exit); never exits the
/// host process directly.
pub(crate) fn exit(code: Option<i32>, sys: System) -> Result<(), JsError> {
if !sys.access.allows_exit() {
return Err(JsError::Type(
"process.exit() is not granted; set capabilities.process.exit to true in the \
manifest"
.into(),
));
}
let handler = sys.exit.ok_or_else(|| {
JsError::Error(
"process.exit() is granted but this host installed no exit request handler".into(),
)
})?;
handler(code.unwrap_or(0));
Ok(())
}
/// The environment variables the app may read: the default metadata
/// names and those capabilities.process.env grants; a new object each
/// time.
#[getter]
pub(crate) fn env(sys: System) -> BTreeMap<String, String> {
#[cfg(target_arch = "wasm32")]
{
let _ = sys;
BTreeMap::new()
}
#[cfg(not(target_arch = "wasm32"))]
{
std::env::vars_os()
.filter_map(|(name, value)| {
Some((name.into_string().ok()?, value.into_string().ok()?))
})
.filter(|(name, _)| sys.access.allows_env(name))
.collect()
}
}
/// The app's own data directory, `null` when the host has none.
#[getter]
pub(crate) fn data_dir(sys: System) -> Option<String> {
sys.data_dir.map(|dir| dir.to_string_lossy().into_owned())
}
}
/// Options of `run`.
#[derive(Debug, Clone, Default, JsObject)]
pub(crate) struct RunOptions {
/// An existing directory within a capabilities.fs.write root (the child
/// may write there); defaults to the app's data directory, created when
/// missing.
cwd: Option<String>,
/// Kills the process tree after this many milliseconds; `run` defaults
/// to 30 seconds.
timeout_ms: Option<f64>,
}
/// Options of `spawn`.
#[derive(Debug, Clone, Default, JsObject)]
pub(crate) struct SpawnOptions {
/// As `run`'s.
cwd: Option<String>,
/// As `run`'s; a spawned child has no deadline by default.
timeout_ms: Option<f64>,
/// `"pipe"` lets `write` and `end` feed stdin; it is closed otherwise.
stdin: Option<Stdin>,
/// Delivers complete lines (without `\n` or `\r\n`) instead of chunks.
lines: Option<bool>,
}
/// A child's stdin.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PropEnum)]
pub(crate) enum Stdin {
/// Written by `write`, closed by `end`.
Pipe,
/// Closed at once.
#[default]
Null,
}
/// What `run` resolves with.
#[derive(Debug, Clone, Default, JsObject)]
pub(crate) struct CommandOutput {
/// The exit status; -1 when a signal ended the child.
code: i32,
stdout: String,
stderr: String,
}
/// How a spawned child ended.
#[derive(Debug, Clone, Default, PartialEq, JsObject)]
pub(crate) struct ExitResult {
/// The exit status, `null` when a signal ended it.
code: Option<i32>,
/// The signal that ended it (`"SIGTERM"`).
signal: Option<String>,
/// Why the runtime ended it (a timeout, an over-long line); absent
/// otherwise.
#[prop(skip_none)]
error: Option<String>,
}
/// A child's events.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PropEnum)]
pub(crate) enum ChildEvent {
Stdout,
Stderr,
Exit,
}
/// The output events (`on`'s first overload).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PropEnum)]
pub(crate) enum OutputEvent {
Stdout,
Stderr,
}
/// The exit event (`on`'s second overload).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PropEnum)]
pub(crate) enum ExitEvent {
Exit,
}
/// What a listener gets: output text, or the exit.
#[derive(Debug, Clone)]
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
pub(crate) enum Payload {
Text(String),
Exit(ExitResult),
}
impl JsValue for Payload {
fn ts_type(types: &mut TsTypes) -> String {
format!("string | {}", ExitResult::ts_type(types))
}
fn from_js(value: crate::ui::JsIn<'_>) -> Result<Self, PropError> {
Err(value.expected("no value (a payload goes to JS only)"))
}
fn to_js<'a>(
&self,
host: &'a dyn crate::ui::JsHost,
) -> Result<crate::ui::JsOwned<'a>, PropError> {
match self {
Payload::Text(text) => text.to_js(host),
Payload::Exit(exit) => exit.to_js(host),
}
}
}
/// A child process started by `spawn`.
#[class]
pub(crate) struct ChildProcess {
pid: u32,
child: Rc<Child>,
}
#[class]
impl ChildProcess {
/// The child's process id.
#[getter]
pub(crate) fn pid(&self) -> u32 {
self.pid
}
/// Calls `handler` with each piece (or line) of output, or once with
/// the exit after the final output (at once when the child has exited).
#[overload(fn(event: OutputEvent, #[args(text)] handler: Callback<String>) -> Instance<ChildProcess>)]
#[overload(fn(event: ExitEvent, #[args(result)] handler: Callback<ExitResult>) -> Instance<ChildProcess>)]
pub(crate) fn on(
this: Instance<Self>,
event: ChildEvent,
#[args(payload)] handler: Callback<Payload>,
) -> Result<Instance<Self>, JsError> {
let child = this.with(|c| c.child.clone());
let exited = child.exited.borrow().clone();
if let Some(exit) = exited {
if event == ChildEvent::Exit {
handler.call(Payload::Exit(exit)).map_err(JsError::Error)?;
}
return Ok(this);
}
let mut listeners = child.listeners.borrow_mut();
let list = &mut listeners[event as usize];
if list.len() >= MAX_LISTENERS {
return Err(JsError::Type(format!(
"a child takes at most {MAX_LISTENERS} listeners per event"
)));
}
list.push(handler);
drop(listeners);
Ok(this)
}
/// Queues UTF-8 for stdin (`stdin: "pipe"`); at most 1 MiB may wait.
pub(crate) fn write(&self, text: String) -> Result<(), JsError> {
if !self.child.stdin_open.get() {
return Err(JsError::Type(
"ChildProcess.write needs stdin:\"pipe\" and an open stdin".into(),
));
}
#[cfg(not(target_arch = "wasm32"))]
return native::write(&self.child, text);
#[cfg(target_arch = "wasm32")]
{
let _ = text;
Err(JsError::Type(UNAVAILABLE.into()))
}
}
/// Closes stdin.
pub(crate) fn end(&self) {
if self.child.stdin_open.replace(false) {
#[cfg(not(target_arch = "wasm32"))]
native::close_stdin(&self.child);
}
}
/// Ends the whole process tree: SIGTERM to its process group, SIGKILL
/// after 250 ms; on Windows its job ends at once.
pub(crate) fn kill(&self) {
#[cfg(not(target_arch = "wasm32"))]
native::kill(&self.child);
}
}
/// `run`'s deadline when none is given.
const RUN_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);
const MAX_LISTENERS: usize = 64;
#[cfg(target_arch = "wasm32")]
const UNAVAILABLE: &str = "process is available on desktop hosts";
fn timeout(ms: Option<f64>) -> Result<Option<std::time::Duration>, JsError> {
match ms {
None => Ok(None),
Some(ms) if ms.is_finite() && ms > 0.0 && ms <= u32::MAX as f64 => {
Ok(Some(std::time::Duration::from_secs_f64(ms / 1000.0)))
}
Some(_) => Err(JsError::Type(
"process timeoutMs must be a positive number of milliseconds".into(),
)),
}
}
/// One spawned child as its object and its task see it.
pub(crate) struct Child {
/// stdout, stderr and exit listeners, released at the exit.
listeners: RefCell<[Vec<Callback<Payload>>; 3]>,
/// How it ended, once it has.
exited: RefCell<Option<ExitResult>>,
stdin_open: Cell<bool>,
#[cfg(not(target_arch = "wasm32"))]
control: native::Control,
}
/// The VM's children: killed and joined when the VM goes.
#[derive(Default)]
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
pub(crate) struct Children {
#[cfg(not(target_arch = "wasm32"))]
inner: native::Children,
/// Children started, for their ids.
next: Cell<u64>,
}
impl Local for Children {
fn close(&self) {
#[cfg(not(target_arch = "wasm32"))]
self.inner.close();
}
}
#[cfg(not(target_arch = "wasm32"))]
mod native {
use std::{
cell::{Cell, RefCell},
ffi::OsString,
io::{Read, Write},
path::{Path, PathBuf},
process::{Child as StdChild, Command, ExitStatus, Stdio},
rc::Rc,
sync::{
Arc,
atomic::{AtomicBool, AtomicUsize, Ordering},
mpsc,
},
thread::{self, JoinHandle},
time::{Duration, Instant},
};
use tokio::sync::mpsc as channel;
use super::{Child, ChildEvent, ChildProcess, CommandOutput, ExitResult, Payload};
use crate::{
access::{Access, env_names_match},
ui::{CallCx, Held, JsError, Later, System},
};
/// Output one VM holds in flight, and what `run` captures per stream
/// (and the longest line `lines: true` delivers).
const QUEUE_BYTES: usize = 8 * 1024 * 1024;
const MAX_CHILDREN: usize = 32;
/// The [`crate::ui::Quotas`] kind children count under.
const CHILDREN: &str = "process.children";
/// How long a child has to exit after it is asked to before it is killed.
const GRACE: Duration = Duration::from_millis(250);
/// How long a killed child may take to exit before its supervisor gives
/// it up: a child of another user (setuid) cannot be signalled.
const ABANDON: Duration = Duration::from_secs(2);
/// stdin queued for one child.
const MAX_STDIN: usize = 1024 * 1024;
/// Host variables every child gets; others only when
/// `capabilities.process.env` grants them. Names match regardless of
/// case; `LC_*` are included too.
const CHILD_ENV: &[&str] = &[
"PATH",
"HOME",
"USERPROFILE",
"SYSTEMROOT",
"TEMP",
"TMP",
"TMPDIR",
"LANG",
];
pub(super) enum Input {
Write(Vec<u8>),
Close,
Kill,
}
pub(super) enum Event {
Output(bool, String),
Exit(Option<i32>, Option<i32>, Option<String>),
}
/// The VM's side of its children.
#[derive(Default)]
pub(super) struct Children {
/// Set when the VM goes: supervisors kill at once, readers stop.
stopped: Arc<AtomicBool>,
/// Output queued for the VM, across its children.
queued: Arc<AtomicUsize>,
/// Each running child's id, input and supervisor.
running: RefCell<Vec<Running>>,
}
impl Children {
/// Every supervisor is told before any is joined, and kills at once
/// (no grace) when the VM stops, so teardown takes about as long as
/// one child does.
pub(super) fn close(&self) {
self.stopped.store(true, Ordering::Release);
let running = std::mem::take(&mut *self.running.borrow_mut());
for (_, input, ..) in &running {
let _ = input.send(Input::Kill);
}
for (_, _, worker, _) in running {
if let Some(worker) = worker {
let _ = worker.join();
}
}
}
/// Forgets a child that exited, joining its supervisor (which sent
/// the exit as its last act).
fn finished(&self, id: u64) {
let worker = {
let mut running = self.running.borrow_mut();
running
.iter()
.position(|(i, ..)| *i == id)
.and_then(|at| running.remove(at).2)
};
if let Some(worker) = worker {
let _ = worker.join();
}
}
}
/// A running child: its id, input, supervisor and place in the VM's
/// quota of children.
type Running = (u64, mpsc::Sender<Input>, Option<JoinHandle<()>>, Held);
/// A started child: its id and pid, its control, its events and the
/// VM's children it is one of.
type Started = (
u64,
u32,
Control,
channel::UnboundedReceiver<Event>,
Rc<super::Children>,
);
/// What the object and its task use to reach the child.
pub(super) struct Control {
input: mpsc::Sender<Input>,
queued_input: Arc<AtomicUsize>,
}
/// A checked launch: the command, its arguments, where and how long.
pub(super) struct Launch {
command: String,
args: Vec<String>,
cwd: PathBuf,
timeout: Option<Duration>,
env: Vec<(OsString, OsString)>,
}
impl Launch {
pub(super) fn new(
sys: &System,
command: String,
args: Vec<String>,
cwd: Option<String>,
timeout: Option<Duration>,
) -> Result<Self, JsError> {
if !cfg!(any(
target_os = "linux",
target_os = "macos",
target_os = "windows"
)) || !sys.os.is_desktop()
{
return Err(JsError::Type(
"process is available on desktop hosts".into(),
));
}
if command.is_empty() || command.contains('\0') {
return Err(JsError::Type("invalid executable".into()));
}
if !sys.access.allows_process(&command) {
return Err(spawn_error(
JsError::Type(format!("process capability denied for `{command}`"))
.with_code("EACCES"),
&command,
));
}
if args.len() > 4096 {
return Err(JsError::Type("too many arguments".into()));
}
let cwd = match cwd {
// A child may write where it runs: its directory needs a write grant.
Some(cwd) => sys
.access
.authorize_file_with_paths(
Path::new(&cwd),
&sys.data_dir.clone().unwrap_or_default(),
&sys.paths,
true,
)
.map_err(|e| {
JsError::Type(e)
.with_code("EACCES")
.with("path", cwd.as_str())
})?,
// The app's own data directory, which needs no grant.
None => {
let dir = sys.data_dir.clone().ok_or_else(|| {
JsError::Type(
"this host configured no app data directory for a default cwd; \
pass options.cwd"
.into(),
)
})?;
std::fs::create_dir_all(&dir).map_err(|e| {
JsError::Type(format!("could not create the app data directory: {e}"))
})?;
dir
}
};
if !cwd.is_dir() {
return Err(JsError::Type("cwd must be an existing directory".into()));
}
Ok(Self {
command,
args,
cwd,
timeout,
env: child_env(&sys.access),
})
}
}
/// The host variables a child gets: [`CHILD_ENV`], `LC_*`, and the names
/// both the app's `capabilities.process.env` and its host grant list.
fn child_env(access: &Access) -> Vec<(OsString, OsString)> {
let listed = |names: &[String], name: &str| names.iter().any(|n| env_names_match(n, name));
std::env::vars_os()
.filter(|(name, _)| {
let Some(name) = name.to_str() else {
return false;
};
CHILD_ENV.iter().any(|n| env_names_match(n, name))
|| name.get(..3).is_some_and(|p| env_names_match(p, "LC_"))
|| (listed(&access.declared.process.env, name)
&& access
.granted
.as_ref()
.is_none_or(|g| listed(&g.process.env, name)))
})
.collect()
}
/// Node's properties of a launch failure of `command`: `syscall`
/// (`spawn <command>`) and `path`.
pub(super) fn spawn_error(error: JsError, command: &str) -> JsError {
error
.with("syscall", format!("spawn {command}"))
.with("path", command)
}
fn resolve_executable(command: &str) -> Result<PathBuf, JsError> {
let path = Path::new(command);
if path.is_absolute() {
return executable(path);
}
if command.contains('/') || command.contains('\\') {
return Err(JsError::Type(
"use a bare executable name or an absolute path".into(),
));
}
let host_path = std::env::var_os("PATH")
.ok_or_else(|| JsError::Error("host PATH is unset".into()).with_code("ENOENT"))?;
for directory in std::env::split_paths(&host_path).filter(|p| p.is_absolute()) {
let candidate = directory.join(command);
if let Ok(path) = executable(&candidate) {
return Ok(path);
}
// Programs only, in PATHEXT's default order: a batch file runs
// through a granted cmd.exe, whose quoting the app then controls.
#[cfg(target_os = "windows")]
if candidate.extension().is_none() {
for extension in ["com", "exe"] {
if let Ok(path) = executable(&candidate.with_extension(extension)) {
return Ok(path);
}
}
}
}
Err(JsError::Error(format!(
"executable `{command}` was not found in the host PATH"
))
.with_code("ENOENT"))
}
fn executable(path: &Path) -> Result<PathBuf, JsError> {
let metadata = std::fs::metadata(path)?;
if !metadata.is_file() {
return Err(JsError::Error("executable is not a file".into()).with_code("EACCES"));
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if metadata.permissions().mode() & 0o111 == 0 {
return Err(JsError::Error("file is not executable".into()).with_code("EACCES"));
}
}
Ok(crate::access::canonicalize(path)?)
}
/// Launches `launch` under a supervisor: its id, pid, control and
/// events, or a launch failure with Node's properties (`code`,
/// `syscall`, `path`). The VM's quota of children is checked here.
fn start(sys: &System, launch: Launch, stdin: bool) -> Result<Started, JsError> {
let command = launch.command.clone();
launch_child(sys, launch, stdin).map_err(|e| spawn_error(e, &command))
}
fn launch_child(sys: &System, launch: Launch, stdin: bool) -> Result<Started, JsError> {
let children = sys.locals.get::<super::Children>();
let place = sys.quotas.take(CHILDREN, MAX_CHILDREN).ok_or_else(|| {
JsError::Range(format!("a VM may run at most {MAX_CHILDREN} children"))
})?;
let mut cmd = Command::new(resolve_executable(&launch.command)?);
cmd.env_clear()
.envs(launch.env)
.args(launch.args)
.current_dir(launch.cwd)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
cmd.process_group(0);
// The resolved file runs, under the name the app gave: a link's
// name picks its behavior (`clang++`, busybox applets, a venv's
// `python`).
cmd.arg0(&launch.command);
}
#[cfg(target_os = "windows")]
{
use std::os::windows::process::CommandExt;
// A console child gets no console window of its own, and starts
// suspended: it runs once it is in its job, so all it starts is.
cmd.creation_flags(
windows_sys::Win32::System::Threading::CREATE_NO_WINDOW
| windows_sys::Win32::System::Threading::CREATE_SUSPENDED,
);
}
let child = cmd.spawn().map_err(|e| {
let message = format!("could not launch process: {e}");
match crate::ui::io_code(&e) {
Some(code) => JsError::Error(message).with_code(code),
None => JsError::Error(message),
}
})?;
let pid = child.id();
let leader = Leader::start(child)
.map_err(|e| JsError::Error(format!("could not start the process in its job: {e}")))?;
let id = children.next.get() + 1;
children.next.set(id);
let (input, inputs) = mpsc::channel();
let (events_tx, events) = channel::unbounded_channel();
let mailbox = Mailbox {
tx: events_tx,
queued: children.inner.queued.clone(),
stopped: children.inner.stopped.clone(),
unreadable: Arc::new(AtomicBool::new(false)),
};
let queued_input = Arc::new(AtomicUsize::new(0));
let worker_input = queued_input.clone();
let timeout = launch.timeout;
// The leader goes to the supervisor once it runs: a child whose
// supervisor cannot start is killed here, not left running.
let (hand, handed) = mpsc::channel::<Leader>();
let worker = thread::Builder::new()
.name("nsh-process".into())
.spawn(move || {
if let Ok(leader) = handed.recv() {
supervise(leader, inputs, mailbox, timeout, worker_input);
}
});
let worker = match worker {
Ok(worker) => {
let _ = hand.send(leader);
worker
}
Err(e) => {
let mut leader = leader;
leader.terminate(true);
let _ = leader.reap();
return Err(JsError::Error(format!(
"could not supervise the process: {e}"
)));
}
};
if !stdin {
let _ = input.send(Input::Close);
}
children
.inner
.running
.borrow_mut()
.push((id, input.clone(), Some(worker), place));
Ok((
id,
pid,
Control {
input,
queued_input,
},
events,
children,
))
}
/// `run`: the captured output once the child exits.
pub(super) fn run(sys: &System, launch: Launch, cx: CallCx<'_>) -> Later<CommandOutput> {
let (id, _, control, mut events, children) = match start(sys, launch, false) {
Ok(started) => started,
Err(e) => return Later::new(async move { Err::<CommandOutput, _>(e) }),
};
let kill = control.input.clone();
let cleanup = cx.on_cleanup(move || {
let _ = kill.send(Input::Kill);
});
let queued = children.inner.queued.clone();
Later::new(async move {
let mut stdout = String::new();
let mut stderr = String::new();
let mut overflowed = false;
loop {
let Some(event) = events.recv().await else {
return Err("the process ended without an exit status".to_owned());
};
match event {
Event::Output(err, chunk) => {
queued.fetch_sub(chunk.len(), Ordering::AcqRel);
let captured = if err { &mut stderr } else { &mut stdout };
if overflowed {
} else if captured.len() + chunk.len() > QUEUE_BYTES {
overflowed = true;
let _ = control.input.send(Input::Kill);
} else {
captured.push_str(&chunk);
}
}
Event::Exit(code, _, error) => {
children.inner.finished(id);
cleanup.release();
if overflowed {
return Err(format!(
"process output exceeded {QUEUE_BYTES} bytes on one stream"
));
}
if let Some(error) = error {
return Err(error);
}
return Ok(CommandOutput {
code: code.unwrap_or(-1),
stdout,
stderr,
});
}
}
}
})
}
/// `spawn`: the child's object; a task of the core delivers its events.
pub(super) fn spawn(
sys: &System,
launch: Launch,
stdin: bool,
lines: bool,
cx: CallCx<'_>,
) -> Result<ChildProcess, JsError> {
let (id, pid, control, events, children) = start(sys, launch, stdin)?;
let kill = control.input.clone();
let child = Rc::new(Child {
listeners: RefCell::default(),
exited: RefCell::default(),
stdin_open: Cell::new(stdin),
control,
});
let cleanup = cx.on_cleanup(move || {
let _ = kill.send(Input::Kill);
});
cx.start(deliver(child.clone(), events, children, id, lines, cleanup));
Ok(ChildProcess { pid, child })
}
/// Posts a child's events to its listeners until it exits.
async fn deliver(
child: Rc<Child>,
mut events: channel::UnboundedReceiver<Event>,
children: Rc<super::Children>,
id: u64,
lines: bool,
cleanup: crate::ui::Cleanup,
) {
let queued = children.inner.queued.clone();
let mut pending = [String::new(), String::new()];
let mut line_error = None;
let post = |stream: usize, text: String| {
for listener in &child.listeners.borrow()[stream] {
listener.post(Payload::Text(text.clone()));
}
};
loop {
let Some(event) = events.recv().await else {
return;
};
match event {
Event::Output(err, chunk) => {
queued.fetch_sub(chunk.len(), Ordering::AcqRel);
let stream = usize::from(err);
if !lines {
post(stream, chunk);
continue;
}
let mut parts = chunk.split('\n').peekable();
while let Some(part) = parts.next() {
pending[stream].push_str(part);
if pending[stream].len() > QUEUE_BYTES {
line_error = Some(
"a line of output exceeded 8 MiB without a newline".to_owned(),
);
pending[stream].clear();
kill(&child);
break;
}
if parts.peek().is_some() {
let mut line = std::mem::take(&mut pending[stream]);
if line.ends_with('\r') {
line.pop();
}
post(stream, line);
}
}
}
Event::Exit(code, signal, error) => {
for (stream, rest) in pending.iter_mut().enumerate() {
if !rest.is_empty() {
post(stream, std::mem::take(rest));
}
}
children.inner.finished(id);
// An owner that lives long must not collect one cleanup
// per child it ever ran.
cleanup.release();
let exit = ExitResult {
code,
signal: signal.map(signal_name),
error: line_error.or(error),
};
*child.exited.borrow_mut() = Some(exit.clone());
child.stdin_open.set(false);
// The listeners go with the exit: nothing calls them again.
let listeners = std::mem::take(&mut *child.listeners.borrow_mut());
for listener in &listeners[ChildEvent::Exit as usize] {
listener.post(Payload::Exit(exit.clone()));
}
return;
}
}
}
}
pub(super) fn write(child: &Child, text: String) -> Result<(), JsError> {
let control = &child.control;
let len = text.len();
if control.queued_input.fetch_add(len, Ordering::AcqRel) + len > MAX_STDIN {
control.queued_input.fetch_sub(len, Ordering::AcqRel);
return Err(JsError::Type("stdin queue exceeded 1 MB".into()));
}
control
.input
.send(Input::Write(text.into_bytes()))
.map_err(|_| JsError::Type("process input is closed".into()))
}
pub(super) fn close_stdin(child: &Child) {
let _ = child.control.input.send(Input::Close);
}
pub(super) fn kill(child: &Child) {
let _ = child.control.input.send(Input::Kill);
}
/// A signal's name (`"SIGTERM"`).
fn signal_name(signal: i32) -> String {
#[cfg(unix)]
{
match signal {
libc::SIGKILL => "SIGKILL",
libc::SIGTERM => "SIGTERM",
libc::SIGINT => "SIGINT",
libc::SIGHUP => "SIGHUP",
libc::SIGQUIT => "SIGQUIT",
libc::SIGABRT => "SIGABRT",
libc::SIGSEGV => "SIGSEGV",
libc::SIGPIPE => "SIGPIPE",
_ => return format!("signal {signal}"),
}
.to_owned()
}
#[cfg(not(unix))]
{
format!("signal {signal}")
}
}
struct Mailbox {
tx: channel::UnboundedSender<Event>,
queued: Arc<AtomicUsize>,
stopped: Arc<AtomicBool>,
unreadable: Arc<AtomicBool>,
}
impl Mailbox {
/// Queues output, waiting while the VM already holds [`QUEUE_BYTES`].
fn output(&self, stderr: bool, chunk: String) -> bool {
let len = chunk.len();
loop {
if self.stopped.load(Ordering::Acquire) {
return false;
}
if self.queued.fetch_add(len, Ordering::AcqRel) + len <= QUEUE_BYTES {
break;
}
self.queued.fetch_sub(len, Ordering::AcqRel);
thread::sleep(Duration::from_millis(5));
}
if self.tx.send(Event::Output(stderr, chunk)).is_err() {
self.queued.fetch_sub(len, Ordering::AcqRel);
return false;
}
true
}
fn exit(&self, code: Option<i32>, signal: Option<i32>, error: Option<String>) {
let _ = self.tx.send(Event::Exit(code, signal, error));
}
}
#[cfg(unix)]
trait Pipe: std::os::fd::AsRawFd {}
#[cfg(unix)]
impl<T: std::os::fd::AsRawFd> Pipe for T {}
#[cfg(not(unix))]
trait Pipe {}
#[cfg(not(unix))]
impl<T> Pipe for T {}
fn prepare_pipe(pipe: &impl Pipe) -> std::io::Result<()> {
#[cfg(unix)]
{
// SAFETY: the borrowed pipe owns a live fd; preserve its other flags.
let flags = unsafe { libc::fcntl(pipe.as_raw_fd(), libc::F_GETFL) };
if flags < 0
// SAFETY: as above, with the flags just read.
|| unsafe { libc::fcntl(pipe.as_raw_fd(), libc::F_SETFL, flags | libc::O_NONBLOCK) }
< 0
{
return Err(std::io::Error::last_os_error());
}
}
#[cfg(not(unix))]
let _ = pipe;
Ok(())
}
fn read_pipe(
mut pipe: impl Read + Pipe,
mailbox: Arc<Mailbox>,
stderr: bool,
stop: Arc<AtomicBool>,
) {
if prepare_pipe(&pipe).is_err() {
mailbox.unreadable.store(true, Ordering::Release);
return;
}
let mut buffer = [0; 4096];
let mut pending = Vec::new();
loop {
if stop.load(Ordering::Acquire) {
break;
}
match pipe.read(&mut buffer) {
Ok(0) => break,
Ok(n) => {
pending.extend_from_slice(&buffer[..n]);
loop {
match std::str::from_utf8(&pending) {
Ok(s) => {
if !s.is_empty() && !mailbox.output(stderr, s.to_owned()) {
return;
}
pending.clear();
break;
}
Err(e) => {
let valid = e.valid_up_to();
if valid != 0 {
let s = String::from_utf8_lossy(&pending[..valid]).into_owned();
if !mailbox.output(stderr, s) {
return;
}
pending.drain(..valid);
}
if let Some(n) = e.error_len() {
pending.drain(..n);
if !mailbox.output(stderr, "\u{fffd}".into()) {
return;
}
} else {
break;
}
}
}
}
}
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
thread::sleep(Duration::from_millis(5));
}
Err(_) => break,
}
}
if !pending.is_empty() {
mailbox.output(stderr, String::from_utf8_lossy(&pending).into_owned());
}
}
fn write_pipe(
mut pipe: impl Write + Pipe,
input: mpsc::Receiver<Input>,
queued: Arc<AtomicUsize>,
stop: Arc<AtomicBool>,
) {
if prepare_pipe(&pipe).is_err() {
return;
}
while !stop.load(Ordering::Acquire) {
let command = match input.recv_timeout(Duration::from_millis(5)) {
Ok(command) => command,
Err(mpsc::RecvTimeoutError::Timeout) => continue,
Err(mpsc::RecvTimeoutError::Disconnected) => break,
};
let Input::Write(bytes) = command else { break };
queued.fetch_sub(bytes.len(), Ordering::AcqRel);
let mut remaining = bytes.as_slice();
while !remaining.is_empty() && !stop.load(Ordering::Acquire) {
match pipe.write(remaining) {
Ok(0) => return,
Ok(n) => remaining = &remaining[n..],
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
thread::sleep(Duration::from_millis(5));
}
Err(_) => return,
}
}
}
}
/// The child a supervisor owns: the leader of a process group (Unix) or
/// of a job (Windows). On Linux and Apple platforms its exit is
/// noticed without reaping it, so its pid, which is also the group's id,
/// stays taken (by the zombie) for as long as the group is signalled by
/// it.
struct Leader {
child: StdChild,
/// The status once the leader is reaped. On Unix its pid may then be
/// reused, and the group is no longer signalled.
reaped: Option<ExitStatus>,
/// Reaped by the system instead (a host that ignores SIGCHLD): its
/// pid, and the group it named, may be another's now, so nothing is
/// signalled or waited for any more.
#[cfg(unix)]
gone: bool,
#[cfg(target_vendor = "apple")]
exits: Option<std::os::fd::OwnedFd>,
/// The job the child and everything it starts run in: it ends them
/// all, and they end with its last handle, whatever ends the host.
#[cfg(target_os = "windows")]
job: std::os::windows::io::OwnedHandle,
}
impl Leader {
/// The leader of `child`, just spawned. On Windows the child starts
/// suspended and runs once it is in its job; a child that cannot be
/// put in one is killed.
fn start(child: StdChild) -> std::io::Result<Self> {
#[cfg(target_os = "windows")]
let job = match job::contain(&child) {
Ok(job) => job,
Err(e) => {
let mut child = child;
let _ = child.kill();
let _ = child.wait();
return Err(e);
}
};
Ok(Self {
#[cfg(target_vendor = "apple")]
exits: exit_queue(child.id()),
child,
reaped: None,
#[cfg(unix)]
gone: false,
#[cfg(target_os = "windows")]
job,
})
}
/// Whether the leader has exited; it stays unreaped where that can be
/// told.
fn exited(&mut self) -> std::io::Result<bool> {
if self.reaped.is_some() {
return Ok(true);
}
#[cfg(unix)]
if self.gone {
return Ok(true);
}
#[cfg(any(target_os = "linux", target_os = "android"))]
{
// SAFETY: an all-zero siginfo_t is valid; waitid fills it for this child.
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
// SAFETY: WNOWAIT leaves the child waitable; info outlives the call.
let result = unsafe {
libc::waitid(
libc::P_PID,
self.child.id() as libc::id_t,
&mut info,
libc::WEXITED | libc::WNOHANG | libc::WNOWAIT,
)
};
if result < 0 {
let error = std::io::Error::last_os_error();
if error.raw_os_error() == Some(libc::ECHILD) {
self.gone = true;
return Ok(true);
}
return Err(error);
}
// SAFETY: waitid succeeded, so si_pid is set (0 while the child runs).
Ok(unsafe { info.si_pid() } != 0)
}
#[cfg(not(any(target_os = "linux", target_os = "android")))]
{
#[cfg(target_vendor = "apple")]
if let Some(queue) = &self.exits {
use std::os::fd::AsRawFd;
// SAFETY: an all-zero kevent is a valid output buffer.
let mut event: libc::kevent = unsafe { std::mem::zeroed() };
let now = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
// SAFETY: polls the live queue for one event without blocking.
let n = unsafe {
libc::kevent(queue.as_raw_fd(), std::ptr::null(), 0, &mut event, 1, &now)
};
return if n < 0 {
Err(std::io::Error::last_os_error())
} else {
Ok(n > 0)
};
}
match self.child.try_wait() {
Ok(status) => self.reaped = status,
#[cfg(unix)]
Err(e) if e.raw_os_error() == Some(libc::ECHILD) => self.gone = true,
Err(e) => return Err(e),
}
#[cfg(unix)]
if self.gone {
return Ok(true);
}
Ok(self.reaped.is_some())
}
}
/// Asks the tree to stop (SIGTERM to the group), or forces it
/// (SIGKILL).
fn terminate(&mut self, force: bool) {
#[cfg(unix)]
if self.reaped.is_none() && !self.gone && !waitable(self.child.id()) {
self.gone = true;
}
#[cfg(unix)]
if self.reaped.is_none() && !self.gone {
// SAFETY: the child leads its own process group and is still
// this process's to wait for, so its pid still names that
// group.
unsafe {
libc::kill(
-(self.child.id() as i32),
if force { libc::SIGKILL } else { libc::SIGTERM },
);
}
}
// Windows has no signal to ask with: the job ends the whole tree,
// the leader and every process it started, at once.
#[cfg(target_os = "windows")]
{
let _ = force;
job::terminate(&self.job);
}
#[cfg(not(any(unix, target_os = "windows")))]
{
let _ = force;
let _ = self.child.kill();
}
}
fn reap(mut self) -> Option<ExitStatus> {
#[cfg(unix)]
if self.gone {
return None;
}
self.reaped.take().or_else(|| self.child.wait().ok())
}
}
/// The Windows job of a child: what ends its whole tree.
#[cfg(target_os = "windows")]
mod job {
use std::{
ffi::c_void,
io,
os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle},
thread::JoinHandle,
};
use windows_sys::Win32::{
Foundation::INVALID_HANDLE_VALUE,
System::{
Diagnostics::ToolHelp::{
CreateToolhelp32Snapshot, TH32CS_SNAPTHREAD, THREADENTRY32, Thread32First,
Thread32Next,
},
IO::CancelSynchronousIo,
JobObjects::{
AssignProcessToJobObject, CreateJobObjectW, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JobObjectExtendedLimitInformation,
SetInformationJobObject, TerminateJobObject,
},
Threading::{OpenThread, ResumeThread, THREAD_SUSPEND_RESUME},
},
};
/// Puts suspended `child` in a new job, which ends everything in it
/// when its last handle closes, then lets the child run.
pub(super) fn contain(child: &std::process::Child) -> io::Result<OwnedHandle> {
// SAFETY: no name and default security; the handle is owned below.
let raw = unsafe { CreateJobObjectW(std::ptr::null(), std::ptr::null()) };
if raw.is_null() {
return Err(io::Error::last_os_error());
}
// SAFETY: a new handle nothing else owns.
let job = unsafe { OwnedHandle::from_raw_handle(raw) };
// SAFETY: an all-zero limit information is valid; one flag is set.
let mut limits: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = unsafe { std::mem::zeroed() };
limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
// SAFETY: the live job; the structure and its size match the class.
let set = unsafe {
SetInformationJobObject(
job.as_raw_handle(),
JobObjectExtendedLimitInformation,
&limits as *const _ as *const c_void,
std::mem::size_of::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>() as u32,
)
};
if set == 0 {
return Err(io::Error::last_os_error());
}
// SAFETY: the live job and the child's live process handle.
if unsafe { AssignProcessToJobObject(job.as_raw_handle(), child.as_raw_handle()) } == 0
{
return Err(io::Error::last_os_error());
}
resume(child.id())?;
Ok(job)
}
/// Resumes the threads of suspended process `pid`: its one, main
/// thread (`std` keeps no handle to it).
fn resume(pid: u32) -> io::Result<()> {
// SAFETY: a snapshot of the system's threads; owned below.
let raw = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0) };
if raw == INVALID_HANDLE_VALUE {
return Err(io::Error::last_os_error());
}
// SAFETY: a new handle nothing else owns.
let snapshot = unsafe { OwnedHandle::from_raw_handle(raw) };
// SAFETY: an all-zero entry is valid once its size is set.
let mut entry: THREADENTRY32 = unsafe { std::mem::zeroed() };
entry.dwSize = std::mem::size_of::<THREADENTRY32>() as u32;
let mut resumed = false;
// SAFETY: the live snapshot fills the entry it is given.
let mut more = unsafe { Thread32First(snapshot.as_raw_handle(), &mut entry) } != 0;
while more {
if entry.th32OwnerProcessID == pid {
// SAFETY: opens the thread by id; owned below when opened.
let thread =
unsafe { OpenThread(THREAD_SUSPEND_RESUME, 0, entry.th32ThreadID) };
if !thread.is_null() {
// SAFETY: a new handle nothing else owns.
let thread = unsafe { OwnedHandle::from_raw_handle(thread) };
// SAFETY: the live thread handle, opened to resume it.
resumed |= unsafe { ResumeThread(thread.as_raw_handle()) } != u32::MAX;
}
}
// SAFETY: as above.
more = unsafe { Thread32Next(snapshot.as_raw_handle(), &mut entry) } != 0;
}
if resumed {
Ok(())
} else {
Err(io::Error::other("its main thread could not be resumed"))
}
}
/// Ends every process in `job`.
pub(super) fn terminate(job: &OwnedHandle) {
// SAFETY: the live job handle.
unsafe { TerminateJobObject(job.as_raw_handle(), 1) };
}
/// Cancels the blocking read or write `thread` is in, if any.
pub(super) fn cancel_io(thread: &JoinHandle<()>) {
// SAFETY: the live thread's handle, which the JoinHandle holds.
unsafe { CancelSynchronousIo(thread.as_raw_handle()) };
}
}
/// Joins the threads that end by `deadline`. One still blocked then (on a
/// pipe a process outside the tree keeps open) ends by itself later;
/// waiting for it could hold the VM's teardown forever.
pub(super) fn join_by(threads: Vec<JoinHandle<()>>, deadline: Instant) {
for thread in threads {
while !thread.is_finished() && Instant::now() < deadline {
// A blocking read or write of an anonymous pipe has no
// timeout on Windows: it is cancelled.
#[cfg(target_os = "windows")]
job::cancel_io(&thread);
thread::sleep(Duration::from_millis(1));
}
if thread.is_finished() {
let _ = thread.join();
} else {
log::warn!("a process pipe thread is still blocked; it is left to end by itself");
}
}
}
/// Whether `pid` is still this process's child to wait for: running, or
/// exited and not reaped. False once the system reaped it (a host that
/// ignores SIGCHLD; `ECHILD`), when its pid may be another's.
#[cfg(unix)]
pub(super) fn waitable(pid: u32) -> bool {
// SAFETY: an all-zero siginfo_t is valid; waitid fills it.
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
// SAFETY: WNOWAIT | WNOHANG neither reaps nor blocks; info outlives the call.
let result = unsafe {
libc::waitid(
libc::P_PID,
pid as libc::id_t,
&mut info,
libc::WEXITED | libc::WNOHANG | libc::WNOWAIT,
)
};
result == 0 || std::io::Error::last_os_error().raw_os_error() != Some(libc::ECHILD)
}
/// A kqueue that reports the child's exit without reaping it; `None` when
/// it cannot be made, and then the exit is noticed by reaping.
#[cfg(target_vendor = "apple")]
fn exit_queue(pid: u32) -> Option<std::os::fd::OwnedFd> {
use std::os::fd::{AsRawFd, FromRawFd};
// SAFETY: kqueue takes no arguments and returns a new descriptor or -1.
let fd = unsafe { libc::kqueue() };
if fd < 0 {
return None;
}
// SAFETY: kqueue returned a descriptor nothing else owns.
let queue = unsafe { std::os::fd::OwnedFd::from_raw_fd(fd) };
// SAFETY: an all-zero kevent is valid; the fields below make the request.
let mut change: libc::kevent = unsafe { std::mem::zeroed() };
change.ident = pid as usize;
change.filter = libc::EVFILT_PROC;
change.flags = libc::EV_ADD;
change.fflags = libc::NOTE_EXIT;
// SAFETY: registers one change on the live queue and receives nothing.
let result = unsafe {
libc::kevent(
queue.as_raw_fd(),
&change,
1,
std::ptr::null_mut(),
0,
std::ptr::null(),
)
};
// The child may already have exited (ESRCH): its exit is then found by reaping.
(result == 0).then_some(queue)
}
fn supervise(
mut leader: Leader,
input: mpsc::Receiver<Input>,
mailbox: Mailbox,
timeout: Option<Duration>,
queued_input: Arc<AtomicUsize>,
) {
let stdout = leader.child.stdout.take().expect("piped stdout");
let stderr = leader.child.stderr.take().expect("piped stderr");
let stdin = leader.child.stdin.take().expect("piped stdin");
let mailbox = Arc::new(mailbox);
let io_stop = Arc::new(AtomicBool::new(false));
let (writer_tx, writer_rx) = mpsc::channel();
let writer_stop = io_stop.clone();
let writer = thread::spawn(move || write_pipe(stdin, writer_rx, queued_input, writer_stop));
let (out_mail, err_mail) = (mailbox.clone(), mailbox.clone());
let (out_stop, err_stop) = (io_stop.clone(), io_stop.clone());
let out = thread::spawn(move || read_pipe(stdout, out_mail, false, out_stop));
let err = thread::spawn(move || read_pipe(stderr, err_mail, true, err_stop));
let started = Instant::now();
let mut terminating: Option<Instant> = None;
let mut forced: Option<Instant> = None;
let mut abandoned = false;
let mut error = None;
loop {
let mut kill = mailbox.stopped.load(Ordering::Acquire);
if mailbox.unreadable.load(Ordering::Acquire) {
kill = true;
error.get_or_insert_with(|| "process output could not be read".to_owned());
}
if timeout.is_some_and(|limit| started.elapsed() >= limit) {
kill = true;
error.get_or_insert_with(|| "process timed out".to_owned());
}
// A pipe write may block: stdin is written on its own thread, never
// this supervisor.
match input.recv_timeout(Duration::from_millis(10)) {
Ok(Input::Write(bytes)) => {
let _ = writer_tx.send(Input::Write(bytes));
}
Ok(Input::Close) => {
let _ = writer_tx.send(Input::Close);
}
Ok(Input::Kill) | Err(mpsc::RecvTimeoutError::Disconnected) => kill = true,
Err(mpsc::RecvTimeoutError::Timeout) => {}
}
if kill && terminating.is_none() {
leader.terminate(false);
terminating = Some(Instant::now());
let _ = writer_tx.send(Input::Close);
}
// A VM being torn down gives no grace period.
let stopped = mailbox.stopped.load(Ordering::Acquire);
if forced.is_none() && terminating.is_some_and(|at| stopped || at.elapsed() >= GRACE) {
leader.terminate(true);
forced = Some(Instant::now());
}
match leader.exited() {
Ok(true) => break,
Ok(false) => {}
Err(e) => {
error = Some(e.to_string());
break;
}
}
if forced.is_some_and(|at| at.elapsed() >= ABANDON) {
error = Some("the process could not be ended (it may run as another user)".into());
abandoned = true;
break;
}
}
// The leader may exit with descendants still holding its pipe handles.
// It is not reaped yet, so its pid still names the group signalled here.
// One that could not be ended is not waited for.
let status = if abandoned {
None
} else {
leader.terminate(true);
leader.reap()
};
let _ = writer_tx.send(Input::Close);
drop(writer_tx);
// Drain final buffered output, but an escaped/detached descendant must
// not keep inherited pipe handles (and therefore VM teardown) alive
// forever. The output of a VM being torn down goes nowhere and is not
// waited for.
let drain_until = Instant::now() + Duration::from_millis(100);
while !(writer.is_finished() && out.is_finished() && err.is_finished())
&& Instant::now() < drain_until
&& !mailbox.stopped.load(Ordering::Acquire)
{
thread::sleep(Duration::from_millis(5));
}
io_stop.store(true, Ordering::Release);
join_by(
vec![writer, out, err],
Instant::now() + Duration::from_millis(100),
);
#[cfg(unix)]
let signal = {
use std::os::unix::process::ExitStatusExt;
status.as_ref().and_then(|s| s.signal())
};
#[cfg(not(unix))]
let signal = None;
mailbox.exit(status.and_then(|s| s.code()), signal, error);
}
}
#[cfg(target_arch = "wasm32")]
mod native {
use crate::ui::{JsError, System};
pub(super) struct Launch;
impl Launch {
pub(super) fn new(
_: &System,
_: String,
_: Vec<String>,
_: Option<String>,
_: Option<std::time::Duration>,
) -> Result<Self, JsError> {
Err(JsError::Type(super::UNAVAILABLE.into()))
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn declarations_come_from_the_spec() {
let registry = crate::runtime::registries()
.iter()
.find(|registry| registry.specifier() == Some("process"))
.unwrap();
let d = crate::ui::declarations(registry, &[]);
for expected in [
"export declare function run(command: string, args?: (string)[] | null, options?: RunOptions | null): Promise<CommandOutput>;",
"export declare function spawn(command: string, args?: (string)[] | null, options?: SpawnOptions | null): ChildProcess;",
"export declare const platform: string;",
" readonly env: Record<string, string>;",
" readonly dataDir: string | null;",
"} & { nextTick: typeof nextTick };",
" on(event: ExitEvent, handler: (result: ExitResult) => void): ChildProcess;",
" on(event: OutputEvent, handler: (text: string) => void): ChildProcess;",
" get pid(): number;",
] {
assert!(d.contains(expected), "{expected}\n{d}");
}
// Getters are the object's, never named exports.
assert!(!d.contains("export declare function env"), "{d}");
}
#[test]
fn a_blocked_pipe_thread_does_not_hold_the_supervisor() {
use std::time::{Duration, Instant};
let (tx, rx) = std::sync::mpsc::channel::<()>();
let blocked = std::thread::spawn(move || {
let _ = rx.recv();
});
let done = std::thread::spawn(|| {});
let start = Instant::now();
super::native::join_by(vec![done, blocked], start + Duration::from_millis(50));
assert!(start.elapsed() < Duration::from_secs(1));
drop(tx);
}
/// A child the system reaped (its host ignores SIGCHLD) is not
/// signalled: its pid may be another process's.
#[cfg(unix)]
#[test]
fn only_children_still_waited_for_are_signalled() {
// The init process is no child of this one.
assert!(!super::native::waitable(1));
let mut child = std::process::Command::new("/bin/sh")
.args(["-c", "exit 0"])
.spawn()
.unwrap();
// Running or exited, it is waitable until it is reaped.
assert!(super::native::waitable(child.id()));
let pid = child.id();
child.wait().unwrap();
assert!(!super::native::waitable(pid));
}
}