]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/poll.2
poll.2: Document spurious EAGAIN error that can occur on other systems
[thirdparty/man-pages.git] / man2 / poll.2
1 .\" Copyright (C) 1997 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright (C) 2006, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Additions from Richard Gooch <rgooch@atnf.CSIRO.AU> and aeb, 971207
27 .\" 2006-03-13, mtk, Added ppoll() + various other rewordings
28 .\" 2006-07-01, mtk, Added POLLRDHUP + various other wording and
29 .\" formatting changes.
30 .\"
31 .TH POLL 2 2015-12-28 "Linux" "Linux Programmer's Manual"
32 .SH NAME
33 poll, ppoll \- wait for some event on a file descriptor
34 .SH SYNOPSIS
35 .nf
36 .B #include <poll.h>
37 .sp
38 .BI "int poll(struct pollfd *" fds ", nfds_t " nfds ", int " timeout );
39 .sp
40 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
41 .B #include <signal.h>
42 .B #include <poll.h>
43 .sp
44 .BI "int ppoll(struct pollfd *" fds ", nfds_t " nfds ", "
45 .BI " const struct timespec *" tmo_p ", const sigset_t *" sigmask );
46 .fi
47 .SH DESCRIPTION
48 .BR poll ()
49 performs a similar task to
50 .BR select (2):
51 it waits for one of a set of file descriptors to become ready
52 to perform I/O.
53
54 The set of file descriptors to be monitored is specified in the
55 .I fds
56 argument, which is an array of structures of the following form:
57 .in +4n
58 .nf
59
60 struct pollfd {
61 int fd; /* file descriptor */
62 short events; /* requested events */
63 short revents; /* returned events */
64 };
65 .in
66 .fi
67 .PP
68 The caller should specify the number of items in the
69 .I fds
70 array in
71 .IR nfds .
72
73 The field
74 .I fd
75 contains a file descriptor for an open file.
76 If this field is negative, then the corresponding
77 .I events
78 field is ignored and the
79 .I revents
80 field returns zero.
81 (This provides an easy way of ignoring a
82 file descriptor for a single
83 .BR poll ()
84 call: simply negate the
85 .I fd
86 field.
87 Note, however, that this technique can't be used to ignore file descriptor 0.)
88
89 The field
90 .I events
91 is an input parameter, a bit mask specifying the events the application
92 is interested in for the file descriptor
93 .IR fd .
94 This field may be specified as zero,
95 in which case the only events that can be returned in
96 .I revents
97 are
98 .BR POLLHUP ,
99 .BR POLLERR ,
100 and
101 .B POLLNVAL
102 (see below).
103
104 The field
105 .I revents
106 is an output parameter, filled by the kernel with the events that
107 actually occurred.
108 The bits returned in
109 .I revents
110 can include any of those specified in
111 .IR events ,
112 or one of the values
113 .BR POLLERR ,
114 .BR POLLHUP ,
115 or
116 .BR POLLNVAL .
117 (These three bits are meaningless in the
118 .I events
119 field, and will be set in the
120 .I revents
121 field whenever the corresponding condition is true.)
122
123 If none of the events requested (and no error) has occurred for any
124 of the file descriptors, then
125 .BR poll ()
126 blocks until one of the events occurs.
127
128 The
129 .I timeout
130 argument specifies the number of milliseconds that
131 .BR poll ()
132 should block waiting for a file descriptor to become ready.
133 The call will block until either:
134 .IP * 3
135 a file descriptor becomes ready;
136 .IP *
137 the call is interrupted by a signal handler; or
138 .IP *
139 the timeout expires.
140 .PP
141 Note that the
142 .I timeout
143 interval will be rounded up to the system clock granularity,
144 and kernel scheduling delays mean that the blocking interval
145 may overrun by a small amount.
146 Specifying a negative value in
147 .I timeout
148 means an infinite timeout.
149 Specifying a
150 .I timeout
151 of zero causes
152 .BR poll ()
153 to return immediately, even if no file descriptors are ready.
154
155 The bits that may be set/returned in
156 .I events
157 and
158 .I revents
159 are defined in \fI<poll.h>\fP:
160 .RS
161 .TP
162 .B POLLIN
163 There is data to read.
164 .TP
165 .B POLLPRI
166 There is urgent data to read (e.g., out-of-band data on TCP socket;
167 pseudoterminal master in packet mode has seen state change in slave).
168 .TP
169 .B POLLOUT
170 Writing is now possible, though a write larger that the available space
171 in a socket or pipe will still block (unless
172 .B O_NONBLOCK
173 is set).
174 .TP
175 .BR POLLRDHUP " (since Linux 2.6.17)"
176 Stream socket peer closed connection,
177 or shut down writing half of connection.
178 The
179 .B _GNU_SOURCE
180 feature test macro must be defined
181 (before including
182 .I any
183 header files)
184 in order to obtain this definition.
185 .TP
186 .B POLLERR
187 Error condition (only returned in
188 .IR revents ;
189 ignored in
190 .IR events ).
191 .TP
192 .B POLLHUP
193 Hang up (only returned in
194 .IR revents ;
195 ignored in
196 .IR events ).
197 Note that when reading from a channel such as a pipe or a stream socket,
198 this event merely indicates that the peer closed its end of the channel.
199 Subsequent reads from the channel will return 0 (end of file)
200 only after all outstanding data in the channel has been consumed.
201 .TP
202 .B POLLNVAL
203 Invalid request:
204 .I fd
205 not open (only returned in
206 .IR revents ;
207 ignored in
208 .IR events ).
209 .RE
210 .PP
211 When compiling with
212 .B _XOPEN_SOURCE
213 defined, one also has the following,
214 which convey no further information beyond the bits listed above:
215 .RS
216 .TP
217 .B POLLRDNORM
218 Equivalent to
219 .BR POLLIN .
220 .TP
221 .B POLLRDBAND
222 Priority band data can be read (generally unused on Linux).
223 .\" POLLRDBAND is used in the DECnet protocol.
224 .TP
225 .B POLLWRNORM
226 Equivalent to
227 .BR POLLOUT .
228 .TP
229 .B POLLWRBAND
230 Priority data may be written.
231 .RE
232 .PP
233 Linux also knows about, but does not use
234 .BR POLLMSG .
235 .SS ppoll()
236 The relationship between
237 .BR poll ()
238 and
239 .BR ppoll ()
240 is analogous to the relationship between
241 .BR select (2)
242 and
243 .BR pselect (2):
244 like
245 .BR pselect (2),
246 .BR ppoll ()
247 allows an application to safely wait until either a file descriptor
248 becomes ready or until a signal is caught.
249 .PP
250 Other than the difference in the precision of the
251 .I timeout
252 argument, the following
253 .BR ppoll ()
254 call:
255 .nf
256
257 ready = ppoll(&fds, nfds, tmo_p, &sigmask);
258
259 .fi
260 is equivalent to
261 .I atomically
262 executing the following calls:
263 .nf
264
265 sigset_t origmask;
266 int timeout;
267
268 timeout = (tmo_p == NULL) ? \-1 :
269 (tmo_p\->tv_sec * 1000 + tmo_p\->tv_nsec / 1000000);
270 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
271 ready = poll(&fds, nfds, timeout);
272 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
273 .fi
274 .PP
275 See the description of
276 .BR pselect (2)
277 for an explanation of why
278 .BR ppoll ()
279 is necessary.
280
281 If the
282 .I sigmask
283 argument is specified as NULL, then
284 no signal mask manipulation is performed
285 (and thus
286 .BR ppoll ()
287 differs from
288 .BR poll ()
289 only in the precision of the
290 .I timeout
291 argument).
292
293 The
294 .I tmo_p
295 argument specifies an upper limit on the amount of time that
296 .BR ppoll ()
297 will block.
298 This argument is a pointer to a structure of the following form:
299 .in +4n
300 .nf
301
302 struct timespec {
303 long tv_sec; /* seconds */
304 long tv_nsec; /* nanoseconds */
305 };
306 .fi
307 .in
308
309 If
310 .I tmo_p
311 is specified as NULL, then
312 .BR ppoll ()
313 can block indefinitely.
314 .SH RETURN VALUE
315 On success, a positive number is returned; this is
316 the number of structures which have nonzero
317 .I revents
318 fields (in other words, those descriptors with events or errors reported).
319 A value of 0 indicates that the call timed out and no file
320 descriptors were ready.
321 On error, \-1 is returned, and
322 .I errno
323 is set appropriately.
324 .SH ERRORS
325 .TP
326 .B EFAULT
327 The array given as argument was not contained in the calling program's
328 address space.
329 .TP
330 .B EINTR
331 A signal occurred before any requested event; see
332 .BR signal (7).
333 .TP
334 .B EINVAL
335 The
336 .I nfds
337 value exceeds the
338 .B RLIMIT_NOFILE
339 value.
340 .TP
341 .B ENOMEM
342 There was no space to allocate file descriptor tables.
343 .SH VERSIONS
344 The
345 .BR poll ()
346 system call was introduced in Linux 2.1.23.
347 On older kernels that lack this system call,
348 .\" library call was introduced in libc 5.4.28
349 the glibc (and the old Linux libc)
350 .BR poll ()
351 wrapper function provides emulation using
352 .BR select (2).
353
354 The
355 .BR ppoll ()
356 system call was added to Linux in kernel 2.6.16.
357 The
358 .BR ppoll ()
359 library call was added in glibc 2.4.
360 .SH CONFORMING TO
361 .BR poll ()
362 conforms to POSIX.1-2001 and POSIX.1-2008.
363 .BR ppoll ()
364 is Linux-specific.
365 .\" NetBSD 3.0 has a pollts() which is like Linux ppoll().
366 .SH NOTES
367 On some other UNIX systems,
368 .\" Darwin, according to a report by Jeremy Sequoia, relayed by Josh Triplett
369 .BR poll ()
370 can fail with the error
371 .B EAGAIN
372 if the system fails to allocate kernel-internal resources, rather than
373 .B ENOMEM
374 as Linux does.
375 POSIX permits this behavior.
376 Portable programs may wish to check for
377 .B EAGAIN
378 and loop, just as with
379 .BR EINTR .
380
381 Some implementations define the nonstandard constant
382 .B INFTIM
383 with the value \-1 for use as a
384 .IR timeout
385 for
386 .BR poll ().
387 This constant is not provided in glibc.
388
389 For a discussion of what may happen if a file descriptor being monitored by
390 .BR poll ()
391 is closed in another thread, see
392 .BR select (2).
393 .SS C library/kernel differences
394 The Linux
395 .BR ppoll ()
396 system call modifies its
397 .I tmo_p
398 argument.
399 However, the glibc wrapper function hides this behavior
400 by using a local variable for the timeout argument that
401 is passed to the system call.
402 Thus, the glibc
403 .BR ppoll ()
404 function does not modify its
405 .I tmo_p
406 argument.
407
408 The raw
409 .BR ppoll ()
410 system call has a fifth argument,
411 .IR "size_t sigsetsize" ,
412 which specifies the size in bytes of the
413 .IR sigmask
414 argument.
415 The glibc
416 .BR ppoll ()
417 wrapper function specifies this argument as a fixed value
418 (equal to
419 .IR sizeof(sigset_t) ).
420 .SH BUGS
421 See the discussion of spurious readiness notifications under the
422 BUGS section of
423 .BR select (2).
424 .SH SEE ALSO
425 .BR restart_syscall (2),
426 .BR select (2),
427 .BR select_tut (2),
428 .BR epoll (7),
429 .BR time (7)