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