Skip to content

Commit e43e63b

Browse files
Added interrupt fn for std::process::Child
1 parent ae0030b commit e43e63b

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

library/std/src/process.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,6 +1860,38 @@ impl Child {
18601860
self.handle.kill()
18611861
}
18621862

1863+
/// Signals the child process to exit. If the child has already exited, an [`InvalidInput`]
1864+
/// error is returned.
1865+
///
1866+
/// Unlike [`kill`] this allows the process to catch the signal and gracefully exit.
1867+
///
1868+
/// The mapping to [`ErrorKind`]s is not part of the compatibility contract of the function.
1869+
///
1870+
/// This is equivalent to sending a SIGINT on Unix platforms.
1871+
///
1872+
/// # Examples
1873+
///
1874+
/// Basic usage:
1875+
///
1876+
/// ```no_run
1877+
/// use std::process::Command;
1878+
///
1879+
/// let mut command = Command::new("yes");
1880+
/// if let Ok(mut child) = command.spawn() {
1881+
/// child.interrupt().expect("command wasn't running");
1882+
/// } else {
1883+
/// println!("yes command didn't start");
1884+
/// }
1885+
/// ```
1886+
///
1887+
/// [`ErrorKind`]: io::ErrorKind
1888+
/// [`InvalidInput`]: io::ErrorKind::InvalidInput
1889+
#[stable(feature = "process", since = "1.0.0")]
1890+
pub fn interrupt(&mut self) -> io::Result<()> {
1891+
self.handle.interrupt()
1892+
}
1893+
1894+
18631895
/// Returns the OS-assigned process identifier associated with this child.
18641896
///
18651897
/// # Examples

library/std/src/sys/unix/process/process_fuchsia.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ impl Process {
160160
Ok(())
161161
}
162162

163+
pub fn interrupt(&mut self) -> io::Result<()> {
164+
use crate::sys::process::zircon::*;
165+
166+
unsafe {
167+
zx_cvt(zx_task_kill(self.handle.raw()))?;
168+
}
169+
170+
Ok(())
171+
}
172+
163173
pub fn wait(&mut self) -> io::Result<ExitStatus> {
164174
use crate::default::Default;
165175
use crate::sys::process::zircon::*;

library/std/src/sys/unix/process/process_unix.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,20 @@ impl Process {
604604
cvt(unsafe { libc::kill(self.pid, libc::SIGKILL) }).map(drop)
605605
}
606606
}
607+
608+
pub fn interrupt(&mut self) -> io::Result<()> {
609+
// If we've already waited on this process then the pid can be recycled
610+
// and used for another process, and we probably shouldn't be interrupting
611+
// random processes, so just return an error.
612+
if self.status.is_some() {
613+
Err(io::const_io_error!(
614+
ErrorKind::InvalidInput,
615+
"invalid argument: can't interrupt an exited process",
616+
))
617+
} else {
618+
cvt(unsafe { libc::kill(self.pid, libc::SIGINT) }).map(drop)
619+
}
620+
}
607621

608622
pub fn wait(&mut self) -> io::Result<ExitStatus> {
609623
use crate::sys::cvt_r;

library/std/src/sys/unix/process/process_unsupported.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ impl Process {
4242
unsupported()
4343
}
4444

45+
pub fn interrupt(&mut self) -> io::Result<()> {
46+
unsupported()
47+
}
48+
4549
pub fn wait(&mut self) -> io::Result<ExitStatus> {
4650
unsupported()
4751
}

library/std/src/sys/unix/process/process_vxworks.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ impl Process {
150150
}
151151
}
152152

153+
pub fn interrupt(&mut self) -> io::Result<()> {
154+
// If we've already waited on this process then the pid can be recycled
155+
// and used for another process, and we probably shouldn't be interrupting
156+
// random processes, so just return an error.
157+
if self.status.is_some() {
158+
Err(io::const_io_error!(
159+
ErrorKind::InvalidInput,
160+
"invalid argument: can't kill an exited process",
161+
))
162+
} else {
163+
cvt(unsafe { libc::kill(self.pid, libc::SIGINT) }).map(drop)
164+
}
165+
}
166+
153167
pub fn wait(&mut self) -> io::Result<ExitStatus> {
154168
use crate::sys::cvt_r;
155169
if let Some(status) = self.status {

library/std/src/sys/unsupported/process.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ impl Process {
182182
self.0
183183
}
184184

185+
pub fn interrupt(&mut self) -> io::Result<()> {
186+
self.0
187+
}
188+
185189
pub fn wait(&mut self) -> io::Result<ExitStatus> {
186190
self.0
187191
}

0 commit comments

Comments
 (0)