Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Josh Gao | 61cf3f3 | 2016-03-08 15:27:15 -0800 | [diff] [blame] | 17 | #include <errno.h> |
Elliott Hughes | afe58ad | 2014-09-04 13:54:42 -0700 | [diff] [blame] | 18 | #include <signal.h> |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame^] | 19 | #include <sys/cdefs.h> |
Josh Gao | 61cf3f3 | 2016-03-08 15:27:15 -0800 | [diff] [blame] | 20 | #include <sys/syscall.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 23 | |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 24 | #include <chrono> |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 25 | #include <thread> |
| 26 | |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 27 | #include <android-base/macros.h> |
Evgeny Eltsin | 4d9264c | 2019-12-17 14:17:07 +0100 | [diff] [blame] | 28 | #include <android-base/threads.h> |
| 29 | |
Elliott Hughes | afe58ad | 2014-09-04 13:54:42 -0700 | [diff] [blame] | 30 | #include <gtest/gtest.h> |
Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 31 | |
Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 32 | #include "SignalUtils.h" |
Evgeny Eltsin | 4d9264c | 2019-12-17 14:17:07 +0100 | [diff] [blame] | 33 | #include "utils.h" |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 34 | |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 35 | using namespace std::chrono_literals; |
| 36 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 37 | static int SIGNAL_MIN() { |
Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 38 | return 1; // Signals start at 1 (SIGHUP), not 0. |
| 39 | } |
| 40 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 41 | template <typename SigSetT> |
| 42 | static int SIGNAL_MAX(SigSetT* set) { |
| 43 | return sizeof(*set) * 8; |
Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 46 | template <typename SigSetT> |
| 47 | static void TestSigSet1(int (fn)(SigSetT*)) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 48 | // nullptr sigset_t*/sigset64_t*. |
| 49 | SigSetT* set_ptr = nullptr; |
Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 50 | errno = 0; |
| 51 | ASSERT_EQ(-1, fn(set_ptr)); |
| 52 | ASSERT_EQ(EINVAL, errno); |
| 53 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 54 | // Non-nullptr. |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 55 | SigSetT set = {}; |
Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 56 | errno = 0; |
| 57 | ASSERT_EQ(0, fn(&set)); |
| 58 | ASSERT_EQ(0, errno); |
| 59 | } |
| 60 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 61 | template <typename SigSetT> |
| 62 | static void TestSigSet2(int (fn)(SigSetT*, int)) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 63 | // nullptr sigset_t*/sigset64_t*. |
| 64 | SigSetT* set_ptr = nullptr; |
Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 65 | errno = 0; |
| 66 | ASSERT_EQ(-1, fn(set_ptr, SIGSEGV)); |
| 67 | ASSERT_EQ(EINVAL, errno); |
| 68 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 69 | SigSetT set = {}; |
Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 70 | |
Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 71 | // Bad signal number: too small. |
| 72 | errno = 0; |
| 73 | ASSERT_EQ(-1, fn(&set, 0)); |
| 74 | ASSERT_EQ(EINVAL, errno); |
| 75 | |
| 76 | // Bad signal number: too high. |
| 77 | errno = 0; |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 78 | ASSERT_EQ(-1, fn(&set, SIGNAL_MAX(&set) + 1)); |
Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 79 | ASSERT_EQ(EINVAL, errno); |
| 80 | |
| 81 | // Good signal numbers, low and high ends of range. |
| 82 | errno = 0; |
Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 83 | ASSERT_EQ(0, fn(&set, SIGNAL_MIN())); |
Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 84 | ASSERT_EQ(0, errno); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 85 | ASSERT_EQ(0, fn(&set, SIGNAL_MAX(&set))); |
Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 86 | ASSERT_EQ(0, errno); |
| 87 | } |
| 88 | |
Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 89 | TEST(signal, sigaddset_invalid) { |
| 90 | TestSigSet2(sigaddset); |
| 91 | } |
| 92 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 93 | TEST(signal, sigaddset64_invalid) { |
| 94 | #if defined(__BIONIC__) |
| 95 | TestSigSet2(sigaddset64); |
| 96 | #endif |
| 97 | } |
| 98 | |
Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 99 | TEST(signal, sigdelset_invalid) { |
| 100 | TestSigSet2(sigdelset); |
| 101 | } |
| 102 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 103 | TEST(signal, sigdelset64_invalid) { |
| 104 | #if defined(__BIONIC__) |
| 105 | TestSigSet2(sigdelset64); |
| 106 | #endif |
| 107 | } |
| 108 | |
Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 109 | TEST(signal, sigemptyset_invalid) { |
| 110 | TestSigSet1(sigemptyset); |
| 111 | } |
| 112 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 113 | TEST(signal, sigemptyset64_invalid) { |
| 114 | #if defined(__BIONIC__) |
| 115 | TestSigSet1(sigemptyset64); |
| 116 | #endif |
| 117 | } |
| 118 | |
Elliott Hughes | da73f65 | 2012-11-30 16:40:55 -0800 | [diff] [blame] | 119 | TEST(signal, sigfillset_invalid) { |
| 120 | TestSigSet1(sigfillset); |
| 121 | } |
Chris Dearman | d8a5a6f | 2012-12-07 18:41:10 -0800 | [diff] [blame] | 122 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 123 | TEST(signal, sigfillset64_invalid) { |
| 124 | #if defined(__BIONIC__) |
| 125 | TestSigSet1(sigfillset64); |
| 126 | #endif |
| 127 | } |
| 128 | |
| 129 | TEST(signal, sigismember_invalid) { |
| 130 | TestSigSet2(sigismember); |
| 131 | } |
| 132 | |
| 133 | TEST(signal, sigismember64_invalid) { |
| 134 | #if defined(__BIONIC__) |
| 135 | TestSigSet2(sigismember64); |
| 136 | #endif |
| 137 | } |
| 138 | |
Chris Dearman | d8a5a6f | 2012-12-07 18:41:10 -0800 | [diff] [blame] | 139 | TEST(signal, raise_invalid) { |
| 140 | errno = 0; |
| 141 | ASSERT_EQ(-1, raise(-1)); |
| 142 | ASSERT_EQ(EINVAL, errno); |
| 143 | } |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 144 | |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 145 | static void raise_in_signal_handler_helper(int signal_number) { |
| 146 | ASSERT_EQ(SIGALRM, signal_number); |
| 147 | static int count = 0; |
| 148 | if (++count == 1) { |
| 149 | raise(SIGALRM); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | TEST(signal, raise_in_signal_handler) { |
| 154 | ScopedSignalHandler ssh(SIGALRM, raise_in_signal_handler_helper); |
| 155 | raise(SIGALRM); |
| 156 | } |
| 157 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 158 | static int g_sigsuspend_signal_handler_call_count = 0; |
| 159 | |
Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 160 | TEST(signal, sigsuspend_sigpending) { |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 161 | SignalMaskRestorer smr; |
| 162 | |
Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 163 | // Block SIGALRM. |
| 164 | sigset_t just_SIGALRM; |
| 165 | sigemptyset(&just_SIGALRM); |
| 166 | sigaddset(&just_SIGALRM, SIGALRM); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 167 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr)); |
Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 168 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 169 | ScopedSignalHandler ssh(SIGALRM, [](int) { ++g_sigsuspend_signal_handler_call_count; }); |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 170 | |
Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 171 | // There should be no pending signals. |
| 172 | sigset_t pending; |
| 173 | sigemptyset(&pending); |
| 174 | ASSERT_EQ(0, sigpending(&pending)); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 175 | for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) { |
Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 176 | EXPECT_FALSE(sigismember(&pending, i)) << i; |
| 177 | } |
| 178 | |
Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 179 | // Raise SIGALRM and check our signal handler wasn't called. |
| 180 | raise(SIGALRM); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 181 | ASSERT_EQ(0, g_sigsuspend_signal_handler_call_count); |
Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 182 | |
Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 183 | // We should now have a pending SIGALRM but nothing else. |
| 184 | sigemptyset(&pending); |
| 185 | ASSERT_EQ(0, sigpending(&pending)); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 186 | for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) { |
Elliott Hughes | 40d105c | 2013-10-16 12:53:58 -0700 | [diff] [blame] | 187 | EXPECT_EQ((i == SIGALRM), sigismember(&pending, i)); |
| 188 | } |
| 189 | |
Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 190 | // Use sigsuspend to block everything except SIGALRM... |
| 191 | sigset_t not_SIGALRM; |
| 192 | sigfillset(¬_SIGALRM); |
| 193 | sigdelset(¬_SIGALRM, SIGALRM); |
| 194 | ASSERT_EQ(-1, sigsuspend(¬_SIGALRM)); |
| 195 | ASSERT_EQ(EINTR, errno); |
| 196 | // ...and check that we now receive our pending SIGALRM. |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 197 | ASSERT_EQ(1, g_sigsuspend_signal_handler_call_count); |
| 198 | } |
Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 199 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 200 | static int g_sigsuspend64_signal_handler_call_count = 0; |
| 201 | |
| 202 | TEST(signal, sigsuspend64_sigpending64) { |
| 203 | SignalMaskRestorer smr; |
| 204 | |
| 205 | // Block SIGRTMIN. |
| 206 | sigset64_t just_SIGRTMIN; |
| 207 | sigemptyset64(&just_SIGRTMIN); |
| 208 | sigaddset64(&just_SIGRTMIN, SIGRTMIN); |
| 209 | ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr)); |
| 210 | |
| 211 | ScopedSignalHandler ssh(SIGRTMIN, [](int) { ++g_sigsuspend64_signal_handler_call_count; }); |
| 212 | |
| 213 | // There should be no pending signals. |
| 214 | sigset64_t pending; |
| 215 | sigemptyset64(&pending); |
| 216 | ASSERT_EQ(0, sigpending64(&pending)); |
| 217 | for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) { |
| 218 | EXPECT_FALSE(sigismember64(&pending, i)) << i; |
| 219 | } |
| 220 | |
| 221 | // Raise SIGRTMIN and check our signal handler wasn't called. |
| 222 | raise(SIGRTMIN); |
| 223 | ASSERT_EQ(0, g_sigsuspend64_signal_handler_call_count); |
| 224 | |
| 225 | // We should now have a pending SIGRTMIN but nothing else. |
| 226 | sigemptyset64(&pending); |
| 227 | ASSERT_EQ(0, sigpending64(&pending)); |
| 228 | for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) { |
| 229 | EXPECT_EQ((i == SIGRTMIN), sigismember64(&pending, i)); |
| 230 | } |
| 231 | |
| 232 | // Use sigsuspend64 to block everything except SIGRTMIN... |
| 233 | sigset64_t not_SIGRTMIN; |
| 234 | sigfillset64(¬_SIGRTMIN); |
| 235 | sigdelset64(¬_SIGRTMIN, SIGRTMIN); |
| 236 | ASSERT_EQ(-1, sigsuspend64(¬_SIGRTMIN)); |
| 237 | ASSERT_EQ(EINTR, errno); |
| 238 | // ...and check that we now receive our pending SIGRTMIN. |
| 239 | ASSERT_EQ(1, g_sigsuspend64_signal_handler_call_count); |
Elliott Hughes | 1f5af92 | 2013-10-15 18:01:56 -0700 | [diff] [blame] | 240 | } |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 241 | |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 242 | template <typename SigActionT, typename SigSetT> |
| 243 | static void TestSigAction(int (sigaction_fn)(int, const SigActionT*, SigActionT*), |
| 244 | int (sigaddset_fn)(SigSetT*, int), |
| 245 | int sig) { |
Elliott Hughes | afe58ad | 2014-09-04 13:54:42 -0700 | [diff] [blame] | 246 | // Both bionic and glibc set SA_RESTORER when talking to the kernel on arm, |
| 247 | // arm64, x86, and x86-64. The version of glibc we're using also doesn't |
Elliott Hughes | 6a65ccd | 2020-02-13 09:48:14 -0800 | [diff] [blame] | 248 | // define SA_RESTORER, but luckily it's the same value everywhere. |
Elliott Hughes | aa13e83 | 2014-09-04 15:43:10 -0700 | [diff] [blame] | 249 | static const unsigned sa_restorer = 0x4000000; |
Elliott Hughes | afe58ad | 2014-09-04 13:54:42 -0700 | [diff] [blame] | 250 | |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 251 | // See what's currently set for this signal. |
| 252 | SigActionT original_sa = {}; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 253 | ASSERT_EQ(0, sigaction_fn(sig, nullptr, &original_sa)); |
| 254 | ASSERT_TRUE(original_sa.sa_handler == nullptr); |
| 255 | ASSERT_TRUE(original_sa.sa_sigaction == nullptr); |
Elliott Hughes | aa13e83 | 2014-09-04 15:43:10 -0700 | [diff] [blame] | 256 | ASSERT_EQ(0U, original_sa.sa_flags & ~sa_restorer); |
Goran Jakovljevic | 3796669 | 2018-02-12 09:03:10 +0100 | [diff] [blame] | 257 | #ifdef SA_RESTORER |
Evgeny Eltsin | 11f6076 | 2018-02-05 13:33:35 +0100 | [diff] [blame] | 258 | ASSERT_EQ(bool(original_sa.sa_flags & sa_restorer), bool(original_sa.sa_restorer)); |
Goran Jakovljevic | 3796669 | 2018-02-12 09:03:10 +0100 | [diff] [blame] | 259 | #endif |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 260 | |
| 261 | // Set a traditional sa_handler signal handler. |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 262 | auto no_op_signal_handler = [](int) {}; |
| 263 | SigActionT sa = {}; |
| 264 | sigaddset_fn(&sa.sa_mask, sig); |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 265 | sa.sa_flags = SA_ONSTACK; |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 266 | sa.sa_handler = no_op_signal_handler; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 267 | ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr)); |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 268 | |
| 269 | // Check that we can read it back. |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 270 | sa = {}; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 271 | ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa)); |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 272 | ASSERT_TRUE(sa.sa_handler == no_op_signal_handler); |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 273 | ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler); |
Elliott Hughes | aa13e83 | 2014-09-04 15:43:10 -0700 | [diff] [blame] | 274 | ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK), sa.sa_flags & ~sa_restorer); |
Goran Jakovljevic | 3796669 | 2018-02-12 09:03:10 +0100 | [diff] [blame] | 275 | #ifdef SA_RESTORER |
Evgeny Eltsin | 11f6076 | 2018-02-05 13:33:35 +0100 | [diff] [blame] | 276 | ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer)); |
Goran Jakovljevic | 3796669 | 2018-02-12 09:03:10 +0100 | [diff] [blame] | 277 | #endif |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 278 | |
| 279 | // Set a new-style sa_sigaction signal handler. |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 280 | auto no_op_sigaction = [](int, siginfo_t*, void*) {}; |
| 281 | sa = {}; |
| 282 | sigaddset_fn(&sa.sa_mask, sig); |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 283 | sa.sa_flags = SA_ONSTACK | SA_SIGINFO; |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 284 | sa.sa_sigaction = no_op_sigaction; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 285 | ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr)); |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 286 | |
| 287 | // Check that we can read it back. |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 288 | sa = {}; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 289 | ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa)); |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 290 | ASSERT_TRUE(sa.sa_sigaction == no_op_sigaction); |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 291 | ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler); |
Elliott Hughes | aa13e83 | 2014-09-04 15:43:10 -0700 | [diff] [blame] | 292 | ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK | SA_SIGINFO), sa.sa_flags & ~sa_restorer); |
Goran Jakovljevic | 3796669 | 2018-02-12 09:03:10 +0100 | [diff] [blame] | 293 | #ifdef SA_RESTORER |
Evgeny Eltsin | 11f6076 | 2018-02-05 13:33:35 +0100 | [diff] [blame] | 294 | ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer)); |
Goran Jakovljevic | 3796669 | 2018-02-12 09:03:10 +0100 | [diff] [blame] | 295 | #endif |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 296 | |
| 297 | // Put everything back how it was. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 298 | ASSERT_EQ(0, sigaction_fn(sig, &original_sa, nullptr)); |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | TEST(signal, sigaction) { |
| 302 | TestSigAction(sigaction, sigaddset, SIGALRM); |
| 303 | } |
| 304 | |
| 305 | TEST(signal, sigaction64_SIGRTMIN) { |
| 306 | TestSigAction(sigaction64, sigaddset64, SIGRTMIN); |
Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 307 | } |
Elliott Hughes | aa0ebda | 2014-02-11 19:57:06 -0800 | [diff] [blame] | 308 | |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 309 | static void ClearSignalMask() { |
| 310 | uint64_t sigset = 0; |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 311 | SignalSetAdd(&sigset, __SIGRTMIN); |
| 312 | if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) { |
| 313 | abort(); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | static void FillSignalMask() { |
| 318 | uint64_t sigset = ~0ULL; |
| 319 | for (int signo = __SIGRTMIN + 1; signo < SIGRTMIN; ++signo) { |
| 320 | SignalSetDel(&sigset, signo); |
| 321 | } |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 322 | if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) { |
| 323 | abort(); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | static uint64_t GetSignalMask() { |
| 328 | uint64_t sigset; |
| 329 | if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, nullptr, &sigset, sizeof(sigset)) != 0) { |
| 330 | abort(); |
| 331 | } |
| 332 | return sigset; |
| 333 | } |
| 334 | |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 335 | static void TestSignalMaskFiltered(uint64_t sigset) { |
| 336 | #if defined(__BIONIC__) |
| 337 | for (int signo = __SIGRTMIN; signo < SIGRTMIN; ++signo) { |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 338 | bool signal_blocked = sigset & (1ULL << (signo - 1)); |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 339 | if (signo == __SIGRTMIN) { |
| 340 | // TIMER_SIGNAL must be blocked. |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 341 | EXPECT_EQ(true, signal_blocked) << "signal " << signo; |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 342 | } else { |
| 343 | // The other reserved signals must not be blocked. |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 344 | EXPECT_EQ(false, signal_blocked) << "signal " << signo; |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 345 | } |
| 346 | } |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 347 | #else |
| 348 | UNUSED(sigset); |
| 349 | #endif |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 352 | static void TestSignalMaskFunction(std::function<void()> fn) { |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 353 | ClearSignalMask(); |
| 354 | fn(); |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 355 | TestSignalMaskFiltered(GetSignalMask()); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | TEST(signal, sigaction_filter) { |
| 359 | ClearSignalMask(); |
| 360 | static uint64_t sigset; |
| 361 | struct sigaction sa = {}; |
| 362 | sa.sa_handler = [](int) { sigset = GetSignalMask(); }; |
Josh Gao | ba40ff6 | 2019-01-22 22:53:49 -0800 | [diff] [blame] | 363 | sa.sa_flags = SA_ONSTACK | SA_NODEFER; |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 364 | sigfillset(&sa.sa_mask); |
| 365 | sigaction(SIGUSR1, &sa, nullptr); |
| 366 | raise(SIGUSR1); |
Josh Gao | ba40ff6 | 2019-01-22 22:53:49 -0800 | [diff] [blame] | 367 | |
| 368 | // On LP32, struct sigaction::sa_mask is only 32-bits wide. |
| 369 | unsigned long expected_sigset = ~0UL; |
| 370 | |
| 371 | // SIGKILL and SIGSTOP are always blocked. |
| 372 | expected_sigset &= ~(1UL << (SIGKILL - 1)); |
| 373 | expected_sigset &= ~(1UL << (SIGSTOP - 1)); |
| 374 | |
| 375 | ASSERT_EQ(static_cast<uint64_t>(expected_sigset), sigset); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | TEST(signal, sigaction64_filter) { |
| 379 | ClearSignalMask(); |
| 380 | static uint64_t sigset; |
| 381 | struct sigaction64 sa = {}; |
| 382 | sa.sa_handler = [](int) { sigset = GetSignalMask(); }; |
Josh Gao | ba40ff6 | 2019-01-22 22:53:49 -0800 | [diff] [blame] | 383 | sa.sa_flags = SA_ONSTACK | SA_NODEFER; |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 384 | sigfillset64(&sa.sa_mask); |
| 385 | sigaction64(SIGUSR1, &sa, nullptr); |
| 386 | raise(SIGUSR1); |
Josh Gao | ba40ff6 | 2019-01-22 22:53:49 -0800 | [diff] [blame] | 387 | |
| 388 | uint64_t expected_sigset = ~0ULL; |
| 389 | |
| 390 | // SIGKILL and SIGSTOP are always blocked. |
| 391 | expected_sigset &= ~(1ULL << (SIGKILL - 1)); |
| 392 | expected_sigset &= ~(1ULL << (SIGSTOP - 1)); |
| 393 | |
| 394 | ASSERT_EQ(expected_sigset, sigset); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | TEST(signal, sigprocmask_setmask_filter) { |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 398 | TestSignalMaskFunction([]() { |
| 399 | ClearSignalMask(); |
| 400 | sigset_t sigset_libc; |
| 401 | sigfillset(&sigset_libc); |
| 402 | ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &sigset_libc, nullptr)); |
| 403 | }); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | TEST(signal, sigprocmask64_setmask_filter) { |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 407 | TestSignalMaskFunction([]() { |
| 408 | ClearSignalMask(); |
| 409 | sigset64_t sigset_libc; |
| 410 | sigfillset64(&sigset_libc); |
| 411 | ASSERT_EQ(0, sigprocmask64(SIG_SETMASK, &sigset_libc, nullptr)); |
| 412 | }); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | TEST(signal, pthread_sigmask_setmask_filter) { |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 416 | TestSignalMaskFunction([]() { |
| 417 | ClearSignalMask(); |
| 418 | sigset_t sigset_libc; |
| 419 | sigfillset(&sigset_libc); |
| 420 | ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &sigset_libc, nullptr)); |
| 421 | }); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | TEST(signal, pthread_sigmask64_setmask_filter) { |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 425 | TestSignalMaskFunction([]() { |
| 426 | ClearSignalMask(); |
| 427 | sigset64_t sigset_libc; |
| 428 | sigfillset64(&sigset_libc); |
| 429 | ASSERT_EQ(0, pthread_sigmask64(SIG_SETMASK, &sigset_libc, nullptr)); |
| 430 | }); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | TEST(signal, sigprocmask_block_filter) { |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 434 | TestSignalMaskFunction([]() { |
| 435 | ClearSignalMask(); |
| 436 | sigset_t sigset_libc; |
| 437 | sigfillset(&sigset_libc); |
| 438 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &sigset_libc, nullptr)); |
| 439 | }); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | TEST(signal, sigprocmask64_block_filter) { |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 443 | TestSignalMaskFunction([]() { |
| 444 | ClearSignalMask(); |
| 445 | sigset64_t sigset_libc; |
| 446 | sigfillset64(&sigset_libc); |
| 447 | ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &sigset_libc, nullptr)); |
| 448 | }); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | TEST(signal, pthread_sigmask_block_filter) { |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 452 | TestSignalMaskFunction([]() { |
| 453 | ClearSignalMask(); |
| 454 | sigset_t sigset_libc; |
| 455 | sigfillset(&sigset_libc); |
| 456 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &sigset_libc, nullptr)); |
| 457 | }); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | TEST(signal, pthread_sigmask64_block_filter) { |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 461 | TestSignalMaskFunction([]() { |
| 462 | ClearSignalMask(); |
| 463 | sigset64_t sigset_libc; |
| 464 | sigfillset64(&sigset_libc); |
| 465 | ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, &sigset_libc, nullptr)); |
| 466 | }); |
| 467 | } |
| 468 | |
| 469 | TEST(signal, sigprocmask_unblock_filter) { |
| 470 | TestSignalMaskFunction([]() { |
| 471 | FillSignalMask(); |
| 472 | sigset_t sigset_libc; |
| 473 | sigfillset(&sigset_libc); |
| 474 | ASSERT_EQ(0, sigprocmask(SIG_UNBLOCK, &sigset_libc, nullptr)); |
| 475 | }); |
| 476 | } |
| 477 | |
| 478 | TEST(signal, sigprocmask64_unblock_filter) { |
| 479 | TestSignalMaskFunction([]() { |
| 480 | FillSignalMask(); |
| 481 | sigset64_t sigset_libc; |
| 482 | sigfillset64(&sigset_libc); |
| 483 | ASSERT_EQ(0, sigprocmask64(SIG_UNBLOCK, &sigset_libc, nullptr)); |
| 484 | }); |
| 485 | } |
| 486 | |
| 487 | TEST(signal, pthread_sigmask_unblock_filter) { |
| 488 | TestSignalMaskFunction([]() { |
| 489 | FillSignalMask(); |
| 490 | sigset_t sigset_libc; |
| 491 | sigfillset(&sigset_libc); |
| 492 | ASSERT_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sigset_libc, nullptr)); |
| 493 | }); |
| 494 | } |
| 495 | |
| 496 | TEST(signal, pthread_sigmask64_unblock_filter) { |
| 497 | TestSignalMaskFunction([]() { |
| 498 | FillSignalMask(); |
| 499 | sigset64_t sigset_libc; |
| 500 | sigfillset64(&sigset_libc); |
| 501 | ASSERT_EQ(0, pthread_sigmask64(SIG_UNBLOCK, &sigset_libc, nullptr)); |
| 502 | }); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | // glibc filters out signals via sigfillset, not the actual underlying functions. |
| 506 | TEST(signal, sigset_filter) { |
| 507 | #if defined(__BIONIC__) |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 508 | TestSignalMaskFunction([]() { |
| 509 | for (int i = 1; i <= 64; ++i) { |
| 510 | sigset(i, SIG_HOLD); |
| 511 | } |
| 512 | }); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 513 | #endif |
| 514 | } |
| 515 | |
| 516 | TEST(signal, sighold_filter) { |
| 517 | #if defined(__BIONIC__) |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 518 | TestSignalMaskFunction([]() { |
| 519 | for (int i = 1; i <= 64; ++i) { |
| 520 | sighold(i); |
| 521 | } |
| 522 | }); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 523 | #endif |
| 524 | } |
| 525 | |
| 526 | #if defined(__BIONIC__) |
| 527 | // Not exposed via headers, but the symbols are available if you declare them yourself. |
| 528 | extern "C" int sigblock(int); |
| 529 | extern "C" int sigsetmask(int); |
| 530 | #endif |
| 531 | |
| 532 | TEST(signal, sigblock_filter) { |
| 533 | #if defined(__BIONIC__) |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 534 | TestSignalMaskFunction([]() { |
| 535 | sigblock(~0U); |
| 536 | }); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 537 | #endif |
| 538 | } |
| 539 | |
| 540 | TEST(signal, sigsetmask_filter) { |
| 541 | #if defined(__BIONIC__) |
Josh Gao | baf20fc | 2018-10-08 17:28:07 -0700 | [diff] [blame] | 542 | TestSignalMaskFunction([]() { |
| 543 | sigsetmask(~0U); |
| 544 | }); |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 545 | #endif |
| 546 | } |
| 547 | |
Elliott Hughes | aa0ebda | 2014-02-11 19:57:06 -0800 | [diff] [blame] | 548 | TEST(signal, sys_signame) { |
Elliott Hughes | 671e236 | 2014-02-12 19:04:27 -0800 | [diff] [blame] | 549 | #if defined(__BIONIC__) |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 550 | ASSERT_TRUE(sys_signame[0] == nullptr); |
Elliott Hughes | aa0ebda | 2014-02-11 19:57:06 -0800 | [diff] [blame] | 551 | ASSERT_STREQ("HUP", sys_signame[SIGHUP]); |
| 552 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 553 | GTEST_SKIP() << "glibc doesn't have sys_signame"; |
Elliott Hughes | aa0ebda | 2014-02-11 19:57:06 -0800 | [diff] [blame] | 554 | #endif |
| 555 | } |
| 556 | |
| 557 | TEST(signal, sys_siglist) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame^] | 558 | #if !defined(ANDROID_HOST_MUSL) |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 559 | ASSERT_TRUE(sys_siglist[0] == nullptr); |
Elliott Hughes | aa0ebda | 2014-02-11 19:57:06 -0800 | [diff] [blame] | 560 | ASSERT_STREQ("Hangup", sys_siglist[SIGHUP]); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 561 | #else |
| 562 | GTEST_SKIP() << "musl doesn't have sys_siglist"; |
| 563 | #endif |
Elliott Hughes | aa0ebda | 2014-02-11 19:57:06 -0800 | [diff] [blame] | 564 | } |
Elliott Hughes | 0990d4f | 2014-04-30 09:45:40 -0700 | [diff] [blame] | 565 | |
| 566 | TEST(signal, limits) { |
Elliott Hughes | 6a65ccd | 2020-02-13 09:48:14 -0800 | [diff] [blame] | 567 | // These come from the kernel. |
Elliott Hughes | 0990d4f | 2014-04-30 09:45:40 -0700 | [diff] [blame] | 568 | ASSERT_EQ(32, __SIGRTMIN); |
Elliott Hughes | 6a65ccd | 2020-02-13 09:48:14 -0800 | [diff] [blame] | 569 | ASSERT_EQ(64, __SIGRTMAX); |
Elliott Hughes | 0990d4f | 2014-04-30 09:45:40 -0700 | [diff] [blame] | 570 | |
| 571 | // We reserve a non-zero number at the bottom for ourselves. |
| 572 | ASSERT_GT(SIGRTMIN, __SIGRTMIN); |
| 573 | |
Elliott Hughes | 0990d4f | 2014-04-30 09:45:40 -0700 | [diff] [blame] | 574 | // We don't currently reserve any at the top. |
| 575 | ASSERT_EQ(SIGRTMAX, __SIGRTMAX); |
| 576 | } |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 577 | |
| 578 | static int g_sigqueue_signal_handler_call_count = 0; |
| 579 | |
| 580 | static void SigqueueSignalHandler(int signum, siginfo_t* info, void*) { |
| 581 | ASSERT_EQ(SIGALRM, signum); |
| 582 | ASSERT_EQ(SIGALRM, info->si_signo); |
| 583 | ASSERT_EQ(SI_QUEUE, info->si_code); |
| 584 | ASSERT_EQ(1, info->si_value.sival_int); |
| 585 | ++g_sigqueue_signal_handler_call_count; |
| 586 | } |
| 587 | |
| 588 | TEST(signal, sigqueue) { |
| 589 | ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 590 | sigval sigval = {.sival_int = 1}; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 591 | errno = 0; |
| 592 | ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); |
| 593 | ASSERT_EQ(0, errno); |
| 594 | ASSERT_EQ(1, g_sigqueue_signal_handler_call_count); |
| 595 | } |
| 596 | |
Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 597 | TEST(signal, pthread_sigqueue_self) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame^] | 598 | #if !defined(ANDROID_HOST_MUSL) |
Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 599 | ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 600 | sigval sigval = {.sival_int = 1}; |
Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 601 | errno = 0; |
| 602 | ASSERT_EQ(0, pthread_sigqueue(pthread_self(), SIGALRM, sigval)); |
| 603 | ASSERT_EQ(0, errno); |
| 604 | ASSERT_EQ(1, g_sigqueue_signal_handler_call_count); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 605 | #else |
| 606 | GTEST_SKIP() << "musl doesn't have pthread_sigqueue"; |
| 607 | #endif |
Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | TEST(signal, pthread_sigqueue_other) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame^] | 611 | #if !defined(ANDROID_HOST_MUSL) |
Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 612 | ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 613 | sigval sigval = {.sival_int = 1}; |
Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 614 | |
| 615 | sigset_t mask; |
| 616 | sigfillset(&mask); |
| 617 | pthread_sigmask(SIG_SETMASK, &mask, nullptr); |
| 618 | pthread_t thread; |
| 619 | int rc = pthread_create(&thread, nullptr, |
| 620 | [](void*) -> void* { |
| 621 | sigset_t mask; |
| 622 | sigemptyset(&mask); |
| 623 | sigsuspend(&mask); |
| 624 | return nullptr; |
| 625 | }, |
| 626 | nullptr); |
| 627 | ASSERT_EQ(0, rc); |
| 628 | |
| 629 | errno = 0; |
| 630 | ASSERT_EQ(0, pthread_sigqueue(thread, SIGALRM, sigval)); |
| 631 | ASSERT_EQ(0, errno); |
| 632 | pthread_join(thread, nullptr); |
| 633 | ASSERT_EQ(1, g_sigqueue_signal_handler_call_count); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 634 | #else |
| 635 | GTEST_SKIP() << "musl doesn't have pthread_sigqueue"; |
| 636 | #endif |
Josh Gao | 726b63f | 2018-08-27 16:00:58 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Elliott Hughes | 50fca4d | 2020-03-20 16:25:54 -0700 | [diff] [blame] | 639 | TEST(signal, sigwait_SIGALRM) { |
| 640 | SignalMaskRestorer smr; |
| 641 | |
| 642 | // Block SIGALRM. |
| 643 | sigset_t just_SIGALRM; |
| 644 | sigemptyset(&just_SIGALRM); |
| 645 | sigaddset(&just_SIGALRM, SIGALRM); |
| 646 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr)); |
| 647 | |
| 648 | // Raise SIGALRM. |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 649 | sigval sigval = {.sival_int = 1}; |
Elliott Hughes | 50fca4d | 2020-03-20 16:25:54 -0700 | [diff] [blame] | 650 | ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); |
| 651 | |
| 652 | // Get pending SIGALRM. |
| 653 | int sig; |
| 654 | ASSERT_EQ(0, sigwait(&just_SIGALRM, &sig)); |
| 655 | ASSERT_EQ(SIGALRM, sig); |
| 656 | } |
| 657 | |
| 658 | TEST(signal, sigwait64_SIGRTMIN) { |
| 659 | SignalMaskRestorer smr; |
| 660 | |
| 661 | // Block SIGRTMIN. |
| 662 | sigset64_t just_SIGRTMIN; |
| 663 | sigemptyset64(&just_SIGRTMIN); |
| 664 | sigaddset64(&just_SIGRTMIN, SIGRTMIN); |
| 665 | ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr)); |
| 666 | |
| 667 | // Raise SIGRTMIN. |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 668 | sigval sigval = {.sival_int = 1}; |
Elliott Hughes | 50fca4d | 2020-03-20 16:25:54 -0700 | [diff] [blame] | 669 | ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval)); |
| 670 | |
| 671 | // Get pending SIGRTMIN. |
| 672 | int sig; |
| 673 | ASSERT_EQ(0, sigwait64(&just_SIGRTMIN, &sig)); |
| 674 | ASSERT_EQ(SIGRTMIN, sig); |
| 675 | } |
| 676 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 677 | TEST(signal, sigwaitinfo) { |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 678 | SignalMaskRestorer smr; |
| 679 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 680 | // Block SIGALRM. |
| 681 | sigset_t just_SIGALRM; |
| 682 | sigemptyset(&just_SIGALRM); |
| 683 | sigaddset(&just_SIGALRM, SIGALRM); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 684 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr)); |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 685 | |
| 686 | // Raise SIGALRM. |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 687 | sigval sigval = {.sival_int = 1}; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 688 | ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); |
| 689 | |
| 690 | // Get pending SIGALRM. |
| 691 | siginfo_t info; |
| 692 | errno = 0; |
| 693 | ASSERT_EQ(SIGALRM, sigwaitinfo(&just_SIGALRM, &info)); |
| 694 | ASSERT_EQ(0, errno); |
| 695 | ASSERT_EQ(SIGALRM, info.si_signo); |
| 696 | ASSERT_EQ(1, info.si_value.sival_int); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 697 | } |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 698 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 699 | TEST(signal, sigwaitinfo64_SIGRTMIN) { |
| 700 | SignalMaskRestorer smr; |
| 701 | |
| 702 | // Block SIGRTMIN. |
| 703 | sigset64_t just_SIGRTMIN; |
| 704 | sigemptyset64(&just_SIGRTMIN); |
| 705 | sigaddset64(&just_SIGRTMIN, SIGRTMIN); |
| 706 | ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr)); |
| 707 | |
| 708 | // Raise SIGRTMIN. |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 709 | sigval sigval = {.sival_int = 1}; |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 710 | ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval)); |
| 711 | |
| 712 | // Get pending SIGRTMIN. |
| 713 | siginfo_t info; |
| 714 | errno = 0; |
| 715 | ASSERT_EQ(SIGRTMIN, sigwaitinfo64(&just_SIGRTMIN, &info)); |
| 716 | ASSERT_EQ(0, errno); |
| 717 | ASSERT_EQ(SIGRTMIN, info.si_signo); |
| 718 | ASSERT_EQ(1, info.si_value.sival_int); |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | TEST(signal, sigtimedwait) { |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 722 | SignalMaskRestorer smr; |
| 723 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 724 | // Block SIGALRM. |
| 725 | sigset_t just_SIGALRM; |
| 726 | sigemptyset(&just_SIGALRM); |
| 727 | sigaddset(&just_SIGALRM, SIGALRM); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 728 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr)); |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 729 | |
| 730 | // Raise SIGALRM. |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 731 | sigval sigval = { .sival_int = 1 }; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 732 | ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); |
| 733 | |
| 734 | // Get pending SIGALRM. |
| 735 | siginfo_t info; |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 736 | timespec timeout = { .tv_sec = 2, .tv_nsec = 0 }; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 737 | errno = 0; |
| 738 | ASSERT_EQ(SIGALRM, sigtimedwait(&just_SIGALRM, &info, &timeout)); |
| 739 | ASSERT_EQ(0, errno); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 740 | } |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 741 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 742 | TEST(signal, sigtimedwait64_SIGRTMIN) { |
| 743 | SignalMaskRestorer smr; |
| 744 | |
| 745 | // Block SIGRTMIN. |
| 746 | sigset64_t just_SIGRTMIN; |
| 747 | sigemptyset64(&just_SIGRTMIN); |
| 748 | sigaddset64(&just_SIGRTMIN, SIGRTMIN); |
| 749 | ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr)); |
| 750 | |
| 751 | // Raise SIGALRM. |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 752 | sigval sigval = { .sival_int = 1 }; |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 753 | ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval)); |
| 754 | |
| 755 | // Get pending SIGALRM. |
| 756 | siginfo_t info; |
| 757 | timespec timeout = { .tv_sec = 2, .tv_nsec = 0 }; |
| 758 | errno = 0; |
| 759 | ASSERT_EQ(SIGRTMIN, sigtimedwait64(&just_SIGRTMIN, &info, &timeout)); |
| 760 | ASSERT_EQ(0, errno); |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 763 | TEST(signal, sigtimedwait_timeout) { |
| 764 | // Block SIGALRM. |
| 765 | sigset_t just_SIGALRM; |
| 766 | sigemptyset(&just_SIGALRM); |
| 767 | sigaddset(&just_SIGALRM, SIGALRM); |
| 768 | sigset_t original_set; |
| 769 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set)); |
| 770 | |
| 771 | // Wait timeout. |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 772 | auto t0 = std::chrono::steady_clock::now(); |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 773 | siginfo_t info; |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 774 | timespec timeout = { .tv_sec = 0, .tv_nsec = 1000000 }; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 775 | errno = 0; |
| 776 | ASSERT_EQ(-1, sigtimedwait(&just_SIGALRM, &info, &timeout)); |
| 777 | ASSERT_EQ(EAGAIN, errno); |
Elliott Hughes | ca3f8e4 | 2019-10-28 15:59:38 -0700 | [diff] [blame] | 778 | auto t1 = std::chrono::steady_clock::now(); |
| 779 | ASSERT_GE(t1-t0, 1000000ns); |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 780 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 781 | ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &original_set, nullptr)); |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 782 | } |
Josh Gao | 61cf3f3 | 2016-03-08 15:27:15 -0800 | [diff] [blame] | 783 | |
| 784 | #if defined(__BIONIC__) |
| 785 | TEST(signal, rt_tgsigqueueinfo) { |
| 786 | // Test whether rt_tgsigqueueinfo allows sending arbitrary si_code values to self. |
| 787 | // If this fails, your kernel needs commit 66dd34a to be backported. |
| 788 | static constexpr char error_msg[] = |
| 789 | "\nPlease ensure that the following kernel patch has been applied:\n" |
| 790 | "* https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=66dd34ad31e5963d72a700ec3f2449291d322921\n"; |
| 791 | static siginfo received; |
| 792 | |
Elliott Hughes | 3e23591 | 2018-02-01 14:21:51 -0800 | [diff] [blame] | 793 | struct sigaction handler = {}; |
Josh Gao | 61cf3f3 | 2016-03-08 15:27:15 -0800 | [diff] [blame] | 794 | handler.sa_sigaction = [](int, siginfo_t* siginfo, void*) { received = *siginfo; }; |
| 795 | handler.sa_flags = SA_SIGINFO; |
| 796 | |
| 797 | ASSERT_EQ(0, sigaction(SIGUSR1, &handler, nullptr)); |
| 798 | |
Josh Gao | d787852 | 2016-03-14 18:15:15 -0700 | [diff] [blame] | 799 | siginfo sent; |
| 800 | memset(&sent, 0, sizeof(sent)); |
Josh Gao | 61cf3f3 | 2016-03-08 15:27:15 -0800 | [diff] [blame] | 801 | |
| 802 | sent.si_code = SI_TKILL; |
| 803 | ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent)) |
| 804 | << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg; |
| 805 | ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected " |
| 806 | << sent.si_code << ", received " << received.si_code |
| 807 | << error_msg; |
| 808 | |
| 809 | sent.si_code = SI_USER; |
| 810 | ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent)) |
| 811 | << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg; |
| 812 | ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected " |
| 813 | << sent.si_code << ", received " << received.si_code |
| 814 | << error_msg; |
| 815 | } |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 816 | #endif |
Josh Gao | c244fcb | 2016-03-29 14:34:03 -0700 | [diff] [blame] | 817 | |
Josh Gao | c244fcb | 2016-03-29 14:34:03 -0700 | [diff] [blame] | 818 | TEST(signal, sigset_size) { |
Elliott Hughes | 370e963 | 2021-01-21 14:40:49 -0800 | [diff] [blame] | 819 | // The setjmp implementations assume that sigset_t can fit in a |
| 820 | // long. This is true because ARM and x86 have broken rt signal support, |
| 821 | // and AArch64 and x86_64 both have a SIGRTMAX defined as 64. |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 822 | #if defined(__BIONIC__) |
Josh Gao | c244fcb | 2016-03-29 14:34:03 -0700 | [diff] [blame] | 823 | static_assert(sizeof(sigset_t) <= sizeof(long), "sigset_t doesn't fit in a long"); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 824 | #endif |
| 825 | static_assert(sizeof(sigset64_t)*8 >= 64, "sigset64_t too small for real-time signals"); |
Josh Gao | c244fcb | 2016-03-29 14:34:03 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 828 | TEST(signal, sigignore_EINVAL) { |
| 829 | errno = 0; |
| 830 | ASSERT_EQ(-1, sigignore(99999)); |
| 831 | ASSERT_EQ(EINVAL, errno); |
| 832 | } |
| 833 | |
| 834 | TEST(signal, sigignore) { |
| 835 | errno = 0; |
| 836 | EXPECT_EQ(-1, sigignore(SIGKILL)); |
| 837 | EXPECT_EQ(errno, EINVAL); |
| 838 | |
| 839 | errno = 0; |
| 840 | EXPECT_EQ(-1, sigignore(SIGSTOP)); |
| 841 | EXPECT_EQ(errno, EINVAL); |
| 842 | |
| 843 | ScopedSignalHandler sigalrm{SIGALRM}; |
| 844 | ASSERT_EQ(0, sigignore(SIGALRM)); |
| 845 | |
| 846 | struct sigaction sa; |
| 847 | ASSERT_EQ(0, sigaction(SIGALRM, nullptr, &sa)); |
| 848 | EXPECT_EQ(SIG_IGN, sa.sa_handler); |
| 849 | } |
| 850 | |
| 851 | TEST(signal, sighold_EINVAL) { |
| 852 | errno = 0; |
| 853 | ASSERT_EQ(-1, sighold(99999)); |
| 854 | ASSERT_EQ(EINVAL, errno); |
| 855 | } |
| 856 | |
| 857 | TEST(signal, sigpause_EINVAL) { |
| 858 | errno = 0; |
| 859 | ASSERT_EQ(-1, sigpause(99999)); |
| 860 | ASSERT_EQ(EINVAL, errno); |
| 861 | } |
| 862 | |
| 863 | TEST(signal, sigrelse_EINVAL) { |
| 864 | errno = 0; |
| 865 | ASSERT_EQ(-1, sigpause(99999)); |
| 866 | ASSERT_EQ(EINVAL, errno); |
| 867 | } |
| 868 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 869 | static void TestSigholdSigpauseSigrelse(int sig) { |
| 870 | static int signal_handler_call_count = 0; |
| 871 | ScopedSignalHandler ssh{sig, [](int) { signal_handler_call_count++; }}; |
| 872 | SignalMaskRestorer mask_restorer; |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 873 | sigset_t set; |
| 874 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 875 | // sighold(SIGALRM/SIGRTMIN) should add SIGALRM/SIGRTMIN to the signal mask ... |
| 876 | ASSERT_EQ(0, sighold(sig)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 877 | ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set)); |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 878 | EXPECT_TRUE(sigismember(&set, sig)); |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 879 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 880 | // ... preventing our SIGALRM/SIGRTMIN handler from running ... |
| 881 | raise(sig); |
| 882 | ASSERT_EQ(0, signal_handler_call_count); |
| 883 | // ... until sigpause(SIGALRM/SIGRTMIN) temporarily unblocks it. |
| 884 | ASSERT_EQ(-1, sigpause(sig)); |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 885 | ASSERT_EQ(EINTR, errno); |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 886 | ASSERT_EQ(1, signal_handler_call_count); |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 887 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 888 | if (sig >= SIGRTMIN && sizeof(void*) == 8) { |
| 889 | // But sigpause(SIGALRM/SIGRTMIN) shouldn't permanently unblock SIGALRM/SIGRTMIN. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 890 | ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set)); |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 891 | EXPECT_TRUE(sigismember(&set, sig)); |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 892 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 893 | // Whereas sigrelse(SIGALRM/SIGRTMIN) should. |
| 894 | ASSERT_EQ(0, sigrelse(sig)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 895 | ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set)); |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 896 | EXPECT_FALSE(sigismember(&set, sig)); |
| 897 | } else { |
| 898 | // sigismember won't work for SIGRTMIN on LP32. |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | TEST(signal, sighold_sigpause_sigrelse) { |
| 903 | TestSigholdSigpauseSigrelse(SIGALRM); |
| 904 | } |
| 905 | |
| 906 | TEST(signal, sighold_sigpause_sigrelse_RT) { |
| 907 | TestSigholdSigpauseSigrelse(SIGRTMIN); |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | TEST(signal, sigset_EINVAL) { |
| 911 | errno = 0; |
| 912 | ASSERT_EQ(SIG_ERR, sigset(99999, SIG_DFL)); |
| 913 | ASSERT_EQ(EINVAL, errno); |
| 914 | } |
| 915 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 916 | TEST(signal, sigset_RT) { |
| 917 | static int signal_handler_call_count = 0; |
| 918 | auto signal_handler = [](int) { signal_handler_call_count++; }; |
| 919 | ScopedSignalHandler ssh{SIGRTMIN, signal_handler}; |
| 920 | SignalMaskRestorer mask_restorer; |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 921 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 922 | ASSERT_EQ(signal_handler, sigset(SIGRTMIN, SIG_HOLD)); |
| 923 | #if defined(__LP64__) |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 924 | sigset_t set; |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 925 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); |
| 926 | ASSERT_TRUE(sigismember(&set, SIGRTMIN)); |
| 927 | #endif |
| 928 | |
| 929 | ASSERT_EQ(SIG_HOLD, sigset(SIGRTMIN, signal_handler)); |
| 930 | ASSERT_EQ(signal_handler, sigset(SIGRTMIN, signal_handler)); |
| 931 | ASSERT_EQ(0, signal_handler_call_count); |
| 932 | raise(SIGRTMIN); |
| 933 | ASSERT_EQ(1, signal_handler_call_count); |
| 934 | } |
| 935 | |
| 936 | TEST(signal, sigset) { |
| 937 | static int signal_handler_call_count = 0; |
| 938 | auto signal_handler = [](int) { signal_handler_call_count++; }; |
| 939 | ScopedSignalHandler ssh{SIGALRM, signal_handler}; |
| 940 | SignalMaskRestorer mask_restorer; |
| 941 | |
| 942 | ASSERT_EQ(0, signal_handler_call_count); |
| 943 | raise(SIGALRM); |
| 944 | ASSERT_EQ(1, signal_handler_call_count); |
| 945 | |
| 946 | // Block SIGALRM so the next sigset(SIGARLM) call will return SIG_HOLD. |
| 947 | sigset_t set; |
| 948 | sigemptyset(&set); |
| 949 | sigaddset(&set, SIGALRM); |
| 950 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &set, nullptr)); |
| 951 | |
| 952 | sigemptyset(&set); |
| 953 | ASSERT_EQ(SIG_HOLD, sigset(SIGALRM, signal_handler)); |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 954 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); |
| 955 | EXPECT_FALSE(sigismember(&set, SIGALRM)); |
| 956 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 957 | ASSERT_EQ(signal_handler, sigset(SIGALRM, SIG_IGN)); |
Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 958 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); |
| 959 | EXPECT_FALSE(sigismember(&set, SIGALRM)); |
| 960 | |
| 961 | ASSERT_EQ(SIG_IGN, sigset(SIGALRM, SIG_DFL)); |
| 962 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); |
| 963 | EXPECT_FALSE(sigismember(&set, SIGALRM)); |
| 964 | |
| 965 | ASSERT_EQ(SIG_DFL, sigset(SIGALRM, SIG_HOLD)); |
| 966 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); |
| 967 | EXPECT_TRUE(sigismember(&set, SIGALRM)); |
| 968 | } |
Elliott Hughes | 7532b32 | 2017-07-11 15:00:17 -0700 | [diff] [blame] | 969 | |
| 970 | TEST(signal, killpg_EINVAL) { |
| 971 | // POSIX leaves pgrp <= 1 undefined, but glibc fails with EINVAL for < 0 |
| 972 | // and passes 0 through to kill(2). |
| 973 | errno = 0; |
| 974 | ASSERT_EQ(-1, killpg(-1, SIGKILL)); |
| 975 | ASSERT_EQ(EINVAL, errno); |
| 976 | } |