Skip to content

Commit

Permalink
remove syscall_u32 and add doc comment for syscall macro and MaybeFd
Browse files Browse the repository at this point in the history
  • Loading branch information
ihciah committed Nov 5, 2024
1 parent b74aec2 commit 7f30f9a
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 112 deletions.
6 changes: 6 additions & 0 deletions monoio/src/driver/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ pub(crate) struct CompletionMeta {
pub(crate) flags: u32,
}

/// MaybeFd is a wrapper for fd or a normal number. If it is marked as fd, it will close the fd when
/// dropped.
/// Use `into_inner` to take the inner fd or number and skip the drop.
///
/// This wrapper is designed to be used in the syscall return value. It can prevent fd leak when the
/// operation is cancelled.
#[derive(Debug)]
pub(crate) struct MaybeFd {
is_fd: bool,
Expand Down
15 changes: 6 additions & 9 deletions monoio/src/driver/op/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::{
use io_uring::{opcode, types};
#[cfg(windows)]
use {
crate::syscall,
std::os::windows::prelude::AsRawSocket,
windows_sys::Win32::Networking::WinSock::{
accept, socklen_t, INVALID_SOCKET, SOCKADDR_STORAGE,
Expand All @@ -19,8 +18,6 @@ use {
use super::{super::shared_fd::SharedFd, Op, OpAble};
#[cfg(any(feature = "legacy", feature = "poll-io"))]
use super::{driver::ready::Direction, MaybeFd};
#[cfg(all(unix, any(feature = "legacy", feature = "poll-io")))]
use crate::syscall_u32;

/// Accept
pub(crate) struct Accept {
Expand Down Expand Up @@ -79,7 +76,7 @@ impl OpAble for Accept {
let addr = self.addr.0.as_mut_ptr() as *mut _;
let len = &mut self.addr.1;

syscall!(accept@FD(fd as _, addr, len), PartialEq::eq, INVALID_SOCKET)
crate::syscall!(accept@FD(fd as _, addr, len), PartialEq::eq, INVALID_SOCKET)
}

#[cfg(all(any(feature = "legacy", feature = "poll-io"), unix))]
Expand Down Expand Up @@ -107,7 +104,7 @@ impl OpAble for Accept {
))]
return {
let flag = libc::SOCK_CLOEXEC | libc::SOCK_NONBLOCK;
syscall_u32!(accept4@FD(fd, addr, len, flag))
crate::syscall!(accept4@FD(fd, addr, len, flag))
};

// But not all platforms have the `accept4(2)` call. Luckily BSD (derived)
Expand All @@ -120,10 +117,10 @@ impl OpAble for Accept {
target_os = "redox"
))]
return {
let stream_fd = syscall_u32!(accept@FD(fd, addr, len))?;
let fd = stream_fd.fd() as i32;
syscall_u32!(fcntl@RAW(fd, libc::F_SETFD, libc::FD_CLOEXEC))?;
syscall_u32!(fcntl@RAW(fd, libc::F_SETFL, libc::O_NONBLOCK))?;
let stream_fd = crate::syscall!(accept@FD(fd, addr, len))?;
let fd = stream_fd.fd() as libc::c_int;
crate::syscall!(fcntl@RAW(fd, libc::F_SETFD, libc::FD_CLOEXEC))?;
crate::syscall!(fcntl@RAW(fd, libc::F_SETFL, libc::O_NONBLOCK))?;
Ok(stream_fd)
};
}
Expand Down
9 changes: 3 additions & 6 deletions monoio/src/driver/op/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use std::os::unix::io::RawFd;
#[cfg(all(target_os = "linux", feature = "iouring"))]
use io_uring::{opcode, types};
#[cfg(windows)]
use {
crate::syscall, std::os::windows::io::RawSocket,
windows_sys::Win32::Networking::WinSock::closesocket,
};
use {std::os::windows::io::RawSocket, windows_sys::Win32::Networking::WinSock::closesocket};

#[cfg(any(feature = "legacy", feature = "poll-io"))]
use super::MaybeFd;
Expand Down Expand Up @@ -52,9 +49,9 @@ impl OpAble for Close {
#[cfg(any(feature = "legacy", feature = "poll-io"))]
fn legacy_call(&mut self) -> io::Result<MaybeFd> {
#[cfg(unix)]
return crate::syscall_u32!(close@NON_FD(self.fd));
return crate::syscall!(close@NON_FD(self.fd));

#[cfg(windows)]
return syscall!(closesocket@NON_FD(self.fd as _), PartialEq::ne, 0);
return crate::syscall!(closesocket@NON_FD(self.fd as _), PartialEq::ne, 0);
}
}
6 changes: 3 additions & 3 deletions monoio/src/driver/op/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl OpAble for Connect {
endpoints.sae_dstaddr = self.socket_addr.as_ptr();
endpoints.sae_dstaddrlen = self.socket_addr_len;

return match crate::syscall_u32!(connectx@RAW(
return match crate::syscall!(connectx@RAW(
self.fd.raw_fd(),
&endpoints as *const _,
libc::SAE_ASSOCID_ANY,
Expand All @@ -86,7 +86,7 @@ impl OpAble for Connect {
}

#[cfg(unix)]
match crate::syscall_u32!(connect@RAW(
match crate::syscall!(connect@RAW(
self.fd.raw_fd(),
self.socket_addr.as_ptr(),
self.socket_addr_len,
Expand Down Expand Up @@ -158,7 +158,7 @@ impl OpAble for ConnectUnix {

#[cfg(any(feature = "legacy", feature = "poll-io"))]
fn legacy_call(&mut self) -> io::Result<MaybeFd> {
match crate::syscall_u32!(connect@RAW(
match crate::syscall!(connect@RAW(
self.fd.raw_fd(),
&self.socket_addr.0 as *const _ as *const _,
self.socket_addr.1
Expand Down
8 changes: 3 additions & 5 deletions monoio/src/driver/op/fsync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,13 @@ impl OpAble for Fsync {

#[cfg(all(any(feature = "legacy", feature = "poll-io"), unix))]
fn legacy_call(&mut self) -> io::Result<MaybeFd> {
use crate::syscall_u32;

#[cfg(target_os = "linux")]
if self.data_sync {
syscall_u32!(fdatasync@NON_FD(self.fd.raw_fd()))
crate::syscall!(fdatasync@NON_FD(self.fd.raw_fd()))
} else {
syscall_u32!(fsync@NON_FD(self.fd.raw_fd()))
crate::syscall!(fsync@NON_FD(self.fd.raw_fd()))
}
#[cfg(not(target_os = "linux"))]
syscall_u32!(fsync@NON_FD(self.fd.raw_fd()))
crate::syscall!(fsync@NON_FD(self.fd.raw_fd()))
}
}
4 changes: 1 addition & 3 deletions monoio/src/driver/op/mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ impl OpAble for MkDir {

#[cfg(all(any(feature = "legacy", feature = "poll-io"), unix))]
fn legacy_call(&mut self) -> std::io::Result<MaybeFd> {
use crate::syscall_u32;

syscall_u32!(mkdirat@NON_FD(
crate::syscall!(mkdirat@NON_FD(
libc::AT_FDCWD,
self.path.as_ptr(),
self.mode
Expand Down
4 changes: 1 addition & 3 deletions monoio/src/driver/op/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use io_uring::{opcode, types};
#[cfg(any(feature = "legacy", feature = "poll-io"))]
use super::{driver::ready::Direction, MaybeFd};
use super::{Op, OpAble};
#[cfg(all(unix, any(feature = "legacy", feature = "poll-io")))]
use crate::syscall_u32;
use crate::{driver::util::cstr, fs::OpenOptions};

/// Open a file
Expand Down Expand Up @@ -69,7 +67,7 @@ impl OpAble for Open {

#[cfg(all(any(feature = "legacy", feature = "poll-io"), not(windows)))]
fn legacy_call(&mut self) -> io::Result<MaybeFd> {
syscall_u32!(open@FD(
crate::syscall!(open@FD(
self.path.as_c_str().as_ptr(),
self.flags,
self.mode as libc::c_int
Expand Down
2 changes: 1 addition & 1 deletion monoio/src/driver/op/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl OpAble for PollAdd {
},
revents: 0,
};
let ret = crate::syscall_u32!(poll@RAW(&mut pollfd as *mut _, 1, 0))?;
let ret = crate::syscall!(poll@RAW(&mut pollfd as *mut _, 1, 0))?;
if ret == 0 {
return Err(ErrorKind::WouldBlock.into());
}
Expand Down
9 changes: 4 additions & 5 deletions monoio/src/driver/op/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,24 +313,23 @@ pub(crate) mod impls {
use libc::iovec;

use super::*;
use crate::syscall_u32;

/// A wrapper for [`libc::read`]
pub(crate) fn read(fd: i32, buf: *mut u8, len: usize) -> io::Result<MaybeFd> {
syscall_u32!(read@NON_FD(fd, buf as _, len))
crate::syscall!(read@NON_FD(fd, buf as _, len))
}

/// A wrapper of [`libc::pread`]
pub(crate) fn read_at(fd: i32, buf: *mut u8, len: usize, offset: u64) -> io::Result<MaybeFd> {
let offset = libc::off_t::try_from(offset)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "offset too big"))?;

syscall_u32!(pread@NON_FD(fd, buf as _, len, offset))
crate::syscall!(pread@NON_FD(fd, buf as _, len, offset))
}

/// A wrapper of [`libc::readv`]
pub(crate) fn read_vectored(fd: i32, buf_vec: *mut iovec, len: usize) -> io::Result<MaybeFd> {
syscall_u32!(readv@NON_FD(fd, buf_vec as _, len as _))
crate::syscall!(readv@NON_FD(fd, buf_vec as _, len as _))
}

/// A wrapper of [`libc::preadv`]
Expand All @@ -343,7 +342,7 @@ pub(crate) mod impls {
let offset = libc::off_t::try_from(offset)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "offset too big"))?;

syscall_u32!(preadv@NON_FD(fd, buf_vec as _, len as _, offset))
crate::syscall!(preadv@NON_FD(fd, buf_vec as _, len as _, offset))
}
}

Expand Down
10 changes: 5 additions & 5 deletions monoio/src/driver/op/recv.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(all(unix, any(feature = "legacy", feature = "poll-io")))]
use std::os::unix::prelude::AsRawFd;
use std::{
io,
mem::{transmute, MaybeUninit},
Expand All @@ -11,8 +13,6 @@ use {
crate::net::unix::SocketAddr as UnixSocketAddr,
libc::{sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t, AF_INET, AF_INET6},
};
#[cfg(all(unix, any(feature = "legacy", feature = "poll-io")))]
use {crate::syscall_u32, std::os::unix::prelude::AsRawFd};
#[cfg(all(windows, any(feature = "legacy", feature = "poll-io")))]
use {
std::os::windows::io::AsRawSocket,
Expand Down Expand Up @@ -98,7 +98,7 @@ impl<T: IoBufMut> OpAble for Recv<T> {
#[cfg(all(any(feature = "legacy", feature = "poll-io"), unix))]
fn legacy_call(&mut self) -> io::Result<MaybeFd> {
let fd = self.fd.as_raw_fd();
syscall_u32!(recv@NON_FD(
crate::syscall!(recv@NON_FD(
fd,
self.buf.write_ptr() as _,
self.buf.bytes_total().min(u32::MAX as usize),
Expand Down Expand Up @@ -237,7 +237,7 @@ impl<T: IoBufMut> OpAble for RecvMsg<T> {
#[cfg(all(any(feature = "legacy", feature = "poll-io"), unix))]
fn legacy_call(&mut self) -> io::Result<MaybeFd> {
let fd = self.fd.as_raw_fd();
syscall_u32!(recvmsg@NON_FD(fd, &mut *self.info.2, 0))
crate::syscall!(recvmsg@NON_FD(fd, &mut *self.info.2, 0))
}

#[cfg(all(any(feature = "legacy", feature = "poll-io"), windows))]
Expand Down Expand Up @@ -355,6 +355,6 @@ impl<T: IoBufMut> OpAble for RecvMsgUnix<T> {
#[cfg(any(feature = "legacy", feature = "poll-io"))]
fn legacy_call(&mut self) -> io::Result<MaybeFd> {
let fd = self.fd.as_raw_fd();
syscall_u32!(recvmsg@NON_FD(fd, &mut self.info.2 as *mut _, 0))
crate::syscall!(recvmsg@NON_FD(fd, &mut self.info.2 as *mut _, 0))
}
}
4 changes: 1 addition & 3 deletions monoio/src/driver/op/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ impl OpAble for Rename {

#[cfg(all(any(feature = "legacy", feature = "poll-io"), unix))]
fn legacy_call(&mut self) -> std::io::Result<MaybeFd> {
use crate::syscall_u32;

syscall_u32!(renameat@NON_FD(
crate::syscall!(renameat@NON_FD(
libc::AT_FDCWD,
self.from.as_ptr(),
libc::AT_FDCWD,
Expand Down
13 changes: 6 additions & 7 deletions monoio/src/driver/op/send.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#[cfg(all(unix, any(feature = "legacy", feature = "poll-io")))]
use std::os::unix::prelude::AsRawFd;
use std::{io, net::SocketAddr};

#[cfg(all(target_os = "linux", feature = "iouring"))]
use io_uring::{opcode, types};
use socket2::SockAddr;
#[cfg(all(windows, any(feature = "legacy", feature = "poll-io")))]
use {
crate::syscall,
std::os::windows::io::AsRawSocket,
windows_sys::Win32::Networking::WinSock::{send, WSASendMsg, SOCKET_ERROR},
};
#[cfg(all(unix, any(feature = "legacy", feature = "poll-io")))]
use {crate::syscall_u32, std::os::unix::prelude::AsRawFd};

use super::{super::shared_fd::SharedFd, Op, OpAble};
#[cfg(any(feature = "legacy", feature = "poll-io"))]
Expand Down Expand Up @@ -104,7 +103,7 @@ impl<T: IoBuf> OpAble for Send<T> {
#[cfg(not(target_os = "linux"))]
let flags = 0;

syscall_u32!(send@NON_FD(
crate::syscall!(send@NON_FD(
fd,
self.buf.read_ptr() as _,
self.buf.bytes_init(),
Expand All @@ -115,7 +114,7 @@ impl<T: IoBuf> OpAble for Send<T> {
#[cfg(all(any(feature = "legacy", feature = "poll-io"), windows))]
fn legacy_call(&mut self) -> io::Result<MaybeFd> {
let fd = self.fd.as_raw_socket();
syscall!(
crate::syscall!(
send@NON_FD(fd as _, self.buf.read_ptr(), self.buf.bytes_init() as _, 0),
PartialOrd::lt,
0
Expand Down Expand Up @@ -215,7 +214,7 @@ impl<T: IoBuf> OpAble for SendMsg<T> {
#[cfg(not(target_os = "linux"))]
const FLAGS: libc::c_int = 0;
let fd = self.fd.as_raw_fd();
syscall_u32!(sendmsg@NON_FD(fd, &*self.info.2, FLAGS))
crate::syscall!(sendmsg@NON_FD(fd, &*self.info.2, FLAGS))
}

#[cfg(all(any(feature = "legacy", feature = "poll-io"), windows))]
Expand Down Expand Up @@ -319,6 +318,6 @@ impl<T: IoBuf> OpAble for SendMsgUnix<T> {
#[cfg(not(target_os = "linux"))]
const FLAGS: libc::c_int = 0;
let fd = self.fd.as_raw_fd();
syscall_u32!(sendmsg@NON_FD(fd, &mut self.info.2 as *mut _, FLAGS))
crate::syscall!(sendmsg@NON_FD(fd, &mut self.info.2 as *mut _, FLAGS))
}
}
7 changes: 2 additions & 5 deletions monoio/src/driver/op/splice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use std::io;
use io_uring::{opcode, types};
#[cfg(all(unix, feature = "legacy"))]
use {
crate::{
driver::{op::MaybeFd, ready::Direction},
syscall_u32,
},
crate::driver::{op::MaybeFd, ready::Direction},
std::os::unix::prelude::AsRawFd,
};

Expand Down Expand Up @@ -97,7 +94,7 @@ impl OpAble for Splice {
let fd_out = self.fd_out.as_raw_fd();
let off_in = std::ptr::null_mut::<libc::loff_t>();
let off_out = std::ptr::null_mut::<libc::loff_t>();
syscall_u32!(splice@NON_FD(
crate::syscall!(splice@NON_FD(
fd_in,
off_in,
fd_out,
Expand Down
10 changes: 5 additions & 5 deletions monoio/src/driver/op/statx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl OpAble for FdStatx {
fn legacy_call(&mut self) -> std::io::Result<MaybeFd> {
use std::os::fd::AsRawFd;

crate::syscall_u32!(statx@NON_FD(
crate::syscall!(statx@NON_FD(
self.inner.as_raw_fd(),
c"".as_ptr(),
libc::AT_EMPTY_PATH,
Expand All @@ -104,7 +104,7 @@ impl OpAble for FdStatx {
fn legacy_call(&mut self) -> std::io::Result<MaybeFd> {
use std::os::fd::AsRawFd;

crate::syscall_u32!(fstat@NON_FD(
crate::syscall!(fstat@NON_FD(
self.inner.as_raw_fd(),
self.stat_buf.as_mut_ptr() as *mut _
))
Expand Down Expand Up @@ -173,7 +173,7 @@ impl OpAble for PathStatx {

#[cfg(all(any(feature = "legacy", feature = "poll-io"), target_os = "linux"))]
fn legacy_call(&mut self) -> std::io::Result<MaybeFd> {
crate::syscall_u32!(statx@NON_FD(
crate::syscall!(statx@NON_FD(
libc::AT_FDCWD,
self.inner.as_ptr(),
self.flags,
Expand All @@ -190,12 +190,12 @@ impl OpAble for PathStatx {
#[cfg(all(any(feature = "legacy", feature = "poll-io"), target_os = "macos"))]
fn legacy_call(&mut self) -> std::io::Result<MaybeFd> {
if self.follow_symlinks {
crate::syscall_u32!(stat@NON_FD(
crate::syscall!(stat@NON_FD(
self.inner.as_ptr(),
self.stat_buf.as_mut_ptr() as *mut _
))
} else {
crate::syscall_u32!(lstat@NON_FD(
crate::syscall!(lstat@NON_FD(
self.inner.as_ptr(),
self.stat_buf.as_mut_ptr() as *mut _
))
Expand Down
2 changes: 1 addition & 1 deletion monoio/src/driver/op/symlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl OpAble for Symlink {

#[cfg(any(feature = "legacy", feature = "poll-io"))]
fn legacy_call(&mut self) -> std::io::Result<MaybeFd> {
crate::syscall_u32!(symlink@NON_FD(
crate::syscall!(symlink@NON_FD(
self.from.as_c_str().as_ptr(),
self.to.as_c_str().as_ptr()
))
Expand Down
Loading

0 comments on commit 7f30f9a

Please sign in to comment.