xref: /freebsd-14.2/lib/libc/sys/select.2 (revision b2c76c41)
1.\" Copyright (c) 1983, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)select.2	8.2 (Berkeley) 3/25/94
29.\"
30.Dd June 25, 2020
31.Dt SELECT 2
32.Os
33.Sh NAME
34.Nm select
35.Nd synchronous I/O multiplexing
36.Sh LIBRARY
37.Lb libc
38.Sh SYNOPSIS
39.In sys/select.h
40.Ft int
41.Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout"
42.Fn FD_SET fd &fdset
43.Fn FD_CLR fd &fdset
44.Fn FD_ISSET fd &fdset
45.Fn FD_ZERO &fdset
46.Sh DESCRIPTION
47The
48.Fn select
49system call
50examines the I/O descriptor sets whose addresses are passed in
51.Fa readfds ,
52.Fa writefds ,
53and
54.Fa exceptfds
55to see if some of their descriptors
56are ready for reading, are ready for writing, or have an exceptional
57condition pending, respectively.
58The only exceptional condition detectable is out-of-band
59data received on a socket.
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.
71The
72.Fn select
73system call
74returns the total number of ready descriptors in all the sets.
75.Pp
76The descriptor sets are stored as bit fields in arrays of integers.
77The following macros are provided for manipulating such descriptor sets:
78.Fn FD_ZERO &fdset
79initializes a descriptor set
80.Fa fdset
81to the null set.
82.Fn FD_SET fd &fdset
83includes a particular descriptor
84.Fa fd
85in
86.Fa fdset .
87.Fn FD_CLR fd &fdset
88removes
89.Fa fd
90from
91.Fa fdset .
92.Fn FD_ISSET fd &fdset
93is non-zero if
94.Fa fd
95is a member of
96.Fa fdset ,
97zero otherwise.
98The behavior of these macros is undefined if
99a descriptor value is less than zero or greater than or equal to
100.Dv FD_SETSIZE ,
101which is normally at least equal
102to the maximum number of descriptors supported by the system.
103.Pp
104If
105.Fa timeout
106is not a null pointer, it specifies the maximum interval to wait for the
107selection to complete.
108System activity can lengthen the interval by
109an indeterminate amount.
110.Pp
111If
112.Fa timeout
113is a null pointer, the select blocks indefinitely.
114.Pp
115To effect a poll, the
116.Fa timeout
117argument should not be a null pointer,
118but it should point to a zero-valued timeval structure.
119.Pp
120Any of
121.Fa readfds ,
122.Fa writefds ,
123and
124.Fa exceptfds
125may be given as null pointers if no descriptors are of interest.
126.Sh RETURN VALUES
127The
128.Fn select
129system call
130returns the number of ready descriptors that are contained in
131the descriptor sets,
132or -1 if an error occurred.
133If the time limit expires,
134.Fn select
135returns 0.
136If
137.Fn select
138returns with an error,
139including one due to an interrupted system call,
140the descriptor sets will be unmodified.
141.Sh ERRORS
142An error return from
143.Fn select
144indicates:
145.Bl -tag -width Er
146.It Bq Er EBADF
147One of the descriptor sets specified an invalid descriptor.
148.It Bq Er EFAULT
149One of the arguments
150.Fa readfds , writefds , exceptfds ,
151or
152.Fa timeout
153points to an invalid address.
154.It Bq Er EINTR
155A signal was delivered before the time limit expired and
156before any of the selected events occurred.
157.It Bq Er EINVAL
158The specified time limit is invalid.
159One of its components is
160negative or too large.
161.It Bq Er EINVAL
162The
163.Fa nfds
164argument
165was invalid.
166.El
167.Sh SEE ALSO
168.Xr accept 2 ,
169.Xr connect 2 ,
170.Xr getdtablesize 2 ,
171.Xr gettimeofday 2 ,
172.Xr kqueue 2 ,
173.Xr poll 2 ,
174.Xr pselect 2 ,
175.Xr read 2 ,
176.Xr recv 2 ,
177.Xr send 2 ,
178.Xr write 2 ,
179.Xr clocks 7
180.Sh NOTES
181The default size of
182.Dv FD_SETSIZE
183is currently 1024.
184In order to accommodate programs which might potentially
185use a larger number of open files with
186.Fn select ,
187it is possible
188to increase this size by having the program define
189.Dv FD_SETSIZE
190before the inclusion of any header which includes
191.In sys/types.h .
192.Pp
193If
194.Fa nfds
195is greater than the number of open files,
196.Fn select
197is not guaranteed to examine the unused file descriptors.
198For historical
199reasons,
200.Fn select
201will always examine the first 256 descriptors.
202.Sh STANDARDS
203The
204.Fn select
205system call and
206.Fn FD_CLR ,
207.Fn FD_ISSET ,
208.Fn FD_SET ,
209and
210.Fn FD_ZERO
211macros conform with
212.St -p1003.1-2001 .
213.Sh HISTORY
214The
215.Fn select
216system call appeared in
217.Bx 4.2 .
218.Sh BUGS
219.St -susv2
220allows systems to modify the original timeout in place.
221Thus, it is unwise to assume that the timeout value will be unmodified
222by the
223.Fn select
224system call.
225.Fx
226does not modify the return value, which can cause problems for applications
227ported from other systems.
228