2005-12-13 Ulrich Drepper <drepper@redhat.com>
[platform/upstream/linaro-glibc.git] / sysdeps / generic / pselect.c
1 /* Copyright (C) 1996,1997,1998,2001,2002,2003 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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <errno.h>
21 #include <signal.h>
22 #include <stddef.h>     /* For NULL.  */
23 #include <sys/time.h>
24 #include <sys/select.h>
25 #include <sysdep-cancel.h>
26
27 /* Check the first NFDS descriptors each in READFDS (if not NULL) for read
28    readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS
29    (if not NULL) for exceptional conditions.  If TIMEOUT is not NULL, time out
30    after waiting the interval specified therein.  Additionally set the sigmask
31    SIGMASK for this call.  Returns the number of ready descriptors, or -1 for
32    errors.  */
33 static int
34 do_pselect (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
35             const struct timespec *timeout, const sigset_t *sigmask)
36 {
37   struct timeval tval;
38   int retval;
39   sigset_t savemask;
40
41   /* Change nanosecond number to microseconds.  This might mean losing
42      precision and therefore the `pselect` should be available.  But
43      for now it is hardly found.  */
44   if (timeout != NULL)
45     TIMESPEC_TO_TIMEVAL (&tval, timeout);
46
47   /* The setting and restoring of the signal mask and the select call
48      should be an atomic operation.  This can't be done without kernel
49      help.  */
50   if (sigmask != NULL)
51     __sigprocmask (SIG_SETMASK, sigmask, &savemask);
52
53   /* Note the pselect() is a cancellation point.  But since we call
54      select() which itself is a cancellation point we do not have
55      to do anything here.  */
56   retval = __select (nfds, readfds, writefds, exceptfds,
57                      timeout != NULL ? &tval : NULL);
58
59   if (sigmask != NULL)
60     __sigprocmask (SIG_SETMASK, &savemask, NULL);
61
62   return retval;
63 }
64
65
66 int
67 __pselect (nfds, readfds, writefds, exceptfds, timeout, sigmask)
68      int nfds;
69      fd_set *readfds;
70      fd_set *writefds;
71      fd_set *exceptfds;
72      const struct timespec *timeout;
73      const sigset_t *sigmask;
74 {
75   if (SINGLE_THREAD_P)
76     return do_pselect (nfds, readfds, writefds, exceptfds, timeout, sigmask);
77
78   int oldtype = LIBC_CANCEL_ASYNC ();
79
80   int result = do_pselect (nfds, readfds, writefds, exceptfds, timeout,
81                            sigmask);
82
83   LIBC_CANCEL_RESET (oldtype);
84
85   return result;
86 }
87 weak_alias (__pselect, pselect)
88 strong_alias (__pselect, __libc_pselect)