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
use std::{io, os::unix::io::RawFd};
use nix::{
libc::{c_long, time_t, timespec},
sys::event::{kevent_ts, kqueue, EventFilter, EventFlag, FilterFlag, KEvent},
};
use super::{Interest, Mode, PollEvent, Readiness, Token};
pub struct Kqueue {
kq: RawFd,
}
fn mode_to_flag(mode: Mode) -> EventFlag {
match mode {
Mode::Level => EventFlag::empty(),
Mode::OneShot => EventFlag::EV_DISPATCH,
Mode::Edge => EventFlag::EV_CLEAR,
}
}
impl Kqueue {
// Kqueue is always high precision
pub(crate) fn new(_high_precision: bool) -> crate::Result<Kqueue> {
let kq = kqueue()?;
Ok(Kqueue { kq })
}
pub(crate) fn poll(
&mut self,
timeout: Option<std::time::Duration>,
) -> crate::Result<Vec<PollEvent>> {
let mut buffer = [KEvent::new(
0,
EventFilter::EVFILT_READ,
EventFlag::empty(),
FilterFlag::empty(),
0,
0,
); 32];
let nevents = kevent_ts(
self.kq,
&[],
&mut buffer,
timeout.map(|d| timespec {
tv_sec: d.as_secs() as time_t,
tv_nsec: d.subsec_nanos() as c_long,
}),
)?;
let ret = buffer
.iter()
.take(nevents)
.map(|event| {
// The kevent data field in Rust's libc FFI bindings is an
// intptr_t, which is specified to allow this kind of
// conversion.
let token_ptr = event.udata() as usize as *const Token;
PollEvent {
readiness: Readiness {
readable: event.filter() == Ok(EventFilter::EVFILT_READ),
writable: event.filter() == Ok(EventFilter::EVFILT_WRITE),
error: event.flags().contains(EventFlag::EV_ERROR) && event.data() != 0,
},
// Why this is safe: it points to memory boxed and owned by
// the parent Poller type.
token: unsafe { *token_ptr },
}
})
.collect();
Ok(ret)
}
pub fn register(
&mut self,
fd: RawFd,
interest: Interest,
mode: Mode,
token: *const Token,
) -> crate::Result<()> {
self.reregister(fd, interest, mode, token)
}
pub fn reregister(
&mut self,
fd: RawFd,
interest: Interest,
mode: Mode,
token: *const Token,
) -> crate::Result<()> {
let write_flags = if interest.writable {
EventFlag::EV_ADD | EventFlag::EV_RECEIPT | mode_to_flag(mode)
} else {
EventFlag::EV_DELETE
};
let read_flags = if interest.readable {
EventFlag::EV_ADD | EventFlag::EV_RECEIPT | mode_to_flag(mode)
} else {
EventFlag::EV_DELETE
};
let changes = [
KEvent::new(
fd as usize,
EventFilter::EVFILT_WRITE,
write_flags,
FilterFlag::empty(),
0,
token as usize as isize,
),
KEvent::new(
fd as usize,
EventFilter::EVFILT_READ,
read_flags,
FilterFlag::empty(),
0,
token as usize as isize,
),
];
let mut out = [
KEvent::new(
0,
EventFilter::EVFILT_WRITE,
EventFlag::empty(),
FilterFlag::empty(),
0,
0,
),
KEvent::new(
0,
EventFilter::EVFILT_READ,
EventFlag::empty(),
FilterFlag::empty(),
0,
0,
),
];
kevent_ts(self.kq, &changes, &mut out, None)?;
for o in &out {
if o.flags().contains(EventFlag::EV_ERROR) && o.data() != 0 {
let e = io::Error::from_raw_os_error(o.data() as i32);
// ignore NotFound error which is raised if we tried to remove a non-existent filter
if e.kind() != io::ErrorKind::NotFound {
return Err(e.into());
}
}
}
Ok(())
}
pub fn unregister(&mut self, fd: RawFd) -> crate::Result<()> {
let changes = [
KEvent::new(
fd as usize,
EventFilter::EVFILT_WRITE,
EventFlag::EV_DELETE,
FilterFlag::empty(),
0,
0,
),
KEvent::new(
fd as usize,
EventFilter::EVFILT_READ,
EventFlag::EV_DELETE,
FilterFlag::empty(),
0,
0,
),
];
let mut out = [
KEvent::new(
0,
EventFilter::EVFILT_WRITE,
EventFlag::empty(),
FilterFlag::empty(),
0,
0,
),
KEvent::new(
0,
EventFilter::EVFILT_READ,
EventFlag::empty(),
FilterFlag::empty(),
0,
0,
),
];
kevent_ts(self.kq, &changes, &mut out, None)?;
// Report an error if *both* fd were missing, meaning we were not registered at all
let mut notfound = 0;
for o in &out {
if o.flags().contains(EventFlag::EV_ERROR) && o.data() != 0 {
let e = io::Error::from_raw_os_error(o.data() as i32);
// ignore NotFound error which is raised if we tried to remove a non-existent filter
if e.kind() != io::ErrorKind::NotFound {
return Err(e.into());
} else {
notfound += 1;
}
}
}
if notfound == 2 {
return Err(std::io::Error::from(io::ErrorKind::NotFound).into());
}
Ok(())
}
}
impl Drop for Kqueue {
fn drop(&mut self) {
let _ = nix::unistd::close(self.kq);
}
}