]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/poll.2
prctl.2: Document the hwpoison prctls in 2.6.32
[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.\"
d8dfbff9 31.TH POLL 2 2009-09-15 "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
22ca838f 40.B #define _GNU_SOURCE
23cb3cb5 41.B #include <poll.h>
22ca838f 42.sp
e755a587
MK
43.BI "int ppoll(struct pollfd *" fds ", nfds_t " nfds ", "
44.BI " const struct timespec *" timeout ", const sigset_t *" sigmask );
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;
23cb3cb5
MK
126pseudo-terminal master in packet mode has seen state change in slave).
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
MK
135.B _GNU_SOURCE
136feature test macro must be defined in order to obtain this definition.
137.TP
138.B POLLERR
139Error condition (output only).
140.TP
141.B POLLHUP
142Hang up (output only).
143.TP
144.B POLLNVAL
c13182ef 145Invalid request:
23cb3cb5
MK
146.I fd
147not open (output only).
148.RE
149.PP
c13182ef 150When compiling with
23cb3cb5 151.B _XOPEN_SOURCE
c13182ef 152defined, one also has the following,
23cb3cb5
MK
153which convey no further information beyond the bits listed above:
154.RS
155.TP
156.B POLLRDNORM
157Equivalent to
158.BR POLLIN .
159.TP
160.B POLLRDBAND
161Priority band data can be read (generally unused on Linux).
162.\" POLLRDBAND is used in the DECnet protocol.
163.TP
164.B POLLWRNORM
165Equivalent to
166.BR POLLOUT .
167.TP
168.B POLLWRBAND
169Priority data may be written.
170.RE
171.PP
c13182ef 172Linux also knows about, but does not use
23cb3cb5 173.BR POLLMSG .
e755a587 174.SS ppoll()
c13182ef 175The relationship between
e755a587 176.BR poll ()
c13182ef
MK
177and
178.BR ppoll ()
e755a587 179is analogous to the relationship between
0bfa087b 180.BR select (2)
e755a587 181and
0bfa087b 182.BR pselect (2):
e755a587 183like
0bfa087b 184.BR pselect (2),
e755a587
MK
185.BR ppoll ()
186allows an application to safely wait until either a file descriptor
187becomes ready or until a signal is caught.
188.PP
c13182ef 189Other than the difference in the
e755a587 190.I timeout
c13182ef 191argument, the following
e755a587
MK
192.BR ppoll ()
193call:
194.nf
195
196 ready = ppoll(&fds, nfds, timeout, &sigmask);
197
198.fi
199is equivalent to
200.I atomically
201executing the following calls:
202.nf
203
204 sigset_t origmask;
205
206 sigprocmask(SIG_SETMASK, &sigmask, &origmask);
00877d8f 207 ready = poll(&fds, nfds, timeout);
e755a587
MK
208 sigprocmask(SIG_SETMASK, &origmask, NULL);
209.fi
210.PP
211See the description of
212.BR pselect (2)
213for an explanation of why
214.BR ppoll ()
215is necessary.
216
3ee89512
MK
217If the
218.I sigmask
219argument is specified as NULL, then
220no signal mask manipulation is performed
221(and thus
222.BR ppoll ()
223differs from
224.BR poll ()
225only in the precision of the
226.I timeout
227argument).
228
e755a587
MK
229The
230.I timeout
c13182ef 231argument specifies an upper limit on the amount of time that
e755a587
MK
232.BR ppoll ()
233will block.
234This argument is a pointer to a structure of the following form:
088a639b 235.in +4n
e755a587
MK
236.nf
237
238struct timespec {
239 long tv_sec; /* seconds */
240 long tv_nsec; /* nanoseconds */
241};
242.fi
a08ea57c 243.in
e755a587
MK
244
245If
246.I timeout
c13182ef 247is specified as NULL, then
e755a587
MK
248.BR ppoll ()
249can block indefinitely.
fea681da 250.SH "RETURN VALUE"
96296ef0 251On success, a positive number is returned; this is
c7094399 252the number of structures which have nonzero
fea681da
MK
253.I revents
254fields (in other words, those descriptors with events or errors reported).
255A value of 0 indicates that the call timed out and no file
c13182ef
MK
256descriptors were ready.
257On error, \-1 is returned, and
fea681da
MK
258.I errno
259is set appropriately.
260.SH ERRORS
261.TP
fea681da
MK
262.B EFAULT
263The array given as argument was not contained in the calling program's
264address space.
265.TP
266.B EINTR
01538d0d
MK
267A signal occurred before any requested event; see
268.BR signal (7).
fea681da
MK
269.TP
270.B EINVAL
271The
272.I nfds
682edefb
MK
273value exceeds the
274.B RLIMIT_NOFILE
275value.
fea681da
MK
276.TP
277.B ENOMEM
278There was no space to allocate file descriptor tables.
e755a587 279.SH VERSIONS
b5cc2ffb 280The
c13182ef 281.BR poll ()
b5cc2ffb 282system call was introduced in Linux 2.1.23.
c13182ef 283The
4d52e8f8 284.BR poll ()
b5cc2ffb 285library call was introduced in libc 5.4.28
0bfa087b 286(and provides emulation using select(2) if your kernel does not
c13182ef 287have a
1e321034
MK
288.BR poll ()
289system call).
e755a587
MK
290
291The
292.BR ppoll ()
293system call was added to Linux in kernel 2.6.16.
294The
295.BR ppoll ()
296library call was added in glibc 2.4.
a1d5f77c
MK
297.SH "CONFORMING TO"
298.BR poll ()
299conforms to POSIX.1-2001.
300.BR ppoll ()
8382f16d 301is Linux-specific.
a1d5f77c 302.\" NetBSD 3.0 has a pollts() which is like Linux ppoll().
e755a587 303.SH NOTES
c8f2dd47 304Some implementations define the nonstandard constant
c1164764
MK
305.B INFTIM
306with the value \-1 for use as a
307.IR timeout .
308This constant is not provided in glibc.
4fb31341
MK
309.SS "Linux Notes"
310The Linux
311.BR ppoll ()
312system call modifies its
313.I timeout
314argument.
d9bfdb9c 315However, the glibc wrapper function hides this behavior
4fb31341
MK
316by using a local variable for the timeout argument that
317is passed to the system call.
318Thus, the glibc
319.BR ppoll ()
320function does not modify its
321.I timeout
322argument.
a1d5f77c
MK
323.SH BUGS
324See the discussion of spurious readiness notifications under the
325BUGS section of
326.BR select (2).
fea681da
MK
327.SH "SEE ALSO"
328.BR select (2),
50e5322c 329.BR select_tut (2),
1d7c4d16
MK
330.BR feature_test_macros (7),
331.BR time (7)