drop IoVec/Mut in favor of std IoSlice/Mut

These used to not be Send + Sync, but they are now.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2022-06-01 13:19:05 +02:00
parent 3715458fce
commit bd5301dc58
4 changed files with 16 additions and 105 deletions

View File

@ -1,86 +0,0 @@
//! Send+Sync IoSlice replacement.
use std::io::{IoSlice, IoSliceMut};
use std::marker::PhantomData;
/// The standard IoSlice does not implement Send and Sync. These types do.
#[derive(Debug)]
#[repr(C)]
pub struct IoVec<'a> {
_iov: libc::iovec,
_phantom: PhantomData<&'a [u8]>,
}
unsafe impl Send for IoVec<'_> {}
unsafe impl Sync for IoVec<'_> {}
impl IoVec<'_> {
pub fn new(slice: &[u8]) -> Self {
Self {
_iov: libc::iovec {
iov_base: slice.as_ptr() as *mut libc::c_void,
iov_len: slice.len(),
},
_phantom: PhantomData,
}
}
}
impl<'s> IoVec<'s> {
pub fn from_io_slice<'a>(ioslice: &'a [IoSlice<'s>]) -> &'a [Self] {
unsafe { &*(ioslice as *const [IoSlice] as *const [Self]) }
}
}
impl<'s> std::ops::Deref for IoVec<'s> {
type Target = [u8];
#[inline]
fn deref(&self) -> &Self::Target {
unsafe { std::slice::from_raw_parts(self._iov.iov_base as *const u8, self._iov.iov_len) }
}
}
#[derive(Debug)]
#[repr(C)]
pub struct IoVecMut<'a> {
_iov: libc::iovec,
_phantom: PhantomData<&'a [u8]>,
}
unsafe impl Send for IoVecMut<'_> {}
unsafe impl Sync for IoVecMut<'_> {}
impl IoVecMut<'_> {
pub fn new(slice: &mut [u8]) -> Self {
Self {
_iov: libc::iovec {
iov_base: slice.as_mut_ptr() as *mut libc::c_void,
iov_len: slice.len(),
},
_phantom: PhantomData,
}
}
}
impl<'s> IoVecMut<'s> {
pub fn from_io_slice_mut<'a>(ioslice: &'a mut [IoSliceMut<'s>]) -> &'a mut [Self] {
unsafe { &mut *(ioslice as *mut [IoSliceMut] as *mut [Self]) }
}
}
impl<'s> std::ops::Deref for IoVecMut<'s> {
type Target = [u8];
#[inline]
fn deref(&self) -> &Self::Target {
unsafe { std::slice::from_raw_parts(self._iov.iov_base as *const u8, self._iov.iov_len) }
}
}
impl<'s> std::ops::DerefMut for IoVecMut<'s> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { std::slice::from_raw_parts_mut(self._iov.iov_base as *mut u8, self._iov.iov_len) }
}
}

View File

@ -1,5 +1,4 @@
pub mod cmsg;
pub mod iovec;
pub mod pipe;
pub mod polled_fd;
pub mod rw_traits;

View File

@ -1,11 +1,11 @@
use std::io::{self, IoSlice, IoSliceMut};
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
use std::ptr;
use std::task::{Context, Poll};
use std::{io, ptr};
use anyhow::Error;
use nix::sys::socket::{self, AddressFamily, SockAddr, SockFlag, SockType};
use crate::io::iovec::{IoVec, IoVecMut};
use crate::io::polled_fd::PolledFd;
use crate::poll_fn::poll_fn;
use crate::tools::AssertSendSync;
@ -94,7 +94,7 @@ impl SeqPacketSocket {
})
}
pub async fn sendmsg_vectored(&self, iov: &[IoVec<'_>]) -> io::Result<usize> {
pub async fn sendmsg_vectored(&self, iov: &[IoSlice<'_>]) -> io::Result<usize> {
let msg = AssertSendSync(libc::msghdr {
msg_name: ptr::null_mut(),
msg_namelen: 0,
@ -125,7 +125,7 @@ impl SeqPacketSocket {
#[allow(clippy::needless_lifetimes)]
pub async fn recvmsg_vectored(
&self,
iov: &mut [IoVecMut<'_>],
iov: &mut [IoSliceMut<'_>],
cmsg_buf: &mut [u8],
) -> io::Result<(usize, usize)> {
let mut msg = AssertSendSync(libc::msghdr {

View File

@ -2,10 +2,11 @@
use std::convert::TryFrom;
use std::ffi::CString;
use std::io::{self, IoSlice, IoSliceMut};
use std::mem;
use std::os::raw::{c_int, c_uint};
use std::os::unix::fs::FileExt;
use std::os::unix::io::{FromRawFd, RawFd};
use std::{io, mem};
use anyhow::{bail, format_err, Error};
use lazy_static::lazy_static;
@ -13,7 +14,6 @@ use libc::pid_t;
use nix::errno::Errno;
use crate::io::cmsg;
use crate::io::iovec::{IoVec, IoVecMut};
use crate::io::seq_packet::SeqPacketSocket;
use crate::process::PidFd;
use crate::seccomp::{SeccompNotif, SeccompNotifResp, SeccompNotifSizes};
@ -68,14 +68,14 @@ pub struct ProxyMessageBuffer {
mem_fd: Option<std::fs::File>,
}
unsafe fn io_vec_mut<T>(value: &mut T) -> IoVecMut {
IoVecMut::new(unsafe {
unsafe fn io_vec_mut<T>(value: &mut T) -> IoSliceMut {
IoSliceMut::new(unsafe {
std::slice::from_raw_parts_mut(value as *mut T as *mut u8, mem::size_of::<T>())
})
}
unsafe fn io_vec<T>(value: &T) -> IoVec {
IoVec::new(unsafe {
unsafe fn io_vec<T>(value: &T) -> IoSlice {
IoSlice::new(unsafe {
std::slice::from_raw_parts(value as *const T as *const u8, mem::size_of::<T>())
})
}
@ -126,18 +126,16 @@ impl ProxyMessageBuffer {
unsafe { io_vec_mut(&mut self.proxy_msg) },
unsafe { io_vec_mut(&mut self.seccomp_notif) },
unsafe { io_vec_mut(&mut self.seccomp_resp) },
IoVecMut::new(self.cookie_buf.as_mut_slice()),
IoSliceMut::new(self.cookie_buf.as_mut_slice()),
];
unsafe {
self.cookie_buf.set_len(0);
}
// receive:
let mut fd_cmsg_buf = cmsg::buffer::<[RawFd; 2]>();
let (datalen, cmsglen) = socket
.recvmsg_vectored(&mut iovec, &mut fd_cmsg_buf)
.await?;
let result = socket.recvmsg_vectored(&mut iovec, &mut fd_cmsg_buf).await;
unsafe {
self.cookie_buf.set_len(0);
}
let (datalen, cmsglen) = result?;
if datalen == 0 {
return Ok(false);