]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/select.2
fuse.4: ffix
[thirdparty/man-pages.git] / man2 / select.2
1 .\" This manpage is copyright (C) 1992 Drew Eckhardt,
2 .\" copyright (C) 1995 Michael Shields.
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
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.
13 .\"
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.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
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>
35 .\" 2005-03-11, mtk, modified pselect() text (it is now a system
36 .\" call in 2.6.16.
37 .\"
38 .TH SELECT 2 2016-03-15 "Linux" "Linux Programmer's Manual"
39 .SH NAME
40 select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \-
41 synchronous I/O multiplexing
42 .SH SYNOPSIS
43 .nf
44 /* According to POSIX.1-2001, POSIX.1-2008 */
45 .br
46 .B #include <sys/select.h>
47 .sp
48 /* According to earlier standards */
49 .br
50 .B #include <sys/time.h>
51 .br
52 .B #include <sys/types.h>
53 .br
54 .B #include <unistd.h>
55 .sp
56 .BI "int select(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
57 .BI " fd_set *" exceptfds ", struct timeval *" timeout );
58 .sp
59 .BI "void FD_CLR(int " fd ", fd_set *" set );
60 .br
61 .BI "int FD_ISSET(int " fd ", fd_set *" set );
62 .br
63 .BI "void FD_SET(int " fd ", fd_set *" set );
64 .br
65 .BI "void FD_ZERO(fd_set *" set );
66 .sp
67 .B #include <sys/select.h>
68 .sp
69 .BI "int pselect(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
70 .BI " fd_set *" exceptfds ", const struct timespec *" timeout ,
71 .BI " const sigset_t *" sigmask );
72 .fi
73 .sp
74 .in -4n
75 Feature Test Macro Requirements for glibc (see
76 .BR feature_test_macros (7)):
77 .in
78 .sp
79 .BR pselect ():
80 _POSIX_C_SOURCE\ >=\ 200112L
81 .SH DESCRIPTION
82 .BR select ()
83 and
84 .BR pselect ()
85 allow a program to monitor multiple file descriptors,
86 waiting until one or more of the file descriptors become "ready"
87 for some class of I/O operation (e.g., input possible).
88 A file descriptor is considered ready if it is possible to
89 perform a corresponding I/O operation (e.g.,
90 .BR read (2)
91 without blocking, or a sufficiently small
92 .BR write (2)).
93 .PP
94 .BR select ()
95 can monitor only file descriptors numbers that are less than
96 .BR FD_SETSIZE ;
97 .BR poll (2)
98 does not have this limitation.
99 See BUGS.
100 .PP
101 The operation of
102 .BR select ()
103 and
104 .BR pselect ()
105 is identical, other than these three differences:
106 .TP
107 (i)
108 .BR select ()
109 uses a timeout that is a
110 .I struct timeval
111 (with seconds and microseconds), while
112 .BR pselect ()
113 uses a
114 .I struct timespec
115 (with seconds and nanoseconds).
116 .TP
117 (ii)
118 .BR select ()
119 may update the
120 .I timeout
121 argument to indicate how much time was left.
122 .BR pselect ()
123 does not change this argument.
124 .TP
125 (iii)
126 .BR select ()
127 has no
128 .I sigmask
129 argument, and behaves as
130 .BR pselect ()
131 called with NULL
132 .IR sigmask .
133 .PP
134 Three independent sets of file descriptors are watched.
135 Those listed in
136 .I readfds
137 will be watched to see if characters become
138 available for reading (more precisely, to see if a read will not
139 block; in particular, a file descriptor is also ready on end-of-file),
140 those in
141 .I writefds
142 will be watched to see if space is available for write (though a large
143 write may still block), and those in
144 .I exceptfds
145 will be watched for exceptions.
146 On exit, the sets are modified in place
147 to indicate which file descriptors actually changed status.
148 Each of the three file descriptor sets may be specified as NULL
149 if no file descriptors are to be watched for the corresponding class
150 of events.
151 .PP
152 Four macros are provided to manipulate the sets.
153 .BR FD_ZERO ()
154 clears a set.
155 .BR FD_SET ()
156 and
157 .BR FD_CLR ()
158 respectively add and remove a given file descriptor from a set.
159 .BR FD_ISSET ()
160 tests to see if a file descriptor is part of the set;
161 this is useful after
162 .BR select ()
163 returns.
164 .PP
165 .I nfds
166 is the highest-numbered file descriptor in any of the three sets, plus 1.
167 .PP
168 The
169 .I timeout
170 argument specifies the interval that
171 .BR select ()
172 should block waiting for a file descriptor to become ready.
173 The call will block until either:
174 .IP * 3
175 a file descriptor becomes ready;
176 .IP *
177 the call is interrupted by a signal handler; or
178 .IP *
179 the timeout expires.
180 .PP
181 Note that the
182 .I timeout
183 interval will be rounded up to the system clock granularity,
184 and kernel scheduling delays mean that the blocking interval
185 may overrun by a small amount.
186 If both fields of the
187 .I timeval
188 structure are zero, then
189 .BR select ()
190 returns immediately.
191 (This is useful for polling.)
192 If
193 .I timeout
194 is NULL (no timeout),
195 .BR select ()
196 can block indefinitely.
197 .PP
198 .I sigmask
199 is a pointer to a signal mask (see
200 .BR sigprocmask (2));
201 if it is not NULL, then
202 .BR pselect ()
203 first replaces the current signal mask by the one pointed to by
204 .IR sigmask ,
205 then does the "select" function, and then restores the original
206 signal mask.
207 .PP
208 Other than the difference in the precision of the
209 .I timeout
210 argument, the following
211 .BR pselect ()
212 call:
213 .nf
214
215 ready = pselect(nfds, &readfds, &writefds, &exceptfds,
216 timeout, &sigmask);
217
218 .fi
219 is equivalent to
220 .I atomically
221 executing the following calls:
222 .nf
223
224 sigset_t origmask;
225
226 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
227 ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
228 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
229 .fi
230 .PP
231 The reason that
232 .BR pselect ()
233 is needed is that if one wants to wait for either a signal
234 or for a file descriptor to become ready, then
235 an atomic test is needed to prevent race conditions.
236 (Suppose the signal handler sets a global flag and
237 returns.
238 Then a test of this global flag followed by a call of
239 .BR select ()
240 could hang indefinitely if the signal arrived just after the test
241 but just before the call.
242 By contrast,
243 .BR pselect ()
244 allows one to first block signals, handle the signals that have come in,
245 then call
246 .BR pselect ()
247 with the desired
248 .IR sigmask ,
249 avoiding the race.)
250 .SS The timeout
251 The time structures involved are defined in
252 .I <sys/time.h>
253 and look like
254
255 .in +4n
256 .nf
257 struct timeval {
258 long tv_sec; /* seconds */
259 long tv_usec; /* microseconds */
260 };
261 .fi
262 .in
263
264 and
265
266 .in +4n
267 .nf
268 struct timespec {
269 long tv_sec; /* seconds */
270 long tv_nsec; /* nanoseconds */
271 };
272 .fi
273 .in
274
275 (However, see below on the POSIX.1 versions.)
276 .PP
277 Some code calls
278 .BR select ()
279 with all three sets empty,
280 .I nfds
281 zero, and a non-NULL
282 .I timeout
283 as a fairly portable way to sleep with subsecond precision.
284 .PP
285 On Linux,
286 .BR select ()
287 modifies
288 .I timeout
289 to reflect the amount of time not slept; most other implementations
290 do not do this.
291 (POSIX.1 permits either behavior.)
292 This causes problems both when Linux code which reads
293 .I timeout
294 is ported to other operating systems, and when code is ported to Linux
295 that reuses a \fIstruct timeval\fP for multiple
296 .BR select ()s
297 in a loop without reinitializing it.
298 Consider
299 .I timeout
300 to be undefined after
301 .BR select ()
302 returns.
303 .\" .PP - it is rumored that:
304 .\" On BSD, when a timeout occurs, the file descriptor bits are not changed.
305 .\" - it is certainly true that:
306 .\" Linux follows SUSv2 and sets the bit masks to zero upon a timeout.
307 .SH RETURN VALUE
308 On success,
309 .BR select ()
310 and
311 .BR pselect ()
312 return the number of file descriptors contained in the three returned
313 descriptor sets (that is, the total number of bits that are set in
314 .IR readfds ,
315 .IR writefds ,
316 .IR exceptfds )
317 which may be zero if the timeout expires before anything interesting happens.
318 On error, \-1 is returned, and
319 .I errno
320 is set to indicate the error;
321 the file descriptor sets are unmodified,
322 and
323 .I timeout
324 becomes undefined.
325 .SH ERRORS
326 .TP
327 .B EBADF
328 An invalid file descriptor was given in one of the sets.
329 (Perhaps a file descriptor that was already closed,
330 or one on which an error has occurred.)
331 .TP
332 .B EINTR
333 A signal was caught; see
334 .BR signal (7).
335 .TP
336 .B EINVAL
337 .I nfds
338 is negative or exceeds the
339 .BR RLIMIT_NOFILE
340 resource limit (see
341 .BR getrlimit (2)).
342 .TP
343 .B EINVAL
344 The value contained within
345 .I timeout
346 is invalid.
347 .TP
348 .B ENOMEM
349 Unable to allocate memory for internal tables.
350 .SH VERSIONS
351 .BR pselect ()
352 was added to Linux in kernel 2.6.16.
353 Prior to this,
354 .BR pselect ()
355 was emulated in glibc (but see BUGS).
356 .SH CONFORMING TO
357 .BR select ()
358 conforms to POSIX.1-2001, POSIX.1-2008, and
359 4.4BSD
360 .RB ( select ()
361 first appeared in 4.2BSD).
362 Generally portable to/from
363 non-BSD systems supporting clones of the BSD socket layer (including
364 System\ V variants).
365 However, note that the System\ V variant typically
366 sets the timeout variable before exit, but the BSD variant does not.
367 .PP
368 .BR pselect ()
369 is defined in POSIX.1g, and in
370 POSIX.1-2001 and POSIX.1-2008.
371 .SH NOTES
372 An
373 .I fd_set
374 is a fixed size buffer.
375 Executing
376 .BR FD_CLR ()
377 or
378 .BR FD_SET ()
379 with a value of
380 .I fd
381 that is negative or is equal to or larger than
382 .B FD_SETSIZE
383 will result
384 in undefined behavior.
385 Moreover, POSIX requires
386 .I fd
387 to be a valid file descriptor.
388
389 On some other UNIX systems,
390 .\" Darwin, according to a report by Jeremy Sequoia, relayed by Josh Triplett
391 .BR select ()
392 can fail with the error
393 .B EAGAIN
394 if the system fails to allocate kernel-internal resources, rather than
395 .B ENOMEM
396 as Linux does.
397 POSIX specifies this error for
398 .BR poll (2),
399 but not for
400 .BR select ().
401 Portable programs may wish to check for
402 .B EAGAIN
403 and loop, just as with
404 .BR EINTR .
405
406 Concerning the types involved, the classical situation is that
407 the two fields of a
408 .I timeval
409 structure are typed as
410 .I long
411 (as shown above), and the structure is defined in
412 .IR <sys/time.h> .
413 The POSIX.1 situation is
414
415 .in +4n
416 .nf
417 struct timeval {
418 time_t tv_sec; /* seconds */
419 suseconds_t tv_usec; /* microseconds */
420 };
421 .fi
422 .in
423
424 where the structure is defined in
425 .I <sys/select.h>
426 and the data types
427 .I time_t
428 and
429 .I suseconds_t
430 are defined in
431 .IR <sys/types.h> .
432 .LP
433 Concerning prototypes, the classical situation is that one should
434 include
435 .I <time.h>
436 for
437 .BR select ().
438 The POSIX.1 situation is that one should include
439 .I <sys/select.h>
440 for
441 .BR select ()
442 and
443 .BR pselect ().
444
445 Under glibc 2.0,
446 .I <sys/select.h>
447 gives the wrong prototype for
448 .BR pselect ().
449 Under glibc 2.1 to 2.2.1, it gives
450 .BR pselect ()
451 when
452 .B _GNU_SOURCE
453 is defined.
454 Since glibc 2.2.2, the requirements are as shown in the SYNOPSIS.
455 .SS Multithreaded applications
456 If a file descriptor being monitored by
457 .BR select ()
458 is closed in another thread, the result is unspecified.
459 On some UNIX systems,
460 .BR select ()
461 unblocks and returns, with an indication that the file descriptor is ready
462 (a subsequent I/O operation will likely fail with an error,
463 unless another the file descriptor reopened between the time
464 .BR select ()
465 returned and the I/O operations was performed).
466 On Linux (and some other systems),
467 closing the file descriptor in another thread has no effect on
468 .BR select ().
469 In summary, any application that relies on a particular behavior
470 in this scenario must be considered buggy.
471 .\"
472 .SS C library/kernel differences
473 The Linux kernel allows file descriptor sets of arbitrary size,
474 determining the length of the sets to be checked from the value of
475 .IR nfds .
476 However, in the glibc implementation, the
477 .IR fd_set
478 type is fixed in size.
479 See also BUGS.
480
481 The
482 .BR pselect ()
483 interface described in this page is implemented by glibc.
484 The underlying Linux system call is named
485 .BR pselect6 ().
486 This system call has somewhat different behavior from the glibc
487 wrapper function.
488
489 The Linux
490 .BR pselect6 ()
491 system call modifies its
492 .I timeout
493 argument.
494 However, the glibc wrapper function hides this behavior
495 by using a local variable for the timeout argument that
496 is passed to the system call.
497 Thus, the glibc
498 .BR pselect ()
499 function does not modify its
500 .I timeout
501 argument;
502 this is the behavior required by POSIX.1-2001.
503
504 The final argument of the
505 .BR pselect6 ()
506 system call is not a
507 .I "sigset_t\ *"
508 pointer, but is instead a structure of the form:
509 .in +4
510 .nf
511
512 struct {
513 const sigset_t *ss; /* Pointer to signal set */
514 size_t ss_len; /* Size (in bytes) of object pointed
515 to by 'ss' */
516 };
517
518 .fi
519 .in
520 This allows the system call to obtain both
521 a pointer to the signal set and its size,
522 while allowing for the fact that most architectures
523 support a maximum of 6 arguments to a system call.
524 .SH BUGS
525 POSIX allows an implementation to define an upper limit,
526 advertised via the constant
527 .BR FD_SETSIZE ,
528 on the range of file descriptors that can be specified
529 in a file descriptor set.
530 The Linux kernel imposes no fixed limit, but the glibc implementation makes
531 .IR fd_set
532 a fixed-size type, with
533 .BR FD_SETSIZE
534 defined as 1024, and the
535 .BR FD_* ()
536 macros operating according to that limit.
537 To monitor file descriptors greater than 1023, use
538 .BR poll (2)
539 instead.
540
541 Glibc 2.0 provided a version of
542 .BR pselect ()
543 that did not take a
544 .I sigmask
545 argument.
546
547 Starting with version 2.1, glibc provided an emulation of
548 .BR pselect ()
549 that was implemented using
550 .BR sigprocmask (2)
551 and
552 .BR select ().
553 This implementation remained vulnerable to the very race condition that
554 .BR pselect ()
555 was designed to prevent.
556 Modern versions of glibc use the (race-free)
557 .BR pselect ()
558 system call on kernels where it is provided.
559
560 On systems that lack
561 .BR pselect (),
562 reliable (and more portable) signal trapping can be achieved
563 using the self-pipe trick.
564 In this technique,
565 a signal handler writes a byte to a pipe whose other end
566 is monitored by
567 .BR select ()
568 in the main program.
569 (To avoid possibly blocking when writing to a pipe that may be full
570 or reading from a pipe that may be empty,
571 nonblocking I/O is used when reading from and writing to the pipe.)
572
573 Under Linux,
574 .BR select ()
575 may report a socket file descriptor as "ready for reading", while
576 nevertheless a subsequent read blocks.
577 This could for example
578 happen when data has arrived but upon examination has wrong
579 checksum and is discarded.
580 There may be other circumstances
581 in which a file descriptor is spuriously reported as ready.
582 .\" Stevens discusses a case where accept can block after select
583 .\" returns successfully because of an intervening RST from the client.
584 Thus it may be safer to use
585 .B O_NONBLOCK
586 on sockets that should not block.
587 .\" Maybe the kernel should have returned EIO in such a situation?
588
589 On Linux,
590 .BR select ()
591 also modifies
592 .I timeout
593 if the call is interrupted by a signal handler (i.e., the
594 .B EINTR
595 error return).
596 This is not permitted by POSIX.1.
597 The Linux
598 .BR pselect ()
599 system call has the same behavior,
600 but the glibc wrapper hides this behavior by internally copying the
601 .I timeout
602 to a local variable and passing that variable to the system call.
603 .SH EXAMPLE
604 .nf
605 #include <stdio.h>
606 #include <stdlib.h>
607 #include <sys/time.h>
608 #include <sys/types.h>
609 #include <unistd.h>
610
611 int
612 main(void)
613 {
614 fd_set rfds;
615 struct timeval tv;
616 int retval;
617
618 /* Watch stdin (fd 0) to see when it has input. */
619
620 FD_ZERO(&rfds);
621 FD_SET(0, &rfds);
622
623 /* Wait up to five seconds. */
624
625 tv.tv_sec = 5;
626 tv.tv_usec = 0;
627
628 retval = select(1, &rfds, NULL, NULL, &tv);
629 /* Don't rely on the value of tv now! */
630
631 if (retval == \-1)
632 perror("select()");
633 else if (retval)
634 printf("Data is available now.\\n");
635 /* FD_ISSET(0, &rfds) will be true. */
636 else
637 printf("No data within five seconds.\\n");
638
639 exit(EXIT_SUCCESS);
640 }
641 .fi
642 .SH SEE ALSO
643 .BR accept (2),
644 .BR connect (2),
645 .BR poll (2),
646 .BR read (2),
647 .BR recv (2),
648 .BR restart_syscall (2),
649 .BR send (2),
650 .BR sigprocmask (2),
651 .BR write (2),
652 .BR epoll (7),
653 .BR time (7)
654
655 For a tutorial with discussion and examples, see
656 .BR select_tut (2).