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
//! Process execution and management
#![cfg_attr(
target_family = "wasm",
allow(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
clippy::missing_const_for_fn,
clippy::doc_markdown,
clippy::type_complexity,
clippy::unnecessary_wraps,
clippy::needless_pass_by_value,
clippy::undocumented_unsafe_blocks,
)
)]
#[cfg(not(target_family = "wasm"))]
mod native_process {
use crate::io;
use crate::string::String;
use crate::vec::Vec;
// ── Linux pidfd async wait ────────────────────────────────────────────────
#[cfg(all(
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
))]
const SYS_PIDFD_OPEN: i64 = 434;
#[cfg(target_os = "linux")]
unsafe extern "C" {
fn syscall(num: i64, ...) -> i64;
fn close(fd: i32) -> i32;
}
#[cfg(all(
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
))]
fn pidfd_open(pid: u32) -> io::Result<i32> {
// SAFETY: variadic syscall with two ABI-correct arguments.
let r = unsafe { syscall(SYS_PIDFD_OPEN, pid as i64, 0i64) };
if r < 0 {
Err(io::Error::last_os_error())
} else {
Ok(r as i32)
}
}
// ── Unix: posix_spawnp, waitpid, kill ────────────────────────────────────
#[cfg(unix)]
unsafe extern "C" {
fn posix_spawnp(
pid: *mut i32,
file: *const u8,
file_actions: *const core::ffi::c_void,
attrp: *const core::ffi::c_void,
argv: *const *const u8,
envp: *const *const u8,
) -> i32;
fn waitpid(pid: i32, status: *mut i32, options: i32) -> i32;
fn kill(pid: i32, sig: i32) -> i32;
static environ: *const *const u8;
}
#[cfg(unix)]
const SIGKILL: i32 = 9;
#[cfg(unix)]
const WNOHANG: i32 = 1;
// ── Windows ───────────────────────────────────────────────────────────────
#[cfg(windows)]
#[repr(C)]
struct StartupInfoW {
cb: u32,
lp_reserved: *mut u16,
lp_desktop: *mut u16,
lp_title: *mut u16,
dw_x: u32,
dw_y: u32,
dw_x_size: u32,
dw_y_size: u32,
dw_x_count_chars: u32,
dw_y_count_chars: u32,
dw_fill_attribute: u32,
dw_flags: u32,
w_show_window: u16,
cb_reserved2: u16,
lp_reserved2: *mut u8,
h_std_input: usize,
h_std_output: usize,
h_std_error: usize,
}
#[cfg(windows)]
#[repr(C)]
struct ProcessInformation {
h_process: usize,
h_thread: usize,
dw_process_id: u32,
dw_thread_id: u32,
}
#[cfg(windows)]
unsafe extern "system" {
fn CreateProcessW(
lp_application_name: *const u16,
lp_command_line: *mut u16,
lp_process_attributes: *const core::ffi::c_void,
lp_thread_attributes: *const core::ffi::c_void,
b_inherit_handles: i32,
dw_creation_flags: u32,
lp_environment: *const core::ffi::c_void,
lp_current_directory: *const u16,
lp_startup_info: *const StartupInfoW,
lp_process_information: *mut ProcessInformation,
) -> i32;
fn GetExitCodeProcess(h_process: usize, lp_exit_code: *mut u32) -> i32;
fn CloseHandle(h_object: usize) -> i32;
fn TerminateProcess(h_process: usize, u_exit_code: u32) -> i32;
}
// ── Exit status ───────────────────────────────────────────────────────────
/// Exit status of a child process.
pub struct ChildExitStatus(i32);
impl ChildExitStatus {
/// Returns `true` if the process exited with code 0.
pub fn success(&self) -> bool {
self.0 == 0
}
/// Returns the raw exit code.
pub fn code(&self) -> Option<i32> {
Some(self.0)
}
}
// ── Stdio ─────────────────────────────────────────────────────────────────
/// I/O configuration for a child process's stdin/stdout/stderr.
pub enum Stdio {
/// Inherit the parent's handle.
Inherit,
/// Redirect to the null device.
Null,
/// Create a pipe.
Piped,
}
impl Stdio {
/// Create a `Stdio` that inherits the parent's handle.
pub fn inherit() -> Self {
Self::Inherit
}
/// Create a `Stdio` that discards all I/O.
pub fn null() -> Self {
Self::Null
}
/// Create a `Stdio` that sets up a pipe.
pub fn piped() -> Self {
Self::Piped
}
}
// ── Child ─────────────────────────────────────────────────────────────────
/// A running child process.
pub struct Child {
#[cfg(unix)]
pid: u32,
#[cfg(windows)]
handle: usize,
#[cfg(not(any(unix, windows)))]
_opaque: (),
}
impl Child {
/// Wait asynchronously for the child to exit.
#[allow(clippy::unused_async)]
pub async fn wait(&mut self) -> io::Result<ChildExitStatus> {
#[cfg(all(
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
))]
{
let pidfd = pidfd_open(self.pid)?;
let res = crate::rt::wait_readable(pidfd).await;
// SAFETY: pidfd is valid and owned by us.
unsafe { close(pidfd) };
res?;
let mut status = 0i32;
// SAFETY: self.pid is valid.
let r = unsafe { waitpid(self.pid as i32, &mut status, 0) };
if r < 0 {
return Err(io::Error::last_os_error());
}
let code = if status & 0x7f == 0 {
(status >> 8) & 0xff
} else {
-1
};
return Ok(ChildExitStatus(code));
}
#[cfg(all(
unix,
not(all(
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
))
))]
{
let mut status = 0i32;
// SAFETY: self.pid is valid.
let r = unsafe { waitpid(self.pid as i32, &mut status, 0) };
if r < 0 {
return Err(io::Error::last_os_error());
}
let code = if status & 0x7f == 0 {
(status >> 8) & 0xff
} else {
-1
};
return Ok(ChildExitStatus(code));
}
#[cfg(windows)]
{
crate::rt::windows::WaitProcess::new(self.handle as u64).await?;
let mut code = 0u32;
// SAFETY: handle is a valid process handle.
unsafe { GetExitCodeProcess(self.handle, &mut code) };
return Ok(ChildExitStatus(code as i32));
}
#[cfg(not(any(unix, windows)))]
Err(io::Error::other(
"Child::wait: not supported on this platform",
))
}
/// Non-blocking check if the child has exited.
pub fn try_wait(&mut self) -> io::Result<Option<ChildExitStatus>> {
#[cfg(unix)]
{
let mut status = 0i32;
// SAFETY: self.pid is valid.
let r = unsafe { waitpid(self.pid as i32, &mut status, WNOHANG) };
if r < 0 {
return Err(io::Error::last_os_error());
}
if r == 0 {
return Ok(None);
}
let code = if status & 0x7f == 0 {
(status >> 8) & 0xff
} else {
-1
};
return Ok(Some(ChildExitStatus(code)));
}
#[cfg(windows)]
{
let mut code = 0u32;
// SAFETY: handle is valid.
unsafe { GetExitCodeProcess(self.handle, &mut code) };
const STILL_ACTIVE: u32 = 259;
return Ok(if code == STILL_ACTIVE {
None
} else {
Some(ChildExitStatus(code as i32))
});
}
#[cfg(not(any(unix, windows)))]
Ok(None)
}
/// Send SIGKILL (Unix) or TerminateProcess (Windows) to the child.
pub fn kill(&mut self) -> io::Result<()> {
#[cfg(unix)]
{
// SAFETY: self.pid is valid.
let r = unsafe { kill(self.pid as i32, SIGKILL) };
if r < 0 {
return Err(io::Error::last_os_error());
}
return Ok(());
}
#[cfg(windows)]
{
// SAFETY: handle is valid.
let r = unsafe { TerminateProcess(self.handle, 1) };
if r == 0 {
return Err(io::Error::last_os_error());
}
return Ok(());
}
#[cfg(not(any(unix, windows)))]
Err(io::Error::other("kill: not supported"))
}
}
#[cfg(windows)]
impl Drop for Child {
fn drop(&mut self) {
// SAFETY: handle is valid and owned by us.
unsafe { CloseHandle(self.handle) };
}
}
// ── Command ───────────────────────────────────────────────────────────────
/// Builder for spawning child processes.
pub struct Command {
program: String,
args: Vec<String>,
envs: Vec<(String, String)>,
stdin: Stdio,
stdout: Stdio,
stderr: Stdio,
}
impl Command {
/// Create a new command for `program`.
pub fn new<S: AsRef<str>>(program: S) -> Self {
Self {
program: program.as_ref().into(),
args: Vec::new(),
envs: Vec::new(),
stdin: Stdio::Inherit,
stdout: Stdio::Inherit,
stderr: Stdio::Inherit,
}
}
/// Append a single argument.
pub fn arg<S: AsRef<str>>(&mut self, arg: S) -> &mut Self {
self.args.push(arg.as_ref().into());
self
}
/// Append multiple arguments.
pub fn args<I, S>(&mut self, args: I) -> &mut Self
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
for a in args {
self.args.push(a.as_ref().into());
}
self
}
/// Set an environment variable for the child process.
pub fn env<K: AsRef<str>, V: AsRef<str>>(&mut self, key: K, val: V) -> &mut Self {
self.envs.push((key.as_ref().into(), val.as_ref().into()));
self
}
/// Configure stdin for the child process.
pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
self.stdin = cfg.into();
self
}
/// Configure stdout for the child process.
pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
self.stdout = cfg.into();
self
}
/// Configure stderr for the child process.
pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
self.stderr = cfg.into();
self
}
/// Spawn the command as a child process.
pub fn spawn(&mut self) -> io::Result<Child> {
self.spawn_impl()
}
#[cfg(unix)]
fn spawn_impl(&mut self) -> io::Result<Child> {
// Build null-terminated byte strings for argv.
let mut argv_storage: Vec<Vec<u8>> = Vec::new();
{
let mut b = Vec::with_capacity(self.program.len() + 1);
b.extend_from_slice(self.program.as_bytes());
b.push(0);
argv_storage.push(b);
}
for arg in &self.args {
let mut b = Vec::with_capacity(arg.len() + 1);
b.extend_from_slice(arg.as_bytes());
b.push(0);
argv_storage.push(b);
}
// Null-terminated pointer array.
let mut argv_ptrs: Vec<*const u8> = argv_storage.iter().map(|v| v.as_ptr()).collect();
argv_ptrs.push(core::ptr::null());
let mut pid = 0i32;
// SAFETY: argv_storage keeps the strings alive for the duration of
// posix_spawnp; environ is a valid global pointer.
let r = unsafe {
posix_spawnp(
&mut pid,
argv_storage[0].as_ptr(),
core::ptr::null(),
core::ptr::null(),
argv_ptrs.as_ptr(),
environ,
)
};
if r != 0 {
return Err(io::Error::from_raw_os_error(r));
}
Ok(Child { pid: pid as u32 })
}
#[cfg(windows)]
fn spawn_impl(&mut self) -> io::Result<Child> {
use crate::ffi::OsStrExt as _;
// Build a wide command line.
let mut cmdline: Vec<u16> = self.program.encode_wide().collect();
for arg in &self.args {
cmdline.push(b' ' as u16);
cmdline.extend(arg.encode_wide());
}
cmdline.push(0);
let si = StartupInfoW {
cb: core::mem::size_of::<StartupInfoW>() as u32,
lp_reserved: core::ptr::null_mut(),
lp_desktop: core::ptr::null_mut(),
lp_title: core::ptr::null_mut(),
dw_x: 0,
dw_y: 0,
dw_x_size: 0,
dw_y_size: 0,
dw_x_count_chars: 0,
dw_y_count_chars: 0,
dw_fill_attribute: 0,
dw_flags: 0,
w_show_window: 0,
cb_reserved2: 0,
lp_reserved2: core::ptr::null_mut(),
h_std_input: 0,
h_std_output: 0,
h_std_error: 0,
};
let mut pi = ProcessInformation {
h_process: 0,
h_thread: 0,
dw_process_id: 0,
dw_thread_id: 0,
};
// SAFETY: si/pi are validly initialised; cmdline is null-terminated.
let ok = unsafe {
CreateProcessW(
core::ptr::null(),
cmdline.as_mut_ptr(),
core::ptr::null(),
core::ptr::null(),
0,
0,
core::ptr::null(),
core::ptr::null(),
&si,
&mut pi,
)
};
if ok == 0 {
return Err(io::Error::last_os_error());
}
// SAFETY: h_thread is a valid handle.
unsafe { CloseHandle(pi.h_thread) };
Ok(Child {
handle: pi.h_process,
})
}
#[cfg(not(any(unix, windows)))]
fn spawn_impl(&mut self) -> io::Result<Child> {
Err(io::Error::other("spawn: not supported on this platform"))
}
}
}
#[cfg(not(target_family = "wasm"))]
pub use native_process::{Child, ChildExitStatus, Command, Stdio};
// ─── WASM ─────────────────────────────────────────────────────────────────────
#[cfg(target_family = "wasm")]
use crate::abi::imports;
#[cfg(target_family = "wasm")]
use crate::rt::wasm::{OverlappedBufferFuture, OverlappedFuture};
#[cfg(target_family = "wasm")]
use crate::string::String;
#[cfg(target_family = "wasm")]
use crate::vec::Vec;
/// WASM child process handle.
#[cfg(target_family = "wasm")]
pub struct Child {
handle: u64,
}
#[cfg(target_family = "wasm")]
impl Child {
/// Wait for the child to exit.
pub async fn wait(&mut self) -> crate::io::Result<ChildExitStatus> {
let (err, result, _) = OverlappedFuture::new({
let handle = self.handle;
move |ov| {
// SAFETY: `ov` is a valid overlapped pointer.
unsafe { imports::process_wait(ov, handle) };
}
})
.await;
if err != 0 {
return Err(crate::io::Error::from_raw_os_error(err as i32));
}
let exit_code = (result >> 32) as i32;
let success = exit_code == 0;
Ok(ChildExitStatus { exit_code, success })
}
}
/// Exit status for a WASM child process.
#[cfg(target_family = "wasm")]
pub struct ChildExitStatus {
exit_code: i32,
success: bool,
}
#[cfg(target_family = "wasm")]
impl ChildExitStatus {
/// Returns the exit code.
pub fn code(&self) -> Option<i32> {
Some(self.exit_code)
}
/// Returns `true` if the process exited successfully.
pub fn success(&self) -> bool {
self.success
}
}
/// I/O configuration for a WASM child process.
#[cfg(target_family = "wasm")]
pub enum Stdio {
/// Inherit the parent's handle.
Inherit,
/// Redirect to the null device.
Null,
/// Create a pipe.
Piped,
}
/// Builder for spawning WASM child processes.
#[cfg(target_family = "wasm")]
pub struct Command {
program: String,
args: Vec<String>,
env: Vec<(String, String)>,
}
#[cfg(target_family = "wasm")]
impl Command {
/// Create a new command.
pub fn new<S: Into<String>>(program: S) -> Self {
Self {
program: program.into(),
args: Vec::new(),
env: Vec::new(),
}
}
/// Add an argument.
pub fn arg<S: Into<String>>(&mut self, arg: S) -> &mut Self {
self.args.push(arg.into());
self
}
/// Add multiple arguments.
pub fn args<I, S>(&mut self, args: I) -> &mut Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
for arg in args {
self.args.push(arg.into());
}
self
}
/// Spawn the command.
pub async fn spawn(&mut self) -> crate::io::Result<Child> {
// Format: program\0arg1\0arg2\0\0key=val\0\0
let mut config = Vec::new();
config.extend_from_slice(self.program.as_bytes());
config.push(0);
for arg in &self.args {
config.extend_from_slice(arg.as_bytes());
config.push(0);
}
config.push(0); // end-of-args
for (k, v) in &self.env {
config.extend_from_slice(k.as_bytes());
config.push(b'=');
config.extend_from_slice(v.as_bytes());
config.push(0);
}
config.push(0); // end-of-env
let (err, handle, _, _config) = OverlappedBufferFuture::new(config, move |ov, ptr, len| {
// SAFETY: `ptr`/`len` describe the future-owned config buffer.
unsafe { imports::process_spawn(ov, ptr.cast_const(), len) };
})
.await;
if err != 0 {
return Err(crate::io::Error::from_raw_os_error(err as i32));
}
Ok(Child { handle })
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::string::ToString;
fn block_on<F: core::future::Future<Output = ()> + 'static>(f: F) {
crate::rt::executor::run(f);
loop {
match crate::rt::executor::poll_step().unwrap() {
crate::rt::executor::PollStatus::Done => break,
crate::rt::executor::PollStatus::Ready => continue,
crate::rt::executor::PollStatus::Idle { next_deadline } => {
if let Some(d) = next_deadline {
std::thread::sleep(d);
} else {
std::thread::sleep(std::time::Duration::from_millis(5));
}
}
}
}
}
#[test]
fn test_process_wait() {
block_on(async {
#[cfg(target_os = "windows")]
let cmd_name = "cmd";
#[cfg(not(target_os = "windows"))]
let cmd_name = "echo";
let mut cmd = Command::new(cmd_name);
#[cfg(target_os = "windows")]
cmd.arg("/c").arg("echo hello");
let spawn_res = cmd.spawn();
if spawn_res.is_err() {
return; // stub or unsupported platform
}
let mut child = spawn_res.unwrap();
let res = child.wait().await;
if let Err(e) = &res {
if e.to_string().contains("pending") {
return;
}
}
let status = res.expect("wait failed");
assert!(status.success());
});
}
}