OSDN Git Service

3f1dd28a2cd20f979a899468761bac336f61f7a5
[uclinux-h8/uclibc-ng.git] / libc / sysdeps / linux / common / pselect.c
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.
4
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.
9
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.
14
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/>.  */
18
19 #include <features.h>
20
21 #ifdef __USE_XOPEN2K
22
23 #include <sys/syscall.h>
24 #include <sys/select.h>
25 #include <sys/time.h>
26 #include <signal.h>
27 #include <cancel.h>
28
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)
32 {
33 #ifdef __NR_pselect6
34 #define NSEC_PER_SEC 1000000000L
35         struct timespec _ts, *ts = 0;
36         if (timeout) {
37                 /* The Linux kernel can in some situations update the timeout value.
38                  * We do not want that so use a local variable.
39                  */
40                 _ts = *timeout;
41
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.
48                 */
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;
52                 }
53
54                 ts = &_ts;
55         }
56
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
61          * the syscall.
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.
73          */
74         struct {
75                 const sigset_t *sigmask;
76                 size_t sigsetsize;
77         } args67 = {
78                 sigmask,
79                 sizeof(sigset_t),
80         };
81
82         return INLINE_SYSCALL(pselect6, 6, nfds, readfds, writefds, exceptfds, ts, &args67);
83 #else
84         struct timeval tval;
85         int retval;
86         sigset_t savemask;
87
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.  */
91         if (timeout != NULL)
92                 TIMESPEC_TO_TIMEVAL (&tval, timeout);
93
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
96            help.  */
97         if (sigmask != NULL)
98                 sigprocmask (SIG_SETMASK, sigmask, &savemask);
99
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);
106
107         if (sigmask != NULL)
108                 sigprocmask (SIG_SETMASK, &savemask, NULL);
109
110         return retval;
111 #endif
112 }
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))
116
117 #endif