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
136 {
137     static assert(false, "Platform not supported");
138 }
139 
140 union epoll_data_t
141 {
142     void *ptr;
143     int fd;
144     uint u32;
145     ulong u64;
146 }
147 
148 /**
149  * Creates an epoll instance.
150  *
151  * Params:
152  *   size = a hint specifying the number of file descriptors to be associated
153  *          with the new instance.  T
154  * Returns: an fd for the new instance. The fd returned by epoll_create() should
155  *          be closed with close().
156  * See_also: epoll_create1 (int flags)
157  */
158 int epoll_create (int size);
159 
160 /* Same as epoll_create but with an FLAGS parameter.  The unused SIZE
161    parameter has been dropped.  */
162 
163 /**
164  * Creates an epoll instance.
165  *
166  * Params:
167  *   flags = a specified flag. If flags is 0, then, other than the fact that the
168  *           obsolete size argument is dropped, epoll_create1() is the same as
169  *           epoll_create().
170  * Returns: an fd for the new instance. The fd returned by epoll_create() should
171  *          be closed with close().
172  * See_also: epoll_create (int size)
173  */
174 int epoll_create1 (int flags);
175 
176 /**
177  * Manipulate an epoll instance
178  *
179  * Params:
180  *   epfd = an epoll file descriptor instance
181  *   op = one of the EPOLL_CTL_* constants
182  *   fd = target file descriptor of the operation
183  *   event = describes which events the caller is interested in and any
184  *           associated user dat
185  * Returns: 0 in case of success, -1 in case of error ( the "errno" variable
186  *          will contain the specific error code )
187  */
188 int epoll_ctl (int epfd, int op, int fd, epoll_event *event);
189 
190 
191 /**
192  * Wait for events on an epoll instance.
193  *
194  *
195  * Params:
196  *   epfd = an epoll file descriptor instance
197  *   events = a buffer that will contain triggered events
198  *   maxevents = the maximum number of events to be returned ( usually size of
199  *               "events" )
200  *   timeout = specifies the maximum wait time in milliseconds (-1 == infinite)
201  *
202  * Returns: the number of triggered events returned in "events" buffer. Or -1 in
203  *          case of error with the "errno" variable set to the specific error
204  *          code.
205  */
206 int epoll_wait (int epfd, epoll_event *events, int maxevents, int timeout);
207 
208 /**
209  * Wait for events on an epoll instance
210  *
211  *
212  * Params:
213  *   epfd = an epoll file descriptor instance
214  *   events = a buffer that will contain triggered events
215  *   maxevents = the maximum number of events to be returned ( usually size of
216  *               "events" )
217  *   timeout = specifies the maximum wait time in milliseconds (-1 == infinite)
218  *   ss = a signal set. May be specified as `null`, in which case epoll_pwait() is
219  *        equivalent to epoll_wait().
220  *
221  * Returns: the number of triggered events returned in "events" buffer. Or -1 in
222  *          case of error with the "errno" variable set to the specific error
223  *          code.
224  */
225 int epoll_pwait (int epfd, epoll_event *events, int maxevents, int timeout,
226     const sigset_t *ss);