xref: /freebsd/lib/libsys/select.2 (revision a91a2465)
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.Dd June 25, 2020
29.Dt SELECT 2
30.Os
31.Sh NAME
32.Nm select
33.Nd synchronous I/O multiplexing
34.Sh LIBRARY
35.Lb libc
36.Sh SYNOPSIS
37.In sys/select.h
38.Ft int
39.Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout"
40.Fn FD_SET fd &fdset
41.Fn FD_CLR fd &fdset
42.Fn FD_ISSET fd &fdset
43.Fn FD_ZERO &fdset
44.Sh DESCRIPTION
45The
46.Fn select
47system call
48examines the I/O descriptor sets whose addresses are passed in
49.Fa readfds ,
50.Fa writefds ,
51and
52.Fa exceptfds
53to see if some of their descriptors
54are ready for reading, are ready for writing, or have an exceptional
55condition pending, respectively.
56The only exceptional condition detectable is out-of-band
57data received on a socket.
58The first
59.Fa nfds
60descriptors are checked in each set;
61i.e., the descriptors from 0 through
62.Fa nfds Ns No -1
63in the descriptor sets are examined.
64On return,
65.Fn select
66replaces the given descriptor sets
67with subsets consisting of those descriptors that are ready
68for the requested operation.
69The
70.Fn select
71system call
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 not a null pointer, it specifies the maximum interval to wait for the
105selection to complete.
106System activity can lengthen the interval by
107an indeterminate amount.
108.Pp
109If
110.Fa timeout
111is a null pointer, the select blocks indefinitely.
112.Pp
113To effect a poll, the
114.Fa timeout
115argument should not be a null pointer,
116but it should point to a zero-valued timeval structure.
117.Pp
118Any of
119.Fa readfds ,
120.Fa writefds ,
121and
122.Fa exceptfds
123may be given as null pointers if no descriptors are of interest.
124.Sh RETURN VALUES
125The
126.Fn select
127system call
128returns the number of ready descriptors that are contained in
129the descriptor sets,
130or -1 if an error occurred.
131If the time limit expires,
132.Fn select
133returns 0.
134If
135.Fn select
136returns with an error,
137including one due to an interrupted system call,
138the descriptor sets will be unmodified.
139.Sh ERRORS
140An error return from
141.Fn select
142indicates:
143.Bl -tag -width Er
144.It Bq Er EBADF
145One of the descriptor sets specified an invalid descriptor.
146.It Bq Er EFAULT
147One of the arguments
148.Fa readfds , writefds , exceptfds ,
149or
150.Fa timeout
151points to an invalid address.
152.It Bq Er EINTR
153A signal was delivered before the time limit expired and
154before any of the selected events occurred.
155.It Bq Er EINVAL
156The specified time limit is invalid.
157One of its components is
158negative or too large.
159.It Bq Er EINVAL
160The
161.Fa nfds
162argument
163was invalid.
164.El
165.Sh SEE ALSO
166.Xr accept 2 ,
167.Xr connect 2 ,
168.Xr getdtablesize 2 ,
169.Xr gettimeofday 2 ,
170.Xr kqueue 2 ,
171.Xr poll 2 ,
172.Xr pselect 2 ,
173.Xr read 2 ,
174.Xr recv 2 ,
175.Xr send 2 ,
176.Xr write 2 ,
177.Xr clocks 7
178.Sh NOTES
179The default size of
180.Dv FD_SETSIZE
181is currently 1024.
182In order to accommodate programs which might potentially
183use a larger number of open files with
184.Fn select ,
185it is possible
186to increase this size by having the program define
187.Dv FD_SETSIZE
188before the inclusion of any header which includes
189.In sys/types.h .
190.Pp
191If
192.Fa nfds
193is greater than the number of open files,
194.Fn select
195is not guaranteed to examine the unused file descriptors.
196For historical
197reasons,
198.Fn select
199will always examine the first 256 descriptors.
200.Sh STANDARDS
201The
202.Fn select
203system call and
204.Fn FD_CLR ,
205.Fn FD_ISSET ,
206.Fn FD_SET ,
207and
208.Fn FD_ZERO
209macros conform with
210.St -p1003.1-2001 .
211.Sh HISTORY
212The
213.Fn select
214system call appeared in
215.Bx 4.2 .
216.Sh BUGS
217.St -susv2
218allows systems to modify the original timeout in place.
219Thus, it is unwise to assume that the timeout value will be unmodified
220by the
221.Fn select
222system call.
223.Fx
224does not modify the return value, which can cause problems for applications
225ported from other systems.
226