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>
30c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes
3136f451a6d93b6807944d99fa23396e039c47e845Elliott Hughesextern "C" void __restore_rt(void);
3236f451a6d93b6807944d99fa23396e039c47e845Elliott Hughesextern "C" void __restore(void);
3336f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes
341cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#if defined(__LP64__)
351cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes
36c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughesextern "C" int __rt_sigaction(int, const struct __kernel_sigaction*, struct __kernel_sigaction*, size_t);
37c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes
38c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughesint sigaction(int signal, const struct sigaction* bionic_new_action, struct sigaction* bionic_old_action) {
39c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes  __kernel_sigaction kernel_new_action;
40c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes  if (bionic_new_action != NULL) {
41c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes    kernel_new_action.sa_flags = bionic_new_action->sa_flags;
42c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes    kernel_new_action.sa_handler = bionic_new_action->sa_handler;
43c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes    kernel_new_action.sa_mask = bionic_new_action->sa_mask;
441cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#if defined(SA_RESTORER)
45c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes    kernel_new_action.sa_restorer = bionic_new_action->sa_restorer;
461cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#if defined(__aarch64__)
471cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes    // arm64 has sa_restorer, but unwinding works best if you just let the
481cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes    // kernel supply the default restorer from [vdso]. gdb doesn't care, but
491cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes    // libgcc needs the nop that the kernel includes before the actual code.
501cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes    // (We could add that ourselves, but why bother?)
511cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#else
52c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes    if (!(kernel_new_action.sa_flags & SA_RESTORER)) {
53c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes      kernel_new_action.sa_flags |= SA_RESTORER;
5436f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes      kernel_new_action.sa_restorer = &__restore_rt;
55c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes    }
5646f3db6bb81b3368783ea3748df46c2d7d2f7b2fChris Dearman#endif
571cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#endif
58c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes  }
59c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes
60c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes  __kernel_sigaction kernel_old_action;
61c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes  int result = __rt_sigaction(signal,
62c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes                              (bionic_new_action != NULL) ? &kernel_new_action : NULL,
63c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes                              (bionic_old_action != NULL) ? &kernel_old_action : NULL,
64c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes                              sizeof(sigset_t));
65c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes
66c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes  if (bionic_old_action != NULL) {
67c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes    bionic_old_action->sa_flags = kernel_old_action.sa_flags;
68c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes    bionic_old_action->sa_handler = kernel_old_action.sa_handler;
69c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes    bionic_old_action->sa_mask = kernel_old_action.sa_mask;
701cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#if defined(SA_RESTORER)
71c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes    bionic_old_action->sa_restorer = kernel_old_action.sa_restorer;
7246f3db6bb81b3368783ea3748df46c2d7d2f7b2fChris Dearman#endif
73c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes  }
74c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes
75c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes  return result;
761cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes}
771cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes
78c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes#else
791cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes
801cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughesextern "C" int __sigaction(int, const struct sigaction*, struct sigaction*);
811cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes
821cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughesint sigaction(int signal, const struct sigaction* bionic_new_action, struct sigaction* bionic_old_action) {
831cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes  // The 32-bit ABI is broken. struct sigaction includes a too-small sigset_t,
841cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes  // so we have to use sigaction(2) rather than rt_sigaction(2).
8536f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes  struct sigaction kernel_new_action;
8636f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes  if (bionic_new_action != NULL) {
8736f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes    kernel_new_action.sa_flags = bionic_new_action->sa_flags;
8836f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes    kernel_new_action.sa_handler = bionic_new_action->sa_handler;
8936f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes    kernel_new_action.sa_mask = bionic_new_action->sa_mask;
901cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#if defined(SA_RESTORER)
9136f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes    kernel_new_action.sa_restorer = bionic_new_action->sa_restorer;
9236f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes
9336f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes    if (!(kernel_new_action.sa_flags & SA_RESTORER)) {
9436f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes      kernel_new_action.sa_flags |= SA_RESTORER;
9536f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes      kernel_new_action.sa_restorer = (kernel_new_action.sa_flags & SA_SIGINFO) ? &__restore_rt : &__restore;
9636f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes    }
9736f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes#endif
9836f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes  }
9936f451a6d93b6807944d99fa23396e039c47e845Elliott Hughes  return __sigaction(signal, (bionic_new_action != NULL) ? &kernel_new_action : NULL, bionic_old_action);
100c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes}
1011cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes
1021cff9a89645a8f362a9ce19c7f9544e98c1fd9e7Elliott Hughes#endif
103