blob: 53b2a028e46b30f14dd409163b2d6e7e183ff76b [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
Elliott Hughes36f451a2014-09-10 15:20:40 -070032extern "C" void __restore_rt(void);
33extern "C" void __restore(void);
34
Elliott Hughes1cff9a82014-09-16 15:49:50 -070035#if defined(__LP64__)
36
Elliott Hughesc7e9b232013-10-16 22:27:54 -070037extern "C" int __rt_sigaction(int, const struct __kernel_sigaction*, struct __kernel_sigaction*, size_t);
Elliott Hughesc7e9b232013-10-16 22:27:54 -070038
39int sigaction(int signal, const struct sigaction* bionic_new_action, struct sigaction* bionic_old_action) {
Elliott Hughesc7e9b232013-10-16 22:27:54 -070040 __kernel_sigaction kernel_new_action;
41 if (bionic_new_action != NULL) {
42 kernel_new_action.sa_flags = bionic_new_action->sa_flags;
43 kernel_new_action.sa_handler = bionic_new_action->sa_handler;
44 kernel_new_action.sa_mask = bionic_new_action->sa_mask;
Elliott Hughes1cff9a82014-09-16 15:49:50 -070045#if defined(SA_RESTORER)
Elliott Hughesc7e9b232013-10-16 22:27:54 -070046 kernel_new_action.sa_restorer = bionic_new_action->sa_restorer;
Elliott Hughes1cff9a82014-09-16 15:49:50 -070047#if defined(__aarch64__)
48 // arm64 has sa_restorer, but unwinding works best if you just let the
49 // kernel supply the default restorer from [vdso]. gdb doesn't care, but
50 // libgcc needs the nop that the kernel includes before the actual code.
51 // (We could add that ourselves, but why bother?)
52#else
Elliott Hughesc7e9b232013-10-16 22:27:54 -070053 if (!(kernel_new_action.sa_flags & SA_RESTORER)) {
54 kernel_new_action.sa_flags |= SA_RESTORER;
Elliott Hughes36f451a2014-09-10 15:20:40 -070055 kernel_new_action.sa_restorer = &__restore_rt;
Elliott Hughesc7e9b232013-10-16 22:27:54 -070056 }
Chris Dearman46f3db62014-01-30 20:01:19 -080057#endif
Elliott Hughes1cff9a82014-09-16 15:49:50 -070058#endif
Elliott Hughesc7e9b232013-10-16 22:27:54 -070059 }
60
61 __kernel_sigaction kernel_old_action;
62 int result = __rt_sigaction(signal,
63 (bionic_new_action != NULL) ? &kernel_new_action : NULL,
64 (bionic_old_action != NULL) ? &kernel_old_action : NULL,
65 sizeof(sigset_t));
66
67 if (bionic_old_action != NULL) {
68 bionic_old_action->sa_flags = kernel_old_action.sa_flags;
69 bionic_old_action->sa_handler = kernel_old_action.sa_handler;
70 bionic_old_action->sa_mask = kernel_old_action.sa_mask;
Elliott Hughes1cff9a82014-09-16 15:49:50 -070071#if defined(SA_RESTORER)
Elliott Hughesc7e9b232013-10-16 22:27:54 -070072 bionic_old_action->sa_restorer = kernel_old_action.sa_restorer;
Chris Dearman46f3db62014-01-30 20:01:19 -080073#endif
Elliott Hughesc7e9b232013-10-16 22:27:54 -070074 }
75
76 return result;
Elliott Hughes1cff9a82014-09-16 15:49:50 -070077}
78
Elliott Hughes3e235912018-02-01 14:21:51 -080079__strong_alias(sigaction64, sigaction);
80
Elliott Hughesc7e9b232013-10-16 22:27:54 -070081#else
Elliott Hughes1cff9a82014-09-16 15:49:50 -070082
Elliott Hughes3e235912018-02-01 14:21:51 -080083extern "C" int __rt_sigaction(int, const struct sigaction64*, struct sigaction64*, size_t);
Elliott Hughes1cff9a82014-09-16 15:49:50 -070084
Elliott Hughes3e235912018-02-01 14:21:51 -080085int sigaction(int signal, const struct sigaction* bionic_new, struct sigaction* bionic_old) {
Elliott Hughes1cff9a82014-09-16 15:49:50 -070086 // The 32-bit ABI is broken. struct sigaction includes a too-small sigset_t,
Elliott Hughes3e235912018-02-01 14:21:51 -080087 // so we have to translate to struct sigaction64 first.
88 struct sigaction64 kernel_new;
89 if (bionic_new) {
90 kernel_new = {};
91 kernel_new.sa_flags = bionic_new->sa_flags;
92 kernel_new.sa_handler = bionic_new->sa_handler;
Evgeny Eltsin11f60762018-02-05 13:33:35 +010093 kernel_new.sa_restorer = bionic_new->sa_restorer;
Elliott Hughes3e235912018-02-01 14:21:51 -080094 memcpy(&kernel_new.sa_mask, &bionic_new->sa_mask, sizeof(bionic_new->sa_mask));
Elliott Hughes36f451a2014-09-10 15:20:40 -070095 }
Elliott Hughes3e235912018-02-01 14:21:51 -080096
97 struct sigaction64 kernel_old;
98 int result = sigaction64(signal, bionic_new ? &kernel_new : nullptr, &kernel_old);
99 if (bionic_old) {
100 *bionic_old = {};
101 bionic_old->sa_flags = kernel_old.sa_flags;
102 bionic_old->sa_handler = kernel_old.sa_handler;
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100103 bionic_old->sa_restorer = kernel_old.sa_restorer;
Elliott Hughes3e235912018-02-01 14:21:51 -0800104 memcpy(&bionic_old->sa_mask, &kernel_old.sa_mask, sizeof(bionic_old->sa_mask));
105 }
106 return result;
107}
108
109int sigaction64(int signal, const struct sigaction64* bionic_new, struct sigaction64* bionic_old) {
110 struct sigaction64 kernel_new;
111 if (bionic_new) {
112 kernel_new = *bionic_new;
113 if (!(kernel_new.sa_flags & SA_RESTORER)) {
114 kernel_new.sa_flags |= SA_RESTORER;
115 kernel_new.sa_restorer = (kernel_new.sa_flags & SA_SIGINFO) ? &__restore_rt : &__restore;
116 }
117 }
118
119 return __rt_sigaction(signal,
120 bionic_new ? &kernel_new : nullptr,
121 bionic_old,
122 sizeof(kernel_new.sa_mask));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700123}
Elliott Hughes1cff9a82014-09-16 15:49:50 -0700124
125#endif