yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 1997,1998,1999,2000,2002,2003 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, write to the Free |
| 16 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 17 | 02111-1307 USA. |
| 18 | |
| 19 | Totally hacked up for uClibc by Erik Andersen <andersen@codepoet.org> |
| 20 | */ |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <signal.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/syscall.h> |
| 26 | #include <bits/kernel_sigaction.h> |
| 27 | |
| 28 | #define SA_RESTORER 0x04000000 |
| 29 | extern void __default_sa_restorer(void); |
| 30 | extern void __default_rt_sa_restorer(void); |
| 31 | |
| 32 | extern __typeof(sigaction) __libc_sigaction; |
| 33 | |
| 34 | /* When RT signals are in use we need to use a different return stub. */ |
| 35 | #ifdef __NR_rt_sigreturn |
| 36 | #define choose_restorer(flags) \ |
| 37 | (flags & SA_SIGINFO) ? __default_rt_sa_restorer \ |
| 38 | : __default_sa_restorer |
| 39 | #else |
| 40 | #define choose_restorer(flags) \ |
| 41 | __default_sa_restorer |
| 42 | #endif |
| 43 | |
| 44 | |
| 45 | #ifdef __NR_rt_sigaction |
| 46 | |
| 47 | /* If ACT is not NULL, change the action for SIG to *ACT. |
| 48 | If OACT is not NULL, put the old action for SIG in *OACT. */ |
| 49 | int __libc_sigaction(int sig, const struct sigaction *act, struct sigaction *oact) |
| 50 | { |
| 51 | struct sigaction kact; |
| 52 | if (act && !(act->sa_flags & SA_RESTORER)) { |
| 53 | memcpy(&kact, act, sizeof(kact)); |
| 54 | kact.sa_restorer = choose_restorer(kact.sa_flags); |
| 55 | kact.sa_flags |= SA_RESTORER; |
| 56 | act = &kact; |
| 57 | } |
| 58 | /* NB: kernel (as of 2.6.25) will return EINVAL |
| 59 | * if sizeof(act->sa_mask) does not match kernel's sizeof(sigset_t) */ |
| 60 | return __syscall_rt_sigaction(sig, act, oact, sizeof(act->sa_mask)); |
| 61 | } |
| 62 | |
| 63 | #else |
| 64 | |
| 65 | /* If ACT is not NULL, change the action for SIG to *ACT. |
| 66 | If OACT is not NULL, put the old action for SIG in *OACT. */ |
| 67 | int __libc_sigaction(int sig, const struct sigaction *act, struct sigaction *oact) |
| 68 | { |
| 69 | int result; |
| 70 | struct old_kernel_sigaction kact, koact; |
| 71 | |
| 72 | if (act) { |
| 73 | kact.k_sa_handler = act->sa_handler; |
| 74 | kact.sa_mask = act->sa_mask.__val[0]; |
| 75 | kact.sa_flags = act->sa_flags; |
| 76 | if (kact.sa_flags & SA_RESTORER) { |
| 77 | kact.sa_restorer = act->sa_restorer; |
| 78 | } else { |
| 79 | kact.sa_restorer = choose_restorer(kact.sa_flags); |
| 80 | kact.sa_flags |= SA_RESTORER; |
| 81 | } |
| 82 | } |
| 83 | result = __syscall_sigaction(sig, |
| 84 | act ? &kact : NULL, |
| 85 | oact ? &koact : NULL); |
| 86 | if (oact && result >= 0) { |
| 87 | oact->sa_handler = koact.k_sa_handler; |
| 88 | oact->sa_mask.__val[0] = koact.sa_mask; |
| 89 | oact->sa_flags = koact.sa_flags; |
| 90 | oact->sa_restorer = koact.sa_restorer; |
| 91 | } |
| 92 | return result; |
| 93 | } |
| 94 | |
| 95 | #endif |
| 96 | |
| 97 | |
| 98 | #ifndef LIBC_SIGACTION |
| 99 | # ifndef __UCLIBC_HAS_THREADS__ |
| 100 | strong_alias(__libc_sigaction,sigaction) |
| 101 | libc_hidden_def(sigaction) |
| 102 | # else |
| 103 | weak_alias(__libc_sigaction,sigaction) |
| 104 | libc_hidden_weak(sigaction) |
| 105 | # endif |
| 106 | #endif |