| 1 | /* Copyright (C) 1997-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <signal.h> |
| 20 | #define __need_NULL |
| 21 | #include <stddef.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #include <nptl/pthreadP.h> |
| 25 | #include <sysdep-cancel.h> |
| 26 | #include <sys/syscall.h> |
| 27 | |
| 28 | #ifdef __NR_rt_sigtimedwait |
| 29 | |
| 30 | /* Return any pending signal or wait for one for the given time. */ |
| 31 | static int |
| 32 | do_sigwait (const sigset_t *set, int *sig) |
| 33 | { |
| 34 | int ret; |
| 35 | |
| 36 | #ifdef SIGCANCEL |
| 37 | sigset_t tmpset; |
| 38 | if (set != NULL |
| 39 | && (__builtin_expect (__sigismember (set, SIGCANCEL), 0) |
| 40 | # ifdef SIGSETXID |
| 41 | || __builtin_expect (__sigismember (set, SIGSETXID), 0) |
| 42 | # endif |
| 43 | )) |
| 44 | { |
| 45 | /* Create a temporary mask without the bit for SIGCANCEL set. */ |
| 46 | // We are not copying more than we have to. |
| 47 | memcpy (&tmpset, set, _NSIG / 8); |
| 48 | __sigdelset (&tmpset, SIGCANCEL); |
| 49 | # ifdef SIGSETXID |
| 50 | __sigdelset (&tmpset, SIGSETXID); |
| 51 | # endif |
| 52 | set = &tmpset; |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | /* XXX The size argument hopefully will have to be changed to the |
| 57 | real size of the user-level sigset_t. */ |
| 58 | #ifdef INTERNAL_SYSCALL |
| 59 | INTERNAL_SYSCALL_DECL (err); |
| 60 | do |
| 61 | ret = INTERNAL_SYSCALL (rt_sigtimedwait, err, 4, set, |
| 62 | NULL, NULL, _NSIG / 8); |
| 63 | while (INTERNAL_SYSCALL_ERROR_P (ret, err) |
| 64 | && INTERNAL_SYSCALL_ERRNO (ret, err) == EINTR); |
| 65 | if (! INTERNAL_SYSCALL_ERROR_P (ret, err)) |
| 66 | { |
| 67 | *sig = ret; |
| 68 | ret = 0; |
| 69 | } |
| 70 | else |
| 71 | ret = INTERNAL_SYSCALL_ERRNO (ret, err); |
| 72 | #else |
| 73 | do |
| 74 | ret = INLINE_SYSCALL (rt_sigtimedwait, 4, set, NULL, NULL, _NSIG / 8); |
| 75 | while (ret == -1 && errno == EINTR); |
| 76 | if (ret != -1) |
| 77 | { |
| 78 | *sig = ret; |
| 79 | ret = 0; |
| 80 | } |
| 81 | else |
| 82 | ret = errno; |
| 83 | #endif |
| 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | int |
| 89 | __sigwait (const sigset_t *set, int *sig) |
| 90 | { |
| 91 | if (SINGLE_THREAD_P) |
| 92 | return do_sigwait (set, sig); |
| 93 | |
| 94 | int oldtype = LIBC_CANCEL_ASYNC (); |
| 95 | |
| 96 | int result = do_sigwait (set, sig); |
| 97 | |
| 98 | LIBC_CANCEL_RESET (oldtype); |
| 99 | |
| 100 | return result; |
| 101 | } |
| 102 | libc_hidden_def (__sigwait) |
| 103 | weak_alias (__sigwait, sigwait) |
| 104 | #else |
| 105 | # include <sysdeps/posix/sigwait.c> |
| 106 | #endif |
| 107 | strong_alias (__sigwait, __libc_sigwait) |
| 108 | |