OSDN Git Service

bf36faf331308f5b134c9077368f5a5f3230b8af
[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-01-31 "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 <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 If this field is negative, then the corresponding
76 .I events
77 field is ignored and the
78 .I revents
79 field returns zero.
80 (This provides an easy way of ignoring a
81 file descriptor for a single
82 .BR poll ()
83 call: simply negate the
84 .I fd
85 field.)
86
87 The field
88 .I events
89 is an input parameter, a bit mask specifying the events the application
90 is interested in for the file descriptor
91 .IR fd .
92 This field may be specified as zero,
93 in which case the only events that can be returned in
94 .I revents
95 are
96 .BR POLLHUP ,
97 .BR POLLERR ,
98 and
99 .B POLLNVAL
100 (see below).
101
102 The field
103 .I revents
104 is an output parameter, filled by the kernel with the events that
105 actually occurred.
106 The bits returned in
107 .I revents
108 can include any of those specified in
109 .IR events ,
110 or one of the values
111 .BR POLLERR ,
112 .BR POLLHUP ,
113 or
114 .BR POLLNVAL .
115 (These three bits are meaningless in the
116 .I events
117 field, and will be set in the
118 .I revents
119 field whenever the corresponding condition is true.)
120
121 If none of the events requested (and no error) has occurred for any
122 of the file descriptors, then
123 .BR poll ()
124 blocks until one of the events occurs.
125
126 The
127 .I timeout
128 argument specifies the number of milliseconds that
129 .BR poll ()
130 should block waiting for a file descriptor to become ready.
131 The call will block until either:
132 .IP * 3
133 a file descriptor becomes ready;
134 .IP *
135 the call is interrupted by a signal handler; or
136 .IP *
137 the timeout expires.
138 .PP
139 Note that the
140 .I timeout
141 interval will be rounded up to the system clock granularity,
142 and kernel scheduling delays mean that the blocking interval
143 may overrun by a small amount.
144 Specifying a negative value in
145 .I timeout
146 means an infinite timeout.
147 Specifying a
148 .I timeout
149 of zero causes
150 .BR poll ()
151 to return immediately, even if no file descriptors are ready.
152
153 The bits that may be set/returned in
154 .I events
155 and
156 .I revents
157 are defined in \fI<poll.h>\fP:
158 .RS
159 .TP
160 .B POLLIN
161 There is data to read.
162 .TP
163 .B POLLPRI
164 There is urgent data to read (e.g., out-of-band data on TCP socket;
165 pseudoterminal master in packet mode has seen state change in slave).
166 .TP
167 .B POLLOUT
168 Writing now will not block.
169 .TP
170 .BR POLLRDHUP " (since Linux 2.6.17)"
171 Stream socket peer closed connection,
172 or shut down writing half of connection.
173 The
174 .B _GNU_SOURCE
175 feature test macro must be defined
176 (before including
177 .I any
178 header files)
179 in order to obtain this definition.
180 .TP
181 .B POLLERR
182 Error condition (output only).
183 .TP
184 .B POLLHUP
185 Hang up (output only).
186 .TP
187 .B POLLNVAL
188 Invalid request:
189 .I fd
190 not open (output only).
191 .RE
192 .PP
193 When compiling with
194 .B _XOPEN_SOURCE
195 defined, one also has the following,
196 which convey no further information beyond the bits listed above:
197 .RS
198 .TP
199 .B POLLRDNORM
200 Equivalent to
201 .BR POLLIN .
202 .TP
203 .B POLLRDBAND
204 Priority band data can be read (generally unused on Linux).
205 .\" POLLRDBAND is used in the DECnet protocol.
206 .TP
207 .B POLLWRNORM
208 Equivalent to
209 .BR POLLOUT .
210 .TP
211 .B POLLWRBAND
212 Priority data may be written.
213 .RE
214 .PP
215 Linux also knows about, but does not use
216 .BR POLLMSG .
217 .SS ppoll()
218 The relationship between
219 .BR poll ()
220 and
221 .BR ppoll ()
222 is analogous to the relationship between
223 .BR select (2)
224 and
225 .BR pselect (2):
226 like
227 .BR pselect (2),
228 .BR ppoll ()
229 allows an application to safely wait until either a file descriptor
230 becomes ready or until a signal is caught.
231 .PP
232 Other than the difference in the precision of the
233 .I timeout
234 argument, the following
235 .BR ppoll ()
236 call:
237 .nf
238
239     ready = ppoll(&fds, nfds, timeout_ts, &sigmask);
240
241 .fi
242 is equivalent to
243 .I atomically
244 executing the following calls:
245 .nf
246
247     sigset_t origmask;
248     int timeout;
249
250     timeout = (timeout_ts == NULL) ? \-1 :
251               (timeout_ts.tv_sec * 1000 + timeout_ts.tv_nsec / 1000000);
252     sigprocmask(SIG_SETMASK, &sigmask, &origmask);
253     ready = poll(&fds, nfds, timeout);
254     sigprocmask(SIG_SETMASK, &origmask, NULL);
255 .fi
256 .PP
257 See the description of
258 .BR pselect (2)
259 for an explanation of why
260 .BR ppoll ()
261 is necessary.
262
263 If the
264 .I sigmask
265 argument is specified as NULL, then
266 no signal mask manipulation is performed
267 (and thus
268 .BR ppoll ()
269 differs from
270 .BR poll ()
271 only in the precision of the
272 .I timeout
273 argument).
274
275 The
276 .I timeout_ts
277 argument specifies an upper limit on the amount of time that
278 .BR ppoll ()
279 will block.
280 This argument is a pointer to a structure of the following form:
281 .in +4n
282 .nf
283
284 struct timespec {
285     long    tv_sec;         /* seconds */
286     long    tv_nsec;        /* nanoseconds */
287 };
288 .fi
289 .in
290
291 If
292 .I timeout_ts
293 is specified as NULL, then
294 .BR ppoll ()
295 can block indefinitely.
296 .SH RETURN VALUE
297 On success, a positive number is returned; this is
298 the number of structures which have nonzero
299 .I revents
300 fields (in other words, those descriptors with events or errors reported).
301 A value of 0 indicates that the call timed out and no file
302 descriptors were ready.
303 On error, \-1 is returned, and
304 .I errno
305 is set appropriately.
306 .SH ERRORS
307 .TP
308 .B EFAULT
309 The array given as argument was not contained in the calling program's
310 address space.
311 .TP
312 .B EINTR
313 A signal occurred before any requested event; see
314 .BR signal (7).
315 .TP
316 .B EINVAL
317 The
318 .I nfds
319 value exceeds the
320 .B RLIMIT_NOFILE
321 value.
322 .TP
323 .B ENOMEM
324 There was no space to allocate file descriptor tables.
325 .SH VERSIONS
326 The
327 .BR poll ()
328 system call was introduced in Linux 2.1.23.
329 On older kernels that lack this system call,
330 .\" library call was introduced in libc 5.4.28
331 the glibc (and the old Linux libc)
332 .BR poll ()
333 wrapper function provides emulation using
334 .BR select (2).
335
336 The
337 .BR ppoll ()
338 system call was added to Linux in kernel 2.6.16.
339 The
340 .BR ppoll ()
341 library call was added in glibc 2.4.
342 .SH CONFORMING TO
343 .BR poll ()
344 conforms to POSIX.1-2001.
345 .BR ppoll ()
346 is Linux-specific.
347 .\" NetBSD 3.0 has a pollts() which is like Linux ppoll().
348 .SH NOTES
349 Some implementations define the nonstandard constant
350 .B INFTIM
351 with the value \-1 for use as a
352 .IR timeout
353 for
354 .BR poll ().
355 This constant is not provided in glibc.
356
357 For a discussion of what may happen if a file descriptor being monitored by
358 .BR poll ()
359 is closed in another thread, see
360 .BR select (2).
361 .SS Linux notes
362 The Linux
363 .BR ppoll ()
364 system call modifies its
365 .I timeout_ts
366 argument.
367 However, the glibc wrapper function hides this behavior
368 by using a local variable for the timeout argument that
369 is passed to the system call.
370 Thus, the glibc
371 .BR ppoll ()
372 function does not modify its
373 .I timeout_ts
374 argument.
375 .SH BUGS
376 See the discussion of spurious readiness notifications under the
377 BUGS section of
378 .BR select (2).
379 .SH SEE ALSO
380 .BR restart_syscall (2),
381 .BR select (2),
382 .BR select_tut (2),
383 .BR time (7)
384 .SH COLOPHON
385 This page is part of release 3.64 of the Linux
386 .I man-pages
387 project.
388 A description of the project,
389 and information about reporting bugs,
390 can be found at
391 \%http://www.kernel.org/doc/man\-pages/.