Skip to content

Implement send_signal for unix child processes #141990

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions library/std/src/os/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ pub mod prelude {
#[stable(feature = "rust1", since = "1.0.0")]
pub use super::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
#[doc(no_inline)]
#[unstable(feature = "unix_send_signal", issue = "141975")]
pub use super::process::ChildExt;
#[doc(no_inline)]
#[stable(feature = "rust1", since = "1.0.0")]
pub use super::process::{CommandExt, ExitStatusExt};
#[doc(no_inline)]
Expand Down
35 changes: 35 additions & 0 deletions library/std/src/os/unix/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,41 @@ impl ExitStatusExt for process::ExitStatusError {
}
}

#[unstable(feature = "unix_send_signal", issue = "141975")]
pub trait ChildExt: Sealed {
/// Sends a signal to a child process.
///
/// # Errors
///
/// This function will return an error if the signal is invalid. The integer values associated
/// with signals are implemenation-specific, so it's encouraged to use a crate that provides
/// posix bindings.
///
/// # Examples
///
/// ```rust
/// #![feature(unix_send_signal)]
///
/// use std::{io, os::unix::process::ChildExt, process::{Command, Stdio}};
///
/// use libc::SIGTERM;
///
/// fn main() -> io::Result<()> {
/// let child = Command::new("cat").stdin(Stdio::piped()).spawn()?;
/// child.send_signal(SIGTERM)?;
/// Ok(())
/// }
/// ```
fn send_signal(&self, signal: i32) -> io::Result<()>;
}

#[unstable(feature = "unix_send_signal", issue = "141975")]
impl ChildExt for process::Child {
fn send_signal(&self, signal: i32) -> io::Result<()> {
self.handle.send_signal(signal)
}
}

#[stable(feature = "process_extensions", since = "1.2.0")]
impl FromRawFd for process::Stdio {
#[inline]
Expand Down
6 changes: 5 additions & 1 deletion library/std/src/sys/pal/unix/linux/pidfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ pub(crate) struct PidFd(FileDesc);

impl PidFd {
pub fn kill(&self) -> io::Result<()> {
self.send_signal(libc::SIGKILL)
}

pub(crate) fn send_signal(&self, signal: i32) -> io::Result<()> {
cvt(unsafe {
libc::syscall(
libc::SYS_pidfd_send_signal,
self.0.as_raw_fd(),
libc::SIGKILL,
signal,
crate::ptr::null::<()>(),
0,
)
Expand Down
10 changes: 7 additions & 3 deletions library/std/src/sys/process/unix/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,18 +964,22 @@ impl Process {
}

pub fn kill(&mut self) -> io::Result<()> {
self.send_signal(libc::SIGKILL)
}

pub(crate) fn send_signal(&self, signal: i32) -> io::Result<()> {
// If we've already waited on this process then the pid can be recycled
// and used for another process, and we probably shouldn't be killing
// and used for another process, and we probably shouldn't be signaling
// random processes, so return Ok because the process has exited already.
if self.status.is_some() {
return Ok(());
}
#[cfg(target_os = "linux")]
if let Some(pid_fd) = self.pidfd.as_ref() {
// pidfd_send_signal predates pidfd_open. so if we were able to get an fd then sending signals will work too
return pid_fd.kill();
return pid_fd.send_signal(signal);
}
cvt(unsafe { libc::kill(self.pid, libc::SIGKILL) }).map(drop)
cvt(unsafe { libc::kill(self.pid, signal) }).map(drop)
}

pub fn wait(&mut self) -> io::Result<ExitStatus> {
Expand Down
Loading