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
|
/* TOOD: Implement for other kqueue based systems
*/
use {Errno, Result};
#[cfg(not(target_os = "netbsd"))]
use libc::{timespec, time_t, c_int, c_long, intptr_t, uintptr_t};
#[cfg(target_os = "netbsd")]
use libc::{timespec, time_t, c_long, intptr_t, uintptr_t, size_t};
use libc;
use std::os::unix::io::RawFd;
use std::ptr;
use std::mem;
// Redefine kevent in terms of programmer-friendly enums and bitfields.
#[derive(Clone, Copy)]
#[repr(C)]
pub struct KEvent {
kevent: libc::kevent,
}
#[cfg(any(target_os = "openbsd", target_os = "freebsd",
target_os = "dragonfly", target_os = "macos"))]
type type_of_udata = *mut ::c_void;
#[cfg(any(target_os = "netbsd"))]
type type_of_udata = intptr_t;
#[cfg(not(target_os = "netbsd"))]
type type_of_event_filter = i16;
#[cfg(not(target_os = "netbsd"))]
#[repr(i16)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum EventFilter {
EVFILT_AIO = libc::EVFILT_AIO,
#[cfg(target_os = "dragonfly")]
EVFILT_EXCEPT = libc::EVFILT_EXCEPT,
#[cfg(any(target_os = "macos",
target_os = "dragonfly",
target_os = "freebsd"))]
EVFILT_FS = libc::EVFILT_FS,
#[cfg(target_os = "freebsd")]
EVFILT_LIO = libc::EVFILT_LIO,
#[cfg(target_os = "macos")]
EVFILT_MACHPORT = libc::EVFILT_MACHPORT,
EVFILT_PROC = libc::EVFILT_PROC,
EVFILT_READ = libc::EVFILT_READ,
EVFILT_SIGNAL = libc::EVFILT_SIGNAL,
EVFILT_TIMER = libc::EVFILT_TIMER,
#[cfg(any(target_os = "macos",
target_os = "dragonfly",
target_os = "freebsd"))]
EVFILT_USER = libc::EVFILT_USER,
#[cfg(target_os = "macos")]
EVFILT_VM = libc::EVFILT_VM,
EVFILT_VNODE = libc::EVFILT_VNODE,
EVFILT_WRITE = libc::EVFILT_WRITE,
}
#[cfg(target_os = "netbsd")]
type type_of_event_filter = i32;
#[cfg(target_os = "netbsd")]
#[repr(u32)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum EventFilter {
EVFILT_READ = libc::EVFILT_READ,
EVFILT_WRITE = libc::EVFILT_WRITE,
EVFILT_AIO = libc::EVFILT_AIO,
EVFILT_VNODE = libc::EVFILT_VNODE,
EVFILT_PROC = libc::EVFILT_PROC,
EVFILT_SIGNAL = libc::EVFILT_SIGNAL,
EVFILT_TIMER = libc::EVFILT_TIMER,
}
#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "dragonfly"))]
pub type type_of_event_flag = u16;
#[cfg(any(target_os = "netbsd", target_os = "openbsd"))]
pub type type_of_event_flag = u32;
libc_bitflags!{
flags EventFlag: type_of_event_flag {
EV_ADD,
EV_CLEAR,
EV_DELETE,
EV_DISABLE,
EV_DISPATCH,
#[cfg(target_os = "freebsd")]
EV_DROP,
EV_ENABLE,
EV_EOF,
EV_ERROR,
#[cfg(target_os = "macos")]
EV_FLAG0,
EV_FLAG1,
#[cfg(target_os = "dragonfly")]
EV_NODATA,
EV_ONESHOT,
#[cfg(target_os = "macos")]
EV_OOBAND,
#[cfg(target_os = "macos")]
EV_POLL,
#[cfg(not(target_os = "openbsd"))]
EV_RECEIPT,
EV_SYSFLAGS,
}
}
bitflags!(
flags FilterFlag: u32 {
#[cfg(target_os = "macos")]
const NOTE_ABSOLUTE = libc::NOTE_ABSOLUTE,
const NOTE_ATTRIB = libc::NOTE_ATTRIB,
const NOTE_CHILD = libc::NOTE_CHILD,
const NOTE_DELETE = libc::NOTE_DELETE,
#[cfg(target_os = "openbsd")]
const NOTE_EOF = libc::NOTE_EOF,
const NOTE_EXEC = libc::NOTE_EXEC,
const NOTE_EXIT = libc::NOTE_EXIT,
#[cfg(target_os = "macos")]
const NOTE_EXIT_REPARENTED = libc::NOTE_EXIT_REPARENTED,
#[cfg(target_os = "macos")]
const NOTE_EXITSTATUS = libc::NOTE_EXITSTATUS,
const NOTE_EXTEND = libc::NOTE_EXTEND,
#[cfg(any(target_os = "macos",
target_os = "freebsd",
target_os = "dragonfly"))]
const NOTE_FFAND = libc::NOTE_FFAND,
#[cfg(any(target_os = "macos",
target_os = "freebsd",
target_os = "dragonfly"))]
const NOTE_FFCOPY = libc::NOTE_FFCOPY,
#[cfg(any(target_os = "macos",
target_os = "freebsd",
target_os = "dragonfly"))]
const NOTE_FFCTRLMASK = libc::NOTE_FFCTRLMASK,
#[cfg(any(target_os = "macos",
target_os = "freebsd",
target_os = "dragonfly"))]
const NOTE_FFLAGSMASK = libc::NOTE_FFLAGSMASK,
#[cfg(any(target_os = "macos",
target_os = "freebsd",
target_os = "dragonfly"))]
const NOTE_FFNOP = libc::NOTE_FFNOP,
#[cfg(any(target_os = "macos",
target_os = "freebsd",
target_os = "dragonfly"))]
const NOTE_FFOR = libc::NOTE_FFOR,
const NOTE_FORK = libc::NOTE_FORK,
const NOTE_LINK = libc::NOTE_LINK,
const NOTE_LOWAT = libc::NOTE_LOWAT,
#[cfg(target_os = "freebsd")]
const NOTE_MSECONDS = libc::NOTE_MSECONDS,
#[cfg(target_os = "macos")]
const NOTE_NONE = libc::NOTE_NONE,
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
const NOTE_NSECONDS = libc::NOTE_NSECONDS,
#[cfg(target_os = "dragonfly")]
const NOTE_OOB = libc::NOTE_OOB,
const NOTE_PCTRLMASK = libc::NOTE_PCTRLMASK,
const NOTE_PDATAMASK = libc::NOTE_PDATAMASK,
#[cfg(target_os = "macos")]
const NOTE_REAP = libc::NOTE_REAP,
const NOTE_RENAME = libc::NOTE_RENAME,
const NOTE_REVOKE = libc::NOTE_REVOKE,
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
const NOTE_SECONDS = libc::NOTE_SECONDS,
#[cfg(target_os = "macos")]
const NOTE_SIGNAL = libc::NOTE_SIGNAL,
const NOTE_TRACK = libc::NOTE_TRACK,
const NOTE_TRACKERR = libc::NOTE_TRACKERR,
#[cfg(any(target_os = "macos",
target_os = "freebsd",
target_os = "dragonfly"))]
const NOTE_TRIGGER = libc::NOTE_TRIGGER,
#[cfg(target_os = "openbsd")]
const NOTE_TRUNCATE = libc::NOTE_TRUNCATE,
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
const NOTE_USECONDS = libc::NOTE_USECONDS,
#[cfg(target_os = "macos")]
const NOTE_VM_ERROR = libc::NOTE_VM_ERROR,
#[cfg(target_os = "macos")]
const NOTE_VM_PRESSURE = libc::NOTE_VM_PRESSURE,
#[cfg(target_os = "macos")]
const NOTE_VM_PRESSURE_SUDDEN_TERMINATE = libc::NOTE_VM_PRESSURE_SUDDEN_TERMINATE,
#[cfg(target_os = "macos")]
const NOTE_VM_PRESSURE_TERMINATE = libc::NOTE_VM_PRESSURE_TERMINATE,
const NOTE_WRITE = libc::NOTE_WRITE,
}
);
pub fn kqueue() -> Result<RawFd> {
let res = unsafe { libc::kqueue() };
Errno::result(res)
}
impl KEvent {
pub fn new(ident: uintptr_t, filter: EventFilter, flags: EventFlag,
fflags:FilterFlag, data: intptr_t, udata: uintptr_t) -> KEvent {
KEvent { kevent: libc::kevent {
ident: ident,
filter: filter as type_of_event_filter,
flags: flags.bits(),
fflags: fflags.bits(),
data: data,
udata: udata as type_of_udata
} }
}
pub fn ident(&self) -> uintptr_t {
self.kevent.ident
}
pub fn filter(&self) -> EventFilter {
unsafe { mem::transmute(self.kevent.filter as type_of_event_filter) }
}
pub fn flags(&self) -> EventFlag {
EventFlag::from_bits(self.kevent.flags).unwrap()
}
pub fn fflags(&self) -> FilterFlag {
FilterFlag::from_bits(self.kevent.fflags).unwrap()
}
pub fn data(&self) -> intptr_t {
self.kevent.data
}
pub fn udata(&self) -> uintptr_t {
self.kevent.udata as uintptr_t
}
}
pub fn kevent(kq: RawFd,
changelist: &[KEvent],
eventlist: &mut [KEvent],
timeout_ms: usize) -> Result<usize> {
// Convert ms to timespec
let timeout = timespec {
tv_sec: (timeout_ms / 1000) as time_t,
tv_nsec: ((timeout_ms % 1000) * 1_000_000) as c_long
};
kevent_ts(kq, changelist, eventlist, Some(timeout))
}
#[cfg(any(target_os = "macos",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd"))]
type type_of_nchanges = c_int;
#[cfg(target_os = "netbsd")]
type type_of_nchanges = size_t;
pub fn kevent_ts(kq: RawFd,
changelist: &[KEvent],
eventlist: &mut [KEvent],
timeout_opt: Option<timespec>) -> Result<usize> {
let res = unsafe {
libc::kevent(
kq,
changelist.as_ptr() as *const libc::kevent,
changelist.len() as type_of_nchanges,
eventlist.as_mut_ptr() as *mut libc::kevent,
eventlist.len() as type_of_nchanges,
if let Some(ref timeout) = timeout_opt {timeout as *const timespec} else {ptr::null()})
};
Errno::result(res).map(|r| r as usize)
}
#[inline]
pub fn ev_set(ev: &mut KEvent,
ident: usize,
filter: EventFilter,
flags: EventFlag,
fflags: FilterFlag,
udata: uintptr_t) {
ev.kevent.ident = ident as uintptr_t;
ev.kevent.filter = filter as type_of_event_filter;
ev.kevent.flags = flags.bits();
ev.kevent.fflags = fflags.bits();
ev.kevent.data = 0;
ev.kevent.udata = udata as type_of_udata;
}
#[test]
fn test_struct_kevent() {
let udata : uintptr_t = 12345;
let expected = libc::kevent{ident: 0xdeadbeef,
filter: libc::EVFILT_READ,
flags: libc::EV_DISPATCH | libc::EV_ADD,
fflags: libc::NOTE_CHILD | libc::NOTE_EXIT,
data: 0x1337,
udata: udata as type_of_udata};
let actual = KEvent::new(0xdeadbeef,
EventFilter::EVFILT_READ,
EV_DISPATCH | EV_ADD,
NOTE_CHILD | NOTE_EXIT,
0x1337,
udata);
assert!(expected.ident == actual.ident());
assert!(expected.filter == actual.filter() as type_of_event_filter);
assert!(expected.flags == actual.flags().bits());
assert!(expected.fflags == actual.fflags().bits());
assert!(expected.data == actual.data());
assert!(expected.udata == actual.udata() as type_of_udata);
assert!(mem::size_of::<libc::kevent>() == mem::size_of::<KEvent>());
}
|