]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" This manpage is copyright (C) 1992 Drew Eckhardt, |
2 | .\" copyright (C) 1995 Michael Shields. | |
3 | .\" | |
93015253 | 4 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
5 | .\" Permission is granted to make and distribute verbatim copies of this |
6 | .\" manual provided the copyright notice and this permission notice are | |
7 | .\" preserved on all copies. | |
8 | .\" | |
9 | .\" Permission is granted to copy and distribute modified versions of this | |
10 | .\" manual under the conditions for verbatim copying, provided that the | |
11 | .\" entire resulting derived work is distributed under the terms of a | |
12 | .\" permission notice identical to this one. | |
c13182ef | 13 | .\" |
fea681da MK |
14 | .\" Since the Linux kernel and libraries are constantly changing, this |
15 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
16 | .\" responsibility for errors or omissions, or for damages resulting from | |
17 | .\" the use of the information contained herein. The author(s) may not | |
18 | .\" have taken the same level of care in the production of this manual, | |
19 | .\" which is licensed free of charge, as they might when working | |
20 | .\" professionally. | |
c13182ef | 21 | .\" |
fea681da MK |
22 | .\" Formatted or processed versions of this manual, if unaccompanied by |
23 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 24 | .\" %%%LICENSE_END |
fea681da MK |
25 | .\" |
26 | .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu> | |
27 | .\" Modified 1995-05-18 by Jim Van Zandt <jrv@vanzandt.mv.com> | |
28 | .\" Sun Feb 11 14:07:00 MET 1996 Martin Schulze <joey@linux.de> | |
29 | .\" * layout slightly modified | |
30 | .\" | |
31 | .\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com> | |
32 | .\" Modified Thu Feb 24 01:41:09 CET 2000 by aeb | |
33 | .\" Modified Thu Feb 9 22:32:09 CET 2001 by bert hubert <ahu@ds9a.nl>, aeb | |
34 | .\" Modified Mon Nov 11 14:35:00 PST 2002 by Ben Woodard <ben@zork.net> | |
d02aa9bc MK |
35 | .\" 2005-03-11, mtk, modified pselect() text (it is now a system |
36 | .\" call in 2.6.16. | |
fea681da | 37 | .\" |
4b8c67d9 | 38 | .TH SELECT 2 2017-09-15 "Linux" "Linux Programmer's Manual" |
fea681da | 39 | .SH NAME |
c13182ef | 40 | select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \- |
35478399 | 41 | synchronous I/O multiplexing |
fea681da | 42 | .SH SYNOPSIS |
cc9befa9 | 43 | .nf |
e9419385 | 44 | /* According to POSIX.1-2001, POSIX.1-2008 */ |
fea681da | 45 | .B #include <sys/select.h> |
dbfe9c70 | 46 | .PP |
fea681da | 47 | /* According to earlier standards */ |
fea681da | 48 | .B #include <sys/time.h> |
fea681da | 49 | .B #include <sys/types.h> |
fea681da | 50 | .B #include <unistd.h> |
68e4db0a | 51 | .PP |
cc4615cc MK |
52 | .BI "int select(int " nfds ", fd_set *" readfds ", fd_set *" writefds , |
53 | .BI " fd_set *" exceptfds ", struct timeval *" timeout ); | |
68e4db0a | 54 | .PP |
071dbad9 | 55 | .BI "void FD_CLR(int " fd ", fd_set *" set ); |
521bf584 | 56 | .BI "int FD_ISSET(int " fd ", fd_set *" set ); |
071dbad9 | 57 | .BI "void FD_SET(int " fd ", fd_set *" set ); |
071dbad9 | 58 | .BI "void FD_ZERO(fd_set *" set ); |
f90f031e | 59 | |
9b813741 | 60 | .B #include <sys/select.h> |
68e4db0a | 61 | .PP |
cc4615cc MK |
62 | .BI "int pselect(int " nfds ", fd_set *" readfds ", fd_set *" writefds , |
63 | .BI " fd_set *" exceptfds ", const struct timespec *" timeout , | |
64 | .BI " const sigset_t *" sigmask ); | |
fea681da | 65 | .fi |
68e4db0a | 66 | .PP |
cc4615cc MK |
67 | .in -4n |
68 | Feature Test Macro Requirements for glibc (see | |
69 | .BR feature_test_macros (7)): | |
70 | .in | |
68e4db0a | 71 | .PP |
cc4615cc | 72 | .BR pselect (): |
a446ac0c | 73 | _POSIX_C_SOURCE\ >=\ 200112L |
fea681da | 74 | .SH DESCRIPTION |
e511ffb6 | 75 | .BR select () |
fea681da | 76 | and |
e511ffb6 | 77 | .BR pselect () |
39179b3e | 78 | allow a program to monitor multiple file descriptors, |
5e01a1de MK |
79 | waiting until one or more of the file descriptors become "ready" |
80 | for some class of I/O operation (e.g., input possible). | |
39179b3e | 81 | A file descriptor is considered ready if it is possible to |
d2e7d1bb RR |
82 | perform a corresponding I/O operation (e.g., |
83 | .BR read (2) | |
84 | without blocking, or a sufficiently small | |
85 | .BR write (2)). | |
fea681da | 86 | .PP |
6b6e9185 | 87 | .BR select () |
6c345305 MK |
88 | can monitor only file descriptors numbers that are less than |
89 | .BR FD_SETSIZE ; | |
90 | .BR poll (2) | |
6b6e9185 | 91 | does not have this limitation. |
6c345305 | 92 | See BUGS. |
6b6e9185 | 93 | .PP |
5e01a1de MK |
94 | The operation of |
95 | .BR select () | |
96 | and | |
97 | .BR pselect () | |
8c1d94ae | 98 | is identical, other than these three differences: |
fea681da MK |
99 | .TP |
100 | (i) | |
e511ffb6 | 101 | .BR select () |
cc9befa9 | 102 | uses a timeout that is a |
fea681da MK |
103 | .I struct timeval |
104 | (with seconds and microseconds), while | |
e511ffb6 | 105 | .BR pselect () |
fea681da MK |
106 | uses a |
107 | .I struct timespec | |
108 | (with seconds and nanoseconds). | |
109 | .TP | |
110 | (ii) | |
e511ffb6 | 111 | .BR select () |
cc9befa9 | 112 | may update the |
fea681da | 113 | .I timeout |
cc9befa9 | 114 | argument to indicate how much time was left. |
e511ffb6 | 115 | .BR pselect () |
cc9befa9 | 116 | does not change this argument. |
fea681da MK |
117 | .TP |
118 | (iii) | |
e511ffb6 | 119 | .BR select () |
cc9befa9 | 120 | has no |
fea681da | 121 | .I sigmask |
cc9befa9 | 122 | argument, and behaves as |
e511ffb6 | 123 | .BR pselect () |
fea681da MK |
124 | called with NULL |
125 | .IR sigmask . | |
126 | .PP | |
c13182ef | 127 | Three independent sets of file descriptors are watched. |
47da6ce7 | 128 | The file descriptors listed in |
fea681da MK |
129 | .I readfds |
130 | will be watched to see if characters become | |
131 | available for reading (more precisely, to see if a read will not | |
47da6ce7 MK |
132 | block; in particular, a file descriptor is also ready on end-of-file). |
133 | The file descriptors in | |
fea681da | 134 | .I writefds |
d2e7d1bb | 135 | will be watched to see if space is available for write (though a large |
47da6ce7 MK |
136 | write may still block). |
137 | The file descriptors in | |
fea681da | 138 | .I exceptfds |
47da6ce7 | 139 | will be watched for exceptional conditions. |
d871cf85 MK |
140 | (For examples of some exceptional conditions, see the discussion of |
141 | .B POLLPRI | |
142 | in | |
143 | .BR poll (2).) | |
efeece04 | 144 | .PP |
47da6ce7 | 145 | On exit, each of the file descriptor sets is modified in place |
22f348ca | 146 | to indicate which file descriptors actually changed status. |
cd2ea4b4 MK |
147 | (Thus, if using |
148 | .BR select () | |
149 | within a loop, the sets must be reinitialized before each call.) | |
efeece04 | 150 | .PP |
22f348ca | 151 | Each of the three file descriptor sets may be specified as NULL |
c13182ef | 152 | if no file descriptors are to be watched for the corresponding class |
22f348ca | 153 | of events. |
fea681da MK |
154 | .PP |
155 | Four macros are provided to manipulate the sets. | |
e511ffb6 | 156 | .BR FD_ZERO () |
21045df8 | 157 | clears a set. |
e511ffb6 | 158 | .BR FD_SET () |
fea681da | 159 | and |
e511ffb6 | 160 | .BR FD_CLR () |
22f348ca | 161 | respectively add and remove a given file descriptor from a set. |
e511ffb6 | 162 | .BR FD_ISSET () |
c13182ef | 163 | tests to see if a file descriptor is part of the set; |
22f348ca | 164 | this is useful after |
e511ffb6 | 165 | .BR select () |
fea681da MK |
166 | returns. |
167 | .PP | |
6efed4df | 168 | .I nfds |
8b58a9d4 MK |
169 | should be set to the highest-numbered file descriptor in any |
170 | of the three sets, plus 1. | |
171 | The indicated file descriptors in each set are checked, up to this limit | |
172 | (but see BUGS). | |
fea681da | 173 | .PP |
8c121f40 | 174 | The |
fea681da | 175 | .I timeout |
073f0240 | 176 | argument specifies the interval that |
e511ffb6 | 177 | .BR select () |
8c121f40 | 178 | should block waiting for a file descriptor to become ready. |
40df3d00 MK |
179 | The call will block until either: |
180 | .IP * 3 | |
181 | a file descriptor becomes ready; | |
182 | .IP * | |
183 | the call is interrupted by a signal handler; or | |
184 | .IP * | |
71e7d7f1 | 185 | the timeout expires. |
40df3d00 MK |
186 | .PP |
187 | Note that the | |
188 | .I timeout | |
189 | interval will be rounded up to the system clock granularity, | |
8c121f40 | 190 | and kernel scheduling delays mean that the blocking interval |
073f0240 | 191 | may overrun by a small amount. |
485eb4ad MK |
192 | If both fields of the |
193 | .I timeval | |
c808bb16 | 194 | structure are zero, then |
e511ffb6 | 195 | .BR select () |
485eb4ad | 196 | returns immediately. |
c13182ef MK |
197 | (This is useful for polling.) |
198 | If | |
fea681da MK |
199 | .I timeout |
200 | is NULL (no timeout), | |
e511ffb6 | 201 | .BR select () |
fea681da MK |
202 | can block indefinitely. |
203 | .PP | |
204 | .I sigmask | |
205 | is a pointer to a signal mask (see | |
206 | .BR sigprocmask (2)); | |
207 | if it is not NULL, then | |
e511ffb6 | 208 | .BR pselect () |
fea681da MK |
209 | first replaces the current signal mask by the one pointed to by |
210 | .IR sigmask , | |
2d986c92 | 211 | then does the "select" function, and then restores the original |
cc9befa9 | 212 | signal mask. |
fea681da | 213 | .PP |
d02aa9bc MK |
214 | Other than the difference in the precision of the |
215 | .I timeout | |
c13182ef | 216 | argument, the following |
d02aa9bc MK |
217 | .BR pselect () |
218 | call: | |
408731d4 MK |
219 | .PP |
220 | .in +4n | |
221 | .EX | |
222 | ready = pselect(nfds, &readfds, &writefds, &exceptfds, | |
223 | timeout, &sigmask); | |
224 | .EE | |
225 | .in | |
226 | .PP | |
d02aa9bc MK |
227 | is equivalent to |
228 | .I atomically | |
229 | executing the following calls: | |
408731d4 MK |
230 | .PP |
231 | .in +4n | |
232 | .EX | |
233 | sigset_t origmask; | |
d02aa9bc | 234 | |
408731d4 MK |
235 | pthread_sigmask(SIG_SETMASK, &sigmask, &origmask); |
236 | ready = select(nfds, &readfds, &writefds, &exceptfds, timeout); | |
237 | pthread_sigmask(SIG_SETMASK, &origmask, NULL); | |
238 | .EE | |
239 | .in | |
240 | .PP | |
d02aa9bc | 241 | .PP |
c13182ef | 242 | The reason that |
e511ffb6 | 243 | .BR pselect () |
d02aa9bc MK |
244 | is needed is that if one wants to wait for either a signal |
245 | or for a file descriptor to become ready, then | |
c13182ef | 246 | an atomic test is needed to prevent race conditions. |
d02aa9bc | 247 | (Suppose the signal handler sets a global flag and |
c13182ef MK |
248 | returns. |
249 | Then a test of this global flag followed by a call of | |
fea681da MK |
250 | .BR select () |
251 | could hang indefinitely if the signal arrived just after the test | |
c13182ef | 252 | but just before the call. |
d02aa9bc | 253 | By contrast, |
e511ffb6 | 254 | .BR pselect () |
fea681da MK |
255 | allows one to first block signals, handle the signals that have come in, |
256 | then call | |
257 | .BR pselect () | |
258 | with the desired | |
259 | .IR sigmask , | |
260 | avoiding the race.) | |
73d8cece | 261 | .SS The timeout |
fea681da MK |
262 | The time structures involved are defined in |
263 | .I <sys/time.h> | |
264 | and look like | |
efeece04 | 265 | .PP |
088a639b | 266 | .in +4n |
b8302363 | 267 | .EX |
c13182ef | 268 | struct timeval { |
fea681da MK |
269 | long tv_sec; /* seconds */ |
270 | long tv_usec; /* microseconds */ | |
271 | }; | |
b8302363 | 272 | .EE |
a08ea57c | 273 | .in |
efeece04 | 274 | .PP |
fea681da | 275 | and |
efeece04 | 276 | .PP |
088a639b | 277 | .in +4n |
b8302363 | 278 | .EX |
fea681da MK |
279 | struct timespec { |
280 | long tv_sec; /* seconds */ | |
281 | long tv_nsec; /* nanoseconds */ | |
282 | }; | |
b8302363 | 283 | .EE |
a08ea57c | 284 | .in |
efeece04 | 285 | .PP |
e9419385 | 286 | (However, see below on the POSIX.1 versions.) |
fea681da MK |
287 | .PP |
288 | Some code calls | |
e511ffb6 | 289 | .BR select () |
fea681da | 290 | with all three sets empty, |
79b8032e | 291 | .I nfds |
22f348ca | 292 | zero, and a non-NULL |
fea681da MK |
293 | .I timeout |
294 | as a fairly portable way to sleep with subsecond precision. | |
295 | .PP | |
c13182ef | 296 | On Linux, |
e511ffb6 | 297 | .BR select () |
fea681da MK |
298 | modifies |
299 | .I timeout | |
300 | to reflect the amount of time not slept; most other implementations | |
77f00d75 | 301 | do not do this. |
e9419385 | 302 | (POSIX.1 permits either behavior.) |
77f00d75 | 303 | This causes problems both when Linux code which reads |
fea681da MK |
304 | .I timeout |
305 | is ported to other operating systems, and when code is ported to Linux | |
0c2ec4f1 | 306 | that reuses a \fIstruct timeval\fP for multiple |
e511ffb6 | 307 | .BR select ()s |
c13182ef MK |
308 | in a loop without reinitializing it. |
309 | Consider | |
fea681da MK |
310 | .I timeout |
311 | to be undefined after | |
e511ffb6 | 312 | .BR select () |
fea681da | 313 | returns. |
d9bfdb9c | 314 | .\" .PP - it is rumored that: |
fea681da MK |
315 | .\" On BSD, when a timeout occurs, the file descriptor bits are not changed. |
316 | .\" - it is certainly true that: | |
317 | .\" Linux follows SUSv2 and sets the bit masks to zero upon a timeout. | |
47297adb | 318 | .SH RETURN VALUE |
fea681da | 319 | On success, |
e511ffb6 | 320 | .BR select () |
fea681da | 321 | and |
e511ffb6 | 322 | .BR pselect () |
22f348ca MK |
323 | return the number of file descriptors contained in the three returned |
324 | descriptor sets (that is, the total number of bits that are set in | |
fea681da MK |
325 | .IR readfds , |
326 | .IR writefds , | |
327 | .IR exceptfds ) | |
328 | which may be zero if the timeout expires before anything interesting happens. | |
329 | On error, \-1 is returned, and | |
330 | .I errno | |
8dc33675 MK |
331 | is set to indicate the error; |
332 | the file descriptor sets are unmodified, | |
333 | and | |
fea681da | 334 | .I timeout |
8dc33675 | 335 | becomes undefined. |
fea681da MK |
336 | .SH ERRORS |
337 | .TP | |
338 | .B EBADF | |
339 | An invalid file descriptor was given in one of the sets. | |
c13182ef | 340 | (Perhaps a file descriptor that was already closed, |
6efed4df | 341 | or one on which an error has occurred.) |
8b58a9d4 | 342 | However, see BUGS. |
fea681da MK |
343 | .TP |
344 | .B EINTR | |
01538d0d MK |
345 | A signal was caught; see |
346 | .BR signal (7). | |
fea681da MK |
347 | .TP |
348 | .B EINVAL | |
6efed4df | 349 | .I nfds |
b9ebc9b7 MK |
350 | is negative or exceeds the |
351 | .BR RLIMIT_NOFILE | |
352 | resource limit (see | |
353 | .BR getrlimit (2)). | |
354 | .TP | |
355 | .B EINVAL | |
02959ce2 | 356 | The value contained within |
fea681da MK |
357 | .I timeout |
358 | is invalid. | |
359 | .TP | |
360 | .B ENOMEM | |
02959ce2 | 361 | Unable to allocate memory for internal tables. |
a1d5f77c MK |
362 | .SH VERSIONS |
363 | .BR pselect () | |
364 | was added to Linux in kernel 2.6.16. | |
365 | Prior to this, | |
366 | .BR pselect () | |
367 | was emulated in glibc (but see BUGS). | |
47297adb | 368 | .SH CONFORMING TO |
c13182ef | 369 | .BR select () |
e9419385 | 370 | conforms to POSIX.1-2001, POSIX.1-2008, and |
c13182ef | 371 | 4.4BSD |
cc9befa9 | 372 | .RB ( select () |
c13182ef MK |
373 | first appeared in 4.2BSD). |
374 | Generally portable to/from | |
fea681da | 375 | non-BSD systems supporting clones of the BSD socket layer (including |
efbfd7ec MK |
376 | System\ V variants). |
377 | However, note that the System\ V variant typically | |
fea681da MK |
378 | sets the timeout variable before exit, but the BSD variant does not. |
379 | .PP | |
e511ffb6 | 380 | .BR pselect () |
97c1eac8 | 381 | is defined in POSIX.1g, and in |
e9419385 | 382 | POSIX.1-2001 and POSIX.1-2008. |
fea681da | 383 | .SH NOTES |
c13182ef MK |
384 | An |
385 | .I fd_set | |
386 | is a fixed size buffer. | |
387 | Executing | |
22f348ca | 388 | .BR FD_CLR () |
c13182ef | 389 | or |
22f348ca MK |
390 | .BR FD_SET () |
391 | with a value of | |
fea681da | 392 | .I fd |
682edefb MK |
393 | that is negative or is equal to or larger than |
394 | .B FD_SETSIZE | |
395 | will result | |
c13182ef MK |
396 | in undefined behavior. |
397 | Moreover, POSIX requires | |
fea681da MK |
398 | .I fd |
399 | to be a valid file descriptor. | |
efeece04 | 400 | .PP |
20cc8fa8 MK |
401 | On some other UNIX systems, |
402 | .\" Darwin, according to a report by Jeremy Sequoia, relayed by Josh Triplett | |
403 | .BR select () | |
404 | can fail with the error | |
405 | .B EAGAIN | |
406 | if the system fails to allocate kernel-internal resources, rather than | |
407 | .B ENOMEM | |
408 | as Linux does. | |
409 | POSIX specifies this error for | |
410 | .BR poll (2), | |
411 | but not for | |
412 | .BR select (). | |
413 | Portable programs may wish to check for | |
414 | .B EAGAIN | |
415 | and loop, just as with | |
416 | .BR EINTR . | |
efeece04 | 417 | .PP |
3116bbe0 MK |
418 | On systems that lack |
419 | .BR pselect (), | |
420 | reliable (and more portable) signal trapping can be achieved | |
421 | using the self-pipe trick. | |
422 | In this technique, | |
423 | a signal handler writes a byte to a pipe whose other end | |
424 | is monitored by | |
425 | .BR select () | |
426 | in the main program. | |
427 | (To avoid possibly blocking when writing to a pipe that may be full | |
428 | or reading from a pipe that may be empty, | |
429 | nonblocking I/O is used when reading from and writing to the pipe.) | |
efeece04 | 430 | .PP |
fea681da | 431 | Concerning the types involved, the classical situation is that |
22f348ca | 432 | the two fields of a |
c13182ef | 433 | .I timeval |
e3e25559 MK |
434 | structure are typed as |
435 | .I long | |
436 | (as shown above), and the structure is defined in | |
fea681da | 437 | .IR <sys/time.h> . |
e9419385 | 438 | The POSIX.1 situation is |
efeece04 | 439 | .PP |
088a639b | 440 | .in +4n |
b8302363 | 441 | .EX |
fea681da MK |
442 | struct timeval { |
443 | time_t tv_sec; /* seconds */ | |
444 | suseconds_t tv_usec; /* microseconds */ | |
445 | }; | |
b8302363 | 446 | .EE |
a08ea57c | 447 | .in |
efeece04 | 448 | .PP |
22f348ca | 449 | where the structure is defined in |
fea681da | 450 | .I <sys/select.h> |
c13182ef MK |
451 | and the data types |
452 | .I time_t | |
453 | and | |
454 | .I suseconds_t | |
22f348ca | 455 | are defined in |
fea681da | 456 | .IR <sys/types.h> . |
dd3568a1 | 457 | .PP |
fea681da MK |
458 | Concerning prototypes, the classical situation is that one should |
459 | include | |
460 | .I <time.h> | |
461 | for | |
e511ffb6 | 462 | .BR select (). |
e9419385 | 463 | The POSIX.1 situation is that one should include |
fea681da MK |
464 | .I <sys/select.h> |
465 | for | |
e511ffb6 | 466 | .BR select () |
fea681da | 467 | and |
e511ffb6 | 468 | .BR pselect (). |
efeece04 | 469 | .PP |
083e5b2f MK |
470 | Under glibc 2.0, |
471 | .I <sys/select.h> | |
472 | gives the wrong prototype for | |
8b98e6fc | 473 | .BR pselect (). |
565ee767 | 474 | Under glibc 2.1 to 2.2.1, it gives |
e511ffb6 | 475 | .BR pselect () |
fea681da MK |
476 | when |
477 | .B _GNU_SOURCE | |
8b98e6fc | 478 | is defined. |
565ee767 | 479 | Since glibc 2.2.2, the requirements are as shown in the SYNOPSIS. |
ab264bff MK |
480 | .\" |
481 | .SS Correspondence between select() and poll() notifications | |
482 | Within the Linux kernel source, | |
483 | .\" fs/select.c | |
484 | we find the following definitions which show the correspondence | |
485 | between the readable, writable, and exceptional condition notifications of | |
486 | .BR select () | |
487 | and the event notifications provided by | |
488 | .BR poll (2) | |
489 | (and | |
490 | .BR epoll (7)): | |
efeece04 | 491 | .PP |
ab264bff | 492 | .in +4n |
b8302363 | 493 | .EX |
ab264bff MK |
494 | #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | |
495 | POLLERR) | |
496 | /* Ready for reading */ | |
497 | #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR) | |
498 | /* Ready for writing */ | |
499 | #define POLLEX_SET (POLLPRI) | |
500 | /* Exceptional condition */ | |
b8302363 | 501 | .EE |
e646a1ba | 502 | .in |
ab264bff | 503 | .\" |
10ed041e MK |
504 | .SS Multithreaded applications |
505 | If a file descriptor being monitored by | |
506 | .BR select () | |
507 | is closed in another thread, the result is unspecified. | |
508 | On some UNIX systems, | |
509 | .BR select () | |
510 | unblocks and returns, with an indication that the file descriptor is ready | |
511 | (a subsequent I/O operation will likely fail with an error, | |
512 | unless another the file descriptor reopened between the time | |
513 | .BR select () | |
514 | returned and the I/O operations was performed). | |
515 | On Linux (and some other systems), | |
516 | closing the file descriptor in another thread has no effect on | |
517 | .BR select (). | |
518 | In summary, any application that relies on a particular behavior | |
519 | in this scenario must be considered buggy. | |
17ec2d27 | 520 | .\" |
0722a578 | 521 | .SS C library/kernel differences |
6c345305 MK |
522 | The Linux kernel allows file descriptor sets of arbitrary size, |
523 | determining the length of the sets to be checked from the value of | |
524 | .IR nfds . | |
525 | However, in the glibc implementation, the | |
526 | .IR fd_set | |
527 | type is fixed in size. | |
528 | See also BUGS. | |
efeece04 | 529 | .PP |
a16eec1e | 530 | The |
77f00d75 | 531 | .BR pselect () |
a16eec1e MK |
532 | interface described in this page is implemented by glibc. |
533 | The underlying Linux system call is named | |
534 | .BR pselect6 (). | |
a59e64be | 535 | This system call has somewhat different behavior from the glibc |
a16eec1e | 536 | wrapper function. |
efeece04 | 537 | .PP |
a16eec1e MK |
538 | The Linux |
539 | .BR pselect6 () | |
c13182ef MK |
540 | system call modifies its |
541 | .I timeout | |
2f11acf5 | 542 | argument. |
d9bfdb9c | 543 | However, the glibc wrapper function hides this behavior |
77f00d75 MK |
544 | by using a local variable for the timeout argument that |
545 | is passed to the system call. | |
c13182ef | 546 | Thus, the glibc |
77f00d75 | 547 | .BR pselect () |
d53de2a7 MK |
548 | function does not modify its |
549 | .I timeout | |
550 | argument; | |
d9bfdb9c | 551 | this is the behavior required by POSIX.1-2001. |
efeece04 | 552 | .PP |
a16eec1e | 553 | The final argument of the |
02ace852 | 554 | .BR pselect6 () |
0ab8aeec | 555 | system call is not a |
a16eec1e MK |
556 | .I "sigset_t\ *" |
557 | pointer, but is instead a structure of the form: | |
408731d4 | 558 | .PP |
a16eec1e | 559 | .in +4 |
408731d4 | 560 | .EX |
a16eec1e | 561 | struct { |
1a116ea0 MK |
562 | const kernel_sigset_t *ss; /* Pointer to signal set */ |
563 | size_t ss_len; /* Size (in bytes) of object | |
564 | pointed to by 'ss' */ | |
a16eec1e | 565 | }; |
e646a1ba | 566 | .EE |
a16eec1e | 567 | .in |
e646a1ba | 568 | .PP |
a16eec1e MK |
569 | This allows the system call to obtain both |
570 | a pointer to the signal set and its size, | |
571 | while allowing for the fact that most architectures | |
f8dcca84 MK |
572 | support a maximum of 6 arguments to a system call. |
573 | See | |
574 | .BR sigprocmask (2) | |
f25ea51b N |
575 | for a discussion of the difference between the kernel and libc |
576 | notion of the signal set. | |
fea681da | 577 | .SH BUGS |
6c345305 MK |
578 | POSIX allows an implementation to define an upper limit, |
579 | advertised via the constant | |
580 | .BR FD_SETSIZE , | |
581 | on the range of file descriptors that can be specified | |
582 | in a file descriptor set. | |
583 | The Linux kernel imposes no fixed limit, but the glibc implementation makes | |
584 | .IR fd_set | |
585 | a fixed-size type, with | |
586 | .BR FD_SETSIZE | |
587 | defined as 1024, and the | |
588 | .BR FD_* () | |
589 | macros operating according to that limit. | |
590 | To monitor file descriptors greater than 1023, use | |
591 | .BR poll (2) | |
592 | instead. | |
efeece04 | 593 | .PP |
8b58a9d4 MK |
594 | According to POSIX, |
595 | .BR select () | |
596 | should check all specified file descriptors in the three file descriptor sets, | |
597 | up to the limit | |
598 | .IR nfds\-1 . | |
599 | However, the current implementation ignores any file descriptor in | |
600 | these sets that is greater than the maximum file descriptor number | |
601 | that the process currently has open. | |
602 | According to POSIX, any such file descriptor that is specified in one | |
603 | of the sets should result in the error | |
604 | .BR EBADF . | |
efeece04 | 605 | .PP |
cc9befa9 | 606 | Glibc 2.0 provided a version of |
e511ffb6 | 607 | .BR pselect () |
c13182ef MK |
608 | that did not take a |
609 | .I sigmask | |
cc9befa9 | 610 | argument. |
efeece04 | 611 | .PP |
3fa2e4b9 | 612 | Starting with version 2.1, glibc provided an emulation of |
c13182ef | 613 | .BR pselect () |
3fa2e4b9 | 614 | that was implemented using |
cc9befa9 MK |
615 | .BR sigprocmask (2) |
616 | and | |
617 | .BR select (). | |
3fa2e4b9 | 618 | This implementation remained vulnerable to the very race condition that |
cc9befa9 MK |
619 | .BR pselect () |
620 | was designed to prevent. | |
3fa2e4b9 MK |
621 | Modern versions of glibc use the (race-free) |
622 | .BR pselect () | |
623 | system call on kernels where it is provided. | |
efeece04 | 624 | .PP |
fea681da | 625 | Under Linux, |
e511ffb6 | 626 | .BR select () |
fea681da | 627 | may report a socket file descriptor as "ready for reading", while |
c13182ef MK |
628 | nevertheless a subsequent read blocks. |
629 | This could for example | |
fea681da | 630 | happen when data has arrived but upon examination has wrong |
c13182ef MK |
631 | checksum and is discarded. |
632 | There may be other circumstances | |
2f11acf5 | 633 | in which a file descriptor is spuriously reported as ready. |
fea681da MK |
634 | .\" Stevens discusses a case where accept can block after select |
635 | .\" returns successfully because of an intervening RST from the client. | |
682edefb MK |
636 | Thus it may be safer to use |
637 | .B O_NONBLOCK | |
638 | on sockets that should not block. | |
fea681da | 639 | .\" Maybe the kernel should have returned EIO in such a situation? |
efeece04 | 640 | .PP |
5766b196 MK |
641 | On Linux, |
642 | .BR select () | |
643 | also modifies | |
644 | .I timeout | |
645 | if the call is interrupted by a signal handler (i.e., the | |
646 | .B EINTR | |
647 | error return). | |
e9419385 | 648 | This is not permitted by POSIX.1. |
5766b196 | 649 | The Linux |
2777b1ca | 650 | .BR pselect () |
5766b196 MK |
651 | system call has the same behavior, |
652 | but the glibc wrapper hides this behavior by internally copying the | |
653 | .I timeout | |
654 | to a local variable and passing that variable to the system call. | |
2b2581ee | 655 | .SH EXAMPLE |
408731d4 | 656 | .EX |
2b2581ee | 657 | #include <stdio.h> |
af9c7ff2 | 658 | #include <stdlib.h> |
2b2581ee MK |
659 | #include <sys/time.h> |
660 | #include <sys/types.h> | |
661 | #include <unistd.h> | |
662 | ||
663 | int | |
664 | main(void) | |
665 | { | |
666 | fd_set rfds; | |
667 | struct timeval tv; | |
668 | int retval; | |
669 | ||
670 | /* Watch stdin (fd 0) to see when it has input. */ | |
887f19e8 | 671 | |
2b2581ee MK |
672 | FD_ZERO(&rfds); |
673 | FD_SET(0, &rfds); | |
674 | ||
675 | /* Wait up to five seconds. */ | |
887f19e8 | 676 | |
2b2581ee MK |
677 | tv.tv_sec = 5; |
678 | tv.tv_usec = 0; | |
679 | ||
680 | retval = select(1, &rfds, NULL, NULL, &tv); | |
681 | /* Don't rely on the value of tv now! */ | |
682 | ||
683 | if (retval == \-1) | |
684 | perror("select()"); | |
685 | else if (retval) | |
686 | printf("Data is available now.\\n"); | |
687 | /* FD_ISSET(0, &rfds) will be true. */ | |
688 | else | |
689 | printf("No data within five seconds.\\n"); | |
690 | ||
691 | exit(EXIT_SUCCESS); | |
692 | } | |
408731d4 | 693 | .EE |
47297adb | 694 | .SH SEE ALSO |
fea681da MK |
695 | .BR accept (2), |
696 | .BR connect (2), | |
697 | .BR poll (2), | |
698 | .BR read (2), | |
699 | .BR recv (2), | |
25a7bfe6 | 700 | .BR restart_syscall (2), |
fea681da MK |
701 | .BR send (2), |
702 | .BR sigprocmask (2), | |
50e5322c | 703 | .BR write (2), |
1d7c4d16 MK |
704 | .BR epoll (7), |
705 | .BR time (7) | |
efeece04 | 706 | .PP |
173fe7e7 DP |
707 | For a tutorial with discussion and examples, see |
708 | .BR select_tut (2). |