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