xref: /netbsd-src/lib/libc/sys/select.2 (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1.\"	$NetBSD: select.2,v 1.23 2004/05/13 10:20:58 wiz Exp $
2.\"
3.\" Copyright (c) 1983, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)select.2	8.2 (Berkeley) 3/25/94
31.\"
32.Dd October 14, 2003
33.Dt SELECT 2
34.Os
35.Sh NAME
36.Nm select
37.Nd synchronous I/O multiplexing
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In sys/types.h
42.In sys/time.h
43.In unistd.h
44.Ft int
45.Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout"
46.Fn FD_SET fd \*[Am]fdset
47.Fn FD_CLR fd \*[Am]fdset
48.Fn FD_ISSET fd \*[Am]fdset
49.Fn FD_ZERO \*[Am]fdset
50.Sh DESCRIPTION
51.Fn select
52examines the I/O descriptor sets whose addresses are passed in
53.Fa readfds ,
54.Fa writefds ,
55and
56.Fa exceptfds
57to see if some of their descriptors
58are ready for reading, are ready for writing, or have an exceptional
59condition pending, respectively.
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.
71.Fn select
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 \*[Am]fdset
77initializes a descriptor set
78.Fa fdset
79to the null set.
80.Fn FD_SET fd \*[Am]fdset
81includes a particular descriptor
82.Fa fd
83in
84.Fa fdset .
85.Fn FD_CLR fd \*[Am]fdset
86removes
87.Fa fd
88from
89.Fa fdset .
90.Fn FD_ISSET fd \*[Am]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 a non-nil pointer, it specifies a maximum interval to wait for the
105selection to complete.
106If
107.Fa timeout
108is a nil pointer, the select blocks indefinitely.
109To affect a poll, the
110.Fa timeout
111argument should be non-nil, pointing to a zero-valued timeval structure.
112.Fa timeout
113is not changed by
114.Fn select ,
115and may be reused on subsequent calls; however, it is good style to
116re-initialize it before each invocation of
117.Fn select .
118.Pp
119Any of
120.Fa readfds ,
121.Fa writefds ,
122and
123.Fa exceptfds
124may be given as nil pointers if no descriptors are of interest.
125.Sh RETURN VALUES
126.Fn select
127returns the number of ready descriptors that are contained in
128the descriptor sets,
129or \-1 if an error occurred.
130If the time limit expires,
131.Fn select
132returns 0.
133If
134.Fn select
135returns with an error,
136including one due to an interrupted call,
137the descriptor sets will be unmodified.
138.Sh ERRORS
139An error return from
140.Fn select
141indicates:
142.Bl -tag -width Er
143.It Bq Er EFAULT
144One or more of
145.Fa readfds ,
146.Fa writefds ,
147or
148.Fa exceptfds
149points outside the process's allocated address space.
150.It Bq Er EBADF
151One of the descriptor sets specified an invalid descriptor.
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 negative or too large.
158.El
159.Sh SEE ALSO
160.Xr accept 2 ,
161.Xr connect 2 ,
162.Xr gettimeofday 2 ,
163.Xr poll 2 ,
164.Xr read 2 ,
165.Xr recv 2 ,
166.Xr send 2 ,
167.Xr write 2 ,
168.Xr getdtablesize 3
169.Sh HISTORY
170The
171.Fn select
172function call appeared in
173.Bx 4.2 .
174.Sh BUGS
175Although the provision of
176.Xr getdtablesize 3
177was intended to allow user programs to be written independent
178of the kernel limit on the number of open files, the dimension
179of a sufficiently large bit field for select remains a problem.
180The default bit size of
181.Ft fd_set
182is based on the symbol
183.Dv FD_SETSIZE
184(currently 256),
185but that is somewhat smaller than the current kernel limit
186to the number of open files.
187However, in order to accommodate programs which might potentially
188use a larger number of open files with select, it is possible
189to increase this size within a program by providing
190a larger definition of
191.Dv FD_SETSIZE
192before the inclusion of
193.Aq Pa sys/types.h .
194The kernel will cope, and the userland libraries provided with the
195system are also ready for large numbers of file descriptors.
196.Pp
197Note:
198.Xr rpc 3
199library uses
200.Ft fd_set
201with the default
202.Dv FD_SETSIZE
203as part of its ABI.
204Therefore, programs that use
205.Xr rpc 3
206routines cannot change
207.Dv FD_SETSIZE .
208.Pp
209Alternatively, to be really safe, it is possible to allocate
210.Ft fd_set
211bit-arrays dynamically.
212The idea is to permit a program to work properly even if it is
213.Xr execve 2 Ns 'd
214with 4000 file descriptors pre-allocated.
215The following illustrates the technique which is used by
216userland libraries:
217.Pp
218.Bd -literal -offset indent -compact
219	fd_set *fdsr;
220	int max = fd;
221
222	fdsr = (fd_set *)calloc(howmany(max+1, NFDBITS),
223	    sizeof(fd_mask));
224	if (fdsr == NULL) {
225		...
226		return (-1);
227	}
228	FD_SET(fd, fdsr);
229	n = select(max+1, fdsr, NULL, NULL, \*[Am]tv);
230	...
231	free(fdsr);
232.Ed
233.Pp
234Alternatively, it is possible to use the
235.Xr poll 2
236interface.
237.Xr poll 2
238is more efficient when the size of
239.Fn select Ns 's
240.Ft fd_set
241bit-arrays are very large, and for fixed numbers of
242file descriptors one need not size and dynamically allocate a
243memory object.
244.Pp
245.Fn select
246should probably have been designed to return the time remaining from the
247original timeout, if any, by modifying the time value in place.
248Even though some systems stupidly act in this different way, it is
249unlikely this semantic will ever be commonly implemented, as the
250change causes massive source code compatibility problems.
251Furthermore, recent new standards have dictated the current behaviour.
252In general, due to the existence of those
253non-conforming systems, it is unwise to assume that the timeout
254value will be unmodified by the
255.Fn select
256call, and the caller should reinitialize it on each invocation.
257Calculating the delta is easily done by calling
258.Xr gettimeofday 2
259before and after the call to
260.Fn select ,
261and using
262.Fn timersub
263(as described in
264.Xr getitimer 2 ) .
265.Pp
266Internally to the kernel,
267.Fn select
268works poorly if multiple processes wait on the same file descriptor.
269