yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1997, 1998, 1999, 2000 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 Library General Public License as |
| 6 | published by the Free Software Foundation; either version 2 of the |
| 7 | 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 | Library General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Library General Public |
| 15 | License along with the GNU C Library; see the file COPYING.LIB. If not, |
| 16 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | Boston, MA 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 | |
| 30 | extern __typeof(sigaction) __libc_sigaction; |
| 31 | |
| 32 | |
| 33 | #if defined __NR_rt_sigaction |
| 34 | |
| 35 | extern void restore_rt(void) __asm__ ("__restore_rt") attribute_hidden; |
| 36 | extern void restore(void) __asm__ ("__restore") attribute_hidden; |
| 37 | |
| 38 | /* If ACT is not NULL, change the action for SIG to *ACT. |
| 39 | If OACT is not NULL, put the old action for SIG in *OACT. */ |
| 40 | int __libc_sigaction(int sig, const struct sigaction *act, struct sigaction *oact) |
| 41 | { |
| 42 | struct sigaction kact; |
| 43 | |
| 44 | if (act) { |
| 45 | memcpy(&kact, act, sizeof(kact)); |
| 46 | kact.sa_flags |= SA_RESTORER; |
| 47 | kact.sa_restorer = (act->sa_flags & SA_SIGINFO) ? &restore_rt : &restore; |
| 48 | act = &kact; |
| 49 | } |
| 50 | /* NB: kernel (as of 2.6.25) will return EINVAL |
| 51 | * if sizeof(act->sa_mask) does not match kernel's sizeof(sigset_t) */ |
| 52 | return __syscall_rt_sigaction(sig, act, oact, sizeof(act->sa_mask)); |
| 53 | } |
| 54 | |
| 55 | #else |
| 56 | |
| 57 | extern void restore(void) __asm__ ("__restore") attribute_hidden; |
| 58 | |
| 59 | /* If ACT is not NULL, change the action for SIG to *ACT. |
| 60 | If OACT is not NULL, put the old action for SIG in *OACT. */ |
| 61 | int __libc_sigaction(int sig, const struct sigaction *act, struct sigaction *oact) |
| 62 | { |
| 63 | int result; |
| 64 | struct old_kernel_sigaction kact, koact; |
| 65 | |
| 66 | if (act) { |
| 67 | kact.k_sa_handler = act->sa_handler; |
| 68 | kact.sa_mask = act->sa_mask.__val[0]; |
| 69 | kact.sa_flags = act->sa_flags | SA_RESTORER; |
| 70 | kact.sa_restorer = &restore; |
| 71 | } |
| 72 | __asm__ __volatile__ ( |
| 73 | " pushl %%ebx\n" |
| 74 | " movl %3, %%ebx\n" |
| 75 | " int $0x80\n" |
| 76 | " popl %%ebx\n" |
| 77 | : "=a" (result), "=m" (koact) |
| 78 | : "0" (__NR_sigaction), "r" (sig), "m" (kact), |
| 79 | "c" (act ? &kact : NULL), |
| 80 | "d" (oact ? &koact : NULL)); |
| 81 | if (result < 0) { |
| 82 | __set_errno(-result); |
| 83 | return -1; |
| 84 | } |
| 85 | if (oact) { |
| 86 | oact->sa_handler = koact.k_sa_handler; |
| 87 | oact->sa_mask.__val[0] = koact.sa_mask; |
| 88 | oact->sa_flags = koact.sa_flags; |
| 89 | oact->sa_restorer = koact.sa_restorer; |
| 90 | } |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | #endif |
| 95 | |
| 96 | |
| 97 | #ifndef LIBC_SIGACTION |
| 98 | # ifndef __UCLIBC_HAS_THREADS__ |
| 99 | strong_alias(__libc_sigaction,sigaction) |
| 100 | libc_hidden_def(sigaction) |
| 101 | # else |
| 102 | weak_alias(__libc_sigaction,sigaction) |
| 103 | libc_hidden_weak(sigaction) |
| 104 | # endif |
| 105 | #endif |
| 106 | |
| 107 | |
| 108 | /* NOTE: Please think twice before making any changes to the bits of |
| 109 | code below. GDB needs some intimate knowledge about it to |
| 110 | recognize them as signal trampolines, and make backtraces through |
| 111 | signal handlers work right. Important are both the names |
| 112 | (__restore and __restore_rt) and the exact instruction sequence. |
| 113 | If you ever feel the need to make any changes, please notify the |
| 114 | appropriate GDB maintainer. */ |
| 115 | |
| 116 | #define RESTORE(name, syscall) RESTORE2(name, syscall) |
| 117 | |
| 118 | #ifdef __NR_rt_sigaction |
| 119 | /* The return code for realtime-signals. */ |
| 120 | # define RESTORE2(name, syscall) \ |
| 121 | __asm__ ( \ |
| 122 | ".text\n" \ |
| 123 | "__" #name ":\n" \ |
| 124 | " movl $" #syscall ", %eax\n" \ |
| 125 | " int $0x80\n" \ |
| 126 | ); |
| 127 | RESTORE(restore_rt, __NR_rt_sigreturn) |
| 128 | #endif |
| 129 | |
| 130 | #ifdef __NR_sigreturn |
| 131 | /* For the boring old signals. */ |
| 132 | # undef RESTORE2 |
| 133 | # define RESTORE2(name, syscall) \ |
| 134 | __asm__ ( \ |
| 135 | ".text\n" \ |
| 136 | "__" #name ":\n" \ |
| 137 | " popl %eax\n" \ |
| 138 | " movl $" #syscall ", %eax\n" \ |
| 139 | " int $0x80\n" \ |
| 140 | ); |
| 141 | RESTORE(restore, __NR_sigreturn) |
| 142 | #endif |