docs

std::process

本页内容

API 参考只有英文版,由标准库源代码中的英文文档注释生成。

child processes.

Spawning is posix_spawnp; the program is looked up in this process's PATH (not the child's env) unless it contains /. Every fd the library opens is close-on-exec, and an exec failure comes back as the IoError. On Linux each child also has a pidfd, so a signal never hits a recycled pid.

Windows: CreateProcessW with arguments quoted for the MSVC CRT (.exe added when there is no extension); the child inherits only its standard handles; batch files refuse arguments holding ", % or a line break. send_signal sends only Signal::Kill, as TerminateProcess; on_signal routes console events: Ctrl-C (Interrupt) and close (Hangup); Ctrl-Break goes through os::windows::process::on_ctrl_break.

The OS-only parts are trait impls under #[cfg] here (os::unix::process, os::linux::process), so a call on another target names the cfg it needs.

output() drains stdout and stderr together. The _async twins (wait_async, output_async, status_async, the pipes' async io) run their sync twin on the blocking lane, as fs does.

on_signal(sig, f): the C handler writes the signal number to a pipe and a dispatcher thread (nabi-signal) calls f, never in signal context. A burst that fills the pipe drops signals.

Structs

SignalErrorSource

pub struct SignalError

on_signal could not route the signal (SIGKILL / SIGSTOP, a bad number, or the OS refused).

  • pub const fn signal(self) -> Signal§

    Source

    The signal that could not be routed.

  • pub const fn os_code(self) -> i32 | none§

    Source

    The OS error (errno; Windows: the error code), or none when the handler thread could not start for another reason.

  • pub const fn message(self) -> str§

    Source

    What went wrong, for display.

Trait implementations 3
  • impl Display for SignalError
  • impl Debug for SignalError
  • impl Error for SignalError

ExitStatusErrorSource

pub struct ExitStatusError

A program run started ended unsuccessfully.

  • pub const fn status(self) -> ExitStatus§

    Source

    How the program ended.

  • pub const fn stderr(self) -> str§

    Source

    What it wrote to stderr (invalid UTF-8 replaced).

  • pub const fn message(self) -> str§

    Source

    What went wrong, for display: the status and the stderr text.

Trait implementations 3
  • impl Display for ExitStatusError
  • impl Debug for ExitStatusError
  • impl Error for ExitStatusError

ChildStdinSource

pub struct ChildStdin

A child's stdin: the write end of its pipe.

  • pub fn close(self) throws (IoError)§

    Source

    Close the pipe: the child reads end of input.

Trait implementations 9

ChildStdoutSource

pub struct ChildStdout

A child's stdout: the read end of its pipe.

  • pub fn close(self) throws (IoError)§

    Source

    Close the pipe; the child's later writes fail.

Trait implementations 9

ChildStderrSource

pub struct ChildStderr

A child's stderr: the read end of its pipe.

  • pub fn close(self) throws (IoError)§

    Source

    Close the pipe; the child's later writes fail.

Trait implementations 9

CommandSource

pub struct Command

A process to run. Builder methods return the updated command and leave the one they were called on as it was, so copies are independent. The streams start as Inherit; output pipes an inherited stdout / stderr and reads stdin from null.

  • pub const fn new<P: AsPath>(program: P) -> Command§

    Source

    A command that runs program with no arguments; the child inherits the environment, the working directory and the standard streams.

  • pub const fn arg(self, arg: str) -> Command§

    Source

    Append one argument.

  • pub const fn args<I: IntoIterator<Item = str>>(self, args: I) -> Command§

    Source

    Append each of args.

  • pub const fn env(self, key: str, value: str) -> Command§

    Source

    Set key to value in the child's environment.

  • pub fn envs<I: IntoIterator<Item = (str, str)>>(self, envs: I) -> Command§

    Source

    Set each (key, value) in the child's environment, as env.

  • pub const fn env_clear(self) -> Command§

    Source

    Start from an empty environment (then env adds to it).

  • pub const fn current_dir<P: AsPath>(self, dir: P) -> Command§

    Source

    Run the child in dir.

  • pub const fn stdin(self, cfg: Stdio) -> Command§

    Source

    Where the child's standard input comes from (default Stdio::Inherit).

  • pub const fn stdout(self, cfg: Stdio) -> Command§

    Source

    Where the child's standard output goes (default Stdio::Inherit).

  • pub const fn stderr(self, cfg: Stdio) -> Command§

    Source

    Where the child's standard error goes (default Stdio::Inherit).

  • pub const fn kill_on_drop(self, on: bool) -> Command§

    Source

    Kill the child when its Child is released (its owner leaves scope or its using block ends).

  • pub fn spawn(self) throws (IoError) -> Child§

    Source

    Start the child.

  • pub fn output(self) throws (IoError) -> Output§

    Source

    Run to completion, collecting stdout and stderr (an inherited stdin reads null instead; a piped one is closed at once, so the child sees end of input).

  • pub async fn output_async(self) throws (IoError) -> Output§

    Source

    output that suspends the task.

  • pub fn status(self) throws (IoError) -> ExitStatus§

    Source

    Run to completion with inherited streams.

  • pub async fn status_async(self) throws (IoError) -> ExitStatus§

    Source

    status that suspends the task.

Trait implementations 2

ChildSource

pub struct Child

A running (or finished) child. Its Piped streams stay in it until taken (take_stdin / take_stdout / take_stderr); the ones never taken close where the Child is released.

  • pub const fn id(self) -> u32§

    Source

    The child's process id.

  • pub fn take_stdin(ref self) -> ChildStdin | none§

    Source

    Take the write end of the child's stdin pipe (none unless Stdio::Piped, or once taken). The taker closes it; close it before wait if the child reads to end of input.

  • pub fn take_stdout(ref self) -> ChildStdout | none§

    Source

    Take the read end of the child's stdout pipe (none unless Stdio::Piped, or once taken). The taker closes it.

  • pub fn take_stderr(ref self) -> ChildStderr | none§

    Source

    Take the read end of the child's stderr pipe (none unless Stdio::Piped, or once taken). The taker closes it.

  • pub fn handle(self) -> ChildHandle§

    Source

    A handle that signals the child while this Child is owned (and waited on) elsewhere: another thread or task can kill it.

  • pub fn kill(self) throws (IoError)§

    Source

    SIGKILL.

  • pub fn send_signal(self, sig: Signal) throws (IoError)§

    Source

    Send sig; a no-op once the child has ended and is being reaped.

  • pub fn wait(ref self) throws (IoError) -> ExitStatus§

    Source

    Wait for the child to end. Closes our end of its stdin first, so a child reading to EOF can finish.

  • pub async fn wait_async(ref self) throws (IoError) -> ExitStatus§

    Source

    wait that suspends the task. Closes stdin here; the blocking thread only reaps.

  • pub fn try_wait(self) throws (IoError) -> ExitStatus | none§

    Source

    The status if it has ended, without blocking.

Trait implementations 3

ChildHandleSource

pub struct ChildHandle

Signals a child that a Child owns (Child::handle). It owns nothing: no pipes, no wait. Copies are free and may go to any thread; once the child has ended and is being reaped, a signal is a no-op, never one for a reused pid.

  • pub const fn id(self) -> u32§

    Source

    The child's process id.

  • pub fn kill(self) throws (IoError)§

    Source

    SIGKILL (Windows: TerminateProcess).

  • pub fn send_signal(self, sig: Signal) throws (IoError)§

    Source

    Send sig, as Child::send_signal.

Trait implementations 1
  • impl Debug for ChildHandle

ExitStatusSource

pub struct ExitStatus

How a child ended (a raw wait status; Windows: the exit code).

  • pub const fn success(self) -> bool§

    Source

    true when the child exited with code 0.

  • pub const fn code(self) -> i32 | none§

    Source

    The exit code; none if a signal ended it.

Trait implementations 3

OutputSource

pub struct Output

What output() collected.

Enums

SignalSource

pub enum Signal

A signal to send or handle. Windows: only Kill can be sent (TerminateProcess), and only Interrupt (Ctrl-C) and Hangup (console closed) handled; Ctrl-Break is os::windows::process::on_ctrl_break.

Trait implementations 1

StdioSource

pub enum Stdio

Where a child's standard stream goes. A File moved in belongs to the command: running it closes the parent's copy, whether the spawn succeeds or fails.

Trait implementations 1

Functions

abortSource

pub const fn abort() -> !

Abnormal termination (SIGABRT); nothing is cleaned up.

exitSource

pub const fn exit(code: i32) -> !

Exit the process now with code; finally and Drop do not run.

idSource

pub fn id() -> u32

This process's id.

idSource

pub fn id() -> u32

Panics: wasm32 has no process ids.

send_signalSource

pub fn send_signal(pid: u32, sig: Signal) throws (IoError)

Send sig to process pid.

raiseSource

pub fn raise(sig: Signal) throws (IoError)

Send sig to this process. Windows: only Interrupt and Hangup, which reach an on_signal handler (Interrupt without one ends the process).

runSource

pub fn run<P: AsPath>(program: P, args: List<str> = []) throws (IoError | ExitStatusError | Utf8Error) -> str

program with args, run to completion like Command::output: its stdout as text. Throws ExitStatusError (with stderr) when the program fails, Utf8Error when stdout is not UTF-8, IoError when it cannot run. The convenient form of Command; Python: subprocess.run([..], check=True, capture_output=True, text=True).stdout.

on_signalSource

pub fn on_signal(sig: Signal, handler: fn() + Send + Sync) throws (SignalError)

Call handler (on the nabi-signal thread) each time the process receives sig; a later on_signal for the same sig replaces it.