xref: /netbsd-src/lib/libc/sys/select.2 (revision aa73cae19608873cc4d1f712c4a0f8f8435f1ffa)
1.\"	$NetBSD: select.2,v 1.24 2005/02/26 18:04:19 matt 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.Ft int
47.Fn pselect "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "const struct timespec *timeout" "const sigset_t *sigmask"
48.Fn FD_SET fd \*[Am]fdset
49.Fn FD_CLR fd \*[Am]fdset
50.Fn FD_ISSET fd \*[Am]fdset
51.Fn FD_ZERO \*[Am]fdset
52.Sh DESCRIPTION
53.Fn select
54and
55.Fn pselect
56examine the I/O descriptor sets whose addresses are passed in
57.Fa readfds ,
58.Fa writefds ,
59and
60.Fa exceptfds
61to see if some of their descriptors
62are ready for reading, are ready for writing, or have an exceptional
63condition pending, respectively.
64The first
65.Fa nfds
66descriptors are checked in each set;
67i.e., the descriptors from 0 through
68.Fa nfds Ns No \-1
69in the descriptor sets are examined.
70On return,
71.Fn select
72and
73.Fn pselect
74replace the given descriptor sets
75with subsets consisting of those descriptors that are ready
76for the requested operation.
77.Fn select
78and
79.Fn pselect
80return the total number of ready descriptors in all the sets.
81.Pp
82The descriptor sets are stored as bit fields in arrays of integers.
83The following macros are provided for manipulating such descriptor sets:
84.Fn FD_ZERO \*[Am]fdset
85initializes a descriptor set
86.Fa fdset
87to the null set.
88.Fn FD_SET fd \*[Am]fdset
89includes a particular descriptor
90.Fa fd
91in
92.Fa fdset .
93.Fn FD_CLR fd \*[Am]fdset
94removes
95.Fa fd
96from
97.Fa fdset .
98.Fn FD_ISSET fd \*[Am]fdset
99is non-zero if
100.Fa fd
101is a member of
102.Fa fdset ,
103zero otherwise.
104The behavior of these macros is undefined if
105a descriptor value is less than zero or greater than or equal to
106.Dv FD_SETSIZE ,
107which is normally at least equal
108to the maximum number of descriptors supported by the system.
109.Pp
110If
111.Fa timeout
112is a non-nil pointer, it specifies a maximum interval to wait for the
113selection to complete.
114If
115.Fa timeout
116is a nil pointer, the select blocks indefinitely.
117To affect a poll, the
118.Fa timeout
119argument should be non-nil, pointing to a zero-valued timeval or timespec
120structure, as appropriate.
121.Fa timeout
122is not changed by
123.Fn select ,
124and may be reused on subsequent calls; however, it is good style to
125re-initialize it before each invocation of
126.Fn select .
127.Pp
128If
129.Fa sigmask
130is a non-nil pointer, then the
131.Fn pselect
132function shall replace the signal mask of the caller by the set of
133signals pointed to by
134.Fa sigmask
135before examining the descriptors, and shall restore the signal mask
136of the calling thread before returning.
137.Pp
138Any of
139.Fa readfds ,
140.Fa writefds ,
141and
142.Fa exceptfds
143may be given as nil pointers if no descriptors are of interest.
144.Sh RETURN VALUES
145.Fn select
146returns the number of ready descriptors that are contained in
147the descriptor sets,
148or \-1 if an error occurred.
149If the time limit expires,
150.Fn select
151returns 0.
152If
153.Fn select
154returns with an error,
155including one due to an interrupted call,
156the descriptor sets will be unmodified.
157.Sh ERRORS
158An error return from
159.Fn select
160indicates:
161.Bl -tag -width Er
162.It Bq Er EFAULT
163One or more of
164.Fa readfds ,
165.Fa writefds ,
166or
167.Fa exceptfds
168points outside the process's allocated address space.
169.It Bq Er EBADF
170One of the descriptor sets specified an invalid descriptor.
171.It Bq Er EINTR
172A signal was delivered before the time limit expired and
173before any of the selected events occurred.
174.It Bq Er EINVAL
175The specified time limit is invalid.
176One of its components is negative or too large.
177.El
178.Sh SEE ALSO
179.Xr accept 2 ,
180.Xr connect 2 ,
181.Xr gettimeofday 2 ,
182.Xr poll 2 ,
183.Xr read 2 ,
184.Xr recv 2 ,
185.Xr send 2 ,
186.Xr write 2 ,
187.Xr getdtablesize 3
188.Sh HISTORY
189The
190.Fn select
191function call appeared in
192.Bx 4.2 .
193.Sh BUGS
194Although the provision of
195.Xr getdtablesize 3
196was intended to allow user programs to be written independent
197of the kernel limit on the number of open files, the dimension
198of a sufficiently large bit field for select remains a problem.
199The default bit size of
200.Ft fd_set
201is based on the symbol
202.Dv FD_SETSIZE
203(currently 256),
204but that is somewhat smaller than the current kernel limit
205to the number of open files.
206However, in order to accommodate programs which might potentially
207use a larger number of open files with select, it is possible
208to increase this size within a program by providing
209a larger definition of
210.Dv FD_SETSIZE
211before the inclusion of
212.Aq Pa sys/types.h .
213The kernel will cope, and the userland libraries provided with the
214system are also ready for large numbers of file descriptors.
215.Pp
216Note:
217.Xr rpc 3
218library uses
219.Ft fd_set
220with the default
221.Dv FD_SETSIZE
222as part of its ABI.
223Therefore, programs that use
224.Xr rpc 3
225routines cannot change
226.Dv FD_SETSIZE .
227.Pp
228Alternatively, to be really safe, it is possible to allocate
229.Ft fd_set
230bit-arrays dynamically.
231The idea is to permit a program to work properly even if it is
232.Xr execve 2 Ns 'd
233with 4000 file descriptors pre-allocated.
234The following illustrates the technique which is used by
235userland libraries:
236.Pp
237.Bd -literal -offset indent -compact
238	fd_set *fdsr;
239	int max = fd;
240
241	fdsr = (fd_set *)calloc(howmany(max+1, NFDBITS),
242	    sizeof(fd_mask));
243	if (fdsr == NULL) {
244		...
245		return (-1);
246	}
247	FD_SET(fd, fdsr);
248	n = select(max+1, fdsr, NULL, NULL, \*[Am]tv);
249	...
250	free(fdsr);
251.Ed
252.Pp
253Alternatively, it is possible to use the
254.Xr poll 2
255interface.
256.Xr poll 2
257is more efficient when the size of
258.Fn select Ns 's
259.Ft fd_set
260bit-arrays are very large, and for fixed numbers of
261file descriptors one need not size and dynamically allocate a
262memory object.
263.Pp
264.Fn select
265should probably have been designed to return the time remaining from the
266original timeout, if any, by modifying the time value in place.
267Even though some systems stupidly act in this different way, it is
268unlikely this semantic will ever be commonly implemented, as the
269change causes massive source code compatibility problems.
270Furthermore, recent new standards have dictated the current behaviour.
271In general, due to the existence of those
272non-conforming systems, it is unwise to assume that the timeout
273value will be unmodified by the
274.Fn select
275call, and the caller should reinitialize it on each invocation.
276Calculating the delta is easily done by calling
277.Xr gettimeofday 2
278before and after the call to
279.Fn select ,
280and using
281.Fn timersub
282(as described in
283.Xr getitimer 2 ) .
284.Pp
285Internally to the kernel,
286.Fn select
287works poorly if multiple processes wait on the same file descriptor.
288