xref: /relibc/src/header/sys_epoll/mod.rs (revision 44f343bec8db8fbca649c11764353d21a0fbce6b)
1 //! sys/epoll.h implementation for Redox, following http://man7.org/linux/man-pages/man7/epoll.7.html
2 
3 use core::ptr;
4 
5 use crate::{
6     header::signal::sigset_t,
7     platform::{types::*, PalEpoll, Sys},
8 };
9 
10 pub use self::sys::*;
11 
12 #[cfg(target_os = "linux")]
13 #[path = "linux.rs"]
14 pub mod sys;
15 
16 #[cfg(target_os = "redox")]
17 #[path = "redox.rs"]
18 pub mod sys;
19 
20 pub const EPOLL_CTL_ADD: c_int = 1;
21 pub const EPOLL_CTL_DEL: c_int = 2;
22 pub const EPOLL_CTL_MOD: c_int = 3;
23 
24 #[repr(C)]
25 #[derive(Clone, Copy)]
26 pub union epoll_data {
27     pub ptr: *mut c_void,
28     pub fd: c_int,
29     pub u32: u32,
30     pub u64: u64,
31 }
32 impl Default for epoll_data {
33     fn default() -> Self {
34         Self { u64: 0 }
35     }
36 }
37 
38 #[cfg(all(target_os = "redox", target_pointer_width = "64"))]
39 #[repr(C)]
40 #[derive(Clone, Copy, Default)]
41 // This will match in size with syscall::Event (24 bytes on 64-bit
42 // systems) on redox. The `Default` trait is here so we don't need to
43 // worry about the padding when using this type.
44 pub struct epoll_event {
45     pub events: u32, // 4 bytes
46     // 4 automatic alignment bytes
47     pub data: epoll_data, // 8 bytes
48 
49     pub _pad: u64, // 8 bytes
50 }
51 
52 #[cfg(not(all(target_os = "redox", target_pointer_width = "64")))]
53 #[repr(C)]
54 #[derive(Clone, Copy, Default)]
55 pub struct epoll_event {
56     pub events: u32,
57     pub data: epoll_data,
58 }
59 
60 #[no_mangle]
61 pub extern "C" fn epoll_create(_size: c_int) -> c_int {
62     epoll_create1(0)
63 }
64 
65 #[no_mangle]
66 pub extern "C" fn epoll_create1(flags: c_int) -> c_int {
67     trace_expr!(Sys::epoll_create1(flags), "epoll_create1({:#x})", flags)
68 }
69 
70 #[no_mangle]
71 pub extern "C" fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *mut epoll_event) -> c_int {
72     trace_expr!(
73         Sys::epoll_ctl(epfd, op, fd, event),
74         "epoll_ctl({}, {}, {}, {:p})",
75         epfd,
76         op,
77         fd,
78         event
79     )
80 }
81 
82 #[no_mangle]
83 pub extern "C" fn epoll_wait(
84     epfd: c_int,
85     events: *mut epoll_event,
86     maxevents: c_int,
87     timeout: c_int,
88 ) -> c_int {
89     epoll_pwait(epfd, events, maxevents, timeout, ptr::null())
90 }
91 
92 #[no_mangle]
93 pub extern "C" fn epoll_pwait(
94     epfd: c_int,
95     events: *mut epoll_event,
96     maxevents: c_int,
97     timeout: c_int,
98     sigmask: *const sigset_t,
99 ) -> c_int {
100     trace_expr!(
101         Sys::epoll_pwait(epfd, events, maxevents, timeout, sigmask),
102         "epoll_pwait({}, {:p}, {}, {}, {:p})",
103         epfd,
104         events,
105         maxevents,
106         timeout,
107         sigmask
108     )
109 }
110