]> git.ipfire.org Git - thirdparty/man-pages.git/blobdiff - man2/select.2
pipe.2: Note that 'pipefd' is left unchanged in the event of an error
[thirdparty/man-pages.git] / man2 / select.2
index 1bba6b3c63c6f0ac49ef5e6908a15346237e89a8..d677ad8ee35ca947518059fabc5adde71ed018e6 100644 (file)
@@ -1,8 +1,7 @@
-.\" Hey Emacs! This file is -*- nroff -*- source.
-.\"
 .\" This manpage is copyright (C) 1992 Drew Eckhardt,
 .\"                 copyright (C) 1995 Michael Shields.
 .\"
+.\" %%%LICENSE_START(VERBATIM)
 .\" Permission is granted to make and distribute verbatim copies of this
 .\" manual provided the copyright notice and this permission notice are
 .\" preserved on all copies.
@@ -22,6 +21,7 @@
 .\"
 .\" Formatted or processed versions of this manual, if unaccompanied by
 .\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
 .\"
 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
 .\" Modified 1995-05-18 by Jim Van Zandt <jrv@vanzandt.mv.com>
 .\" 2005-03-11, mtk, modified pselect() text (it is now a system
 .\"     call in 2.6.16.
 .\"
-.TH SELECT 2 2008-12-05 "Linux" "Linux Programmer's Manual"
+.TH SELECT 2 2019-03-06 "Linux" "Linux Programmer's Manual"
 .SH NAME
 select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \-
 synchronous I/O multiplexing
 .SH SYNOPSIS
 .nf
-/* According to POSIX.1-2001 */
-.br
+/* According to POSIX.1-2001, POSIX.1-2008 */
 .B #include <sys/select.h>
-.sp
+.PP
 /* According to earlier standards */
-.br
 .B #include <sys/time.h>
-.br
 .B #include <sys/types.h>
-.br
 .B #include <unistd.h>
-.sp
+.PP
 .BI "int select(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
 .BI "           fd_set *" exceptfds ", struct timeval *" timeout );
-.sp
+.PP
 .BI "void FD_CLR(int " fd ", fd_set *" set );
-.br
 .BI "int  FD_ISSET(int " fd ", fd_set *" set );
-.br
 .BI "void FD_SET(int " fd ", fd_set *" set );
-.br
 .BI "void FD_ZERO(fd_set *" set );
-.sp
+
 .B #include <sys/select.h>
-.sp
+.PP
 .BI "int pselect(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
 .BI "            fd_set *" exceptfds ", const struct timespec *" timeout ,
 .BI "            const sigset_t *" sigmask );
 .fi
-.sp
+.PP
 .in -4n
 Feature Test Macro Requirements for glibc (see
 .BR feature_test_macros (7)):
 .in
-.sp
+.PP
 .BR pselect ():
-_POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
+_POSIX_C_SOURCE\ >=\ 200112L
 .SH DESCRIPTION
 .BR select ()
 and
@@ -86,15 +79,24 @@ allow a program to monitor multiple file descriptors,
 waiting until one or more of the file descriptors become "ready"
 for some class of I/O operation (e.g., input possible).
 A file descriptor is considered ready if it is possible to
-perform the corresponding I/O operation (e.g.,
-.BR read (2))
+perform a corresponding I/O operation (e.g.,
+.BR read (2),
+or a sufficiently small
+.BR write (2))
 without blocking.
 .PP
+.BR select ()
+can monitor only file descriptors numbers that are less than
+.BR FD_SETSIZE ;
+.BR poll (2)
+does not have this limitation.
+See BUGS.
+.PP
 The operation of
 .BR select ()
 and
 .BR pselect ()
-is identical, with three differences:
+is identical, other than these three differences:
 .TP
 (i)
 .BR select ()
@@ -124,19 +126,29 @@ called with NULL
 .IR sigmask .
 .PP
 Three independent sets of file descriptors are watched.
-Those listed in
+The file descriptors listed in
 .I readfds
 will be watched to see if characters become
 available for reading (more precisely, to see if a read will not
-block; in particular, a file descriptor is also ready on end-of-file),
-those in
+block; in particular, a file descriptor is also ready on end-of-file).
+The file descriptors in
 .I writefds
-will be watched to see if a write will not block, and
-those in
+will be watched to see if space is available for write (though a large
+write may still block).
+The file descriptors in
 .I exceptfds
-will be watched for exceptions.
-On exit, the sets are modified in place
+will be watched for exceptional conditions.
+(For examples of some exceptional conditions, see the discussion of
+.B POLLPRI
+in
+.BR poll (2).)
+.PP
+On exit, each of the file descriptor sets is modified in place
 to indicate which file descriptors actually changed status.
+(Thus, if using
+.BR select ()
+within a loop, the sets must be reinitialized before each call.)
+.PP
 Each of the three file descriptor sets may be specified as NULL
 if no file descriptors are to be watched for the corresponding class
 of events.
@@ -147,7 +159,7 @@ clears a set.
 .BR FD_SET ()
 and
 .BR FD_CLR ()
-respectively add and remove a given file descriptor from a set.
+add and remove a given file descriptor from a set.
 .BR FD_ISSET ()
 tests to see if a file descriptor is part of the set;
 this is useful after
@@ -155,12 +167,29 @@ this is useful after
 returns.
 .PP
 .I nfds
-is the highest-numbered file descriptor in any of the three sets, plus 1.
+should be set to the highest-numbered file descriptor in any
+of the three sets, plus 1.
+The indicated file descriptors in each set are checked, up to this limit
+(but see BUGS).
 .PP
+The
 .I timeout
-is an upper bound on the amount of time elapsed before
+argument specifies the interval that
 .BR select ()
-returns.
+should block waiting for a file descriptor to become ready.
+The call will block until either:
+.IP * 3
+a file descriptor becomes ready;
+.IP *
+the call is interrupted by a signal handler; or
+.IP *
+the timeout expires.
+.PP
+Note that the
+.I timeout
+interval will be rounded up to the system clock granularity,
+and kernel scheduling delays mean that the blocking interval
+may overrun by a small amount.
 If both fields of the
 .I timeval
 structure are zero, then
@@ -188,23 +217,28 @@ Other than the difference in the precision of the
 argument, the following
 .BR pselect ()
 call:
-.nf
-
-    ready = pselect(nfds, &readfds, &writefds, &exceptfds,
-                    timeout, &sigmask);
-
-.fi
+.PP
+.in +4n
+.EX
+ready = pselect(nfds, &readfds, &writefds, &exceptfds,
+                timeout, &sigmask);
+.EE
+.in
+.PP
 is equivalent to
 .I atomically
 executing the following calls:
-.nf
-
-    sigset_t origmask;
+.PP
+.in +4n
+.EX
+sigset_t origmask;
 
-    sigprocmask(SIG_SETMASK, &sigmask, &origmask);
-    ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
-    sigprocmask(SIG_SETMASK, &origmask, NULL);
-.fi
+pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
+ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
+pthread_sigmask(SIG_SETMASK, &origmask, NULL);
+.EE
+.in
+.PP
 .PP
 The reason that
 .BR pselect ()
@@ -225,32 +259,32 @@ then call
 with the desired
 .IR sigmask ,
 avoiding the race.)
-.SS "The timeout"
+.SS The timeout
 The time structures involved are defined in
 .I <sys/time.h>
 and look like
-
+.PP
 .in +4n
-.nf
+.EX
 struct timeval {
     long    tv_sec;         /* seconds */
     long    tv_usec;        /* microseconds */
 };
-.fi
+.EE
 .in
-
+.PP
 and
-
+.PP
 .in +4n
-.nf
+.EX
 struct timespec {
     long    tv_sec;         /* seconds */
     long    tv_nsec;        /* nanoseconds */
 };
-.fi
+.EE
 .in
-
-(However, see below on the POSIX.1-2001 versions.)
+.PP
+(However, see below on the POSIX.1 versions.)
 .PP
 Some code calls
 .BR select ()
@@ -266,7 +300,7 @@ modifies
 .I timeout
 to reflect the amount of time not slept; most other implementations
 do not do this.
-(POSIX.1-2001 permits either behavior.)
+(POSIX.1 permits either behavior.)
 This causes problems both when Linux code which reads
 .I timeout
 is ported to other operating systems, and when code is ported to Linux
@@ -282,7 +316,7 @@ returns.
 .\" On BSD, when a timeout occurs, the file descriptor bits are not changed.
 .\" - it is certainly true that:
 .\" Linux follows SUSv2 and sets the bit masks to zero upon a timeout.
-.SH "RETURN VALUE"
+.SH RETURN VALUE
 On success,
 .BR select ()
 and
@@ -295,16 +329,18 @@ descriptor sets (that is, the total number of bits that are set in
 which may be zero if the timeout expires before anything interesting happens.
 On error, \-1 is returned, and
 .I errno
-is set appropriately; the sets and
+is set to indicate the error;
+the file descriptor sets are unmodified,
+and
 .I timeout
-become undefined, so do not
-rely on their contents after an error.
+becomes undefined.
 .SH ERRORS
 .TP
 .B EBADF
 An invalid file descriptor was given in one of the sets.
 (Perhaps a file descriptor that was already closed,
 or one on which an error has occurred.)
+However, see BUGS.
 .TP
 .B EINTR
 A signal was caught; see
@@ -312,33 +348,39 @@ A signal was caught; see
 .TP
 .B EINVAL
 .I nfds
-is negative or the value contained within
+is negative or exceeds the
+.BR RLIMIT_NOFILE
+resource limit (see
+.BR getrlimit (2)).
+.TP
+.B EINVAL
+The value contained within
 .I timeout
 is invalid.
 .TP
 .B ENOMEM
-unable to allocate memory for internal tables.
+Unable to allocate memory for internal tables.
 .SH VERSIONS
 .BR pselect ()
 was added to Linux in kernel 2.6.16.
 Prior to this,
 .BR pselect ()
 was emulated in glibc (but see BUGS).
-.SH "CONFORMING TO"
+.SH CONFORMING TO
 .BR select ()
-conforms to POSIX.1-2001 and
+conforms to POSIX.1-2001, POSIX.1-2008, and
 4.4BSD
 .RB ( select ()
 first appeared in 4.2BSD).
 Generally portable to/from
 non-BSD systems supporting clones of the BSD socket layer (including
-System V variants).
-However, note that the System V variant typically
+System\ V variants).
+However, note that the System\ V variant typically
 sets the timeout variable before exit, but the BSD variant does not.
 .PP
 .BR pselect ()
 is defined in POSIX.1g, and in
-POSIX.1-2001.
+POSIX.1-2001 and POSIX.1-2008.
 .SH NOTES
 An
 .I fd_set
@@ -356,7 +398,45 @@ in undefined behavior.
 Moreover, POSIX requires
 .I fd
 to be a valid file descriptor.
-
+.PP
+The operation of
+.BR select ()
+and
+.BR pselect ()
+is not affected by the
+.BR O_NONBLOCK
+flag.
+.PP
+On some other UNIX systems,
+.\" Darwin, according to a report by Jeremy Sequoia, relayed by Josh Triplett
+.BR select ()
+can fail with the error
+.B EAGAIN
+if the system fails to allocate kernel-internal resources, rather than
+.B ENOMEM
+as Linux does.
+POSIX specifies this error for
+.BR poll (2),
+but not for
+.BR select ().
+Portable programs may wish to check for
+.B EAGAIN
+and loop, just as with
+.BR EINTR .
+.PP
+On systems that lack
+.BR pselect (),
+reliable (and more portable) signal trapping can be achieved
+using the self-pipe trick.
+In this technique,
+a signal handler writes a byte to a pipe whose other end
+is monitored by
+.BR select ()
+in the main program.
+(To avoid possibly blocking when writing to a pipe that may be full
+or reading from a pipe that may be empty,
+nonblocking I/O is used when reading from and writing to the pipe.)
+.PP
 Concerning the types involved, the classical situation is that
 the two fields of a
 .I timeval
@@ -364,17 +444,17 @@ structure are typed as
 .I long
 (as shown above), and the structure is defined in
 .IR <sys/time.h> .
-The POSIX.1-2001 situation is
-
+The POSIX.1 situation is
+.PP
 .in +4n
-.nf
+.EX
 struct timeval {
     time_t         tv_sec;     /* seconds */
     suseconds_t    tv_usec;    /* microseconds */
 };
-.fi
+.EE
 .in
-
+.PP
 where the structure is defined in
 .I <sys/select.h>
 and the data types
@@ -383,33 +463,89 @@ and
 .I suseconds_t
 are defined in
 .IR <sys/types.h> .
-.LP
+.PP
 Concerning prototypes, the classical situation is that one should
 include
 .I <time.h>
 for
 .BR select ().
-The POSIX.1-2001 situation is that one should include
+The POSIX.1 situation is that one should include
 .I <sys/select.h>
 for
 .BR select ()
 and
 .BR pselect ().
-
-Libc4 and libc5 do not have a
+.PP
+Under glibc 2.0,
 .I <sys/select.h>
-header; under glibc 2.0 and later this header exists.
-Under glibc 2.0 it unconditionally gives the wrong prototype for
+gives the wrong prototype for
 .BR pselect ().
-Under glibc 2.1 to 2.2.1 it gives
+Under glibc 2.1 to 2.2.1, it gives
 .BR pselect ()
 when
 .B _GNU_SOURCE
 is defined.
-Since glibc 2.2.2 the requirements are as shown in the SYNOPSIS.
-.SS "Linux Notes"
-The Linux
+Since glibc 2.2.2, the requirements are as shown in the SYNOPSIS.
+.\"
+.SS Correspondence between select() and poll() notifications
+Within the Linux kernel source,
+.\" fs/select.c
+we find the following definitions which show the correspondence
+between the readable, writable, and exceptional condition notifications of
+.BR select ()
+and the event notifications provided by
+.BR poll (2)
+(and
+.BR epoll (7)):
+.PP
+.in +4n
+.EX
+#define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP |
+                    POLLERR)
+                   /* Ready for reading */
+#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
+                   /* Ready for writing */
+#define POLLEX_SET (POLLPRI)
+                   /* Exceptional condition */
+.EE
+.in
+.\"
+.SS Multithreaded applications
+If a file descriptor being monitored by
+.BR select ()
+is closed in another thread, the result is unspecified.
+On some UNIX systems,
+.BR select ()
+unblocks and returns, with an indication that the file descriptor is ready
+(a subsequent I/O operation will likely fail with an error,
+unless another process reopens file descriptor between the time
+.BR select ()
+returned and the I/O operation is performed).
+On Linux (and some other systems),
+closing the file descriptor in another thread has no effect on
+.BR select ().
+In summary, any application that relies on a particular behavior
+in this scenario must be considered buggy.
+.\"
+.SS C library/kernel differences
+The Linux kernel allows file descriptor sets of arbitrary size,
+determining the length of the sets to be checked from the value of
+.IR nfds .
+However, in the glibc implementation, the
+.IR fd_set
+type is fixed in size.
+See also BUGS.
+.PP
+The
 .BR pselect ()
+interface described in this page is implemented by glibc.
+The underlying Linux system call is named
+.BR pselect6 ().
+This system call has somewhat different behavior from the glibc
+wrapper function.
+.PP
+The Linux
+.BR pselect6 ()
 system call modifies its
 .I timeout
 argument.
@@ -418,33 +554,92 @@ by using a local variable for the timeout argument that
 is passed to the system call.
 Thus, the glibc
 .BR pselect ()
-function does not modify its timeout argument;
+function does not modify its
+.I timeout
+argument;
 this is the behavior required by POSIX.1-2001.
+.PP
+The final argument of the
+.BR pselect6 ()
+system call is not a
+.I "sigset_t\ *"
+pointer, but is instead a structure of the form:
+.PP
+.in +4
+.EX
+struct {
+    const kernel_sigset_t *ss;   /* Pointer to signal set */
+    size_t ss_len;               /* Size (in bytes) of object
+                                    pointed to by 'ss' */
+};
+.EE
+.in
+.PP
+This allows the system call to obtain both
+a pointer to the signal set and its size,
+while allowing for the fact that most architectures
+support a maximum of 6 arguments to a system call.
+See
+.BR sigprocmask (2)
+for a discussion of the difference between the kernel and libc
+notion of the signal set.
 .SH BUGS
+POSIX allows an implementation to define an upper limit,
+advertised via the constant
+.BR FD_SETSIZE ,
+on the range of file descriptors that can be specified
+in a file descriptor set.
+The Linux kernel imposes no fixed limit, but the glibc implementation makes
+.IR fd_set
+a fixed-size type, with
+.BR FD_SETSIZE
+defined as 1024, and the
+.BR FD_* ()
+macros operating according to that limit.
+To monitor file descriptors greater than 1023, use
+.BR poll (2)
+instead.
+.PP
+The implementation of the
+.I fd_set
+arguments as value-result arguments means that they must be
+reinitialized on each call to
+.BR select ().
+This design error is avoided by
+.BR poll (2),
+which uses separate structure fields for the input and output of the call.
+.PP
+According to POSIX,
+.BR select ()
+should check all specified file descriptors in the three file descriptor sets,
+up to the limit
+.IR nfds\-1 .
+However, the current implementation ignores any file descriptor in
+these sets that is greater than the maximum file descriptor number
+that the process currently has open.
+According to POSIX, any such file descriptor that is specified in one
+of the sets should result in the error
+.BR EBADF .
+.PP
 Glibc 2.0 provided a version of
 .BR pselect ()
 that did not take a
 .I sigmask
 argument.
-
-Since version 2.1, glibc has provided an emulation of
+.PP
+Starting with version 2.1, glibc provided an emulation of
 .BR pselect ()
-that is implemented using
+that was implemented using
 .BR sigprocmask (2)
 and
 .BR select ().
-This implementation remains vulnerable to the very race condition that
+This implementation remained vulnerable to the very race condition that
 .BR pselect ()
 was designed to prevent.
-On systems that lack
-.BR pselect (),
-reliable (and more portable) signal trapping can be achieved
-using the self-pipe trick
-(where a signal handler writes a byte to a pipe whose other end
-is monitored by
-.BR select ()
-in the main program.)
-
+Modern versions of glibc use the (race-free)
+.BR pselect ()
+system call on kernels where it is provided.
+.PP
 Under Linux,
 .BR select ()
 may report a socket file descriptor as "ready for reading", while
@@ -460,7 +655,7 @@ Thus it may be safer to use
 .B O_NONBLOCK
 on sockets that should not block.
 .\" Maybe the kernel should have returned EIO in such a situation?
-
+.PP
 On Linux,
 .BR select ()
 also modifies
@@ -468,7 +663,7 @@ also modifies
 if the call is interrupted by a signal handler (i.e., the
 .B EINTR
 error return).
-This is not permitted by POSIX.1-2001.
+This is not permitted by POSIX.1.
 The Linux
 .BR pselect ()
 system call has the same behavior,
@@ -476,7 +671,7 @@ but the glibc wrapper hides this behavior by internally copying the
 .I timeout
 to a local variable and passing that variable to the system call.
 .SH EXAMPLE
-.nf
+.EX
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/time.h>
@@ -491,10 +686,12 @@ main(void)
     int retval;
 
     /* Watch stdin (fd 0) to see when it has input. */
+
     FD_ZERO(&rfds);
     FD_SET(0, &rfds);
 
     /* Wait up to five seconds. */
+
     tv.tv_sec = 5;
     tv.tv_usec = 0;
 
@@ -504,26 +701,26 @@ main(void)
     if (retval == \-1)
         perror("select()");
     else if (retval)
-        printf("Data is available now.\\n");
+        printf("Data is available now.\en");
         /* FD_ISSET(0, &rfds) will be true. */
     else
-        printf("No data within five seconds.\\n");
+        printf("No data within five seconds.\en");
 
     exit(EXIT_SUCCESS);
 }
-.fi
-.SH "SEE ALSO"
-For a tutorial with discussion and examples, see
-.BR select_tut (2).
-.LP
-For vaguely related stuff, see
+.EE
+.SH SEE ALSO
 .BR accept (2),
 .BR connect (2),
 .BR poll (2),
 .BR read (2),
 .BR recv (2),
+.BR restart_syscall (2),
 .BR send (2),
 .BR sigprocmask (2),
 .BR write (2),
 .BR epoll (7),
 .BR time (7)
+.PP
+For a tutorial with discussion and examples, see
+.BR select_tut (2).