]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/poll.2
Merge LUA 5.4.6
[FreeBSD/FreeBSD.git] / lib / libc / sys / poll.2
1 .\"     $NetBSD: poll.2,v 1.3 1996/09/07 21:53:08 mycroft Exp $
2 .\" $FreeBSD$
3 .\"
4 .\" Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\"    notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\"    notice, this list of conditions and the following disclaimer in the
13 .\"    documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\"    must display the following acknowledgement:
16 .\"     This product includes software developed by Charles M. Hannum.
17 .\" 4. The name of the author may not be used to endorse or promote products
18 .\"    derived from this software without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 .\"
31 .Dd April 27, 2021
32 .Dt POLL 2
33 .Os
34 .Sh NAME
35 .Nm poll
36 .Nd synchronous I/O multiplexing
37 .Sh LIBRARY
38 .Lb libc
39 .Sh SYNOPSIS
40 .In poll.h
41 .Ft int
42 .Fn poll "struct pollfd fds[]" "nfds_t nfds" "int timeout"
43 .Ft int
44 .Fo ppoll
45 .Fa "struct pollfd fds[]"
46 .Fa "nfds_t nfds"
47 .Fa "const struct timespec * restrict timeout"
48 .Fa "const sigset_t * restrict newsigmask"
49 .Fc
50 .Sh DESCRIPTION
51 The
52 .Fn poll
53 system call
54 examines a set of file descriptors to see if some of them are ready for
55 I/O.
56 The
57 .Fa fds
58 argument is a pointer to an array of pollfd structures as defined in
59 .In poll.h
60 (shown below).
61 The
62 .Fa nfds
63 argument determines the size of the
64 .Fa fds
65 array.
66 .Bd -literal
67 struct pollfd {
68     int    fd;       /* file descriptor */
69     short  events;   /* events to look for */
70     short  revents;  /* events returned */
71 };
72 .Ed
73 .Pp
74 The fields of
75 .Fa struct pollfd
76 are as follows:
77 .Bl -tag -width XXXrevents
78 .It fd
79 File descriptor to poll.
80 If fd is equal to -1 then
81 .Fa revents
82 is cleared (set to zero), and that pollfd is not checked.
83 .It events
84 Events to poll for.
85 (See below.)
86 .It revents
87 Events which may occur.
88 (See below.)
89 .El
90 .Pp
91 The event bitmasks in
92 .Fa events
93 and
94 .Fa revents
95 have the following bits:
96 .Bl -tag -width XXXPOLLWRNORM
97 .It POLLIN
98 Data other than high priority data may be read without blocking.
99 .It POLLRDNORM
100 Normal data may be read without blocking.
101 .It POLLRDBAND
102 Data with a non-zero priority may be read without blocking.
103 .It POLLPRI
104 High priority data may be read without blocking.
105 .It POLLOUT
106 .It POLLWRNORM
107 Normal data may be written without blocking.
108 .It POLLWRBAND
109 Data with a non-zero priority may be written without blocking.
110 .It POLLERR
111 An exceptional condition has occurred on the device or socket.
112 This
113 flag is always checked, even if not present in the
114 .Fa events
115 bitmask.
116 .It POLLHUP
117 The device or socket has been disconnected.
118 This flag is always
119 checked, even if not present in the
120 .Fa events
121 bitmask.
122 Note that
123 POLLHUP
124 and
125 POLLOUT
126 should never be present in the
127 .Fa revents
128 bitmask at the same time.
129 .It POLLRDHUP
130 Remote peer closed connection, or shut down writing.
131 Unlike
132 POLLHUP,
133 POLLRDHUP
134 must be present in the
135 .Fa events
136 bitmask to be reported.
137 Applies only to stream sockets.
138 .It POLLNVAL
139 The file descriptor is not open,
140 or in capability mode the file descriptor has insufficient rights.
141 This flag is always checked, even
142 if not present in the
143 .Fa events
144 bitmask.
145 .El
146 .Pp
147 If
148 .Fa timeout
149 is neither zero nor INFTIM (-1), it specifies a maximum interval to
150 wait for any file descriptor to become ready, in milliseconds.
151 If
152 .Fa timeout
153 is INFTIM (-1), the poll blocks indefinitely.
154 If
155 .Fa timeout
156 is zero, then
157 .Fn poll
158 will return without blocking.
159 .Pp
160 The
161 .Fn ppoll
162 system call, unlike
163 .Fn poll ,
164 is used to safely wait until either a set of file descriptors becomes
165 ready or until a signal is caught.
166 The
167 .Fa fds
168 and
169 .Fa nfds
170 arguments are identical to the analogous arguments of
171 .Fn poll .
172 The
173 .Fa timeout
174 argument in
175 .Fn ppoll
176 points to a
177 .Vt "const struct timespec"
178 which is defined in
179 .In sys/timespec.h
180 (shown below) rather than the
181 .Vt "int timeout"
182 used by
183 .Fn poll .
184 A null pointer may be passed to indicate that
185 .Fn ppoll
186 should wait indefinitely.
187 Finally,
188 .Fa newsigmask
189 specifies a signal mask which is set while waiting for input.
190 When
191 .Fn ppoll
192 returns, the original signal mask is restored.
193 .Bd -literal
194 struct timespec {
195         time_t  tv_sec;         /* seconds */
196         long    tv_nsec;        /* and nanoseconds */
197 };
198 .Ed
199 .Sh RETURN VALUES
200 The
201 .Fn poll
202 system call
203 returns the number of descriptors that are ready for I/O, or -1 if an
204 error occurred.
205 If the time limit expires,
206 .Fn poll
207 returns 0.
208 If
209 .Fn poll
210 returns with an error,
211 including one due to an interrupted system call,
212 the
213 .Fa fds
214 array will be unmodified.
215 .Sh COMPATIBILITY
216 This implementation differs from the historical one in that a given
217 file descriptor may not cause
218 .Fn poll
219 to return with an error.
220 In cases where this would have happened in
221 the historical implementation (e.g.\& trying to poll a
222 .Xr revoke 2 Ns ed
223 descriptor), this implementation instead copies the
224 .Fa events
225 bitmask to the
226 .Fa revents
227 bitmask.
228 Attempting to perform I/O on this descriptor will then
229 return an error.
230 This behaviour is believed to be more useful.
231 .Sh ERRORS
232 An error return from
233 .Fn poll
234 indicates:
235 .Bl -tag -width Er
236 .It Bq Er EFAULT
237 The
238 .Fa fds
239 argument
240 points outside the process's allocated address space.
241 .It Bq Er EINTR
242 A signal was delivered before the time limit expired and
243 before any of the selected events occurred.
244 .It Bq Er EINVAL
245 The specified time limit is invalid.
246 One of its components is negative or too large.
247 .It Bq Er EINVAL
248 The number of pollfd structures specified by
249 .Fa nfds
250 exceeds the system tunable
251 .Va kern.maxfilesperproc
252 and
253 .Dv FD_SETSIZE .
254 .El
255 .Sh SEE ALSO
256 .Xr accept 2 ,
257 .Xr connect 2 ,
258 .Xr kqueue 2 ,
259 .Xr pselect 2 ,
260 .Xr read 2 ,
261 .Xr recv 2 ,
262 .Xr select 2 ,
263 .Xr send 2 ,
264 .Xr write 2
265 .Sh STANDARDS
266 The
267 .Fn poll
268 function conforms to
269 .St -p1003.1-2001 .
270 The
271 .Fn ppoll
272 is not specified by POSIX.
273 The
274 POLLRDHUP
275 flag is not specified by POSIX, but is compatible with Linux and illumos.
276 .Sh HISTORY
277 The
278 .Fn poll
279 function appeared in
280 .At V .
281 This manual page and the core of the implementation was taken from
282 .Nx .
283 The
284 .Fn ppoll
285 function first appeared in
286 .Fx 11.0
287 .Sh BUGS
288 The distinction between some of the fields in the
289 .Fa events
290 and
291 .Fa revents
292 bitmasks is really not useful without STREAMS.
293 The fields are
294 defined for compatibility with existing software.