]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/select.2
zfs: merge openzfs/zfs@a382e2119
[FreeBSD/FreeBSD.git] / lib / libc / sys / select.2
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
45 The
46 .Fn select
47 system call
48 examines the I/O descriptor sets whose addresses are passed in
49 .Fa readfds ,
50 .Fa writefds ,
51 and
52 .Fa exceptfds
53 to see if some of their descriptors
54 are ready for reading, are ready for writing, or have an exceptional
55 condition pending, respectively.
56 The only exceptional condition detectable is out-of-band
57 data received on a socket.
58 The first
59 .Fa nfds
60 descriptors are checked in each set;
61 i.e., the descriptors from 0 through
62 .Fa nfds Ns No -1
63 in the descriptor sets are examined.
64 On return,
65 .Fn select
66 replaces the given descriptor sets
67 with subsets consisting of those descriptors that are ready
68 for the requested operation.
69 The
70 .Fn select
71 system call
72 returns the total number of ready descriptors in all the sets.
73 .Pp
74 The descriptor sets are stored as bit fields in arrays of integers.
75 The following macros are provided for manipulating such descriptor sets:
76 .Fn FD_ZERO &fdset
77 initializes a descriptor set
78 .Fa fdset
79 to the null set.
80 .Fn FD_SET fd &fdset
81 includes a particular descriptor
82 .Fa fd
83 in
84 .Fa fdset .
85 .Fn FD_CLR fd &fdset
86 removes
87 .Fa fd
88 from
89 .Fa fdset .
90 .Fn FD_ISSET fd &fdset
91 is non-zero if
92 .Fa fd
93 is a member of
94 .Fa fdset ,
95 zero otherwise.
96 The behavior of these macros is undefined if
97 a descriptor value is less than zero or greater than or equal to
98 .Dv FD_SETSIZE ,
99 which is normally at least equal
100 to the maximum number of descriptors supported by the system.
101 .Pp
102 If
103 .Fa timeout
104 is not a null pointer, it specifies the maximum interval to wait for the
105 selection to complete.
106 System activity can lengthen the interval by
107 an indeterminate amount.
108 .Pp
109 If
110 .Fa timeout
111 is a null pointer, the select blocks indefinitely.
112 .Pp
113 To effect a poll, the
114 .Fa timeout
115 argument should not be a null pointer,
116 but it should point to a zero-valued timeval structure.
117 .Pp
118 Any of
119 .Fa readfds ,
120 .Fa writefds ,
121 and
122 .Fa exceptfds
123 may be given as null pointers if no descriptors are of interest.
124 .Sh RETURN VALUES
125 The
126 .Fn select
127 system call
128 returns the number of ready descriptors that are contained in
129 the descriptor sets,
130 or -1 if an error occurred.
131 If the time limit expires,
132 .Fn select
133 returns 0.
134 If
135 .Fn select
136 returns with an error,
137 including one due to an interrupted system call,
138 the descriptor sets will be unmodified.
139 .Sh ERRORS
140 An error return from
141 .Fn select
142 indicates:
143 .Bl -tag -width Er
144 .It Bq Er EBADF
145 One of the descriptor sets specified an invalid descriptor.
146 .It Bq Er EFAULT
147 One of the arguments
148 .Fa readfds , writefds , exceptfds ,
149 or
150 .Fa timeout
151 points to an invalid address.
152 .It Bq Er EINTR
153 A signal was delivered before the time limit expired and
154 before any of the selected events occurred.
155 .It Bq Er EINVAL
156 The specified time limit is invalid.
157 One of its components is
158 negative or too large.
159 .It Bq Er EINVAL
160 The
161 .Fa nfds
162 argument
163 was 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
179 The default size of
180 .Dv FD_SETSIZE
181 is currently 1024.
182 In order to accommodate programs which might potentially
183 use a larger number of open files with
184 .Fn select ,
185 it is possible
186 to increase this size by having the program define
187 .Dv FD_SETSIZE
188 before the inclusion of any header which includes
189 .In sys/types.h .
190 .Pp
191 If
192 .Fa nfds
193 is greater than the number of open files,
194 .Fn select
195 is not guaranteed to examine the unused file descriptors.
196 For historical
197 reasons,
198 .Fn select
199 will always examine the first 256 descriptors.
200 .Sh STANDARDS
201 The
202 .Fn select
203 system call and
204 .Fn FD_CLR ,
205 .Fn FD_ISSET ,
206 .Fn FD_SET ,
207 and
208 .Fn FD_ZERO
209 macros conform with
210 .St -p1003.1-2001 .
211 .Sh HISTORY
212 The
213 .Fn select
214 system call appeared in
215 .Bx 4.2 .
216 .Sh BUGS
217 .St -susv2
218 allows systems to modify the original timeout in place.
219 Thus, it is unwise to assume that the timeout value will be unmodified
220 by the
221 .Fn select
222 system call.
223 .Fx
224 does not modify the return value, which can cause problems for applications
225 ported from other systems.