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