xref: /xnu-10002.81.5/libsyscall/wrappers/select-base.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2005, 2007 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*5e3eaea3SApple OSS Distributions  * file.
12*5e3eaea3SApple OSS Distributions  *
13*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*5e3eaea3SApple OSS Distributions  * limitations under the License.
20*5e3eaea3SApple OSS Distributions  *
21*5e3eaea3SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*5e3eaea3SApple OSS Distributions  */
23*5e3eaea3SApple OSS Distributions 
24*5e3eaea3SApple OSS Distributions #if defined(__LP64__) && (defined(VARIANT_CANCELABLE) || defined(VARIANT_PRE1050))
25*5e3eaea3SApple OSS Distributions #undef __DARWIN_NON_CANCELABLE
26*5e3eaea3SApple OSS Distributions #define __DARWIN_NON_CANCELABLE 0
27*5e3eaea3SApple OSS Distributions #endif /* __LP64__ && (VARIANT_CANCELABLE || VARIANT_PRE1050) */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions #if defined(VARIANT_DARWIN_EXTSN)
30*5e3eaea3SApple OSS Distributions #define _DARWIN_C_SOURCE
31*5e3eaea3SApple OSS Distributions #define _DARWIN_UNLIMITED_SELECT
32*5e3eaea3SApple OSS Distributions #endif
33*5e3eaea3SApple OSS Distributions 
34*5e3eaea3SApple OSS Distributions #include <sys/select.h>
35*5e3eaea3SApple OSS Distributions #include <sys/time.h>
36*5e3eaea3SApple OSS Distributions #include <sys/signal.h>
37*5e3eaea3SApple OSS Distributions #include "_errno.h"
38*5e3eaea3SApple OSS Distributions 
39*5e3eaea3SApple OSS Distributions #if defined(VARIANT_CANCELABLE) || defined(VARIANT_PRE1050)
40*5e3eaea3SApple OSS Distributions #if !defined(VARIANT_DARWIN_EXTSN)
41*5e3eaea3SApple OSS Distributions extern int __select(int, fd_set * __restrict, fd_set * __restrict,
42*5e3eaea3SApple OSS Distributions     fd_set * __restrict, struct timeval * __restrict);
43*5e3eaea3SApple OSS Distributions #endif
44*5e3eaea3SApple OSS Distributions int __pselect(int, fd_set * __restrict, fd_set * __restrict,
45*5e3eaea3SApple OSS Distributions     fd_set * __restrict, const struct timespec * __restrict, const sigset_t * __restrict);
46*5e3eaea3SApple OSS Distributions #else /* !VARIANT_CANCELABLE && !VARIANT_PRE1050 */
47*5e3eaea3SApple OSS Distributions #if !defined(VARIANT_DARWIN_EXTSN)
48*5e3eaea3SApple OSS Distributions int __select_nocancel(int, fd_set * __restrict, fd_set * __restrict,
49*5e3eaea3SApple OSS Distributions     fd_set * __restrict, struct timeval * __restrict);
50*5e3eaea3SApple OSS Distributions #endif
51*5e3eaea3SApple OSS Distributions int __pselect_nocancel(int, fd_set * __restrict, fd_set * __restrict,
52*5e3eaea3SApple OSS Distributions     fd_set * __restrict, const struct timespec * __restrict, const sigset_t * __restrict);
53*5e3eaea3SApple OSS Distributions #endif /* VARIANT_CANCELABLE || VARIANT_PRE1050 */
54*5e3eaea3SApple OSS Distributions 
55*5e3eaea3SApple OSS Distributions #if !defined(VARIANT_DARWIN_EXTSN)
56*5e3eaea3SApple OSS Distributions /*
57*5e3eaea3SApple OSS Distributions  * select() implementation for 1050 and legacy (cancelable and non-cancelable)
58*5e3eaea3SApple OSS Distributions  * variants. The darwin extension variants (both cancelable & non-cancelable) are
59*5e3eaea3SApple OSS Distributions  * mapped directly to the syscall stub.
60*5e3eaea3SApple OSS Distributions  */
61*5e3eaea3SApple OSS Distributions int
select(int nfds,fd_set * __restrict readfds,fd_set * __restrict writefds,fd_set * __restrict exceptfds,struct timeval * __restrict intimeout)62*5e3eaea3SApple OSS Distributions select(int nfds, fd_set * __restrict readfds, fd_set * __restrict writefds,
63*5e3eaea3SApple OSS Distributions     fd_set * __restrict exceptfds, struct timeval * __restrict
64*5e3eaea3SApple OSS Distributions #if defined(VARIANT_LEGACY) || defined(VARIANT_PRE1050)
65*5e3eaea3SApple OSS Distributions     intimeout
66*5e3eaea3SApple OSS Distributions #else /* !VARIANT_LEGACY && !VARIANT_PRE1050 */
67*5e3eaea3SApple OSS Distributions     timeout
68*5e3eaea3SApple OSS Distributions #endif /* VARIANT_LEGACY || VARIANT_PRE1050 */
69*5e3eaea3SApple OSS Distributions     )
70*5e3eaea3SApple OSS Distributions {
71*5e3eaea3SApple OSS Distributions #if defined(VARIANT_LEGACY) || defined(VARIANT_PRE1050)
72*5e3eaea3SApple OSS Distributions 	struct timeval tb, *timeout;
73*5e3eaea3SApple OSS Distributions 
74*5e3eaea3SApple OSS Distributions 	/*
75*5e3eaea3SApple OSS Distributions 	 * Legacy select behavior is minimum 10 msec when tv_usec is non-zero
76*5e3eaea3SApple OSS Distributions 	 */
77*5e3eaea3SApple OSS Distributions 	if (intimeout && intimeout->tv_sec == 0 && intimeout->tv_usec > 0 && intimeout->tv_usec < 10000) {
78*5e3eaea3SApple OSS Distributions 		tb.tv_sec = 0;
79*5e3eaea3SApple OSS Distributions 		tb.tv_usec = 10000;
80*5e3eaea3SApple OSS Distributions 		timeout = &tb;
81*5e3eaea3SApple OSS Distributions 	} else {
82*5e3eaea3SApple OSS Distributions 		timeout = intimeout;
83*5e3eaea3SApple OSS Distributions 	}
84*5e3eaea3SApple OSS Distributions #else /* !VARIANT_LEGACY && !VARIANT_PRE1050 */
85*5e3eaea3SApple OSS Distributions 	if (nfds > FD_SETSIZE) {
86*5e3eaea3SApple OSS Distributions 		errno = EINVAL;
87*5e3eaea3SApple OSS Distributions 		return -1;
88*5e3eaea3SApple OSS Distributions 	}
89*5e3eaea3SApple OSS Distributions #endif
90*5e3eaea3SApple OSS Distributions 
91*5e3eaea3SApple OSS Distributions #if defined(VARIANT_CANCELABLE) || defined(VARIANT_PRE1050)
92*5e3eaea3SApple OSS Distributions 	return __select(nfds, readfds, writefds, exceptfds, timeout);
93*5e3eaea3SApple OSS Distributions #else /* !VARIANT_CANCELABLE && !VARIANT_PRE1050 */
94*5e3eaea3SApple OSS Distributions 	return __select_nocancel(nfds, readfds, writefds, exceptfds, timeout);
95*5e3eaea3SApple OSS Distributions #endif /* VARIANT_CANCELABLE || VARIANT_PRE1050 */
96*5e3eaea3SApple OSS Distributions }
97*5e3eaea3SApple OSS Distributions #endif /* !defined(VARIANT_DARWIN_EXTSN) */
98*5e3eaea3SApple OSS Distributions 
99*5e3eaea3SApple OSS Distributions 
100*5e3eaea3SApple OSS Distributions /*
101*5e3eaea3SApple OSS Distributions  * User-space emulation of pselect() syscall for B&I
102*5e3eaea3SApple OSS Distributions  * TODO: remove when B&I move to xnu with native pselect()
103*5e3eaea3SApple OSS Distributions  */
104*5e3eaea3SApple OSS Distributions extern int __pthread_sigmask(int, const sigset_t *, sigset_t *);
105*5e3eaea3SApple OSS Distributions static int
_pselect_emulated(int count,fd_set * __restrict rfds,fd_set * __restrict wfds,fd_set * __restrict efds,const struct timespec * __restrict timo,const sigset_t * __restrict mask)106*5e3eaea3SApple OSS Distributions _pselect_emulated(int count, fd_set * __restrict rfds, fd_set * __restrict wfds,
107*5e3eaea3SApple OSS Distributions     fd_set * __restrict efds, const struct timespec * __restrict timo,
108*5e3eaea3SApple OSS Distributions     const sigset_t * __restrict mask)
109*5e3eaea3SApple OSS Distributions {
110*5e3eaea3SApple OSS Distributions 	sigset_t omask;
111*5e3eaea3SApple OSS Distributions 	struct timeval tvtimo, *tvp;
112*5e3eaea3SApple OSS Distributions 	int rv, sverrno;
113*5e3eaea3SApple OSS Distributions 
114*5e3eaea3SApple OSS Distributions 	if (timo) {
115*5e3eaea3SApple OSS Distributions 		tvtimo.tv_sec = timo->tv_sec;
116*5e3eaea3SApple OSS Distributions 		tvtimo.tv_usec = (__darwin_suseconds_t)(timo->tv_nsec / 1000);
117*5e3eaea3SApple OSS Distributions 		tvp = &tvtimo;
118*5e3eaea3SApple OSS Distributions 	} else {
119*5e3eaea3SApple OSS Distributions 		tvp = 0;
120*5e3eaea3SApple OSS Distributions 	}
121*5e3eaea3SApple OSS Distributions 
122*5e3eaea3SApple OSS Distributions 	if (mask != 0) {
123*5e3eaea3SApple OSS Distributions 		rv = __pthread_sigmask(SIG_SETMASK, mask, &omask);
124*5e3eaea3SApple OSS Distributions 		if (rv != 0) {
125*5e3eaea3SApple OSS Distributions 			return rv;
126*5e3eaea3SApple OSS Distributions 		}
127*5e3eaea3SApple OSS Distributions 	}
128*5e3eaea3SApple OSS Distributions 
129*5e3eaea3SApple OSS Distributions 	rv = select(count, rfds, wfds, efds, tvp);
130*5e3eaea3SApple OSS Distributions 	if (mask != 0) {
131*5e3eaea3SApple OSS Distributions 		sverrno = errno;
132*5e3eaea3SApple OSS Distributions 		__pthread_sigmask(SIG_SETMASK, &omask, (sigset_t *)0);
133*5e3eaea3SApple OSS Distributions 		errno = sverrno;
134*5e3eaea3SApple OSS Distributions 	}
135*5e3eaea3SApple OSS Distributions 
136*5e3eaea3SApple OSS Distributions 	return rv;
137*5e3eaea3SApple OSS Distributions }
138*5e3eaea3SApple OSS Distributions 
139*5e3eaea3SApple OSS Distributions /*
140*5e3eaea3SApple OSS Distributions  * pselect() implementation for all variants. Unlike select(), we implement the
141*5e3eaea3SApple OSS Distributions  * darwin extension variants here to catch cases where xnu doesn't implement
142*5e3eaea3SApple OSS Distributions  * pselect and we need to emulate.
143*5e3eaea3SApple OSS Distributions  */
144*5e3eaea3SApple OSS Distributions int
pselect(int nfds,fd_set * __restrict readfds,fd_set * __restrict writefds,fd_set * __restrict exceptfds,const struct timespec * __restrict intimeout,const sigset_t * __restrict sigmask)145*5e3eaea3SApple OSS Distributions pselect(int nfds, fd_set * __restrict readfds, fd_set * __restrict writefds,
146*5e3eaea3SApple OSS Distributions     fd_set * __restrict exceptfds, const struct timespec * __restrict
147*5e3eaea3SApple OSS Distributions #if defined(VARIANT_LEGACY) || defined(VARIANT_PRE1050)
148*5e3eaea3SApple OSS Distributions     intimeout,
149*5e3eaea3SApple OSS Distributions #else /* !VARIANT_LEGACY && !VARIANT_PRE1050 */
150*5e3eaea3SApple OSS Distributions     timeout,
151*5e3eaea3SApple OSS Distributions #endif /* VARIANT_LEGACY || VARIANT_PRE1050 */
152*5e3eaea3SApple OSS Distributions     const sigset_t * __restrict sigmask)
153*5e3eaea3SApple OSS Distributions {
154*5e3eaea3SApple OSS Distributions 	int ret;
155*5e3eaea3SApple OSS Distributions #if defined(VARIANT_LEGACY) || defined(VARIANT_PRE1050)
156*5e3eaea3SApple OSS Distributions 	struct timespec tb;
157*5e3eaea3SApple OSS Distributions 	const struct timespec *timeout;
158*5e3eaea3SApple OSS Distributions 
159*5e3eaea3SApple OSS Distributions 	/*
160*5e3eaea3SApple OSS Distributions 	 * Legacy select behavior is minimum 10 msec when tv_usec is non-zero
161*5e3eaea3SApple OSS Distributions 	 */
162*5e3eaea3SApple OSS Distributions 	if (intimeout && intimeout->tv_sec == 0 && intimeout->tv_nsec > 0 && intimeout->tv_nsec < 10000000L) {
163*5e3eaea3SApple OSS Distributions 		tb.tv_sec = 0;
164*5e3eaea3SApple OSS Distributions 		tb.tv_nsec = 10000000L;
165*5e3eaea3SApple OSS Distributions 		timeout = &tb;
166*5e3eaea3SApple OSS Distributions 	} else {
167*5e3eaea3SApple OSS Distributions 		timeout = intimeout;
168*5e3eaea3SApple OSS Distributions 	}
169*5e3eaea3SApple OSS Distributions #elif defined(VARIANT_DARWIN_EXTSN)
170*5e3eaea3SApple OSS Distributions #else
171*5e3eaea3SApple OSS Distributions 	/* 1050 variant */
172*5e3eaea3SApple OSS Distributions 	if (nfds > FD_SETSIZE) {
173*5e3eaea3SApple OSS Distributions 		errno = EINVAL;
174*5e3eaea3SApple OSS Distributions 		return -1;
175*5e3eaea3SApple OSS Distributions 	}
176*5e3eaea3SApple OSS Distributions #endif
177*5e3eaea3SApple OSS Distributions 
178*5e3eaea3SApple OSS Distributions #if defined(VARIANT_CANCELABLE) || defined(VARIANT_PRE1050)
179*5e3eaea3SApple OSS Distributions 	ret = __pselect(nfds, readfds, writefds, exceptfds, timeout, sigmask);
180*5e3eaea3SApple OSS Distributions #else /* !VARIANT_CANCELABLE && !VARIANT_PRE1050 */
181*5e3eaea3SApple OSS Distributions 	ret = __pselect_nocancel(nfds, readfds, writefds, exceptfds, timeout, sigmask);
182*5e3eaea3SApple OSS Distributions #endif /* VARIANT_CANCELABLE || VARIANT_PRE1050 */
183*5e3eaea3SApple OSS Distributions 
184*5e3eaea3SApple OSS Distributions 	if (ret == -1 && errno == ENOSYS) {
185*5e3eaea3SApple OSS Distributions 		ret = _pselect_emulated(nfds, readfds, writefds, exceptfds, timeout, sigmask);
186*5e3eaea3SApple OSS Distributions 	}
187*5e3eaea3SApple OSS Distributions 
188*5e3eaea3SApple OSS Distributions 	return ret;
189*5e3eaea3SApple OSS Distributions }
190