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