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