yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 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, 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 | #ifdef __UCLIBC_HAS_THREADS_NATIVE__ |
| 26 | #include <sysdep-cancel.h> |
| 27 | #endif |
| 28 | |
| 29 | libc_hidden_proto(sigprocmask) |
| 30 | libc_hidden_proto(select) |
| 31 | |
| 32 | |
| 33 | /* Check the first NFDS descriptors each in READFDS (if not NULL) for read |
| 34 | readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS |
| 35 | (if not NULL) for exceptional conditions. If TIMEOUT is not NULL, time out |
| 36 | after waiting the interval specified therein. Additionally set the sigmask |
| 37 | SIGMASK for this call. Returns the number of ready descriptors, or -1 for |
| 38 | errors. */ |
| 39 | #ifdef __UCLIBC_HAS_THREADS_NATIVE__ |
| 40 | static int |
| 41 | __pselect (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
| 42 | #else |
| 43 | int |
| 44 | pselect (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
| 45 | #endif |
| 46 | const struct timespec *timeout, const sigset_t *sigmask) |
| 47 | { |
| 48 | struct timeval tval; |
| 49 | int retval; |
| 50 | sigset_t savemask; |
| 51 | |
| 52 | /* Change nanosecond number to microseconds. This might mean losing |
| 53 | precision and therefore the `pselect` should be available. But |
| 54 | for now it is hardly found. */ |
| 55 | if (timeout != NULL) |
| 56 | TIMESPEC_TO_TIMEVAL (&tval, timeout); |
| 57 | |
| 58 | /* The setting and restoring of the signal mask and the select call |
| 59 | should be an atomic operation. This can't be done without kernel |
| 60 | help. */ |
| 61 | if (sigmask != NULL) |
| 62 | sigprocmask (SIG_SETMASK, sigmask, &savemask); |
| 63 | |
| 64 | /* Note the pselect() is a cancellation point. But since we call |
| 65 | select() which itself is a cancellation point we do not have |
| 66 | to do anything here. */ |
| 67 | retval = select (nfds, readfds, writefds, exceptfds, |
| 68 | timeout != NULL ? &tval : NULL); |
| 69 | |
| 70 | if (sigmask != NULL) |
| 71 | sigprocmask (SIG_SETMASK, &savemask, NULL); |
| 72 | |
| 73 | return retval; |
| 74 | } |
| 75 | |
| 76 | #ifdef __UCLIBC_HAS_THREADS_NATIVE__ |
| 77 | int |
| 78 | pselect (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
| 79 | const struct timespec *timeout, const sigset_t *sigmask) |
| 80 | { |
| 81 | if (SINGLE_THREAD_P) |
| 82 | return __pselect (nfds, readfds, writefds, exceptfds, |
| 83 | timeout, sigmask); |
| 84 | |
| 85 | int oldtype = LIBC_CANCEL_ASYNC (); |
| 86 | |
| 87 | int result = __pselect (nfds, readfds, writefds, exceptfds, |
| 88 | timeout, sigmask); |
| 89 | |
| 90 | LIBC_CANCEL_RESET (oldtype); |
| 91 | |
| 92 | return result; |
| 93 | } |
| 94 | #endif |