.\"
.\" @(#)select.2 8.2 (Berkeley) 3/25/94
.\"
-.Dd March 25, 1994
+.Dd March 18, 2015
.Dt SELECT 2
.Os BSD 4.2
.Sh NAME
+.Nm FD_CLR ,
+.Nm FD_COPY ,
+.Nm FD_ISSET ,
+.Nm FD_SET ,
+.Nm FD_ZERO ,
.Nm select
.Nd synchronous I/O multiplexing
.Sh SYNOPSIS
.Fd #include <sys/select.h>
-.D1 "- or -"
-.Fd #include <sys/types.h>
-.Fd #include <sys/time.h>
-.Fd #include <unistd.h>
+.\"
+.Ft void
+.Fo FD_CLR
+.Fa fd
+.Fa "fd_set *fdset"
+.Fc
+.Ft void
+.Fo FD_COPY
+.Fa "fd_set *fdset_orig"
+.Fa "fd_set *fdset_copy"
+.Fc
.Ft int
-.Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout"
-.Fn FD_SET fd &fdset
-.Fn FD_CLR fd &fdset
-.Fn FD_ISSET fd &fdset
-.Fn FD_ZERO &fdset
+.Fo FD_ISSET
+.Fa fd
+.Fa "fd_set *fdset"
+.Fc
+.Ft void
+.Fo FD_SET
+.Fa fd
+.Fa "fd_set *fdset"
+.Fc
+.Ft void
+.Fo FD_ZERO
+.Fa "fd_set *fdset"
+.Fc
+.Ft int
+.Fo select
+.Fa "int nfds"
+.Fa "fd_set *restrict readfds"
+.Fa "fd_set *restrict writefds"
+.Fa "fd_set *restrict errorfds"
+.Fa "struct timeval *restrict timeout"
+.Fc
.Sh DESCRIPTION
-.Fn Select
+.Fn select
examines the I/O descriptor sets whose addresses are passed in
.Fa readfds ,
.Fa writefds ,
and
-.Fa exceptfds
+.Fa errorfds
to see if some of their descriptors
are ready for reading, are ready for writing, or have an exceptional
condition pending, respectively.
descriptors are checked in each set;
i.e., the descriptors from 0 through
.Fa nfds Ns No -1
-in the descriptor sets are examined.
+in the descriptor sets are examined. (Example: If you have set two file descriptors "4" and "17",
+.Fa nfds
+should not be "2", but rather "17 + 1" or "18".)
On return,
.Fn select
replaces the given descriptor sets
with subsets consisting of those descriptors that are ready
for the requested operation.
-.Fn Select
+.Fn select
returns the total number of ready descriptors in all the sets.
.Pp
The descriptor sets are stored as bit fields in arrays of integers.
is a member of
.Fa fdset ,
zero otherwise.
+.Fn FD_COPY &fdset_orig &fdset_copy
+replaces an already allocated
+.Fa &fdset_copy
+file descriptor set with a copy of
+.Fa &fdset_orig .
The behavior of these macros is undefined if
a descriptor value is less than zero or greater than or equal to
.Dv FD_SETSIZE ,
.Pp
If
.Fa timeout
-is a non-nil pointer, it specifies a maximum interval to wait for the
-selection to complete. If
+is not a null pointer, it specifies a maximum interval to wait for the
+selection to complete.
+.Pp
+If
+.Fa timeout
+is a null pointer, the select blocks indefinitely.
+.Pp
+To effect a poll, the
.Fa timeout
-is a nil pointer, the select blocks indefinitely. To effect a poll, the
+argument should be not be a null pointer,
+but it should point to a zero-valued timeval structure.
+.Pp
.Fa timeout
-argument should be non-nil, pointing to a zero-valued timeval structure.
-.Fa Timeout
is not changed by
.Fn select ,
and may be reused on subsequent calls, however it is good style to re-initialize
.Fa readfds ,
.Fa writefds ,
and
-.Fa exceptfds
-may be given as nil pointers if no descriptors are of interest.
+.Fa errorfds
+may be given as null pointers if no descriptors are of interest.
.Sh RETURN VALUES
-.Fn Select
+.Fn select
returns the number of ready descriptors that are contained in
the descriptor sets,
or -1 if an error occurred.
.Fn select
returns with an error,
including one due to an interrupted call,
-the descriptor sets will be unmodified.
+the descriptor sets will be unmodified and the global variable
+.Va errno
+will be set to indicate the error.
.Sh ERRORS
An error return from
.Fn select
indicates:
.Bl -tag -width Er
+.\" ===========
+.It Bq Er EAGAIN
+The kernel was (perhaps temporarily) unable
+to allocate the requested number of file descriptors.
+.\" ===========
.It Bq Er EBADF
One of the descriptor sets specified an invalid descriptor.
+.\" ===========
.It Bq Er EINTR
A signal was delivered before the time limit expired and
before any of the selected events occurred.
+.\" ===========
.It Bq Er EINVAL
The specified time limit is invalid. One of its components is
negative or too large.
+.\" ===========
+.It Bq Er EINVAL
+.Fa ndfs
+is greater than FD_SETSIZE and _DARWIN_UNLIMITED_SELECT is not defined.
.El
+.Sh LEGACY SYNOPSIS
+.Fd #include <sys/select.h>
+.D1 "- or -"
+.Fd #include <sys/types.h>
+.Fd #include <sys/time.h>
+.Fd #include <unistd.h>
+.Pp
+.Fo FD_SET
+.Fa fd
+.Fa &fdset
+.Fc ;
+.Pp
+.Fo FD_CLR
+.Fa fd
+.Fa &fdset
+.Fc ;
+.Pp
+.Fo FD_ISSET
+.Fa fd
+.Fa &fdset
+.Fc ;
+.Pp
+.Fo FD_COPY
+.Fa &fdset_orig
+.Fa &fdset_copy
+.Fc ;
+.Pp
+.Fo FD_ZERO
+.Fa &fdset
+.Fc ;
+.Sh COMPATIBILITY
+.Fn select
+now returns with
+.Va errno
+set to EINVAL when
+.Fa nfds
+is greater than FD_SETSIZE.
+Use a smaller value for
+.Fa nfds
+or compile with -D_DARWIN_UNLIMITED_SELECT.
.Sh SEE ALSO
.Xr accept 2 ,
.Xr connect 2 ,
+.Xr connectx 2 ,
.Xr getdtablesize 2 ,
.Xr gettimeofday 2 ,
.Xr read 2 ,
.Xr recv 2 ,
.Xr send 2 ,
-.Xr write 2
+.Xr write 2 ,
+.Xr compat 5
.Sh BUGS
Although the provision of
.Xr getdtablesize 2
before the inclusion of
.Aq Pa sys/types.h .
.Pp
-.Fn Select
+.Fn select
should probably have been designed to return the time remaining from the
original timeout, if any, by modifying the time value in place.
However, it is unlikely this semantic will ever be implemented, as the