1.\" $OpenBSD: select.2,v 1.45 2022/01/21 16:18:16 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. 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: January 21 2022 $ 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. 226If descriptor values greater than FD_SETSIZE are possible in 227a program, use 228.Xr poll 2 229instead. 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.Xr timersub 3 . 249