1 /* Copyright (C) 1996-1998,2001,2002,2003,2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
23 #include <sys/syscall.h>
24 #include <sys/select.h>
29 static int __NC(pselect)(int nfds, fd_set *readfds, fd_set *writefds,
30 fd_set *exceptfds, const struct timespec *timeout,
31 const sigset_t *sigmask)
34 #define NSEC_PER_SEC 1000000000L
35 struct timespec _ts, *ts = 0;
37 /* The Linux kernel can in some situations update the timeout value.
38 * We do not want that so use a local variable.
42 /* GNU extension: allow for timespec values where the sub-sec
43 * field is equal to or more than 1 second. The kernel will
44 * reject this on us, so take care of the time shift ourself.
45 * Some applications (like readline and linphone) do this.
46 * See 'clarification on select() type calls and invalid timeouts'
47 * on the POSIX general list for more information.
49 if (_ts.tv_nsec >= NSEC_PER_SEC) {
50 _ts.tv_sec += _ts.tv_nsec / NSEC_PER_SEC;
51 _ts.tv_nsec %= NSEC_PER_SEC;
57 /* The pselect6 syscall API is strange. It wants a 7th arg to be
58 * the sizeof(*sigmask). However syscalls with > 6 arguments aren't
59 * supported on linux. So arguments 6 and 7 are stuffed in a struct
60 * and a pointer to that struct is passed as the 6th argument to
62 * Glibc stuffs arguments 6 and 7 in a ulong[2]. Linux reads
63 * them as if there were a struct { sigset_t*; size_t } in
64 * userspace. There woudl be trouble if userspace and the kernel are
65 * compiled differently enough that size_t isn't the same as ulong,
66 * but not enough to trigger the compat layer in linux. I can't
67 * think of such a case, so I'm using linux's struct.
68 * Furthermore Glibc sets the sigsetsize to _NSIG/8. However linux
69 * checks for sizeof(sigset_t), which internally is a ulong array.
70 * This means that if _NSIG isn't a multiple of BITS_PER_LONG then
71 * linux will refuse glibc's value. So I prefer sizeof(sigset_t) for
72 * the value of sigsetsize.
75 const sigset_t *sigmask;
82 return INLINE_SYSCALL(pselect6, 6, nfds, readfds, writefds, exceptfds, ts, &args67);
88 /* Change nanosecond number to microseconds. This might mean losing
89 precision and therefore the `pselect` should be available. But
90 for now it is hardly found. */
92 TIMESPEC_TO_TIMEVAL (&tval, timeout);
94 /* The setting and restoring of the signal mask and the select call
95 should be an atomic operation. This can't be done without kernel
98 sigprocmask (SIG_SETMASK, sigmask, &savemask);
100 /* The comment below does not apply on uClibc, since we use __select_nocancel */
101 /* Note the pselect() is a cancellation point. But since we call
102 select() which itself is a cancellation point we do not have
103 to do anything here. */
104 retval = __NC(select)(nfds, readfds, writefds, exceptfds,
105 timeout != NULL ? &tval : NULL);
108 sigprocmask (SIG_SETMASK, &savemask, NULL);
113 CANCELLABLE_SYSCALL(int, pselect, (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
114 const struct timespec *timeout, const sigset_t *sigmask),
115 (nfds, readfds, writefds, exceptfds, timeout, sigmask))