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