blob: 3c660346df47d9c30c8985b4e8ffc9ed686acd2b [file] [log] [blame]
Elliott Hughesda73f652012-11-30 16:40:55 -08001/*
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 Gao61cf3f32016-03-08 15:27:15 -080017#include <errno.h>
Elliott Hughesafe58ad2014-09-04 13:54:42 -070018#include <signal.h>
Josh Gao61cf3f32016-03-08 15:27:15 -080019#include <sys/syscall.h>
20#include <sys/types.h>
21#include <unistd.h>
Elliott Hughesda73f652012-11-30 16:40:55 -080022
Elliott Hughesca3f8e42019-10-28 15:59:38 -070023#include <chrono>
Elliott Hughes5905d6f2018-01-30 15:09:51 -080024#include <thread>
25
Josh Gaobaf20fc2018-10-08 17:28:07 -070026#include <android-base/macros.h>
Evgeny Eltsin4d9264c2019-12-17 14:17:07 +010027#include <android-base/threads.h>
28
Elliott Hughesafe58ad2014-09-04 13:54:42 -070029#include <gtest/gtest.h>
Elliott Hughesda73f652012-11-30 16:40:55 -080030
Elliott Hughes71ba5892018-02-07 12:44:45 -080031#include "SignalUtils.h"
Evgeny Eltsin4d9264c2019-12-17 14:17:07 +010032#include "utils.h"
Christopher Ferris13613132013-10-28 15:24:04 -070033
Elliott Hughesca3f8e42019-10-28 15:59:38 -070034using namespace std::chrono_literals;
35
Elliott Hughes5905d6f2018-01-30 15:09:51 -080036static int SIGNAL_MIN() {
Elliott Hughes40d105c2013-10-16 12:53:58 -070037 return 1; // Signals start at 1 (SIGHUP), not 0.
38}
39
Elliott Hughes5905d6f2018-01-30 15:09:51 -080040template <typename SigSetT>
41static int SIGNAL_MAX(SigSetT* set) {
42 return sizeof(*set) * 8;
Elliott Hughes40d105c2013-10-16 12:53:58 -070043}
44
Elliott Hughes5905d6f2018-01-30 15:09:51 -080045template <typename SigSetT>
46static void TestSigSet1(int (fn)(SigSetT*)) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070047 // nullptr sigset_t*/sigset64_t*.
48 SigSetT* set_ptr = nullptr;
Elliott Hughesda73f652012-11-30 16:40:55 -080049 errno = 0;
50 ASSERT_EQ(-1, fn(set_ptr));
51 ASSERT_EQ(EINVAL, errno);
52
Yi Kong32bc0fc2018-08-02 17:31:13 -070053 // Non-nullptr.
Elliott Hughes5905d6f2018-01-30 15:09:51 -080054 SigSetT set = {};
Elliott Hughesda73f652012-11-30 16:40:55 -080055 errno = 0;
56 ASSERT_EQ(0, fn(&set));
57 ASSERT_EQ(0, errno);
58}
59
Elliott Hughes5905d6f2018-01-30 15:09:51 -080060template <typename SigSetT>
61static void TestSigSet2(int (fn)(SigSetT*, int)) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070062 // nullptr sigset_t*/sigset64_t*.
63 SigSetT* set_ptr = nullptr;
Elliott Hughesda73f652012-11-30 16:40:55 -080064 errno = 0;
65 ASSERT_EQ(-1, fn(set_ptr, SIGSEGV));
66 ASSERT_EQ(EINVAL, errno);
67
Elliott Hughes5905d6f2018-01-30 15:09:51 -080068 SigSetT set = {};
Elliott Hughesda73f652012-11-30 16:40:55 -080069
Elliott Hughesda73f652012-11-30 16:40:55 -080070 // Bad signal number: too small.
71 errno = 0;
72 ASSERT_EQ(-1, fn(&set, 0));
73 ASSERT_EQ(EINVAL, errno);
74
75 // Bad signal number: too high.
76 errno = 0;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080077 ASSERT_EQ(-1, fn(&set, SIGNAL_MAX(&set) + 1));
Elliott Hughesda73f652012-11-30 16:40:55 -080078 ASSERT_EQ(EINVAL, errno);
79
80 // Good signal numbers, low and high ends of range.
81 errno = 0;
Elliott Hughes40d105c2013-10-16 12:53:58 -070082 ASSERT_EQ(0, fn(&set, SIGNAL_MIN()));
Elliott Hughesda73f652012-11-30 16:40:55 -080083 ASSERT_EQ(0, errno);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080084 ASSERT_EQ(0, fn(&set, SIGNAL_MAX(&set)));
Elliott Hughesda73f652012-11-30 16:40:55 -080085 ASSERT_EQ(0, errno);
86}
87
Elliott Hughesda73f652012-11-30 16:40:55 -080088TEST(signal, sigaddset_invalid) {
89 TestSigSet2(sigaddset);
90}
91
Elliott Hughes5905d6f2018-01-30 15:09:51 -080092TEST(signal, sigaddset64_invalid) {
93#if defined(__BIONIC__)
94 TestSigSet2(sigaddset64);
95#endif
96}
97
Elliott Hughesda73f652012-11-30 16:40:55 -080098TEST(signal, sigdelset_invalid) {
99 TestSigSet2(sigdelset);
100}
101
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800102TEST(signal, sigdelset64_invalid) {
103#if defined(__BIONIC__)
104 TestSigSet2(sigdelset64);
105#endif
106}
107
Elliott Hughesda73f652012-11-30 16:40:55 -0800108TEST(signal, sigemptyset_invalid) {
109 TestSigSet1(sigemptyset);
110}
111
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800112TEST(signal, sigemptyset64_invalid) {
113#if defined(__BIONIC__)
114 TestSigSet1(sigemptyset64);
115#endif
116}
117
Elliott Hughesda73f652012-11-30 16:40:55 -0800118TEST(signal, sigfillset_invalid) {
119 TestSigSet1(sigfillset);
120}
Chris Dearmand8a5a6f2012-12-07 18:41:10 -0800121
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800122TEST(signal, sigfillset64_invalid) {
123#if defined(__BIONIC__)
124 TestSigSet1(sigfillset64);
125#endif
126}
127
128TEST(signal, sigismember_invalid) {
129 TestSigSet2(sigismember);
130}
131
132TEST(signal, sigismember64_invalid) {
133#if defined(__BIONIC__)
134 TestSigSet2(sigismember64);
135#endif
136}
137
Chris Dearmand8a5a6f2012-12-07 18:41:10 -0800138TEST(signal, raise_invalid) {
139 errno = 0;
140 ASSERT_EQ(-1, raise(-1));
141 ASSERT_EQ(EINVAL, errno);
142}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800143
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800144static void raise_in_signal_handler_helper(int signal_number) {
145 ASSERT_EQ(SIGALRM, signal_number);
146 static int count = 0;
147 if (++count == 1) {
148 raise(SIGALRM);
149 }
150}
151
152TEST(signal, raise_in_signal_handler) {
153 ScopedSignalHandler ssh(SIGALRM, raise_in_signal_handler_helper);
154 raise(SIGALRM);
155}
156
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800157TEST(signal, sigwait_SIGALRM) {
158 ScopedSignalHandler ssh(SIGALRM, [](int sig) { ASSERT_EQ(SIGALRM, sig); });
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800159
160 sigset_t wait_set;
161 sigemptyset(&wait_set);
162 sigaddset(&wait_set, SIGALRM);
163
164 alarm(1);
165
166 int received_signal;
167 errno = 0;
168 ASSERT_EQ(0, sigwait(&wait_set, &received_signal));
169 ASSERT_EQ(0, errno);
170 ASSERT_EQ(SIGALRM, received_signal);
171}
Elliott Hughes1f5af922013-10-15 18:01:56 -0700172
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800173TEST(signal, sigwait64_SIGRTMIN) {
174 ScopedSignalHandler ssh(SIGRTMIN, [](int sig) { ASSERT_EQ(SIGRTMIN, sig); });
Elliott Hughes1f5af922013-10-15 18:01:56 -0700175
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800176 sigset64_t wait_set;
177 sigemptyset64(&wait_set);
178 sigaddset64(&wait_set, SIGRTMIN);
179
Evgeny Eltsin4d9264c2019-12-17 14:17:07 +0100180 pid_t tid = gettid();
181 std::thread thread([&tid]() {
Evgeny Eltsinb29173c2019-12-16 16:57:29 +0100182 sleep(1);
Evgeny Eltsin4d9264c2019-12-17 14:17:07 +0100183 tgkill(getpid(), tid, SIGRTMIN);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800184 });
185
186 int received_signal;
187 errno = 0;
188 ASSERT_EQ(0, sigwait64(&wait_set, &received_signal));
189 ASSERT_EQ(0, errno);
190 ASSERT_EQ(SIGRTMIN, received_signal);
191
192 thread.join();
Elliott Hughes1f5af922013-10-15 18:01:56 -0700193}
194
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800195static int g_sigsuspend_signal_handler_call_count = 0;
196
Elliott Hughes40d105c2013-10-16 12:53:58 -0700197TEST(signal, sigsuspend_sigpending) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800198 SignalMaskRestorer smr;
199
Elliott Hughes1f5af922013-10-15 18:01:56 -0700200 // Block SIGALRM.
201 sigset_t just_SIGALRM;
202 sigemptyset(&just_SIGALRM);
203 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800204 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Elliott Hughes1f5af922013-10-15 18:01:56 -0700205
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800206 ScopedSignalHandler ssh(SIGALRM, [](int) { ++g_sigsuspend_signal_handler_call_count; });
Elliott Hughes11952072013-10-24 15:15:14 -0700207
Elliott Hughes40d105c2013-10-16 12:53:58 -0700208 // There should be no pending signals.
209 sigset_t pending;
210 sigemptyset(&pending);
211 ASSERT_EQ(0, sigpending(&pending));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800212 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
Elliott Hughes40d105c2013-10-16 12:53:58 -0700213 EXPECT_FALSE(sigismember(&pending, i)) << i;
214 }
215
Elliott Hughes1f5af922013-10-15 18:01:56 -0700216 // Raise SIGALRM and check our signal handler wasn't called.
217 raise(SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800218 ASSERT_EQ(0, g_sigsuspend_signal_handler_call_count);
Elliott Hughes1f5af922013-10-15 18:01:56 -0700219
Elliott Hughes40d105c2013-10-16 12:53:58 -0700220 // We should now have a pending SIGALRM but nothing else.
221 sigemptyset(&pending);
222 ASSERT_EQ(0, sigpending(&pending));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800223 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
Elliott Hughes40d105c2013-10-16 12:53:58 -0700224 EXPECT_EQ((i == SIGALRM), sigismember(&pending, i));
225 }
226
Elliott Hughes1f5af922013-10-15 18:01:56 -0700227 // Use sigsuspend to block everything except SIGALRM...
228 sigset_t not_SIGALRM;
229 sigfillset(&not_SIGALRM);
230 sigdelset(&not_SIGALRM, SIGALRM);
231 ASSERT_EQ(-1, sigsuspend(&not_SIGALRM));
232 ASSERT_EQ(EINTR, errno);
233 // ...and check that we now receive our pending SIGALRM.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800234 ASSERT_EQ(1, g_sigsuspend_signal_handler_call_count);
235}
Elliott Hughes1f5af922013-10-15 18:01:56 -0700236
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800237static int g_sigsuspend64_signal_handler_call_count = 0;
238
239TEST(signal, sigsuspend64_sigpending64) {
240 SignalMaskRestorer smr;
241
242 // Block SIGRTMIN.
243 sigset64_t just_SIGRTMIN;
244 sigemptyset64(&just_SIGRTMIN);
245 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
246 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
247
248 ScopedSignalHandler ssh(SIGRTMIN, [](int) { ++g_sigsuspend64_signal_handler_call_count; });
249
250 // There should be no pending signals.
251 sigset64_t pending;
252 sigemptyset64(&pending);
253 ASSERT_EQ(0, sigpending64(&pending));
254 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
255 EXPECT_FALSE(sigismember64(&pending, i)) << i;
256 }
257
258 // Raise SIGRTMIN and check our signal handler wasn't called.
259 raise(SIGRTMIN);
260 ASSERT_EQ(0, g_sigsuspend64_signal_handler_call_count);
261
262 // We should now have a pending SIGRTMIN but nothing else.
263 sigemptyset64(&pending);
264 ASSERT_EQ(0, sigpending64(&pending));
265 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
266 EXPECT_EQ((i == SIGRTMIN), sigismember64(&pending, i));
267 }
268
269 // Use sigsuspend64 to block everything except SIGRTMIN...
270 sigset64_t not_SIGRTMIN;
271 sigfillset64(&not_SIGRTMIN);
272 sigdelset64(&not_SIGRTMIN, SIGRTMIN);
273 ASSERT_EQ(-1, sigsuspend64(&not_SIGRTMIN));
274 ASSERT_EQ(EINTR, errno);
275 // ...and check that we now receive our pending SIGRTMIN.
276 ASSERT_EQ(1, g_sigsuspend64_signal_handler_call_count);
Elliott Hughes1f5af922013-10-15 18:01:56 -0700277}
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700278
Elliott Hughes3e235912018-02-01 14:21:51 -0800279template <typename SigActionT, typename SigSetT>
280static void TestSigAction(int (sigaction_fn)(int, const SigActionT*, SigActionT*),
281 int (sigaddset_fn)(SigSetT*, int),
282 int sig) {
Elliott Hughesafe58ad2014-09-04 13:54:42 -0700283 // Both bionic and glibc set SA_RESTORER when talking to the kernel on arm,
284 // arm64, x86, and x86-64. The version of glibc we're using also doesn't
Elliott Hughes6a65ccd2020-02-13 09:48:14 -0800285 // define SA_RESTORER, but luckily it's the same value everywhere.
Elliott Hughesaa13e832014-09-04 15:43:10 -0700286 static const unsigned sa_restorer = 0x4000000;
Elliott Hughesafe58ad2014-09-04 13:54:42 -0700287
Elliott Hughes3e235912018-02-01 14:21:51 -0800288 // See what's currently set for this signal.
289 SigActionT original_sa = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700290 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &original_sa));
291 ASSERT_TRUE(original_sa.sa_handler == nullptr);
292 ASSERT_TRUE(original_sa.sa_sigaction == nullptr);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700293 ASSERT_EQ(0U, original_sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100294#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100295 ASSERT_EQ(bool(original_sa.sa_flags & sa_restorer), bool(original_sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100296#endif
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700297
298 // Set a traditional sa_handler signal handler.
Elliott Hughes3e235912018-02-01 14:21:51 -0800299 auto no_op_signal_handler = [](int) {};
300 SigActionT sa = {};
301 sigaddset_fn(&sa.sa_mask, sig);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700302 sa.sa_flags = SA_ONSTACK;
Elliott Hughes3e235912018-02-01 14:21:51 -0800303 sa.sa_handler = no_op_signal_handler;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700304 ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700305
306 // Check that we can read it back.
Elliott Hughes3e235912018-02-01 14:21:51 -0800307 sa = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700308 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa));
Elliott Hughes3e235912018-02-01 14:21:51 -0800309 ASSERT_TRUE(sa.sa_handler == no_op_signal_handler);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700310 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700311 ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK), sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100312#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100313 ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100314#endif
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700315
316 // Set a new-style sa_sigaction signal handler.
Elliott Hughes3e235912018-02-01 14:21:51 -0800317 auto no_op_sigaction = [](int, siginfo_t*, void*) {};
318 sa = {};
319 sigaddset_fn(&sa.sa_mask, sig);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700320 sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
Elliott Hughes3e235912018-02-01 14:21:51 -0800321 sa.sa_sigaction = no_op_sigaction;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700322 ASSERT_EQ(0, sigaction_fn(sig, &sa, nullptr));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700323
324 // Check that we can read it back.
Elliott Hughes3e235912018-02-01 14:21:51 -0800325 sa = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -0700326 ASSERT_EQ(0, sigaction_fn(sig, nullptr, &sa));
Elliott Hughes3e235912018-02-01 14:21:51 -0800327 ASSERT_TRUE(sa.sa_sigaction == no_op_sigaction);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700328 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700329 ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK | SA_SIGINFO), sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100330#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100331 ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100332#endif
Elliott Hughes11952072013-10-24 15:15:14 -0700333
334 // Put everything back how it was.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700335 ASSERT_EQ(0, sigaction_fn(sig, &original_sa, nullptr));
Elliott Hughes3e235912018-02-01 14:21:51 -0800336}
337
338TEST(signal, sigaction) {
339 TestSigAction(sigaction, sigaddset, SIGALRM);
340}
341
342TEST(signal, sigaction64_SIGRTMIN) {
343 TestSigAction(sigaction64, sigaddset64, SIGRTMIN);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700344}
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800345
Josh Gao6fcba932018-02-09 13:38:32 -0800346static void ClearSignalMask() {
347 uint64_t sigset = 0;
Josh Gaobaf20fc2018-10-08 17:28:07 -0700348 SignalSetAdd(&sigset, __SIGRTMIN);
349 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) {
350 abort();
351 }
352}
353
354static void FillSignalMask() {
355 uint64_t sigset = ~0ULL;
356 for (int signo = __SIGRTMIN + 1; signo < SIGRTMIN; ++signo) {
357 SignalSetDel(&sigset, signo);
358 }
Josh Gao6fcba932018-02-09 13:38:32 -0800359 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, &sigset, nullptr, sizeof(sigset)) != 0) {
360 abort();
361 }
362}
363
364static uint64_t GetSignalMask() {
365 uint64_t sigset;
366 if (syscall(__NR_rt_sigprocmask, SIG_SETMASK, nullptr, &sigset, sizeof(sigset)) != 0) {
367 abort();
368 }
369 return sigset;
370}
371
Josh Gaobaf20fc2018-10-08 17:28:07 -0700372static void TestSignalMaskFiltered(uint64_t sigset) {
373#if defined(__BIONIC__)
374 for (int signo = __SIGRTMIN; signo < SIGRTMIN; ++signo) {
Josh Gao6fcba932018-02-09 13:38:32 -0800375 bool signal_blocked = sigset & (1ULL << (signo - 1));
Josh Gaobaf20fc2018-10-08 17:28:07 -0700376 if (signo == __SIGRTMIN) {
377 // TIMER_SIGNAL must be blocked.
Josh Gao6fcba932018-02-09 13:38:32 -0800378 EXPECT_EQ(true, signal_blocked) << "signal " << signo;
Josh Gaobaf20fc2018-10-08 17:28:07 -0700379 } else {
380 // The other reserved signals must not be blocked.
Josh Gao6fcba932018-02-09 13:38:32 -0800381 EXPECT_EQ(false, signal_blocked) << "signal " << signo;
Josh Gao6fcba932018-02-09 13:38:32 -0800382 }
383 }
Josh Gaobaf20fc2018-10-08 17:28:07 -0700384#else
385 UNUSED(sigset);
386#endif
Josh Gao6fcba932018-02-09 13:38:32 -0800387}
388
Josh Gaobaf20fc2018-10-08 17:28:07 -0700389static void TestSignalMaskFunction(std::function<void()> fn) {
Josh Gao6fcba932018-02-09 13:38:32 -0800390 ClearSignalMask();
391 fn();
Josh Gaobaf20fc2018-10-08 17:28:07 -0700392 TestSignalMaskFiltered(GetSignalMask());
Josh Gao6fcba932018-02-09 13:38:32 -0800393}
394
395TEST(signal, sigaction_filter) {
396 ClearSignalMask();
397 static uint64_t sigset;
398 struct sigaction sa = {};
399 sa.sa_handler = [](int) { sigset = GetSignalMask(); };
Josh Gaoba40ff62019-01-22 22:53:49 -0800400 sa.sa_flags = SA_ONSTACK | SA_NODEFER;
Josh Gao6fcba932018-02-09 13:38:32 -0800401 sigfillset(&sa.sa_mask);
402 sigaction(SIGUSR1, &sa, nullptr);
403 raise(SIGUSR1);
Josh Gaoba40ff62019-01-22 22:53:49 -0800404
405 // On LP32, struct sigaction::sa_mask is only 32-bits wide.
406 unsigned long expected_sigset = ~0UL;
407
408 // SIGKILL and SIGSTOP are always blocked.
409 expected_sigset &= ~(1UL << (SIGKILL - 1));
410 expected_sigset &= ~(1UL << (SIGSTOP - 1));
411
412 ASSERT_EQ(static_cast<uint64_t>(expected_sigset), sigset);
Josh Gao6fcba932018-02-09 13:38:32 -0800413}
414
415TEST(signal, sigaction64_filter) {
416 ClearSignalMask();
417 static uint64_t sigset;
418 struct sigaction64 sa = {};
419 sa.sa_handler = [](int) { sigset = GetSignalMask(); };
Josh Gaoba40ff62019-01-22 22:53:49 -0800420 sa.sa_flags = SA_ONSTACK | SA_NODEFER;
Josh Gao6fcba932018-02-09 13:38:32 -0800421 sigfillset64(&sa.sa_mask);
422 sigaction64(SIGUSR1, &sa, nullptr);
423 raise(SIGUSR1);
Josh Gaoba40ff62019-01-22 22:53:49 -0800424
425 uint64_t expected_sigset = ~0ULL;
426
427 // SIGKILL and SIGSTOP are always blocked.
428 expected_sigset &= ~(1ULL << (SIGKILL - 1));
429 expected_sigset &= ~(1ULL << (SIGSTOP - 1));
430
431 ASSERT_EQ(expected_sigset, sigset);
Josh Gao6fcba932018-02-09 13:38:32 -0800432}
433
434TEST(signal, sigprocmask_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700435 TestSignalMaskFunction([]() {
436 ClearSignalMask();
437 sigset_t sigset_libc;
438 sigfillset(&sigset_libc);
439 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &sigset_libc, nullptr));
440 });
Josh Gao6fcba932018-02-09 13:38:32 -0800441}
442
443TEST(signal, sigprocmask64_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700444 TestSignalMaskFunction([]() {
445 ClearSignalMask();
446 sigset64_t sigset_libc;
447 sigfillset64(&sigset_libc);
448 ASSERT_EQ(0, sigprocmask64(SIG_SETMASK, &sigset_libc, nullptr));
449 });
Josh Gao6fcba932018-02-09 13:38:32 -0800450}
451
452TEST(signal, pthread_sigmask_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700453 TestSignalMaskFunction([]() {
454 ClearSignalMask();
455 sigset_t sigset_libc;
456 sigfillset(&sigset_libc);
457 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &sigset_libc, nullptr));
458 });
Josh Gao6fcba932018-02-09 13:38:32 -0800459}
460
461TEST(signal, pthread_sigmask64_setmask_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700462 TestSignalMaskFunction([]() {
463 ClearSignalMask();
464 sigset64_t sigset_libc;
465 sigfillset64(&sigset_libc);
466 ASSERT_EQ(0, pthread_sigmask64(SIG_SETMASK, &sigset_libc, nullptr));
467 });
Josh Gao6fcba932018-02-09 13:38:32 -0800468}
469
470TEST(signal, sigprocmask_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700471 TestSignalMaskFunction([]() {
472 ClearSignalMask();
473 sigset_t sigset_libc;
474 sigfillset(&sigset_libc);
475 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &sigset_libc, nullptr));
476 });
Josh Gao6fcba932018-02-09 13:38:32 -0800477}
478
479TEST(signal, sigprocmask64_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700480 TestSignalMaskFunction([]() {
481 ClearSignalMask();
482 sigset64_t sigset_libc;
483 sigfillset64(&sigset_libc);
484 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &sigset_libc, nullptr));
485 });
Josh Gao6fcba932018-02-09 13:38:32 -0800486}
487
488TEST(signal, pthread_sigmask_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700489 TestSignalMaskFunction([]() {
490 ClearSignalMask();
491 sigset_t sigset_libc;
492 sigfillset(&sigset_libc);
493 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &sigset_libc, nullptr));
494 });
Josh Gao6fcba932018-02-09 13:38:32 -0800495}
496
497TEST(signal, pthread_sigmask64_block_filter) {
Josh Gaobaf20fc2018-10-08 17:28:07 -0700498 TestSignalMaskFunction([]() {
499 ClearSignalMask();
500 sigset64_t sigset_libc;
501 sigfillset64(&sigset_libc);
502 ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, &sigset_libc, nullptr));
503 });
504}
505
506TEST(signal, sigprocmask_unblock_filter) {
507 TestSignalMaskFunction([]() {
508 FillSignalMask();
509 sigset_t sigset_libc;
510 sigfillset(&sigset_libc);
511 ASSERT_EQ(0, sigprocmask(SIG_UNBLOCK, &sigset_libc, nullptr));
512 });
513}
514
515TEST(signal, sigprocmask64_unblock_filter) {
516 TestSignalMaskFunction([]() {
517 FillSignalMask();
518 sigset64_t sigset_libc;
519 sigfillset64(&sigset_libc);
520 ASSERT_EQ(0, sigprocmask64(SIG_UNBLOCK, &sigset_libc, nullptr));
521 });
522}
523
524TEST(signal, pthread_sigmask_unblock_filter) {
525 TestSignalMaskFunction([]() {
526 FillSignalMask();
527 sigset_t sigset_libc;
528 sigfillset(&sigset_libc);
529 ASSERT_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sigset_libc, nullptr));
530 });
531}
532
533TEST(signal, pthread_sigmask64_unblock_filter) {
534 TestSignalMaskFunction([]() {
535 FillSignalMask();
536 sigset64_t sigset_libc;
537 sigfillset64(&sigset_libc);
538 ASSERT_EQ(0, pthread_sigmask64(SIG_UNBLOCK, &sigset_libc, nullptr));
539 });
Josh Gao6fcba932018-02-09 13:38:32 -0800540}
541
542// glibc filters out signals via sigfillset, not the actual underlying functions.
543TEST(signal, sigset_filter) {
544#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700545 TestSignalMaskFunction([]() {
546 for (int i = 1; i <= 64; ++i) {
547 sigset(i, SIG_HOLD);
548 }
549 });
Josh Gao6fcba932018-02-09 13:38:32 -0800550#endif
551}
552
553TEST(signal, sighold_filter) {
554#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700555 TestSignalMaskFunction([]() {
556 for (int i = 1; i <= 64; ++i) {
557 sighold(i);
558 }
559 });
Josh Gao6fcba932018-02-09 13:38:32 -0800560#endif
561}
562
563#if defined(__BIONIC__)
564// Not exposed via headers, but the symbols are available if you declare them yourself.
565extern "C" int sigblock(int);
566extern "C" int sigsetmask(int);
567#endif
568
569TEST(signal, sigblock_filter) {
570#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700571 TestSignalMaskFunction([]() {
572 sigblock(~0U);
573 });
Josh Gao6fcba932018-02-09 13:38:32 -0800574#endif
575}
576
577TEST(signal, sigsetmask_filter) {
578#if defined(__BIONIC__)
Josh Gaobaf20fc2018-10-08 17:28:07 -0700579 TestSignalMaskFunction([]() {
580 sigsetmask(~0U);
581 });
Josh Gao6fcba932018-02-09 13:38:32 -0800582#endif
583}
584
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800585TEST(signal, sys_signame) {
Elliott Hughes671e2362014-02-12 19:04:27 -0800586#if defined(__BIONIC__)
Yi Kong32bc0fc2018-08-02 17:31:13 -0700587 ASSERT_TRUE(sys_signame[0] == nullptr);
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800588 ASSERT_STREQ("HUP", sys_signame[SIGHUP]);
589#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800590 GTEST_SKIP() << "glibc doesn't have sys_signame";
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800591#endif
592}
593
594TEST(signal, sys_siglist) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700595 ASSERT_TRUE(sys_siglist[0] == nullptr);
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800596 ASSERT_STREQ("Hangup", sys_siglist[SIGHUP]);
597}
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700598
599TEST(signal, limits) {
Elliott Hughes6a65ccd2020-02-13 09:48:14 -0800600 // These come from the kernel.
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700601 ASSERT_EQ(32, __SIGRTMIN);
Elliott Hughes6a65ccd2020-02-13 09:48:14 -0800602 ASSERT_EQ(64, __SIGRTMAX);
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700603
604 // We reserve a non-zero number at the bottom for ourselves.
605 ASSERT_GT(SIGRTMIN, __SIGRTMIN);
606
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700607 // We don't currently reserve any at the top.
608 ASSERT_EQ(SIGRTMAX, __SIGRTMAX);
609}
Yabin Cui63481602014-12-01 17:41:04 -0800610
611static int g_sigqueue_signal_handler_call_count = 0;
612
613static void SigqueueSignalHandler(int signum, siginfo_t* info, void*) {
614 ASSERT_EQ(SIGALRM, signum);
615 ASSERT_EQ(SIGALRM, info->si_signo);
616 ASSERT_EQ(SI_QUEUE, info->si_code);
617 ASSERT_EQ(1, info->si_value.sival_int);
618 ++g_sigqueue_signal_handler_call_count;
619}
620
621TEST(signal, sigqueue) {
622 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
623 sigval_t sigval;
624 sigval.sival_int = 1;
625 errno = 0;
626 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
627 ASSERT_EQ(0, errno);
628 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
629}
630
Josh Gao726b63f2018-08-27 16:00:58 -0700631TEST(signal, pthread_sigqueue_self) {
632 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
633 sigval_t sigval;
634 sigval.sival_int = 1;
635 errno = 0;
636 ASSERT_EQ(0, pthread_sigqueue(pthread_self(), SIGALRM, sigval));
637 ASSERT_EQ(0, errno);
638 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
639}
640
641TEST(signal, pthread_sigqueue_other) {
642 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
643 sigval_t sigval;
644 sigval.sival_int = 1;
645
646 sigset_t mask;
647 sigfillset(&mask);
648 pthread_sigmask(SIG_SETMASK, &mask, nullptr);
649 pthread_t thread;
650 int rc = pthread_create(&thread, nullptr,
651 [](void*) -> void* {
652 sigset_t mask;
653 sigemptyset(&mask);
654 sigsuspend(&mask);
655 return nullptr;
656 },
657 nullptr);
658 ASSERT_EQ(0, rc);
659
660 errno = 0;
661 ASSERT_EQ(0, pthread_sigqueue(thread, SIGALRM, sigval));
662 ASSERT_EQ(0, errno);
663 pthread_join(thread, nullptr);
664 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
665}
666
Yabin Cui63481602014-12-01 17:41:04 -0800667TEST(signal, sigwaitinfo) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800668 SignalMaskRestorer smr;
669
Yabin Cui63481602014-12-01 17:41:04 -0800670 // Block SIGALRM.
671 sigset_t just_SIGALRM;
672 sigemptyset(&just_SIGALRM);
673 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800674 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Yabin Cui63481602014-12-01 17:41:04 -0800675
676 // Raise SIGALRM.
677 sigval_t sigval;
678 sigval.sival_int = 1;
679 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
680
681 // Get pending SIGALRM.
682 siginfo_t info;
683 errno = 0;
684 ASSERT_EQ(SIGALRM, sigwaitinfo(&just_SIGALRM, &info));
685 ASSERT_EQ(0, errno);
686 ASSERT_EQ(SIGALRM, info.si_signo);
687 ASSERT_EQ(1, info.si_value.sival_int);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800688}
Yabin Cui63481602014-12-01 17:41:04 -0800689
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800690TEST(signal, sigwaitinfo64_SIGRTMIN) {
691 SignalMaskRestorer smr;
692
693 // Block SIGRTMIN.
694 sigset64_t just_SIGRTMIN;
695 sigemptyset64(&just_SIGRTMIN);
696 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
697 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
698
699 // Raise SIGRTMIN.
700 sigval_t sigval;
701 sigval.sival_int = 1;
702 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
703
704 // Get pending SIGRTMIN.
705 siginfo_t info;
706 errno = 0;
707 ASSERT_EQ(SIGRTMIN, sigwaitinfo64(&just_SIGRTMIN, &info));
708 ASSERT_EQ(0, errno);
709 ASSERT_EQ(SIGRTMIN, info.si_signo);
710 ASSERT_EQ(1, info.si_value.sival_int);
Yabin Cui63481602014-12-01 17:41:04 -0800711}
712
713TEST(signal, sigtimedwait) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800714 SignalMaskRestorer smr;
715
Yabin Cui63481602014-12-01 17:41:04 -0800716 // Block SIGALRM.
717 sigset_t just_SIGALRM;
718 sigemptyset(&just_SIGALRM);
719 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800720 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Yabin Cui63481602014-12-01 17:41:04 -0800721
722 // Raise SIGALRM.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800723 sigval_t sigval = { .sival_int = 1 };
Yabin Cui63481602014-12-01 17:41:04 -0800724 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
725
726 // Get pending SIGALRM.
727 siginfo_t info;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800728 timespec timeout = { .tv_sec = 2, .tv_nsec = 0 };
Yabin Cui63481602014-12-01 17:41:04 -0800729 errno = 0;
730 ASSERT_EQ(SIGALRM, sigtimedwait(&just_SIGALRM, &info, &timeout));
731 ASSERT_EQ(0, errno);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800732}
Yabin Cui63481602014-12-01 17:41:04 -0800733
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800734TEST(signal, sigtimedwait64_SIGRTMIN) {
735 SignalMaskRestorer smr;
736
737 // Block SIGRTMIN.
738 sigset64_t just_SIGRTMIN;
739 sigemptyset64(&just_SIGRTMIN);
740 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
741 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
742
743 // Raise SIGALRM.
744 sigval_t sigval = { .sival_int = 1 };
745 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
746
747 // Get pending SIGALRM.
748 siginfo_t info;
749 timespec timeout = { .tv_sec = 2, .tv_nsec = 0 };
750 errno = 0;
751 ASSERT_EQ(SIGRTMIN, sigtimedwait64(&just_SIGRTMIN, &info, &timeout));
752 ASSERT_EQ(0, errno);
Yabin Cui63481602014-12-01 17:41:04 -0800753}
754
Yabin Cui63481602014-12-01 17:41:04 -0800755TEST(signal, sigtimedwait_timeout) {
756 // Block SIGALRM.
757 sigset_t just_SIGALRM;
758 sigemptyset(&just_SIGALRM);
759 sigaddset(&just_SIGALRM, SIGALRM);
760 sigset_t original_set;
761 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set));
762
763 // Wait timeout.
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700764 auto t0 = std::chrono::steady_clock::now();
Yabin Cui63481602014-12-01 17:41:04 -0800765 siginfo_t info;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800766 timespec timeout = { .tv_sec = 0, .tv_nsec = 1000000 };
Yabin Cui63481602014-12-01 17:41:04 -0800767 errno = 0;
768 ASSERT_EQ(-1, sigtimedwait(&just_SIGALRM, &info, &timeout));
769 ASSERT_EQ(EAGAIN, errno);
Elliott Hughesca3f8e42019-10-28 15:59:38 -0700770 auto t1 = std::chrono::steady_clock::now();
771 ASSERT_GE(t1-t0, 1000000ns);
Yabin Cui63481602014-12-01 17:41:04 -0800772
Yi Kong32bc0fc2018-08-02 17:31:13 -0700773 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &original_set, nullptr));
Yabin Cui63481602014-12-01 17:41:04 -0800774}
Josh Gao61cf3f32016-03-08 15:27:15 -0800775
776#if defined(__BIONIC__)
777TEST(signal, rt_tgsigqueueinfo) {
778 // Test whether rt_tgsigqueueinfo allows sending arbitrary si_code values to self.
779 // If this fails, your kernel needs commit 66dd34a to be backported.
780 static constexpr char error_msg[] =
781 "\nPlease ensure that the following kernel patch has been applied:\n"
782 "* https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=66dd34ad31e5963d72a700ec3f2449291d322921\n";
783 static siginfo received;
784
Elliott Hughes3e235912018-02-01 14:21:51 -0800785 struct sigaction handler = {};
Josh Gao61cf3f32016-03-08 15:27:15 -0800786 handler.sa_sigaction = [](int, siginfo_t* siginfo, void*) { received = *siginfo; };
787 handler.sa_flags = SA_SIGINFO;
788
789 ASSERT_EQ(0, sigaction(SIGUSR1, &handler, nullptr));
790
Josh Gaod7878522016-03-14 18:15:15 -0700791 siginfo sent;
792 memset(&sent, 0, sizeof(sent));
Josh Gao61cf3f32016-03-08 15:27:15 -0800793
794 sent.si_code = SI_TKILL;
795 ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent))
796 << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg;
797 ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected "
798 << sent.si_code << ", received " << received.si_code
799 << error_msg;
800
801 sent.si_code = SI_USER;
802 ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent))
803 << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg;
804 ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected "
805 << sent.si_code << ", received " << received.si_code
806 << error_msg;
807}
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800808#endif
Josh Gaoc244fcb2016-03-29 14:34:03 -0700809
Josh Gaoc244fcb2016-03-29 14:34:03 -0700810TEST(signal, sigset_size) {
811 // The setjmp implementations for ARM, AArch64, x86, and x86_64 assume that sigset_t can fit in a
812 // long. This is true because ARM and x86 have broken rt signal support, and AArch64 and x86_64
813 // both have a SIGRTMAX defined as 64.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800814#if defined(__arm__) || defined(__aarch64__) || defined(__i386__) || defined(__x86_64__)
815#if defined(__BIONIC__)
Josh Gaoc244fcb2016-03-29 14:34:03 -0700816 static_assert(sizeof(sigset_t) <= sizeof(long), "sigset_t doesn't fit in a long");
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800817#endif
818 static_assert(sizeof(sigset64_t)*8 >= 64, "sigset64_t too small for real-time signals");
819#endif
Josh Gaoc244fcb2016-03-29 14:34:03 -0700820}
821
Greg Hackmann5375bf62016-02-29 12:35:33 -0800822TEST(signal, sigignore_EINVAL) {
823 errno = 0;
824 ASSERT_EQ(-1, sigignore(99999));
825 ASSERT_EQ(EINVAL, errno);
826}
827
828TEST(signal, sigignore) {
829 errno = 0;
830 EXPECT_EQ(-1, sigignore(SIGKILL));
831 EXPECT_EQ(errno, EINVAL);
832
833 errno = 0;
834 EXPECT_EQ(-1, sigignore(SIGSTOP));
835 EXPECT_EQ(errno, EINVAL);
836
837 ScopedSignalHandler sigalrm{SIGALRM};
838 ASSERT_EQ(0, sigignore(SIGALRM));
839
840 struct sigaction sa;
841 ASSERT_EQ(0, sigaction(SIGALRM, nullptr, &sa));
842 EXPECT_EQ(SIG_IGN, sa.sa_handler);
843}
844
845TEST(signal, sighold_EINVAL) {
846 errno = 0;
847 ASSERT_EQ(-1, sighold(99999));
848 ASSERT_EQ(EINVAL, errno);
849}
850
851TEST(signal, sigpause_EINVAL) {
852 errno = 0;
853 ASSERT_EQ(-1, sigpause(99999));
854 ASSERT_EQ(EINVAL, errno);
855}
856
857TEST(signal, sigrelse_EINVAL) {
858 errno = 0;
859 ASSERT_EQ(-1, sigpause(99999));
860 ASSERT_EQ(EINVAL, errno);
861}
862
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800863static void TestSigholdSigpauseSigrelse(int sig) {
864 static int signal_handler_call_count = 0;
865 ScopedSignalHandler ssh{sig, [](int) { signal_handler_call_count++; }};
866 SignalMaskRestorer mask_restorer;
Greg Hackmann5375bf62016-02-29 12:35:33 -0800867 sigset_t set;
868
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800869 // sighold(SIGALRM/SIGRTMIN) should add SIGALRM/SIGRTMIN to the signal mask ...
870 ASSERT_EQ(0, sighold(sig));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700871 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800872 EXPECT_TRUE(sigismember(&set, sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800873
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800874 // ... preventing our SIGALRM/SIGRTMIN handler from running ...
875 raise(sig);
876 ASSERT_EQ(0, signal_handler_call_count);
877 // ... until sigpause(SIGALRM/SIGRTMIN) temporarily unblocks it.
878 ASSERT_EQ(-1, sigpause(sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800879 ASSERT_EQ(EINTR, errno);
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800880 ASSERT_EQ(1, signal_handler_call_count);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800881
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800882 if (sig >= SIGRTMIN && sizeof(void*) == 8) {
883 // But sigpause(SIGALRM/SIGRTMIN) shouldn't permanently unblock SIGALRM/SIGRTMIN.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700884 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800885 EXPECT_TRUE(sigismember(&set, sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800886
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800887 // Whereas sigrelse(SIGALRM/SIGRTMIN) should.
888 ASSERT_EQ(0, sigrelse(sig));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700889 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, nullptr, &set));
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800890 EXPECT_FALSE(sigismember(&set, sig));
891 } else {
892 // sigismember won't work for SIGRTMIN on LP32.
893 }
894}
895
896TEST(signal, sighold_sigpause_sigrelse) {
897 TestSigholdSigpauseSigrelse(SIGALRM);
898}
899
900TEST(signal, sighold_sigpause_sigrelse_RT) {
901 TestSigholdSigpauseSigrelse(SIGRTMIN);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800902}
903
904TEST(signal, sigset_EINVAL) {
905 errno = 0;
906 ASSERT_EQ(SIG_ERR, sigset(99999, SIG_DFL));
907 ASSERT_EQ(EINVAL, errno);
908}
909
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800910TEST(signal, sigset_RT) {
911 static int signal_handler_call_count = 0;
912 auto signal_handler = [](int) { signal_handler_call_count++; };
913 ScopedSignalHandler ssh{SIGRTMIN, signal_handler};
914 SignalMaskRestorer mask_restorer;
Greg Hackmann5375bf62016-02-29 12:35:33 -0800915
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800916 ASSERT_EQ(signal_handler, sigset(SIGRTMIN, SIG_HOLD));
917#if defined(__LP64__)
Greg Hackmann5375bf62016-02-29 12:35:33 -0800918 sigset_t set;
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800919 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
920 ASSERT_TRUE(sigismember(&set, SIGRTMIN));
921#endif
922
923 ASSERT_EQ(SIG_HOLD, sigset(SIGRTMIN, signal_handler));
924 ASSERT_EQ(signal_handler, sigset(SIGRTMIN, signal_handler));
925 ASSERT_EQ(0, signal_handler_call_count);
926 raise(SIGRTMIN);
927 ASSERT_EQ(1, signal_handler_call_count);
928}
929
930TEST(signal, sigset) {
931 static int signal_handler_call_count = 0;
932 auto signal_handler = [](int) { signal_handler_call_count++; };
933 ScopedSignalHandler ssh{SIGALRM, signal_handler};
934 SignalMaskRestorer mask_restorer;
935
936 ASSERT_EQ(0, signal_handler_call_count);
937 raise(SIGALRM);
938 ASSERT_EQ(1, signal_handler_call_count);
939
940 // Block SIGALRM so the next sigset(SIGARLM) call will return SIG_HOLD.
941 sigset_t set;
942 sigemptyset(&set);
943 sigaddset(&set, SIGALRM);
944 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &set, nullptr));
945
946 sigemptyset(&set);
947 ASSERT_EQ(SIG_HOLD, sigset(SIGALRM, signal_handler));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800948 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
949 EXPECT_FALSE(sigismember(&set, SIGALRM));
950
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800951 ASSERT_EQ(signal_handler, sigset(SIGALRM, SIG_IGN));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800952 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
953 EXPECT_FALSE(sigismember(&set, SIGALRM));
954
955 ASSERT_EQ(SIG_IGN, sigset(SIGALRM, SIG_DFL));
956 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
957 EXPECT_FALSE(sigismember(&set, SIGALRM));
958
959 ASSERT_EQ(SIG_DFL, sigset(SIGALRM, SIG_HOLD));
960 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
961 EXPECT_TRUE(sigismember(&set, SIGALRM));
962}
Elliott Hughes7532b322017-07-11 15:00:17 -0700963
964TEST(signal, killpg_EINVAL) {
965 // POSIX leaves pgrp <= 1 undefined, but glibc fails with EINVAL for < 0
966 // and passes 0 through to kill(2).
967 errno = 0;
968 ASSERT_EQ(-1, killpg(-1, SIGKILL));
969 ASSERT_EQ(EINVAL, errno);
970}