1 use crate::Result;
2 use crate::errno::Errno;
3 use libc::{self, c_int};
4 use std::os::unix::io::RawFd;
5 use std::ptr;
6 use std::mem;
7 
8 libc_bitflags!(
9     pub struct EpollFlags: c_int {
10         EPOLLIN;
11         EPOLLPRI;
12         EPOLLOUT;
13         EPOLLRDNORM;
14         EPOLLRDBAND;
15         EPOLLWRNORM;
16         EPOLLWRBAND;
17         EPOLLMSG;
18         EPOLLERR;
19         EPOLLHUP;
20         EPOLLRDHUP;
21         #[cfg(target_os = "linux")]  // Added in 4.5; not in Android.
22         EPOLLEXCLUSIVE;
23         #[cfg(not(target_arch = "mips"))]
24         EPOLLWAKEUP;
25         EPOLLONESHOT;
26         EPOLLET;
27     }
28 );
29 
30 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
31 #[repr(i32)]
32 #[non_exhaustive]
33 pub enum EpollOp {
34     EpollCtlAdd = libc::EPOLL_CTL_ADD,
35     EpollCtlDel = libc::EPOLL_CTL_DEL,
36     EpollCtlMod = libc::EPOLL_CTL_MOD,
37 }
38 
39 libc_bitflags!{
40     pub struct EpollCreateFlags: c_int {
41         EPOLL_CLOEXEC;
42     }
43 }
44 
45 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
46 #[repr(transparent)]
47 pub struct EpollEvent {
48     event: libc::epoll_event,
49 }
50 
51 impl EpollEvent {
new(events: EpollFlags, data: u64) -> Self52     pub fn new(events: EpollFlags, data: u64) -> Self {
53         EpollEvent { event: libc::epoll_event { events: events.bits() as u32, u64: data } }
54     }
55 
empty() -> Self56     pub fn empty() -> Self {
57         unsafe { mem::zeroed::<EpollEvent>() }
58     }
59 
events(&self) -> EpollFlags60     pub fn events(&self) -> EpollFlags {
61         EpollFlags::from_bits(self.event.events as c_int).unwrap()
62     }
63 
data(&self) -> u6464     pub fn data(&self) -> u64 {
65         self.event.u64
66     }
67 }
68 
69 #[inline]
epoll_create() -> Result<RawFd>70 pub fn epoll_create() -> Result<RawFd> {
71     let res = unsafe { libc::epoll_create(1024) };
72 
73     Errno::result(res)
74 }
75 
76 #[inline]
epoll_create1(flags: EpollCreateFlags) -> Result<RawFd>77 pub fn epoll_create1(flags: EpollCreateFlags) -> Result<RawFd> {
78     let res = unsafe { libc::epoll_create1(flags.bits()) };
79 
80     Errno::result(res)
81 }
82 
83 #[inline]
epoll_ctl<'a, T>(epfd: RawFd, op: EpollOp, fd: RawFd, event: T) -> Result<()> where T: Into<Option<&'a mut EpollEvent>>84 pub fn epoll_ctl<'a, T>(epfd: RawFd, op: EpollOp, fd: RawFd, event: T) -> Result<()>
85     where T: Into<Option<&'a mut EpollEvent>>
86 {
87     let mut event: Option<&mut EpollEvent> = event.into();
88     if event.is_none() && op != EpollOp::EpollCtlDel {
89         Err(Errno::EINVAL)
90     } else {
91         let res = unsafe {
92             if let Some(ref mut event) = event {
93                 libc::epoll_ctl(epfd, op as c_int, fd, &mut event.event)
94             } else {
95                 libc::epoll_ctl(epfd, op as c_int, fd, ptr::null_mut())
96             }
97         };
98         Errno::result(res).map(drop)
99     }
100 }
101 
102 #[inline]
epoll_wait(epfd: RawFd, events: &mut [EpollEvent], timeout_ms: isize) -> Result<usize>103 pub fn epoll_wait(epfd: RawFd, events: &mut [EpollEvent], timeout_ms: isize) -> Result<usize> {
104     let res = unsafe {
105         libc::epoll_wait(epfd, events.as_mut_ptr() as *mut libc::epoll_event, events.len() as c_int, timeout_ms as c_int)
106     };
107 
108     Errno::result(res).map(|r| r as usize)
109 }
110