#
std/sys/pal/unix/linux/
pidfd.rs

    1use crate::io;
    2use crate::os::fd::{AsRawFd, FromRawFd, RawFd};
    3use crate::sys::cvt;
    4use crate::sys::fd::FileDesc;
    5use crate::sys::process::ExitStatus;
    6use crate::sys_common::{AsInner, FromInner, IntoInner};
    7
    8#[cfg(test)]
    9mod tests;
    10
    11#[derive(Debug)]
    12pub(crate) struct PidFd(FileDesc);
    13
    14impl PidFd {
    15    pub fn kill(&self) -> io::Result<()> {
    16        self.send_signal(libc::SIGKILL)
    17    }
    18
    19    pub(crate) fn send_signal(&self, signal: i32) -> io::Result<()> {
    20        cvt(unsafe {
    21            libc::syscall(
    22                libc::SYS_pidfd_send_signal,
    23                self.0.as_raw_fd(),
    24                signal,
    25                crate::ptr::null::<()>(),
    26                0,
    27            )
    28        })
    29        .map(drop)
    30    }
    31
    32    pub fn wait(&self) -> io::Result<ExitStatus> {
    33        let mut siginfo: libc::siginfo_t = unsafe { crate::mem::zeroed() };
    34        cvt(unsafe {
    35            libc::waitid(libc::P_PIDFD, self.0.as_raw_fd() as u32, &mut siginfo, libc::WEXITED)
    36        })?;
    37        Ok(ExitStatus::from_waitid_siginfo(siginfo))
    38    }
    39
    40    pub fn try_wait(&self) -> io::Result<Option<ExitStatus>> {
    41        let mut siginfo: libc::siginfo_t = unsafe { crate::mem::zeroed() };
    42
    43        cvt(unsafe {
    44            libc::waitid(
    45                libc::P_PIDFD,
    46                self.0.as_raw_fd() as u32,
    47                &mut siginfo,
    48                libc::WEXITED | libc::WNOHANG,
    49            )
    50        })?;
    51        if unsafe { siginfo.si_pid() } == 0 {
    52            Ok(None)
    53        } else {
    54            Ok(Some(ExitStatus::from_waitid_siginfo(siginfo)))
    55        }
    56    }
    57}
    58
    59impl AsInner<FileDesc> for PidFd {
    60    fn as_inner(&self) -> &FileDesc {
    61        &self.0
    62    }
    63}
    64
    65impl IntoInner<FileDesc> for PidFd {
    66    fn into_inner(self) -> FileDesc {
    67        self.0
    68    }
    69}
    70
    71impl FromInner<FileDesc> for PidFd {
    72    fn from_inner(inner: FileDesc) -> Self {
    73        Self(inner)
    74    }
    75}
    76
    77impl FromRawFd for PidFd {
    78    unsafe fn from_raw_fd(fd: RawFd) -> Self {
    79        Self(FileDesc::from_raw_fd(fd))
    80    }
    81}