OSDN Git Service

81d119db265f6f24e620d198e49e56e049144f16
[linuxjm/LDP_man-pages.git] / original / man2 / poll.2
1 .\" Copyright (C) 1997 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright (C) 2006, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
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 2014-09-06 "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 .BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
41 .B #include <signal.h>
42 .B #include <poll.h>
43 .sp
44 .BI "int ppoll(struct pollfd *" fds ", nfds_t " nfds ", "
45 .BI "        const struct timespec *" timeout_ts ", const sigset_t *" sigmask );
46 .fi
47 .SH DESCRIPTION
48 .BR poll ()
49 performs a similar task to
50 .BR select (2):
51 it waits for one of a set of file descriptors to become ready
52 to perform I/O.
53
54 The set of file descriptors to be monitored is specified in the
55 .I fds
56 argument, which is an array of structures of the following form:
57 .in +4n
58 .nf
59
60 struct pollfd {
61     int   fd;         /* file descriptor */
62     short events;     /* requested events */
63     short revents;    /* returned events */
64 };
65 .in
66 .fi
67 .PP
68 The caller should specify the number of items in the
69 .I fds
70 array in
71 .IR nfds .
72
73 The field
74 .I fd
75 contains a file descriptor for an open file.
76 If this field is negative, then the corresponding
77 .I events
78 field is ignored and the
79 .I revents
80 field returns zero.
81 (This provides an easy way of ignoring a
82 file descriptor for a single
83 .BR poll ()
84 call: simply negate the
85 .I fd
86 field.
87 Note, however, that this technique can't be used to ignore file descriptor 0.)
88
89 The field
90 .I events
91 is an input parameter, a bit mask specifying the events the application
92 is interested in for the file descriptor
93 .IR fd .
94 This field may be specified as zero,
95 in which case the only events that can be returned in
96 .I revents
97 are
98 .BR POLLHUP ,
99 .BR POLLERR ,
100 and
101 .B POLLNVAL
102 (see below).
103
104 The field
105 .I revents
106 is an output parameter, filled by the kernel with the events that
107 actually occurred.
108 The bits returned in
109 .I revents
110 can include any of those specified in
111 .IR events ,
112 or one of the values
113 .BR POLLERR ,
114 .BR POLLHUP ,
115 or
116 .BR POLLNVAL .
117 (These three bits are meaningless in the
118 .I events
119 field, and will be set in the
120 .I revents
121 field whenever the corresponding condition is true.)
122
123 If none of the events requested (and no error) has occurred for any
124 of the file descriptors, then
125 .BR poll ()
126 blocks until one of the events occurs.
127
128 The
129 .I timeout
130 argument specifies the number of milliseconds that
131 .BR poll ()
132 should block waiting for a file descriptor to become ready.
133 The call will block until either:
134 .IP * 3
135 a file descriptor becomes ready;
136 .IP *
137 the call is interrupted by a signal handler; or
138 .IP *
139 the timeout expires.
140 .PP
141 Note that the
142 .I timeout
143 interval will be rounded up to the system clock granularity,
144 and kernel scheduling delays mean that the blocking interval
145 may overrun by a small amount.
146 Specifying a negative value in
147 .I timeout
148 means an infinite timeout.
149 Specifying a
150 .I timeout
151 of zero causes
152 .BR poll ()
153 to return immediately, even if no file descriptors are ready.
154
155 The bits that may be set/returned in
156 .I events
157 and
158 .I revents
159 are defined in \fI<poll.h>\fP:
160 .RS
161 .TP
162 .B POLLIN
163 There is data to read.
164 .TP
165 .B POLLPRI
166 There is urgent data to read (e.g., out-of-band data on TCP socket;
167 pseudoterminal master in packet mode has seen state change in slave).
168 .TP
169 .B POLLOUT
170 Writing is now possible, though a write larger that the available space
171 in a socket or pipe will still block (unless
172 .B O_NONBLOCK
173 is set).
174 .TP
175 .BR POLLRDHUP " (since Linux 2.6.17)"
176 Stream socket peer closed connection,
177 or shut down writing half of connection.
178 The
179 .B _GNU_SOURCE
180 feature test macro must be defined
181 (before including
182 .I any
183 header files)
184 in order to obtain this definition.
185 .TP
186 .B POLLERR
187 Error condition (output only).
188 .TP
189 .B POLLHUP
190 Hang up (output only).
191 .TP
192 .B POLLNVAL
193 Invalid request:
194 .I fd
195 not open (output only).
196 .RE
197 .PP
198 When compiling with
199 .B _XOPEN_SOURCE
200 defined, one also has the following,
201 which convey no further information beyond the bits listed above:
202 .RS
203 .TP
204 .B POLLRDNORM
205 Equivalent to
206 .BR POLLIN .
207 .TP
208 .B POLLRDBAND
209 Priority band data can be read (generally unused on Linux).
210 .\" POLLRDBAND is used in the DECnet protocol.
211 .TP
212 .B POLLWRNORM
213 Equivalent to
214 .BR POLLOUT .
215 .TP
216 .B POLLWRBAND
217 Priority data may be written.
218 .RE
219 .PP
220 Linux also knows about, but does not use
221 .BR POLLMSG .
222 .SS ppoll()
223 The relationship between
224 .BR poll ()
225 and
226 .BR ppoll ()
227 is analogous to the relationship between
228 .BR select (2)
229 and
230 .BR pselect (2):
231 like
232 .BR pselect (2),
233 .BR ppoll ()
234 allows an application to safely wait until either a file descriptor
235 becomes ready or until a signal is caught.
236 .PP
237 Other than the difference in the precision of the
238 .I timeout
239 argument, the following
240 .BR ppoll ()
241 call:
242 .nf
243
244     ready = ppoll(&fds, nfds, timeout_ts, &sigmask);
245
246 .fi
247 is equivalent to
248 .I atomically
249 executing the following calls:
250 .nf
251
252     sigset_t origmask;
253     int timeout;
254
255     timeout = (timeout_ts == NULL) ? \-1 :
256               (timeout_ts.tv_sec * 1000 + timeout_ts.tv_nsec / 1000000);
257     sigprocmask(SIG_SETMASK, &sigmask, &origmask);
258     ready = poll(&fds, nfds, timeout);
259     sigprocmask(SIG_SETMASK, &origmask, NULL);
260 .fi
261 .PP
262 See the description of
263 .BR pselect (2)
264 for an explanation of why
265 .BR ppoll ()
266 is necessary.
267
268 If the
269 .I sigmask
270 argument is specified as NULL, then
271 no signal mask manipulation is performed
272 (and thus
273 .BR ppoll ()
274 differs from
275 .BR poll ()
276 only in the precision of the
277 .I timeout
278 argument).
279
280 The
281 .I timeout_ts
282 argument specifies an upper limit on the amount of time that
283 .BR ppoll ()
284 will block.
285 This argument is a pointer to a structure of the following form:
286 .in +4n
287 .nf
288
289 struct timespec {
290     long    tv_sec;         /* seconds */
291     long    tv_nsec;        /* nanoseconds */
292 };
293 .fi
294 .in
295
296 If
297 .I timeout_ts
298 is specified as NULL, then
299 .BR ppoll ()
300 can block indefinitely.
301 .SH RETURN VALUE
302 On success, a positive number is returned; this is
303 the number of structures which have nonzero
304 .I revents
305 fields (in other words, those descriptors with events or errors reported).
306 A value of 0 indicates that the call timed out and no file
307 descriptors were ready.
308 On error, \-1 is returned, and
309 .I errno
310 is set appropriately.
311 .SH ERRORS
312 .TP
313 .B EFAULT
314 The array given as argument was not contained in the calling program's
315 address space.
316 .TP
317 .B EINTR
318 A signal occurred before any requested event; see
319 .BR signal (7).
320 .TP
321 .B EINVAL
322 The
323 .I nfds
324 value exceeds the
325 .B RLIMIT_NOFILE
326 value.
327 .TP
328 .B ENOMEM
329 There was no space to allocate file descriptor tables.
330 .SH VERSIONS
331 The
332 .BR poll ()
333 system call was introduced in Linux 2.1.23.
334 On older kernels that lack this system call,
335 .\" library call was introduced in libc 5.4.28
336 the glibc (and the old Linux libc)
337 .BR poll ()
338 wrapper function provides emulation using
339 .BR select (2).
340
341 The
342 .BR ppoll ()
343 system call was added to Linux in kernel 2.6.16.
344 The
345 .BR ppoll ()
346 library call was added in glibc 2.4.
347 .SH CONFORMING TO
348 .BR poll ()
349 conforms to POSIX.1-2001.
350 .BR ppoll ()
351 is Linux-specific.
352 .\" NetBSD 3.0 has a pollts() which is like Linux ppoll().
353 .SH NOTES
354 Some implementations define the nonstandard constant
355 .B INFTIM
356 with the value \-1 for use as a
357 .IR timeout
358 for
359 .BR poll ().
360 This constant is not provided in glibc.
361
362 For a discussion of what may happen if a file descriptor being monitored by
363 .BR poll ()
364 is closed in another thread, see
365 .BR select (2).
366 .SS C library/kernel ABI differences
367 The Linux
368 .BR ppoll ()
369 system call modifies its
370 .I timeout_ts
371 argument.
372 However, the glibc wrapper function hides this behavior
373 by using a local variable for the timeout argument that
374 is passed to the system call.
375 Thus, the glibc
376 .BR ppoll ()
377 function does not modify its
378 .I timeout_ts
379 argument.
380
381 The raw
382 .BR ppoll ()
383 system call has a fifth argument,
384 .IR "size_t sigsetsize" ,
385 which specifies the size in bytes of the
386 .IR sigmask
387 argument.
388 The glibc
389 .BR ppoll ()
390 wrapper function specifies this argument as a fixed value
391 (equal to
392 .IR sizeof(sigset_t) ).
393 .SH BUGS
394 See the discussion of spurious readiness notifications under the
395 BUGS section of
396 .BR select (2).
397 .SH SEE ALSO
398 .BR restart_syscall (2),
399 .BR select (2),
400 .BR select_tut (2),
401 .BR time (7)
402 .SH COLOPHON
403 This page is part of release 3.78 of the Linux
404 .I man-pages
405 project.
406 A description of the project,
407 information about reporting bugs,
408 and the latest version of this page,
409 can be found at
410 \%http://www.kernel.org/doc/man\-pages/.