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