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