aboutsummaryrefslogtreecommitdiff
path: root/src/sys/event.rs
blob: ec7f7e277a3dfc05305cb438a5c0ea20825071f4 (plain)
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
//! Kernel event notification mechanism
//!
//! # See Also
//! [kqueue(2)](https://www.freebsd.org/cgi/man.cgi?query=kqueue)

use crate::{Errno, Result};
#[cfg(not(target_os = "netbsd"))]
use libc::{c_int, c_long, intptr_t, time_t, timespec, uintptr_t};
#[cfg(target_os = "netbsd")]
use libc::{c_long, intptr_t, size_t, time_t, timespec, uintptr_t};
use std::convert::TryInto;
use std::mem;
use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd};
use std::ptr;

/// A kernel event queue.  Used to notify a process of various asynchronous
/// events.
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct KEvent {
    kevent: libc::kevent,
}

/// A kernel event queue.
///
/// Used by the kernel to notify the process of various types of asynchronous
/// events.
#[repr(transparent)]
#[derive(Debug)]
pub struct Kqueue(OwnedFd);

impl Kqueue {
    /// Create a new kernel event queue.
    pub fn new() -> Result<Self> {
        let res = unsafe { libc::kqueue() };

        Errno::result(res).map(|fd| unsafe { Self(OwnedFd::from_raw_fd(fd)) })
    }

    /// Register new events with the kqueue, and return any pending events to
    /// the user.
    ///
    /// This method will block until either the timeout expires, or a registered
    /// event triggers a notification.
    ///
    /// # Arguments
    /// - `changelist` - Any new kevents to register for notifications.
    /// - `eventlist` - Storage space for the kernel to return notifications.
    /// - `timeout` - An optional timeout.
    ///
    /// # Returns
    /// Returns the number of events placed in the `eventlist`.  If an error
    /// occurs while processing an element of the `changelist` and there is
    /// enough room in the `eventlist`, then the event will be placed in the
    /// `eventlist` with `EV_ERROR` set in `flags` and the system error in
    /// `data`.
    pub fn kevent(
        &self,
        changelist: &[KEvent],
        eventlist: &mut [KEvent],
        timeout_opt: Option<timespec>,
    ) -> Result<usize> {
        let res = unsafe {
            libc::kevent(
                self.0.as_raw_fd(),
                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)
    }
}

#[cfg(any(
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "ios",
    target_os = "macos",
    target_os = "openbsd"
))]
type type_of_udata = *mut libc::c_void;
#[cfg(target_os = "netbsd")]
type type_of_udata = intptr_t;

#[cfg(target_os = "netbsd")]
type type_of_event_filter = u32;
#[cfg(not(target_os = "netbsd"))]
type type_of_event_filter = i16;
libc_enum! {
    #[cfg_attr(target_os = "netbsd", repr(u32))]
    #[cfg_attr(not(target_os = "netbsd"), repr(i16))]
    #[non_exhaustive]
    /// Kqueue filter types.  These are all the different types of event that a
    /// kqueue can notify for.
    pub enum EventFilter {
        /// Notifies on the completion of a POSIX AIO operation.
        EVFILT_AIO,
        #[cfg(target_os = "freebsd")]
        /// Returns whenever there is no remaining data in the write buffer
        EVFILT_EMPTY,
        #[cfg(target_os = "dragonfly")]
        /// Takes a descriptor as the identifier, and returns whenever one of
        /// the specified exceptional conditions has occurred on the descriptor.
        EVFILT_EXCEPT,
        #[cfg(any(target_os = "dragonfly",
                  target_os = "freebsd",
                  target_os = "ios",
                  target_os = "macos"))]
        /// Establishes a file system monitor.
        EVFILT_FS,
        #[cfg(target_os = "freebsd")]
        /// Notify for completion of a list of POSIX AIO operations.
        /// # See Also
        /// [lio_listio(2)](https://www.freebsd.org/cgi/man.cgi?query=lio_listio)
        EVFILT_LIO,
        #[cfg(any(target_os = "ios", target_os = "macos"))]
        /// Mach portsets
        EVFILT_MACHPORT,
        /// Notifies when a process performs one or more of the requested
        /// events.
        EVFILT_PROC,
        /// Returns events associated with the process referenced by a given
        /// process descriptor, created by `pdfork()`. The events to monitor are:
        ///
        /// - NOTE_EXIT: the process has exited. The exit status will be stored in data.
        #[cfg(target_os = "freebsd")]
        EVFILT_PROCDESC,
        /// Takes a file descriptor as the identifier, and notifies whenever
        /// there is data available to read.
        EVFILT_READ,
        #[cfg(target_os = "freebsd")]
        #[doc(hidden)]
        #[deprecated(since = "0.27.0", note = "Never fully implemented by the OS")]
        EVFILT_SENDFILE,
        /// Takes a signal number to monitor as the identifier and notifies when
        /// the given signal is delivered to the process.
        EVFILT_SIGNAL,
        /// Establishes a timer and notifies when the timer expires.
        EVFILT_TIMER,
        #[cfg(any(target_os = "dragonfly",
                  target_os = "freebsd",
                  target_os = "ios",
                  target_os = "macos"))]
        /// Notifies only when explicitly requested by the user.
        EVFILT_USER,
        #[cfg(any(target_os = "ios", target_os = "macos"))]
        /// Virtual memory events
        EVFILT_VM,
        /// Notifies when a requested event happens on a specified file.
        EVFILT_VNODE,
        /// Takes a file descriptor as the identifier, and notifies whenever
        /// it is possible to write to the file without blocking.
        EVFILT_WRITE,
    }
    impl TryFrom<type_of_event_filter>
}

#[cfg(any(
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "ios",
    target_os = "macos",
    target_os = "openbsd"
))]
#[doc(hidden)]
pub type type_of_event_flag = u16;
#[cfg(target_os = "netbsd")]
#[doc(hidden)]
pub type type_of_event_flag = u32;
libc_bitflags! {
    /// Event flags.  See the man page for details.
    // There's no useful documentation we can write for the individual flags
    // that wouldn't simply be repeating the man page.
    pub struct EventFlag: type_of_event_flag {
        #[allow(missing_docs)]
        EV_ADD;
        #[allow(missing_docs)]
        EV_CLEAR;
        #[allow(missing_docs)]
        EV_DELETE;
        #[allow(missing_docs)]
        EV_DISABLE;
        #[cfg(any(target_os = "dragonfly", target_os = "freebsd",
                  target_os = "ios", target_os = "macos",
                  target_os = "netbsd", target_os = "openbsd"))]
        #[allow(missing_docs)]
        EV_DISPATCH;
        #[cfg(target_os = "freebsd")]
        #[allow(missing_docs)]
        EV_DROP;
        #[allow(missing_docs)]
        EV_ENABLE;
        #[allow(missing_docs)]
        EV_EOF;
        #[allow(missing_docs)]
        EV_ERROR;
        #[cfg(any(target_os = "macos", target_os = "ios"))]
        #[allow(missing_docs)]
        EV_FLAG0;
        #[allow(missing_docs)]
        EV_FLAG1;
        #[cfg(target_os = "dragonfly")]
        #[allow(missing_docs)]
        EV_NODATA;
        #[allow(missing_docs)]
        EV_ONESHOT;
        #[cfg(any(target_os = "macos", target_os = "ios"))]
        #[allow(missing_docs)]
        EV_OOBAND;
        #[cfg(any(target_os = "macos", target_os = "ios"))]
        #[allow(missing_docs)]
        EV_POLL;
        #[cfg(any(target_os = "dragonfly", target_os = "freebsd",
                  target_os = "ios", target_os = "macos",
                  target_os = "netbsd", target_os = "openbsd"))]
        #[allow(missing_docs)]
        EV_RECEIPT;
    }
}

libc_bitflags!(
    /// Filter-specific flags.  See the man page for details.
    // There's no useful documentation we can write for the individual flags
    // that wouldn't simply be repeating the man page.
    #[allow(missing_docs)]
    pub struct FilterFlag: u32 {
        #[cfg(any(target_os = "macos", target_os = "ios"))]
        #[allow(missing_docs)]
        NOTE_ABSOLUTE;
        #[allow(missing_docs)]
        NOTE_ATTRIB;
        #[allow(missing_docs)]
        NOTE_CHILD;
        #[allow(missing_docs)]
        NOTE_DELETE;
        #[cfg(target_os = "openbsd")]
        #[allow(missing_docs)]
        NOTE_EOF;
        #[allow(missing_docs)]
        NOTE_EXEC;
        #[allow(missing_docs)]
        NOTE_EXIT;
        #[cfg(any(target_os = "macos", target_os = "ios"))]
        #[allow(missing_docs)]
        NOTE_EXITSTATUS;
        #[allow(missing_docs)]
        NOTE_EXTEND;
        #[cfg(any(target_os = "macos",
                  target_os = "ios",
                  target_os = "freebsd",
                  target_os = "dragonfly"))]
        #[allow(missing_docs)]
        NOTE_FFAND;
        #[cfg(any(target_os = "macos",
                  target_os = "ios",
                  target_os = "freebsd",
                  target_os = "dragonfly"))]
        #[allow(missing_docs)]
        NOTE_FFCOPY;
        #[cfg(any(target_os = "macos",
                  target_os = "ios",
                  target_os = "freebsd",
                  target_os = "dragonfly"))]
        #[allow(missing_docs)]
        NOTE_FFCTRLMASK;
        #[cfg(any(target_os = "macos",
                  target_os = "ios",
                  target_os = "freebsd",
                  target_os = "dragonfly"))]
        #[allow(missing_docs)]
        NOTE_FFLAGSMASK;
        #[cfg(any(target_os = "macos",
                  target_os = "ios",
                  target_os = "freebsd",
                  target_os = "dragonfly"))]
        #[allow(missing_docs)]
        NOTE_FFNOP;
        #[cfg(any(target_os = "macos",
                  target_os = "ios",
                  target_os = "freebsd",
                  target_os = "dragonfly"))]
        #[allow(missing_docs)]
        NOTE_FFOR;
        #[allow(missing_docs)]
        NOTE_FORK;
        #[allow(missing_docs)]
        NOTE_LINK;
        #[allow(missing_docs)]
        NOTE_LOWAT;
        #[cfg(target_os = "freebsd")]
        #[allow(missing_docs)]
        NOTE_MSECONDS;
        #[cfg(any(target_os = "macos", target_os = "ios"))]
        #[allow(missing_docs)]
        NOTE_NONE;
        #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))]
        #[allow(missing_docs)]
        NOTE_NSECONDS;
        #[cfg(target_os = "dragonfly")]
        #[allow(missing_docs)]
        NOTE_OOB;
        #[allow(missing_docs)]
        NOTE_PCTRLMASK;
        #[allow(missing_docs)]
        NOTE_PDATAMASK;
        #[allow(missing_docs)]
        NOTE_RENAME;
        #[allow(missing_docs)]
        NOTE_REVOKE;
        #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))]
        #[allow(missing_docs)]
        NOTE_SECONDS;
        #[cfg(any(target_os = "macos", target_os = "ios"))]
        #[allow(missing_docs)]
        NOTE_SIGNAL;
        #[allow(missing_docs)]
        NOTE_TRACK;
        #[allow(missing_docs)]
        NOTE_TRACKERR;
        #[cfg(any(target_os = "macos",
                  target_os = "ios",
                  target_os = "freebsd",
                  target_os = "dragonfly"))]
        #[allow(missing_docs)]
        NOTE_TRIGGER;
        #[cfg(target_os = "openbsd")]
        #[allow(missing_docs)]
        NOTE_TRUNCATE;
        #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))]
        #[allow(missing_docs)]
        NOTE_USECONDS;
        #[cfg(any(target_os = "macos", target_os = "ios"))]
        #[allow(missing_docs)]
        NOTE_VM_ERROR;
        #[cfg(any(target_os = "macos", target_os = "ios"))]
        #[allow(missing_docs)]
        NOTE_VM_PRESSURE;
        #[cfg(any(target_os = "macos", target_os = "ios"))]
        #[allow(missing_docs)]
        NOTE_VM_PRESSURE_SUDDEN_TERMINATE;
        #[cfg(any(target_os = "macos", target_os = "ios"))]
        #[allow(missing_docs)]
        NOTE_VM_PRESSURE_TERMINATE;
        #[allow(missing_docs)]
        NOTE_WRITE;
    }
);

#[allow(missing_docs)]
#[deprecated(since = "0.27.0", note = "Use KEvent::new instead")]
pub fn kqueue() -> Result<Kqueue> {
    Kqueue::new()
}

// KEvent can't derive Send because on some operating systems, udata is defined
// as a void*.  However, KEvent's public API always treats udata as an intptr_t,
// which is safe to Send.
unsafe impl Send for KEvent {}

impl KEvent {
    #[allow(clippy::needless_update)] // Not needless on all platforms.
    /// Construct a new `KEvent` suitable for submission to the kernel via the
    /// `changelist` argument of [`Kqueue::kevent`].
    pub fn new(
        ident: uintptr_t,
        filter: EventFilter,
        flags: EventFlag,
        fflags: FilterFlag,
        data: intptr_t,
        udata: intptr_t,
    ) -> KEvent {
        KEvent {
            kevent: libc::kevent {
                ident,
                filter: filter as type_of_event_filter,
                flags: flags.bits(),
                fflags: fflags.bits(),
                // data can be either i64 or intptr_t, depending on platform
                data: data as _,
                udata: udata as type_of_udata,
                ..unsafe { mem::zeroed() }
            },
        }
    }

    /// Value used to identify this event.  The exact interpretation is
    /// determined by the attached filter, but often is a raw file descriptor.
    pub fn ident(&self) -> uintptr_t {
        self.kevent.ident
    }

    /// Identifies the kernel filter used to process this event.
    ///
    /// Will only return an error if the kernel reports an event via a filter
    /// that is unknown to Nix.
    pub fn filter(&self) -> Result<EventFilter> {
        self.kevent.filter.try_into()
    }

    /// Flags control what the kernel will do when this event is added with
    /// [`Kqueue::kevent`].
    pub fn flags(&self) -> EventFlag {
        EventFlag::from_bits(self.kevent.flags).unwrap()
    }

    /// Filter-specific flags.
    pub fn fflags(&self) -> FilterFlag {
        FilterFlag::from_bits(self.kevent.fflags).unwrap()
    }

    /// Filter-specific data value.
    pub fn data(&self) -> intptr_t {
        self.kevent.data as intptr_t
    }

    /// Opaque user-defined value passed through the kernel unchanged.
    pub fn udata(&self) -> intptr_t {
        self.kevent.udata as intptr_t
    }
}

#[allow(missing_docs)]
#[deprecated(since = "0.27.0", note = "Use Kqueue::kevent instead")]
pub fn kevent(
    kq: &Kqueue,
    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,
    };

    kq.kevent(changelist, eventlist, Some(timeout))
}

#[cfg(any(
    target_os = "macos",
    target_os = "ios",
    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;

#[allow(missing_docs)]
#[deprecated(since = "0.27.0", note = "Use Kqueue::kevent instead")]
pub fn kevent_ts(
    kq: &Kqueue,
    changelist: &[KEvent],
    eventlist: &mut [KEvent],
    timeout_opt: Option<timespec>,
) -> Result<usize> {
    kq.kevent(changelist, eventlist, timeout_opt)
}

/// Modify an existing [`KEvent`].
// Probably should deprecate.  Would anybody ever use it over `KEvent::new`?
#[deprecated(since = "0.27.0", note = "Use Kqueue::kevent instead")]
#[inline]
pub fn ev_set(
    ev: &mut KEvent,
    ident: usize,
    filter: EventFilter,
    flags: EventFlag,
    fflags: FilterFlag,
    udata: intptr_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() {
    use std::mem;

    let udata: intptr_t = 12345;

    let actual = KEvent::new(
        0xdead_beef,
        EventFilter::EVFILT_READ,
        EventFlag::EV_ONESHOT | EventFlag::EV_ADD,
        FilterFlag::NOTE_CHILD | FilterFlag::NOTE_EXIT,
        0x1337,
        udata,
    );
    assert_eq!(0xdead_beef, actual.ident());
    let filter = actual.kevent.filter;
    assert_eq!(libc::EVFILT_READ, filter);
    assert_eq!(libc::EV_ONESHOT | libc::EV_ADD, actual.flags().bits());
    assert_eq!(libc::NOTE_CHILD | libc::NOTE_EXIT, actual.fflags().bits());
    assert_eq!(0x1337, actual.data());
    assert_eq!(udata as type_of_udata, actual.udata() as type_of_udata);
    assert_eq!(mem::size_of::<libc::kevent>(), mem::size_of::<KEvent>());
}

#[test]
fn test_kevent_filter() {
    let udata: intptr_t = 12345;

    let actual = KEvent::new(
        0xdead_beef,
        EventFilter::EVFILT_READ,
        EventFlag::EV_ONESHOT | EventFlag::EV_ADD,
        FilterFlag::NOTE_CHILD | FilterFlag::NOTE_EXIT,
        0x1337,
        udata,
    );
    assert_eq!(EventFilter::EVFILT_READ, actual.filter().unwrap());
}