2009-03-04 04:28:35 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 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.
|
|
|
|
*/
|
|
|
|
|
2018-01-27 02:47:56 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
2013-10-17 07:27:54 +02:00
|
|
|
#include <signal.h>
|
2018-01-27 02:47:56 +01:00
|
|
|
#include <string.h>
|
2018-01-31 00:09:51 +01:00
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <sys/signalfd.h>
|
2018-01-27 02:47:56 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-12-13 22:55:53 +01:00
|
|
|
#include <platform/bionic/reserved_signals.h>
|
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
#include "private/ErrnoRestorer.h"
|
|
|
|
#include "private/SigSetConverter.h"
|
2018-01-27 02:47:56 +01:00
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
extern "C" int __rt_sigpending(const sigset64_t*, size_t);
|
2019-06-19 21:47:53 +02:00
|
|
|
extern "C" int __rt_sigqueueinfo(pid_t, int, siginfo_t*);
|
2018-01-31 00:09:51 +01:00
|
|
|
extern "C" int __rt_sigsuspend(const sigset64_t*, size_t);
|
|
|
|
extern "C" int __rt_sigtimedwait(const sigset64_t*, siginfo_t*, const timespec*, size_t);
|
2018-01-27 02:47:56 +01:00
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
int pthread_sigmask(int how, const sigset_t* new_set, sigset_t* old_set) {
|
|
|
|
ErrnoRestorer errno_restorer;
|
|
|
|
return (sigprocmask(how, new_set, old_set) == -1) ? errno : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_sigmask64(int how, const sigset64_t* new_set, sigset64_t* old_set) {
|
|
|
|
ErrnoRestorer errno_restorer;
|
|
|
|
return (sigprocmask64(how, new_set, old_set) == -1) ? errno : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename SigSetT>
|
|
|
|
int SigAddSet(SigSetT* set, int sig) {
|
|
|
|
int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
|
2018-01-27 02:47:56 +01:00
|
|
|
unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
|
2018-01-31 00:09:51 +01:00
|
|
|
if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
|
2018-01-27 02:47:56 +01:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
int sigaddset(sigset_t* set, int sig) {
|
|
|
|
return SigAddSet(set, sig);
|
|
|
|
}
|
2018-01-27 02:47:56 +01:00
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
int sigaddset64(sigset64_t* set, int sig) {
|
|
|
|
return SigAddSet(set, sig);
|
|
|
|
}
|
2018-01-27 02:47:56 +01:00
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
// This isn't in our header files, but is exposed on all architectures.
|
|
|
|
extern "C" int sigblock(int mask) {
|
|
|
|
SigSetConverter in, out;
|
|
|
|
sigemptyset(&in.sigset);
|
|
|
|
in.bsd = mask;
|
|
|
|
if (sigprocmask(SIG_BLOCK, &in.sigset, &out.sigset) == -1) return -1;
|
|
|
|
return out.bsd;
|
2018-01-27 02:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
template <typename SigSetT>
|
|
|
|
int SigDelSet(SigSetT* set, int sig) {
|
|
|
|
int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
|
2018-01-27 02:47:56 +01:00
|
|
|
unsigned long* local_set = reinterpret_cast<unsigned long*>(set);
|
2018-01-31 00:09:51 +01:00
|
|
|
if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
|
2018-01-27 02:47:56 +01:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
local_set[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
int sigdelset(sigset_t* set, int sig) {
|
|
|
|
return SigDelSet(set, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sigdelset64(sigset64_t* set, int sig) {
|
|
|
|
return SigDelSet(set, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename SigSetT>
|
|
|
|
int SigEmptySet(SigSetT* set) {
|
|
|
|
if (set == nullptr) {
|
2018-01-27 02:47:56 +01:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2018-01-31 00:09:51 +01:00
|
|
|
memset(set, 0, sizeof(*set));
|
2018-01-27 02:47:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
int sigemptyset(sigset_t* set) {
|
|
|
|
return SigEmptySet(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sigemptyset64(sigset64_t* set) {
|
|
|
|
return SigEmptySet(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename SigSetT>
|
|
|
|
int SigFillSet(SigSetT* set) {
|
|
|
|
if (set == nullptr) {
|
2018-01-27 02:47:56 +01:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2018-01-31 00:09:51 +01:00
|
|
|
memset(set, 0xff, sizeof(*set));
|
2018-01-27 02:47:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
int sigfillset(sigset_t* set) {
|
|
|
|
return SigFillSet(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sigfillset64(sigset64_t* set) {
|
|
|
|
return SigFillSet(set);
|
|
|
|
}
|
|
|
|
|
2018-01-27 02:47:56 +01:00
|
|
|
int sighold(int sig) {
|
2018-01-31 00:09:51 +01:00
|
|
|
sigset64_t set = {};
|
|
|
|
if (sigaddset64(&set, sig) == -1) return -1;
|
|
|
|
return sigprocmask64(SIG_BLOCK, &set, nullptr);
|
2018-01-27 02:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int sigignore(int sig) {
|
2018-02-01 23:21:51 +01:00
|
|
|
struct sigaction64 sa = { .sa_handler = SIG_IGN };
|
|
|
|
return sigaction64(sig, &sa, nullptr);
|
2018-01-27 02:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int siginterrupt(int sig, int flag) {
|
2018-02-01 23:21:51 +01:00
|
|
|
struct sigaction64 act;
|
|
|
|
sigaction64(sig, nullptr, &act);
|
2018-01-27 02:47:56 +01:00
|
|
|
if (flag) {
|
|
|
|
act.sa_flags &= ~SA_RESTART;
|
|
|
|
} else {
|
|
|
|
act.sa_flags |= SA_RESTART;
|
|
|
|
}
|
2018-02-01 23:21:51 +01:00
|
|
|
return sigaction64(sig, &act, nullptr);
|
2018-01-27 02:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
template <typename SigSetT>
|
|
|
|
int SigIsMember(const SigSetT* set, int sig) {
|
|
|
|
int bit = sig - 1; // Signal numbers start at 1, but bit positions start at 0.
|
2018-01-27 02:47:56 +01:00
|
|
|
const unsigned long* local_set = reinterpret_cast<const unsigned long*>(set);
|
2018-01-31 00:09:51 +01:00
|
|
|
if (set == nullptr || bit < 0 || bit >= static_cast<int>(8*sizeof(*set))) {
|
2018-01-27 02:47:56 +01:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return static_cast<int>((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
|
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
int sigismember(const sigset_t* set, int sig) {
|
|
|
|
return SigIsMember(set, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sigismember64(const sigset64_t* set, int sig) {
|
|
|
|
return SigIsMember(set, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
__LIBC_HIDDEN__ sighandler_t _signal(int sig, sighandler_t handler, int flags) {
|
2018-02-01 23:21:51 +01:00
|
|
|
struct sigaction64 sa = { .sa_handler = handler, .sa_flags = flags };
|
|
|
|
return (sigaction64(sig, &sa, &sa) == -1) ? SIG_ERR : sa.sa_handler;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
sighandler_t signal(int sig, sighandler_t handler) {
|
|
|
|
return _signal(sig, handler, SA_RESTART);
|
2013-10-17 07:27:54 +02:00
|
|
|
}
|
2018-01-27 02:47:56 +01:00
|
|
|
|
|
|
|
int sigpause(int sig) {
|
2018-01-31 00:09:51 +01:00
|
|
|
sigset64_t set = {};
|
|
|
|
if (sigprocmask64(SIG_SETMASK, nullptr, &set) == -1 || sigdelset64(&set, sig) == -1) return -1;
|
|
|
|
return sigsuspend64(&set);
|
2018-01-27 02:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int sigpending(sigset_t* bionic_set) {
|
2018-01-31 00:09:51 +01:00
|
|
|
SigSetConverter set = {};
|
|
|
|
set.sigset = *bionic_set;
|
|
|
|
if (__rt_sigpending(&set.sigset64, sizeof(set.sigset64)) == -1) return -1;
|
|
|
|
*bionic_set = set.sigset;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sigpending64(sigset64_t* set) {
|
|
|
|
return __rt_sigpending(set, sizeof(*set));
|
2018-01-27 02:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
int sigqueue(pid_t pid, int sig, const sigval value) {
|
2018-01-27 02:47:56 +01:00
|
|
|
siginfo_t info;
|
|
|
|
memset(&info, 0, sizeof(siginfo_t));
|
2018-01-31 00:09:51 +01:00
|
|
|
info.si_signo = sig;
|
2018-01-27 02:47:56 +01:00
|
|
|
info.si_code = SI_QUEUE;
|
|
|
|
info.si_pid = getpid();
|
|
|
|
info.si_uid = getuid();
|
|
|
|
info.si_value = value;
|
2019-06-19 21:47:53 +02:00
|
|
|
return __rt_sigqueueinfo(pid, sig, &info);
|
2018-01-27 02:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int sigrelse(int sig) {
|
2018-01-31 00:09:51 +01:00
|
|
|
sigset64_t set = {};
|
|
|
|
if (sigaddset64(&set, sig) == -1) return -1;
|
|
|
|
return sigprocmask64(SIG_UNBLOCK, &set, nullptr);
|
2018-01-27 02:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sighandler_t sigset(int sig, sighandler_t disp) {
|
2018-02-01 23:21:51 +01:00
|
|
|
struct sigaction64 new_sa;
|
|
|
|
if (disp != SIG_HOLD) new_sa = { .sa_handler = disp };
|
2018-01-27 02:47:56 +01:00
|
|
|
|
2018-02-01 23:21:51 +01:00
|
|
|
struct sigaction64 old_sa;
|
|
|
|
if (sigaction64(sig, (disp == SIG_HOLD) ? nullptr : &new_sa, &old_sa) == -1) {
|
2018-01-27 02:47:56 +01:00
|
|
|
return SIG_ERR;
|
|
|
|
}
|
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
sigset64_t new_mask = {};
|
|
|
|
sigaddset64(&new_mask, sig);
|
|
|
|
sigset64_t old_mask;
|
|
|
|
if (sigprocmask64(disp == SIG_HOLD ? SIG_BLOCK : SIG_UNBLOCK, &new_mask, &old_mask) == -1) {
|
2018-01-27 02:47:56 +01:00
|
|
|
return SIG_ERR;
|
|
|
|
}
|
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
return sigismember64(&old_mask, sig) ? SIG_HOLD : old_sa.sa_handler;
|
2018-01-27 02:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This isn't in our header files, but is exposed on all architectures.
|
|
|
|
extern "C" int sigsetmask(int mask) {
|
2018-01-31 00:09:51 +01:00
|
|
|
SigSetConverter in, out;
|
|
|
|
sigemptyset(&in.sigset);
|
|
|
|
in.bsd = mask;
|
|
|
|
if (sigprocmask(SIG_SETMASK, &in.sigset, &out.sigset) == -1) return -1;
|
|
|
|
return out.bsd;
|
|
|
|
}
|
2018-01-27 02:47:56 +01:00
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
int sigsuspend(const sigset_t* bionic_set) {
|
|
|
|
SigSetConverter set = {};
|
|
|
|
set.sigset = *bionic_set;
|
2018-02-09 22:38:32 +01:00
|
|
|
return sigsuspend64(&set.sigset64);
|
2018-01-31 00:09:51 +01:00
|
|
|
}
|
2018-01-27 02:47:56 +01:00
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
int sigsuspend64(const sigset64_t* set) {
|
2018-02-09 22:38:32 +01:00
|
|
|
sigset64_t mutable_set;
|
|
|
|
sigset64_t* mutable_set_ptr = nullptr;
|
|
|
|
if (set) {
|
2018-10-09 02:28:07 +02:00
|
|
|
mutable_set = filter_reserved_signals(*set, SIG_SETMASK);
|
2018-02-09 22:38:32 +01:00
|
|
|
mutable_set_ptr = &mutable_set;
|
|
|
|
}
|
|
|
|
return __rt_sigsuspend(mutable_set_ptr, sizeof(*set));
|
2018-01-27 02:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
int sigtimedwait(const sigset_t* bionic_set, siginfo_t* info, const timespec* timeout) {
|
|
|
|
SigSetConverter set = {};
|
|
|
|
set.sigset = *bionic_set;
|
2018-02-09 22:38:32 +01:00
|
|
|
return sigtimedwait64(&set.sigset64, info, timeout);
|
2018-01-27 02:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
int sigtimedwait64(const sigset64_t* set, siginfo_t* info, const timespec* timeout) {
|
2018-02-09 22:38:32 +01:00
|
|
|
sigset64_t mutable_set;
|
|
|
|
sigset64_t* mutable_set_ptr = nullptr;
|
|
|
|
if (set) {
|
2018-10-09 02:28:07 +02:00
|
|
|
mutable_set = filter_reserved_signals(*set, SIG_SETMASK);
|
2018-02-09 22:38:32 +01:00
|
|
|
mutable_set_ptr = &mutable_set;
|
|
|
|
}
|
|
|
|
return __rt_sigtimedwait(mutable_set_ptr, info, timeout, sizeof(*set));
|
2018-01-27 02:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 00:09:51 +01:00
|
|
|
int sigwait(const sigset_t* bionic_set, int* sig) {
|
|
|
|
SigSetConverter set = {};
|
|
|
|
set.sigset = *bionic_set;
|
|
|
|
return sigwait64(&set.sigset64, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sigwait64(const sigset64_t* set, int* sig) {
|
2018-01-27 02:47:56 +01:00
|
|
|
while (true) {
|
|
|
|
// __rt_sigtimedwait can return EAGAIN or EINTR, we need to loop
|
|
|
|
// around them since sigwait is only allowed to return EINVAL.
|
2018-01-31 00:09:51 +01:00
|
|
|
int result = sigtimedwait64(set, nullptr, nullptr);
|
2018-01-27 02:47:56 +01:00
|
|
|
if (result >= 0) {
|
|
|
|
*sig = result;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-01-31 00:09:51 +01:00
|
|
|
if (errno != EAGAIN && errno != EINTR) return errno;
|
2018-01-27 02:47:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int sigwaitinfo(const sigset_t* set, siginfo_t* info) {
|
2018-01-31 00:09:51 +01:00
|
|
|
return sigtimedwait(set, info, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sigwaitinfo64(const sigset64_t* set, siginfo_t* info) {
|
|
|
|
return sigtimedwait64(set, info, nullptr);
|
2018-01-27 02:47:56 +01:00
|
|
|
}
|