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
//! Read-only confirmation of the exact attached daemon's process exit.
use std::io;
#[cfg(target_os = "linux")]
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
use std::os::unix::net::UnixStream;
use std::time::Duration;
#[cfg(target_os = "linux")]
use std::time::Instant;
#[cfg(all(test, target_os = "linux"))]
mod tests;
/// A process handle obtained from the connected socket, never from a reusable
/// PID or session pathname. Unsupported kernels remain explicitly unconfirmed.
pub(crate) struct PeerExit {
/// Linux's socket-bound pidfd pins the peer incarnation even across PID
/// reuse.
#[cfg(target_os = "linux")]
fd: OwnedFd,
}
impl PeerExit {
/// Pin the peer of an admitted attached-UI socket without acquiring any
/// signaling or session ownership.
#[cfg(target_os = "linux")]
#[allow(unsafe_code)] // No socket-bound pidfd wrapper in our socket2 version.
pub(crate) fn from_socket(stream: &UnixStream) -> io::Result<Self> {
let mut fd: libc::c_int = -1;
let mut len = std::mem::size_of_val(&fd) as libc::socklen_t;
// SAFETY: both output pointers refer to initialized values of the exact
// size supplied; the socket remains borrowed for the entire syscall.
let result = unsafe {
libc::getsockopt(
stream.as_raw_fd(),
libc::SOL_SOCKET,
libc::SO_PEERPIDFD,
(&raw mut fd).cast(),
&raw mut len,
)
};
if result != 0 {
return Err(io::Error::last_os_error());
}
// SAFETY: successful SO_PEERPIDFD returns a new owned, close-on-exec
// fd.
Ok(Self {
fd: unsafe { OwnedFd::from_raw_fd(fd) },
})
}
/// Other platforms cannot currently confirm an unrelated daemon's exit.
#[cfg(not(target_os = "linux"))]
pub(crate) fn from_socket(_stream: &UnixStream) -> io::Result<Self> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"attached daemon exit confirmation is unavailable on this platform",
))
}
/// Wait boundedly for this exact process to exit; this makes no claim about
/// successful persistence cleanup or a non-child's exit status.
#[cfg(target_os = "linux")]
#[allow(unsafe_code)] // Read-only poll of an owned descriptor.
pub(crate) fn wait(&self, timeout: Duration) -> io::Result<bool> {
let deadline = Instant::now() + timeout;
loop {
let mut pollfd = libc::pollfd {
fd: self.fd.as_raw_fd(),
events: libc::POLLIN,
revents: 0,
};
let remaining = deadline.saturating_duration_since(Instant::now());
let millis = remaining.as_millis().min(i32::MAX as u128) as i32;
// SAFETY: pollfd is a live, initialized one-element array.
let result = unsafe { libc::poll(&raw mut pollfd, 1, millis) };
if 0 <= result {
return Ok(pollfd.revents & (libc::POLLIN | libc::POLLHUP) != 0);
}
let error = io::Error::last_os_error();
if error.kind() != io::ErrorKind::Interrupted {
return Err(error);
}
if deadline <= Instant::now() {
return Ok(false);
}
}
}
/// Unsupported platforms conservatively leave the outcome unconfirmed.
#[cfg(not(target_os = "linux"))]
pub(crate) fn wait(&self, _timeout: Duration) -> io::Result<bool> {
Ok(false)
}
}