Skip to main content

Module process

Module process 

Source
Expand description

Running other programs, in the style of std::process.

WASI preview 1 has no way to start a process, so this goes through the architecture’s own process model: a program can run one child at a time, which runs while the parent waits, and pauses whenever it has something for the parent (output, a read from stdin, or its exit). Child exposes that as a stream of Events. Command::status and Child::wait relay it to this program’s own stdio, which is how output from a program run by a program run from the shell reaches the screen.

use wackasm_opencomputers::process::Command;

// Run with this program's stdin and stdout.
let status = Command::new("hello").arg("world").status()?;
println!("hello exited with {status}");

// Capture the output instead.
let output = Command::new("cat").arg("notes.txt").output()?;
println!("{} bytes", output.stdout.len());

Children start with this program’s environment (plus changes made with Command::env and friends) and see the same disk as /. stderr is merged into stdout.

§Working directory

std::env::current_dir and std::env::set_current_dir work as usual, and relative paths in std::fs start at the current directory. A child starts in this program’s current directory, or the one given to Command::current_dir. It gets that directory as PWD, the way Unix shells pass it on.

WASI programs always start in /, so something has to change into PWD before main. This crate does that, in a constructor that runs before main whenever the linker keeps it. It always does with LTO (as in the example programs’ release profile), and when the program calls into process. Otherwise, for example in a debug build that only uses std::fs, call enter_working_dir at the top of main. A program that doesn’t link this crate at all starts in /, but can read PWD itself.

Structs§

Child
A running program. Dropping it before it exits kills it.
Command
A program to run.
Output
What a captured run produced.

Enums§

Event
Something a running child did.
ExitStatus
How a program ended.

Functions§

enter_working_dir
Changes into the directory PWD names, if it’s not /. The constructor described in the module docs does this before main. Calling it again does nothing, so it’s safe at the top of main either way.