OSDN Git Service

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