]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/poll.2
pldd.1, bpf.2, chdir.2, clone.2, fanotify_init.2, fanotify_mark.2, intro.2, ipc.2...
[thirdparty/man-pages.git] / man2 / poll.2
1 .\" Copyright (C) 1997 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright (C) 2006, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Additions from Richard Gooch <rgooch@atnf.CSIRO.AU> and aeb, 971207
27 .\" 2006-03-13, mtk, Added ppoll() + various other rewordings
28 .\" 2006-07-01, mtk, Added POLLRDHUP + various other wording and
29 .\" formatting changes.
30 .\"
31 .TH POLL 2 2019-08-02 "Linux" "Linux Programmer's Manual"
32 .SH NAME
33 poll, ppoll \- wait for some event on a file descriptor
34 .SH SYNOPSIS
35 .nf
36 .B #include <poll.h>
37 .PP
38 .BI "int poll(struct pollfd *" fds ", nfds_t " nfds ", int " timeout );
39
40 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
41 .B #include <signal.h>
42 .B #include <poll.h>
43 .PP
44 .BI "int ppoll(struct pollfd *" fds ", nfds_t " nfds ", "
45 .BI " const struct timespec *" tmo_p ", const sigset_t *" sigmask );
46 .fi
47 .SH DESCRIPTION
48 .BR poll ()
49 performs a similar task to
50 .BR select (2):
51 it waits for one of a set of file descriptors to become ready
52 to perform I/O.
53 .PP
54 The set of file descriptors to be monitored is specified in the
55 .I fds
56 argument, which is an array of structures of the following form:
57 .PP
58 .in +4n
59 .EX
60 struct pollfd {
61 int fd; /* file descriptor */
62 short events; /* requested events */
63 short revents; /* returned events */
64 };
65 .EE
66 .in
67 .PP
68 The caller should specify the number of items in the
69 .I fds
70 array in
71 .IR nfds .
72 .PP
73 The field
74 .I fd
75 contains a file descriptor for an open file.
76 If this field is negative, then the corresponding
77 .I events
78 field is ignored and the
79 .I revents
80 field returns zero.
81 (This provides an easy way of ignoring a
82 file descriptor for a single
83 .BR poll ()
84 call: simply negate the
85 .I fd
86 field.
87 Note, however, that this technique can't be used to ignore file descriptor 0.)
88 .PP
89 The field
90 .I events
91 is an input parameter, a bit mask specifying the events the application
92 is interested in for the file descriptor
93 .IR fd .
94 This field may be specified as zero,
95 in which case the only events that can be returned in
96 .I revents
97 are
98 .BR POLLHUP ,
99 .BR POLLERR ,
100 and
101 .B POLLNVAL
102 (see below).
103 .PP
104 The field
105 .I revents
106 is an output parameter, filled by the kernel with the events that
107 actually occurred.
108 The bits returned in
109 .I revents
110 can include any of those specified in
111 .IR events ,
112 or one of the values
113 .BR POLLERR ,
114 .BR POLLHUP ,
115 or
116 .BR POLLNVAL .
117 (These three bits are meaningless in the
118 .I events
119 field, and will be set in the
120 .I revents
121 field whenever the corresponding condition is true.)
122 .PP
123 If none of the events requested (and no error) has occurred for any
124 of the file descriptors, then
125 .BR poll ()
126 blocks until one of the events occurs.
127 .PP
128 The
129 .I timeout
130 argument specifies the number of milliseconds that
131 .BR poll ()
132 should block waiting for a file descriptor to become ready.
133 The call will block until either:
134 .IP * 3
135 a file descriptor becomes ready;
136 .IP *
137 the call is interrupted by a signal handler; or
138 .IP *
139 the timeout expires.
140 .PP
141 Note that the
142 .I timeout
143 interval will be rounded up to the system clock granularity,
144 and kernel scheduling delays mean that the blocking interval
145 may overrun by a small amount.
146 Specifying a negative value in
147 .I timeout
148 means an infinite timeout.
149 Specifying a
150 .I timeout
151 of zero causes
152 .BR poll ()
153 to return immediately, even if no file descriptors are ready.
154 .PP
155 The bits that may be set/returned in
156 .I events
157 and
158 .I revents
159 are defined in \fI<poll.h>\fP:
160 .TP
161 .B POLLIN
162 There is data to read.
163 .TP
164 .B POLLPRI
165 There is some exceptional condition on the file descriptor.
166 Possibilities include:
167 .RS
168 .IP * 3
169 There is out-of-band data on a TCP socket (see
170 .BR tcp (7)).
171 .IP *
172 A pseudoterminal master in packet mode has seen a state change on the slave
173 (see
174 .BR ioctl_tty (2)).
175 .IP *
176 A
177 .I cgroup.events
178 file has been modified (see
179 .BR cgroups (7)).
180 .RE
181 .TP
182 .B POLLOUT
183 Writing is now possible, though a write larger that the available space
184 in a socket or pipe will still block (unless
185 .B O_NONBLOCK
186 is set).
187 .TP
188 .BR POLLRDHUP " (since Linux 2.6.17)"
189 Stream socket peer closed connection,
190 or shut down writing half of connection.
191 The
192 .B _GNU_SOURCE
193 feature test macro must be defined
194 (before including
195 .I any
196 header files)
197 in order to obtain this definition.
198 .TP
199 .B POLLERR
200 Error condition (only returned in
201 .IR revents ;
202 ignored in
203 .IR events ).
204 This bit is also set for a file descriptor referring
205 to the write end of a pipe when the read end has been closed.
206 .TP
207 .B POLLHUP
208 Hang up (only returned in
209 .IR revents ;
210 ignored in
211 .IR events ).
212 Note that when reading from a channel such as a pipe or a stream socket,
213 this event merely indicates that the peer closed its end of the channel.
214 Subsequent reads from the channel will return 0 (end of file)
215 only after all outstanding data in the channel has been consumed.
216 .TP
217 .B POLLNVAL
218 Invalid request:
219 .I fd
220 not open (only returned in
221 .IR revents ;
222 ignored in
223 .IR events ).
224 .PP
225 When compiling with
226 .B _XOPEN_SOURCE
227 defined, one also has the following,
228 which convey no further information beyond the bits listed above:
229 .TP
230 .B POLLRDNORM
231 Equivalent to
232 .BR POLLIN .
233 .TP
234 .B POLLRDBAND
235 Priority band data can be read (generally unused on Linux).
236 .\" POLLRDBAND is used in the DECnet protocol.
237 .TP
238 .B POLLWRNORM
239 Equivalent to
240 .BR POLLOUT .
241 .TP
242 .B POLLWRBAND
243 Priority data may be written.
244 .PP
245 Linux also knows about, but does not use
246 .BR POLLMSG .
247 .SS ppoll()
248 The relationship between
249 .BR poll ()
250 and
251 .BR ppoll ()
252 is analogous to the relationship between
253 .BR select (2)
254 and
255 .BR pselect (2):
256 like
257 .BR pselect (2),
258 .BR ppoll ()
259 allows an application to safely wait until either a file descriptor
260 becomes ready or until a signal is caught.
261 .PP
262 Other than the difference in the precision of the
263 .I timeout
264 argument, the following
265 .BR ppoll ()
266 call:
267 .PP
268 .in +4n
269 .EX
270 ready = ppoll(&fds, nfds, tmo_p, &sigmask);
271 .EE
272 .in
273 .PP
274 is nearly equivalent to
275 .I atomically
276 executing the following calls:
277 .PP
278 .in +4n
279 .EX
280 sigset_t origmask;
281 int timeout;
282
283 timeout = (tmo_p == NULL) ? \-1 :
284 (tmo_p\->tv_sec * 1000 + tmo_p\->tv_nsec / 1000000);
285 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
286 ready = poll(&fds, nfds, timeout);
287 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
288 .EE
289 .in
290 .PP
291 The above code segment is described as
292 .I nearly
293 equivalent because whereas a negative
294 .I timeout
295 value for
296 .BR poll ()
297 is interpreted as an infinite timeout, a negative value expressed in
298 .IR *tmo_p
299 results in an error from
300 .BR ppoll ().
301 .PP
302 See the description of
303 .BR pselect (2)
304 for an explanation of why
305 .BR ppoll ()
306 is necessary.
307 .PP
308 If the
309 .I sigmask
310 argument is specified as NULL, then
311 no signal mask manipulation is performed
312 (and thus
313 .BR ppoll ()
314 differs from
315 .BR poll ()
316 only in the precision of the
317 .I timeout
318 argument).
319 .PP
320 The
321 .I tmo_p
322 argument specifies an upper limit on the amount of time that
323 .BR ppoll ()
324 will block.
325 This argument is a pointer to a structure of the following form:
326 .PP
327 .in +4n
328 .EX
329 struct timespec {
330 long tv_sec; /* seconds */
331 long tv_nsec; /* nanoseconds */
332 };
333 .EE
334 .in
335 .PP
336 If
337 .I tmo_p
338 is specified as NULL, then
339 .BR ppoll ()
340 can block indefinitely.
341 .SH RETURN VALUE
342 On success, a positive number is returned; this is
343 the number of structures which have nonzero
344 .I revents
345 fields (in other words, those descriptors with events or errors reported).
346 A value of 0 indicates that the call timed out and no file
347 descriptors were ready.
348 On error, \-1 is returned, and
349 .I errno
350 is set appropriately.
351 .SH ERRORS
352 .TP
353 .B EFAULT
354 The array given as argument was not contained in the calling program's
355 address space.
356 .TP
357 .B EINTR
358 A signal occurred before any requested event; see
359 .BR signal (7).
360 .TP
361 .B EINVAL
362 The
363 .I nfds
364 value exceeds the
365 .B RLIMIT_NOFILE
366 value.
367 .TP
368 .B EINVAL
369 .RB ( ppoll ())
370 The timeout value expressed in
371 .IR *ip
372 is invalid (negative).
373 .TP
374 .B ENOMEM
375 There was no space to allocate file descriptor tables.
376 .SH VERSIONS
377 The
378 .BR poll ()
379 system call was introduced in Linux 2.1.23.
380 On older kernels that lack this system call,
381 .\" library call was introduced in libc 5.4.28
382 the glibc (and the old Linux libc)
383 .BR poll ()
384 wrapper function provides emulation using
385 .BR select (2).
386 .PP
387 The
388 .BR ppoll ()
389 system call was added to Linux in kernel 2.6.16.
390 The
391 .BR ppoll ()
392 library call was added in glibc 2.4.
393 .SH CONFORMING TO
394 .BR poll ()
395 conforms to POSIX.1-2001 and POSIX.1-2008.
396 .BR ppoll ()
397 is Linux-specific.
398 .\" NetBSD 3.0 has a pollts() which is like Linux ppoll().
399 .SH NOTES
400 The operation of
401 .BR poll ()
402 and
403 .BR ppoll ()
404 is not affected by the
405 .BR O_NONBLOCK
406 flag.
407 .PP
408 On some other UNIX systems,
409 .\" Darwin, according to a report by Jeremy Sequoia, relayed by Josh Triplett
410 .BR poll ()
411 can fail with the error
412 .B EAGAIN
413 if the system fails to allocate kernel-internal resources, rather than
414 .B ENOMEM
415 as Linux does.
416 POSIX permits this behavior.
417 Portable programs may wish to check for
418 .B EAGAIN
419 and loop, just as with
420 .BR EINTR .
421 .PP
422 Some implementations define the nonstandard constant
423 .B INFTIM
424 with the value \-1 for use as a
425 .IR timeout
426 for
427 .BR poll ().
428 This constant is not provided in glibc.
429 .PP
430 For a discussion of what may happen if a file descriptor being monitored by
431 .BR poll ()
432 is closed in another thread, see
433 .BR select (2).
434 .SS C library/kernel differences
435 The Linux
436 .BR ppoll ()
437 system call modifies its
438 .I tmo_p
439 argument.
440 However, the glibc wrapper function hides this behavior
441 by using a local variable for the timeout argument that
442 is passed to the system call.
443 Thus, the glibc
444 .BR ppoll ()
445 function does not modify its
446 .I tmo_p
447 argument.
448 .PP
449 The raw
450 .BR ppoll ()
451 system call has a fifth argument,
452 .IR "size_t sigsetsize" ,
453 which specifies the size in bytes of the
454 .IR sigmask
455 argument.
456 The glibc
457 .BR ppoll ()
458 wrapper function specifies this argument as a fixed value
459 (equal to
460 .IR sizeof(kernel_sigset_t) ).
461 See
462 .BR sigprocmask (2)
463 for a discussion on the differences between the kernel and the libc
464 notion of the sigset.
465 .SH BUGS
466 See the discussion of spurious readiness notifications under the
467 BUGS section of
468 .BR select (2).
469 .SH SEE ALSO
470 .BR restart_syscall (2),
471 .BR select (2),
472 .BR select_tut (2),
473 .BR epoll (7),
474 .BR time (7)