mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-21 17:52:12 +03:00
Add ChildExt::kill_process_group
This function wraps POSIX `killpg(pid, SIGKILL)`, and on Linux additionally may be implemented by `pidfd_send_signal`.
This commit is contained in:
@@ -452,6 +452,36 @@ pub trait ChildExt: Sealed {
|
||||
/// ```
|
||||
#[unstable(feature = "unix_send_signal", issue = "141975")]
|
||||
fn send_process_group_signal(&self, signal: i32) -> io::Result<()>;
|
||||
|
||||
/// Forces the child process's process group to exit.
|
||||
///
|
||||
/// This is analogous to [`Child::kill`] but applies to every process in
|
||||
/// the child process's process group.
|
||||
///
|
||||
/// Use [`CommandExt::process_group`] to assign a child process to an
|
||||
/// existing process group, or to make it the leader of a new process group.
|
||||
/// By default spawned processes are in the parent's process group.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// #![feature(unix_kill_process_group)]
|
||||
///
|
||||
/// use std::{os::unix::process::{ChildExt, CommandExt}, process::{Command, Stdio}};
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let mut child = Command::new("cat")
|
||||
/// .stdin(Stdio::piped())
|
||||
/// .process_group(0)
|
||||
/// .spawn()?;
|
||||
/// child.kill_process_group()?;
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// [`Child::kill`]: process::Child::kill
|
||||
#[unstable(feature = "unix_kill_process_group", issue = "156537")]
|
||||
fn kill_process_group(&mut self) -> io::Result<()>;
|
||||
}
|
||||
|
||||
#[unstable(feature = "unix_send_signal", issue = "141975")]
|
||||
@@ -463,6 +493,10 @@ fn send_signal(&self, signal: i32) -> io::Result<()> {
|
||||
fn send_process_group_signal(&self, signal: i32) -> io::Result<()> {
|
||||
self.handle.send_process_group_signal(signal)
|
||||
}
|
||||
|
||||
fn kill_process_group(&mut self) -> io::Result<()> {
|
||||
self.handle.send_process_group_signal(libc::SIGKILL)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "process_extensions", since = "1.2.0")]
|
||||
|
||||
Reference in New Issue
Block a user