xref: /openbsd-src/lib/libc/sys/select.2 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\"	$OpenBSD: select.2,v 1.19 2001/03/03 21:32:35 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. All advertising materials mentioning features or use of this software
16.\"    must display the following acknowledgement:
17.\"	This product includes software developed by the University of
18.\"	California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"     @(#)select.2	8.2 (Berkeley) 3/25/94
36.\"
37.Dd March 25, 1994
38.Dt SELECT 2
39.Os
40.Sh NAME
41.Nm select
42.Nd synchronous I/O multiplexing
43.Sh SYNOPSIS
44.Fd #include <sys/types.h>
45.Fd #include <sys/time.h>
46.Fd #include <unistd.h>
47.Ft int
48.Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout"
49.Fn FD_SET fd &fdset
50.Fn FD_CLR fd &fdset
51.Fn FD_ISSET fd &fdset
52.Fn FD_ZERO &fdset
53.Sh DESCRIPTION
54.Fn select
55examines the I/O descriptor sets whose addresses are passed in
56.Fa readfds ,
57.Fa writefds ,
58and
59.Fa exceptfds
60to see if some of their descriptors
61are ready for reading, are ready for writing, or have an exceptional
62condition pending, respectively.
63The first
64.Fa nfds
65descriptors are checked in each set;
66i.e., the descriptors from 0 through
67.Fa nfds Ns No -1
68in the descriptor sets are examined.
69On return,
70.Fn select
71replaces the given descriptor sets
72with subsets consisting of those descriptors that are ready
73for the requested operation.
74.Fn select
75returns the total number of ready descriptors in all the sets.
76.Pp
77The descriptor sets are stored as bit fields in arrays of integers.
78The following macros are provided for manipulating such descriptor sets:
79.Fn FD_ZERO &fdset
80initializes a descriptor set
81.Fa fdset
82to the null set.
83.Fn FD_SET fd &fdset
84includes a particular descriptor
85.Fa fd
86in
87.Fa fdset .
88.Fn FD_CLR fd &fdset
89removes
90.Fa fd
91from
92.Fa fdset .
93.Fn FD_ISSET fd &fdset
94is non-zero if
95.Fa fd
96is a member of
97.Fa fdset ,
98zero otherwise.
99The behavior of these macros is undefined if
100a descriptor value is less than zero or greater than or equal to
101.Dv FD_SETSIZE ,
102which is normally at least equal
103to the maximum number of descriptors supported by the system.
104.Pp
105If
106.Fa timeout
107is a non-null pointer, it specifies a maximum interval to wait for the
108selection to complete.
109If
110.Fa timeout
111is a null pointer, the select blocks indefinitely.
112To effect a poll, the
113.Fa timeout
114argument should be non-null, pointing to a zero-valued timeval structure.
115.Fa timeout
116is not changed by
117.Fn select ,
118and may be reused on subsequent calls; however, it is good style to
119re-initialize it before each invocation of
120.Fn select .
121.Pp
122Any of
123.Fa readfds ,
124.Fa writefds ,
125and
126.Fa exceptfds
127may be given as null pointers if no descriptors are of interest.
128.Sh RETURN VALUES
129.Fn select
130returns the number of ready descriptors that are contained in
131the descriptor sets, or \-1 is an error occurred.
132If the time limit expires,
133.Fn select
134returns 0.
135If
136.Fn select
137returns with an error, including one due to an interrupted 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 EFAULT
145One or more of
146.Fa readfds ,
147.Fa writefds ,
148or
149.Fa exceptfds
150points outside the process's allocated address space.
151.It Bq Er EBADF
152One of the descriptor sets specified an invalid descriptor.
153.It Bq Er EINTR
154A signal was delivered before the time limit expired and
155before any of the selected events occurred.
156.It Bq Er EINVAL
157The specified time limit is invalid.
158One of its components is negative or too large.
159.El
160.Sh SEE ALSO
161.Xr accept 2 ,
162.Xr connect 2 ,
163.Xr gettimeofday 2 ,
164.Xr poll 2 ,
165.Xr read 2 ,
166.Xr recv 2 ,
167.Xr send 2 ,
168.Xr write 2 ,
169.Xr getdtablesize 3
170.Sh BUGS
171Although the provision of
172.Xr getdtablesize 3
173was intended to allow user programs to be written independent
174of the kernel limit on the number of open files, the dimension
175of a sufficiently large bit field for select remains a problem.
176The default size
177.Dv FD_SETSIZE
178(currently 256) is somewhat smaller than
179the current kernel limit to the number of open files.
180However, in order to accommodate programs which might potentially
181use a larger number of open files with select, it is possible
182to increase this size within a program by providing
183a larger definition of
184.Dv FD_SETSIZE
185before the inclusion of
186.Aq Pa sys/types.h .
187The kernel will cope, and the userland libraries provided with the
188system are also ready for large numbers of file descriptors.
189.Pp
190Alternatively, to be really safe, it is possible to allocate
191.Ft fd_set
192bit-arrays dynamically.
193The idea is to permit a program to work properly even if it is
194.Xr execve 2 Ns 'd
195with 4000 file descriptors pre-allocated.
196The following illustrates the technique which is used by
197userland libraries:
198.Pp
199.Bd -literal -offset indent -compact
200	fd_set *fdsr;
201	int max = fd;
202
203	fdsr = (fd_set *)calloc(howmany(max+1, NFDBITS),
204	    sizeof(fd_mask));
205	if (fdsr == NULL) {
206		...
207		return (-1);
208	}
209	FD_SET(fd, fdsr);
210	n = select(max+1, fdsr, NULL, NULL, &tv);
211	...
212	free(fdsr);
213.Ed
214.Pp
215Alternatively, it is possible to use the
216.Xr poll 2
217interface.
218.Xr poll 2
219is more efficient when the size of
220.Fn select Ns 's
221.Ft fd_set
222bit-arrays are very large, and for fixed numbers of
223file descriptors one need not size and dynamically allocate a
224memory object.
225.Pp
226.Fn select
227should probably have been designed to return the time remaining from the
228original timeout, if any, by modifying the time value in place.
229Even though some systems stupidly act in this different way, it is
230unlikely this semantic will ever be commonly implemented, as the
231change causes massive source code compatibility problems.
232Furthermore, recent new standards have dictated the current behaviour.
233In general, due to the existence of those brain-damaged
234non-conforming systems, it is unwise to assume that the timeout
235value will be unmodified by the
236.Fn select
237call, and the caller should reinitialize it on each invocation.
238Calculating the delta is easily done by calling
239.Xr gettimeofday 2
240before and after the call to
241.Fn select Ns ,
242and using
243.Fn timersub
244(as described in
245.Xr getitimer 2 ) .
246.Pp
247Internally to the kernel,
248.Fn select
249works poorly if multiple processes wait on the same file descriptor.
250Given that, it is rather surprising to see that many daemons are
251written that way (i.e.,
252.Xr httpd 8 ) .
253.Sh HISTORY
254The
255.Fn select
256function call appeared in
257.Bx 4.2 .
258