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