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