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