1 .\" Copyright (C) 2006, 2019 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" Additions from Richard Gooch <rgooch@atnf.CSIRO.AU> and aeb, 971207
26 .\" 2006-03-13, mtk, Added ppoll() + various other rewordings
27 .\" 2006-07-01, mtk, Added POLLRDHUP + various other wording and
28 .\" formatting changes.
30 .TH POLL 2 2021-03-22 "Linux" "Linux Programmer's Manual"
32 poll, ppoll \- wait for some event on a file descriptor
37 .BI "int poll(struct pollfd *" fds ", nfds_t " nfds ", int " timeout );
39 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
42 .BI "int ppoll(struct pollfd *" fds ", nfds_t " nfds ,
43 .BI " const struct timespec *" tmo_p ", const sigset_t *" sigmask );
47 performs a similar task to
49 it waits for one of a set of file descriptors to become ready
53 API performs a similar task, but offers features beyond those found in
56 The set of file descriptors to be monitored is specified in the
58 argument, which is an array of structures of the following form:
63 int fd; /* file descriptor */
64 short events; /* requested events */
65 short revents; /* returned events */
70 The caller should specify the number of items in the
77 contains a file descriptor for an open file.
78 If this field is negative, then the corresponding
80 field is ignored and the
83 (This provides an easy way of ignoring a
84 file descriptor for a single
86 call: simply negate the
89 Note, however, that this technique can't be used to ignore file descriptor 0.)
93 is an input parameter, a bit mask specifying the events the application
94 is interested in for the file descriptor
96 This field may be specified as zero,
97 in which case the only events that can be returned in
108 is an output parameter, filled by the kernel with the events that
112 can include any of those specified in
119 (These three bits are meaningless in the
121 field, and will be set in the
123 field whenever the corresponding condition is true.)
125 If none of the events requested (and no error) has occurred for any
126 of the file descriptors, then
128 blocks until one of the events occurs.
132 argument specifies the number of milliseconds that
134 should block waiting for a file descriptor to become ready.
135 The call will block until either:
137 a file descriptor becomes ready;
139 the call is interrupted by a signal handler; or
145 interval will be rounded up to the system clock granularity,
146 and kernel scheduling delays mean that the blocking interval
147 may overrun by a small amount.
148 Specifying a negative value in
150 means an infinite timeout.
155 to return immediately, even if no file descriptors are ready.
157 The bits that may be set/returned in
161 are defined in \fI<poll.h>\fP:
164 There is data to read.
167 There is some exceptional condition on the file descriptor.
168 Possibilities include:
171 There is out-of-band data on a TCP socket (see
174 A pseudoterminal master in packet mode has seen a state change on the slave
180 file has been modified (see
185 Writing is now possible, though a write larger than the available space
186 in a socket or pipe will still block (unless
190 .BR POLLRDHUP " (since Linux 2.6.17)"
191 Stream socket peer closed connection,
192 or shut down writing half of connection.
195 feature test macro must be defined
199 in order to obtain this definition.
202 Error condition (only returned in
206 This bit is also set for a file descriptor referring
207 to the write end of a pipe when the read end has been closed.
210 Hang up (only returned in
214 Note that when reading from a channel such as a pipe or a stream socket,
215 this event merely indicates that the peer closed its end of the channel.
216 Subsequent reads from the channel will return 0 (end of file)
217 only after all outstanding data in the channel has been consumed.
222 not open (only returned in
229 defined, one also has the following,
230 which convey no further information beyond the bits listed above:
237 Priority band data can be read (generally unused on Linux).
238 .\" POLLRDBAND is used in the DECnet protocol.
245 Priority data may be written.
247 Linux also knows about, but does not use
250 The relationship between
254 is analogous to the relationship between
261 allows an application to safely wait until either a file descriptor
262 becomes ready or until a signal is caught.
264 Other than the difference in the precision of the
266 argument, the following
272 ready = ppoll(&fds, nfds, tmo_p, &sigmask);
276 is nearly equivalent to
278 executing the following calls:
285 timeout = (tmo_p == NULL) ? \-1 :
286 (tmo_p\->tv_sec * 1000 + tmo_p\->tv_nsec / 1000000);
287 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
288 ready = poll(&fds, nfds, timeout);
289 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
293 The above code segment is described as
295 equivalent because whereas a negative
299 is interpreted as an infinite timeout, a negative value expressed in
301 results in an error from
304 See the description of
306 for an explanation of why
312 argument is specified as NULL, then
313 no signal mask manipulation is performed
318 only in the precision of the
324 argument specifies an upper limit on the amount of time that
327 This argument is a pointer to a structure of the following form:
332 long tv_sec; /* seconds */
333 long tv_nsec; /* nanoseconds */
340 is specified as NULL, then
342 can block indefinitely.
346 returns a nonnegative value which is the number of elements in the
350 fields have been set to a nonzero value (indicating an event or an error).
351 A return value of zero indicates that the system call timed out
352 before any file descriptors became read.
354 On error, \-1 is returned, and
356 is set to indicate the error.
361 points outside the process's accessible address space.
362 The array given as argument was not contained in the calling program's
366 A signal occurred before any requested event; see
378 The timeout value expressed in
380 is invalid (negative).
383 Unable to allocate memory for kernel data structures.
387 system call was introduced in Linux 2.1.23.
388 On older kernels that lack this system call,
391 wrapper function provides emulation using
396 system call was added to Linux in kernel 2.6.16.
399 library call was added in glibc 2.4.
402 conforms to POSIX.1-2001 and POSIX.1-2008.
406 .\" ppoll() is proposed for inclusion in POSIX:
407 .\" https://www.austingroupbugs.net/view.php?id=1263
408 .\" NetBSD 3.0 has a pollts() which is like Linux ppoll().
414 is not affected by the
418 On some other UNIX systems,
419 .\" Darwin, according to a report by Jeremy Sequoia, relayed by Josh Triplett
421 can fail with the error
423 if the system fails to allocate kernel-internal resources, rather than
426 POSIX permits this behavior.
427 Portable programs may wish to check for
429 and loop, just as with
432 Some implementations define the nonstandard constant
434 with the value \-1 for use as a
438 This constant is not provided in glibc.
440 For a discussion of what may happen if a file descriptor being monitored by
442 is closed in another thread, see
444 .SS C library/kernel differences
447 system call modifies its
450 However, the glibc wrapper function hides this behavior
451 by using a local variable for the timeout argument that
452 is passed to the system call.
455 function does not modify its
461 system call has a fifth argument,
462 .IR "size_t sigsetsize" ,
463 which specifies the size in bytes of the
468 wrapper function specifies this argument as a fixed value
470 .IR sizeof(kernel_sigset_t) ).
473 for a discussion on the differences between the kernel and the libc
474 notion of the sigset.
476 See the discussion of spurious readiness notifications under the
480 The program below opens each of the files named in its command-line
481 arguments and monitors the resulting file descriptors for readiness to read
483 The program loops, repeatedly using
485 to monitor the file descriptors,
486 printing the number of ready file descriptors on return.
487 For each ready file descriptor, the program:
489 displays the returned
491 field in a human-readable form;
493 if the file descriptor is readable, reads some data from it,
494 and displays that data on standard output; and
496 if the file descriptors was not readable,
497 but some other event occurred (presumably
499 closes the file descriptor.
501 Suppose we run the program in one terminal, asking it to open a FIFO:
505 $ \fBmkfifo myfifo\fP
506 $ \fB./poll_input myfifo\fP
510 In a second terminal window, we then open the FIFO for writing,
511 write some data to it, and close the FIFO:
515 $ \fBecho aaaaabbbbbccccc > myfifo\fP
519 In the terminal where we are running the program, we would then see:
523 Opened "myfifo" on fd 3
526 fd=3; events: POLLIN POLLHUP
527 read 10 bytes: aaaaabbbbb
530 fd=3; events: POLLIN POLLHUP
535 fd=3; events: POLLHUP
537 All file descriptors closed; bye
541 In the above output, we see that
543 returned three times:
545 On the first return, the bits returned in the
549 indicating that the file descriptor is readable, and
551 indicating that the other end of the FIFO has been closed.
552 The program then consumed some of the available input.
554 The second return from
560 the program then consumed the last of the available input.
567 at which point the file descriptor was closed and the program terminated.
574 Licensed under GNU General Public License v2 or later.
578 #include <sys/types.h>
583 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
587 main(int argc, char *argv[])
589 int nfds, num_open_fds;
593 fprintf(stderr, "Usage: %s file...\en", argv[0]);
597 num_open_fds = nfds = argc \- 1;
598 pfds = calloc(nfds, sizeof(struct pollfd));
602 /* Open each file on command line, and add it \(aqpfds\(aq array. */
604 for (int j = 0; j < nfds; j++) {
605 pfds[j].fd = open(argv[j + 1], O_RDONLY);
606 if (pfds[j].fd == \-1)
609 printf("Opened \e"%s\e" on fd %d\en", argv[j + 1], pfds[j].fd);
611 pfds[j].events = POLLIN;
614 /* Keep calling poll() as long as at least one file descriptor is
617 while (num_open_fds > 0) {
620 printf("About to poll()\en");
621 ready = poll(pfds, nfds, \-1);
625 printf("Ready: %d\en", ready);
627 /* Deal with array returned by poll(). */
629 for (int j = 0; j < nfds; j++) {
632 if (pfds[j].revents != 0) {
633 printf(" fd=%d; events: %s%s%s\en", pfds[j].fd,
634 (pfds[j].revents & POLLIN) ? "POLLIN " : "",
635 (pfds[j].revents & POLLHUP) ? "POLLHUP " : "",
636 (pfds[j].revents & POLLERR) ? "POLLERR " : "");
638 if (pfds[j].revents & POLLIN) {
639 ssize_t s = read(pfds[j].fd, buf, sizeof(buf));
642 printf(" read %zd bytes: %.*s\en",
644 } else { /* POLLERR | POLLHUP */
645 printf(" closing fd %d\en", pfds[j].fd);
646 if (close(pfds[j].fd) == \-1)
654 printf("All file descriptors closed; bye\en");
659 .BR restart_syscall (2),