1 .\" Copyright (C) 1997 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright (C) 2006, Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
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.
31 .TH POLL 2 2015-12-28 "Linux" "Linux Programmer's Manual"
33 poll, ppoll \- wait for some event on a file descriptor
38 .BI "int poll(struct pollfd *" fds ", nfds_t " nfds ", int " timeout );
40 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
41 .B #include <signal.h>
44 .BI "int ppoll(struct pollfd *" fds ", nfds_t " nfds ", "
45 .BI " const struct timespec *" tmo_p ", const sigset_t *" sigmask );
49 performs a similar task to
51 it waits for one of a set of file descriptors to become ready
54 The set of file descriptors to be monitored is specified in the
56 argument, which is an array of structures of the following form:
61 int fd; /* file descriptor */
62 short events; /* requested events */
63 short revents; /* returned events */
68 The caller should specify the number of items in the
75 contains a file descriptor for an open file.
76 If this field is negative, then the corresponding
78 field is ignored and the
81 (This provides an easy way of ignoring a
82 file descriptor for a single
84 call: simply negate the
87 Note, however, that this technique can't be used to ignore file descriptor 0.)
91 is an input parameter, a bit mask specifying the events the application
92 is interested in for the file descriptor
94 This field may be specified as zero,
95 in which case the only events that can be returned in
106 is an output parameter, filled by the kernel with the events that
110 can include any of those specified in
117 (These three bits are meaningless in the
119 field, and will be set in the
121 field whenever the corresponding condition is true.)
123 If none of the events requested (and no error) has occurred for any
124 of the file descriptors, then
126 blocks until one of the events occurs.
130 argument specifies the number of milliseconds that
132 should block waiting for a file descriptor to become ready.
133 The call will block until either:
135 a file descriptor becomes ready;
137 the call is interrupted by a signal handler; or
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
148 means an infinite timeout.
153 to return immediately, even if no file descriptors are ready.
155 The bits that may be set/returned in
159 are defined in \fI<poll.h>\fP:
163 There is data to read.
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).
170 Writing is now possible, though a write larger that the available space
171 in a socket or pipe will still block (unless
175 .BR POLLRDHUP " (since Linux 2.6.17)"
176 Stream socket peer closed connection,
177 or shut down writing half of connection.
180 feature test macro must be defined
184 in order to obtain this definition.
187 Error condition (only returned in
193 Hang up (only returned in
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.
205 not open (only returned in
213 defined, one also has the following,
214 which convey no further information beyond the bits listed above:
222 Priority band data can be read (generally unused on Linux).
223 .\" POLLRDBAND is used in the DECnet protocol.
230 Priority data may be written.
233 Linux also knows about, but does not use
236 The relationship between
240 is analogous to the relationship between
247 allows an application to safely wait until either a file descriptor
248 becomes ready or until a signal is caught.
250 Other than the difference in the precision of the
252 argument, the following
257 ready = ppoll(&fds, nfds, tmo_p, &sigmask);
262 executing the following calls:
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);
275 See the description of
277 for an explanation of why
283 argument is specified as NULL, then
284 no signal mask manipulation is performed
289 only in the precision of the
295 argument specifies an upper limit on the amount of time that
298 This argument is a pointer to a structure of the following form:
303 long tv_sec; /* seconds */
304 long tv_nsec; /* nanoseconds */
311 is specified as NULL, then
313 can block indefinitely.
315 On success, a positive number is returned; this is
316 the number of structures which have nonzero
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
323 is set appropriately.
327 The array given as argument was not contained in the calling program's
331 A signal occurred before any requested event; see
342 There was no space to allocate file descriptor tables.
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)
351 wrapper function provides emulation using
356 system call was added to Linux in kernel 2.6.16.
359 library call was added in glibc 2.4.
362 conforms to POSIX.1-2001 and POSIX.1-2008.
365 .\" NetBSD 3.0 has a pollts() which is like Linux ppoll().
367 On some other UNIX systems,
368 .\" Darwin, according to a report by Jeremy Sequoia, relayed by Josh Triplett
370 can fail with the error
372 if the system fails to allocate kernel-internal resources, rather than
375 POSIX permits this behavior.
376 Portable programs may wish to check for
378 and loop, just as with
381 Some implementations define the nonstandard constant
383 with the value \-1 for use as a
387 This constant is not provided in glibc.
389 For a discussion of what may happen if a file descriptor being monitored by
391 is closed in another thread, see
393 .SS C library/kernel differences
396 system call modifies its
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.
404 function does not modify its
410 system call has a fifth argument,
411 .IR "size_t sigsetsize" ,
412 which specifies the size in bytes of the
417 wrapper function specifies this argument as a fixed value
419 .IR sizeof(sigset_t) ).
421 See the discussion of spurious readiness notifications under the
425 .BR restart_syscall (2),