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