OSDN Git Service

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