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