xref: /xnu-8796.141.3/bsd/man/man2/select.2 (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions.\"	$NetBSD: select.2,v 1.5 1995/06/27 22:32:28 cgd Exp $
2*1b191cb5SApple OSS Distributions.\"
3*1b191cb5SApple OSS Distributions.\" Copyright (c) 1983, 1991, 1993
4*1b191cb5SApple OSS Distributions.\"	The Regents of the University of California.  All rights reserved.
5*1b191cb5SApple OSS Distributions.\"
6*1b191cb5SApple OSS Distributions.\" Redistribution and use in source and binary forms, with or without
7*1b191cb5SApple OSS Distributions.\" modification, are permitted provided that the following conditions
8*1b191cb5SApple OSS Distributions.\" are met:
9*1b191cb5SApple OSS Distributions.\" 1. Redistributions of source code must retain the above copyright
10*1b191cb5SApple OSS Distributions.\"    notice, this list of conditions and the following disclaimer.
11*1b191cb5SApple OSS Distributions.\" 2. Redistributions in binary form must reproduce the above copyright
12*1b191cb5SApple OSS Distributions.\"    notice, this list of conditions and the following disclaimer in the
13*1b191cb5SApple OSS Distributions.\"    documentation and/or other materials provided with the distribution.
14*1b191cb5SApple OSS Distributions.\" 3. All advertising materials mentioning features or use of this software
15*1b191cb5SApple OSS Distributions.\"    must display the following acknowledgement:
16*1b191cb5SApple OSS Distributions.\"	This product includes software developed by the University of
17*1b191cb5SApple OSS Distributions.\"	California, Berkeley and its contributors.
18*1b191cb5SApple OSS Distributions.\" 4. Neither the name of the University nor the names of its contributors
19*1b191cb5SApple OSS Distributions.\"    may be used to endorse or promote products derived from this software
20*1b191cb5SApple OSS Distributions.\"    without specific prior written permission.
21*1b191cb5SApple OSS Distributions.\"
22*1b191cb5SApple OSS Distributions.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*1b191cb5SApple OSS Distributions.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*1b191cb5SApple OSS Distributions.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*1b191cb5SApple OSS Distributions.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*1b191cb5SApple OSS Distributions.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*1b191cb5SApple OSS Distributions.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*1b191cb5SApple OSS Distributions.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*1b191cb5SApple OSS Distributions.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*1b191cb5SApple OSS Distributions.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*1b191cb5SApple OSS Distributions.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*1b191cb5SApple OSS Distributions.\" SUCH DAMAGE.
33*1b191cb5SApple OSS Distributions.\"
34*1b191cb5SApple OSS Distributions.\"     @(#)select.2	8.2 (Berkeley) 3/25/94
35*1b191cb5SApple OSS Distributions.\"
36*1b191cb5SApple OSS Distributions.Dd March 18, 2015
37*1b191cb5SApple OSS Distributions.Dt SELECT 2
38*1b191cb5SApple OSS Distributions.Os BSD 4.2
39*1b191cb5SApple OSS Distributions.Sh NAME
40*1b191cb5SApple OSS Distributions.Nm FD_CLR ,
41*1b191cb5SApple OSS Distributions.Nm FD_COPY ,
42*1b191cb5SApple OSS Distributions.Nm FD_ISSET ,
43*1b191cb5SApple OSS Distributions.Nm FD_SET ,
44*1b191cb5SApple OSS Distributions.Nm FD_ZERO ,
45*1b191cb5SApple OSS Distributions.Nm select
46*1b191cb5SApple OSS Distributions.Nd synchronous I/O multiplexing
47*1b191cb5SApple OSS Distributions.Sh SYNOPSIS
48*1b191cb5SApple OSS Distributions.Fd #include <sys/select.h>
49*1b191cb5SApple OSS Distributions.\"
50*1b191cb5SApple OSS Distributions.Ft void
51*1b191cb5SApple OSS Distributions.Fo FD_CLR
52*1b191cb5SApple OSS Distributions.Fa fd
53*1b191cb5SApple OSS Distributions.Fa "fd_set *fdset"
54*1b191cb5SApple OSS Distributions.Fc
55*1b191cb5SApple OSS Distributions.Ft void
56*1b191cb5SApple OSS Distributions.Fo FD_COPY
57*1b191cb5SApple OSS Distributions.Fa "fd_set *fdset_orig"
58*1b191cb5SApple OSS Distributions.Fa "fd_set *fdset_copy"
59*1b191cb5SApple OSS Distributions.Fc
60*1b191cb5SApple OSS Distributions.Ft int
61*1b191cb5SApple OSS Distributions.Fo FD_ISSET
62*1b191cb5SApple OSS Distributions.Fa fd
63*1b191cb5SApple OSS Distributions.Fa "fd_set *fdset"
64*1b191cb5SApple OSS Distributions.Fc
65*1b191cb5SApple OSS Distributions.Ft void
66*1b191cb5SApple OSS Distributions.Fo FD_SET
67*1b191cb5SApple OSS Distributions.Fa fd
68*1b191cb5SApple OSS Distributions.Fa "fd_set *fdset"
69*1b191cb5SApple OSS Distributions.Fc
70*1b191cb5SApple OSS Distributions.Ft void
71*1b191cb5SApple OSS Distributions.Fo FD_ZERO
72*1b191cb5SApple OSS Distributions.Fa "fd_set *fdset"
73*1b191cb5SApple OSS Distributions.Fc
74*1b191cb5SApple OSS Distributions.Ft int
75*1b191cb5SApple OSS Distributions.Fo select
76*1b191cb5SApple OSS Distributions.Fa "int nfds"
77*1b191cb5SApple OSS Distributions.Fa "fd_set *restrict readfds"
78*1b191cb5SApple OSS Distributions.Fa "fd_set *restrict writefds"
79*1b191cb5SApple OSS Distributions.Fa "fd_set *restrict errorfds"
80*1b191cb5SApple OSS Distributions.Fa "struct timeval *restrict timeout"
81*1b191cb5SApple OSS Distributions.Fc
82*1b191cb5SApple OSS Distributions.Sh DESCRIPTION
83*1b191cb5SApple OSS Distributions.Fn select
84*1b191cb5SApple OSS Distributionsexamines the I/O descriptor sets whose addresses are passed in
85*1b191cb5SApple OSS Distributions.Fa readfds ,
86*1b191cb5SApple OSS Distributions.Fa writefds ,
87*1b191cb5SApple OSS Distributionsand
88*1b191cb5SApple OSS Distributions.Fa errorfds
89*1b191cb5SApple OSS Distributionsto see if some of their descriptors
90*1b191cb5SApple OSS Distributionsare ready for reading, are ready for writing, or have an exceptional
91*1b191cb5SApple OSS Distributionscondition pending, respectively.
92*1b191cb5SApple OSS DistributionsThe first
93*1b191cb5SApple OSS Distributions.Fa nfds
94*1b191cb5SApple OSS Distributionsdescriptors are checked in each set;
95*1b191cb5SApple OSS Distributionsi.e., the descriptors from 0 through
96*1b191cb5SApple OSS Distributions.Fa nfds Ns No -1
97*1b191cb5SApple OSS Distributionsin the descriptor sets are examined.  (Example: If you have set two file descriptors "4" and "17",
98*1b191cb5SApple OSS Distributions.Fa nfds
99*1b191cb5SApple OSS Distributionsshould  not be "2", but rather "17 + 1" or "18".)
100*1b191cb5SApple OSS DistributionsOn return,
101*1b191cb5SApple OSS Distributions.Fn select
102*1b191cb5SApple OSS Distributionsreplaces the given descriptor sets
103*1b191cb5SApple OSS Distributionswith subsets consisting of those descriptors that are ready
104*1b191cb5SApple OSS Distributionsfor the requested operation.
105*1b191cb5SApple OSS Distributions.Fn select
106*1b191cb5SApple OSS Distributionsreturns the total number of ready descriptors in all the sets.
107*1b191cb5SApple OSS Distributions.Pp
108*1b191cb5SApple OSS DistributionsThe descriptor sets are stored as bit fields in arrays of integers.
109*1b191cb5SApple OSS DistributionsThe following macros are provided for manipulating such descriptor sets:
110*1b191cb5SApple OSS Distributions.Fn FD_ZERO &fdset
111*1b191cb5SApple OSS Distributionsinitializes a descriptor set
112*1b191cb5SApple OSS Distributions.Fa fdset
113*1b191cb5SApple OSS Distributionsto the null set.
114*1b191cb5SApple OSS Distributions.Fn FD_SET fd &fdset
115*1b191cb5SApple OSS Distributionsincludes a particular descriptor
116*1b191cb5SApple OSS Distributions.Fa fd
117*1b191cb5SApple OSS Distributionsin
118*1b191cb5SApple OSS Distributions.Fa fdset .
119*1b191cb5SApple OSS Distributions.Fn FD_CLR fd &fdset
120*1b191cb5SApple OSS Distributionsremoves
121*1b191cb5SApple OSS Distributions.Fa fd
122*1b191cb5SApple OSS Distributionsfrom
123*1b191cb5SApple OSS Distributions.Fa fdset .
124*1b191cb5SApple OSS Distributions.Fn FD_ISSET fd &fdset
125*1b191cb5SApple OSS Distributionsis non-zero if
126*1b191cb5SApple OSS Distributions.Fa fd
127*1b191cb5SApple OSS Distributionsis a member of
128*1b191cb5SApple OSS Distributions.Fa fdset ,
129*1b191cb5SApple OSS Distributionszero otherwise.
130*1b191cb5SApple OSS Distributions.Fn FD_COPY &fdset_orig &fdset_copy
131*1b191cb5SApple OSS Distributionsreplaces an already allocated
132*1b191cb5SApple OSS Distributions.Fa &fdset_copy
133*1b191cb5SApple OSS Distributionsfile descriptor set with a copy of
134*1b191cb5SApple OSS Distributions.Fa &fdset_orig .
135*1b191cb5SApple OSS DistributionsThe behavior of these macros is undefined if
136*1b191cb5SApple OSS Distributionsa descriptor value is less than zero or greater than or equal to
137*1b191cb5SApple OSS Distributions.Dv FD_SETSIZE ,
138*1b191cb5SApple OSS Distributionswhich is normally at least equal
139*1b191cb5SApple OSS Distributionsto the maximum number of descriptors supported by the system.
140*1b191cb5SApple OSS Distributions.Pp
141*1b191cb5SApple OSS DistributionsIf
142*1b191cb5SApple OSS Distributions.Fa timeout
143*1b191cb5SApple OSS Distributionsis not a null pointer, it specifies a maximum interval to wait for the
144*1b191cb5SApple OSS Distributionsselection to complete.
145*1b191cb5SApple OSS Distributions.Pp
146*1b191cb5SApple OSS DistributionsIf
147*1b191cb5SApple OSS Distributions.Fa timeout
148*1b191cb5SApple OSS Distributionsis a null pointer, the select blocks indefinitely.
149*1b191cb5SApple OSS Distributions.Pp
150*1b191cb5SApple OSS DistributionsTo effect a poll, the
151*1b191cb5SApple OSS Distributions.Fa timeout
152*1b191cb5SApple OSS Distributionsargument should be not be a null pointer,
153*1b191cb5SApple OSS Distributionsbut it should point to a zero-valued timeval structure.
154*1b191cb5SApple OSS Distributions.Pp
155*1b191cb5SApple OSS Distributions.Fa timeout
156*1b191cb5SApple OSS Distributionsis not changed by
157*1b191cb5SApple OSS Distributions.Fn select ,
158*1b191cb5SApple OSS Distributionsand may be reused on subsequent calls, however it is good style to re-initialize
159*1b191cb5SApple OSS Distributionsit before each invocation of
160*1b191cb5SApple OSS Distributions.Fn select .
161*1b191cb5SApple OSS Distributions.Pp
162*1b191cb5SApple OSS DistributionsAny of
163*1b191cb5SApple OSS Distributions.Fa readfds ,
164*1b191cb5SApple OSS Distributions.Fa writefds ,
165*1b191cb5SApple OSS Distributionsand
166*1b191cb5SApple OSS Distributions.Fa errorfds
167*1b191cb5SApple OSS Distributionsmay be given as null pointers if no descriptors are of interest.
168*1b191cb5SApple OSS Distributions.Sh RETURN VALUES
169*1b191cb5SApple OSS Distributions.Fn select
170*1b191cb5SApple OSS Distributionsreturns the number of ready descriptors that are contained in
171*1b191cb5SApple OSS Distributionsthe descriptor sets,
172*1b191cb5SApple OSS Distributionsor -1 if an error occurred.
173*1b191cb5SApple OSS DistributionsIf the time limit expires,
174*1b191cb5SApple OSS Distributions.Fn select
175*1b191cb5SApple OSS Distributionsreturns 0.
176*1b191cb5SApple OSS DistributionsIf
177*1b191cb5SApple OSS Distributions.Fn select
178*1b191cb5SApple OSS Distributionsreturns with an error,
179*1b191cb5SApple OSS Distributionsincluding one due to an interrupted call,
180*1b191cb5SApple OSS Distributionsthe descriptor sets will be unmodified and the global variable
181*1b191cb5SApple OSS Distributions.Va errno
182*1b191cb5SApple OSS Distributionswill be set to indicate the error.
183*1b191cb5SApple OSS Distributions.Sh ERRORS
184*1b191cb5SApple OSS DistributionsAn error return from
185*1b191cb5SApple OSS Distributions.Fn select
186*1b191cb5SApple OSS Distributionsindicates:
187*1b191cb5SApple OSS Distributions.Bl -tag -width Er
188*1b191cb5SApple OSS Distributions.\" ===========
189*1b191cb5SApple OSS Distributions.It Bq Er EAGAIN
190*1b191cb5SApple OSS DistributionsThe kernel was (perhaps temporarily) unable
191*1b191cb5SApple OSS Distributionsto allocate the requested number of file descriptors.
192*1b191cb5SApple OSS Distributions.\" ===========
193*1b191cb5SApple OSS Distributions.It Bq Er EBADF
194*1b191cb5SApple OSS DistributionsOne of the descriptor sets specified an invalid descriptor.
195*1b191cb5SApple OSS Distributions.\" ===========
196*1b191cb5SApple OSS Distributions.It Bq Er EINTR
197*1b191cb5SApple OSS DistributionsA signal was delivered before the time limit expired and
198*1b191cb5SApple OSS Distributionsbefore any of the selected events occurred.
199*1b191cb5SApple OSS Distributions.\" ===========
200*1b191cb5SApple OSS Distributions.It Bq Er EINVAL
201*1b191cb5SApple OSS DistributionsThe specified time limit is invalid.  One of its components is
202*1b191cb5SApple OSS Distributionsnegative or too large.
203*1b191cb5SApple OSS Distributions.\" ===========
204*1b191cb5SApple OSS Distributions.It Bq Er EINVAL
205*1b191cb5SApple OSS Distributions.Fa ndfs
206*1b191cb5SApple OSS Distributionsis greater than FD_SETSIZE and _DARWIN_UNLIMITED_SELECT is not defined.
207*1b191cb5SApple OSS Distributions.El
208*1b191cb5SApple OSS Distributions.Sh LEGACY SYNOPSIS
209*1b191cb5SApple OSS Distributions.Fd #include <sys/select.h>
210*1b191cb5SApple OSS Distributions.D1 "- or -"
211*1b191cb5SApple OSS Distributions.Fd #include <sys/types.h>
212*1b191cb5SApple OSS Distributions.Fd #include <sys/time.h>
213*1b191cb5SApple OSS Distributions.Fd #include <unistd.h>
214*1b191cb5SApple OSS Distributions.Pp
215*1b191cb5SApple OSS Distributions.Fo FD_SET
216*1b191cb5SApple OSS Distributions.Fa fd
217*1b191cb5SApple OSS Distributions.Fa &fdset
218*1b191cb5SApple OSS Distributions.Fc ;
219*1b191cb5SApple OSS Distributions.Pp
220*1b191cb5SApple OSS Distributions.Fo FD_CLR
221*1b191cb5SApple OSS Distributions.Fa fd
222*1b191cb5SApple OSS Distributions.Fa &fdset
223*1b191cb5SApple OSS Distributions.Fc ;
224*1b191cb5SApple OSS Distributions.Pp
225*1b191cb5SApple OSS Distributions.Fo FD_ISSET
226*1b191cb5SApple OSS Distributions.Fa fd
227*1b191cb5SApple OSS Distributions.Fa &fdset
228*1b191cb5SApple OSS Distributions.Fc ;
229*1b191cb5SApple OSS Distributions.Pp
230*1b191cb5SApple OSS Distributions.Fo FD_COPY
231*1b191cb5SApple OSS Distributions.Fa &fdset_orig
232*1b191cb5SApple OSS Distributions.Fa &fdset_copy
233*1b191cb5SApple OSS Distributions.Fc ;
234*1b191cb5SApple OSS Distributions.Pp
235*1b191cb5SApple OSS Distributions.Fo FD_ZERO
236*1b191cb5SApple OSS Distributions.Fa &fdset
237*1b191cb5SApple OSS Distributions.Fc ;
238*1b191cb5SApple OSS Distributions.Sh COMPATIBILITY
239*1b191cb5SApple OSS Distributions.Fn select
240*1b191cb5SApple OSS Distributionsnow returns with
241*1b191cb5SApple OSS Distributions.Va errno
242*1b191cb5SApple OSS Distributionsset to EINVAL when
243*1b191cb5SApple OSS Distributions.Fa nfds
244*1b191cb5SApple OSS Distributionsis greater than FD_SETSIZE.
245*1b191cb5SApple OSS DistributionsUse a smaller value for
246*1b191cb5SApple OSS Distributions.Fa nfds
247*1b191cb5SApple OSS Distributionsor compile with -D_DARWIN_UNLIMITED_SELECT.
248*1b191cb5SApple OSS Distributions.Sh SEE ALSO
249*1b191cb5SApple OSS Distributions.Xr accept 2 ,
250*1b191cb5SApple OSS Distributions.Xr connect 2 ,
251*1b191cb5SApple OSS Distributions.Xr connectx 2 ,
252*1b191cb5SApple OSS Distributions.Xr getdtablesize 2 ,
253*1b191cb5SApple OSS Distributions.Xr gettimeofday 2 ,
254*1b191cb5SApple OSS Distributions.Xr read 2 ,
255*1b191cb5SApple OSS Distributions.Xr recv 2 ,
256*1b191cb5SApple OSS Distributions.Xr send 2 ,
257*1b191cb5SApple OSS Distributions.Xr write 2 ,
258*1b191cb5SApple OSS Distributions.Xr compat 5
259*1b191cb5SApple OSS Distributions.Sh BUGS
260*1b191cb5SApple OSS DistributionsAlthough the provision of
261*1b191cb5SApple OSS Distributions.Xr getdtablesize 2
262*1b191cb5SApple OSS Distributionswas intended to allow user programs to be written independent
263*1b191cb5SApple OSS Distributionsof the kernel limit on the number of open files, the dimension
264*1b191cb5SApple OSS Distributionsof a sufficiently large bit field for select remains a problem.
265*1b191cb5SApple OSS DistributionsThe default size
266*1b191cb5SApple OSS Distributions.Dv FD_SETSIZE
267*1b191cb5SApple OSS Distributions(currently 1024) is somewhat smaller than
268*1b191cb5SApple OSS Distributionsthe current kernel limit to the number of open files.
269*1b191cb5SApple OSS DistributionsHowever, in order to accommodate programs which might potentially
270*1b191cb5SApple OSS Distributionsuse a larger number of open files with select, it is possible
271*1b191cb5SApple OSS Distributionsto increase this size within a program by providing
272*1b191cb5SApple OSS Distributionsa larger definition of
273*1b191cb5SApple OSS Distributions.Dv FD_SETSIZE
274*1b191cb5SApple OSS Distributionsbefore the inclusion of
275*1b191cb5SApple OSS Distributions.Aq Pa sys/types.h .
276*1b191cb5SApple OSS Distributions.Pp
277*1b191cb5SApple OSS Distributions.Fn select
278*1b191cb5SApple OSS Distributionsshould probably have been designed to return the time remaining from the
279*1b191cb5SApple OSS Distributionsoriginal timeout, if any, by modifying the time value in place.
280*1b191cb5SApple OSS DistributionsHowever, it is unlikely this semantic will ever be implemented, as the
281*1b191cb5SApple OSS Distributionschange would cause source code compatibility problems.
282*1b191cb5SApple OSS DistributionsIn general it is unwise to assume that the timeout value will be
283*1b191cb5SApple OSS Distributionsunmodified by the
284*1b191cb5SApple OSS Distributions.Fn select
285*1b191cb5SApple OSS Distributionscall, and the caller should reinitialize it on each invocation.
286*1b191cb5SApple OSS Distributions.Sh HISTORY
287*1b191cb5SApple OSS DistributionsThe
288*1b191cb5SApple OSS Distributions.Fn select
289*1b191cb5SApple OSS Distributionsfunction call appeared in
290*1b191cb5SApple OSS Distributions.Bx 4.2 .
291