blob: 6468b2d467fb172785b35dc879f02e312500a749 [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>
30
31#if __LP64__
32extern "C" void __rt_sigreturn(void);
33extern "C" int __rt_sigaction(int, const struct __kernel_sigaction*, struct __kernel_sigaction*, size_t);
34#else
35extern "C" int __sigaction(int, const struct sigaction*, struct sigaction*);
36#endif
37
38int sigaction(int signal, const struct sigaction* bionic_new_action, struct sigaction* bionic_old_action) {
39#if __LP64__
40 __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;
45 kernel_new_action.sa_restorer = bionic_new_action->sa_restorer;
46
47 if (!(kernel_new_action.sa_flags & SA_RESTORER)) {
48 kernel_new_action.sa_flags |= SA_RESTORER;
49 kernel_new_action.sa_restorer = &__rt_sigreturn;
50 }
51 }
52
53 __kernel_sigaction kernel_old_action;
54 int result = __rt_sigaction(signal,
55 (bionic_new_action != NULL) ? &kernel_new_action : NULL,
56 (bionic_old_action != NULL) ? &kernel_old_action : NULL,
57 sizeof(sigset_t));
58
59 if (bionic_old_action != NULL) {
60 bionic_old_action->sa_flags = kernel_old_action.sa_flags;
61 bionic_old_action->sa_handler = kernel_old_action.sa_handler;
62 bionic_old_action->sa_mask = kernel_old_action.sa_mask;
63 bionic_old_action->sa_restorer = kernel_old_action.sa_restorer;
64
65 if (bionic_old_action->sa_restorer == &__rt_sigreturn) {
66 bionic_old_action->sa_flags &= ~SA_RESTORER;
67 }
68 }
69
70 return result;
71#else
72 // The 32-bit ABI is broken. struct sigaction includes a too-small sigset_t.
73 // TODO: if we also had correct struct sigaction definitions available, we could copy in and out.
74 return __sigaction(signal, bionic_new_action, bionic_old_action);
75#endif
76}