1 /**
2  * D header file to interface with the Linux epoll API (http://man7.org/linux/man-pages/man7/epoll.7.html).
3  * Available since Linux 2.6
4  *
5  * Copyright: Copyright Adil Baig 2012.
6  * License : $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7  * Authors  : Adil Baig (github.com/adilbaig)
8  */
9 module core.sys.linux.epoll;
10 
11 version (linux):
12 
13 import core.sys.posix.signal : sigset_t;
14 
15 extern (C):
16 @nogc:
17 nothrow:
18 
19 version (ARM)     version = ARM_Any;
20 version (AArch64) version = ARM_Any;
21 version (HPPA)    version = HPPA_Any;
22 version (MIPS32)  version = MIPS_Any;
23 version (MIPS64)  version = MIPS_Any;
24 version (PPC)     version = PPC_Any;
25 version (PPC64)   version = PPC_Any;
26 version (RISCV32) version = RISCV_Any;
27 version (RISCV64) version = RISCV_Any;
28 version (S390)    version = IBMZ_Any;
29 version (SPARC)   version = SPARC_Any;
30 version (SPARC64) version = SPARC_Any;
31 version (SystemZ) version = IBMZ_Any;
32 version (X86)     version = X86_Any;
33 version (X86_64)  version = X86_Any;
34 
35 enum
36 {
37     EPOLL_CLOEXEC  = 0x80000,
38     EPOLL_NONBLOCK = 0x800
39 }
40 
41 enum
42 {
43     EPOLLIN     = 0x001,
44     EPOLLPRI    = 0x002,
45     EPOLLOUT    = 0x004,
46     EPOLLRDNORM = 0x040,
47     EPOLLRDBAND = 0x080,
48     EPOLLWRNORM = 0x100,
49     EPOLLWRBAND = 0x200,
50     EPOLLMSG    = 0x400,
51     EPOLLERR    = 0x008,
52     EPOLLHUP    = 0x010,
53     EPOLLRDHUP  = 0x2000, // since Linux 2.6.17
54     EPOLLEXCLUSIVE = 1u << 28, // since Linux 4.5
55     EPOLLWAKEUP = 1u << 29,
56     EPOLLONESHOT = 1u << 30,
57     EPOLLET     = 1u << 31
58 }
59 
60 /**
61  * Valid opcodes ( "op" parameter ) to issue to epoll_ctl().
62  */
63 enum
64 {
65     EPOLL_CTL_ADD = 1, /// Add a file descriptor to the interface.
66     EPOLL_CTL_DEL = 2, /// Remove a file descriptor from the interface.
67     EPOLL_CTL_MOD = 3, /// Change file descriptor epoll_event structure.
68 }
69 
70 version (X86_Any)
71 {
72     align(1) struct epoll_event
73     {
74     align(1):
75         uint events;
76         epoll_data_t data;
77     }
78 }
79 else version (ARM_Any)
80 {
81     struct epoll_event
82     {
83         uint events;
84         epoll_data_t data;
85     }
86 }
87 else version (PPC_Any)
88 {
89     struct epoll_event
90     {
91         uint events;
92         epoll_data_t data;
93     }
94 }
95 else version (HPPA_Any)
96 {
97     struct epoll_event
98     {
99         uint events;
100         epoll_data_t data;
101     }
102 }
103 else version (MIPS_Any)
104 {
105     struct epoll_event
106     {
107         uint events;
108         epoll_data_t data;
109     }
110 }
111 else version (RISCV_Any)
112 {
113     struct epoll_event
114     {
115         uint events;
116         epoll_data_t data;
117     }
118 }
119 else version (SPARC_Any)
120 {
121     struct epoll_event
122     {
123         uint events;
124         epoll_data_t data;
125     }
126 }
127 else version (IBMZ_Any)
128 {
129     struct epoll_event
130     {
131         uint events;
132         epoll_data_t data;
133     }
134 }
135 else version (LoongArch64)
136 {
137     struct epoll_event
138     {
139         uint events;
140         epoll_data_t data;
141     }
142 }
143 else
144 {
145     static assert(false, "Platform not supported");
146 }
147 
148 union epoll_data_t
149 {
150     void *ptr;
151     int fd;
152     uint u32;
153     ulong u64;
154 }
155 
156 /**
157  * Creates an epoll instance.
158  *
159  * Params:
160  *   size = a hint specifying the number of file descriptors to be associated
161  *          with the new instance.  T
162  * Returns: an fd for the new instance. The fd returned by epoll_create() should
163  *          be closed with close().
164  * See_also: epoll_create1 (int flags)
165  */
166 int epoll_create (int size);
167 
168 /* Same as epoll_create but with an FLAGS parameter.  The unused SIZE
169    parameter has been dropped.  */
170 
171 /**
172  * Creates an epoll instance.
173  *
174  * Params:
175  *   flags = a specified flag. If flags is 0, then, other than the fact that the
176  *           obsolete size argument is dropped, epoll_create1() is the same as
177  *           epoll_create().
178  * Returns: an fd for the new instance. The fd returned by epoll_create() should
179  *          be closed with close().
180  * See_also: epoll_create (int size)
181  */
182 int epoll_create1 (int flags);
183 
184 /**
185  * Manipulate an epoll instance
186  *
187  * Params:
188  *   epfd = an epoll file descriptor instance
189  *   op = one of the EPOLL_CTL_* constants
190  *   fd = target file descriptor of the operation
191  *   event = describes which events the caller is interested in and any
192  *           associated user dat
193  * Returns: 0 in case of success, -1 in case of error ( the "errno" variable
194  *          will contain the specific error code )
195  */
196 int epoll_ctl (int epfd, int op, int fd, epoll_event *event);
197 
198 
199 /**
200  * Wait for events on an epoll instance.
201  *
202  *
203  * Params:
204  *   epfd = an epoll file descriptor instance
205  *   events = a buffer that will contain triggered events
206  *   maxevents = the maximum number of events to be returned ( usually size of
207  *               "events" )
208  *   timeout = specifies the maximum wait time in milliseconds (-1 == infinite)
209  *
210  * Returns: the number of triggered events returned in "events" buffer. Or -1 in
211  *          case of error with the "errno" variable set to the specific error
212  *          code.
213  */
214 int epoll_wait (int epfd, epoll_event *events, int maxevents, int timeout);
215 
216 /**
217  * Wait for events on an epoll instance
218  *
219  *
220  * Params:
221  *   epfd = an epoll file descriptor instance
222  *   events = a buffer that will contain triggered events
223  *   maxevents = the maximum number of events to be returned ( usually size of
224  *               "events" )
225  *   timeout = specifies the maximum wait time in milliseconds (-1 == infinite)
226  *   ss = a signal set. May be specified as `null`, in which case epoll_pwait() is
227  *        equivalent to epoll_wait().
228  *
229  * Returns: the number of triggered events returned in "events" buffer. Or -1 in
230  *          case of error with the "errno" variable set to the specific error
231  *          code.
232  */
233 int epoll_pwait (int epfd, epoll_event *events, int maxevents, int timeout,
234     const sigset_t *ss);