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