| 1 |
// Copyright © 2021 Mozilla Foundation |
| 2 |
// |
| 3 |
// This program is made available under an ISC-style license. See the |
| 4 |
// accompanying file LICENSE for details |
| 5 |
|
| 6 |
use std::io::Result; |
| 7 |
use std::os::unix::prelude::{AsRawFd, FromRawFd}; |
| 8 |
|
| 9 |
use bytes::{Buf, BufMut, BytesMut}; |
| 10 |
use iovec::IoVec; |
| 11 |
use mio::net::UnixStream; |
| 12 |
|
| 13 |
use crate::PlatformHandle; |
| 14 |
|
| 15 |
use super::{ConnectionBuffer, RecvMsg, SendMsg, HANDLE_QUEUE_LIMIT}; |
| 16 |
|
| 17 |
pub mod cmsg; |
| 18 |
mod msg; |
| 19 |
|
| 20 |
pub struct Pipe { |
| 21 |
pub(crate) io: UnixStream, |
| 22 |
cmsg: BytesMut, |
| 23 |
} |
| 24 |
|
| 25 |
impl Pipe { |
| 26 |
fn new(io: UnixStream) -> Self { |
| 27 |
Pipe { |
| 28 |
io, |
| 29 |
cmsg: BytesMut::with_capacity(cmsg::space( |
| 30 |
std::mem::size_of::<i32>() * HANDLE_QUEUE_LIMIT, |
| 31 |
)), |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
// Create a connected "pipe" pair. The `Pipe` is the server end, |
| 37 |
// the `PlatformHandle` is the client end to be remoted. |
| 38 |
pub fn make_pipe_pair() -> Result<(Pipe, PlatformHandle)> { |
| 39 |
let (server, client) = UnixStream::pair()?; |
| 40 |
Ok((Pipe::new(server), PlatformHandle::from(client))) |
| 41 |
} |
| 42 |
|
| 43 |
impl Pipe { |
| 44 |
#[allow(clippy::missing_safety_doc)] |
| 45 |
pub unsafe fn from_raw_handle(handle: crate::PlatformHandle) -> Pipe { |
| 46 |
Pipe::new(UnixStream::from_raw_fd(handle.into_raw())) |
| 47 |
} |
| 48 |
|
| 49 |
pub fn shutdown(&mut self) -> Result<()> { |
| 50 |
self.io.shutdown(std::net::Shutdown::Both) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
impl RecvMsg for Pipe { |
| 55 |
// Receive data (and fds) from the associated connection. `recv_msg` expects the capacity of |
| 56 |
// the `ConnectionBuffer` members has been adjusted appropriate by the caller. |
| 57 |
fn recv_msg(&mut self, buf: &mut ConnectionBuffer) -> Result<usize> { |
| 58 |
assert!(buf.buf.remaining_mut() > 0); |
| 59 |
// TODO: MSG_CMSG_CLOEXEC not portable. |
| 60 |
// TODO: MSG_NOSIGNAL not portable; macOS can set socket option SO_NOSIGPIPE instead. |
| 61 |
#[cfg(target_os = "linux")] |
| 62 |
let flags = libc::MSG_CMSG_CLOEXEC | libc::MSG_NOSIGNAL; |
| 63 |
#[cfg(not(target_os = "linux"))] |
| 64 |
let flags = 0; |
| 65 |
let r = unsafe { |
| 66 |
let chunk = buf.buf.chunk_mut(); |
| 67 |
let slice = std::slice::from_raw_parts_mut(chunk.as_mut_ptr(), chunk.len()); |
| 68 |
let mut iovec = [<&mut IoVec>::from(slice)]; |
| 69 |
msg::recv_msg_with_flags( |
| 70 |
self.io.as_raw_fd(), |
| 71 |
&mut iovec, |
| 72 |
self.cmsg.chunk_mut(), |
| 73 |
flags, |
| 74 |
) |
| 75 |
}; |
| 76 |
match r { |
| 77 |
Ok((n, cmsg_n, msg_flags)) => unsafe { |
| 78 |
trace!("recv_msg_with_flags flags={}", msg_flags); |
| 79 |
buf.buf.advance_mut(n); |
| 80 |
self.cmsg.advance_mut(cmsg_n); |
| 81 |
let handles = cmsg::decode_handles(&mut self.cmsg); |
| 82 |
self.cmsg.clear(); |
| 83 |
let unused = 0; |
| 84 |
for h in handles { |
| 85 |
buf.push_handle(super::RemoteHandle::new(h, unused)); |
| 86 |
} |
| 87 |
Ok(n) |
| 88 |
}, |
| 89 |
Err(e) => Err(e), |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
impl SendMsg for Pipe { |
| 95 |
// Send data (and fds) on the associated connection. `send_msg` adjusts the length of the |
| 96 |
// `ConnectionBuffer` members based on the size of the successful send operation. |
| 97 |
fn send_msg(&mut self, buf: &mut ConnectionBuffer) -> Result<usize> { |
| 98 |
assert!(!buf.buf.is_empty()); |
| 99 |
if !buf.handles.is_empty() { |
| 100 |
let mut handles = [-1i32; HANDLE_QUEUE_LIMIT]; |
| 101 |
for (i, h) in buf.handles.iter().enumerate() { |
| 102 |
handles[i] = h.handle; |
| 103 |
} |
| 104 |
cmsg::encode_handles(&mut self.cmsg, &handles[..buf.handles.len()]); |
| 105 |
} |
| 106 |
let r = { |
| 107 |
// TODO: MSG_NOSIGNAL not portable; macOS can set socket option SO_NOSIGPIPE instead. |
| 108 |
#[cfg(target_os = "linux")] |
| 109 |
let flags = libc::MSG_NOSIGNAL; |
| 110 |
#[cfg(not(target_os = "linux"))] |
| 111 |
let flags = 0; |
| 112 |
let iovec = [<&IoVec>::from(&buf.buf[..buf.buf.len()])]; |
| 113 |
msg::send_msg_with_flags(self.io.as_raw_fd(), &iovec, &self.cmsg, flags) |
| 114 |
}; |
| 115 |
match r { |
| 116 |
Ok(n) => { |
| 117 |
buf.buf.advance(n); |
| 118 |
// Discard sent handles. |
| 119 |
while buf.handles.pop_front().is_some() {} |
| 120 |
self.cmsg.clear(); |
| 121 |
Ok(n) |
| 122 |
} |
| 123 |
Err(e) => Err(e), |
| 124 |
} |
| 125 |
} |
| 126 |
} |