]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/poll.2
poll.2: ffix
[thirdparty/man-pages.git] / man2 / poll.2
CommitLineData
fea681da
MK
1.\" Hey Emacs! This file is -*- nroff -*- source.
2.\"
3.\" Copyright (C) 1997 Andries Brouwer (aeb@cwi.nl)
c11b1abf 4.\" and Copyright (C) 2006, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da
MK
5.\"
6.\" Permission is granted to make and distribute verbatim copies of this
7.\" manual provided the copyright notice and this permission notice are
8.\" preserved on all copies.
9.\"
10.\" Permission is granted to copy and distribute modified versions of this
11.\" manual under the conditions for verbatim copying, provided that the
12.\" entire resulting derived work is distributed under the terms of a
13.\" permission notice identical to this one.
c13182ef 14.\"
fea681da
MK
15.\" Since the Linux kernel and libraries are constantly changing, this
16.\" manual page may be incorrect or out-of-date. The author(s) assume no
17.\" responsibility for errors or omissions, or for damages resulting from
18.\" the use of the information contained herein. The author(s) may not
19.\" have taken the same level of care in the production of this manual,
20.\" which is licensed free of charge, as they might when working
21.\" professionally.
c13182ef 22.\"
fea681da
MK
23.\" Formatted or processed versions of this manual, if unaccompanied by
24.\" the source, must acknowledge the copyright and authors of this work.
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.\"
50831f9b 31.TH POLL 2 2010-09-20 "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.
23cb3cb5 75
fea681da
MK
76The field
77.I events
10f5f294 78is an input parameter, a bit mask specifying the events the application
fea681da 79is interested in.
23cb3cb5 80
fea681da
MK
81The field
82.I revents
83is an output parameter, filled by the kernel with the events that
e755a587
MK
84actually occurred.
85The bits returned in
86.I revents
c13182ef 87can include any of those specified in
e755a587
MK
88.IR events ,
89or one of the values
90.BR POLLERR ,
91.BR POLLHUP ,
fea681da
MK
92or
93.BR POLLNVAL .
94(These three bits are meaningless in the
95.I events
96field, and will be set in the
97.I revents
98field whenever the corresponding condition is true.)
23cb3cb5 99
fea681da 100If none of the events requested (and no error) has occurred for any
e755a587
MK
101of the file descriptors, then
102.BR poll ()
103blocks until one of the events occurs.
96296ef0 104
c13182ef 105The
e755a587
MK
106.I timeout
107argument specifies an upper limit on the time for which
108.BR poll ()
109will block, in milliseconds.
110Specifying a negative value in
111.I timeout
112means an infinite timeout.
23cb3cb5
MK
113
114The bits that may be set/returned in
115.I events
116and
117.I revents
c84371c6 118are defined in \fI<poll.h>\fP:
23cb3cb5
MK
119.RS
120.TP
121.B POLLIN
122There is data to read.
123.TP
124.B POLLPRI
c13182ef 125There is urgent data to read (e.g., out-of-band data on TCP socket;
b218b023 126pseudoterminal master in packet mode has seen state change in slave).
23cb3cb5
MK
127.TP
128.B POLLOUT
129Writing now will not block.
130.TP
131.BR POLLRDHUP " (since Linux 2.6.17)"
c13182ef 132Stream socket peer closed connection,
23cb3cb5 133or shut down writing half of connection.
c13182ef 134The
23cb3cb5 135.B _GNU_SOURCE
e417acb0
MK
136feature test macro must be defined
137(before including
138.I any
139header files)
140in order to obtain this definition.
23cb3cb5
MK
141.TP
142.B POLLERR
143Error condition (output only).
144.TP
145.B POLLHUP
146Hang up (output only).
147.TP
148.B POLLNVAL
c13182ef 149Invalid request:
23cb3cb5
MK
150.I fd
151not open (output only).
152.RE
153.PP
c13182ef 154When compiling with
23cb3cb5 155.B _XOPEN_SOURCE
c13182ef 156defined, one also has the following,
23cb3cb5
MK
157which convey no further information beyond the bits listed above:
158.RS
159.TP
160.B POLLRDNORM
161Equivalent to
162.BR POLLIN .
163.TP
164.B POLLRDBAND
165Priority band data can be read (generally unused on Linux).
166.\" POLLRDBAND is used in the DECnet protocol.
167.TP
168.B POLLWRNORM
169Equivalent to
170.BR POLLOUT .
171.TP
172.B POLLWRBAND
173Priority data may be written.
174.RE
175.PP
c13182ef 176Linux also knows about, but does not use
23cb3cb5 177.BR POLLMSG .
e755a587 178.SS ppoll()
c13182ef 179The relationship between
e755a587 180.BR poll ()
c13182ef
MK
181and
182.BR ppoll ()
e755a587 183is analogous to the relationship between
0bfa087b 184.BR select (2)
e755a587 185and
0bfa087b 186.BR pselect (2):
e755a587 187like
0bfa087b 188.BR pselect (2),
e755a587
MK
189.BR ppoll ()
190allows an application to safely wait until either a file descriptor
191becomes ready or until a signal is caught.
192.PP
b8c599cb 193Other than the difference in the precision of the
39c05c26
YK
194.I timeout
195argument, the following
e755a587
MK
196.BR ppoll ()
197call:
198.nf
199
b8c599cb 200 ready = ppoll(&fds, nfds, timeout_ts, &sigmask);
e755a587
MK
201
202.fi
203is equivalent to
204.I atomically
205executing the following calls:
206.nf
207
208 sigset_t origmask;
b8c599cb 209 int timeout;
e755a587 210
b8c599cb
MK
211 timeout = (timeout_ts == NULL) ? -1 :
212 (timeout_ts.tv_sec * 1000 + timeout_ts.tv_nsec / 1000000);
e755a587 213 sigprocmask(SIG_SETMASK, &sigmask, &origmask);
00877d8f 214 ready = poll(&fds, nfds, timeout);
e755a587
MK
215 sigprocmask(SIG_SETMASK, &origmask, NULL);
216.fi
217.PP
218See the description of
219.BR pselect (2)
220for an explanation of why
221.BR ppoll ()
222is necessary.
223
3ee89512
MK
224If the
225.I sigmask
226argument is specified as NULL, then
227no signal mask manipulation is performed
228(and thus
229.BR ppoll ()
230differs from
231.BR poll ()
b8c599cb 232only in the precision of the timeout argument).
3ee89512 233
e755a587 234The
b8c599cb 235.I timeout_ts
c13182ef 236argument specifies an upper limit on the amount of time that
e755a587
MK
237.BR ppoll ()
238will block.
239This argument is a pointer to a structure of the following form:
088a639b 240.in +4n
e755a587
MK
241.nf
242
243struct timespec {
244 long tv_sec; /* seconds */
245 long tv_nsec; /* nanoseconds */
246};
247.fi
a08ea57c 248.in
e755a587
MK
249
250If
b8c599cb 251.I timeout_ts
c13182ef 252is specified as NULL, then
e755a587
MK
253.BR ppoll ()
254can block indefinitely.
fea681da 255.SH "RETURN VALUE"
96296ef0 256On success, a positive number is returned; this is
c7094399 257the number of structures which have nonzero
fea681da
MK
258.I revents
259fields (in other words, those descriptors with events or errors reported).
260A value of 0 indicates that the call timed out and no file
c13182ef
MK
261descriptors were ready.
262On error, \-1 is returned, and
fea681da
MK
263.I errno
264is set appropriately.
265.SH ERRORS
266.TP
fea681da
MK
267.B EFAULT
268The array given as argument was not contained in the calling program's
269address space.
270.TP
271.B EINTR
01538d0d
MK
272A signal occurred before any requested event; see
273.BR signal (7).
fea681da
MK
274.TP
275.B EINVAL
276The
277.I nfds
682edefb
MK
278value exceeds the
279.B RLIMIT_NOFILE
280value.
fea681da
MK
281.TP
282.B ENOMEM
283There was no space to allocate file descriptor tables.
e755a587 284.SH VERSIONS
b5cc2ffb 285The
c13182ef 286.BR poll ()
b5cc2ffb 287system call was introduced in Linux 2.1.23.
c13182ef 288The
4d52e8f8 289.BR poll ()
b5cc2ffb 290library call was introduced in libc 5.4.28
cc12d0c5
YK
291(and provides emulation using
292.BR select (2)
293if your kernel does not
c13182ef 294have a
1e321034
MK
295.BR poll ()
296system call).
e755a587
MK
297
298The
299.BR ppoll ()
300system call was added to Linux in kernel 2.6.16.
301The
302.BR ppoll ()
303library call was added in glibc 2.4.
a1d5f77c
MK
304.SH "CONFORMING TO"
305.BR poll ()
306conforms to POSIX.1-2001.
307.BR ppoll ()
8382f16d 308is Linux-specific.
a1d5f77c 309.\" NetBSD 3.0 has a pollts() which is like Linux ppoll().
e755a587 310.SH NOTES
c8f2dd47 311Some implementations define the nonstandard constant
c1164764
MK
312.B INFTIM
313with the value \-1 for use as a
b8c599cb
MK
314.IR timeout
315for
ba2e3e9a 316.BR poll ().
c1164764 317This constant is not provided in glibc.
4fb31341
MK
318.SS "Linux Notes"
319The Linux
320.BR ppoll ()
321system call modifies its
b8c599cb 322.I timeout_ts
4fb31341 323argument.
d9bfdb9c 324However, the glibc wrapper function hides this behavior
4fb31341
MK
325by using a local variable for the timeout argument that
326is passed to the system call.
327Thus, the glibc
328.BR ppoll ()
329function does not modify its
b8c599cb 330.I timeout_ts
4fb31341 331argument.
a1d5f77c
MK
332.SH BUGS
333See the discussion of spurious readiness notifications under the
334BUGS section of
335.BR select (2).
fea681da
MK
336.SH "SEE ALSO"
337.BR select (2),
50e5322c 338.BR select_tut (2),
1d7c4d16 339.BR time (7)