jay-compositor 1.15.0

The Jay compositor
use crate::io_uring::IoUring;
use crate::io_uring::IoUringData;
use crate::io_uring::IoUringError;
use crate::io_uring::IoUringTaskId;
use crate::io_uring::Task;
use crate::io_uring::pending_result::PendingResult;
use crate::io_uring::sys::IORING_OP_SENDMSG;
use crate::io_uring::sys::io_uring_cqe;
use crate::io_uring::sys::io_uring_sqe;
use crate::time::Time;
use crate::utils::buf::Buf;
use crate::utils::compat::IovLength;
use crate::utils::vec_ext::UninitVecExt;
use derivative::Derivative;
use std::mem::MaybeUninit;
use std::ptr;
use std::rc::Rc;
use uapi::OwnedFd;
use uapi::c;

impl IoUring {
    pub async fn sendmsg_one(
        &self,
        fd: &Rc<OwnedFd>,
        buf: Buf,
        fds: Vec<Rc<OwnedFd>>,
        timeout: Option<Time>,
    ) -> Result<usize, IoUringError> {
        self.sendmsg(fd, &mut [buf], fds, timeout).await
    }

    pub async fn sendmsg(
        &self,
        fd: &Rc<OwnedFd>,
        bufs: &mut [Buf],
        fds: Vec<Rc<OwnedFd>>,
        timeout: Option<Time>,
    ) -> Result<usize, IoUringError> {
        self.ring.check_destroyed()?;
        let id = self.ring.id();
        let pr = self.ring.pending_results.acquire();
        {
            let mut st = self.ring.cached_sendmsg.pop().unwrap_or_default();
            st.fds = fds;
            if st.fds.len() > 0 {
                let mut fd_ids = self.ring.fd_ids_scratch.borrow_mut();
                fd_ids.clear();
                fd_ids.extend(st.fds.iter().map(|f| f.raw()));
                let space = uapi::cmsg_space(size_of_val(&fd_ids[..]));
                st.cmsg.clear();
                st.cmsg.reserve(space);
                st.cmsg.set_len_safe(space);
                let mut hdr: c::cmsghdr = uapi::pod_zeroed();
                hdr.cmsg_level = c::SOL_SOCKET;
                hdr.cmsg_type = c::SCM_RIGHTS;
                uapi::cmsg_write(&mut &mut st.cmsg[..], hdr, &fd_ids[..]).unwrap();
                st.msghdr.msg_control = st.cmsg.as_ptr() as _;
                st.msghdr.msg_controllen = st.cmsg.len() as _;
            } else {
                st.msghdr.msg_control = ptr::null_mut();
                st.msghdr.msg_controllen = 0;
            }
            st.id = id.id;
            st.fd = fd.raw();
            st.bufs.clear();
            st.bufs.extend(bufs.iter_mut().map(|b| b.clone()));
            st.iovecs.clear();
            st.iovecs.extend(bufs.iter().map(|b| c::iovec {
                iov_base: b.as_ptr() as _,
                iov_len: b.len(),
            }));
            st.msghdr.msg_iov = st.iovecs.as_ptr() as _;
            st.msghdr.msg_iovlen = st.iovecs.len() as IovLength;
            st.data = Some(SendmsgTaskData {
                _fd: fd.clone(),
                res: pr.clone(),
            });
            st.has_timeout = timeout.is_some();
            self.ring.schedule(st);
            if let Some(timeout) = timeout {
                self.schedule_timeout_link(timeout);
            }
        }
        Ok(pr.await? as _)
    }
}

struct SendmsgTaskData {
    _fd: Rc<OwnedFd>,
    res: PendingResult,
}

#[derive(Derivative)]
#[derivative(Default)]
pub struct SendmsgTask {
    id: IoUringTaskId,
    iovecs: Vec<c::iovec>,
    #[derivative(Default(value = "uapi::pod_zeroed()"))]
    msghdr: c::msghdr,
    bufs: Vec<Buf>,
    fd: i32,
    has_timeout: bool,
    fds: Vec<Rc<OwnedFd>>,
    cmsg: Vec<MaybeUninit<u8>>,
    data: Option<SendmsgTaskData>,
}

unsafe impl Task for SendmsgTask {
    fn id(&self) -> IoUringTaskId {
        self.id
    }

    fn complete(mut self: Box<Self>, ring: &IoUringData, cqe: &io_uring_cqe) {
        let res = cqe.res;
        self.fds.clear();
        self.bufs.clear();
        if let Some(data) = self.data.take() {
            data.res.complete(res);
        }
        ring.cached_sendmsg.push(self);
    }

    fn encode(&self, sqe: &mut io_uring_sqe) {
        sqe.opcode = IORING_OP_SENDMSG;
        sqe.fd = self.fd;
        sqe.u2.addr = &self.msghdr as *const _ as _;
        sqe.u3.msg_flags = c::MSG_NOSIGNAL as _;
    }

    fn has_timeout(&self) -> bool {
        self.has_timeout
    }
}