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