xref: /openbsd-src/lib/libc/sys/select.2 (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1.\"	$OpenBSD: select.2,v 1.37 2014/03/13 10:12:11 florian 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. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)select.2	8.2 (Berkeley) 3/25/94
32.\"
33.Dd $Mdocdate: March 13 2014 $
34.Dt SELECT 2
35.Os
36.Sh NAME
37.Nm select ,
38.Nm pselect
39.Nd synchronous I/O multiplexing
40.Sh SYNOPSIS
41.In sys/select.h
42.Ft int
43.Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout"
44.Ft int
45.Fn pselect "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "const struct timespec *timeout" "const sigset_t *mask"
46.Fn FD_SET fd &fdset
47.Fn FD_CLR fd &fdset
48.Fn FD_ISSET fd &fdset
49.Fn FD_ZERO &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 -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 &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 a non-null pointer, it specifies a maximum interval to wait for the
105selection to complete.
106If
107.Fa timeout
108is a null pointer, the select blocks indefinitely.
109To effect a poll, the
110.Fa timeout
111argument should be non-null, 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 null pointers if no descriptors are of interest.
125.Pp
126The
127.Fn pselect
128function is similar to
129.Fn select
130except that it specifies the timeout using a timespec structure.
131Also, if
132.Fa mask
133is a non-null pointer,
134.Fn pselect
135atomically sets the calling thread's signal mask to the signal set
136pointed to by
137.Fa mask
138for the duration of the function call.
139In this case, the original signal mask will be restored before
140.Fn pselect
141returns.
142.Sh RETURN VALUES
143If successful,
144.Fn select
145and
146.Fn pselect
147return the number of ready descriptors that are contained in
148the descriptor sets.
149If a descriptor is included in multiple descriptor sets,
150each inclusion is counted separately.
151If the time limit expires before any descriptors become ready,
152they return 0.
153.Pp
154Otherwise, if
155.Fn select
156or
157.Fn pselect
158return with an error, including one due to an interrupted call,
159they return \-1,
160and the descriptor sets will be unmodified.
161.Sh ERRORS
162An error return from
163.Fn select
164or
165.Fn pselect
166indicates:
167.Bl -tag -width Er
168.It Bq Er EFAULT
169One or more of
170.Fa readfds ,
171.Fa writefds ,
172or
173.Fa exceptfds
174points outside the process's allocated address space.
175.It Bq Er EBADF
176One of the descriptor sets specified an invalid descriptor.
177.It Bq Er EINTR
178A signal was delivered before the time limit expired and
179before any of the selected descriptors became ready.
180.It Bq Er EINVAL
181The specified time limit is invalid.
182One of its components is negative or too large.
183.El
184.Sh SEE ALSO
185.Xr accept 2 ,
186.Xr clock_gettime 2 ,
187.Xr connect 2 ,
188.Xr gettimeofday 2 ,
189.Xr poll 2 ,
190.Xr read 2 ,
191.Xr recv 2 ,
192.Xr send 2 ,
193.Xr write 2 ,
194.Xr getdtablesize 3
195.Sh HISTORY
196The
197.Fn select
198system call first appeared in
199.Bx 4.1c .
200The
201.Fn pselect
202system call has been available since
203.Ox 5.4 .
204.Sh BUGS
205Although the provision of
206.Xr getdtablesize 3
207was intended to allow user programs to be written independent
208of the kernel limit on the number of open files, the dimension
209of a sufficiently large bit field for select remains a problem.
210The default bit size of
211.Ft fd_set
212is based on the symbol
213.Dv FD_SETSIZE
214(currently 1024),
215but that is somewhat smaller than the current kernel limit
216to the number of open files.
217However, in order to accommodate programs which might potentially
218use a larger number of open files with select, it is possible
219to increase this size within a program by providing
220a larger definition of
221.Dv FD_SETSIZE
222before the inclusion of any headers.
223The kernel will cope, and the userland libraries provided with the
224system are also ready for large numbers of file descriptors.
225.Pp
226Alternatively, to be really safe, it is possible to allocate
227.Ft fd_set
228bit-arrays dynamically.
229The idea is to permit a program to work properly even if it is
230.Xr execve 2 Ns 'd
231with 4000 file descriptors pre-allocated.
232The following illustrates the technique which is used by
233userland libraries:
234.Bd -literal -offset indent
235fd_set *fdsr;
236int max = fd;
237
238fdsr = calloc(howmany(max+1, NFDBITS), sizeof(fd_mask));
239if (fdsr == NULL) {
240	...
241	return (-1);
242}
243FD_SET(fd, fdsr);
244n = select(max+1, fdsr, NULL, NULL, &tv);
245\&...
246free(fdsr);
247.Ed
248.Pp
249Alternatively, it is possible to use the
250.Xr poll 2
251interface.
252.Xr poll 2
253is more efficient when the size of
254.Fn select Ns 's
255.Ft fd_set
256bit-arrays are very large, and for fixed numbers of
257file descriptors one need not size and dynamically allocate a
258memory object.
259.Pp
260.Fn select
261should probably have been designed to return the time remaining from the
262original timeout, if any, by modifying the time value in place.
263Even though some systems stupidly act in this different way, it is
264unlikely this semantic will ever be commonly implemented, as the
265change causes massive source code compatibility problems.
266Furthermore, recent new standards have dictated the current behaviour.
267In general, due to the existence of those brain-damaged
268non-conforming systems, it is unwise to assume that the timeout
269value will be unmodified by the
270.Fn select
271call, and the caller should reinitialize it on each invocation.
272Calculating the delta is easily done by calling
273.Xr gettimeofday 2
274before and after the call to
275.Fn select ,
276and using
277.Fn timersub
278(as described in
279.Xr getitimer 2 ) .
280.Pp
281Internally to the kernel,
282.Fn select
283and
284.Fn pselect
285work poorly if multiple processes wait on the same file descriptor.
286Given that, it is rather surprising to see that many daemons are
287written that way.
288