]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/poll.2
mbind.2: Fix prototype for mbind(2)
[thirdparty/man-pages.git] / man2 / poll.2
CommitLineData
fea681da 1.\" Copyright (C) 1997 Andries Brouwer (aeb@cwi.nl)
c11b1abf 2.\" and Copyright (C) 2006, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
c13182ef 13.\"
fea681da
MK
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.
c13182ef 21.\"
fea681da
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
fea681da
MK
25.\"
26.\" Additions from Richard Gooch <rgooch@atnf.CSIRO.AU> and aeb, 971207
e755a587 27.\" 2006-03-13, mtk, Added ppoll() + various other rewordings
23cb3cb5
MK
28.\" 2006-07-01, mtk, Added POLLRDHUP + various other wording and
29.\" formatting changes.
4b9d4c1a 30.\"
40df3d00 31.TH POLL 2 2014-01-31 "Linux" "Linux Programmer's Manual"
fea681da 32.SH NAME
e755a587 33poll, ppoll \- wait for some event on a file descriptor
fea681da 34.SH SYNOPSIS
e755a587 35.nf
23cb3cb5 36.B #include <poll.h>
fea681da 37.sp
e755a587
MK
38.BI "int poll(struct pollfd *" fds ", nfds_t " nfds ", int " timeout );
39.sp
b80f966b 40.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
23cb3cb5 41.B #include <poll.h>
22ca838f 42.sp
e755a587 43.BI "int ppoll(struct pollfd *" fds ", nfds_t " nfds ", "
b8c599cb 44.BI " const struct timespec *" timeout_ts ", 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.
52
53The set of file descriptors to be monitored is specified in the
54.I fds
d8dfbff9 55argument, which is an array of structures of the following form:
088a639b 56.in +4n
fea681da 57.nf
96296ef0 58
a08ea57c
MK
59struct pollfd {
60 int fd; /* file descriptor */
61 short events; /* requested events */
62 short revents; /* returned events */
63};
a08ea57c 64.in
d8dfbff9
MK
65.fi
66.PP
67The caller should specify the number of items in the
68.I fds
69array in
70.IR nfds .
71
fea681da
MK
72The field
73.I fd
74contains a file descriptor for an open file.
096ca08a
MK
75If this field is negative, then the corresponding
76.I events
77field is ignored and the
78.I revents
79field returns zero.
80(This provides an easy way of ignoring a
81file descriptor for a single
82.BR poll ()
83call: simply negate the
84.I fd
85field.)
23cb3cb5 86
fea681da
MK
87The field
88.I events
10f5f294 89is an input parameter, a bit mask specifying the events the application
b857fda5
MK
90is interested in for the file descriptor
91.IR fd .
210cded6
MK
92This field may be specified as zero,
93in which case the only events that can be returned in
b857fda5 94.I revents
210cded6
MK
95are
96.BR POLLHUP ,
97.BR POLLERR ,
98and
99.B POLLNVAL
100(see below).
23cb3cb5 101
fea681da
MK
102The field
103.I revents
104is an output parameter, filled by the kernel with the events that
e755a587
MK
105actually occurred.
106The bits returned in
107.I revents
c13182ef 108can include any of those specified in
e755a587
MK
109.IR events ,
110or one of the values
111.BR POLLERR ,
112.BR POLLHUP ,
fea681da
MK
113or
114.BR POLLNVAL .
115(These three bits are meaningless in the
116.I events
117field, and will be set in the
118.I revents
119field whenever the corresponding condition is true.)
23cb3cb5 120
fea681da 121If none of the events requested (and no error) has occurred for any
e755a587
MK
122of the file descriptors, then
123.BR poll ()
124blocks until one of the events occurs.
96296ef0 125
c13182ef 126The
e755a587 127.I timeout
0d9101c4 128argument specifies the number of milliseconds that
e755a587 129.BR poll ()
0d9101c4 130should block waiting for a file descriptor to become ready.
40df3d00
MK
131The call will block until either:
132.IP * 3
133a file descriptor becomes ready;
134.IP *
135the call is interrupted by a signal handler; or
136.IP *
216b9e61 137the timeout expires.
40df3d00
MK
138.PP
139Note that the
140.I timeout
141interval will be rounded up to the system clock granularity,
19257d8f 142and kernel scheduling delays mean that the blocking interval
0d9101c4 143may overrun by a small amount.
e755a587
MK
144Specifying a negative value in
145.I timeout
146means an infinite timeout.
77e74cf1
MK
147Specifying a
148.I timeout
149of zero causes
150.BR poll ()
01d8b73f 151to return immediately, even if no file descriptors are ready.
23cb3cb5
MK
152
153The bits that may be set/returned in
154.I events
155and
156.I revents
c84371c6 157are defined in \fI<poll.h>\fP:
23cb3cb5
MK
158.RS
159.TP
160.B POLLIN
161There is data to read.
162.TP
163.B POLLPRI
c13182ef 164There is urgent data to read (e.g., out-of-band data on TCP socket;
b218b023 165pseudoterminal master in packet mode has seen state change in slave).
23cb3cb5
MK
166.TP
167.B POLLOUT
168Writing now will not block.
169.TP
170.BR POLLRDHUP " (since Linux 2.6.17)"
c13182ef 171Stream socket peer closed connection,
23cb3cb5 172or shut down writing half of connection.
c13182ef 173The
23cb3cb5 174.B _GNU_SOURCE
e417acb0
MK
175feature test macro must be defined
176(before including
177.I any
178header files)
179in order to obtain this definition.
23cb3cb5
MK
180.TP
181.B POLLERR
182Error condition (output only).
183.TP
184.B POLLHUP
185Hang up (output only).
186.TP
187.B POLLNVAL
c13182ef 188Invalid request:
23cb3cb5
MK
189.I fd
190not open (output only).
191.RE
192.PP
c13182ef 193When compiling with
23cb3cb5 194.B _XOPEN_SOURCE
c13182ef 195defined, one also has the following,
23cb3cb5
MK
196which convey no further information beyond the bits listed above:
197.RS
198.TP
199.B POLLRDNORM
200Equivalent to
201.BR POLLIN .
202.TP
203.B POLLRDBAND
204Priority band data can be read (generally unused on Linux).
205.\" POLLRDBAND is used in the DECnet protocol.
206.TP
207.B POLLWRNORM
208Equivalent to
209.BR POLLOUT .
210.TP
211.B POLLWRBAND
212Priority data may be written.
213.RE
214.PP
c13182ef 215Linux also knows about, but does not use
23cb3cb5 216.BR POLLMSG .
e755a587 217.SS ppoll()
c13182ef 218The relationship between
e755a587 219.BR poll ()
c13182ef
MK
220and
221.BR ppoll ()
e755a587 222is analogous to the relationship between
0bfa087b 223.BR select (2)
e755a587 224and
0bfa087b 225.BR pselect (2):
e755a587 226like
0bfa087b 227.BR pselect (2),
e755a587
MK
228.BR ppoll ()
229allows an application to safely wait until either a file descriptor
230becomes ready or until a signal is caught.
231.PP
b8c599cb 232Other than the difference in the precision of the
39c05c26
YK
233.I timeout
234argument, the following
e755a587
MK
235.BR ppoll ()
236call:
237.nf
238
b8c599cb 239 ready = ppoll(&fds, nfds, timeout_ts, &sigmask);
e755a587
MK
240
241.fi
242is equivalent to
243.I atomically
244executing the following calls:
245.nf
246
247 sigset_t origmask;
b8c599cb 248 int timeout;
e755a587 249
c3074d70 250 timeout = (timeout_ts == NULL) ? \-1 :
b8c599cb 251 (timeout_ts.tv_sec * 1000 + timeout_ts.tv_nsec / 1000000);
e755a587 252 sigprocmask(SIG_SETMASK, &sigmask, &origmask);
00877d8f 253 ready = poll(&fds, nfds, timeout);
e755a587
MK
254 sigprocmask(SIG_SETMASK, &origmask, NULL);
255.fi
256.PP
257See the description of
258.BR pselect (2)
259for an explanation of why
260.BR ppoll ()
261is necessary.
262
3ee89512
MK
263If the
264.I sigmask
265argument is specified as NULL, then
266no signal mask manipulation is performed
267(and thus
268.BR ppoll ()
269differs from
270.BR poll ()
79fd50e6
YK
271only in the precision of the
272.I timeout
273argument).
3ee89512 274
e755a587 275The
b8c599cb 276.I timeout_ts
c13182ef 277argument specifies an upper limit on the amount of time that
e755a587
MK
278.BR ppoll ()
279will block.
280This argument is a pointer to a structure of the following form:
088a639b 281.in +4n
e755a587
MK
282.nf
283
284struct timespec {
285 long tv_sec; /* seconds */
286 long tv_nsec; /* nanoseconds */
287};
288.fi
a08ea57c 289.in
e755a587
MK
290
291If
b8c599cb 292.I timeout_ts
c13182ef 293is specified as NULL, then
e755a587
MK
294.BR ppoll ()
295can block indefinitely.
47297adb 296.SH RETURN VALUE
96296ef0 297On success, a positive number is returned; this is
c7094399 298the number of structures which have nonzero
fea681da
MK
299.I revents
300fields (in other words, those descriptors with events or errors reported).
301A value of 0 indicates that the call timed out and no file
c13182ef
MK
302descriptors were ready.
303On error, \-1 is returned, and
fea681da
MK
304.I errno
305is set appropriately.
306.SH ERRORS
307.TP
fea681da
MK
308.B EFAULT
309The array given as argument was not contained in the calling program's
310address space.
311.TP
312.B EINTR
01538d0d
MK
313A signal occurred before any requested event; see
314.BR signal (7).
fea681da
MK
315.TP
316.B EINVAL
317The
318.I nfds
682edefb
MK
319value exceeds the
320.B RLIMIT_NOFILE
321value.
fea681da
MK
322.TP
323.B ENOMEM
324There was no space to allocate file descriptor tables.
e755a587 325.SH VERSIONS
b5cc2ffb 326The
c13182ef 327.BR poll ()
b5cc2ffb 328system call was introduced in Linux 2.1.23.
6f0b2c8c
MK
329On older kernels that lack this system call,
330.\" library call was introduced in libc 5.4.28
331the glibc (and the old Linux libc)
1e321034 332.BR poll ()
6f0b2c8c
MK
333wrapper function provides emulation using
334.BR select (2).
e755a587
MK
335
336The
337.BR ppoll ()
338system call was added to Linux in kernel 2.6.16.
339The
340.BR ppoll ()
341library call was added in glibc 2.4.
47297adb 342.SH CONFORMING TO
a1d5f77c
MK
343.BR poll ()
344conforms to POSIX.1-2001.
345.BR ppoll ()
8382f16d 346is Linux-specific.
a1d5f77c 347.\" NetBSD 3.0 has a pollts() which is like Linux ppoll().
e755a587 348.SH NOTES
c8f2dd47 349Some implementations define the nonstandard constant
c1164764
MK
350.B INFTIM
351with the value \-1 for use as a
b8c599cb
MK
352.IR timeout
353for
ba2e3e9a 354.BR poll ().
c1164764 355This constant is not provided in glibc.
f33050d6
MK
356
357For a discussion of what may happen if a file descriptor being monitored by
358.BR poll ()
359is closed in another thread, see
360.BR select (2).
c634028a 361.SS Linux notes
4fb31341
MK
362The Linux
363.BR ppoll ()
364system call modifies its
b8c599cb 365.I timeout_ts
4fb31341 366argument.
d9bfdb9c 367However, the glibc wrapper function hides this behavior
4fb31341
MK
368by using a local variable for the timeout argument that
369is passed to the system call.
370Thus, the glibc
371.BR ppoll ()
372function does not modify its
b8c599cb 373.I timeout_ts
4fb31341 374argument.
a1d5f77c
MK
375.SH BUGS
376See the discussion of spurious readiness notifications under the
377BUGS section of
378.BR select (2).
47297adb 379.SH SEE ALSO
d806bc05 380.BR restart_syscall (2),
fea681da 381.BR select (2),
50e5322c 382.BR select_tut (2),
1d7c4d16 383.BR time (7)