OSDN Git Service

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