Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <signal.h> |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 30 | #include <string.h> |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 31 | |
Josh Gao | 5074e7d | 2019-12-13 13:55:53 -0800 | [diff] [blame] | 32 | #include <platform/bionic/reserved_signals.h> |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 33 | |
Elliott Hughes | 36f451a | 2014-09-10 15:20:40 -0700 | [diff] [blame] | 34 | extern "C" void __restore_rt(void); |
| 35 | extern "C" void __restore(void); |
| 36 | |
Elliott Hughes | 1cff9a8 | 2014-09-16 15:49:50 -0700 | [diff] [blame] | 37 | #if defined(__LP64__) |
| 38 | |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 39 | extern "C" int __rt_sigaction(int, const struct __kernel_sigaction*, struct __kernel_sigaction*, size_t); |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 40 | |
| 41 | int sigaction(int signal, const struct sigaction* bionic_new_action, struct sigaction* bionic_old_action) { |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 42 | __kernel_sigaction kernel_new_action; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 43 | if (bionic_new_action != nullptr) { |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 44 | kernel_new_action.sa_flags = bionic_new_action->sa_flags; |
| 45 | kernel_new_action.sa_handler = bionic_new_action->sa_handler; |
Josh Gao | ba40ff6 | 2019-01-22 22:53:49 -0800 | [diff] [blame] | 46 | // Don't filter signals here; if the caller asked for everything to be blocked, we should obey. |
| 47 | kernel_new_action.sa_mask = bionic_new_action->sa_mask; |
Elliott Hughes | 1cff9a8 | 2014-09-16 15:49:50 -0700 | [diff] [blame] | 48 | #if defined(SA_RESTORER) |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 49 | kernel_new_action.sa_restorer = bionic_new_action->sa_restorer; |
Elliott Hughes | 1cff9a8 | 2014-09-16 15:49:50 -0700 | [diff] [blame] | 50 | #if defined(__aarch64__) |
| 51 | // arm64 has sa_restorer, but unwinding works best if you just let the |
| 52 | // kernel supply the default restorer from [vdso]. gdb doesn't care, but |
| 53 | // libgcc needs the nop that the kernel includes before the actual code. |
| 54 | // (We could add that ourselves, but why bother?) |
| 55 | #else |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 56 | if (!(kernel_new_action.sa_flags & SA_RESTORER)) { |
| 57 | kernel_new_action.sa_flags |= SA_RESTORER; |
Elliott Hughes | 36f451a | 2014-09-10 15:20:40 -0700 | [diff] [blame] | 58 | kernel_new_action.sa_restorer = &__restore_rt; |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 59 | } |
Chris Dearman | 46f3db6 | 2014-01-30 20:01:19 -0800 | [diff] [blame] | 60 | #endif |
Elliott Hughes | 1cff9a8 | 2014-09-16 15:49:50 -0700 | [diff] [blame] | 61 | #endif |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | __kernel_sigaction kernel_old_action; |
| 65 | int result = __rt_sigaction(signal, |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 66 | (bionic_new_action != nullptr) ? &kernel_new_action : nullptr, |
| 67 | (bionic_old_action != nullptr) ? &kernel_old_action : nullptr, |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 68 | sizeof(sigset_t)); |
| 69 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 70 | if (bionic_old_action != nullptr) { |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 71 | bionic_old_action->sa_flags = kernel_old_action.sa_flags; |
| 72 | bionic_old_action->sa_handler = kernel_old_action.sa_handler; |
| 73 | bionic_old_action->sa_mask = kernel_old_action.sa_mask; |
Elliott Hughes | 1cff9a8 | 2014-09-16 15:49:50 -0700 | [diff] [blame] | 74 | #if defined(SA_RESTORER) |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 75 | bionic_old_action->sa_restorer = kernel_old_action.sa_restorer; |
Chris Dearman | 46f3db6 | 2014-01-30 20:01:19 -0800 | [diff] [blame] | 76 | #endif |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return result; |
Elliott Hughes | 1cff9a8 | 2014-09-16 15:49:50 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 82 | __strong_alias(sigaction64, sigaction); |
| 83 | |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 84 | #else |
Elliott Hughes | 1cff9a8 | 2014-09-16 15:49:50 -0700 | [diff] [blame] | 85 | |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 86 | extern "C" int __rt_sigaction(int, const struct sigaction64*, struct sigaction64*, size_t); |
Elliott Hughes | 1cff9a8 | 2014-09-16 15:49:50 -0700 | [diff] [blame] | 87 | |
Josh Gao | 11623dd | 2020-05-19 14:31:45 -0700 | [diff] [blame^] | 88 | // sigaction and sigaction64 get interposed in ART: ensure that we don't end up calling |
| 89 | // sigchain sigaction -> bionic sigaction -> sigchain sigaction64 -> bionic sigaction64 |
| 90 | // by extracting the implementation of sigaction64 to a static function. |
| 91 | static int __sigaction64(int signal, const struct sigaction64* bionic_new, |
| 92 | struct sigaction64* bionic_old) { |
| 93 | struct sigaction64 kernel_new; |
| 94 | if (bionic_new) { |
| 95 | kernel_new = *bionic_new; |
| 96 | #if defined(SA_RESTORER) |
| 97 | if (!(kernel_new.sa_flags & SA_RESTORER)) { |
| 98 | kernel_new.sa_flags |= SA_RESTORER; |
| 99 | kernel_new.sa_restorer = (kernel_new.sa_flags & SA_SIGINFO) ? &__restore_rt : &__restore; |
| 100 | } |
| 101 | #endif |
| 102 | // Don't filter signals here; if the caller asked for everything to be blocked, we should obey. |
| 103 | kernel_new.sa_mask = kernel_new.sa_mask; |
| 104 | } |
| 105 | |
| 106 | return __rt_sigaction(signal, bionic_new ? &kernel_new : nullptr, bionic_old, |
| 107 | sizeof(kernel_new.sa_mask)); |
| 108 | } |
| 109 | |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 110 | int sigaction(int signal, const struct sigaction* bionic_new, struct sigaction* bionic_old) { |
Elliott Hughes | 1cff9a8 | 2014-09-16 15:49:50 -0700 | [diff] [blame] | 111 | // The 32-bit ABI is broken. struct sigaction includes a too-small sigset_t, |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 112 | // so we have to translate to struct sigaction64 first. |
| 113 | struct sigaction64 kernel_new; |
| 114 | if (bionic_new) { |
| 115 | kernel_new = {}; |
| 116 | kernel_new.sa_flags = bionic_new->sa_flags; |
| 117 | kernel_new.sa_handler = bionic_new->sa_handler; |
Goran Jakovljevic | 87c6aac | 2018-02-07 11:05:54 +0100 | [diff] [blame] | 118 | #if defined(SA_RESTORER) |
Evgeny Eltsin | 11f6076 | 2018-02-05 13:33:35 +0100 | [diff] [blame] | 119 | kernel_new.sa_restorer = bionic_new->sa_restorer; |
Goran Jakovljevic | 87c6aac | 2018-02-07 11:05:54 +0100 | [diff] [blame] | 120 | #endif |
Josh Gao | ba40ff6 | 2019-01-22 22:53:49 -0800 | [diff] [blame] | 121 | // Don't filter signals here; if the caller asked for everything to be blocked, we should obey. |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 122 | memcpy(&kernel_new.sa_mask, &bionic_new->sa_mask, sizeof(bionic_new->sa_mask)); |
Elliott Hughes | 36f451a | 2014-09-10 15:20:40 -0700 | [diff] [blame] | 123 | } |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 124 | |
| 125 | struct sigaction64 kernel_old; |
Josh Gao | 11623dd | 2020-05-19 14:31:45 -0700 | [diff] [blame^] | 126 | int result = __sigaction64(signal, bionic_new ? &kernel_new : nullptr, &kernel_old); |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 127 | if (bionic_old) { |
| 128 | *bionic_old = {}; |
| 129 | bionic_old->sa_flags = kernel_old.sa_flags; |
| 130 | bionic_old->sa_handler = kernel_old.sa_handler; |
Goran Jakovljevic | 87c6aac | 2018-02-07 11:05:54 +0100 | [diff] [blame] | 131 | #if defined(SA_RESTORER) |
Evgeny Eltsin | 11f6076 | 2018-02-05 13:33:35 +0100 | [diff] [blame] | 132 | bionic_old->sa_restorer = kernel_old.sa_restorer; |
Goran Jakovljevic | 87c6aac | 2018-02-07 11:05:54 +0100 | [diff] [blame] | 133 | #endif |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 134 | memcpy(&bionic_old->sa_mask, &kernel_old.sa_mask, sizeof(bionic_old->sa_mask)); |
| 135 | } |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | int sigaction64(int signal, const struct sigaction64* bionic_new, struct sigaction64* bionic_old) { |
Josh Gao | 11623dd | 2020-05-19 14:31:45 -0700 | [diff] [blame^] | 140 | return __sigaction64(signal, bionic_new, bionic_old); |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 141 | } |
Elliott Hughes | 1cff9a8 | 2014-09-16 15:49:50 -0700 | [diff] [blame] | 142 | |
| 143 | #endif |