use core::mem::size_of;
use crate::errno::SysResult;
use crate::fd::RawFd;
use crate::raw::{nr, syscall1, syscall2, syscall4, syscall5};
use crate::time::Timespec;
pub const POLLIN: i16 = 0x001;
pub const POLLPRI: i16 = 0x002;
pub const POLLOUT: i16 = 0x004;
pub const POLLERR: i16 = 0x008;
pub const POLLHUP: i16 = 0x010;
pub const POLLNVAL: i16 = 0x020;
pub const POLLRDHUP: i16 = 0x2000;
pub const EPOLLIN: u32 = 0x001;
pub const EPOLLOUT: u32 = 0x004;
pub const EPOLLERR: u32 = 0x008;
pub const EPOLLHUP: u32 = 0x010;
pub const EPOLLRDHUP: u32 = 0x2000;
pub const EPOLLONESHOT: u32 = 1 << 30;
pub const EPOLLET: u32 = 1 << 31;
pub const EPOLL_CLOEXEC: i32 = 0x80000;
pub const EPOLL_CTL_ADD: i32 = 1;
pub const EPOLL_CTL_DEL: i32 = 2;
pub const EPOLL_CTL_MOD: i32 = 3;
pub const EFD_SEMAPHORE: i32 = 1;
pub const EFD_NONBLOCK: i32 = 0x800;
pub const EFD_CLOEXEC: i32 = 0x80000;
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct PollFd {
pub fd: i32,
pub events: i16,
pub revents: i16,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct EpollEvent {
pub events: u32,
pub _pad: u32,
pub data: u64,
}
const _: () = {
assert!(size_of::<PollFd>() == 8);
assert!(size_of::<EpollEvent>() == 16);
};
pub fn poll(fds: &mut [PollFd], timeout_ms: i32) -> SysResult<usize> {
let mut ts = Timespec::default();
let ts_ptr = if timeout_ms < 0 {
core::ptr::null::<Timespec>()
} else {
let ms = i64::from(timeout_ms);
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1_000_000;
core::ptr::from_ref(&ts)
};
let fds_ptr = if fds.is_empty() {
core::ptr::null_mut::<PollFd>()
} else {
fds.as_mut_ptr()
};
unsafe {
syscall5(
nr::PPOLL,
fds_ptr as usize,
fds.len(),
ts_ptr as usize,
0,
8,
)
}
}
pub fn epoll_create1(flags: i32) -> SysResult<RawFd> {
unsafe { syscall1(nr::EPOLL_CREATE1, flags as usize).map(|v| v as RawFd) }
}
pub fn epoll_ctl(epfd: RawFd, op: i32, fd: RawFd, event: Option<&EpollEvent>) -> SysResult<()> {
let ev_ptr = event.map_or(0, |ev| core::ptr::from_ref(ev) as usize);
unsafe {
syscall4(
nr::EPOLL_CTL,
epfd as usize,
op as usize,
fd as usize,
ev_ptr,
)?;
}
Ok(())
}
pub fn epoll_wait(
epfd: RawFd,
events: &mut [EpollEvent],
timeout: Option<&Timespec>,
) -> SysResult<usize> {
let ts_ptr = timeout.map_or(0, |t| core::ptr::from_ref(t) as usize);
unsafe {
syscall6_epoll_pwait2(
epfd as usize,
events.as_mut_ptr() as usize,
events.len(),
ts_ptr,
)
}
}
#[inline]
unsafe fn syscall6_epoll_pwait2(
epfd: usize,
events_ptr: usize,
maxevents: usize,
timeout_ptr: usize,
) -> SysResult<usize> {
unsafe {
crate::raw::syscall6(
nr::EPOLL_PWAIT2,
epfd,
events_ptr,
maxevents,
timeout_ptr,
0,
8,
)
}
}
pub fn eventfd(initval: u32, flags: i32) -> SysResult<RawFd> {
unsafe { syscall2(nr::EVENTFD2, initval as usize, flags as usize).map(|v| v as RawFd) }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::raw::with_mock_syscall;
#[test]
fn poll_converts_timeout_ms_to_ppoll_timespec() {
let mut fds = [PollFd {
fd: 3,
events: POLLIN,
revents: 0,
}];
let ready = with_mock_syscall(
|sysno, args| {
assert_eq!(sysno, nr::PPOLL);
assert_eq!(args[1], 1);
let ts_ptr = args[2] as *const Timespec;
assert!(!ts_ptr.is_null());
let ts = unsafe { *ts_ptr };
assert_eq!(
ts,
Timespec {
tv_sec: 1,
tv_nsec: 250_000_000,
}
);
assert_eq!(args[3], 0);
assert_eq!(args[4], 8);
let pfd = args[0] as *mut PollFd;
unsafe {
(*pfd).revents = POLLIN;
}
Ok(1)
},
|| poll(&mut fds, 1250).unwrap(),
);
assert_eq!(ready, 1);
assert_eq!(fds[0].revents, POLLIN);
with_mock_syscall(
|sysno, args| {
assert_eq!(sysno, nr::PPOLL);
assert_eq!(args[2], 0);
Ok(0)
},
|| {
assert_eq!(poll(&mut fds, -1).unwrap(), 0);
},
);
}
}