1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//! kqueue implementation.
//!
//! Manuals:
//! * <https://man.freebsd.org/cgi/man.cgi?query=kqueue>
//! * <https://man.openbsd.org/kqueue>
//! * <https://www.dragonflybsd.org/cgi/web-man/?command=kqueue>
//! * <https://man.netbsd.org/kqueue.2>
use std::mem::{drop as unlock, swap};
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
use std::sync::Mutex;
use std::{fmt, ptr, task};
use crate::{PollingState, debug_detail, lock, syscall};
pub(crate) mod config;
mod cq;
pub(crate) mod fd;
pub(crate) mod fs;
pub(crate) mod fs_notify;
pub(crate) mod io;
pub(crate) mod mem;
pub(crate) mod net;
pub(crate) mod op;
pub(crate) mod pipe;
pub(crate) mod poll;
pub(crate) mod process;
mod sq;
pub(crate) use config::Config;
pub(crate) use cq::Completions;
pub(crate) use sq::Submissions;
use cq::WAKE_USER_DATA;
fn kqueue() -> io::Result<OwnedFd> {
// SAFETY: `kqueue(2)` ensures the fd is valid.
let kq = unsafe { OwnedFd::from_raw_fd(syscall!(kqueue())?) };
syscall!(fcntl(kq.as_raw_fd(), libc::F_SETFD, libc::FD_CLOEXEC))?;
Ok(kq)
}
#[derive(Debug)]
pub(crate) struct Shared {
/// Maximum size of the change list before it's submitted to the kernel,
/// without waiting on a call to poll.
max_change_list_size: u32,
/// Batched events to register.
change_list: Mutex<Vec<Event>>,
polling: PollingState,
/// kqueue(2) file descriptor.
kq: OwnedFd,
}
impl Shared {
/// Reuse the allocation of a change list.
///
/// Reusing allocations (if it makes sense).
fn reuse_change_list(&self, mut changes: Vec<Event>) {
if changes.is_empty() && changes.capacity() == 0 {
return;
}
let mut change_list = lock(&self.change_list);
if changes.capacity() >= change_list.capacity() {
swap(&mut *change_list, &mut changes); // Reuse allocation.
}
change_list.append(&mut changes);
unlock(change_list); // Unlock before any deallocations.
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_wrap)]
// False positive, see
// <https://github.com/rust-lang/rust-clippy/issues/4737>
#[allow(clippy::debug_assert_with_mut_call)]
fn kevent(
&self,
changes: &mut Vec<Event>,
mut events: UseEvents<'_>,
timeout: Option<&libc::timespec>,
) {
// SAFETY: casting `Event` to `libc::kevent` is safe due to
// `repr(transparent)` on `Event`.
let (events_ptr, events_len) = match events {
UseEvents::Some(ref mut events) => {
debug_assert!(events.is_empty());
(events.as_mut_ptr().cast(), events.capacity() as _)
}
UseEvents::UseChangeList => (changes.as_mut_ptr().cast(), changes.len() as _),
UseEvents::UseChangeListWithWakeUp => {
// When we submit the changes with a wakeup event we do NOT want
// to process the wakeup event itself, as that would mean we
// don't wake up the thread currently calling Ring::poll. Which
// would defeat the entire purpose of the wake up event.
//
// We do this by providing 1 less space in the events list than
// the amount of changes we submit (changes.len() - 1).
//
// Make sure the last event is a wakeup event.
debug_assert!(
matches!(changes.last(), Some(e) if e.0.filter == libc::EVFILT_USER && e.0.udata == cq::WAKE_USER_DATA)
);
(changes.as_mut_ptr().cast(), (changes.len() - 1) as _)
}
};
let result = syscall!(kevent(
self.kq.as_raw_fd(),
changes.as_ptr().cast(),
changes.len() as _,
events_ptr,
events_len,
timeout.map_or(ptr::null_mut(), ptr::from_ref),
));
let events = match result {
// SAFETY: `kevent` ensures that `n` events are written.
Ok(n) => {
let events = match events {
UseEvents::Some(events) => {
changes.clear();
events
}
UseEvents::UseChangeList | UseEvents::UseChangeListWithWakeUp => changes,
};
unsafe { events.set_len(n as usize) }
events
}
Err(err) => {
// According to the manual page of FreeBSD: "When kevent() call
// fails with EINTR error, all changes in the changelist have
// been applied", so we can safely ignore it. We'll have zero
// completions though.
if err.raw_os_error() != Some(libc::EINTR) && !changes.is_empty() {
log::warn!(changes:?; "failed to submit change list: {err}, dropping changes");
}
if let UseEvents::Some(events) = events {
events.clear();
}
changes.clear();
return;
}
};
for event in events.iter() {
log::trace!(event:?; "got event");
if event.0.flags & libc::EV_ERROR != 0 && event.0.data == 0 {
// If the error number (event.data) is zero it means the event
// was succesfully submitted and can be safely ignored.
continue;
}
match event.0.filter {
libc::EVFILT_USER if event.0.udata == WAKE_USER_DATA => {}
libc::EVFILT_USER => {
let ptr = event.0.udata.cast::<fd::SharedState>();
debug_assert!(!ptr.is_null());
// SAFETY: see fd::State::drop.
unsafe { ptr::drop_in_place(ptr) };
}
libc::EVFILT_READ | libc::EVFILT_WRITE => {
let ptr = event.0.udata.cast::<fd::SharedState>();
debug_assert!(!ptr.is_null());
// SAFETY: in kqueue::op we ensure that the pointer is
// always valid (the kernel should copy it over for us).
lock(unsafe { &*ptr }).wake(event);
}
libc::EVFILT_PROC => {
// In some cases a second EVFILT_PROC event is returned, at
// least on macOS, that would case a use after free if we
// tried to use the same user_data to wake the Waker again.
// So we only continue here if EV_EOF or EV_ERROR is set,
// which should always be the last event.
if event.0.flags & (libc::EV_EOF | libc::EV_ERROR) == 0 {
log::trace!(event:?; "skipping event");
continue;
}
// Wake the future that was waiting for the result.
// SAFETY: WaitIdOp set this pointer for us.
unsafe { Box::<task::Waker>::from_raw(event.0.udata.cast()).wake() };
}
_ => log::debug!(event:?; "unexpected event, ignoring it"),
}
}
events.clear();
}
}
enum UseEvents<'a> {
Some(&'a mut Vec<Event>),
UseChangeList,
UseChangeListWithWakeUp,
}
/// Wrapper around `libc::kevent` to implementation traits and methods.
///
/// This is both a submission and a completion event.
#[repr(transparent)] // Requirement for `kevent` calls.
pub(crate) struct Event(libc::kevent);
// SAFETY: `libc::kevent` is thread safe.
unsafe impl Send for Event {}
unsafe impl Sync for Event {}
impl Event {
#[allow(clippy::too_many_lines)] // The helper types and cfg attributes make this long.
pub(crate) fn fmt<'a, 'b, 'f>(
&self,
f: &'f mut fmt::DebugStruct<'a, 'b>,
) -> &'f mut fmt::DebugStruct<'a, 'b> {
debug_detail!(
match FilterDetails(libc::c_short),
libc::EVFILT_READ,
libc::EVFILT_WRITE,
libc::EVFILT_AIO,
libc::EVFILT_VNODE,
libc::EVFILT_PROC,
libc::EVFILT_SIGNAL,
libc::EVFILT_TIMER,
#[cfg(target_os = "freebsd")]
libc::EVFILT_PROCDESC,
#[cfg(any(
target_os = "freebsd",
target_os = "dragonfly",
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos",
))]
libc::EVFILT_FS,
#[cfg(target_os = "freebsd")]
libc::EVFILT_LIO,
libc::EVFILT_USER,
#[cfg(target_os = "freebsd")]
libc::EVFILT_SENDFILE,
#[cfg(target_os = "freebsd")]
libc::EVFILT_EMPTY,
#[cfg(target_os = "dragonfly")]
libc::EVFILT_EXCEPT,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::EVFILT_MACHPORT,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::EVFILT_VM,
);
debug_detail!(
bitset FlagsDetails(libc::c_ushort),
libc::EV_ADD,
libc::EV_DELETE,
libc::EV_ENABLE,
libc::EV_DISABLE,
libc::EV_ONESHOT,
libc::EV_CLEAR,
libc::EV_RECEIPT,
libc::EV_DISPATCH,
#[cfg(target_os = "freebsd")]
libc::EV_DROP,
libc::EV_FLAG1,
libc::EV_ERROR,
libc::EV_EOF,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::EV_OOBAND,
#[cfg(target_os = "dragonfly")]
libc::EV_NODATA,
);
debug_detail!(
bitset IoFlagsDetails(libc::c_uint), // For EVFILT_{READ|WRITE}.
libc::NOTE_LOWAT,
#[cfg(target_os = "freebsd")]
libc::NOTE_FILE_POLL,
);
debug_detail!(
bitset UserFlagsDetails(libc::c_uint), // For EVFILT_USER.
libc::NOTE_TRIGGER,
libc::NOTE_FFNOP,
libc::NOTE_FFAND,
libc::NOTE_FFOR,
libc::NOTE_FFCOPY,
libc::NOTE_FFCTRLMASK,
libc::NOTE_FFLAGSMASK,
);
debug_detail!(
bitset ProcFlagsDetails(libc::c_uint), // For EVFILT_PROC.
libc::NOTE_EXIT,
libc::NOTE_FORK,
libc::NOTE_EXEC,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_SIGNAL,
libc::NOTE_TRACK,
libc::NOTE_TRACKERR,
libc::NOTE_CHILD,
);
debug_detail!(
bitset SignalFlagsDetails(libc::c_uint), // For EVFILT_SIGNAL.
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_SIGNAL,
);
debug_detail!(
bitset VnodeFlagsDetails(libc::c_uint), // For EVFILT_VNODE.
libc::NOTE_ATTRIB,
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
libc::NOTE_CLOSE,
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
libc::NOTE_CLOSE_WRITE,
libc::NOTE_DELETE,
libc::NOTE_EXTEND,
libc::NOTE_LINK,
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
libc::NOTE_OPEN,
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
libc::NOTE_READ,
libc::NOTE_RENAME,
libc::NOTE_REVOKE,
libc::NOTE_WRITE,
#[cfg(target_os = "openbsd")]
libc::NOTE_TRUNCATE,
);
// Can't reference fields in packed structures.
let udata = self.0.udata;
let ident = self.0.ident;
let data = self.0.data;
let fflags = self.0.fflags;
f.field("udata", &udata)
.field("ident", &ident)
.field("filter", &FilterDetails(self.0.filter))
.field("flags", &FlagsDetails(self.0.flags))
.field(
"fflags",
match self.0.filter {
libc::EVFILT_READ | libc::EVFILT_WRITE => IoFlagsDetails::from_ref(&fflags),
libc::EVFILT_USER => UserFlagsDetails::from_ref(&fflags),
libc::EVFILT_PROC => ProcFlagsDetails::from_ref(&fflags),
libc::EVFILT_SIGNAL => SignalFlagsDetails::from_ref(&fflags),
libc::EVFILT_VNODE => VnodeFlagsDetails::from_ref(&fflags),
_ => &fflags,
},
)
.field("data", &data)
}
}
impl fmt::Debug for Event {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = f.debug_struct("kqueue::Event");
self.fmt(&mut f).finish()
}
}