1.\" $OpenBSD: select.2,v 1.43 2020/08/13 01:00:03 cheloha 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 $Mdocdate: August 13 2020 $ 34.Dt SELECT 2 35.Os 36.Sh NAME 37.Nm select , 38.Nm pselect , 39.Nm FD_SET , 40.Nm FD_CLR , 41.Nm FD_ISSET , 42.Nm FD_ZERO 43.Nd synchronous I/O multiplexing 44.Sh SYNOPSIS 45.In sys/select.h 46.Ft int 47.Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout" 48.Ft int 49.Fn pselect "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "const struct timespec *timeout" "const sigset_t *mask" 50.Fn FD_SET fd &fdset 51.Fn FD_CLR fd &fdset 52.Fn FD_ISSET fd &fdset 53.Fn FD_ZERO &fdset 54.Sh DESCRIPTION 55.Fn select 56examines the I/O descriptor sets whose addresses are passed in 57.Fa readfds , 58.Fa writefds , 59and 60.Fa exceptfds 61to see if some of their descriptors 62are ready for reading, are ready for writing, or have an exceptional 63condition pending, respectively. 64Exceptional conditions include the presence of out-of-band data 65on a socket. 66The first 67.Fa nfds 68descriptors are checked in each set; 69i.e., the descriptors from 0 through 70.Fa nfds Ns -1 71in the descriptor sets are examined. 72On return, 73.Fn select 74replaces the given descriptor sets 75with subsets consisting of those descriptors that are ready 76for the requested operation. 77.Fn select 78returns the total number of ready descriptors in all the sets. 79.Pp 80The descriptor sets are stored as bit fields in arrays of integers. 81The following macros are provided for manipulating such descriptor sets: 82.Fn FD_ZERO &fdset 83initializes a descriptor set 84.Fa fdset 85to the null set. 86.Fn FD_SET fd &fdset 87includes a particular descriptor 88.Fa fd 89in 90.Fa fdset . 91.Fn FD_CLR fd &fdset 92removes 93.Fa fd 94from 95.Fa fdset . 96.Fn FD_ISSET fd &fdset 97is non-zero if 98.Fa fd 99is a member of 100.Fa fdset , 101zero otherwise. 102The behavior of these macros is undefined if 103a descriptor value is less than zero or greater than or equal to 104.Dv FD_SETSIZE , 105which is normally at least equal 106to the maximum number of descriptors supported by the system. 107.Pp 108If 109.Fa timeout 110is a non-null pointer, it specifies a maximum interval to wait for the 111selection to complete. 112If 113.Fa timeout 114is a null pointer, the select blocks indefinitely. 115To effect a poll, the 116.Fa timeout 117argument should be non-null, pointing to a zero-valued timeval structure. 118.Fa timeout 119is not changed by 120.Fn select , 121and may be reused on subsequent calls; however, it is good style to 122re-initialize it before each invocation of 123.Fn select . 124.Pp 125Any of 126.Fa readfds , 127.Fa writefds , 128and 129.Fa exceptfds 130may be given as null pointers if no descriptors are of interest. 131.Pp 132The 133.Fn pselect 134function is similar to 135.Fn select 136except that it specifies the timeout using a timespec structure. 137Also, if 138.Fa mask 139is a non-null pointer, 140.Fn pselect 141atomically sets the calling thread's signal mask to the signal set 142pointed to by 143.Fa mask 144for the duration of the function call. 145In this case, the original signal mask will be restored before 146.Fn pselect 147returns. 148.Sh RETURN VALUES 149If successful, 150.Fn select 151and 152.Fn pselect 153return the number of ready descriptors that are contained in 154the descriptor sets. 155If a descriptor is included in multiple descriptor sets, 156each inclusion is counted separately. 157If the time limit expires before any descriptors become ready, 158they return 0. 159.Pp 160Otherwise, if 161.Fn select 162or 163.Fn pselect 164return with an error, including one due to an interrupted call, 165they return \-1, 166and the descriptor sets will be unmodified. 167.Sh ERRORS 168An error return from 169.Fn select 170or 171.Fn pselect 172indicates: 173.Bl -tag -width Er 174.It Bq Er EFAULT 175One or more of 176.Fa readfds , 177.Fa writefds , 178or 179.Fa exceptfds 180points outside the process's allocated address space. 181.It Bq Er EBADF 182One of the descriptor sets specified an invalid descriptor. 183.It Bq Er EINTR 184A signal was delivered before the time limit expired and 185before any of the selected descriptors became ready. 186.It Bq Er EINVAL 187The specified time limit is invalid. 188One of its components is negative or too large. 189.It Bq Er EINVAL 190.Fa nfds 191was less than 0. 192.El 193.Sh SEE ALSO 194.Xr accept 2 , 195.Xr clock_gettime 2 , 196.Xr connect 2 , 197.Xr gettimeofday 2 , 198.Xr poll 2 , 199.Xr read 2 , 200.Xr recv 2 , 201.Xr send 2 , 202.Xr write 2 , 203.Xr getdtablesize 3 204.Sh STANDARDS 205The 206.Fn select 207and 208.Fn pselect 209functions conform to 210.St -p1003.1-2008 . 211.Sh HISTORY 212The 213.Fn select 214system call first appeared in 215.Bx 4.1c . 216The 217.Fn pselect 218system call has been available since 219.Ox 5.4 . 220.Sh BUGS 221Although the provision of 222.Xr getdtablesize 3 223was intended to allow user programs to be written independent 224of the kernel limit on the number of open files, the dimension 225of a sufficiently large bit field for select remains a problem. 226The default bit size of 227.Ft fd_set 228is based on the symbol 229.Dv FD_SETSIZE 230(currently 1024), 231but that is somewhat smaller than the current kernel limit 232to the number of open files. 233However, in order to accommodate programs which might potentially 234use a larger number of open files with select, it is possible 235to increase this size within a program by providing 236a larger definition of 237.Dv FD_SETSIZE 238before the inclusion of any headers. 239The kernel will cope, and the userland libraries provided with the 240system are also ready for large numbers of file descriptors. 241.Pp 242Alternatively, to be really safe, it is possible to allocate 243.Ft fd_set 244bit-arrays dynamically. 245The idea is to permit a program to work properly even if it is 246.Xr execve 2 Ns 'd 247with 4000 file descriptors pre-allocated. 248The following illustrates the technique which is used by 249userland libraries: 250.Bd -literal -offset indent 251fd_set *fdsr; 252int max = fd; 253 254fdsr = calloc(howmany(max+1, NFDBITS), sizeof(fd_mask)); 255if (fdsr == NULL) { 256 ... 257 return (-1); 258} 259FD_SET(fd, fdsr); 260n = select(max+1, fdsr, NULL, NULL, &tv); 261\&... 262free(fdsr); 263.Ed 264.Pp 265Alternatively, it is possible to use the 266.Xr poll 2 267interface. 268.Xr poll 2 269is more efficient when the size of 270.Fn select Ns 's 271.Ft fd_set 272bit-arrays are very large, and for fixed numbers of 273file descriptors one need not size and dynamically allocate a 274memory object. 275.Pp 276.Fn select 277should probably have been designed to return the time remaining from the 278original timeout, if any, by modifying the time value in place. 279Even though some systems stupidly act in this different way, it is 280unlikely this semantic will ever be commonly implemented, as the 281change causes massive source code compatibility problems. 282Furthermore, recent new standards have dictated the current behaviour. 283In general, due to the existence of those brain-damaged 284non-conforming systems, it is unwise to assume that the timeout 285value will be unmodified by the 286.Fn select 287call, and the caller should reinitialize it on each invocation. 288Calculating the delta is easily done by calling 289.Xr gettimeofday 2 290before and after the call to 291.Fn select , 292and using 293.Xr timersub 3 . 294.Pp 295Internally to the kernel, 296.Fn select 297and 298.Fn pselect 299work poorly if multiple processes wait on the same file descriptor. 300