Skip to content

Add set_status for std::process::Child #69843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,40 @@ impl Child {
let status = self.wait()?;
Ok(Output { status, stdout, stderr })
}

/// Indicates that the process has exited and the exit status is known.
///
/// You should only call this function after using `waitpid` on Unix or an
/// equivalent function on another platform. It will cause future calls to
/// [`wait`] and [`wait_with_output`] to use this exit status.
///
/// # Examples
///
/// ```
/// # use std::process::{Child, ExitStatus};
/// use std::process::{Command, Stdio};
///
/// let mut child = Command::new("echo")
/// .arg("Hello world")
/// .stdout(Stdio::piped())
/// .spawn()
/// .expect("failed to execute child");
///
/// # fn external_wait(child: &mut Child) -> ExitStatus {
/// # child.wait().unwrap()
/// # }
/// let status = external_wait(&mut child);
/// child.set_status(status);
///
/// assert_eq!(status, child.wait().expect("failed to wait on child"));
/// ```
///
/// [`wait`]: #method.wait
/// [`wait_with_output`]: #method.wait_with_output
#[stable(feature = "child_set_status", since = "1.44.0")]
pub fn set_status(&mut self, status: ExitStatus) {
self.handle.set_status(status.0);
}
}

/// Terminates the current process with the specified exit code.
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/sys/cloudabi/shims/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,6 @@ impl Process {
pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
match self.0 {}
}

pub fn set_status(&mut self, _: ExitStatus) {}
}
2 changes: 2 additions & 0 deletions src/libstd/sys/hermit/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,6 @@ impl Process {
pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
match self.0 {}
}

pub fn set_status(&mut self, _: ExitStatus) {}
}
2 changes: 2 additions & 0 deletions src/libstd/sys/sgx/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,6 @@ impl Process {
pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
match self.0 {}
}

pub fn set_status(&mut self, _: ExitStatus) {}
}
22 changes: 19 additions & 3 deletions src/libstd/sys/unix/process/process_fuchsia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Command {

let process_handle = unsafe { self.do_exec(theirs, envp.as_ref())? };

Ok((Process { handle: Handle::new(process_handle) }, ours))
Ok((Process { handle: Handle::new(process_handle), status: None }, ours))
}

pub fn exec(&mut self, default: Stdio) -> io::Error {
Expand Down Expand Up @@ -139,6 +139,7 @@ impl Command {

pub struct Process {
handle: Handle,
status: Option<ExitStatus>,
}

impl Process {
Expand All @@ -160,6 +161,10 @@ impl Process {
use crate::default::Default;
use crate::sys::process::zircon::*;

if let Some(status) = self.status {
return Ok(status);
}

let mut proc_info: zx_info_process_t = Default::default();
let mut actual: size_t = 0;
let mut avail: size_t = 0;
Expand All @@ -186,13 +191,19 @@ impl Process {
"Failed to get exit status of process",
));
}
Ok(ExitStatus(proc_info.return_code))
let status = ExitStatus(proc_info.return_code);
self.set_status(status);
Ok(status)
}

pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
use crate::default::Default;
use crate::sys::process::zircon::*;

if let Some(status) = self.status {
return Ok(status);
}

let mut proc_info: zx_info_process_t = Default::default();
let mut actual: size_t = 0;
let mut avail: size_t = 0;
Expand Down Expand Up @@ -224,7 +235,12 @@ impl Process {
"Failed to get exit status of process",
));
}
Ok(Some(ExitStatus(proc_info.return_code)))
self.set_status(ExitStatus(proc_info.return_code));
Ok(self.status)
}

pub fn set_status(&mut self, status: ExitStatus) {
self.status = Some(status);
}
}

Expand Down
20 changes: 9 additions & 11 deletions src/libstd/sys/unix/process/process_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,8 @@ impl Process {
}
let mut status = 0 as c_int;
cvt_r(|| unsafe { libc::waitpid(self.pid, &mut status, 0) })?;
self.status = Some(ExitStatus::new(status));
Ok(ExitStatus::new(status))
self.set_status(ExitStatus(status));
Ok(ExitStatus(status))
}

pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
Expand All @@ -446,12 +446,14 @@ impl Process {
}
let mut status = 0 as c_int;
let pid = cvt(unsafe { libc::waitpid(self.pid, &mut status, libc::WNOHANG) })?;
if pid == 0 {
Ok(None)
} else {
self.status = Some(ExitStatus::new(status));
Ok(Some(ExitStatus::new(status)))
if pid != 0 {
self.set_status(ExitStatus(status));
}
Ok(self.status)
}

pub fn set_status(&mut self, status: ExitStatus) {
self.status = Some(status);
}
}

Expand All @@ -460,10 +462,6 @@ impl Process {
pub struct ExitStatus(c_int);

impl ExitStatus {
pub fn new(status: c_int) -> ExitStatus {
ExitStatus(status)
}

fn exited(&self) -> bool {
unsafe { libc::WIFEXITED(self.0) }
}
Expand Down
4 changes: 0 additions & 4 deletions src/libstd/sys/vxworks/process/process_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,6 @@ impl fmt::Debug for Command {
pub struct ExitStatus(c_int);

impl ExitStatus {
pub fn new(status: c_int) -> ExitStatus {
ExitStatus(status)
}

fn exited(&self) -> bool {
/*unsafe*/
{ libc::WIFEXITED(self.0) }
Expand Down
16 changes: 9 additions & 7 deletions src/libstd/sys/vxworks/process/process_vxworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ impl Process {
}
let mut status = 0 as c_int;
cvt_r(|| unsafe { libc::waitpid(self.pid, &mut status, 0) })?;
self.status = Some(ExitStatus::new(status));
Ok(ExitStatus::new(status))
self.set_status(ExitStatus(status));
Ok(ExitStatus(status))
}

pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
Expand All @@ -159,11 +159,13 @@ impl Process {
}
let mut status = 0 as c_int;
let pid = cvt(unsafe { libc::waitpid(self.pid, &mut status, libc::WNOHANG) })?;
if pid == 0 {
Ok(None)
} else {
self.status = Some(ExitStatus::new(status));
Ok(Some(ExitStatus::new(status)))
if pid != 0 {
self.set_status(ExitStatus(status));
}
Ok(self.status)
}

pub fn set_status(&mut self, status: ExitStatus) {
self.status = Some(status);
}
}
2 changes: 2 additions & 0 deletions src/libstd/sys/wasi/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,6 @@ impl Process {
pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
match self.0 {}
}

pub fn set_status(&mut self, _: ExitStatus) {}
}
2 changes: 2 additions & 0 deletions src/libstd/sys/wasm/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,6 @@ impl Process {
pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
match self.0 {}
}

pub fn set_status(&mut self, _: ExitStatus) {}
}
17 changes: 15 additions & 2 deletions src/libstd/sys/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl Command {
// around to be able to close it later.
drop(Handle::new(pi.hThread));

Ok((Process { handle: Handle::new(pi.hProcess) }, pipes))
Ok((Process { handle: Handle::new(pi.hProcess), status: None }, pipes))
}
}

Expand Down Expand Up @@ -319,6 +319,7 @@ impl From<File> for Stdio {
/// for the process to terminate.
pub struct Process {
handle: Handle,
status: Option<ExitStatus>,
}

impl Process {
Expand All @@ -332,18 +333,25 @@ impl Process {
}

pub fn wait(&mut self) -> io::Result<ExitStatus> {
if let Some(status) = self.status {
return Ok(status);
}
unsafe {
let res = c::WaitForSingleObject(self.handle.raw(), c::INFINITE);
if res != c::WAIT_OBJECT_0 {
return Err(Error::last_os_error());
}
let mut status = 0;
cvt(c::GetExitCodeProcess(self.handle.raw(), &mut status))?;
self.set_status(ExitStatus(status));
Ok(ExitStatus(status))
}
}

pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
if let Some(status) = self.status {
return Ok(Some(status));
}
unsafe {
match c::WaitForSingleObject(self.handle.raw(), 0) {
c::WAIT_OBJECT_0 => {}
Expand All @@ -354,10 +362,15 @@ impl Process {
}
let mut status = 0;
cvt(c::GetExitCodeProcess(self.handle.raw(), &mut status))?;
Ok(Some(ExitStatus(status)))
self.set_status(ExitStatus(status));
Ok(self.status)
}
}

pub fn set_status(&mut self, status: ExitStatus) {
self.status = Some(status);
}

pub fn handle(&self) -> &Handle {
&self.handle
}
Expand Down