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
#![no_std]
#[allow(unused_imports)]
use libc::{c_int, c_short, c_uint, c_ushort, c_void, intptr_t, size_t, timespec, uintptr_t};

#[cfg(not(target_os = "netbsd"))]
use core::ptr;

pub mod constants;

pub use self::constants::*;

#[cfg(not(target_os = "netbsd"))]
pub type EventListSize = c_int;

#[cfg(target_os = "netbsd")]
pub type EventListSize = size_t;

#[cfg(all(not(target_os = "netbsd"), not(target_os = "freebsd")))]
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct kevent {
    pub ident: uintptr_t,
    pub filter: EventFilter,
    pub flags: EventFlag,
    pub fflags: FilterFlag,
    pub data: i64,
    pub udata: *mut c_void,
}

#[cfg(target_os = "netbsd")]
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct kevent {
    pub ident: uintptr_t,
    pub filter: EventFilter,
    pub flags: EventFlag,
    pub fflags: FilterFlag,
    pub data: i64,
    pub udata: intptr_t,
}

#[cfg(target_os = "freebsd")]
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct kevent {
    pub ident: uintptr_t,
    pub filter: EventFilter,
    pub flags: EventFlag,
    pub fflags: FilterFlag,
    pub data: i64,
    pub udata: *mut c_void,
    pub ext: [i64; 4],
}

impl kevent {
    #[cfg(all(not(target_os = "netbsd"), not(target_os = "freebsd")))]
    pub fn new(
        ident: uintptr_t,
        filter: EventFilter,
        flags: EventFlag,
        fflags: FilterFlag,
    ) -> kevent {
        kevent {
            ident,
            filter,
            flags,
            fflags,
            data: 0,
            udata: ptr::null_mut(),
        }
    }

    #[cfg(target_os = "netbsd")]
    pub fn new(
        ident: uintptr_t,
        filter: EventFilter,
        flags: EventFlag,
        fflags: FilterFlag,
    ) -> kevent {
        kevent {
            ident,
            filter,
            flags,
            fflags,
            data: 0,
            udata: 0,
        }
    }

    #[cfg(target_os = "freebsd")]
    pub fn new(
        ident: uintptr_t,
        filter: EventFilter,
        flags: EventFlag,
        fflags: FilterFlag,
    ) -> kevent {
        kevent {
            ident,
            filter,
            flags,
            fflags,
            data: 0,
            udata: ptr::null_mut(),
            ext: [0; 4],
        }
    }
}

#[allow(improper_ctypes)]
extern "C" {
    pub fn kqueue() -> c_int;

    pub fn kevent(
        kq: c_int,
        changelist: *const kevent,
        nchanges: EventListSize,
        eventlist: *mut kevent,
        nevents: EventListSize,
        timeout: *const timespec,
    ) -> c_int;

    #[cfg(target_os = "netbsd")]
    pub fn kqueue1(flags: c_int) -> c_int;
}

#[cfg(test)]
mod test {
    use super::kqueue;

    #[test]
    fn test_kqueue() {
        unsafe {
            assert!(kqueue() > 0);
        }
    }
}