]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man/man2/poll.2
man/, share/mk/: Move man*/ to man/
[thirdparty/man-pages.git] / man / man2 / poll.2
CommitLineData
733192cb 1.\" Copyright (C) 2006, 2019 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 2.\"
5fbde956 3.\" SPDX-License-Identifier: Linux-man-pages-copyleft
fea681da
MK
4.\"
5.\" Additions from Richard Gooch <rgooch@atnf.CSIRO.AU> and aeb, 971207
e755a587 6.\" 2006-03-13, mtk, Added ppoll() + various other rewordings
23cb3cb5
MK
7.\" 2006-07-01, mtk, Added POLLRDHUP + various other wording and
8.\" formatting changes.
4b9d4c1a 9.\"
4c1c5274 10.TH poll 2 (date) "Linux man-pages (unreleased)"
fea681da 11.SH NAME
e755a587 12poll, ppoll \- wait for some event on a file descriptor
c29343b4
AC
13.SH LIBRARY
14Standard C library
8fc3b2cf 15.RI ( libc ", " \-lc )
fea681da 16.SH SYNOPSIS
e755a587 17.nf
23cb3cb5 18.B #include <poll.h>
c6d039a3 19.P
e755a587 20.BI "int poll(struct pollfd *" fds ", nfds_t " nfds ", int " timeout );
c6d039a3 21.P
b80f966b 22.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
23cb3cb5 23.B #include <poll.h>
c6d039a3 24.P
77ca5b1d 25.BI "int ppoll(struct pollfd *" fds ", nfds_t " nfds ,
5490a42f
AC
26.BI " const struct timespec *_Nullable " tmo_p ,
27.BI " const sigset_t *_Nullable " sigmask );
e755a587 28.fi
fea681da 29.SH DESCRIPTION
96296ef0 30.BR poll ()
e755a587
MK
31performs a similar task to
32.BR select (2):
33it waits for one of a set of file descriptors to become ready
34to perform I/O.
83bb822c
MK
35The Linux-specific
36.BR epoll (7)
37API performs a similar task, but offers features beyond those found in
a36d5a35 38.BR poll ().
c6d039a3 39.P
e755a587
MK
40The set of file descriptors to be monitored is specified in the
41.I fds
d8dfbff9 42argument, which is an array of structures of the following form:
c6d039a3 43.P
088a639b 44.in +4n
e646a1ba 45.EX
a08ea57c
MK
46struct pollfd {
47 int fd; /* file descriptor */
48 short events; /* requested events */
49 short revents; /* returned events */
50};
b8302363 51.EE
e646a1ba 52.in
c6d039a3 53.P
d8dfbff9
MK
54The caller should specify the number of items in the
55.I fds
56array in
57.IR nfds .
c6d039a3 58.P
fea681da
MK
59The field
60.I fd
61contains a file descriptor for an open file.
096ca08a
MK
62If this field is negative, then the corresponding
63.I events
64field is ignored and the
65.I revents
66field returns zero.
67(This provides an easy way of ignoring a
68file descriptor for a single
69.BR poll ()
17411a0f 70call: simply set the
096ca08a 71.I fd
17411a0f 72field to its bitwise complement.)
c6d039a3 73.P
fea681da
MK
74The field
75.I events
10f5f294 76is an input parameter, a bit mask specifying the events the application
b857fda5
MK
77is interested in for the file descriptor
78.IR fd .
210cded6
MK
79This field may be specified as zero,
80in which case the only events that can be returned in
b857fda5 81.I revents
210cded6
MK
82are
83.BR POLLHUP ,
84.BR POLLERR ,
85and
86.B POLLNVAL
87(see below).
c6d039a3 88.P
fea681da
MK
89The field
90.I revents
91is an output parameter, filled by the kernel with the events that
e755a587
MK
92actually occurred.
93The bits returned in
94.I revents
c13182ef 95can include any of those specified in
e755a587
MK
96.IR events ,
97or one of the values
98.BR POLLERR ,
99.BR POLLHUP ,
fea681da
MK
100or
101.BR POLLNVAL .
102(These three bits are meaningless in the
103.I events
104field, and will be set in the
105.I revents
106field whenever the corresponding condition is true.)
c6d039a3 107.P
fea681da 108If none of the events requested (and no error) has occurred for any
e755a587
MK
109of the file descriptors, then
110.BR poll ()
111blocks until one of the events occurs.
c6d039a3 112.P
c13182ef 113The
e755a587 114.I timeout
0d9101c4 115argument specifies the number of milliseconds that
e755a587 116.BR poll ()
0d9101c4 117should block waiting for a file descriptor to become ready.
40df3d00 118The call will block until either:
cdede5cd 119.IP \[bu] 3
40df3d00 120a file descriptor becomes ready;
cdede5cd 121.IP \[bu]
40df3d00 122the call is interrupted by a signal handler; or
cdede5cd 123.IP \[bu]
216b9e61 124the timeout expires.
c6d039a3 125.P
f9f86b3d 126Being "ready" means that the requested operation will not block; thus,
127.BR poll ()ing
128regular files,
129block devices,
130and other files with no reasonable polling semantic
131.I always
132returns instantly as ready to read and write.
c6d039a3 133.P
40df3d00
MK
134Note that the
135.I timeout
136interval will be rounded up to the system clock granularity,
19257d8f 137and kernel scheduling delays mean that the blocking interval
0d9101c4 138may overrun by a small amount.
e755a587
MK
139Specifying a negative value in
140.I timeout
141means an infinite timeout.
77e74cf1
MK
142Specifying a
143.I timeout
144of zero causes
145.BR poll ()
01d8b73f 146to return immediately, even if no file descriptors are ready.
c6d039a3 147.P
23cb3cb5
MK
148The bits that may be set/returned in
149.I events
150and
151.I revents
c84371c6 152are defined in \fI<poll.h>\fP:
23cb3cb5
MK
153.TP
154.B POLLIN
155There is data to read.
156.TP
157.B POLLPRI
e629dd78
MK
158There is some exceptional condition on the file descriptor.
159Possibilities include:
160.RS
cdede5cd 161.IP \[bu] 3
e629dd78
MK
162There is out-of-band data on a TCP socket (see
163.BR tcp (7)).
cdede5cd 164.IP \[bu]
e629dd78
MK
165A pseudoterminal master in packet mode has seen a state change on the slave
166(see
167.BR ioctl_tty (2)).
cdede5cd 168.IP \[bu]
e629dd78
MK
169A
170.I cgroup.events
171file has been modified (see
172.BR cgroups (7)).
173.RE
23cb3cb5
MK
174.TP
175.B POLLOUT
aeaf667e 176Writing is now possible, though a write larger than the available space
d2e7d1bb
RR
177in a socket or pipe will still block (unless
178.B O_NONBLOCK
179is set).
23cb3cb5
MK
180.TP
181.BR POLLRDHUP " (since Linux 2.6.17)"
c13182ef 182Stream socket peer closed connection,
23cb3cb5 183or shut down writing half of connection.
c13182ef 184The
23cb3cb5 185.B _GNU_SOURCE
e417acb0
MK
186feature test macro must be defined
187(before including
188.I any
189header files)
190in order to obtain this definition.
23cb3cb5
MK
191.TP
192.B POLLERR
b09322c4
MK
193Error condition (only returned in
194.IR revents ;
195ignored in
196.IR events ).
b3da9249
MK
197This bit is also set for a file descriptor referring
198to the write end of a pipe when the read end has been closed.
23cb3cb5
MK
199.TP
200.B POLLHUP
b09322c4
MK
201Hang up (only returned in
202.IR revents ;
203ignored in
204.IR events ).
7e8924ba
MK
205Note that when reading from a channel such as a pipe or a stream socket,
206this event merely indicates that the peer closed its end of the channel.
207Subsequent reads from the channel will return 0 (end of file)
208only after all outstanding data in the channel has been consumed.
23cb3cb5
MK
209.TP
210.B POLLNVAL
c13182ef 211Invalid request:
23cb3cb5 212.I fd
b09322c4
MK
213not open (only returned in
214.IR revents ;
215ignored in
216.IR events ).
c6d039a3 217.P
c13182ef 218When compiling with
23cb3cb5 219.B _XOPEN_SOURCE
c13182ef 220defined, one also has the following,
23cb3cb5 221which convey no further information beyond the bits listed above:
23cb3cb5
MK
222.TP
223.B POLLRDNORM
224Equivalent to
225.BR POLLIN .
226.TP
227.B POLLRDBAND
228Priority band data can be read (generally unused on Linux).
229.\" POLLRDBAND is used in the DECnet protocol.
230.TP
231.B POLLWRNORM
232Equivalent to
233.BR POLLOUT .
234.TP
235.B POLLWRBAND
236Priority data may be written.
c6d039a3 237.P
c13182ef 238Linux also knows about, but does not use
23cb3cb5 239.BR POLLMSG .
e755a587 240.SS ppoll()
c13182ef 241The relationship between
e755a587 242.BR poll ()
c13182ef
MK
243and
244.BR ppoll ()
e755a587 245is analogous to the relationship between
0bfa087b 246.BR select (2)
e755a587 247and
0bfa087b 248.BR pselect (2):
e755a587 249like
0bfa087b 250.BR pselect (2),
e755a587
MK
251.BR ppoll ()
252allows an application to safely wait until either a file descriptor
253becomes ready or until a signal is caught.
c6d039a3 254.P
b8c599cb 255Other than the difference in the precision of the
39c05c26
YK
256.I timeout
257argument, the following
e755a587
MK
258.BR ppoll ()
259call:
c6d039a3 260.P
408731d4
MK
261.in +4n
262.EX
263ready = ppoll(&fds, nfds, tmo_p, &sigmask);
264.EE
265.in
c6d039a3 266.P
0bdda5d0 267is nearly equivalent to
e755a587
MK
268.I atomically
269executing the following calls:
c6d039a3 270.P
408731d4
MK
271.in +4n
272.EX
273sigset_t origmask;
274int timeout;
fe5dba13 275\&
408731d4
MK
276timeout = (tmo_p == NULL) ? \-1 :
277 (tmo_p\->tv_sec * 1000 + tmo_p\->tv_nsec / 1000000);
278pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
279ready = poll(&fds, nfds, timeout);
280pthread_sigmask(SIG_SETMASK, &origmask, NULL);
281.EE
282.in
c6d039a3 283.P
0bdda5d0
MK
284The above code segment is described as
285.I nearly
286equivalent because whereas a negative
287.I timeout
288value for
289.BR poll ()
290is interpreted as an infinite timeout, a negative value expressed in
1ae6b2c7 291.I *tmo_p
0bdda5d0
MK
292results in an error from
293.BR ppoll ().
c6d039a3 294.P
e755a587
MK
295See the description of
296.BR pselect (2)
297for an explanation of why
298.BR ppoll ()
299is necessary.
c6d039a3 300.P
3ee89512
MK
301If the
302.I sigmask
303argument is specified as NULL, then
304no signal mask manipulation is performed
305(and thus
306.BR ppoll ()
307differs from
308.BR poll ()
79fd50e6
YK
309only in the precision of the
310.I timeout
311argument).
c6d039a3 312.P
e755a587 313The
0bf502fc 314.I tmo_p
c13182ef 315argument specifies an upper limit on the amount of time that
e755a587
MK
316.BR ppoll ()
317will block.
e97e048a 318This argument is a pointer to a
319.BR timespec (3)
320structure.
c6d039a3 321.P
e755a587 322If
0bf502fc 323.I tmo_p
c13182ef 324is specified as NULL, then
e755a587
MK
325.BR ppoll ()
326can block indefinitely.
47297adb 327.SH RETURN VALUE
8bcfd688
MK
328On success,
329.BR poll ()
330returns a nonnegative value which is the number of elements in the
331.I pollfds
332whose
fea681da 333.I revents
8bcfd688
MK
334fields have been set to a nonzero value (indicating an event or an error).
335A return value of zero indicates that the system call timed out
acb3399a 336before any file descriptors became ready.
c6d039a3 337.P
c13182ef 338On error, \-1 is returned, and
fea681da 339.I errno
855d489a 340is set to indicate the error.
fea681da
MK
341.SH ERRORS
342.TP
fea681da 343.B EFAULT
342819c8
MK
344.I fds
345points outside the process's accessible address space.
fea681da
MK
346The array given as argument was not contained in the calling program's
347address space.
348.TP
349.B EINTR
01538d0d
MK
350A signal occurred before any requested event; see
351.BR signal (7).
fea681da
MK
352.TP
353.B EINVAL
354The
355.I nfds
682edefb
MK
356value exceeds the
357.B RLIMIT_NOFILE
358value.
fea681da 359.TP
0bdda5d0
MK
360.B EINVAL
361.RB ( ppoll ())
362The timeout value expressed in
03ea37c5 363.I *tmo_p
0bdda5d0
MK
364is invalid (negative).
365.TP
fea681da 366.B ENOMEM
8bcfd688 367Unable to allocate memory for kernel data structures.
e755a587 368.SH VERSIONS
f1463757
MK
369On some other UNIX systems,
370.\" Darwin, according to a report by Jeremy Sequoia, relayed by Josh Triplett
371.BR poll ()
372can fail with the error
373.B EAGAIN
374if the system fails to allocate kernel-internal resources, rather than
375.B ENOMEM
376as Linux does.
377POSIX permits this behavior.
378Portable programs may wish to check for
379.B EAGAIN
380and loop, just as with
381.BR EINTR .
c6d039a3 382.P
c8f2dd47 383Some implementations define the nonstandard constant
c1164764
MK
384.B INFTIM
385with the value \-1 for use as a
1ae6b2c7 386.I timeout
b8c599cb 387for
ba2e3e9a 388.BR poll ().
c1164764 389This constant is not provided in glibc.
0722a578 390.SS C library/kernel differences
4fb31341
MK
391The Linux
392.BR ppoll ()
393system call modifies its
0bf502fc 394.I tmo_p
4fb31341 395argument.
d9bfdb9c 396However, the glibc wrapper function hides this behavior
4fb31341
MK
397by using a local variable for the timeout argument that
398is passed to the system call.
399Thus, the glibc
400.BR ppoll ()
401function does not modify its
0bf502fc 402.I tmo_p
4fb31341 403argument.
c6d039a3 404.P
361ec688
MK
405The raw
406.BR ppoll ()
407system call has a fifth argument,
408.IR "size_t sigsetsize" ,
409which specifies the size in bytes of the
1ae6b2c7 410.I sigmask
361ec688
MK
411argument.
412The glibc
413.BR ppoll ()
414wrapper function specifies this argument as a fixed value
415(equal to
f8dcca84
MK
416.IR sizeof(kernel_sigset_t) ).
417See
418.BR sigprocmask (2)
f25ea51b
N
419for a discussion on the differences between the kernel and the libc
420notion of the sigset.
4131356c
AC
421.SH STANDARDS
422.TP
423.BR poll ()
424POSIX.1-2008.
425.TP
426.BR ppoll ()
427Linux.
428.\" FIXME .
429.\" ppoll() is proposed for inclusion in POSIX:
430.\" https://www.austingroupbugs.net/view.php?id=1263
431.\" NetBSD 3.0 has a pollts() which is like Linux ppoll().
432.SH HISTORY
433.TP
434.BR poll ()
435POSIX.1-2001.
436Linux 2.1.23.
437.IP
438On older kernels that lack this system call,
439the glibc
440.BR poll ()
441wrapper function provides emulation using
442.BR select (2).
443.TP
444.BR ppoll ()
445Linux 2.6.16,
446glibc 2.4.
447.SH NOTES
448The operation of
449.BR poll ()
450and
451.BR ppoll ()
452is not affected by the
453.B O_NONBLOCK
454flag.
c6d039a3 455.P
4131356c
AC
456For a discussion of what may happen if a file descriptor being monitored by
457.BR poll ()
458is closed in another thread, see
459.BR select (2).
a1d5f77c
MK
460.SH BUGS
461See the discussion of spurious readiness notifications under the
462BUGS section of
463.BR select (2).
a14af333 464.SH EXAMPLES
836efc18
MK
465The program below opens each of the files named in its command-line
466arguments and monitors the resulting file descriptors for readiness to read
467.RB ( POLLIN ).
468The program loops, repeatedly using
469.BR poll ()
470to monitor the file descriptors,
471printing the number of ready file descriptors on return.
472For each ready file descriptor, the program:
cdede5cd 473.IP \[bu] 3
836efc18
MK
474displays the returned
475.I revents
476field in a human-readable form;
cdede5cd 477.IP \[bu]
836efc18
MK
478if the file descriptor is readable, reads some data from it,
479and displays that data on standard output; and
cdede5cd 480.IP \[bu]
03ea37c5 481if the file descriptor was not readable,
836efc18
MK
482but some other event occurred (presumably
483.BR POLLHUP ),
484closes the file descriptor.
c6d039a3 485.P
836efc18 486Suppose we run the program in one terminal, asking it to open a FIFO:
c6d039a3 487.P
836efc18
MK
488.in +4n
489.EX
490$ \fBmkfifo myfifo\fP
491$ \fB./poll_input myfifo\fP
492.EE
493.in
c6d039a3 494.P
836efc18
MK
495In a second terminal window, we then open the FIFO for writing,
496write some data to it, and close the FIFO:
c6d039a3 497.P
836efc18
MK
498.in +4n
499.EX
500$ \fBecho aaaaabbbbbccccc > myfifo\fP
501.EE
502.in
c6d039a3 503.P
836efc18 504In the terminal where we are running the program, we would then see:
c6d039a3 505.P
836efc18
MK
506.in +4n
507.EX
508Opened "myfifo" on fd 3
509About to poll()
510Ready: 1
511 fd=3; events: POLLIN POLLHUP
512 read 10 bytes: aaaaabbbbb
513About to poll()
514Ready: 1
515 fd=3; events: POLLIN POLLHUP
516 read 6 bytes: ccccc
fe5dba13 517\&
836efc18
MK
518About to poll()
519Ready: 1
520 fd=3; events: POLLHUP
521 closing fd 3
522All file descriptors closed; bye
523.EE
524.in
c6d039a3 525.P
836efc18
MK
526In the above output, we see that
527.BR poll ()
528returned three times:
cdede5cd 529.IP \[bu] 3
836efc18
MK
530On the first return, the bits returned in the
531.I revents
532field were
533.BR POLLIN ,
534indicating that the file descriptor is readable, and
535.BR POLLHUP ,
536indicating that the other end of the FIFO has been closed.
537The program then consumed some of the available input.
cdede5cd 538.IP \[bu]
836efc18
MK
539The second return from
540.BR poll ()
541also indicated
1ae6b2c7 542.B POLLIN
836efc18
MK
543and
544.BR POLLHUP ;
545the program then consumed the last of the available input.
cdede5cd 546.IP \[bu]
836efc18
MK
547On the final return,
548.BR poll ()
549indicated only
1ae6b2c7 550.B POLLHUP
836efc18
MK
551on the FIFO,
552at which point the file descriptor was closed and the program terminated.
553.\"
554.SS Program source
555\&
33857069 556.\" SRC BEGIN (poll_input.c)
30dd9882 557.EX
78391fde 558/* poll_input.c
fe5dba13 559\&
78391fde
MK
560 Licensed under GNU General Public License v2 or later.
561*/
836efc18 562#include <fcntl.h>
47b94bbd 563#include <poll.h>
836efc18
MK
564#include <stdio.h>
565#include <stdlib.h>
566#include <unistd.h>
fe5dba13 567\&
836efc18
MK
568#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
569 } while (0)
fe5dba13 570\&
836efc18
MK
571int
572main(int argc, char *argv[])
573{
b42296e4 574 int ready;
041a155c 575 char buf[10];
b42296e4 576 nfds_t num_open_fds, nfds;
041a155c
AC
577 ssize_t s;
578 struct pollfd *pfds;
fe5dba13 579\&
836efc18
MK
580 if (argc < 2) {
581 fprintf(stderr, "Usage: %s file...\en", argv[0]);
582 exit(EXIT_FAILURE);
583 }
fe5dba13 584\&
836efc18
MK
585 num_open_fds = nfds = argc \- 1;
586 pfds = calloc(nfds, sizeof(struct pollfd));
587 if (pfds == NULL)
588 errExit("malloc");
fe5dba13 589\&
b957f81f 590 /* Open each file on command line, and add it to \[aq]pfds\[aq] array. */
fe5dba13 591\&
b42296e4 592 for (nfds_t j = 0; j < nfds; j++) {
836efc18
MK
593 pfds[j].fd = open(argv[j + 1], O_RDONLY);
594 if (pfds[j].fd == \-1)
595 errExit("open");
fe5dba13 596\&
836efc18 597 printf("Opened \e"%s\e" on fd %d\en", argv[j + 1], pfds[j].fd);
fe5dba13 598\&
836efc18
MK
599 pfds[j].events = POLLIN;
600 }
fe5dba13 601\&
836efc18 602 /* Keep calling poll() as long as at least one file descriptor is
46b20ca1 603 open. */
fe5dba13 604\&
836efc18 605 while (num_open_fds > 0) {
836efc18
MK
606 printf("About to poll()\en");
607 ready = poll(pfds, nfds, \-1);
608 if (ready == \-1)
609 errExit("poll");
fe5dba13 610\&
836efc18 611 printf("Ready: %d\en", ready);
fe5dba13 612\&
46b20ca1 613 /* Deal with array returned by poll(). */
fe5dba13 614\&
b42296e4 615 for (nfds_t j = 0; j < nfds; j++) {
836efc18
MK
616 if (pfds[j].revents != 0) {
617 printf(" fd=%d; events: %s%s%s\en", pfds[j].fd,
4687ab0e
AC
618 (pfds[j].revents & POLLIN) ? "POLLIN " : "",
619 (pfds[j].revents & POLLHUP) ? "POLLHUP " : "",
620 (pfds[j].revents & POLLERR) ? "POLLERR " : "");
fe5dba13 621\&
836efc18 622 if (pfds[j].revents & POLLIN) {
041a155c 623 s = read(pfds[j].fd, buf, sizeof(buf));
836efc18
MK
624 if (s == \-1)
625 errExit("read");
626 printf(" read %zd bytes: %.*s\en",
4687ab0e 627 s, (int) s, buf);
836efc18
MK
628 } else { /* POLLERR | POLLHUP */
629 printf(" closing fd %d\en", pfds[j].fd);
630 if (close(pfds[j].fd) == \-1)
631 errExit("close");
632 num_open_fds\-\-;
633 }
634 }
635 }
636 }
fe5dba13 637\&
836efc18
MK
638 printf("All file descriptors closed; bye\en");
639 exit(EXIT_SUCCESS);
640}
30dd9882 641.EE
33857069 642.\" SRC END
47297adb 643.SH SEE ALSO
d806bc05 644.BR restart_syscall (2),
fea681da 645.BR select (2),
50e5322c 646.BR select_tut (2),
e97e048a 647.BR timespec (3),
9755c034 648.BR epoll (7),
1d7c4d16 649.BR time (7)