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