scripts/bash_aliases: tfix
[man-pages.git] / man2 / epoll_wait.2
blob69c79d6eccdb95735b62eb15ee6057f87bf7fb41
1 .\"  Copyright (C) 2003  Davide Libenzi
2 .\"  Davide Libenzi <davidel@xmailserver.org>
3 .\" and Copyright 2007, 2012, 2014, 2018 Michael Kerrisk <tk.manpages@gmail.com>
4 .\"
5 .\" %%%LICENSE_START(GPLv2+_SW_3_PARA)
6 .\"  This program is free software; you can redistribute it and/or modify
7 .\"  it under the terms of the GNU General Public License as published by
8 .\"  the Free Software Foundation; either version 2 of the License, or
9 .\"  (at your option) any later version.
10 .\"
11 .\"  This program is distributed in the hope that it will be useful,
12 .\"  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 .\"  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 .\"  GNU General Public License for more details.
15 .\"
16 .\" You should have received a copy of the GNU General Public
17 .\" License along with this manual; if not, see
18 .\" <http://www.gnu.org/licenses/>.
19 .\" %%%LICENSE_END
20 .\"
21 .\" 2007-04-30: mtk, Added description of epoll_pwait()
22 .\"
23 .TH EPOLL_WAIT 2 2021-03-22 "Linux" "Linux Programmer's Manual"
24 .SH NAME
25 epoll_wait, epoll_pwait, epoll_pwait2 \- wait for an I/O event on an epoll file descriptor
26 .SH SYNOPSIS
27 .nf
28 .B #include <sys/epoll.h>
29 .PP
30 .BI "int epoll_wait(int " epfd ", struct epoll_event *" events ,
31 .BI "               int " maxevents ", int " timeout );
32 .BI "int epoll_pwait(int " epfd ", struct epoll_event *" events ,
33 .BI "               int " maxevents ", int " timeout ,
34 .BI "               const sigset_t *" sigmask );
35 .BI "int epoll_pwait2(int " epfd ", struct epoll_event *" events ,
36 .BI "               int " maxevents ", const struct timespec *" timeout ,
37 .BI "               const sigset_t *" sigmask );
38 .\" FIXME: Check if glibc has added a wrapper for epoll_pwait2(),
39 .\"        and if so, check the prototype.
40 .\"        https://sourceware.org/bugzilla/show_bug.cgi?id=27359
41 .fi
42 .SH DESCRIPTION
43 The
44 .BR epoll_wait ()
45 system call waits for events on the
46 .BR epoll (7)
47 instance referred to by the file descriptor
48 .IR epfd .
49 The buffer pointed to by
50 .I events
51 is used to return information from the ready list
52 about file descriptors in the interest list that
53 have some events available.
54 Up to
55 .I maxevents
56 are returned by
57 .BR epoll_wait ().
58 The
59 .I maxevents
60 argument must be greater than zero.
61 .PP
62 The
63 .I timeout
64 argument specifies the number of milliseconds that
65 .BR epoll_wait ()
66 will block.
67 Time is measured against the
68 .B CLOCK_MONOTONIC
69 clock.
70 .PP
71 A call to
72 .BR epoll_wait ()
73 will block until either:
74 .IP \(bu 2
75 a file descriptor delivers an event;
76 .IP \(bu
77 the call is interrupted by a signal handler; or
78 .IP \(bu
79 the timeout expires.
80 .PP
81 Note that the
82 .I timeout
83 interval will be rounded up to the system clock granularity,
84 and kernel scheduling delays mean that the blocking interval
85 may overrun by a small amount.
86 Specifying a
87 .I timeout
88 of \-1 causes
89 .BR epoll_wait ()
90 to block indefinitely, while specifying a
91 .I timeout
92 equal to zero cause
93 .BR epoll_wait ()
94 to return immediately, even if no events are available.
95 .PP
96 The
97 .I struct epoll_event
98 is defined as:
99 .PP
100 .in +4n
102 typedef union epoll_data {
103     void    *ptr;
104     int      fd;
105     uint32_t u32;
106     uint64_t u64;
107 } epoll_data_t;
109 struct epoll_event {
110     uint32_t     events;    /* Epoll events */
111     epoll_data_t data;      /* User data variable */
117 .I data
118 field of each returned
119 .I epoll_event
120 structure contains the same data as was specified
121 in the most recent call to
122 .BR epoll_ctl (2)
123 .RB ( EPOLL_CTL_ADD ", " EPOLL_CTL_MOD )
124 for the corresponding open file descriptor.
127 .I events
128 field is a bit mask that indicates the events that have occurred for the
129 corresponding open file description.
131 .BR epoll_ctl (2)
132 for a list of the bits that may appear in this mask.
134 .SS epoll_pwait()
135 The relationship between
136 .BR epoll_wait ()
138 .BR epoll_pwait ()
139 is analogous to the relationship between
140 .BR select (2)
142 .BR pselect (2):
143 like
144 .BR pselect (2),
145 .BR epoll_pwait ()
146 allows an application to safely wait until either a file descriptor
147 becomes ready or until a signal is caught.
149 The following
150 .BR epoll_pwait ()
151 call:
153 .in +4n
155 ready = epoll_pwait(epfd, &events, maxevents, timeout, &sigmask);
159 is equivalent to
160 .I atomically
161 executing the following calls:
163 .in +4n
165 sigset_t origmask;
167 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
168 ready = epoll_wait(epfd, &events, maxevents, timeout);
169 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
174 .I sigmask
175 argument may be specified as NULL, in which case
176 .BR epoll_pwait ()
177 is equivalent to
178 .BR epoll_wait ().
180 .SS epoll_pwait2()
182 .BR epoll_pwait2 ()
183 system call is equivalent to
184 .BR epoll_pwait ()
185 except for the
186 .I timeout
187 argument.
188 It takes an argument of type
189 .I timespec
190 to be able to specify nanosecond resolution timeout.
191 This argument functions the same as in
192 .BR pselect (2)
194 .BR ppoll (2).
196 .I timeout
197 is NULL, then
198 .BR epoll_pwait2 ()
199 can block indefinitely.
200 .SH RETURN VALUE
201 On success,
202 .BR epoll_wait ()
203 returns the number of file descriptors ready for the requested I/O, or zero
204 if no file descriptor became ready during the requested
205 .I timeout
206 milliseconds.
207 On failure,
208 .BR epoll_wait ()
209 returns \-1 and
210 .I errno
211 is set to indicate the error.
212 .SH ERRORS
214 .B EBADF
215 .I epfd
216 is not a valid file descriptor.
218 .B EFAULT
219 The memory area pointed to by
220 .I events
221 is not accessible with write permissions.
223 .B EINTR
224 The call was interrupted by a signal handler before either (1) any of the
225 requested events occurred or (2) the
226 .I timeout
227 expired; see
228 .BR signal (7).
230 .B EINVAL
231 .I epfd
232 is not an
233 .B epoll
234 file descriptor, or
235 .I maxevents
236 is less than or equal to zero.
237 .SH VERSIONS
238 .BR epoll_wait ()
239 was added to the kernel in version 2.6.
240 .\" To be precise: kernel 2.5.44.
241 .\" The interface should be finalized by Linux kernel 2.5.66.
242 Library support is provided in glibc starting with version 2.3.2.
244 .BR epoll_pwait ()
245 was added to Linux in kernel 2.6.19.
246 Library support is provided in glibc starting with version 2.6.
248 .BR epoll_pwait2 ()
249 was added to Linux in kernel 5.11.
250 .SH CONFORMING TO
251 .BR epoll_wait (),
252 .BR epoll_pwait (),
254 .BR epoll_pwait2 ()
255 are Linux-specific.
256 .SH NOTES
257 While one thread is blocked in a call to
258 .BR epoll_wait (),
259 it is possible for another thread to add a file descriptor to the waited-upon
260 .B epoll
261 instance.
262 If the new file descriptor becomes ready,
263 it will cause the
264 .BR epoll_wait ()
265 call to unblock.
267 If more than
268 .I maxevents
269 file descriptors are ready when
270 .BR epoll_wait ()
271 is called, then successive
272 .BR epoll_wait ()
273 calls will round robin through the set of ready file descriptors.
274 This behavior helps avoid starvation scenarios,
275 where a process fails to notice that additional file descriptors
276 are ready because it focuses on a set of file descriptors that
277 are already known to be ready.
279 Note that it is possible to call
280 .BR epoll_wait ()
281 on an
282 .B epoll
283 instance whose interest list is currently empty
284 (or whose interest list becomes empty because file descriptors are closed
285 or removed from the interest in another thread).
286 The call will block until some file descriptor is later added to the
287 interest list (in another thread) and that file descriptor becomes ready.
288 .SS C library/kernel differences
289 The raw
290 .BR epoll_pwait ()
292 .BR epoll_pwait2 ()
293 system calls have a sixth argument,
294 .IR "size_t sigsetsize" ,
295 which specifies the size in bytes of the
296 .IR sigmask
297 argument.
298 The glibc
299 .BR epoll_pwait ()
300 wrapper function specifies this argument as a fixed value
301 (equal to
302 .IR sizeof(sigset_t) ).
303 .SH BUGS
304 In kernels before 2.6.37, a
305 .I timeout
306 value larger than approximately
307 .I LONG_MAX / HZ
308 milliseconds is treated as \-1 (i.e., infinity).
309 Thus, for example, on a system where
310 .I sizeof(long)
311 is 4 and the kernel
312 .I HZ
313 value is 1000,
314 this means that timeouts greater than 35.79 minutes are treated as infinity.
315 .SH SEE ALSO
316 .BR epoll_create (2),
317 .BR epoll_ctl (2),
318 .BR epoll (7)