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
use std::convert::TryInto;
use std::io;
use std::time;

use bitflags::bitflags;

use super::Int;

enum CtlOp {
    Add = libc::EPOLL_CTL_ADD as isize,
    Mod = libc::EPOLL_CTL_MOD as isize,
    Del = libc::EPOLL_CTL_DEL as isize,
}

bitflags! {
    #[derive(Default)]
    #[repr(transparent)]
    pub struct Events: u32 {
        const IN = libc::EPOLLIN as u32;
        const OUT = libc::EPOLLOUT as u32;
        const ERR = libc::EPOLLERR as u32;
        const ET = libc::EPOLLET as u32;
        const HUP = libc::EPOLLHUP as u32;
        const RDHUP = libc::EPOLLRDHUP as u32;
        const ONESHOT = libc::EPOLLONESHOT as u32;
        const WAKEUP = libc::EPOLLWAKEUP as u32;
        const EXCLUSIVE = libc::EPOLLEXCLUSIVE as u32;
    }
}

#[derive(Debug, Copy, Clone)]
pub struct Event {
    pub events: Events,
    pub data: u64,
}

impl Default for Event {
    #[inline]
    fn default() -> Event {
        Event {
            events: Events::empty(),
            data: 0,
        }
    }
}

bitflags! {
    #[derive(Default)]
    pub struct EpollFlags: Int {
        const CLOEXEC = libc::EPOLL_CLOEXEC;
    }
}

pub struct Epoll {
    fd: Int,
}

impl Epoll {
    pub fn new(flags: EpollFlags) -> io::Result<Epoll> {
        let fd = unsafe { libc::epoll_create1(flags.bits) };

        super::error::convert_neg_ret(fd).map(|fd| Epoll { fd })
    }

    #[inline]
    pub fn fd(&self) -> Int {
        self.fd
    }

    fn ctl(&mut self, op: CtlOp, fd: Int, events: Events, data: u64) -> io::Result<()> {
        let mut ep_event = libc::epoll_event {
            events: events.bits as u32,
            u64: data,
        };

        super::error::convert(
            unsafe { libc::epoll_ctl(self.fd, op as Int, fd, &mut ep_event) },
            (),
        )
    }

    #[inline]
    pub fn del(&mut self, fd: Int) -> io::Result<()> {
        self.ctl(CtlOp::Del, fd, Events::empty(), 0)
    }

    #[inline]
    pub fn add(&mut self, fd: Int, events: Events) -> io::Result<()> {
        self.add3(fd, events, fd as u64)
    }

    #[inline]
    pub fn modify(&mut self, fd: Int, events: Events) -> io::Result<()> {
        self.modify3(fd, events, fd as u64)
    }

    #[inline]
    pub fn add2(&mut self, fd: Int, event: Event) -> io::Result<()> {
        self.add3(fd, event.events, event.data)
    }

    #[inline]
    pub fn modify2(&mut self, fd: Int, event: Event) -> io::Result<()> {
        self.modify3(fd, event.events, event.data)
    }

    #[inline]
    pub fn add3(&mut self, fd: Int, events: Events, data: u64) -> io::Result<()> {
        self.ctl(CtlOp::Add, fd, events, data)
    }

    #[inline]
    pub fn modify3(&mut self, fd: Int, events: Events, data: u64) -> io::Result<()> {
        self.ctl(CtlOp::Mod, fd, events, data)
    }

    pub fn pwait(
        &self,
        events: &mut [Event],
        timeout: Option<time::Duration>,
        sigmask: Option<&super::signal::Sigset>,
    ) -> io::Result<Int> {
        let maxevents = events.len();

        let raw_timeout: Int = match timeout {
            Some(t) => t.as_millis().try_into().unwrap_or(Int::MAX),
            None => -1,
        };

        let raw_sigmask = match sigmask {
            Some(s) => &s.raw_set(),
            None => std::ptr::null(),
        };

        let mut ep_events: Vec<libc::epoll_event> = Vec::new();

        ep_events.resize(maxevents, libc::epoll_event { events: 0, u64: 0 });

        super::error::convert_neg_ret(unsafe {
            libc::epoll_pwait(
                self.fd,
                ep_events.as_mut_ptr(),
                maxevents as Int,
                raw_timeout,
                raw_sigmask,
            )
        })
        .map(|res| {
            for i in 0..(res as usize) {
                events[i] = Event {
                    events: Events::from_bits_truncate(ep_events[i].events),
                    data: ep_events[i].u64,
                };
            }
            res
        })
    }

    #[inline]
    pub fn wait(&self, events: &mut [Event], timeout: Option<time::Duration>) -> io::Result<Int> {
        self.pwait(events, timeout, None)
    }
}

impl Drop for Epoll {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            libc::close(self.fd);
        }
    }
}

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

    use std::io::Write;
    use std::os::unix::io::AsRawFd;

    use super::super::pipe2;

    #[test]
    fn test_epoll() {
        let mut poller = Epoll::new(EpollFlags::CLOEXEC).unwrap();
        let mut events = [Event::default(); 3];

        let (r1, mut w1) = pipe2(libc::O_CLOEXEC).unwrap();
        let (r2, mut w2) = pipe2(libc::O_CLOEXEC).unwrap();

        poller.add(r1.as_raw_fd(), Events::IN).unwrap();
        poller
            .add3(r2.as_raw_fd(), Events::IN, w2.as_raw_fd() as u64)
            .unwrap();

        // Nothing to start
        assert_eq!(
            poller
                .wait(&mut events, Some(time::Duration::from_secs(0)))
                .unwrap(),
            0,
        );

        // Now we write some data and test again
        w1.write(b"a").unwrap();
        assert_eq!(
            poller
                .wait(&mut events, Some(time::Duration::from_secs(0)))
                .unwrap(),
            1,
        );
        assert_eq!(events[0].data, r1.as_raw_fd() as u64);
        assert_eq!(events[0].events, Events::IN);

        // Now make sure reading two files works
        w2.write(b"a").unwrap();
        assert_eq!(
            poller
                .wait(&mut events, Some(time::Duration::from_secs(0)))
                .unwrap(),
            2,
        );
        assert_eq!(events[0].data, r1.as_raw_fd() as u64);
        assert_eq!(events[0].events, Events::IN);
        assert_eq!(events[1].data, w2.as_raw_fd() as u64);
        assert_eq!(events[1].events, Events::IN);
    }
}