1c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes/* 2c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * Copyright (C) 2013 The Android Open Source Project 3c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * All rights reserved. 4c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * 5c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * Redistribution and use in source and binary forms, with or without 6c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * modification, are permitted provided that the following conditions 7c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * are met: 8c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * * Redistributions of source code must retain the above copyright 9c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * notice, this list of conditions and the following disclaimer. 10c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * * Redistributions in binary form must reproduce the above copyright 11c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * notice, this list of conditions and the following disclaimer in 12c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * the documentation and/or other materials provided with the 13c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * distribution. 14c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * 15c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes * SUCH DAMAGE. 27c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes */ 28c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes 29c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes#include <signal.h> 303e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes#include <string.h> 31c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes 326fcba93b17d52c22bf00211b8bf1524ef61bbcabJosh Gao#include "private/sigrtmin.h" 336fcba93b17d52c22bf00211b8bf1524ef61bbcabJosh Gao 3436f451a6d93b6807944d99fa23396e039c47e845Elliott Hughesextern "C" void __restore_rt(void); 3536f451a6d93b6807944d99fa23396e039c47e845Elliott Hughesextern "C" void __restore(void); 3636f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes 371cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#if defined(__LP64__) 381cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes 39c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughesextern "C" int __rt_sigaction(int, const struct __kernel_sigaction*, struct __kernel_sigaction*, size_t); 40c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes 41c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughesint sigaction(int signal, const struct sigaction* bionic_new_action, struct sigaction* bionic_old_action) { 42c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes __kernel_sigaction kernel_new_action; 43c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes if (bionic_new_action != NULL) { 44c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes kernel_new_action.sa_flags = bionic_new_action->sa_flags; 45c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes kernel_new_action.sa_handler = bionic_new_action->sa_handler; 466fcba93b17d52c22bf00211b8bf1524ef61bbcabJosh Gao kernel_new_action.sa_mask = filter_reserved_signals(bionic_new_action->sa_mask); 471cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#if defined(SA_RESTORER) 48c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes kernel_new_action.sa_restorer = bionic_new_action->sa_restorer; 491cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#if defined(__aarch64__) 501cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes // arm64 has sa_restorer, but unwinding works best if you just let the 511cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes // kernel supply the default restorer from [vdso]. gdb doesn't care, but 521cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes // libgcc needs the nop that the kernel includes before the actual code. 531cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes // (We could add that ourselves, but why bother?) 541cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#else 55c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes if (!(kernel_new_action.sa_flags & SA_RESTORER)) { 56c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes kernel_new_action.sa_flags |= SA_RESTORER; 5736f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes kernel_new_action.sa_restorer = &__restore_rt; 58c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes } 5946f3db6bb81b3368783ea3748df46c2d7d2f7b2fChris Dearman#endif 601cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#endif 61c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes } 62c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes 63c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes __kernel_sigaction kernel_old_action; 64c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes int result = __rt_sigaction(signal, 65c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes (bionic_new_action != NULL) ? &kernel_new_action : NULL, 66c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes (bionic_old_action != NULL) ? &kernel_old_action : NULL, 67c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes sizeof(sigset_t)); 68c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes 69c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes if (bionic_old_action != NULL) { 70c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes bionic_old_action->sa_flags = kernel_old_action.sa_flags; 71c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes bionic_old_action->sa_handler = kernel_old_action.sa_handler; 72c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes bionic_old_action->sa_mask = kernel_old_action.sa_mask; 731cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#if defined(SA_RESTORER) 74c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes bionic_old_action->sa_restorer = kernel_old_action.sa_restorer; 7546f3db6bb81b3368783ea3748df46c2d7d2f7b2fChris Dearman#endif 76c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes } 77c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes 78c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes return result; 791cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes} 801cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes 813e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes__strong_alias(sigaction64, sigaction); 823e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes 83c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes#else 841cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes 853e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughesextern "C" int __rt_sigaction(int, const struct sigaction64*, struct sigaction64*, size_t); 861cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes 873e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughesint sigaction(int signal, const struct sigaction* bionic_new, struct sigaction* bionic_old) { 881cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes // The 32-bit ABI is broken. struct sigaction includes a too-small sigset_t, 893e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes // so we have to translate to struct sigaction64 first. 903e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes struct sigaction64 kernel_new; 913e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes if (bionic_new) { 923e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes kernel_new = {}; 933e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes kernel_new.sa_flags = bionic_new->sa_flags; 943e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes kernel_new.sa_handler = bionic_new->sa_handler; 9587c6aac6d2d63ad46778dc38727f229bd4f45898Goran Jakovljevic#if defined(SA_RESTORER) 9611f607641000993a90ef774bfab18347052094d6Evgeny Eltsin kernel_new.sa_restorer = bionic_new->sa_restorer; 9787c6aac6d2d63ad46778dc38727f229bd4f45898Goran Jakovljevic#endif 983e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes memcpy(&kernel_new.sa_mask, &bionic_new->sa_mask, sizeof(bionic_new->sa_mask)); 993e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes } 10036f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes 1013e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes struct sigaction64 kernel_old; 1023e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes int result = sigaction64(signal, bionic_new ? &kernel_new : nullptr, &kernel_old); 1033e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes if (bionic_old) { 1043e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes *bionic_old = {}; 1053e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes bionic_old->sa_flags = kernel_old.sa_flags; 1063e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes bionic_old->sa_handler = kernel_old.sa_handler; 10787c6aac6d2d63ad46778dc38727f229bd4f45898Goran Jakovljevic#if defined(SA_RESTORER) 10811f607641000993a90ef774bfab18347052094d6Evgeny Eltsin bionic_old->sa_restorer = kernel_old.sa_restorer; 10987c6aac6d2d63ad46778dc38727f229bd4f45898Goran Jakovljevic#endif 1103e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes memcpy(&bionic_old->sa_mask, &kernel_old.sa_mask, sizeof(bionic_old->sa_mask)); 1113e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes } 1123e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes return result; 1133e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes} 1143e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes 1153e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughesint sigaction64(int signal, const struct sigaction64* bionic_new, struct sigaction64* bionic_old) { 1163e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes struct sigaction64 kernel_new; 1173e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes if (bionic_new) { 1183e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes kernel_new = *bionic_new; 11987c6aac6d2d63ad46778dc38727f229bd4f45898Goran Jakovljevic#if defined(SA_RESTORER) 1203e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes if (!(kernel_new.sa_flags & SA_RESTORER)) { 1213e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes kernel_new.sa_flags |= SA_RESTORER; 1223e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes kernel_new.sa_restorer = (kernel_new.sa_flags & SA_SIGINFO) ? &__restore_rt : &__restore; 12336f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes } 12487c6aac6d2d63ad46778dc38727f229bd4f45898Goran Jakovljevic#endif 1256fcba93b17d52c22bf00211b8bf1524ef61bbcabJosh Gao kernel_new.sa_mask = filter_reserved_signals(kernel_new.sa_mask); 12636f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes } 1273e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes 1283e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes return __rt_sigaction(signal, 1293e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes bionic_new ? &kernel_new : nullptr, 1303e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes bionic_old, 1313e235911c9cf5062adbb73efb53fe5ed712d7c53Elliott Hughes sizeof(kernel_new.sa_mask)); 132c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes} 1331cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes 1341cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#endif 135