Initial import from FreeBSD RELENG_4:
[dragonfly.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. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     @(#)select.2    8.2 (Berkeley) 3/25/94
33 .\" $FreeBSD: src/lib/libc/sys/select.2,v 1.14.2.5 2001/12/14 18:34:01 ru Exp $
34 .\"
35 .Dd March 25, 1994
36 .Dt SELECT 2
37 .Os
38 .Sh NAME
39 .Nm select
40 .Nd synchronous I/O multiplexing
41 .Sh LIBRARY
42 .Lb libc
43 .Sh SYNOPSIS
44 .In sys/types.h
45 .In sys/time.h
46 .In 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
55 examines the I/O descriptor sets whose addresses are passed in
56 .Fa readfds ,
57 .Fa writefds ,
58 and
59 .Fa exceptfds
60 to see if some of their descriptors
61 are ready for reading, are ready for writing, or have an exceptional
62 condition pending, respectively.
63 The only exceptional condition detectable is out-of-band
64 data received on a socket.
65 The first
66 .Fa nfds
67 descriptors are checked in each set;
68 i.e., the descriptors from 0 through
69 .Fa nfds Ns No -1
70 in the descriptor sets are examined.
71 On return,
72 .Fn select
73 replaces the given descriptor sets
74 with subsets consisting of those descriptors that are ready
75 for the requested operation.
76 .Fn Select
77 returns the total number of ready descriptors in all the sets.
78 .Pp
79 The descriptor sets are stored as bit fields in arrays of integers.
80 The following macros are provided for manipulating such descriptor sets:
81 .Fn FD_ZERO &fdset
82 initializes a descriptor set
83 .Fa fdset
84 to the null set.
85 .Fn FD_SET fd &fdset
86 includes a particular descriptor
87 .Fa fd
88 in
89 .Fa fdset .
90 .Fn FD_CLR fd &fdset
91 removes
92 .Fa fd
93 from
94 .Fa fdset .
95 .Fn FD_ISSET fd &fdset
96 is non-zero if
97 .Fa fd
98 is a member of
99 .Fa fdset ,
100 zero otherwise.
101 The behavior of these macros is undefined if
102 a descriptor value is less than zero or greater than or equal to
103 .Dv FD_SETSIZE ,
104 which is normally at least equal
105 to the maximum number of descriptors supported by the system.
106 .Pp
107 If
108 .Fa timeout
109 is a non-nil pointer, it specifies the maximum interval to wait for the
110 selection to complete.  System activity can lengthen the interval by
111 an indeterminate amount.
112 .Pp
113 If
114 .Fa timeout
115 is a nil pointer, the select blocks indefinitely.
116 .Pp
117 To effect a poll, the
118 .Fa timeout
119 argument should be non-nil, pointing to a zero-valued timeval structure.
120 .Pp
121 Any of
122 .Fa readfds ,
123 .Fa writefds ,
124 and
125 .Fa exceptfds
126 may be given as nil pointers if no descriptors are of interest.
127 .Sh RETURN VALUES
128 .Fn Select
129 returns the number of ready descriptors that are contained in
130 the descriptor sets,
131 or -1 if an error occurred.
132 If the time limit expires,
133 .Fn select
134 returns 0.
135 If
136 .Fn select
137 returns with an error,
138 including one due to an interrupted call,
139 the descriptor sets will be unmodified.
140 .Sh ERRORS
141 An error return from
142 .Fn select
143 indicates:
144 .Bl -tag -width Er
145 .It Bq Er EBADF
146 One of the descriptor sets specified an invalid descriptor.
147 .It Bq Er EINTR
148 A signal was delivered before the time limit expired and
149 before any of the selected events occurred.
150 .It Bq Er EINVAL
151 The specified time limit is invalid.  One of its components is
152 negative or too large.
153 .It Bq Er EINVAL
154 .Fa nfds
155 was invalid.
156 .El
157 .Sh SEE ALSO
158 .Xr accept 2 ,
159 .Xr connect 2 ,
160 .Xr getdtablesize 2 ,
161 .Xr gettimeofday 2 ,
162 .Xr read 2 ,
163 .Xr recv 2 ,
164 .Xr send 2 ,
165 .Xr write 2 ,
166 .Xr clocks 7
167 .Sh NOTES
168 The default size of
169 .Dv FD_SETSIZE
170 is currently 1024.
171 In order to accommodate programs which might potentially
172 use a larger number of open files with
173 .Fn select ,
174 it is possible
175 to increase this size by having the program define
176 .Dv FD_SETSIZE
177 before the inclusion of any header which includes
178 .Aq Pa sys/types.h .
179 .Pp
180 If
181 .Fa nfds
182 is greater than the number of open files,
183 .Fn select
184 is not guaranteed to examine the unused file descriptors.   For historical
185 reasons,
186 .Fn select
187 will always examine the first 256 descriptors.
188 .Sh BUGS
189 .St -susv2
190 allows systems to modify the original timeout in place.
191 Thus, it is unwise to assume that the timeout value will be unmodified
192 by the
193 .Fn select
194 call.
195 .Sh HISTORY
196 The
197 .Fn select
198 function call appeared in
199 .Bx 4.2 .