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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! 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, OwnedFd};
use std::sync::Mutex;
use std::sync::atomic::AtomicBool;
use std::{fmt, ptr, task};
use crate::{debug_detail, lock, syscall};
pub(crate) mod config;
mod cq;
pub(crate) mod fd;
pub(crate) mod fs;
pub(crate) mod io;
pub(crate) mod mem;
pub(crate) mod net;
pub(crate) mod op;
pub(crate) mod pipe;
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;
#[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>>,
/// Boolean indicating a thread is [`Ring::poll`]ing.
is_polling: AtomicBool,
/// 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: Option<&mut Vec<Event>>,
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 {
Some(ref mut events) => {
debug_assert!(events.is_empty());
(events.as_mut_ptr().cast(), events.capacity() as _)
}
None => (changes.as_mut_ptr().cast(), changes.capacity() 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 {
Some(events) => {
changes.clear();
events
}
None => 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");
}
events.map(Vec::clear);
changes.clear();
return;
}
};
for event in events.iter() {
log::trace!(event:?; "got event");
if let Some(err) = event.error() {
log::warn!(event:?; "submitted change has an error: {err}, dropping it");
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 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.
if event.0.flags & libc::EV_EOF == 0 {
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();
}
}
/// 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);
impl Event {
/// Returns an error from the event, if any.
#[allow(clippy::unnecessary_cast)]
fn error(&self) -> Option<io::Error> {
// We can't use references to packed structures (in checking the ignored
// errors), so we need copy the data out before use.
let data = self.0.data as i64;
// Check for the error flag, the actual error will be in the `data`
// field.
//
// Older versions of macOS (OS X 10.11 and 10.10 have been witnessed)
// can return EPIPE when registering a pipe file descriptor where the
// other end has already disappeared. For example code that creates a
// pipe, closes a file descriptor, and then registers the other end will
// see an EPIPE returned from `register`.
//
// It also turns out that kevent will still report events on the file
// descriptor, telling us that it's readable/hup at least after we've
// done this registration. As a result we just ignore `EPIPE` here
// instead of propagating it.
//
// More info can be found at tokio-rs/mio#582.
//
// The ENOENT error informs us that a filter we're trying to remove
// wasn't there in first place, but we don't really care since our goal
// is accomplished.
if (self.0.flags & libc::EV_ERROR != 0)
&& data != 0
&& data != libc::EPIPE.into()
&& data != libc::ENOENT.into()
{
Some(io::Error::from_raw_os_error(data as i32))
} else {
None
}
}
}
// SAFETY: `libc::kevent` is thread safe.
unsafe impl Send for Event {}
unsafe impl Sync for Event {}
impl fmt::Debug for Event {
#[allow(clippy::too_many_lines)] // The helper types and cfg attributes make this long.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
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,
#[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_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 FflagsDetails(libc::c_uint),
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos",
))]
libc::NOTE_TRIGGER,
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos",
))]
libc::NOTE_FFNOP,
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos",
))]
libc::NOTE_FFAND,
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos",
))]
libc::NOTE_FFOR,
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos",
))]
libc::NOTE_FFCOPY,
libc::NOTE_LOWAT,
libc::NOTE_DELETE,
libc::NOTE_WRITE,
#[cfg(target_os = "dragonfly")]
libc::NOTE_OOB,
#[cfg(target_os = "openbsd")]
libc::NOTE_EOF,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_EXTEND,
libc::NOTE_ATTRIB,
libc::NOTE_LINK,
libc::NOTE_RENAME,
libc::NOTE_REVOKE,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_NONE,
#[cfg(any(target_os = "openbsd"))]
libc::NOTE_TRUNCATE,
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,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_EXITSTATUS,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_EXIT_DETAIL,
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
))]
libc::NOTE_TRACK,
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
))]
libc::NOTE_TRACKERR,
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
))]
libc::NOTE_CHILD,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_EXIT_DECRYPTFAIL,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_EXIT_MEMORY,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_EXIT_CSERROR,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_VM_PRESSURE,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_VM_PRESSURE_TERMINATE,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_VM_PRESSURE_SUDDEN_TERMINATE,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_VM_ERROR,
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_SECONDS,
#[cfg(any(target_os = "freebsd"))]
libc::NOTE_MSECONDS,
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_USECONDS,
#[cfg(any(
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_NSECONDS,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_ABSOLUTE,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_LEEWAY,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_CRITICAL,
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos"
))]
libc::NOTE_BACKGROUND,
);
// Can't reference fields in packed structures.
let udata = self.0.udata;
let ident = self.0.ident;
let data = self.0.data;
f.debug_struct("kqueue::Event")
.field("udata", &udata)
.field("ident", &ident)
.field("filter", &FilterDetails(self.0.filter))
.field("flags", &FlagsDetails(self.0.flags))
.field("fflags", &FflagsDetails(self.0.fflags))
.field("data", &data)
.finish()
}
}