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