| 1 | use std::{ |
| 2 | ffi::c_int, |
| 3 | io, |
| 4 | mem::size_of, |
| 5 | os::fd::{AsFd, BorrowedFd}, |
| 6 | }; |
| 7 | |
| 8 | use crate::{ |
| 9 | common::bin_serde::{BinPipe, DeSerialize}, |
| 10 | exec::signal_fmt, |
| 11 | system::interface::ProcessId, |
| 12 | }; |
| 13 | |
| 14 | use super::CommandStatus; |
| 15 | |
| 16 | type Prefix = u8; |
| 17 | type ParentData = c_int; |
| 18 | type MonitorData = c_int; |
| 19 | |
| 20 | const PREFIX_LEN: usize = size_of::<Prefix>(); |
| 21 | const PARENT_DATA_LEN: usize = size_of::<ParentData>(); |
| 22 | const MONITOR_DATA_LEN: usize = size_of::<MonitorData>(); |
| 23 | |
| 24 | pub(super) struct BackchannelPair { |
| 25 | pub(super) parent: ParentBackchannel, |
| 26 | pub(super) monitor: MonitorBackchannel, |
| 27 | } |
| 28 | |
| 29 | impl BackchannelPair { |
| 30 | pub(super) fn new() -> io::Result<Self> { |
| 31 | let (sock1, sock2) = BinPipe::pair()?; |
| 32 | |
| 33 | #[cfg(debug_assertions)] |
| 34 | { |
| 35 | sock1.set_nonblocking(true)?; |
| 36 | sock2.set_nonblocking(true)?; |
| 37 | } |
| 38 | |
| 39 | Ok(Self { |
| 40 | parent: ParentBackchannel { |
| 41 | socket: sock1, |
| 42 | #[cfg(debug_assertions)] |
| 43 | nonblocking_asserts: false, |
| 44 | }, |
| 45 | monitor: MonitorBackchannel { |
| 46 | socket: sock2, |
| 47 | #[cfg(debug_assertions)] |
| 48 | nonblocking_asserts: false, |
| 49 | }, |
| 50 | }) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub(super) enum ParentMessage { |
| 55 | IoError(c_int), |
| 56 | CommandStatus(CommandStatus), |
| 57 | CommandPid(ProcessId), |
| 58 | ShortRead, |
| 59 | } |
| 60 | |
| 61 | impl ParentMessage { |
| 62 | const LEN: usize = PREFIX_LEN + PARENT_DATA_LEN; |
| 63 | const IO_ERROR: Prefix = 0; |
| 64 | const CMD_STAT_EXIT: Prefix = 1; |
| 65 | const CMD_STAT_TERM: Prefix = 2; |
| 66 | const CMD_STAT_STOP: Prefix = 3; |
| 67 | const CMD_PID: Prefix = 4; |
| 68 | const SHORT_READ: Prefix = 5; |
| 69 | |
| 70 | fn from_parts(prefix: Prefix, data: ParentData) -> Self { |
| 71 | match prefix { |
| 72 | Self::IO_ERROR => Self::IoError(data), |
| 73 | Self::CMD_STAT_EXIT => Self::CommandStatus(CommandStatus::Exit(data)), |
| 74 | Self::CMD_STAT_TERM => Self::CommandStatus(CommandStatus::Term(data)), |
| 75 | Self::CMD_STAT_STOP => Self::CommandStatus(CommandStatus::Stop(data)), |
| 76 | Self::CMD_PID => Self::CommandPid(ProcessId::new(data)), |
| 77 | Self::SHORT_READ => Self::ShortRead, |
| 78 | _ => unreachable!(), |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn to_parts(&self) -> (Prefix, ParentData) { |
| 83 | let prefix = match self { |
| 84 | ParentMessage::IoError(_) => Self::IO_ERROR, |
| 85 | ParentMessage::CommandStatus(CommandStatus::Exit(_)) => Self::CMD_STAT_EXIT, |
| 86 | ParentMessage::CommandStatus(CommandStatus::Term(_)) => Self::CMD_STAT_TERM, |
| 87 | ParentMessage::CommandStatus(CommandStatus::Stop(_)) => Self::CMD_STAT_STOP, |
| 88 | ParentMessage::CommandPid(_) => Self::CMD_PID, |
| 89 | ParentMessage::ShortRead => Self::SHORT_READ, |
| 90 | }; |
| 91 | |
| 92 | let data = match self { |
| 93 | ParentMessage::IoError(data) => *data, |
| 94 | ParentMessage::CommandPid(data) => data.inner(), |
| 95 | ParentMessage::CommandStatus(status) => match status { |
| 96 | CommandStatus::Exit(data) |
| 97 | | CommandStatus::Term(data) |
| 98 | | CommandStatus::Stop(data) => *data, |
| 99 | }, |
| 100 | ParentMessage::ShortRead => 0, |
| 101 | }; |
| 102 | |
| 103 | (prefix, data) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl TryFrom<io::Error> for ParentMessage { |
| 108 | type Error = io::Error; |
| 109 | |
| 110 | fn try_from(err: io::Error) -> Result<Self, Self::Error> { |
| 111 | err.raw_os_error() |
| 112 | .map(Self::IoError) |
| 113 | .or_else(|| (err.kind() == io::ErrorKind::UnexpectedEof).then_some(Self::ShortRead)) |
| 114 | .ok_or(err) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | impl From<CommandStatus> for ParentMessage { |
| 119 | fn from(status: CommandStatus) -> Self { |
| 120 | Self::CommandStatus(status) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | impl DeSerialize for ParentMessage { |
| 125 | type Bytes = [u8; ParentMessage::LEN]; |
| 126 | |
| 127 | fn serialize(&self) -> Self::Bytes { |
| 128 | let mut buf = [0; ParentMessage::LEN]; |
| 129 | |
| 130 | let (prefix_buf, data_buf) = buf.split_at_mut(PREFIX_LEN); |
| 131 | let (prefix, data) = self.to_parts(); |
| 132 | |
| 133 | prefix_buf.copy_from_slice(&prefix.to_ne_bytes()); |
| 134 | data_buf.copy_from_slice(&data.to_ne_bytes()); |
| 135 | buf |
| 136 | } |
| 137 | |
| 138 | fn deserialize(buf: Self::Bytes) -> Self { |
| 139 | let (prefix_buf, data_buf) = buf.split_at(PREFIX_LEN); |
| 140 | |
| 141 | let prefix = Prefix::from_ne_bytes(prefix_buf.try_into().unwrap()); |
| 142 | let data = MonitorData::from_ne_bytes(data_buf.try_into().unwrap()); |
| 143 | |
| 144 | ParentMessage::from_parts(prefix, data) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /// A socket use for communication between the monitor and the parent process. |
| 149 | pub(super) struct ParentBackchannel { |
| 150 | socket: BinPipe<ParentMessage, MonitorMessage>, |
| 151 | #[cfg(debug_assertions)] |
| 152 | nonblocking_asserts: bool, |
| 153 | } |
| 154 | |
| 155 | impl ParentBackchannel { |
| 156 | /// Send a [`MonitorMessage`]. |
| 157 | /// |
| 158 | /// Calling this method will block until the socket is ready for writing. |
| 159 | #[track_caller] |
| 160 | pub(super) fn send(&mut self, event: &MonitorMessage) -> io::Result<()> { |
| 161 | self.socket.write(event).inspect_err(|_err| { |
| 162 | #[cfg(debug_assertions)] |
| 163 | if self.nonblocking_asserts { |
| 164 | assert_ne!(_err.kind(), io::ErrorKind::WouldBlock); |
| 165 | } |
| 166 | }) |
| 167 | } |
| 168 | |
| 169 | /// Receive a [`ParentMessage`]. |
| 170 | /// |
| 171 | /// Calling this method will block until the socket is ready for reading. |
| 172 | #[track_caller] |
| 173 | pub(super) fn recv(&mut self) -> io::Result<ParentMessage> { |
| 174 | let msg = self.socket.read().inspect_err(|_err| { |
| 175 | #[cfg(debug_assertions)] |
| 176 | if self.nonblocking_asserts { |
| 177 | assert_ne!(_err.kind(), io::ErrorKind::WouldBlock); |
| 178 | } |
| 179 | })?; |
| 180 | Ok(msg) |
| 181 | } |
| 182 | |
| 183 | pub(super) fn set_nonblocking_asserts(&mut self, _doit: bool) { |
| 184 | #[cfg(debug_assertions)] |
| 185 | { |
| 186 | self.nonblocking_asserts = _doit; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | impl AsFd for ParentBackchannel { |
| 192 | fn as_fd(&self) -> BorrowedFd<'_> { |
| 193 | self.socket.as_fd() |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /// Different messages exchanged between the monitor and the parent process using a [`ParentBackchannel`]. |
| 198 | #[derive(PartialEq, Eq)] |
| 199 | pub(super) enum MonitorMessage { |
| 200 | Edge, |
| 201 | Signal(c_int), |
| 202 | } |
| 203 | |
| 204 | impl MonitorMessage { |
| 205 | const LEN: usize = PREFIX_LEN + MONITOR_DATA_LEN; |
| 206 | const EDGE_CMD: Prefix = 0; |
| 207 | const SIGNAL: Prefix = 1; |
| 208 | |
| 209 | fn from_parts(prefix: Prefix, data: MonitorData) -> Self { |
| 210 | match prefix { |
| 211 | Self::EDGE_CMD => Self::Edge, |
| 212 | Self::SIGNAL => Self::Signal(data), |
| 213 | _ => unreachable!(), |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | fn to_parts(&self) -> (Prefix, MonitorData) { |
| 218 | let prefix = match self { |
| 219 | MonitorMessage::Edge => Self::EDGE_CMD, |
| 220 | MonitorMessage::Signal(_) => Self::SIGNAL, |
| 221 | }; |
| 222 | |
| 223 | let data = match self { |
| 224 | MonitorMessage::Edge => 0, |
| 225 | MonitorMessage::Signal(data) => *data, |
| 226 | }; |
| 227 | |
| 228 | (prefix, data) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | impl std::fmt::Debug for MonitorMessage { |
| 233 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 234 | match self { |
| 235 | Self::Edge => "Edge".fmt(f), |
| 236 | &Self::Signal(signal) => write!(f, "Signal({})", signal_fmt(signal)), |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | impl DeSerialize for MonitorMessage { |
| 242 | type Bytes = [u8; MonitorMessage::LEN]; |
| 243 | |
| 244 | fn serialize(&self) -> Self::Bytes { |
| 245 | let mut buf = [0; MonitorMessage::LEN]; |
| 246 | |
| 247 | let (prefix_buf, data_buf) = buf.split_at_mut(PREFIX_LEN); |
| 248 | let (prefix, data) = self.to_parts(); |
| 249 | |
| 250 | prefix_buf.copy_from_slice(&prefix.to_ne_bytes()); |
| 251 | data_buf.copy_from_slice(&data.to_ne_bytes()); |
| 252 | buf |
| 253 | } |
| 254 | |
| 255 | fn deserialize(bytes: Self::Bytes) -> Self { |
| 256 | let (prefix_buf, data_buf) = bytes.split_at(PREFIX_LEN); |
| 257 | |
| 258 | let prefix = Prefix::from_ne_bytes(prefix_buf.try_into().unwrap()); |
| 259 | let data = MonitorData::from_ne_bytes(data_buf.try_into().unwrap()); |
| 260 | |
| 261 | MonitorMessage::from_parts(prefix, data) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /// A socket use for communication between the monitor and the parent process. |
| 266 | pub(super) struct MonitorBackchannel { |
| 267 | socket: BinPipe<MonitorMessage, ParentMessage>, |
| 268 | #[cfg(debug_assertions)] |
| 269 | nonblocking_asserts: bool, |
| 270 | } |
| 271 | |
| 272 | impl MonitorBackchannel { |
| 273 | /// Send a [`ParentMessage`]. |
| 274 | /// |
| 275 | /// Calling this method will block until the socket is ready for writing. |
| 276 | #[track_caller] |
| 277 | pub(super) fn send(&mut self, event: &ParentMessage) -> io::Result<()> { |
| 278 | self.socket.write(event).inspect_err(|_err| { |
| 279 | #[cfg(debug_assertions)] |
| 280 | if self.nonblocking_asserts { |
| 281 | assert_ne!(_err.kind(), io::ErrorKind::WouldBlock); |
| 282 | } |
| 283 | }) |
| 284 | } |
| 285 | |
| 286 | /// Receive a [`MonitorMessage`]. |
| 287 | /// |
| 288 | /// Calling this method will block until the socket is ready for reading. |
| 289 | #[track_caller] |
| 290 | pub(super) fn recv(&mut self) -> io::Result<MonitorMessage> { |
| 291 | let msg = self.socket.read().inspect_err(|_err| { |
| 292 | #[cfg(debug_assertions)] |
| 293 | if self.nonblocking_asserts { |
| 294 | assert_ne!(_err.kind(), io::ErrorKind::WouldBlock); |
| 295 | } |
| 296 | })?; |
| 297 | Ok(msg) |
| 298 | } |
| 299 | |
| 300 | pub(super) fn set_nonblocking_assertions(&mut self, _doit: bool) { |
| 301 | #[cfg(debug_assertions)] |
| 302 | { |
| 303 | self.nonblocking_asserts = _doit; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | impl AsFd for MonitorBackchannel { |
| 309 | fn as_fd(&self) -> BorrowedFd<'_> { |
| 310 | self.socket.as_fd() |
| 311 | } |
| 312 | } |