blob: a84886be9bb878ad88f185956d7a53eea3bc12f8 [file] [log] [blame]
Elliott Hughesc7e9b232013-10-16 22:27:54 -07001/*
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 Hughes3e235912018-02-01 14:21:51 -080030#include <string.h>
Elliott Hughesc7e9b232013-10-16 22:27:54 -070031
Josh Gao5074e7d2019-12-13 13:55:53 -080032#include <platform/bionic/reserved_signals.h>
Josh Gao6fcba932018-02-09 13:38:32 -080033
Elliott Hughes36f451a2014-09-10 15:20:40 -070034extern "C" void __restore_rt(void);
35extern "C" void __restore(void);
36
Elliott Hughes1cff9a82014-09-16 15:49:50 -070037#if defined(__LP64__)
38
Elliott Hughesc7e9b232013-10-16 22:27:54 -070039extern "C" int __rt_sigaction(int, const struct __kernel_sigaction*, struct __kernel_sigaction*, size_t);
Elliott Hughesc7e9b232013-10-16 22:27:54 -070040
41int sigaction(int signal, const struct sigaction* bionic_new_action, struct sigaction* bionic_old_action) {
Elliott Hughes20a36612024-01-24 14:25:00 -080042 __kernel_sigaction kernel_new_action = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -070043 if (bionic_new_action != nullptr) {
Elliott Hughesc7e9b232013-10-16 22:27:54 -070044 kernel_new_action.sa_flags = bionic_new_action->sa_flags;
45 kernel_new_action.sa_handler = bionic_new_action->sa_handler;
Josh Gaoba40ff62019-01-22 22:53:49 -080046 // 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 Hughes20a36612024-01-24 14:25:00 -080048#if defined(__x86_64__)
49 // riscv64 doesn't have sa_restorer. For arm64 and 32-bit x86, unwinding
50 // works best if you just let the kernel supply the default restorer
51 // from [vdso]. gdb doesn't care, but libgcc needs the nop that the
52 // kernel includes before the actual code. (We could add that ourselves,
53 // but why bother?)
54 // TODO: why do arm32 and x86-64 need this to unwind through signal handlers?
Elliott Hughesc7e9b232013-10-16 22:27:54 -070055 kernel_new_action.sa_restorer = bionic_new_action->sa_restorer;
Elliott Hughesc7e9b232013-10-16 22:27:54 -070056 if (!(kernel_new_action.sa_flags & SA_RESTORER)) {
57 kernel_new_action.sa_flags |= SA_RESTORER;
Elliott Hughes36f451a2014-09-10 15:20:40 -070058 kernel_new_action.sa_restorer = &__restore_rt;
Elliott Hughesc7e9b232013-10-16 22:27:54 -070059 }
Chris Dearman46f3db62014-01-30 20:01:19 -080060#endif
Elliott Hughesc7e9b232013-10-16 22:27:54 -070061 }
62
63 __kernel_sigaction kernel_old_action;
64 int result = __rt_sigaction(signal,
Yi Kong32bc0fc2018-08-02 17:31:13 -070065 (bionic_new_action != nullptr) ? &kernel_new_action : nullptr,
66 (bionic_old_action != nullptr) ? &kernel_old_action : nullptr,
Elliott Hughesc7e9b232013-10-16 22:27:54 -070067 sizeof(sigset_t));
68
Yi Kong32bc0fc2018-08-02 17:31:13 -070069 if (bionic_old_action != nullptr) {
Elliott Hughesc7e9b232013-10-16 22:27:54 -070070 bionic_old_action->sa_flags = kernel_old_action.sa_flags;
71 bionic_old_action->sa_handler = kernel_old_action.sa_handler;
72 bionic_old_action->sa_mask = kernel_old_action.sa_mask;
Elliott Hughes1cff9a82014-09-16 15:49:50 -070073#if defined(SA_RESTORER)
Elliott Hughesc7e9b232013-10-16 22:27:54 -070074 bionic_old_action->sa_restorer = kernel_old_action.sa_restorer;
Chris Dearman46f3db62014-01-30 20:01:19 -080075#endif
Elliott Hughesc7e9b232013-10-16 22:27:54 -070076 }
77
78 return result;
Elliott Hughes1cff9a82014-09-16 15:49:50 -070079}
80
Elliott Hughes3e235912018-02-01 14:21:51 -080081__strong_alias(sigaction64, sigaction);
82
Elliott Hughesc7e9b232013-10-16 22:27:54 -070083#else
Elliott Hughes1cff9a82014-09-16 15:49:50 -070084
Elliott Hughes3e235912018-02-01 14:21:51 -080085extern "C" int __rt_sigaction(int, const struct sigaction64*, struct sigaction64*, size_t);
Elliott Hughes1cff9a82014-09-16 15:49:50 -070086
Josh Gao11623dd2020-05-19 14:31:45 -070087// sigaction and sigaction64 get interposed in ART: ensure that we don't end up calling
88// sigchain sigaction -> bionic sigaction -> sigchain sigaction64 -> bionic sigaction64
89// by extracting the implementation of sigaction64 to a static function.
90static int __sigaction64(int signal, const struct sigaction64* bionic_new,
91 struct sigaction64* bionic_old) {
Elliott Hughes20a36612024-01-24 14:25:00 -080092 struct sigaction64 kernel_new = {};
Josh Gao11623dd2020-05-19 14:31:45 -070093 if (bionic_new) {
94 kernel_new = *bionic_new;
Elliott Hughes20a36612024-01-24 14:25:00 -080095#if defined(__arm__)
96 // (See sa_restorer comment in sigaction() above.)
Josh Gao11623dd2020-05-19 14:31:45 -070097 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 Hughes3e235912018-02-01 14:21:51 -0800110int sigaction(int signal, const struct sigaction* bionic_new, struct sigaction* bionic_old) {
Elliott Hughes1cff9a82014-09-16 15:49:50 -0700111 // The 32-bit ABI is broken. struct sigaction includes a too-small sigset_t,
Elliott Hughes3e235912018-02-01 14:21:51 -0800112 // so we have to translate to struct sigaction64 first.
Elliott Hughes20a36612024-01-24 14:25:00 -0800113 struct sigaction64 kernel_new = {};
Elliott Hughes3e235912018-02-01 14:21:51 -0800114 if (bionic_new) {
Elliott Hughes3e235912018-02-01 14:21:51 -0800115 kernel_new.sa_flags = bionic_new->sa_flags;
116 kernel_new.sa_handler = bionic_new->sa_handler;
Goran Jakovljevic87c6aac2018-02-07 11:05:54 +0100117#if defined(SA_RESTORER)
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100118 kernel_new.sa_restorer = bionic_new->sa_restorer;
Goran Jakovljevic87c6aac2018-02-07 11:05:54 +0100119#endif
Josh Gaoba40ff62019-01-22 22:53:49 -0800120 // Don't filter signals here; if the caller asked for everything to be blocked, we should obey.
Elliott Hughes3e235912018-02-01 14:21:51 -0800121 memcpy(&kernel_new.sa_mask, &bionic_new->sa_mask, sizeof(bionic_new->sa_mask));
Elliott Hughes36f451a2014-09-10 15:20:40 -0700122 }
Elliott Hughes3e235912018-02-01 14:21:51 -0800123
124 struct sigaction64 kernel_old;
Josh Gao11623dd2020-05-19 14:31:45 -0700125 int result = __sigaction64(signal, bionic_new ? &kernel_new : nullptr, &kernel_old);
Elliott Hughes3e235912018-02-01 14:21:51 -0800126 if (bionic_old) {
127 *bionic_old = {};
128 bionic_old->sa_flags = kernel_old.sa_flags;
129 bionic_old->sa_handler = kernel_old.sa_handler;
Goran Jakovljevic87c6aac2018-02-07 11:05:54 +0100130#if defined(SA_RESTORER)
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100131 bionic_old->sa_restorer = kernel_old.sa_restorer;
Goran Jakovljevic87c6aac2018-02-07 11:05:54 +0100132#endif
Elliott Hughes3e235912018-02-01 14:21:51 -0800133 memcpy(&bionic_old->sa_mask, &kernel_old.sa_mask, sizeof(bionic_old->sa_mask));
134 }
135 return result;
136}
137
138int sigaction64(int signal, const struct sigaction64* bionic_new, struct sigaction64* bionic_old) {
Josh Gao11623dd2020-05-19 14:31:45 -0700139 return __sigaction64(signal, bionic_new, bionic_old);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700140}
Elliott Hughes1cff9a82014-09-16 15:49:50 -0700141
142#endif