|
1 | 1 | use crate::errno::Errno;
|
| 2 | +use crate::sys::signal::Signal; |
2 | 3 | use crate::unistd::Pid;
|
3 | 4 | use crate::Result;
|
4 | 5 | use std::convert::TryFrom;
|
@@ -74,3 +75,44 @@ pub fn pid_open(pid: Pid, nonblock: bool) -> Result<OwnedFd> {
|
74 | 75 | _ => unreachable!(),
|
75 | 76 | }
|
76 | 77 | }
|
| 78 | + |
| 79 | +/// Sends the signal `sig` to the target process referred to by `pid`, a PID file descriptor that |
| 80 | +/// refers to a process. |
| 81 | +/// |
| 82 | +/// If the info argument is some [`libc::siginfo_t`] buffer, that buffer should be populated as |
| 83 | +/// described in [rt_sigqueueinfo(2)](https://man7.org/linux/man-pages/man2/rt_sigqueueinfo.2.html). |
| 84 | +/// |
| 85 | +/// If the info argument is `None`, this is equivalent to specifying a pointer to a `siginfo_t` |
| 86 | +/// buffer whose fields match the values that are implicitly supplied when a signal is sent using |
| 87 | +/// [`crate::sys::signal::kill`]: |
| 88 | +/// |
| 89 | +/// - `si_signo` is set to the signal number; |
| 90 | +/// - `si_errno` is set to 0; |
| 91 | +/// - `si_code` is set to SI_USER; |
| 92 | +/// - `si_pid` is set to the caller's PID; and |
| 93 | +/// - `si_uid` is set to the caller's real user ID. |
| 94 | +/// |
| 95 | +/// The calling process must either be in the same PID namespace as the process referred to by |
| 96 | +/// pidfd, or be in an ancestor of that namespace. |
| 97 | +pub fn pidfd_send_signal<Fd: AsRawFd>( |
| 98 | + pid: Fd, |
| 99 | + sig: Signal, |
| 100 | + info: Option<libc::siginfo_t>, |
| 101 | +) -> Result<()> { |
| 102 | + let info = match info { |
| 103 | + Some(i) => &i, |
| 104 | + None => std::ptr::null(), |
| 105 | + }; |
| 106 | + match unsafe { |
| 107 | + libc::syscall( |
| 108 | + libc::SYS_pidfd_send_signal, |
| 109 | + pid.as_raw_fd(), |
| 110 | + sig as i32, |
| 111 | + info, |
| 112 | + ) |
| 113 | + } { |
| 114 | + -1 => Err(Errno::last()), |
| 115 | + 0 => Ok(()), |
| 116 | + _ => unreachable!(), |
| 117 | + } |
| 118 | +} |
0 commit comments