| 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 |  | 
| Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 27 | #include "ScopedSignalHandler.h" | 
|  | 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*)) { | 
|  | 40 | // NULL sigset_t*/sigset64_t*. | 
|  | 41 | SigSetT* set_ptr = NULL; | 
| 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 |  | 
|  | 46 | // Non-NULL. | 
| 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)) { | 
|  | 55 | // NULL sigset_t*/sigset64_t*. | 
|  | 56 | SigSetT* set_ptr = NULL; | 
| 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 |  | 
|  | 272 | static void EmptySignalHandler(int) {} | 
|  | 273 | static void EmptySignalAction(int, siginfo_t*, void*) {} | 
|  | 274 |  | 
|  | 275 | TEST(signal, sigaction) { | 
| 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 | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 282 | // See what's currently set for SIGALRM. | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 283 | struct sigaction original_sa; | 
|  | 284 | memset(&original_sa, 0, sizeof(original_sa)); | 
|  | 285 | ASSERT_EQ(0, sigaction(SIGALRM, NULL, &original_sa)); | 
|  | 286 | ASSERT_TRUE(original_sa.sa_handler == NULL); | 
|  | 287 | ASSERT_TRUE(original_sa.sa_sigaction == NULL); | 
| Elliott Hughes | aa13e83 | 2014-09-04 15:43:10 -0700 | [diff] [blame] | 288 | ASSERT_EQ(0U, original_sa.sa_flags & ~sa_restorer); | 
| Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 289 |  | 
|  | 290 | // Set a traditional sa_handler signal handler. | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 291 | struct sigaction sa; | 
| Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 292 | memset(&sa, 0, sizeof(sa)); | 
|  | 293 | sigaddset(&sa.sa_mask, SIGALRM); | 
|  | 294 | sa.sa_flags = SA_ONSTACK; | 
|  | 295 | sa.sa_handler = EmptySignalHandler; | 
|  | 296 | ASSERT_EQ(0, sigaction(SIGALRM, &sa, NULL)); | 
|  | 297 |  | 
|  | 298 | // Check that we can read it back. | 
|  | 299 | memset(&sa, 0, sizeof(sa)); | 
|  | 300 | ASSERT_EQ(0, sigaction(SIGALRM, NULL, &sa)); | 
|  | 301 | ASSERT_TRUE(sa.sa_handler == EmptySignalHandler); | 
|  | 302 | ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler); | 
| Elliott Hughes | aa13e83 | 2014-09-04 15:43:10 -0700 | [diff] [blame] | 303 | ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK), sa.sa_flags & ~sa_restorer); | 
| Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 304 |  | 
|  | 305 | // Set a new-style sa_sigaction signal handler. | 
|  | 306 | memset(&sa, 0, sizeof(sa)); | 
|  | 307 | sigaddset(&sa.sa_mask, SIGALRM); | 
|  | 308 | sa.sa_flags = SA_ONSTACK | SA_SIGINFO; | 
|  | 309 | sa.sa_sigaction = EmptySignalAction; | 
|  | 310 | ASSERT_EQ(0, sigaction(SIGALRM, &sa, NULL)); | 
|  | 311 |  | 
|  | 312 | // Check that we can read it back. | 
|  | 313 | memset(&sa, 0, sizeof(sa)); | 
|  | 314 | ASSERT_EQ(0, sigaction(SIGALRM, NULL, &sa)); | 
|  | 315 | ASSERT_TRUE(sa.sa_sigaction == EmptySignalAction); | 
|  | 316 | ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler); | 
| Elliott Hughes | aa13e83 | 2014-09-04 15:43:10 -0700 | [diff] [blame] | 317 | ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK | SA_SIGINFO), sa.sa_flags & ~sa_restorer); | 
| Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 318 |  | 
|  | 319 | // Put everything back how it was. | 
|  | 320 | ASSERT_EQ(0, sigaction(SIGALRM, &original_sa, NULL)); | 
| Elliott Hughes | c7e9b23 | 2013-10-16 22:27:54 -0700 | [diff] [blame] | 321 | } | 
| Elliott Hughes | aa0ebda | 2014-02-11 19:57:06 -0800 | [diff] [blame] | 322 |  | 
|  | 323 | TEST(signal, sys_signame) { | 
| Elliott Hughes | 671e236 | 2014-02-12 19:04:27 -0800 | [diff] [blame] | 324 | #if defined(__BIONIC__) | 
| Elliott Hughes | aa0ebda | 2014-02-11 19:57:06 -0800 | [diff] [blame] | 325 | ASSERT_TRUE(sys_signame[0] == NULL); | 
|  | 326 | ASSERT_STREQ("HUP", sys_signame[SIGHUP]); | 
|  | 327 | #else | 
|  | 328 | GTEST_LOG_(INFO) << "This test does nothing.\n"; | 
|  | 329 | #endif | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | TEST(signal, sys_siglist) { | 
|  | 333 | ASSERT_TRUE(sys_siglist[0] == NULL); | 
|  | 334 | ASSERT_STREQ("Hangup", sys_siglist[SIGHUP]); | 
|  | 335 | } | 
| Elliott Hughes | 0990d4f | 2014-04-30 09:45:40 -0700 | [diff] [blame] | 336 |  | 
|  | 337 | TEST(signal, limits) { | 
|  | 338 | // This comes from the kernel. | 
|  | 339 | ASSERT_EQ(32, __SIGRTMIN); | 
|  | 340 |  | 
|  | 341 | // We reserve a non-zero number at the bottom for ourselves. | 
|  | 342 | ASSERT_GT(SIGRTMIN, __SIGRTMIN); | 
|  | 343 |  | 
|  | 344 | // MIPS has more signals than everyone else. | 
|  | 345 | #if defined(__mips__) | 
|  | 346 | ASSERT_EQ(128, __SIGRTMAX); | 
|  | 347 | #else | 
|  | 348 | ASSERT_EQ(64, __SIGRTMAX); | 
|  | 349 | #endif | 
|  | 350 |  | 
|  | 351 | // We don't currently reserve any at the top. | 
|  | 352 | ASSERT_EQ(SIGRTMAX, __SIGRTMAX); | 
|  | 353 | } | 
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 354 |  | 
|  | 355 | static int g_sigqueue_signal_handler_call_count = 0; | 
|  | 356 |  | 
|  | 357 | static void SigqueueSignalHandler(int signum, siginfo_t* info, void*) { | 
|  | 358 | ASSERT_EQ(SIGALRM, signum); | 
|  | 359 | ASSERT_EQ(SIGALRM, info->si_signo); | 
|  | 360 | ASSERT_EQ(SI_QUEUE, info->si_code); | 
|  | 361 | ASSERT_EQ(1, info->si_value.sival_int); | 
|  | 362 | ++g_sigqueue_signal_handler_call_count; | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | TEST(signal, sigqueue) { | 
|  | 366 | ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO); | 
|  | 367 | sigval_t sigval; | 
|  | 368 | sigval.sival_int = 1; | 
|  | 369 | errno = 0; | 
|  | 370 | ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); | 
|  | 371 | ASSERT_EQ(0, errno); | 
|  | 372 | ASSERT_EQ(1, g_sigqueue_signal_handler_call_count); | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 | TEST(signal, sigwaitinfo) { | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 376 | SignalMaskRestorer smr; | 
|  | 377 |  | 
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 378 | // Block SIGALRM. | 
|  | 379 | sigset_t just_SIGALRM; | 
|  | 380 | sigemptyset(&just_SIGALRM); | 
|  | 381 | sigaddset(&just_SIGALRM, SIGALRM); | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 382 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr)); | 
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 383 |  | 
|  | 384 | // Raise SIGALRM. | 
|  | 385 | sigval_t sigval; | 
|  | 386 | sigval.sival_int = 1; | 
|  | 387 | ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); | 
|  | 388 |  | 
|  | 389 | // Get pending SIGALRM. | 
|  | 390 | siginfo_t info; | 
|  | 391 | errno = 0; | 
|  | 392 | ASSERT_EQ(SIGALRM, sigwaitinfo(&just_SIGALRM, &info)); | 
|  | 393 | ASSERT_EQ(0, errno); | 
|  | 394 | ASSERT_EQ(SIGALRM, info.si_signo); | 
|  | 395 | ASSERT_EQ(1, info.si_value.sival_int); | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 396 | } | 
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 397 |  | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 398 | TEST(signal, sigwaitinfo64_SIGRTMIN) { | 
|  | 399 | SignalMaskRestorer smr; | 
|  | 400 |  | 
|  | 401 | // Block SIGRTMIN. | 
|  | 402 | sigset64_t just_SIGRTMIN; | 
|  | 403 | sigemptyset64(&just_SIGRTMIN); | 
|  | 404 | sigaddset64(&just_SIGRTMIN, SIGRTMIN); | 
|  | 405 | ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr)); | 
|  | 406 |  | 
|  | 407 | // Raise SIGRTMIN. | 
|  | 408 | sigval_t sigval; | 
|  | 409 | sigval.sival_int = 1; | 
|  | 410 | ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval)); | 
|  | 411 |  | 
|  | 412 | // Get pending SIGRTMIN. | 
|  | 413 | siginfo_t info; | 
|  | 414 | errno = 0; | 
|  | 415 | ASSERT_EQ(SIGRTMIN, sigwaitinfo64(&just_SIGRTMIN, &info)); | 
|  | 416 | ASSERT_EQ(0, errno); | 
|  | 417 | ASSERT_EQ(SIGRTMIN, info.si_signo); | 
|  | 418 | ASSERT_EQ(1, info.si_value.sival_int); | 
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 419 | } | 
|  | 420 |  | 
|  | 421 | TEST(signal, sigtimedwait) { | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 422 | SignalMaskRestorer smr; | 
|  | 423 |  | 
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 424 | // Block SIGALRM. | 
|  | 425 | sigset_t just_SIGALRM; | 
|  | 426 | sigemptyset(&just_SIGALRM); | 
|  | 427 | sigaddset(&just_SIGALRM, SIGALRM); | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 428 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr)); | 
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 429 |  | 
|  | 430 | // Raise SIGALRM. | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 431 | sigval_t sigval = { .sival_int = 1 }; | 
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 432 | ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); | 
|  | 433 |  | 
|  | 434 | // Get pending SIGALRM. | 
|  | 435 | siginfo_t info; | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 436 | timespec timeout = { .tv_sec = 2, .tv_nsec = 0 }; | 
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 437 | errno = 0; | 
|  | 438 | ASSERT_EQ(SIGALRM, sigtimedwait(&just_SIGALRM, &info, &timeout)); | 
|  | 439 | ASSERT_EQ(0, errno); | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 440 | } | 
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 441 |  | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 442 | TEST(signal, sigtimedwait64_SIGRTMIN) { | 
|  | 443 | SignalMaskRestorer smr; | 
|  | 444 |  | 
|  | 445 | // Block SIGRTMIN. | 
|  | 446 | sigset64_t just_SIGRTMIN; | 
|  | 447 | sigemptyset64(&just_SIGRTMIN); | 
|  | 448 | sigaddset64(&just_SIGRTMIN, SIGRTMIN); | 
|  | 449 | ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr)); | 
|  | 450 |  | 
|  | 451 | // Raise SIGALRM. | 
|  | 452 | sigval_t sigval = { .sival_int = 1 }; | 
|  | 453 | ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval)); | 
|  | 454 |  | 
|  | 455 | // Get pending SIGALRM. | 
|  | 456 | siginfo_t info; | 
|  | 457 | timespec timeout = { .tv_sec = 2, .tv_nsec = 0 }; | 
|  | 458 | errno = 0; | 
|  | 459 | ASSERT_EQ(SIGRTMIN, sigtimedwait64(&just_SIGRTMIN, &info, &timeout)); | 
|  | 460 | ASSERT_EQ(0, errno); | 
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 461 | } | 
|  | 462 |  | 
|  | 463 | static int64_t NanoTime() { | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 464 | timespec t; | 
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 465 | clock_gettime(CLOCK_MONOTONIC, &t); | 
|  | 466 | return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec; | 
|  | 467 | } | 
|  | 468 |  | 
|  | 469 | TEST(signal, sigtimedwait_timeout) { | 
|  | 470 | // Block SIGALRM. | 
|  | 471 | sigset_t just_SIGALRM; | 
|  | 472 | sigemptyset(&just_SIGALRM); | 
|  | 473 | sigaddset(&just_SIGALRM, SIGALRM); | 
|  | 474 | sigset_t original_set; | 
|  | 475 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set)); | 
|  | 476 |  | 
|  | 477 | // Wait timeout. | 
|  | 478 | int64_t start_time = NanoTime(); | 
|  | 479 | siginfo_t info; | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 480 | timespec timeout = { .tv_sec = 0, .tv_nsec = 1000000 }; | 
| Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 481 | errno = 0; | 
|  | 482 | ASSERT_EQ(-1, sigtimedwait(&just_SIGALRM, &info, &timeout)); | 
|  | 483 | ASSERT_EQ(EAGAIN, errno); | 
|  | 484 | ASSERT_GE(NanoTime() - start_time, 1000000); | 
|  | 485 |  | 
|  | 486 | ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &original_set, NULL)); | 
|  | 487 | } | 
| Josh Gao | 61cf3f3 | 2016-03-08 15:27:15 -0800 | [diff] [blame] | 488 |  | 
|  | 489 | #if defined(__BIONIC__) | 
|  | 490 | TEST(signal, rt_tgsigqueueinfo) { | 
|  | 491 | // Test whether rt_tgsigqueueinfo allows sending arbitrary si_code values to self. | 
|  | 492 | // If this fails, your kernel needs commit 66dd34a to be backported. | 
|  | 493 | static constexpr char error_msg[] = | 
|  | 494 | "\nPlease ensure that the following kernel patch has been applied:\n" | 
|  | 495 | "* https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=66dd34ad31e5963d72a700ec3f2449291d322921\n"; | 
|  | 496 | static siginfo received; | 
|  | 497 |  | 
| Josh Gao | d787852 | 2016-03-14 18:15:15 -0700 | [diff] [blame] | 498 | struct sigaction handler; | 
|  | 499 | memset(&handler, 0, sizeof(handler)); | 
| Josh Gao | 61cf3f3 | 2016-03-08 15:27:15 -0800 | [diff] [blame] | 500 | handler.sa_sigaction = [](int, siginfo_t* siginfo, void*) { received = *siginfo; }; | 
|  | 501 | handler.sa_flags = SA_SIGINFO; | 
|  | 502 |  | 
|  | 503 | ASSERT_EQ(0, sigaction(SIGUSR1, &handler, nullptr)); | 
|  | 504 |  | 
| Josh Gao | d787852 | 2016-03-14 18:15:15 -0700 | [diff] [blame] | 505 | siginfo sent; | 
|  | 506 | memset(&sent, 0, sizeof(sent)); | 
| Josh Gao | 61cf3f3 | 2016-03-08 15:27:15 -0800 | [diff] [blame] | 507 |  | 
|  | 508 | sent.si_code = SI_TKILL; | 
|  | 509 | ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent)) | 
|  | 510 | << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg; | 
|  | 511 | ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected " | 
|  | 512 | << sent.si_code << ", received " << received.si_code | 
|  | 513 | << error_msg; | 
|  | 514 |  | 
|  | 515 | sent.si_code = SI_USER; | 
|  | 516 | ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent)) | 
|  | 517 | << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg; | 
|  | 518 | ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected " | 
|  | 519 | << sent.si_code << ", received " << received.si_code | 
|  | 520 | << error_msg; | 
|  | 521 | } | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 522 | #endif | 
| Josh Gao | c244fcb | 2016-03-29 14:34:03 -0700 | [diff] [blame] | 523 |  | 
| Josh Gao | c244fcb | 2016-03-29 14:34:03 -0700 | [diff] [blame] | 524 | TEST(signal, sigset_size) { | 
|  | 525 | // The setjmp implementations for ARM, AArch64, x86, and x86_64 assume that sigset_t can fit in a | 
|  | 526 | // long. This is true because ARM and x86 have broken rt signal support, and AArch64 and x86_64 | 
|  | 527 | // both have a SIGRTMAX defined as 64. | 
| Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 528 | #if defined(__arm__) || defined(__aarch64__) || defined(__i386__) || defined(__x86_64__) | 
|  | 529 | #if defined(__BIONIC__) | 
| Josh Gao | c244fcb | 2016-03-29 14:34:03 -0700 | [diff] [blame] | 530 | 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] | 531 | #endif | 
|  | 532 | static_assert(sizeof(sigset64_t)*8 >= 64, "sigset64_t too small for real-time signals"); | 
|  | 533 | #endif | 
| Josh Gao | c244fcb | 2016-03-29 14:34:03 -0700 | [diff] [blame] | 534 | } | 
|  | 535 |  | 
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 536 | TEST(signal, sigignore_EINVAL) { | 
|  | 537 | errno = 0; | 
|  | 538 | ASSERT_EQ(-1, sigignore(99999)); | 
|  | 539 | ASSERT_EQ(EINVAL, errno); | 
|  | 540 | } | 
|  | 541 |  | 
|  | 542 | TEST(signal, sigignore) { | 
|  | 543 | errno = 0; | 
|  | 544 | EXPECT_EQ(-1, sigignore(SIGKILL)); | 
|  | 545 | EXPECT_EQ(errno, EINVAL); | 
|  | 546 |  | 
|  | 547 | errno = 0; | 
|  | 548 | EXPECT_EQ(-1, sigignore(SIGSTOP)); | 
|  | 549 | EXPECT_EQ(errno, EINVAL); | 
|  | 550 |  | 
|  | 551 | ScopedSignalHandler sigalrm{SIGALRM}; | 
|  | 552 | ASSERT_EQ(0, sigignore(SIGALRM)); | 
|  | 553 |  | 
|  | 554 | struct sigaction sa; | 
|  | 555 | ASSERT_EQ(0, sigaction(SIGALRM, nullptr, &sa)); | 
|  | 556 | EXPECT_EQ(SIG_IGN, sa.sa_handler); | 
|  | 557 | } | 
|  | 558 |  | 
|  | 559 | TEST(signal, sighold_EINVAL) { | 
|  | 560 | errno = 0; | 
|  | 561 | ASSERT_EQ(-1, sighold(99999)); | 
|  | 562 | ASSERT_EQ(EINVAL, errno); | 
|  | 563 | } | 
|  | 564 |  | 
|  | 565 | TEST(signal, sigpause_EINVAL) { | 
|  | 566 | errno = 0; | 
|  | 567 | ASSERT_EQ(-1, sigpause(99999)); | 
|  | 568 | ASSERT_EQ(EINVAL, errno); | 
|  | 569 | } | 
|  | 570 |  | 
|  | 571 | TEST(signal, sigrelse_EINVAL) { | 
|  | 572 | errno = 0; | 
|  | 573 | ASSERT_EQ(-1, sigpause(99999)); | 
|  | 574 | ASSERT_EQ(EINVAL, errno); | 
|  | 575 | } | 
|  | 576 |  | 
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 577 | static void TestSigholdSigpauseSigrelse(int sig) { | 
|  | 578 | static int signal_handler_call_count = 0; | 
|  | 579 | ScopedSignalHandler ssh{sig, [](int) { signal_handler_call_count++; }}; | 
|  | 580 | SignalMaskRestorer mask_restorer; | 
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 581 | sigset_t set; | 
|  | 582 |  | 
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 583 | // sighold(SIGALRM/SIGRTMIN) should add SIGALRM/SIGRTMIN to the signal mask ... | 
|  | 584 | ASSERT_EQ(0, sighold(sig)); | 
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 585 | ASSERT_EQ(0, sigprocmask(SIG_SETMASK, 0, &set)); | 
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 586 | EXPECT_TRUE(sigismember(&set, sig)); | 
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 587 |  | 
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 588 | // ... preventing our SIGALRM/SIGRTMIN handler from running ... | 
|  | 589 | raise(sig); | 
|  | 590 | ASSERT_EQ(0, signal_handler_call_count); | 
|  | 591 | // ... until sigpause(SIGALRM/SIGRTMIN) temporarily unblocks it. | 
|  | 592 | ASSERT_EQ(-1, sigpause(sig)); | 
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 593 | ASSERT_EQ(EINTR, errno); | 
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 594 | ASSERT_EQ(1, signal_handler_call_count); | 
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 595 |  | 
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 596 | if (sig >= SIGRTMIN && sizeof(void*) == 8) { | 
|  | 597 | // But sigpause(SIGALRM/SIGRTMIN) shouldn't permanently unblock SIGALRM/SIGRTMIN. | 
|  | 598 | ASSERT_EQ(0, sigprocmask(SIG_SETMASK, 0, &set)); | 
|  | 599 | EXPECT_TRUE(sigismember(&set, sig)); | 
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 600 |  | 
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 601 | // Whereas sigrelse(SIGALRM/SIGRTMIN) should. | 
|  | 602 | ASSERT_EQ(0, sigrelse(sig)); | 
|  | 603 | ASSERT_EQ(0, sigprocmask(SIG_SETMASK, 0, &set)); | 
|  | 604 | EXPECT_FALSE(sigismember(&set, sig)); | 
|  | 605 | } else { | 
|  | 606 | // sigismember won't work for SIGRTMIN on LP32. | 
|  | 607 | } | 
|  | 608 | } | 
|  | 609 |  | 
|  | 610 | TEST(signal, sighold_sigpause_sigrelse) { | 
|  | 611 | TestSigholdSigpauseSigrelse(SIGALRM); | 
|  | 612 | } | 
|  | 613 |  | 
|  | 614 | TEST(signal, sighold_sigpause_sigrelse_RT) { | 
|  | 615 | TestSigholdSigpauseSigrelse(SIGRTMIN); | 
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 616 | } | 
|  | 617 |  | 
|  | 618 | TEST(signal, sigset_EINVAL) { | 
|  | 619 | errno = 0; | 
|  | 620 | ASSERT_EQ(SIG_ERR, sigset(99999, SIG_DFL)); | 
|  | 621 | ASSERT_EQ(EINVAL, errno); | 
|  | 622 | } | 
|  | 623 |  | 
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 624 | TEST(signal, sigset_RT) { | 
|  | 625 | static int signal_handler_call_count = 0; | 
|  | 626 | auto signal_handler = [](int) { signal_handler_call_count++; }; | 
|  | 627 | ScopedSignalHandler ssh{SIGRTMIN, signal_handler}; | 
|  | 628 | SignalMaskRestorer mask_restorer; | 
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 629 |  | 
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 630 | ASSERT_EQ(signal_handler, sigset(SIGRTMIN, SIG_HOLD)); | 
|  | 631 | #if defined(__LP64__) | 
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 632 | sigset_t set; | 
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 633 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); | 
|  | 634 | ASSERT_TRUE(sigismember(&set, SIGRTMIN)); | 
|  | 635 | #endif | 
|  | 636 |  | 
|  | 637 | ASSERT_EQ(SIG_HOLD, sigset(SIGRTMIN, signal_handler)); | 
|  | 638 | ASSERT_EQ(signal_handler, sigset(SIGRTMIN, signal_handler)); | 
|  | 639 | ASSERT_EQ(0, signal_handler_call_count); | 
|  | 640 | raise(SIGRTMIN); | 
|  | 641 | ASSERT_EQ(1, signal_handler_call_count); | 
|  | 642 | } | 
|  | 643 |  | 
|  | 644 | TEST(signal, sigset) { | 
|  | 645 | static int signal_handler_call_count = 0; | 
|  | 646 | auto signal_handler = [](int) { signal_handler_call_count++; }; | 
|  | 647 | ScopedSignalHandler ssh{SIGALRM, signal_handler}; | 
|  | 648 | SignalMaskRestorer mask_restorer; | 
|  | 649 |  | 
|  | 650 | ASSERT_EQ(0, signal_handler_call_count); | 
|  | 651 | raise(SIGALRM); | 
|  | 652 | ASSERT_EQ(1, signal_handler_call_count); | 
|  | 653 |  | 
|  | 654 | // Block SIGALRM so the next sigset(SIGARLM) call will return SIG_HOLD. | 
|  | 655 | sigset_t set; | 
|  | 656 | sigemptyset(&set); | 
|  | 657 | sigaddset(&set, SIGALRM); | 
|  | 658 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &set, nullptr)); | 
|  | 659 |  | 
|  | 660 | sigemptyset(&set); | 
|  | 661 | ASSERT_EQ(SIG_HOLD, sigset(SIGALRM, signal_handler)); | 
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 662 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); | 
|  | 663 | EXPECT_FALSE(sigismember(&set, SIGALRM)); | 
|  | 664 |  | 
| Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 665 | ASSERT_EQ(signal_handler, sigset(SIGALRM, SIG_IGN)); | 
| Greg Hackmann | 5375bf6 | 2016-02-29 12:35:33 -0800 | [diff] [blame] | 666 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); | 
|  | 667 | EXPECT_FALSE(sigismember(&set, SIGALRM)); | 
|  | 668 |  | 
|  | 669 | ASSERT_EQ(SIG_IGN, sigset(SIGALRM, SIG_DFL)); | 
|  | 670 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); | 
|  | 671 | EXPECT_FALSE(sigismember(&set, SIGALRM)); | 
|  | 672 |  | 
|  | 673 | ASSERT_EQ(SIG_DFL, sigset(SIGALRM, SIG_HOLD)); | 
|  | 674 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set)); | 
|  | 675 | EXPECT_TRUE(sigismember(&set, SIGALRM)); | 
|  | 676 | } | 
| Elliott Hughes | 7532b32 | 2017-07-11 15:00:17 -0700 | [diff] [blame] | 677 |  | 
|  | 678 | TEST(signal, killpg_EINVAL) { | 
|  | 679 | // POSIX leaves pgrp <= 1 undefined, but glibc fails with EINVAL for < 0 | 
|  | 680 | // and passes 0 through to kill(2). | 
|  | 681 | errno = 0; | 
|  | 682 | ASSERT_EQ(-1, killpg(-1, SIGKILL)); | 
|  | 683 | ASSERT_EQ(EINVAL, errno); | 
|  | 684 | } |