xref: /minix3/lib/libc/sys/select.2 (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc.\"	$NetBSD: select.2,v 1.41 2013/11/28 03:45:31 khorben Exp $
22fe8fb19SBen Gras.\"
32fe8fb19SBen Gras.\" Copyright (c) 1983, 1991, 1993
42fe8fb19SBen Gras.\"	The Regents of the University of California.  All rights reserved.
52fe8fb19SBen Gras.\"
62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
82fe8fb19SBen Gras.\" are met:
92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras.\"    may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras.\"    without specific prior written permission.
172fe8fb19SBen Gras.\"
182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras.\" SUCH DAMAGE.
292fe8fb19SBen Gras.\"
302fe8fb19SBen Gras.\"     @(#)select.2	8.2 (Berkeley) 3/25/94
312fe8fb19SBen Gras.\"
32*84d9c625SLionel Sambuc.Dd November 28, 2013
332fe8fb19SBen Gras.Dt SELECT 2
342fe8fb19SBen Gras.Os
352fe8fb19SBen Gras.Sh NAME
362fe8fb19SBen Gras.Nm select ,
372fe8fb19SBen Gras.Nm pselect
382fe8fb19SBen Gras.Nd synchronous I/O multiplexing
392fe8fb19SBen Gras.Sh LIBRARY
402fe8fb19SBen Gras.Lb libc
412fe8fb19SBen Gras.Sh SYNOPSIS
422fe8fb19SBen Gras.In sys/select.h
432fe8fb19SBen Gras.Ft int
442fe8fb19SBen Gras.Fn select "int nfds" "fd_set * restrict readfds" "fd_set * restrict writefds" "fd_set * restrict exceptfds" "struct timeval * restrict timeout"
452fe8fb19SBen Gras.Ft int
462fe8fb19SBen Gras.Fn pselect "int nfds" "fd_set * restrict readfds" "fd_set * restrict writefds" "fd_set * restrict exceptfds" "const struct timespec *restrict timeout" "const sigset_t * restrict sigmask"
472fe8fb19SBen Gras.Fn FD_SET "int fd" "fd_set *fdset"
482fe8fb19SBen Gras.Fn FD_CLR "int fd" "fd_set *fdset"
492fe8fb19SBen Gras.Fn FD_ISSET "int fd" "fd_set *fdset"
502fe8fb19SBen Gras.Fn FD_ZERO "fd_set *fdset"
512fe8fb19SBen Gras.Sh DESCRIPTION
522fe8fb19SBen Gras.Fn select
532fe8fb19SBen Grasand
542fe8fb19SBen Gras.Fn pselect
552fe8fb19SBen Grasexamine the I/O descriptor sets whose addresses are passed in
562fe8fb19SBen Gras.Fa readfds ,
572fe8fb19SBen Gras.Fa writefds ,
582fe8fb19SBen Grasand
592fe8fb19SBen Gras.Fa exceptfds
602fe8fb19SBen Grasto see if some of their descriptors
612fe8fb19SBen Grasare ready for reading, are ready for writing, or have an exceptional
622fe8fb19SBen Grascondition pending, respectively.
632fe8fb19SBen GrasThe first
642fe8fb19SBen Gras.Fa nfds
652fe8fb19SBen Grasdescriptors are checked in each set;
662fe8fb19SBen Grasi.e., the descriptors from 0 through
672fe8fb19SBen Gras.Fa nfds Ns No \-1
682fe8fb19SBen Grasin the descriptor sets are examined.
692fe8fb19SBen GrasThis means that
702fe8fb19SBen Gras.Fa nfds
712fe8fb19SBen Grasmust be set to the highest file descriptor of the three sets, plus one.
722fe8fb19SBen GrasOn return,
732fe8fb19SBen Gras.Fn select
742fe8fb19SBen Grasand
752fe8fb19SBen Gras.Fn pselect
762fe8fb19SBen Grasreplace the given descriptor sets
772fe8fb19SBen Graswith subsets consisting of those descriptors that are ready
782fe8fb19SBen Grasfor the requested operation.
792fe8fb19SBen Gras.Fn select
802fe8fb19SBen Grasand
812fe8fb19SBen Gras.Fn pselect
822fe8fb19SBen Grasreturn the total number of ready descriptors in all the sets.
832fe8fb19SBen Gras.Pp
842fe8fb19SBen GrasThe descriptor sets are stored as bit fields in arrays of integers.
852fe8fb19SBen GrasThe following macros are provided for manipulating such descriptor sets:
862fe8fb19SBen Gras.Fn FD_ZERO fdset
872fe8fb19SBen Grasinitializes a descriptor set pointed to by
882fe8fb19SBen Gras.Fa fdset
892fe8fb19SBen Grasto the null set.
902fe8fb19SBen Gras.Fn FD_SET fd fdset
912fe8fb19SBen Grasincludes a particular descriptor
922fe8fb19SBen Gras.Fa fd
932fe8fb19SBen Grasin
942fe8fb19SBen Gras.Fa fdset .
952fe8fb19SBen Gras.Fn FD_CLR fd fdset
962fe8fb19SBen Grasremoves
972fe8fb19SBen Gras.Fa fd
982fe8fb19SBen Grasfrom
992fe8fb19SBen Gras.Fa fdset .
1002fe8fb19SBen Gras.Fn FD_ISSET fd fdset
1012fe8fb19SBen Grasis non-zero if
1022fe8fb19SBen Gras.Fa fd
1032fe8fb19SBen Grasis a member of
1042fe8fb19SBen Gras.Fa fdset ,
1052fe8fb19SBen Graszero otherwise.
1062fe8fb19SBen GrasThe behavior of these macros is undefined if
1072fe8fb19SBen Grasa descriptor value is less than zero or greater than or equal to
1082fe8fb19SBen Gras.Dv FD_SETSIZE ,
1092fe8fb19SBen Graswhich is normally at least equal
1102fe8fb19SBen Grasto the maximum number of descriptors supported by the system.
1112fe8fb19SBen Gras.Pp
1122fe8fb19SBen GrasIf
1132fe8fb19SBen Gras.Fa timeout
1142fe8fb19SBen Grasis a non-null pointer, it specifies a maximum interval to wait for the
1152fe8fb19SBen Grasselection to complete.
1162fe8fb19SBen GrasIf
1172fe8fb19SBen Gras.Fa timeout
1182fe8fb19SBen Grasis a null pointer, the select blocks indefinitely.
1192fe8fb19SBen GrasTo poll without blocking, the
1202fe8fb19SBen Gras.Fa timeout
1212fe8fb19SBen Grasargument should be non-null, pointing to a zero-valued timeval or timespec
1222fe8fb19SBen Grasstructure, as appropriate.
1232fe8fb19SBen Gras.Fa timeout
1242fe8fb19SBen Grasis not changed by
1252fe8fb19SBen Gras.Fn select ,
1262fe8fb19SBen Grasand may be reused on subsequent calls; however, it is good style to
1272fe8fb19SBen Grasre-initialize it before each invocation of
1282fe8fb19SBen Gras.Fn select .
1292fe8fb19SBen Gras.Pp
1302fe8fb19SBen GrasIf
1312fe8fb19SBen Gras.Fa sigmask
1322fe8fb19SBen Grasis a non-null pointer, then the
1332fe8fb19SBen Gras.Fn pselect
1342fe8fb19SBen Grasfunction shall replace the signal mask of the caller by the set of
1352fe8fb19SBen Grassignals pointed to by
1362fe8fb19SBen Gras.Fa sigmask
1372fe8fb19SBen Grasbefore examining the descriptors, and shall restore the signal mask
1382fe8fb19SBen Grasof the calling thread before returning.
1392fe8fb19SBen Gras.Pp
1402fe8fb19SBen GrasAny of
1412fe8fb19SBen Gras.Fa readfds ,
1422fe8fb19SBen Gras.Fa writefds ,
1432fe8fb19SBen Grasand
1442fe8fb19SBen Gras.Fa exceptfds
1452fe8fb19SBen Grasmay be given as null pointers if no descriptors are of interest.
1462fe8fb19SBen Gras.Sh NOTES
1472fe8fb19SBen GrasIt is recommended to use the
1482fe8fb19SBen Gras.Xr poll 2
1492fe8fb19SBen Grasinterface instead, which tends to be more portable and efficient.
1502fe8fb19SBen Gras.Sh RETURN VALUES
1512fe8fb19SBen Gras.Fn select
1522fe8fb19SBen Grasreturns the number of ready descriptors that are contained in
1532fe8fb19SBen Grasthe descriptor sets,
1542fe8fb19SBen Grasor \-1 if an error occurred.
1552fe8fb19SBen GrasIf the time limit expires,
1562fe8fb19SBen Gras.Fn select
1572fe8fb19SBen Grasreturns 0.
1582fe8fb19SBen GrasIf
1592fe8fb19SBen Gras.Fn select
1602fe8fb19SBen Grasreturns with an error,
1612fe8fb19SBen Grasincluding one due to an interrupted call,
1622fe8fb19SBen Grasthe descriptor sets will be unmodified.
1632fe8fb19SBen Gras.Sh EXAMPLES
1642fe8fb19SBen Gras.Bd -literal
1652fe8fb19SBen Gras#include \*[Lt]stdio.h\*[Gt]
1662fe8fb19SBen Gras#include \*[Lt]stdlib.h\*[Gt]
1672fe8fb19SBen Gras#include \*[Lt]unistd.h\*[Gt]
1682fe8fb19SBen Gras#include \*[Lt]string.h\*[Gt]
1692fe8fb19SBen Gras#include \*[Lt]err.h\*[Gt]
1702fe8fb19SBen Gras#include \*[Lt]errno.h\*[Gt]
1712fe8fb19SBen Gras#include \*[Lt]sys/types.h\*[Gt]
1722fe8fb19SBen Gras#include \*[Lt]sys/time.h\*[Gt]
1732fe8fb19SBen Gras
1742fe8fb19SBen Grasint
1752fe8fb19SBen Grasmain(int argc, char **argv)
1762fe8fb19SBen Gras{
1772fe8fb19SBen Gras	fd_set read_set;
1782fe8fb19SBen Gras	struct timeval timeout;
1792fe8fb19SBen Gras	int ret, fd, i;
1802fe8fb19SBen Gras
1812fe8fb19SBen Gras	/* file descriptor 1 is stdout */
1822fe8fb19SBen Gras	fd = 1;
1832fe8fb19SBen Gras
1842fe8fb19SBen Gras	/* Wait for ten seconds. */
1852fe8fb19SBen Gras	timeout.tv_sec = 10;
1862fe8fb19SBen Gras	timeout.tv_usec = 0;
1872fe8fb19SBen Gras
1882fe8fb19SBen Gras	/* Initialize the read set to null */
1892fe8fb19SBen Gras	FD_ZERO(\*[Am]read_set);
1902fe8fb19SBen Gras
1912fe8fb19SBen Gras	/* Add file descriptor 1 to read_set */
1922fe8fb19SBen Gras	FD_SET(fd, \*[Am]read_set);
1932fe8fb19SBen Gras
1942fe8fb19SBen Gras	/*
195*84d9c625SLionel Sambuc	 * Check if data is ready to be read on
1962fe8fb19SBen Gras	 * file descriptor 1, give up after 10 seconds.
1972fe8fb19SBen Gras	 */
1982fe8fb19SBen Gras	ret = select(fd + 1, \*[Am]read_set, NULL, NULL, \*[Am]timeout);
1992fe8fb19SBen Gras
2002fe8fb19SBen Gras	/*
2012fe8fb19SBen Gras	 * Returned value is the number of file
2022fe8fb19SBen Gras	 * descriptors ready for I/O, or -1 on error.
2032fe8fb19SBen Gras	 */
2042fe8fb19SBen Gras	switch (ret) {
2052fe8fb19SBen Gras	case \-1:
2062fe8fb19SBen Gras		err(EXIT_FAILURE, "select() failed");
2072fe8fb19SBen Gras		break;
2082fe8fb19SBen Gras
2092fe8fb19SBen Gras	case 0:
2102fe8fb19SBen Gras		printf("Timeout, no data received.\en");
2112fe8fb19SBen Gras		break;
2122fe8fb19SBen Gras
2132fe8fb19SBen Gras	default:
214*84d9c625SLionel Sambuc		printf("Data received on %d file descriptor(s)\en", ret);
2152fe8fb19SBen Gras
2162fe8fb19SBen Gras		/*
2172fe8fb19SBen Gras		 * select(2) hands back a file descriptor set where
2182fe8fb19SBen Gras		 * only descriptors ready for I/O are set. These can
2192fe8fb19SBen Gras		 * be tested using FD_ISSET
2202fe8fb19SBen Gras		 */
2212fe8fb19SBen Gras		for (i = 0; i \*[Lt]= fd; i++) {
2222fe8fb19SBen Gras			if (FD_ISSET(i, \*[Am]read_set)) {
2232fe8fb19SBen Gras				printf("Data on file descriptor %d\en", i);
2242fe8fb19SBen Gras				/* Remove the file descriptor from the set */
2252fe8fb19SBen Gras				FD_CLR(fd, \*[Am]read_set);
2262fe8fb19SBen Gras			}
2272fe8fb19SBen Gras		}
2282fe8fb19SBen Gras		break;
2292fe8fb19SBen Gras	}
2302fe8fb19SBen Gras
2312fe8fb19SBen Gras	return 0;
2322fe8fb19SBen Gras}
2332fe8fb19SBen Gras.Ed
2342fe8fb19SBen Gras.Sh ERRORS
2352fe8fb19SBen GrasAn error return from
2362fe8fb19SBen Gras.Fn select
2372fe8fb19SBen Grasindicates:
2382fe8fb19SBen Gras.Bl -tag -width Er
2392fe8fb19SBen Gras.It Bq Er EBADF
2402fe8fb19SBen GrasOne of the descriptor sets specified an invalid descriptor.
2412fe8fb19SBen Gras.It Bq Er EFAULT
2422fe8fb19SBen GrasOne or more of
2432fe8fb19SBen Gras.Fa readfds ,
2442fe8fb19SBen Gras.Fa writefds ,
2452fe8fb19SBen Grasor
2462fe8fb19SBen Gras.Fa exceptfds
2472fe8fb19SBen Graspoints outside the process's allocated address space.
2482fe8fb19SBen Gras.It Bq Er EINTR
2492fe8fb19SBen GrasA signal was delivered before the time limit expired and
2502fe8fb19SBen Grasbefore any of the selected events occurred.
2512fe8fb19SBen Gras.It Bq Er EINVAL
2522fe8fb19SBen GrasThe specified time limit is invalid.
2532fe8fb19SBen GrasOne of its components is negative or too large.
2542fe8fb19SBen Gras.El
2552fe8fb19SBen Gras.Sh SEE ALSO
2562fe8fb19SBen Gras.Xr accept 2 ,
2572fe8fb19SBen Gras.Xr connect 2 ,
2582fe8fb19SBen Gras.Xr gettimeofday 2 ,
2592fe8fb19SBen Gras.Xr poll 2 ,
2602fe8fb19SBen Gras.Xr read 2 ,
2612fe8fb19SBen Gras.Xr recv 2 ,
2622fe8fb19SBen Gras.Xr send 2 ,
2632fe8fb19SBen Gras.Xr write 2 ,
2642fe8fb19SBen Gras.Xr getdtablesize 3
2652fe8fb19SBen Gras.Sh HISTORY
2662fe8fb19SBen GrasThe
2672fe8fb19SBen Gras.Fn select
2682fe8fb19SBen Grasfunction call appeared in
2692fe8fb19SBen Gras.Bx 4.2 .
2702fe8fb19SBen Gras.Sh BUGS
2712fe8fb19SBen GrasAlthough the provision of
2722fe8fb19SBen Gras.Xr getdtablesize 3
2732fe8fb19SBen Graswas intended to allow user programs to be written independent
2742fe8fb19SBen Grasof the kernel limit on the number of open files, the dimension
2752fe8fb19SBen Grasof a sufficiently large bit field for select remains a problem.
2762fe8fb19SBen GrasThe default bit size of
2772fe8fb19SBen Gras.Ft fd_set
2782fe8fb19SBen Grasis based on the symbol
2792fe8fb19SBen Gras.Dv FD_SETSIZE
2802fe8fb19SBen Gras(currently 256),
2812fe8fb19SBen Grasbut that is somewhat smaller than the current kernel limit
2822fe8fb19SBen Grasto the number of open files.
2832fe8fb19SBen GrasHowever, in order to accommodate programs which might potentially
2842fe8fb19SBen Grasuse a larger number of open files with select, it is possible
2852fe8fb19SBen Grasto increase this size within a program by providing
2862fe8fb19SBen Grasa larger definition of
2872fe8fb19SBen Gras.Dv FD_SETSIZE
2882fe8fb19SBen Grasbefore the inclusion of
2892fe8fb19SBen Gras.In sys/types.h .
2902fe8fb19SBen GrasThe kernel will cope, and the userland libraries provided with the
2912fe8fb19SBen Grassystem are also ready for large numbers of file descriptors.
2922fe8fb19SBen Gras.Pp
2932fe8fb19SBen GrasNote:
2942fe8fb19SBen Gras.Xr rpc 3
2952fe8fb19SBen Graslibrary uses
2962fe8fb19SBen Gras.Ft fd_set
2972fe8fb19SBen Graswith the default
2982fe8fb19SBen Gras.Dv FD_SETSIZE
2992fe8fb19SBen Grasas part of its ABI.
3002fe8fb19SBen GrasTherefore, programs that use
3012fe8fb19SBen Gras.Xr rpc 3
3022fe8fb19SBen Grasroutines cannot change
3032fe8fb19SBen Gras.Dv FD_SETSIZE .
3042fe8fb19SBen Gras.Pp
3052fe8fb19SBen GrasAlternatively, to be really safe, it is possible to allocate
3062fe8fb19SBen Gras.Ft fd_set
3072fe8fb19SBen Grasbit-arrays dynamically.
3082fe8fb19SBen GrasThe idea is to permit a program to work properly even if it is
3092fe8fb19SBen Gras.Xr execve 2 Ns 'd
3102fe8fb19SBen Graswith 4000 file descriptors pre-allocated.
3112fe8fb19SBen GrasThe following illustrates the technique which is used by
3122fe8fb19SBen Grasuserland libraries:
3132fe8fb19SBen Gras.Pp
3142fe8fb19SBen Gras.Bd -literal -offset indent -compact
3152fe8fb19SBen Gras	fd_set *fdsr;
3162fe8fb19SBen Gras	int max = fd;
3172fe8fb19SBen Gras
3182fe8fb19SBen Gras	fdsr = (fd_set *)calloc(howmany(max+1, NFDBITS),
3192fe8fb19SBen Gras	    sizeof(fd_mask));
3202fe8fb19SBen Gras	if (fdsr == NULL) {
3212fe8fb19SBen Gras		...
3222fe8fb19SBen Gras		return (-1);
3232fe8fb19SBen Gras	}
3242fe8fb19SBen Gras	FD_SET(fd, fdsr);
3252fe8fb19SBen Gras	n = select(max+1, fdsr, NULL, NULL, \*[Am]tv);
3262fe8fb19SBen Gras	...
3272fe8fb19SBen Gras	free(fdsr);
3282fe8fb19SBen Gras.Ed
3292fe8fb19SBen Gras.Pp
3302fe8fb19SBen Gras.Fn select
3312fe8fb19SBen Grasshould probably have been designed to return the time remaining from the
3322fe8fb19SBen Grasoriginal timeout, if any, by modifying the time value in place.
3332fe8fb19SBen GrasEven though some systems stupidly act in this different way, it is
3342fe8fb19SBen Grasunlikely this semantic will ever be commonly implemented, as the
3352fe8fb19SBen Graschange causes massive source code compatibility problems.
3362fe8fb19SBen GrasFurthermore, recent new standards have dictated the current behaviour.
3372fe8fb19SBen GrasIn general, due to the existence of those
3382fe8fb19SBen Grasnon-conforming systems, it is unwise to assume that the timeout
3392fe8fb19SBen Grasvalue will be unmodified by the
3402fe8fb19SBen Gras.Fn select
3412fe8fb19SBen Grascall, and the caller should reinitialize it on each invocation.
3422fe8fb19SBen GrasCalculating the delta is easily done by calling
3432fe8fb19SBen Gras.Xr gettimeofday 2
3442fe8fb19SBen Grasbefore and after the call to
3452fe8fb19SBen Gras.Fn select ,
3462fe8fb19SBen Grasand using
3472fe8fb19SBen Gras.Fn timersub
3482fe8fb19SBen Gras(as described in
3492fe8fb19SBen Gras.Xr getitimer 2 ) .
3502fe8fb19SBen Gras.Pp
3512fe8fb19SBen GrasInternally to the kernel,
3522fe8fb19SBen Gras.Fn select
3532fe8fb19SBen Grasworks poorly if multiple processes wait on the same file descriptor.
354