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