Skip to content
CryoCryo home
Stdlibprocess

child

import std::process::child; · source

Types

Child

type struct Child {
    stdin:  Option<ChildStdin>;
    stdout: Option<ChildStdout>;
    stderr: Option<ChildStderr>;

    static new(pid: i32, handle: u64, stdin: Option<ChildStdin>, stdout: Option<ChildStdout>, stderr: Option<ChildStderr>) -> Child;
    id(&this) -> i32;
    wait(mut &this) -> Result<ExitStatus, IoError>;
    async join(mut &this) -> Result<ExitStatus, IoError>;
    try_wait(mut &this) -> Result<Option<ExitStatus>, IoError>;
    kill(&this) -> Result<(), IoError>;
    send_signal(&this, sig: Signal) -> Result<(), IoError>;
}

A running subprocess, produced by spawn. It holds the pid (private, behind id()) and whichever pipe ends you asked for.

wait blocks and closes stdin first so the child sees EOF; join is the non-blocking spelling, parking on the reactor (a pidfd on Linux) or the blocking pool; try_wait returns Ok(None) while the child is still running. kill is SIGKILL on POSIX and does not wait.

wait closing stdin first is deliberate: it is deadlock-avoidant for a child that reads all of stdin before writing anything to stdout. join does not touch the pipes, so a child that writes more than a pipe buffer holds must have its stdout / stderr drained while you await — the same hazard, and the reason Command::collect drains first.

Properties

PropertyType
stdinOption<ChildStdin>
stdoutOption<ChildStdout>
stderrOption<ChildStderr>

Constructors

static new(pid: i32, handle: u64, stdin: Option<ChildStdin>, stdout: Option<ChildStdout>, stderr: Option<ChildStderr>) -> Child;

Low-level constructor used by Command::spawn. Application code should not call this directly. handle is 0 on POSIX and the Win32 process HANDLE on Windows.

Methods

id(&this) -> i32;
wait(mut &this) -> Result<ExitStatus, IoError>;

Block until the child exits. Closes stdin first so the child sees EOF; this is deadlock-avoidant for children that read all of stdin before writing to stdout.

async join(mut &this) -> Result<ExitStatus, IoError>;

Await the child's exit without blocking the calling thread: the async spelling of wait. Same stdin-close, same status, same waited bookkeeping — but the task parks and the executor worker stays free. ChildWait documents which of the two routes each platform takes.

try_wait(mut &this) -> Result<Option<ExitStatus>, IoError>;

Non-blocking check. Ok(Some(status)) means the child has exited; Ok(None) means it's still running.

kill(&this) -> Result<(), IoError>;

Send SIGKILL (POSIX) / terminate the process (Windows). Does not wait; call wait afterwards to reap the zombie on POSIX.

send_signal(&this, sig: Signal) -> Result<(), IoError>;

The pipe ends

type struct ChildStdin {
    fd: i32;

    static from_fd(raw_fd: i32) -> ChildStdin;
}

type struct ChildStdout {
    fd: i32;

    static from_fd(raw_fd: i32) -> ChildStdout;
}

type struct ChildStderr {
    fd: i32;

    static from_fd(raw_fd: i32) -> ChildStderr;
}

The parent's ends of the pipes a Stdio::Piped configuration created. ChildStdin implements Write; ChildStdout and ChildStderr implement Read. Dropping ChildStdin closes the pipe, which is what signals EOF to the child.

ChildStdin
Properties
PropertyType
fdi32
Constructors

####### static from_fd(raw_fd: i32) -> ChildStdin;

ChildStdout
Properties
PropertyType
fdi32
Constructors

####### static from_fd(raw_fd: i32) -> ChildStdout;

ChildStderr
Properties
PropertyType
fdi32
Constructors

####### static from_fd(raw_fd: i32) -> ChildStderr;

Reaping

Every Child should have wait — or kill then wait — called before it drops.

Child::drop closes any captured pipe ends and makes a non-blocking best-effort reap. If the child has already exited, its zombie is released there. If it is still running, a destructor cannot block, so the child stays detached and reaping it remains your responsibility. That closes the common "spawn, child finishes, drop without wait" leak without ever blocking. Cancelling a join mid-wait leaves the child in exactly this state.

Trait implementations

Child
implement trait Drop for struct Child
ChildStdin
implement trait Write for struct ChildStdin
implement trait Drop for struct ChildStdin
ChildStdout
implement trait Read for struct ChildStdout
implement trait Drop for struct ChildStdout
ChildStderr
implement trait Read for struct ChildStderr
implement trait Drop for struct ChildStderr

ExitStatus

type struct ExitStatus {
    code:     i32;
    signal:   i32;
    signaled: boolean;

    success(&this) -> boolean;
    exit_code(&this) -> Option<i32>;
    terminating_signal(&this) -> Option<i32>;
}

POSIX packs a normal exit and a signal death into one status word; ExitStatus is the decoded form.

success() is an exit with status 0. exit_code() is None and terminating_signal() is Some when a signal ended the process.

Properties

PropertyType
codei32
signali32
signaledboolean

Methods

success(&this) -> boolean;

Convenience: did the child exit with status 0?

exit_code(&this) -> Option<i32>;

Some(code) for a normal exit, None if the child was killed by a signal.

terminating_signal(&this) -> Option<i32>;

Some(signal) if the child was killed by a signal.