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