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