1use core::mem::size_of;
4
5use crate::errno::SysResult;
6use crate::fd::RawFd;
7use crate::raw::{nr, syscall1, syscall2, syscall4, syscall5};
8use crate::time::Timespec;
9
10pub const POLLIN: i16 = 0x001;
12pub const POLLPRI: i16 = 0x002;
14pub const POLLOUT: i16 = 0x004;
16pub const POLLERR: i16 = 0x008;
18pub const POLLHUP: i16 = 0x010;
20pub const POLLNVAL: i16 = 0x020;
22pub const POLLRDHUP: i16 = 0x2000;
24
25pub const EPOLLIN: u32 = 0x001;
27pub const EPOLLOUT: u32 = 0x004;
29pub const EPOLLERR: u32 = 0x008;
31pub const EPOLLHUP: u32 = 0x010;
33pub const EPOLLRDHUP: u32 = 0x2000;
35pub const EPOLLONESHOT: u32 = 1 << 30;
37pub const EPOLLET: u32 = 1 << 31;
39pub const EPOLL_CLOEXEC: i32 = 0x80000;
41
42pub const EPOLL_CTL_ADD: i32 = 1;
44pub const EPOLL_CTL_DEL: i32 = 2;
46pub const EPOLL_CTL_MOD: i32 = 3;
48
49pub const EFD_SEMAPHORE: i32 = 1;
51pub const EFD_NONBLOCK: i32 = 0x800;
53pub const EFD_CLOEXEC: i32 = 0x80000;
55
56#[repr(C)]
58#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
59pub struct PollFd {
60 pub fd: i32,
61 pub events: i16,
62 pub revents: i16,
63}
64
65#[repr(C)]
67#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
68pub struct EpollEvent {
69 pub events: u32,
70 pub _pad: u32,
71 pub data: u64,
72}
73
74const _: () = {
75 assert!(size_of::<PollFd>() == 8);
76 assert!(size_of::<EpollEvent>() == 16);
77};
78
79pub fn poll(fds: &mut [PollFd], timeout_ms: i32) -> SysResult<usize> {
82 let mut ts = Timespec::default();
83 let ts_ptr = if timeout_ms < 0 {
84 core::ptr::null::<Timespec>()
85 } else {
86 let ms = i64::from(timeout_ms);
87 ts.tv_sec = ms / 1000;
88 ts.tv_nsec = (ms % 1000) * 1_000_000;
89 core::ptr::from_ref(&ts)
90 };
91 let fds_ptr = if fds.is_empty() {
92 core::ptr::null_mut::<PollFd>()
93 } else {
94 fds.as_mut_ptr()
95 };
96 unsafe {
99 syscall5(
100 nr::PPOLL,
101 fds_ptr as usize,
102 fds.len(),
103 ts_ptr as usize,
104 0,
105 8,
106 )
107 }
108}
109
110pub fn epoll_create1(flags: i32) -> SysResult<RawFd> {
112 unsafe { syscall1(nr::EPOLL_CREATE1, flags as usize).map(|v| v as RawFd) }
114}
115
116pub fn epoll_ctl(epfd: RawFd, op: i32, fd: RawFd, event: Option<&EpollEvent>) -> SysResult<()> {
118 let ev_ptr = event.map_or(0, |ev| core::ptr::from_ref(ev) as usize);
119 unsafe {
121 syscall4(
122 nr::EPOLL_CTL,
123 epfd as usize,
124 op as usize,
125 fd as usize,
126 ev_ptr,
127 )?;
128 }
129 Ok(())
130}
131
132pub fn epoll_wait(
134 epfd: RawFd,
135 events: &mut [EpollEvent],
136 timeout: Option<&Timespec>,
137) -> SysResult<usize> {
138 let ts_ptr = timeout.map_or(0, |t| core::ptr::from_ref(t) as usize);
139 unsafe {
141 syscall6_epoll_pwait2(
142 epfd as usize,
143 events.as_mut_ptr() as usize,
144 events.len(),
145 ts_ptr,
146 )
147 }
148}
149
150#[inline]
151unsafe fn syscall6_epoll_pwait2(
152 epfd: usize,
153 events_ptr: usize,
154 maxevents: usize,
155 timeout_ptr: usize,
156) -> SysResult<usize> {
157 unsafe {
159 crate::raw::syscall6(
160 nr::EPOLL_PWAIT2,
161 epfd,
162 events_ptr,
163 maxevents,
164 timeout_ptr,
165 0,
166 8,
167 )
168 }
169}
170
171pub fn eventfd(initval: u32, flags: i32) -> SysResult<RawFd> {
173 unsafe { syscall2(nr::EVENTFD2, initval as usize, flags as usize).map(|v| v as RawFd) }
175}
176
177#[cfg(test)]
178mod tests {
179 use super::*;
180 use crate::raw::with_mock_syscall;
181
182 #[test]
183 fn poll_converts_timeout_ms_to_ppoll_timespec() {
184 let mut fds = [PollFd {
185 fd: 3,
186 events: POLLIN,
187 revents: 0,
188 }];
189
190 let ready = with_mock_syscall(
191 |sysno, args| {
192 assert_eq!(sysno, nr::PPOLL);
193 assert_eq!(args[1], 1);
194 let ts_ptr = args[2] as *const Timespec;
195 assert!(!ts_ptr.is_null());
196 let ts = unsafe { *ts_ptr };
198 assert_eq!(
199 ts,
200 Timespec {
201 tv_sec: 1,
202 tv_nsec: 250_000_000,
203 }
204 );
205 assert_eq!(args[3], 0);
206 assert_eq!(args[4], 8);
207 let pfd = args[0] as *mut PollFd;
208 unsafe {
210 (*pfd).revents = POLLIN;
211 }
212 Ok(1)
213 },
214 || poll(&mut fds, 1250).unwrap(),
215 );
216 assert_eq!(ready, 1);
217 assert_eq!(fds[0].revents, POLLIN);
218
219 with_mock_syscall(
221 |sysno, args| {
222 assert_eq!(sysno, nr::PPOLL);
223 assert_eq!(args[2], 0);
224 Ok(0)
225 },
226 || {
227 assert_eq!(poll(&mut fds, -1).unwrap(), 0);
228 },
229 );
230 }
231}