2013-10-17 07:27:54 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <signal.h>
|
2018-02-01 23:21:51 +01:00
|
|
|
#include <string.h>
|
2013-10-17 07:27:54 +02:00
|
|
|
|
2018-02-09 22:38:32 +01:00
|
|
|
#include "private/sigrtmin.h"
|
|
|
|
|
2014-09-11 00:20:40 +02:00
|
|
|
extern "C" void __restore_rt(void);
|
|
|
|
extern "C" void __restore(void);
|
|
|
|
|
2014-09-17 00:49:50 +02:00
|
|
|
#if defined(__LP64__)
|
|
|
|
|
2013-10-17 07:27:54 +02:00
|
|
|
extern "C" int __rt_sigaction(int, const struct __kernel_sigaction*, struct __kernel_sigaction*, size_t);
|
|
|
|
|
|
|
|
int sigaction(int signal, const struct sigaction* bionic_new_action, struct sigaction* bionic_old_action) {
|
|
|
|
__kernel_sigaction kernel_new_action;
|
2018-08-03 02:31:13 +02:00
|
|
|
if (bionic_new_action != nullptr) {
|
2013-10-17 07:27:54 +02:00
|
|
|
kernel_new_action.sa_flags = bionic_new_action->sa_flags;
|
|
|
|
kernel_new_action.sa_handler = bionic_new_action->sa_handler;
|
2018-02-09 22:38:32 +01:00
|
|
|
kernel_new_action.sa_mask = filter_reserved_signals(bionic_new_action->sa_mask);
|
2014-09-17 00:49:50 +02:00
|
|
|
#if defined(SA_RESTORER)
|
2013-10-17 07:27:54 +02:00
|
|
|
kernel_new_action.sa_restorer = bionic_new_action->sa_restorer;
|
2014-09-17 00:49:50 +02:00
|
|
|
#if defined(__aarch64__)
|
|
|
|
// arm64 has sa_restorer, but unwinding works best if you just let the
|
|
|
|
// kernel supply the default restorer from [vdso]. gdb doesn't care, but
|
|
|
|
// libgcc needs the nop that the kernel includes before the actual code.
|
|
|
|
// (We could add that ourselves, but why bother?)
|
|
|
|
#else
|
2013-10-17 07:27:54 +02:00
|
|
|
if (!(kernel_new_action.sa_flags & SA_RESTORER)) {
|
|
|
|
kernel_new_action.sa_flags |= SA_RESTORER;
|
2014-09-11 00:20:40 +02:00
|
|
|
kernel_new_action.sa_restorer = &__restore_rt;
|
2013-10-17 07:27:54 +02:00
|
|
|
}
|
2014-09-17 00:49:50 +02:00
|
|
|
#endif
|
2014-01-31 05:01:19 +01:00
|
|
|
#endif
|
2013-10-17 07:27:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
__kernel_sigaction kernel_old_action;
|
|
|
|
int result = __rt_sigaction(signal,
|
2018-08-03 02:31:13 +02:00
|
|
|
(bionic_new_action != nullptr) ? &kernel_new_action : nullptr,
|
|
|
|
(bionic_old_action != nullptr) ? &kernel_old_action : nullptr,
|
2013-10-17 07:27:54 +02:00
|
|
|
sizeof(sigset_t));
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
if (bionic_old_action != nullptr) {
|
2013-10-17 07:27:54 +02:00
|
|
|
bionic_old_action->sa_flags = kernel_old_action.sa_flags;
|
|
|
|
bionic_old_action->sa_handler = kernel_old_action.sa_handler;
|
|
|
|
bionic_old_action->sa_mask = kernel_old_action.sa_mask;
|
2014-09-17 00:49:50 +02:00
|
|
|
#if defined(SA_RESTORER)
|
2013-10-17 07:27:54 +02:00
|
|
|
bionic_old_action->sa_restorer = kernel_old_action.sa_restorer;
|
2014-01-31 05:01:19 +01:00
|
|
|
#endif
|
2013-10-17 07:27:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2014-09-17 00:49:50 +02:00
|
|
|
}
|
|
|
|
|
2018-02-01 23:21:51 +01:00
|
|
|
__strong_alias(sigaction64, sigaction);
|
|
|
|
|
2013-10-17 07:27:54 +02:00
|
|
|
#else
|
2014-09-17 00:49:50 +02:00
|
|
|
|
2018-02-01 23:21:51 +01:00
|
|
|
extern "C" int __rt_sigaction(int, const struct sigaction64*, struct sigaction64*, size_t);
|
2014-09-17 00:49:50 +02:00
|
|
|
|
2018-02-01 23:21:51 +01:00
|
|
|
int sigaction(int signal, const struct sigaction* bionic_new, struct sigaction* bionic_old) {
|
2014-09-17 00:49:50 +02:00
|
|
|
// The 32-bit ABI is broken. struct sigaction includes a too-small sigset_t,
|
2018-02-01 23:21:51 +01:00
|
|
|
// so we have to translate to struct sigaction64 first.
|
|
|
|
struct sigaction64 kernel_new;
|
|
|
|
if (bionic_new) {
|
|
|
|
kernel_new = {};
|
|
|
|
kernel_new.sa_flags = bionic_new->sa_flags;
|
|
|
|
kernel_new.sa_handler = bionic_new->sa_handler;
|
2018-02-07 11:05:54 +01:00
|
|
|
#if defined(SA_RESTORER)
|
2018-02-05 13:33:35 +01:00
|
|
|
kernel_new.sa_restorer = bionic_new->sa_restorer;
|
2018-02-07 11:05:54 +01:00
|
|
|
#endif
|
2018-02-01 23:21:51 +01:00
|
|
|
memcpy(&kernel_new.sa_mask, &bionic_new->sa_mask, sizeof(bionic_new->sa_mask));
|
|
|
|
}
|
2014-09-11 00:20:40 +02:00
|
|
|
|
2018-02-01 23:21:51 +01:00
|
|
|
struct sigaction64 kernel_old;
|
|
|
|
int result = sigaction64(signal, bionic_new ? &kernel_new : nullptr, &kernel_old);
|
|
|
|
if (bionic_old) {
|
|
|
|
*bionic_old = {};
|
|
|
|
bionic_old->sa_flags = kernel_old.sa_flags;
|
|
|
|
bionic_old->sa_handler = kernel_old.sa_handler;
|
2018-02-07 11:05:54 +01:00
|
|
|
#if defined(SA_RESTORER)
|
2018-02-05 13:33:35 +01:00
|
|
|
bionic_old->sa_restorer = kernel_old.sa_restorer;
|
2018-02-07 11:05:54 +01:00
|
|
|
#endif
|
2018-02-01 23:21:51 +01:00
|
|
|
memcpy(&bionic_old->sa_mask, &kernel_old.sa_mask, sizeof(bionic_old->sa_mask));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sigaction64(int signal, const struct sigaction64* bionic_new, struct sigaction64* bionic_old) {
|
|
|
|
struct sigaction64 kernel_new;
|
|
|
|
if (bionic_new) {
|
|
|
|
kernel_new = *bionic_new;
|
2018-02-07 11:05:54 +01:00
|
|
|
#if defined(SA_RESTORER)
|
2018-02-01 23:21:51 +01:00
|
|
|
if (!(kernel_new.sa_flags & SA_RESTORER)) {
|
|
|
|
kernel_new.sa_flags |= SA_RESTORER;
|
|
|
|
kernel_new.sa_restorer = (kernel_new.sa_flags & SA_SIGINFO) ? &__restore_rt : &__restore;
|
2014-09-11 00:20:40 +02:00
|
|
|
}
|
2018-02-07 11:05:54 +01:00
|
|
|
#endif
|
2018-02-09 22:38:32 +01:00
|
|
|
kernel_new.sa_mask = filter_reserved_signals(kernel_new.sa_mask);
|
2014-09-11 00:20:40 +02:00
|
|
|
}
|
2018-02-01 23:21:51 +01:00
|
|
|
|
|
|
|
return __rt_sigaction(signal,
|
|
|
|
bionic_new ? &kernel_new : nullptr,
|
|
|
|
bionic_old,
|
|
|
|
sizeof(kernel_new.sa_mask));
|
2013-10-17 07:27:54 +02:00
|
|
|
}
|
2014-09-17 00:49:50 +02:00
|
|
|
|
|
|
|
#endif
|