OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / 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 2009-09-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 .B #define _GNU_SOURCE
41 .B #include <poll.h>
42 .sp
43 .BI "int ppoll(struct pollfd *" fds ", nfds_t " nfds ", "
44 .BI "        const struct timespec *" timeout ", 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 pseudo-terminal 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 in order to obtain this definition.
137 .TP
138 .B POLLERR
139 Error condition (output only).
140 .TP
141 .B POLLHUP
142 Hang up (output only).
143 .TP
144 .B POLLNVAL
145 Invalid request:
146 .I fd
147 not open (output only).
148 .RE
149 .PP
150 When compiling with
151 .B _XOPEN_SOURCE
152 defined, one also has the following,
153 which convey no further information beyond the bits listed above:
154 .RS
155 .TP
156 .B POLLRDNORM
157 Equivalent to
158 .BR POLLIN .
159 .TP
160 .B POLLRDBAND
161 Priority band data can be read (generally unused on Linux).
162 .\" POLLRDBAND is used in the DECnet protocol.
163 .TP
164 .B POLLWRNORM
165 Equivalent to
166 .BR POLLOUT .
167 .TP
168 .B POLLWRBAND
169 Priority data may be written.
170 .RE
171 .PP
172 Linux also knows about, but does not use
173 .BR POLLMSG .
174 .SS ppoll()
175 The relationship between
176 .BR poll ()
177 and
178 .BR ppoll ()
179 is analogous to the relationship between
180 .BR select (2)
181 and
182 .BR pselect (2):
183 like
184 .BR pselect (2),
185 .BR ppoll ()
186 allows an application to safely wait until either a file descriptor
187 becomes ready or until a signal is caught.
188 .PP
189 Other than the difference in the
190 .I timeout
191 argument, the following
192 .BR ppoll ()
193 call:
194 .nf
195
196     ready = ppoll(&fds, nfds, timeout, &sigmask);
197
198 .fi
199 is equivalent to
200 .I atomically
201 executing the following calls:
202 .nf
203
204     sigset_t origmask;
205
206     sigprocmask(SIG_SETMASK, &sigmask, &origmask);
207     ready = poll(&fds, nfds, timeout);
208     sigprocmask(SIG_SETMASK, &origmask, NULL);
209 .fi
210 .PP
211 See the description of
212 .BR pselect (2)
213 for an explanation of why
214 .BR ppoll ()
215 is necessary.
216
217 If the
218 .I sigmask
219 argument is specified as NULL, then
220 no signal mask manipulation is performed
221 (and thus
222 .BR ppoll ()
223 differs from
224 .BR poll ()
225 only in the precision of the
226 .I timeout
227 argument).
228
229 The
230 .I timeout
231 argument specifies an upper limit on the amount of time that
232 .BR ppoll ()
233 will block.
234 This argument is a pointer to a structure of the following form:
235 .in +4n
236 .nf
237
238 struct timespec {
239     long    tv_sec;         /* seconds */
240     long    tv_nsec;        /* nanoseconds */
241 };
242 .fi
243 .in
244
245 If
246 .I timeout
247 is specified as NULL, then
248 .BR ppoll ()
249 can block indefinitely.
250 .SH "RETURN VALUE"
251 On success, a positive number is returned; this is
252 the number of structures which have nonzero
253 .I revents
254 fields (in other words, those descriptors with events or errors reported).
255 A value of 0 indicates that the call timed out and no file
256 descriptors were ready.
257 On error, \-1 is returned, and
258 .I errno
259 is set appropriately.
260 .SH ERRORS
261 .TP
262 .B EFAULT
263 The array given as argument was not contained in the calling program's
264 address space.
265 .TP
266 .B EINTR
267 A signal occurred before any requested event; see
268 .BR signal (7).
269 .TP
270 .B EINVAL
271 The
272 .I nfds
273 value exceeds the
274 .B RLIMIT_NOFILE
275 value.
276 .TP
277 .B ENOMEM
278 There was no space to allocate file descriptor tables.
279 .SH VERSIONS
280 The
281 .BR poll ()
282 system call was introduced in Linux 2.1.23.
283 The
284 .BR poll ()
285 library call was introduced in libc 5.4.28
286 (and provides emulation using select(2) if your kernel does not
287 have a
288 .BR poll ()
289 system call).
290
291 The
292 .BR ppoll ()
293 system call was added to Linux in kernel 2.6.16.
294 The
295 .BR ppoll ()
296 library call was added in glibc 2.4.
297 .SH "CONFORMING TO"
298 .BR poll ()
299 conforms to POSIX.1-2001.
300 .BR ppoll ()
301 is Linux-specific.
302 .\" NetBSD 3.0 has a pollts() which is like Linux ppoll().
303 .SH NOTES
304 Some implementations define the nonstandard constant
305 .B INFTIM
306 with the value \-1 for use as a
307 .IR timeout .
308 This constant is not provided in glibc.
309 .SS "Linux Notes"
310 The Linux
311 .BR ppoll ()
312 system call modifies its
313 .I timeout
314 argument.
315 However, the glibc wrapper function hides this behavior
316 by using a local variable for the timeout argument that
317 is passed to the system call.
318 Thus, the glibc
319 .BR ppoll ()
320 function does not modify its
321 .I timeout
322 argument.
323 .SH BUGS
324 See the discussion of spurious readiness notifications under the
325 BUGS section of
326 .BR select (2).
327 .SH "SEE ALSO"
328 .BR select (2),
329 .BR select_tut (2),
330 .BR feature_test_macros (7),
331 .BR time (7)