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 SignalErroron_signal could not route the signal (SIGKILL / SIGSTOP, a bad
number, or the OS refused).
The signal that could not be routed.
The OS error (errno; Windows: the error code), or
nonewhen the handler thread could not start for another reason.What went wrong, for display.
ExitStatusErrorSource
pub struct ExitStatusErrorA program run started ended unsuccessfully.
Sourcepub const fn status(§self) -> ExitStatus How the program ended.
What it wrote to stderr (invalid UTF-8 replaced).
What went wrong, for display: the status and the stderr text.
ChildStdinSource
pub struct ChildStdinA child's stdin: the write end of its pipe.
Close the pipe: the child reads end of input.
Trait implementations 9
impl Debug for ChildStdinimpl Write for ChildStdinimpl AsyncWrite for ChildStdinimpl Close for ChildStdinimpl AsyncClose for ChildStdinimpl AsRawFd for ChildStdinimpl AsRawHandle for ChildStdinimpl IntoRawFd for ChildStdinimpl IntoRawHandle for ChildStdin
ChildStdoutSource
pub struct ChildStdoutA child's stdout: the read end of its pipe.
Close the pipe; the child's later writes fail.
Trait implementations 9
impl Debug for ChildStdoutimpl Read for ChildStdoutimpl AsyncRead for ChildStdoutimpl Close for ChildStdoutimpl AsyncClose for ChildStdoutimpl AsRawFd for ChildStdoutimpl AsRawHandle for ChildStdoutimpl IntoRawFd for ChildStdoutimpl IntoRawHandle for ChildStdout
ChildStderrSource
pub struct ChildStderrA child's stderr: the read end of its pipe.
Close the pipe; the child's later writes fail.
Trait implementations 9
impl Debug for ChildStderrimpl Read for ChildStderrimpl AsyncRead for ChildStderrimpl Close for ChildStderrimpl AsyncClose for ChildStderrimpl AsRawFd for ChildStderrimpl AsRawHandle for ChildStderrimpl IntoRawFd for ChildStderrimpl IntoRawHandle for ChildStderr
CommandSource
pub struct CommandA 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.
A command that runs
programwith no arguments; the child inherits the environment, the working directory and the standard streams.Append one argument.
Sourcepub const fn args<I: IntoIterator<Item = str>>(§self, args: I) -> Command Append each of
args.Set
keytovaluein the child's environment.
Sourcepub fn envs<I: IntoIterator<Item = (str, str)>>(§self, envs: I) -> Command Set each
(key, value)in the child's environment, asenv.Start from an empty environment (then
envadds to it).Run the child in
dir.Where the child's standard input comes from (default
Stdio::Inherit).Where the child's standard output goes (default
Stdio::Inherit).Where the child's standard error goes (default
Stdio::Inherit).Kill the child when its
Childis released (its owner leaves scope or itsusingblock ends).Start the child.
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).
outputthat suspends the task.
Sourcepub fn status(§self) throws (IoError) -> ExitStatus Run to completion with inherited streams.
Sourcepub async fn status_async(§self) throws (IoError) -> ExitStatus statusthat suspends the task.
Trait implementations 2
impl Debug for Commandimpl CommandExt for Command
ChildSource
pub struct ChildA 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.
The child's process id.
Sourcepub fn take_stdin(§ref self) -> ChildStdin | none Take the write end of the child's stdin pipe (
noneunlessStdio::Piped, or once taken). The taker closes it; close it beforewaitif the child reads to end of input.
Sourcepub fn take_stdout(§ref self) -> ChildStdout | none Take the read end of the child's stdout pipe (
noneunlessStdio::Piped, or once taken). The taker closes it.
Sourcepub fn take_stderr(§ref self) -> ChildStderr | none Take the read end of the child's stderr pipe (
noneunlessStdio::Piped, or once taken). The taker closes it.
Sourcepub fn handle(§self) -> ChildHandle A handle that signals the child while this
Childis owned (and waited on) elsewhere: another thread or task can kill it.SIGKILL.Send
sig; a no-op once the child has ended and is being reaped.
Sourcepub fn wait(§ref self) throws (IoError) -> ExitStatus Wait for the child to end. Closes our end of its stdin first, so a child reading to EOF can finish.
Sourcepub async fn wait_async(§ref self) throws (IoError) -> ExitStatus waitthat suspends the task. Closes stdin here; the blocking thread only reaps.
Sourcepub fn try_wait(§self) throws (IoError) -> ExitStatus | none The status if it has ended, without blocking.
ChildHandleSource
pub struct ChildHandleSignals 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.
The child's process id.
SIGKILL(Windows:TerminateProcess).Send
sig, asChild::send_signal.
Trait implementations 1
impl Debug for ChildHandle
ExitStatusSource
pub struct ExitStatusHow a child ended (a raw wait status; Windows: the exit code).
truewhen the child exited with code 0.The exit code;
noneif a signal ended it.
Trait implementations 3
impl Debug for ExitStatusimpl Display for ExitStatusimpl ExitStatusExt for ExitStatus
OutputSource
pub struct OutputWhat output() collected.
Sourcepub status: ExitStatus§
Enums
SignalSource
pub enum SignalA 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.
SIGHUP.SIGINT.SIGQUIT.SIGILL.SIGTRAP.SIGABRT.SIGBUS.SIGFPE.SIGKILL: cannot be handled.SIGSEGV.SIGPIPE.SIGALRM.SIGTERM.SIGUSR1.SIGUSR2.SIGCHLD.SIGCONT.SIGSTOP: cannot be handled.SIGTSTP.SIGTTIN.SIGTTOU.SIGURG.SIGXCPU.SIGXFSZ.SIGVTALRM.SIGPROF.SIGWINCH.SIGIO.SIGSYS.The OS number (Linux's, else the BSD / macOS one; Windows: the CRT's for the ones it routes).
Trait implementations 1
impl Debug for Signal
StdioSource
pub enum StdioWhere 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
impl Close for Stdio
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.