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