]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/poll.2
s/bitfield/bit field/
[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)
e755a587 4.\" and Copyright (C) 2006, Michael Kerrisk <mtk-manpages@gmx.net>
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.\"
24b3ed1d 31.\" FIXME . 2.6.17 has a definition for POLLREMOVE, but this
0c373d01 32.\" flag is not used in the code. Check later to see if it
a7b9318d 33.\" does get a use. 2.6.21 still shows no use.
0c373d01 34.\"
d9343c5c 35.TH POLL 2 2006-03-13 "Linux" "Linux Programmer's Manual"
fea681da 36.SH NAME
e755a587 37poll, ppoll \- wait for some event on a file descriptor
fea681da 38.SH SYNOPSIS
e755a587 39.nf
23cb3cb5 40.B #include <poll.h>
fea681da 41.sp
e755a587
MK
42.BI "int poll(struct pollfd *" fds ", nfds_t " nfds ", int " timeout );
43.sp
22ca838f 44.B #define _GNU_SOURCE
23cb3cb5 45.B #include <poll.h>
22ca838f 46.sp
e755a587
MK
47.BI "int ppoll(struct pollfd *" fds ", nfds_t " nfds ", "
48.BI " const struct timespec *" timeout ", const sigset_t *" sigmask );
49.fi
fea681da 50.SH DESCRIPTION
96296ef0 51.BR poll ()
e755a587
MK
52performs a similar task to
53.BR select (2):
54it waits for one of a set of file descriptors to become ready
55to perform I/O.
56
57The set of file descriptors to be monitored is specified in the
58.I fds
59argument, which is an array of
fea681da 60.I nfds
e755a587 61structures of the following form:
fea681da 62.nf
96296ef0
MK
63
64 struct pollfd {
e755a587 65 int fd; /* file descriptor */
96296ef0
MK
66 short events; /* requested events */
67 short revents; /* returned events */
68 };
69
fea681da 70.fi
fea681da
MK
71The field
72.I fd
73contains a file descriptor for an open file.
23cb3cb5 74
fea681da
MK
75The field
76.I events
77is an input parameter, a bitmask specifying the events the application
78is interested in.
23cb3cb5 79
fea681da
MK
80The field
81.I revents
82is an output parameter, filled by the kernel with the events that
e755a587
MK
83actually occurred.
84The bits returned in
85.I revents
c13182ef 86can include any of those specified in
e755a587
MK
87.IR events ,
88or one of the values
89.BR POLLERR ,
90.BR POLLHUP ,
fea681da
MK
91or
92.BR POLLNVAL .
93(These three bits are meaningless in the
94.I events
95field, and will be set in the
96.I revents
97field whenever the corresponding condition is true.)
23cb3cb5 98
fea681da 99If none of the events requested (and no error) has occurred for any
e755a587
MK
100of the file descriptors, then
101.BR poll ()
102blocks until one of the events occurs.
96296ef0 103
c13182ef 104The
e755a587
MK
105.I timeout
106argument specifies an upper limit on the time for which
107.BR poll ()
108will block, in milliseconds.
109Specifying a negative value in
110.I timeout
111means an infinite timeout.
23cb3cb5
MK
112
113The bits that may be set/returned in
114.I events
115and
116.I revents
c84371c6 117are defined in \fI<poll.h>\fP:
23cb3cb5
MK
118.RS
119.TP
120.B POLLIN
121There is data to read.
122.TP
123.B POLLPRI
c13182ef 124There is urgent data to read (e.g., out-of-band data on TCP socket;
23cb3cb5
MK
125pseudo-terminal master in packet mode has seen state change in slave).
126.TP
127.B POLLOUT
128Writing now will not block.
129.TP
130.BR POLLRDHUP " (since Linux 2.6.17)"
c13182ef 131Stream socket peer closed connection,
23cb3cb5 132or shut down writing half of connection.
c13182ef 133The
23cb3cb5
MK
134.B _GNU_SOURCE
135feature test macro must be defined in order to obtain this definition.
136.TP
137.B POLLERR
138Error condition (output only).
139.TP
140.B POLLHUP
141Hang up (output only).
142.TP
143.B POLLNVAL
c13182ef 144Invalid request:
23cb3cb5
MK
145.I fd
146not open (output only).
147.RE
148.PP
c13182ef 149When compiling with
23cb3cb5 150.B _XOPEN_SOURCE
c13182ef 151defined, one also has the following,
23cb3cb5
MK
152which convey no further information beyond the bits listed above:
153.RS
154.TP
155.B POLLRDNORM
156Equivalent to
157.BR POLLIN .
158.TP
159.B POLLRDBAND
160Priority band data can be read (generally unused on Linux).
161.\" POLLRDBAND is used in the DECnet protocol.
162.TP
163.B POLLWRNORM
164Equivalent to
165.BR POLLOUT .
166.TP
167.B POLLWRBAND
168Priority data may be written.
169.RE
170.PP
c13182ef 171Linux also knows about, but does not use
23cb3cb5 172.BR POLLMSG .
e755a587 173.SS ppoll()
c13182ef 174The relationship between
e755a587 175.BR poll ()
c13182ef
MK
176and
177.BR ppoll ()
e755a587 178is analogous to the relationship between
0bfa087b 179.BR select (2)
e755a587 180and
0bfa087b 181.BR pselect (2):
e755a587 182like
0bfa087b 183.BR pselect (2),
e755a587
MK
184.BR ppoll ()
185allows an application to safely wait until either a file descriptor
186becomes ready or until a signal is caught.
187.PP
c13182ef 188Other than the difference in the
e755a587 189.I timeout
c13182ef 190argument, the following
e755a587
MK
191.BR ppoll ()
192call:
193.nf
194
195 ready = ppoll(&fds, nfds, timeout, &sigmask);
196
197.fi
198is equivalent to
199.I atomically
200executing the following calls:
201.nf
202
203 sigset_t origmask;
204
205 sigprocmask(SIG_SETMASK, &sigmask, &origmask);
00877d8f 206 ready = poll(&fds, nfds, timeout);
e755a587
MK
207 sigprocmask(SIG_SETMASK, &origmask, NULL);
208.fi
209.PP
210See the description of
211.BR pselect (2)
212for an explanation of why
213.BR ppoll ()
214is necessary.
215
216The
217.I timeout
c13182ef 218argument specifies an upper limit on the amount of time that
e755a587
MK
219.BR ppoll ()
220will block.
221This argument is a pointer to a structure of the following form:
222.in +0.25i
223.nf
224
225struct timespec {
226 long tv_sec; /* seconds */
227 long tv_nsec; /* nanoseconds */
228};
229.fi
230.in -0.25i
231
232If
233.I timeout
c13182ef 234is specified as NULL, then
e755a587
MK
235.BR ppoll ()
236can block indefinitely.
fea681da 237.SH "RETURN VALUE"
96296ef0
MK
238On success, a positive number is returned; this is
239the number of structures which have non-zero
fea681da
MK
240.I revents
241fields (in other words, those descriptors with events or errors reported).
242A value of 0 indicates that the call timed out and no file
c13182ef
MK
243descriptors were ready.
244On error, \-1 is returned, and
fea681da
MK
245.I errno
246is set appropriately.
247.SH ERRORS
248.TP
249.B EBADF
250An invalid file descriptor was given in one of the sets.
251.TP
252.B EFAULT
253The array given as argument was not contained in the calling program's
254address space.
255.TP
256.B EINTR
257A signal occurred before any requested event.
258.TP
259.B EINVAL
260The
261.I nfds
682edefb
MK
262value exceeds the
263.B RLIMIT_NOFILE
264value.
fea681da
MK
265.TP
266.B ENOMEM
267There was no space to allocate file descriptor tables.
e755a587 268.SH VERSIONS
b5cc2ffb 269The
c13182ef 270.BR poll ()
b5cc2ffb 271system call was introduced in Linux 2.1.23.
c13182ef 272The
4d52e8f8 273.BR poll ()
b5cc2ffb 274library call was introduced in libc 5.4.28
0bfa087b 275(and provides emulation using select(2) if your kernel does not
c13182ef 276have a
1e321034
MK
277.BR poll ()
278system call).
e755a587
MK
279
280The
281.BR ppoll ()
282system call was added to Linux in kernel 2.6.16.
283The
284.BR ppoll ()
285library call was added in glibc 2.4.
a1d5f77c
MK
286.SH "CONFORMING TO"
287.BR poll ()
288conforms to POSIX.1-2001.
289.BR ppoll ()
290is Linux specific.
291.\" NetBSD 3.0 has a pollts() which is like Linux ppoll().
e755a587 292.SH NOTES
c1164764
MK
293Some implementations define the non-standard constant
294.B INFTIM
295with the value \-1 for use as a
296.IR timeout .
297This constant is not provided in glibc.
4fb31341
MK
298.SS "Linux Notes"
299The Linux
300.BR ppoll ()
301system call modifies its
302.I timeout
303argument.
d9bfdb9c 304However, the glibc wrapper function hides this behavior
4fb31341
MK
305by using a local variable for the timeout argument that
306is passed to the system call.
307Thus, the glibc
308.BR ppoll ()
309function does not modify its
310.I timeout
311argument.
a1d5f77c
MK
312.SH BUGS
313See the discussion of spurious readiness notifications under the
314BUGS section of
315.BR select (2).
fea681da
MK
316.SH "SEE ALSO"
317.BR select (2),
50e5322c 318.BR select_tut (2),
a8e7c990 319.BR feature_test_macros (7)