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