Skip to content

Commit 28036de

Browse files
authored
Make io_uring_enter more ergonomic. (#1345)
* Make `io_uring_enter` more ergonomic. Define a `Sigset` type which fully encapsulates the `linux_raw_sys` type, and change `io_uring_enter`'s `arg`/`size` arguments to a `&T` argument where `T` must either be `Sigset` or `io_uring_getevents_arg` (with `IoringEnterFlags::EXT_ARG` set accordingly). Supporting this, also define a `KernelSigSet` type, and define our own versions of `KernelSigaction` and friends. * Define the `check_renamed_struct` macro. * Fix FFI type imports.
1 parent 29e7524 commit 28036de

9 files changed

Lines changed: 1069 additions & 278 deletions

File tree

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ advice applies to the end of the file. To convert an arbitrary `u64` value to
230230

231231
[`rustix::fs::fadvise`]: https://docs.rs/rustix/1.0.0/rustix/fs/fn.fadvise.html
232232

233+
[`rustix::io_uring::io_uring_enter`] has a new signature; instead of `arg` and
234+
`size` providing a raw `*mut c_void` and `usize`, it now takes any `&T` and
235+
infers the size.
236+
237+
[`rustix::io_uring::io_uring_enter`]: https://docs.rs/rustix/1.0.0/rustix/io_uring/fn.io_uring_enter.html
238+
233239
The [`sigmask`] and [`ts`] fields of [`rustix::io_uring::getevents_arg`]
234240
changed from `u64` to [`rustix::io_uring::io_uring_ptr`], to better preserve
235241
pointer provenance.

Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ name = "mod"
8383
harness = false
8484

8585
[package.metadata.docs.rs]
86-
features = ["all-apis", "use-libc-sigrt"]
86+
features = ["all-apis"]
8787
targets = [
8888
"x86_64-unknown-linux-gnu",
8989
"i686-unknown-linux-gnu",
@@ -196,9 +196,6 @@ all-apis = [
196196
"time",
197197
]
198198

199-
# Enable `Signal::rt` and related features, which depend on libc.
200-
use-libc-sigrt = []
201-
202199
# When using the linux_raw backend, should we use libc for reading the aux
203200
# vectors, instead of reading them ourselves from /proc/self/auxv?
204201
use-libc-auxv = []

src/check_types.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,26 @@ macro_rules! check_struct {
106106
$(check_struct_field!($name, $field));*
107107
};
108108
}
109+
110+
/// For the case of renaming, check all fields of a struct.
111+
macro_rules! check_renamed_struct {
112+
($to_struct:ident, $from_struct:ident, $($field:ident),*) => {
113+
// Check the size and alignment.
114+
check_renamed_type!($to_struct, $from_struct);
115+
116+
// Check that we have all the fields.
117+
if false {
118+
#[allow(unreachable_code)]
119+
let _test = $to_struct {
120+
$($field: panic!()),*
121+
};
122+
#[allow(unreachable_code)]
123+
let _test = c::$from_struct {
124+
$($field: panic!()),*
125+
};
126+
}
127+
128+
// Check that the fields have the right sizes and offsets.
129+
$(check_renamed_struct_field!($to_struct, $from_struct, $field));*
130+
};
131+
}

src/io_uring/mod.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727
mod bindgen_types;
2828

2929
use crate::fd::{AsFd, BorrowedFd, OwnedFd, RawFd};
30+
use crate::utils::option_as_ptr;
3031
use crate::{backend, io};
3132
use bindgen_types::*;
3233
use core::cmp::Ordering;
3334
use core::ffi::c_void;
3435
use core::hash::{Hash, Hasher};
35-
use core::mem::MaybeUninit;
36+
use core::mem::{size_of, MaybeUninit};
3637
use core::ptr::{null_mut, write_bytes};
3738
use linux_raw_sys::net;
3839

@@ -47,12 +48,12 @@ pub use crate::fs::{
4748
};
4849
pub use crate::io::ReadWriteFlags;
4950
pub use crate::net::{RecvFlags, SendFlags, SocketFlags};
51+
pub use crate::sigset::SigSet;
5052
pub use crate::thread::futex::{
5153
Wait as FutexWait, WaitFlags as FutexWaitFlags, WaitPtr as FutexWaitPtr,
5254
WaitvFlags as FutexWaitvFlags,
5355
};
5456
pub use crate::timespec::{Nsecs, Secs, Timespec};
55-
pub use linux_raw_sys::general::sigset_t;
5657

5758
pub use net::{__kernel_sockaddr_storage as sockaddr_storage, msghdr, sockaddr, socklen_t};
5859

@@ -123,35 +124,48 @@ pub unsafe fn io_uring_register_with<Fd: AsFd>(
123124
backend::io_uring::syscalls::io_uring_register_with(fd.as_fd(), opcode, flags, arg, nr_args)
124125
}
125126

126-
/// `io_uring_enter(fd, to_submit, min_complete, flags, arg, size)`—Initiate
127-
/// and/or complete asynchronous I/O.
127+
/// `io_uring_enter2(fd, to_submit, min_complete, flags, arg,
128+
/// size_of_val(arg))`—Initiate and/or complete asynchronous I/O.
128129
///
129130
/// # Safety
130131
///
131132
/// io_uring operates on raw pointers and raw file descriptors. Users are
132133
/// responsible for ensuring that memory and resources are only accessed in
133134
/// valid ways.
134135
///
136+
/// And, `arg` must either be a [`SigSet`] or a [`io_uring_getevents_arg`], and
137+
/// `flags` must contain [`IoringEnterFlags::EXT_ARG`] if and only if it's a
138+
/// `io_uring_getevents_arg`.
139+
///
135140
/// # References
136141
/// - [Linux]
137142
///
138143
/// [Linux]: https://www.man7.org/linux/man-pages/man2/io_uring_enter.2.html
144+
#[doc(alias = "io_uring_enter2")]
139145
#[inline]
140-
pub unsafe fn io_uring_enter<Fd: AsFd>(
146+
pub unsafe fn io_uring_enter<Fd: AsFd, T>(
141147
fd: Fd,
142148
to_submit: u32,
143149
min_complete: u32,
144150
flags: IoringEnterFlags,
145-
arg: *const c_void,
146-
size: usize,
151+
arg: Option<&T>,
147152
) -> io::Result<u32> {
153+
debug_assert!(
154+
size_of::<T>() == size_of::<SigSet>()
155+
|| size_of::<T>() == size_of::<io_uring_getevents_arg>()
156+
);
157+
debug_assert!(
158+
(size_of::<T>() == size_of::<io_uring_getevents_arg>())
159+
== (flags.contains(IoringEnterFlags::EXT_ARG))
160+
);
161+
148162
backend::io_uring::syscalls::io_uring_enter(
149163
fd.as_fd(),
150164
to_submit,
151165
min_complete,
152166
flags,
153-
arg,
154-
size,
167+
option_as_ptr(arg).cast::<c_void>(),
168+
size_of::<T>(),
155169
)
156170
}
157171

0 commit comments

Comments
 (0)