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