1 |
27 |
unneback |
.\" $OpenBSD: select.2,v 1.20 2001/09/04 08:04:08 deraadt Exp $
|
2 |
|
|
.\" $NetBSD: select.2,v 1.5 1995/06/27 22:32:28 cgd Exp $
|
3 |
|
|
.\"
|
4 |
|
|
.\" Copyright (c) 1983, 1991, 1993
|
5 |
|
|
.\" The Regents of the University of California. All rights reserved.
|
6 |
|
|
.\"
|
7 |
|
|
.\" Redistribution and use in source and binary forms, with or without
|
8 |
|
|
.\" modification, are permitted provided that the following conditions
|
9 |
|
|
.\" are met:
|
10 |
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
11 |
|
|
.\" notice, this list of conditions and the following disclaimer.
|
12 |
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
13 |
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
14 |
|
|
.\" documentation and/or other materials provided with the distribution.
|
15 |
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
16 |
|
|
.\" must display the following acknowledgement:
|
17 |
|
|
.\" This product includes software developed by the University of
|
18 |
|
|
.\" California, Berkeley and its contributors.
|
19 |
|
|
.\" 4. Neither the name of the University nor the names of its contributors
|
20 |
|
|
.\" may be used to endorse or promote products derived from this software
|
21 |
|
|
.\" without specific prior written permission.
|
22 |
|
|
.\"
|
23 |
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
24 |
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
25 |
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
26 |
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
27 |
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
28 |
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
29 |
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
30 |
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
31 |
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
32 |
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
33 |
|
|
.\" SUCH DAMAGE.
|
34 |
|
|
.\"
|
35 |
|
|
.\" @(#)select.2 8.2 (Berkeley) 3/25/94
|
36 |
|
|
.\"
|
37 |
|
|
.Dd March 25, 1994
|
38 |
|
|
.Dt SELECT 2
|
39 |
|
|
.Os
|
40 |
|
|
.Sh NAME
|
41 |
|
|
.Nm select
|
42 |
|
|
.Nd synchronous I/O multiplexing
|
43 |
|
|
.Sh SYNOPSIS
|
44 |
|
|
.Fd #include
|
45 |
|
|
.Fd #include
|
46 |
|
|
.Fd #include
|
47 |
|
|
.Ft int
|
48 |
|
|
.Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout"
|
49 |
|
|
.Fn FD_SET fd &fdset
|
50 |
|
|
.Fn FD_CLR fd &fdset
|
51 |
|
|
.Fn FD_ISSET fd &fdset
|
52 |
|
|
.Fn FD_ZERO &fdset
|
53 |
|
|
.Sh DESCRIPTION
|
54 |
|
|
.Fn select
|
55 |
|
|
examines the I/O descriptor sets whose addresses are passed in
|
56 |
|
|
.Fa readfds ,
|
57 |
|
|
.Fa writefds ,
|
58 |
|
|
and
|
59 |
|
|
.Fa exceptfds
|
60 |
|
|
to see if some of their descriptors
|
61 |
|
|
are ready for reading, are ready for writing, or have an exceptional
|
62 |
|
|
condition pending, respectively.
|
63 |
|
|
The first
|
64 |
|
|
.Fa nfds
|
65 |
|
|
descriptors are checked in each set;
|
66 |
|
|
i.e., the descriptors from 0 through
|
67 |
|
|
.Fa nfds Ns No -1
|
68 |
|
|
in the descriptor sets are examined.
|
69 |
|
|
On return,
|
70 |
|
|
.Fn select
|
71 |
|
|
replaces the given descriptor sets
|
72 |
|
|
with subsets consisting of those descriptors that are ready
|
73 |
|
|
for the requested operation.
|
74 |
|
|
.Fn select
|
75 |
|
|
returns the total number of ready descriptors in all the sets.
|
76 |
|
|
.Pp
|
77 |
|
|
The descriptor sets are stored as bit fields in arrays of integers.
|
78 |
|
|
The following macros are provided for manipulating such descriptor sets:
|
79 |
|
|
.Fn FD_ZERO &fdset
|
80 |
|
|
initializes a descriptor set
|
81 |
|
|
.Fa fdset
|
82 |
|
|
to the null set.
|
83 |
|
|
.Fn FD_SET fd &fdset
|
84 |
|
|
includes a particular descriptor
|
85 |
|
|
.Fa fd
|
86 |
|
|
in
|
87 |
|
|
.Fa fdset .
|
88 |
|
|
.Fn FD_CLR fd &fdset
|
89 |
|
|
removes
|
90 |
|
|
.Fa fd
|
91 |
|
|
from
|
92 |
|
|
.Fa fdset .
|
93 |
|
|
.Fn FD_ISSET fd &fdset
|
94 |
|
|
is non-zero if
|
95 |
|
|
.Fa fd
|
96 |
|
|
is a member of
|
97 |
|
|
.Fa fdset ,
|
98 |
|
|
zero otherwise.
|
99 |
|
|
The behavior of these macros is undefined if
|
100 |
|
|
a descriptor value is less than zero or greater than or equal to
|
101 |
|
|
.Dv FD_SETSIZE ,
|
102 |
|
|
which is normally at least equal
|
103 |
|
|
to the maximum number of descriptors supported by the system.
|
104 |
|
|
.Pp
|
105 |
|
|
If
|
106 |
|
|
.Fa timeout
|
107 |
|
|
is a non-null pointer, it specifies a maximum interval to wait for the
|
108 |
|
|
selection to complete.
|
109 |
|
|
If
|
110 |
|
|
.Fa timeout
|
111 |
|
|
is a null pointer, the select blocks indefinitely.
|
112 |
|
|
To effect a poll, the
|
113 |
|
|
.Fa timeout
|
114 |
|
|
argument should be non-null, pointing to a zero-valued timeval structure.
|
115 |
|
|
.Fa timeout
|
116 |
|
|
is not changed by
|
117 |
|
|
.Fn select ,
|
118 |
|
|
and may be reused on subsequent calls; however, it is good style to
|
119 |
|
|
re-initialize it before each invocation of
|
120 |
|
|
.Fn select .
|
121 |
|
|
.Pp
|
122 |
|
|
Any of
|
123 |
|
|
.Fa readfds ,
|
124 |
|
|
.Fa writefds ,
|
125 |
|
|
and
|
126 |
|
|
.Fa exceptfds
|
127 |
|
|
may be given as null pointers if no descriptors are of interest.
|
128 |
|
|
.Sh RETURN VALUES
|
129 |
|
|
.Fn select
|
130 |
|
|
returns the number of ready descriptors that are contained in
|
131 |
|
|
the descriptor sets, or \-1 is an error occurred.
|
132 |
|
|
If the time limit expires,
|
133 |
|
|
.Fn select
|
134 |
|
|
returns 0.
|
135 |
|
|
If
|
136 |
|
|
.Fn select
|
137 |
|
|
returns with an error, including one due to an interrupted call,
|
138 |
|
|
the descriptor sets will be unmodified.
|
139 |
|
|
.Sh ERRORS
|
140 |
|
|
An error return from
|
141 |
|
|
.Fn select
|
142 |
|
|
indicates:
|
143 |
|
|
.Bl -tag -width Er
|
144 |
|
|
.It Bq Er EFAULT
|
145 |
|
|
One or more of
|
146 |
|
|
.Fa readfds ,
|
147 |
|
|
.Fa writefds ,
|
148 |
|
|
or
|
149 |
|
|
.Fa exceptfds
|
150 |
|
|
points outside the process's allocated address space.
|
151 |
|
|
.It Bq Er EBADF
|
152 |
|
|
One of the descriptor sets specified an invalid descriptor.
|
153 |
|
|
.It Bq Er EINTR
|
154 |
|
|
A signal was delivered before the time limit expired and
|
155 |
|
|
before any of the selected events occurred.
|
156 |
|
|
.It Bq Er EINVAL
|
157 |
|
|
The specified time limit is invalid.
|
158 |
|
|
One of its components is negative or too large.
|
159 |
|
|
.El
|
160 |
|
|
.Sh SEE ALSO
|
161 |
|
|
.Xr accept 2 ,
|
162 |
|
|
.Xr connect 2 ,
|
163 |
|
|
.Xr gettimeofday 2 ,
|
164 |
|
|
.Xr poll 2 ,
|
165 |
|
|
.Xr read 2 ,
|
166 |
|
|
.Xr recv 2 ,
|
167 |
|
|
.Xr send 2 ,
|
168 |
|
|
.Xr write 2 ,
|
169 |
|
|
.Xr getdtablesize 3
|
170 |
|
|
.Sh BUGS
|
171 |
|
|
Although the provision of
|
172 |
|
|
.Xr getdtablesize 3
|
173 |
|
|
was intended to allow user programs to be written independent
|
174 |
|
|
of the kernel limit on the number of open files, the dimension
|
175 |
|
|
of a sufficiently large bit field for select remains a problem.
|
176 |
|
|
The default bit size of
|
177 |
|
|
.Ft fd_set
|
178 |
|
|
is based on the symbol
|
179 |
|
|
.Dv FD_SETSIZE
|
180 |
|
|
(currently 256),
|
181 |
|
|
but that is somewhat smaller than the current kernel limit
|
182 |
|
|
to the number of open files.
|
183 |
|
|
However, in order to accommodate programs which might potentially
|
184 |
|
|
use a larger number of open files with select, it is possible
|
185 |
|
|
to increase this size within a program by providing
|
186 |
|
|
a larger definition of
|
187 |
|
|
.Dv FD_SETSIZE
|
188 |
|
|
before the inclusion of
|
189 |
|
|
.Aq Pa sys/types.h .
|
190 |
|
|
The kernel will cope, and the userland libraries provided with the
|
191 |
|
|
system are also ready for large numbers of file descriptors.
|
192 |
|
|
.Pp
|
193 |
|
|
Alternatively, to be really safe, it is possible to allocate
|
194 |
|
|
.Ft fd_set
|
195 |
|
|
bit-arrays dynamically.
|
196 |
|
|
The idea is to permit a program to work properly even if it is
|
197 |
|
|
.Xr execve 2 Ns 'd
|
198 |
|
|
with 4000 file descriptors pre-allocated.
|
199 |
|
|
The following illustrates the technique which is used by
|
200 |
|
|
userland libraries:
|
201 |
|
|
.Pp
|
202 |
|
|
.Bd -literal -offset indent -compact
|
203 |
|
|
fd_set *fdsr;
|
204 |
|
|
int max = fd;
|
205 |
|
|
|
206 |
|
|
fdsr = (fd_set *)calloc(howmany(max+1, NFDBITS),
|
207 |
|
|
sizeof(fd_mask));
|
208 |
|
|
if (fdsr == NULL) {
|
209 |
|
|
...
|
210 |
|
|
return (-1);
|
211 |
|
|
}
|
212 |
|
|
FD_SET(fd, fdsr);
|
213 |
|
|
n = select(max+1, fdsr, NULL, NULL, &tv);
|
214 |
|
|
...
|
215 |
|
|
free(fdsr);
|
216 |
|
|
.Ed
|
217 |
|
|
.Pp
|
218 |
|
|
Alternatively, it is possible to use the
|
219 |
|
|
.Xr poll 2
|
220 |
|
|
interface.
|
221 |
|
|
.Xr poll 2
|
222 |
|
|
is more efficient when the size of
|
223 |
|
|
.Fn select Ns 's
|
224 |
|
|
.Ft fd_set
|
225 |
|
|
bit-arrays are very large, and for fixed numbers of
|
226 |
|
|
file descriptors one need not size and dynamically allocate a
|
227 |
|
|
memory object.
|
228 |
|
|
.Pp
|
229 |
|
|
.Fn select
|
230 |
|
|
should probably have been designed to return the time remaining from the
|
231 |
|
|
original timeout, if any, by modifying the time value in place.
|
232 |
|
|
Even though some systems stupidly act in this different way, it is
|
233 |
|
|
unlikely this semantic will ever be commonly implemented, as the
|
234 |
|
|
change causes massive source code compatibility problems.
|
235 |
|
|
Furthermore, recent new standards have dictated the current behaviour.
|
236 |
|
|
In general, due to the existence of those brain-damaged
|
237 |
|
|
non-conforming systems, it is unwise to assume that the timeout
|
238 |
|
|
value will be unmodified by the
|
239 |
|
|
.Fn select
|
240 |
|
|
call, and the caller should reinitialize it on each invocation.
|
241 |
|
|
Calculating the delta is easily done by calling
|
242 |
|
|
.Xr gettimeofday 2
|
243 |
|
|
before and after the call to
|
244 |
|
|
.Fn select Ns ,
|
245 |
|
|
and using
|
246 |
|
|
.Fn timersub
|
247 |
|
|
(as described in
|
248 |
|
|
.Xr getitimer 2 ) .
|
249 |
|
|
.Pp
|
250 |
|
|
Internally to the kernel,
|
251 |
|
|
.Fn select
|
252 |
|
|
works poorly if multiple processes wait on the same file descriptor.
|
253 |
|
|
Given that, it is rather surprising to see that many daemons are
|
254 |
|
|
written that way (i.e.,
|
255 |
|
|
.Xr httpd 8 ) .
|
256 |
|
|
.Sh HISTORY
|
257 |
|
|
The
|
258 |
|
|
.Fn select
|
259 |
|
|
function call appeared in
|
260 |
|
|
.Bx 4.2 .
|