1use crate::errno::Errno; 2use crate::Result; 3use libc::{self, c_int}; 4use std::mem; 5use std::os::unix::io::RawFd; 6use std::ptr; 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 { 53 event: libc::epoll_event { 54 events: events.bits() as u32, 55 u64: data, 56 }, 57 } 58 } 59 60 pub fn empty() -> Self { 61 unsafe { mem::zeroed::<EpollEvent>() } 62 } 63 64 pub fn events(&self) -> EpollFlags { 65 EpollFlags::from_bits(self.event.events as c_int).unwrap() 66 } 67 68 pub fn data(&self) -> u64 { 69 self.event.u64 70 } 71} 72 73#[inline] 74pub fn epoll_create() -> Result<RawFd> { 75 let res = unsafe { libc::epoll_create(1024) }; 76 77 Errno::result(res) 78} 79 80#[inline] 81pub fn epoll_create1(flags: EpollCreateFlags) -> Result<RawFd> { 82 let res = unsafe { libc::epoll_create1(flags.bits()) }; 83 84 Errno::result(res) 85} 86 87#[inline] 88pub fn epoll_ctl<'a, T>( 89 epfd: RawFd, 90 op: EpollOp, 91 fd: RawFd, 92 event: T, 93) -> Result<()> 94where 95 T: Into<Option<&'a mut EpollEvent>>, 96{ 97 let mut event: Option<&mut EpollEvent> = event.into(); 98 if event.is_none() && op != EpollOp::EpollCtlDel { 99 Err(Errno::EINVAL) 100 } else { 101 let res = unsafe { 102 if let Some(ref mut event) = event { 103 libc::epoll_ctl(epfd, op as c_int, fd, &mut event.event) 104 } else { 105 libc::epoll_ctl(epfd, op as c_int, fd, ptr::null_mut()) 106 } 107 }; 108 Errno::result(res).map(drop) 109 } 110} 111 112#[inline] 113pub fn epoll_wait( 114 epfd: RawFd, 115 events: &mut [EpollEvent], 116 timeout_ms: isize, 117) -> Result<usize> { 118 let res = unsafe { 119 libc::epoll_wait( 120 epfd, 121 events.as_mut_ptr() as *mut libc::epoll_event, 122 events.len() as c_int, 123 timeout_ms as c_int, 124 ) 125 }; 126 127 Errno::result(res).map(|r| r as usize) 128} 129