1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#[cfg(not(target_os = "linux"))]
compile_error!("PidFd is only supported on Linux >= 5.3");

use fd_reactor::{Interest, REACTOR};

use std::{
    convert::TryInto,
    future::Future,
    io,
    mem::MaybeUninit,
    os::unix::{
        io::{AsRawFd, RawFd},
        process::ExitStatusExt,
    },
    pin::Pin,
    process::ExitStatus,
    sync::{
        atomic::{AtomicBool, Ordering},
        mpsc, Arc, Mutex,
    },
    task::{Context, Poll, Waker},
    time::Duration,
};

const PIDFD_OPEN: libc::c_int = 434;
const PID_SEND: libc::c_int = 424;
const P_PIDFD: libc::idtype_t = 3;

/// A file descriptor which refers to a process
pub struct PidFd(RawFd);

impl PidFd {
    /// Converts a `Child` into a `PidFd`; validating if the PID is in range
    pub fn from_std_checked(child: &std::process::Child) -> io::Result<Self> {
        child
            .id()
            .try_into()
            .map_err(|_| {
                io::Error::new(
                    io::ErrorKind::Other,
                    "child process ID is outside the range of libc::pid_t",
                )
            })
            .and_then(|pid| unsafe { Self::open(pid, 0) })
    }

    pub fn into_future(self) -> PidFuture {
        self.into()
    }

    #[cfg(feature = "waitid")]
    pub fn wait(&self) -> io::Result<ExitStatus> {
        waitid(self.0)
    }

    /// Creates a PID file descriptor from a PID
    pub unsafe fn open(pid: libc::pid_t, flags: libc::c_uint) -> io::Result<Self> {
        let pidfd = pidfd_create(pid, flags);
        if -1 == pidfd {
            Err(io::Error::last_os_error())
        } else {
            Ok(Self(pidfd))
        }
    }

    /// Sends a signal to the process owned by this PID file descriptor
    pub unsafe fn send_raw_signal(
        &self,
        sig: libc::c_int,
        info: *const libc::siginfo_t,
        flags: libc::c_uint,
    ) -> io::Result<()> {
        if -1 == pidfd_send_signal(self.0, sig, info, flags) {
            Err(io::Error::last_os_error())
        } else {
            Ok(())
        }
    }
}

impl From<PidFd> for PidFuture {
    fn from(fd: PidFd) -> Self {
        Self {
            fd,
            completed: Arc::new(AtomicBool::new(false)),
            registered: false,
        }
    }
}

pub struct PidFuture {
    fd: PidFd,
    completed: Arc<AtomicBool>,
    registered: bool,
}

impl Future for PidFuture {
    type Output = io::Result<ExitStatus>;

    fn poll(mut self: Pin<&mut Self>, context: &mut Context) -> Poll<Self::Output> {
        if self.completed.load(Ordering::SeqCst) {
            REACTOR.unregister(self.fd.0);

            #[cfg(feature = "waitid")]
            {
                Poll::Ready(waitid(self.fd.0))
            }

            #[cfg(not(feature = "waitid"))]
            {
                Poll::Ready(Ok(ExitStatus::from_raw(0)))
            }
        } else {
            if !self.registered {
                REACTOR.register(
                    self.fd.0,
                    Interest::READ,
                    Arc::clone(&self.completed),
                    context.waker().clone(),
                );

                self.registered = true;
            }

            Poll::Pending
        }
    }
}

impl AsRawFd for PidFd {
    fn as_raw_fd(&self) -> RawFd {
        self.0
    }
}

impl Drop for PidFd {
    fn drop(&mut self) {
        let _ = unsafe { libc::close(self.0) };
    }
}

impl From<&std::process::Child> for PidFd {
    fn from(child: &std::process::Child) -> Self {
        Self::from_std_checked(child).unwrap()
    }
}

impl From<std::process::Child> for PidFd {
    fn from(child: std::process::Child) -> Self {
        Self::from(&child)
    }
}

extern "C" {
    fn syscall(num: libc::c_int, ...) -> libc::c_int;
}

unsafe fn pidfd_create(pid: libc::pid_t, flags: libc::c_uint) -> libc::c_int {
    syscall(PIDFD_OPEN, pid, flags)
}

unsafe fn pidfd_send_signal(
    pidfd: libc::c_int,
    sig: libc::c_int,
    info: *const libc::siginfo_t,
    flags: libc::c_uint,
) -> libc::c_int {
    syscall(PID_SEND, pidfd, sig, info, flags)
}

#[cfg(feature = "waitid")]
fn waitid(pidfd: RawFd) -> io::Result<ExitStatus> {
    unsafe {
        let mut info = MaybeUninit::<libc::siginfo_t>::uninit();
        let exit_status = libc::waitid(P_PIDFD, pidfd as u32, info.as_mut_ptr(), libc::WEXITED);
        if -1 == exit_status {
            Err(io::Error::last_os_error())
        } else {
            Ok(ExitStatus::from_raw(info.assume_init().si_errno))
        }
    }
}