]>
Commit | Line | Data |
---|---|---|
6d2010ae A |
1 | /* |
2 | * Copyright (c) 2005, 2007 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
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. | |
12 | * | |
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. | |
20 | * | |
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 A |
41 | extern int __select(int, fd_set * __restrict, fd_set * __restrict, |
42 | fd_set * __restrict, struct timeval * __restrict); | |
4bd07ac2 A |
43 | #endif |
44 | int __pselect(int, fd_set * __restrict, fd_set * __restrict, | |
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 A |
48 | int __select_nocancel(int, fd_set * __restrict, fd_set * __restrict, |
49 | fd_set * __restrict, struct timeval * __restrict); | |
4bd07ac2 A |
50 | #endif |
51 | int __pselect_nocancel(int, fd_set * __restrict, fd_set * __restrict, | |
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 | */ |
61 | int | |
62 | select(int nfds, fd_set * __restrict readfds, fd_set * __restrict writefds, | |
63 | fd_set * __restrict exceptfds, struct timeval * __restrict | |
64 | #if defined(VARIANT_LEGACY) || defined(VARIANT_PRE1050) | |
65 | intimeout | |
66 | #else /* !VARIANT_LEGACY && !VARIANT_PRE1050 */ | |
67 | timeout | |
68 | #endif /* VARIANT_LEGACY || VARIANT_PRE1050 */ | |
69 | ) | |
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 | */ | |
104 | extern int __pthread_sigmask(int, const sigset_t *, sigset_t *); | |
105 | static int | |
106 | _pselect_emulated(int count, fd_set * __restrict rfds, fd_set * __restrict wfds, | |
107 | fd_set * __restrict efds, const struct timespec * __restrict timo, | |
108 | const sigset_t * __restrict mask) | |
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); | |
124 | if (rv != 0) | |
125 | return rv; | |
126 | } | |
127 | ||
128 | rv = select(count, rfds, wfds, efds, tvp); | |
129 | if (mask != 0) { | |
130 | sverrno = errno; | |
131 | __pthread_sigmask(SIG_SETMASK, &omask, (sigset_t *)0); | |
132 | errno = sverrno; | |
133 | } | |
134 | ||
135 | return rv; | |
136 | } | |
137 | ||
138 | /* | |
139 | * pselect() implementation for all variants. Unlike select(), we implement the | |
140 | * darwin extension variants here to catch cases where xnu doesn't implement | |
141 | * pselect and we need to emulate. | |
142 | */ | |
143 | int | |
144 | pselect(int nfds, fd_set * __restrict readfds, fd_set * __restrict writefds, | |
145 | fd_set * __restrict exceptfds, const struct timespec * __restrict | |
146 | #if defined(VARIANT_LEGACY) || defined(VARIANT_PRE1050) | |
147 | intimeout, | |
148 | #else /* !VARIANT_LEGACY && !VARIANT_PRE1050 */ | |
149 | timeout, | |
150 | #endif /* VARIANT_LEGACY || VARIANT_PRE1050 */ | |
151 | const sigset_t * __restrict sigmask) | |
152 | { | |
153 | int ret; | |
154 | #if defined(VARIANT_LEGACY) || defined(VARIANT_PRE1050) | |
155 | struct timespec tb; | |
156 | const struct timespec *timeout; | |
157 | ||
158 | /* | |
159 | * Legacy select behavior is minimum 10 msec when tv_usec is non-zero | |
160 | */ | |
161 | if (intimeout && intimeout->tv_sec == 0 && intimeout->tv_nsec > 0 && intimeout->tv_nsec < 10000000L) { | |
162 | tb.tv_sec = 0; | |
163 | tb.tv_nsec = 10000000L; | |
164 | timeout = &tb; | |
165 | } else { | |
166 | timeout = intimeout; | |
167 | } | |
168 | #elif defined(VARIANT_DARWIN_EXTSN) | |
169 | #else | |
170 | /* 1050 variant */ | |
171 | if (nfds > FD_SETSIZE) { | |
172 | errno = EINVAL; | |
173 | return -1; | |
174 | } | |
175 | #endif | |
176 | ||
177 | #if defined(VARIANT_CANCELABLE) || defined(VARIANT_PRE1050) | |
178 | ret = __pselect(nfds, readfds, writefds, exceptfds, timeout, sigmask); | |
179 | #else /* !VARIANT_CANCELABLE && !VARIANT_PRE1050 */ | |
180 | ret = __pselect_nocancel(nfds, readfds, writefds, exceptfds, timeout, sigmask); | |
181 | #endif /* VARIANT_CANCELABLE || VARIANT_PRE1050 */ | |
182 | ||
183 | if (ret == -1 && errno == ENOSYS) { | |
184 | ret = _pselect_emulated(nfds, readfds, writefds, exceptfds, timeout, sigmask); | |
185 | } | |
186 | ||
187 | return ret; | |
188 | } |