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
29#include <sys/select.h>
30#include "_errno.h"
31
32#if defined(VARIANT_CANCELABLE) || defined(VARIANT_PRE1050)
33extern int __select(int, fd_set * __restrict, fd_set * __restrict,
34	fd_set * __restrict, struct timeval * __restrict);
35#else /* !VARIANT_CANCELABLE && !VARIANT_PRE1050 */
36int __select_nocancel(int, fd_set * __restrict, fd_set * __restrict,
37	fd_set * __restrict, struct timeval * __restrict);
38#endif /* VARIANT_CANCELABLE || VARIANT_PRE1050 */
39
40/*
41 * select stub, return error if nfds > FD_SETSIZE
42 * add pthread cancelability
43 * mandated for conformance.
44 *
45 * This is only for (non DARWINEXTSN) UNIX03 (both cancelable and
46 * non-cancelable) and for legacy
47 */
48int
49select(int nfds, fd_set * __restrict readfds, fd_set * __restrict writefds,
50	fd_set * __restrict exceptfds, struct timeval * __restrict
51#if defined(VARIANT_LEGACY) || defined(VARIANT_PRE1050)
52	intimeout
53#else /* !VARIANT_LEGACY && !VARIANT_PRE1050 */
54	timeout
55#endif /* VARIANT_LEGACY || VARIANT_PRE1050 */
56	)
57{
58
59#if defined(VARIANT_LEGACY) || defined(VARIANT_PRE1050)
60	struct timeval tb, *timeout;
61
62	/*
63	 * Legacy select behavior is minimum 10 msec when tv_usec is non-zero
64	 */
65	if (intimeout && intimeout->tv_sec == 0 && intimeout->tv_usec > 0 && intimeout->tv_usec < 10000) {
66		tb.tv_sec = 0;
67		tb.tv_usec = 10000;
68		timeout = &tb;
69	} else
70		timeout = intimeout;
71#else /* !VARIANT_LEGACY && !VARIANT_PRE1050 */
72	if (nfds > FD_SETSIZE) {
73		errno = EINVAL;
74		return -1;
75	}
76#endif /* VARIANT_LEGACY || VARIANT_PRE1050 */
77#if defined(VARIANT_CANCELABLE) || defined(VARIANT_PRE1050)
78	return __select(nfds, readfds, writefds, exceptfds, timeout);
79#else /* !VARIANT_CANCELABLE && !VARIANT_PRE1050 */
80	return __select_nocancel(nfds, readfds, writefds, exceptfds, timeout);
81#endif /* VARIANT_CANCELABLE || VARIANT_PRE1050 */
82}
83