blob: d374c500e9622eadd7d16bf8a9b4102df147b456 [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 Hughes5905d6f2018-01-30 15:09:51 -080023#include <thread>
24
Elliott Hughesafe58ad2014-09-04 13:54:42 -070025#include <gtest/gtest.h>
Elliott Hughesda73f652012-11-30 16:40:55 -080026
Christopher Ferris13613132013-10-28 15:24:04 -070027#include "ScopedSignalHandler.h"
28
Elliott Hughes5905d6f2018-01-30 15:09:51 -080029static int SIGNAL_MIN() {
Elliott Hughes40d105c2013-10-16 12:53:58 -070030 return 1; // Signals start at 1 (SIGHUP), not 0.
31}
32
Elliott Hughes5905d6f2018-01-30 15:09:51 -080033template <typename SigSetT>
34static int SIGNAL_MAX(SigSetT* set) {
35 return sizeof(*set) * 8;
Elliott Hughes40d105c2013-10-16 12:53:58 -070036}
37
Elliott Hughes5905d6f2018-01-30 15:09:51 -080038template <typename SigSetT>
39static void TestSigSet1(int (fn)(SigSetT*)) {
40 // NULL sigset_t*/sigset64_t*.
41 SigSetT* set_ptr = NULL;
Elliott Hughesda73f652012-11-30 16:40:55 -080042 errno = 0;
43 ASSERT_EQ(-1, fn(set_ptr));
44 ASSERT_EQ(EINVAL, errno);
45
46 // Non-NULL.
Elliott Hughes5905d6f2018-01-30 15:09:51 -080047 SigSetT set = {};
Elliott Hughesda73f652012-11-30 16:40:55 -080048 errno = 0;
49 ASSERT_EQ(0, fn(&set));
50 ASSERT_EQ(0, errno);
51}
52
Elliott Hughes5905d6f2018-01-30 15:09:51 -080053template <typename SigSetT>
54static void TestSigSet2(int (fn)(SigSetT*, int)) {
55 // NULL sigset_t*/sigset64_t*.
56 SigSetT* set_ptr = NULL;
Elliott Hughesda73f652012-11-30 16:40:55 -080057 errno = 0;
58 ASSERT_EQ(-1, fn(set_ptr, SIGSEGV));
59 ASSERT_EQ(EINVAL, errno);
60
Elliott Hughes5905d6f2018-01-30 15:09:51 -080061 SigSetT set = {};
Elliott Hughesda73f652012-11-30 16:40:55 -080062
Elliott Hughesda73f652012-11-30 16:40:55 -080063 // Bad signal number: too small.
64 errno = 0;
65 ASSERT_EQ(-1, fn(&set, 0));
66 ASSERT_EQ(EINVAL, errno);
67
68 // Bad signal number: too high.
69 errno = 0;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080070 ASSERT_EQ(-1, fn(&set, SIGNAL_MAX(&set) + 1));
Elliott Hughesda73f652012-11-30 16:40:55 -080071 ASSERT_EQ(EINVAL, errno);
72
73 // Good signal numbers, low and high ends of range.
74 errno = 0;
Elliott Hughes40d105c2013-10-16 12:53:58 -070075 ASSERT_EQ(0, fn(&set, SIGNAL_MIN()));
Elliott Hughesda73f652012-11-30 16:40:55 -080076 ASSERT_EQ(0, errno);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080077 ASSERT_EQ(0, fn(&set, SIGNAL_MAX(&set)));
Elliott Hughesda73f652012-11-30 16:40:55 -080078 ASSERT_EQ(0, errno);
79}
80
Elliott Hughesda73f652012-11-30 16:40:55 -080081TEST(signal, sigaddset_invalid) {
82 TestSigSet2(sigaddset);
83}
84
Elliott Hughes5905d6f2018-01-30 15:09:51 -080085TEST(signal, sigaddset64_invalid) {
86#if defined(__BIONIC__)
87 TestSigSet2(sigaddset64);
88#endif
89}
90
Elliott Hughesda73f652012-11-30 16:40:55 -080091TEST(signal, sigdelset_invalid) {
92 TestSigSet2(sigdelset);
93}
94
Elliott Hughes5905d6f2018-01-30 15:09:51 -080095TEST(signal, sigdelset64_invalid) {
96#if defined(__BIONIC__)
97 TestSigSet2(sigdelset64);
98#endif
99}
100
Elliott Hughesda73f652012-11-30 16:40:55 -0800101TEST(signal, sigemptyset_invalid) {
102 TestSigSet1(sigemptyset);
103}
104
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800105TEST(signal, sigemptyset64_invalid) {
106#if defined(__BIONIC__)
107 TestSigSet1(sigemptyset64);
108#endif
109}
110
Elliott Hughesda73f652012-11-30 16:40:55 -0800111TEST(signal, sigfillset_invalid) {
112 TestSigSet1(sigfillset);
113}
Chris Dearmand8a5a6f2012-12-07 18:41:10 -0800114
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800115TEST(signal, sigfillset64_invalid) {
116#if defined(__BIONIC__)
117 TestSigSet1(sigfillset64);
118#endif
119}
120
121TEST(signal, sigismember_invalid) {
122 TestSigSet2(sigismember);
123}
124
125TEST(signal, sigismember64_invalid) {
126#if defined(__BIONIC__)
127 TestSigSet2(sigismember64);
128#endif
129}
130
Chris Dearmand8a5a6f2012-12-07 18:41:10 -0800131TEST(signal, raise_invalid) {
132 errno = 0;
133 ASSERT_EQ(-1, raise(-1));
134 ASSERT_EQ(EINVAL, errno);
135}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800136
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800137static void raise_in_signal_handler_helper(int signal_number) {
138 ASSERT_EQ(SIGALRM, signal_number);
139 static int count = 0;
140 if (++count == 1) {
141 raise(SIGALRM);
142 }
143}
144
145TEST(signal, raise_in_signal_handler) {
146 ScopedSignalHandler ssh(SIGALRM, raise_in_signal_handler_helper);
147 raise(SIGALRM);
148}
149
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800150TEST(signal, sigwait_SIGALRM) {
151 ScopedSignalHandler ssh(SIGALRM, [](int sig) { ASSERT_EQ(SIGALRM, sig); });
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800152
153 sigset_t wait_set;
154 sigemptyset(&wait_set);
155 sigaddset(&wait_set, SIGALRM);
156
157 alarm(1);
158
159 int received_signal;
160 errno = 0;
161 ASSERT_EQ(0, sigwait(&wait_set, &received_signal));
162 ASSERT_EQ(0, errno);
163 ASSERT_EQ(SIGALRM, received_signal);
164}
Elliott Hughes1f5af922013-10-15 18:01:56 -0700165
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800166TEST(signal, sigwait64_SIGRTMIN) {
167 ScopedSignalHandler ssh(SIGRTMIN, [](int sig) { ASSERT_EQ(SIGRTMIN, sig); });
Elliott Hughes1f5af922013-10-15 18:01:56 -0700168
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800169 sigset64_t wait_set;
170 sigemptyset64(&wait_set);
171 sigaddset64(&wait_set, SIGRTMIN);
172
173 pid_t pid = getpid();
174 std::thread thread([&pid]() {
175 usleep(5000);
176 kill(pid, SIGRTMIN);
177 });
178
179 int received_signal;
180 errno = 0;
181 ASSERT_EQ(0, sigwait64(&wait_set, &received_signal));
182 ASSERT_EQ(0, errno);
183 ASSERT_EQ(SIGRTMIN, received_signal);
184
185 thread.join();
Elliott Hughes1f5af922013-10-15 18:01:56 -0700186}
187
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800188static int g_sigsuspend_signal_handler_call_count = 0;
189
Elliott Hughes40d105c2013-10-16 12:53:58 -0700190TEST(signal, sigsuspend_sigpending) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800191 SignalMaskRestorer smr;
192
Elliott Hughes1f5af922013-10-15 18:01:56 -0700193 // Block SIGALRM.
194 sigset_t just_SIGALRM;
195 sigemptyset(&just_SIGALRM);
196 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800197 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Elliott Hughes1f5af922013-10-15 18:01:56 -0700198
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800199 ScopedSignalHandler ssh(SIGALRM, [](int) { ++g_sigsuspend_signal_handler_call_count; });
Elliott Hughes11952072013-10-24 15:15:14 -0700200
Elliott Hughes40d105c2013-10-16 12:53:58 -0700201 // There should be no pending signals.
202 sigset_t pending;
203 sigemptyset(&pending);
204 ASSERT_EQ(0, sigpending(&pending));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800205 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
Elliott Hughes40d105c2013-10-16 12:53:58 -0700206 EXPECT_FALSE(sigismember(&pending, i)) << i;
207 }
208
Elliott Hughes1f5af922013-10-15 18:01:56 -0700209 // Raise SIGALRM and check our signal handler wasn't called.
210 raise(SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800211 ASSERT_EQ(0, g_sigsuspend_signal_handler_call_count);
Elliott Hughes1f5af922013-10-15 18:01:56 -0700212
Elliott Hughes40d105c2013-10-16 12:53:58 -0700213 // We should now have a pending SIGALRM but nothing else.
214 sigemptyset(&pending);
215 ASSERT_EQ(0, sigpending(&pending));
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800216 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
Elliott Hughes40d105c2013-10-16 12:53:58 -0700217 EXPECT_EQ((i == SIGALRM), sigismember(&pending, i));
218 }
219
Elliott Hughes1f5af922013-10-15 18:01:56 -0700220 // Use sigsuspend to block everything except SIGALRM...
221 sigset_t not_SIGALRM;
222 sigfillset(&not_SIGALRM);
223 sigdelset(&not_SIGALRM, SIGALRM);
224 ASSERT_EQ(-1, sigsuspend(&not_SIGALRM));
225 ASSERT_EQ(EINTR, errno);
226 // ...and check that we now receive our pending SIGALRM.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800227 ASSERT_EQ(1, g_sigsuspend_signal_handler_call_count);
228}
Elliott Hughes1f5af922013-10-15 18:01:56 -0700229
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800230static int g_sigsuspend64_signal_handler_call_count = 0;
231
232TEST(signal, sigsuspend64_sigpending64) {
233 SignalMaskRestorer smr;
234
235 // Block SIGRTMIN.
236 sigset64_t just_SIGRTMIN;
237 sigemptyset64(&just_SIGRTMIN);
238 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
239 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
240
241 ScopedSignalHandler ssh(SIGRTMIN, [](int) { ++g_sigsuspend64_signal_handler_call_count; });
242
243 // There should be no pending signals.
244 sigset64_t pending;
245 sigemptyset64(&pending);
246 ASSERT_EQ(0, sigpending64(&pending));
247 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
248 EXPECT_FALSE(sigismember64(&pending, i)) << i;
249 }
250
251 // Raise SIGRTMIN and check our signal handler wasn't called.
252 raise(SIGRTMIN);
253 ASSERT_EQ(0, g_sigsuspend64_signal_handler_call_count);
254
255 // We should now have a pending SIGRTMIN but nothing else.
256 sigemptyset64(&pending);
257 ASSERT_EQ(0, sigpending64(&pending));
258 for (int i = SIGNAL_MIN(); i <= SIGNAL_MAX(&pending); ++i) {
259 EXPECT_EQ((i == SIGRTMIN), sigismember64(&pending, i));
260 }
261
262 // Use sigsuspend64 to block everything except SIGRTMIN...
263 sigset64_t not_SIGRTMIN;
264 sigfillset64(&not_SIGRTMIN);
265 sigdelset64(&not_SIGRTMIN, SIGRTMIN);
266 ASSERT_EQ(-1, sigsuspend64(&not_SIGRTMIN));
267 ASSERT_EQ(EINTR, errno);
268 // ...and check that we now receive our pending SIGRTMIN.
269 ASSERT_EQ(1, g_sigsuspend64_signal_handler_call_count);
Elliott Hughes1f5af922013-10-15 18:01:56 -0700270}
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700271
Elliott Hughes3e235912018-02-01 14:21:51 -0800272template <typename SigActionT, typename SigSetT>
273static void TestSigAction(int (sigaction_fn)(int, const SigActionT*, SigActionT*),
274 int (sigaddset_fn)(SigSetT*, int),
275 int sig) {
Elliott Hughesafe58ad2014-09-04 13:54:42 -0700276 // Both bionic and glibc set SA_RESTORER when talking to the kernel on arm,
277 // arm64, x86, and x86-64. The version of glibc we're using also doesn't
278 // define SA_RESTORER, but luckily it's the same value everywhere, and mips
279 // doesn't use the bit for anything.
Elliott Hughesaa13e832014-09-04 15:43:10 -0700280 static const unsigned sa_restorer = 0x4000000;
Elliott Hughesafe58ad2014-09-04 13:54:42 -0700281
Elliott Hughes3e235912018-02-01 14:21:51 -0800282 // See what's currently set for this signal.
283 SigActionT original_sa = {};
284 ASSERT_EQ(0, sigaction_fn(sig, NULL, &original_sa));
Elliott Hughes11952072013-10-24 15:15:14 -0700285 ASSERT_TRUE(original_sa.sa_handler == NULL);
286 ASSERT_TRUE(original_sa.sa_sigaction == NULL);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700287 ASSERT_EQ(0U, original_sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100288#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100289 ASSERT_EQ(bool(original_sa.sa_flags & sa_restorer), bool(original_sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100290#endif
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700291
292 // Set a traditional sa_handler signal handler.
Elliott Hughes3e235912018-02-01 14:21:51 -0800293 auto no_op_signal_handler = [](int) {};
294 SigActionT sa = {};
295 sigaddset_fn(&sa.sa_mask, sig);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700296 sa.sa_flags = SA_ONSTACK;
Elliott Hughes3e235912018-02-01 14:21:51 -0800297 sa.sa_handler = no_op_signal_handler;
298 ASSERT_EQ(0, sigaction_fn(sig, &sa, NULL));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700299
300 // Check that we can read it back.
Elliott Hughes3e235912018-02-01 14:21:51 -0800301 sa = {};
302 ASSERT_EQ(0, sigaction_fn(sig, NULL, &sa));
303 ASSERT_TRUE(sa.sa_handler == no_op_signal_handler);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700304 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700305 ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK), sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100306#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100307 ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100308#endif
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700309
310 // Set a new-style sa_sigaction signal handler.
Elliott Hughes3e235912018-02-01 14:21:51 -0800311 auto no_op_sigaction = [](int, siginfo_t*, void*) {};
312 sa = {};
313 sigaddset_fn(&sa.sa_mask, sig);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700314 sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
Elliott Hughes3e235912018-02-01 14:21:51 -0800315 sa.sa_sigaction = no_op_sigaction;
316 ASSERT_EQ(0, sigaction_fn(sig, &sa, NULL));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700317
318 // Check that we can read it back.
Elliott Hughes3e235912018-02-01 14:21:51 -0800319 sa = {};
320 ASSERT_EQ(0, sigaction_fn(sig, NULL, &sa));
321 ASSERT_TRUE(sa.sa_sigaction == no_op_sigaction);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700322 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
Elliott Hughesaa13e832014-09-04 15:43:10 -0700323 ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK | SA_SIGINFO), sa.sa_flags & ~sa_restorer);
Goran Jakovljevic37966692018-02-12 09:03:10 +0100324#ifdef SA_RESTORER
Evgeny Eltsin11f60762018-02-05 13:33:35 +0100325 ASSERT_EQ(bool(sa.sa_flags & sa_restorer), bool(sa.sa_restorer));
Goran Jakovljevic37966692018-02-12 09:03:10 +0100326#endif
Elliott Hughes11952072013-10-24 15:15:14 -0700327
328 // Put everything back how it was.
Elliott Hughes3e235912018-02-01 14:21:51 -0800329 ASSERT_EQ(0, sigaction_fn(sig, &original_sa, NULL));
330}
331
332TEST(signal, sigaction) {
333 TestSigAction(sigaction, sigaddset, SIGALRM);
334}
335
336TEST(signal, sigaction64_SIGRTMIN) {
337 TestSigAction(sigaction64, sigaddset64, SIGRTMIN);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700338}
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800339
340TEST(signal, sys_signame) {
Elliott Hughes671e2362014-02-12 19:04:27 -0800341#if defined(__BIONIC__)
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800342 ASSERT_TRUE(sys_signame[0] == NULL);
343 ASSERT_STREQ("HUP", sys_signame[SIGHUP]);
344#else
345 GTEST_LOG_(INFO) << "This test does nothing.\n";
346#endif
347}
348
349TEST(signal, sys_siglist) {
350 ASSERT_TRUE(sys_siglist[0] == NULL);
351 ASSERT_STREQ("Hangup", sys_siglist[SIGHUP]);
352}
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700353
354TEST(signal, limits) {
355 // This comes from the kernel.
356 ASSERT_EQ(32, __SIGRTMIN);
357
358 // We reserve a non-zero number at the bottom for ourselves.
359 ASSERT_GT(SIGRTMIN, __SIGRTMIN);
360
361 // MIPS has more signals than everyone else.
362#if defined(__mips__)
363 ASSERT_EQ(128, __SIGRTMAX);
364#else
365 ASSERT_EQ(64, __SIGRTMAX);
366#endif
367
368 // We don't currently reserve any at the top.
369 ASSERT_EQ(SIGRTMAX, __SIGRTMAX);
370}
Yabin Cui63481602014-12-01 17:41:04 -0800371
372static int g_sigqueue_signal_handler_call_count = 0;
373
374static void SigqueueSignalHandler(int signum, siginfo_t* info, void*) {
375 ASSERT_EQ(SIGALRM, signum);
376 ASSERT_EQ(SIGALRM, info->si_signo);
377 ASSERT_EQ(SI_QUEUE, info->si_code);
378 ASSERT_EQ(1, info->si_value.sival_int);
379 ++g_sigqueue_signal_handler_call_count;
380}
381
382TEST(signal, sigqueue) {
383 ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO);
384 sigval_t sigval;
385 sigval.sival_int = 1;
386 errno = 0;
387 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
388 ASSERT_EQ(0, errno);
389 ASSERT_EQ(1, g_sigqueue_signal_handler_call_count);
390}
391
392TEST(signal, sigwaitinfo) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800393 SignalMaskRestorer smr;
394
Yabin Cui63481602014-12-01 17:41:04 -0800395 // Block SIGALRM.
396 sigset_t just_SIGALRM;
397 sigemptyset(&just_SIGALRM);
398 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800399 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Yabin Cui63481602014-12-01 17:41:04 -0800400
401 // Raise SIGALRM.
402 sigval_t sigval;
403 sigval.sival_int = 1;
404 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
405
406 // Get pending SIGALRM.
407 siginfo_t info;
408 errno = 0;
409 ASSERT_EQ(SIGALRM, sigwaitinfo(&just_SIGALRM, &info));
410 ASSERT_EQ(0, errno);
411 ASSERT_EQ(SIGALRM, info.si_signo);
412 ASSERT_EQ(1, info.si_value.sival_int);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800413}
Yabin Cui63481602014-12-01 17:41:04 -0800414
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800415TEST(signal, sigwaitinfo64_SIGRTMIN) {
416 SignalMaskRestorer smr;
417
418 // Block SIGRTMIN.
419 sigset64_t just_SIGRTMIN;
420 sigemptyset64(&just_SIGRTMIN);
421 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
422 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
423
424 // Raise SIGRTMIN.
425 sigval_t sigval;
426 sigval.sival_int = 1;
427 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
428
429 // Get pending SIGRTMIN.
430 siginfo_t info;
431 errno = 0;
432 ASSERT_EQ(SIGRTMIN, sigwaitinfo64(&just_SIGRTMIN, &info));
433 ASSERT_EQ(0, errno);
434 ASSERT_EQ(SIGRTMIN, info.si_signo);
435 ASSERT_EQ(1, info.si_value.sival_int);
Yabin Cui63481602014-12-01 17:41:04 -0800436}
437
438TEST(signal, sigtimedwait) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800439 SignalMaskRestorer smr;
440
Yabin Cui63481602014-12-01 17:41:04 -0800441 // Block SIGALRM.
442 sigset_t just_SIGALRM;
443 sigemptyset(&just_SIGALRM);
444 sigaddset(&just_SIGALRM, SIGALRM);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800445 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, nullptr));
Yabin Cui63481602014-12-01 17:41:04 -0800446
447 // Raise SIGALRM.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800448 sigval_t sigval = { .sival_int = 1 };
Yabin Cui63481602014-12-01 17:41:04 -0800449 ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval));
450
451 // Get pending SIGALRM.
452 siginfo_t info;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800453 timespec timeout = { .tv_sec = 2, .tv_nsec = 0 };
Yabin Cui63481602014-12-01 17:41:04 -0800454 errno = 0;
455 ASSERT_EQ(SIGALRM, sigtimedwait(&just_SIGALRM, &info, &timeout));
456 ASSERT_EQ(0, errno);
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800457}
Yabin Cui63481602014-12-01 17:41:04 -0800458
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800459TEST(signal, sigtimedwait64_SIGRTMIN) {
460 SignalMaskRestorer smr;
461
462 // Block SIGRTMIN.
463 sigset64_t just_SIGRTMIN;
464 sigemptyset64(&just_SIGRTMIN);
465 sigaddset64(&just_SIGRTMIN, SIGRTMIN);
466 ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, &just_SIGRTMIN, nullptr));
467
468 // Raise SIGALRM.
469 sigval_t sigval = { .sival_int = 1 };
470 ASSERT_EQ(0, sigqueue(getpid(), SIGRTMIN, sigval));
471
472 // Get pending SIGALRM.
473 siginfo_t info;
474 timespec timeout = { .tv_sec = 2, .tv_nsec = 0 };
475 errno = 0;
476 ASSERT_EQ(SIGRTMIN, sigtimedwait64(&just_SIGRTMIN, &info, &timeout));
477 ASSERT_EQ(0, errno);
Yabin Cui63481602014-12-01 17:41:04 -0800478}
479
480static int64_t NanoTime() {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800481 timespec t;
Yabin Cui63481602014-12-01 17:41:04 -0800482 clock_gettime(CLOCK_MONOTONIC, &t);
483 return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec;
484}
485
486TEST(signal, sigtimedwait_timeout) {
487 // Block SIGALRM.
488 sigset_t just_SIGALRM;
489 sigemptyset(&just_SIGALRM);
490 sigaddset(&just_SIGALRM, SIGALRM);
491 sigset_t original_set;
492 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set));
493
494 // Wait timeout.
495 int64_t start_time = NanoTime();
496 siginfo_t info;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800497 timespec timeout = { .tv_sec = 0, .tv_nsec = 1000000 };
Yabin Cui63481602014-12-01 17:41:04 -0800498 errno = 0;
499 ASSERT_EQ(-1, sigtimedwait(&just_SIGALRM, &info, &timeout));
500 ASSERT_EQ(EAGAIN, errno);
501 ASSERT_GE(NanoTime() - start_time, 1000000);
502
503 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &original_set, NULL));
504}
Josh Gao61cf3f32016-03-08 15:27:15 -0800505
506#if defined(__BIONIC__)
507TEST(signal, rt_tgsigqueueinfo) {
508 // Test whether rt_tgsigqueueinfo allows sending arbitrary si_code values to self.
509 // If this fails, your kernel needs commit 66dd34a to be backported.
510 static constexpr char error_msg[] =
511 "\nPlease ensure that the following kernel patch has been applied:\n"
512 "* https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=66dd34ad31e5963d72a700ec3f2449291d322921\n";
513 static siginfo received;
514
Elliott Hughes3e235912018-02-01 14:21:51 -0800515 struct sigaction handler = {};
Josh Gao61cf3f32016-03-08 15:27:15 -0800516 handler.sa_sigaction = [](int, siginfo_t* siginfo, void*) { received = *siginfo; };
517 handler.sa_flags = SA_SIGINFO;
518
519 ASSERT_EQ(0, sigaction(SIGUSR1, &handler, nullptr));
520
Josh Gaod7878522016-03-14 18:15:15 -0700521 siginfo sent;
522 memset(&sent, 0, sizeof(sent));
Josh Gao61cf3f32016-03-08 15:27:15 -0800523
524 sent.si_code = SI_TKILL;
525 ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent))
526 << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg;
527 ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected "
528 << sent.si_code << ", received " << received.si_code
529 << error_msg;
530
531 sent.si_code = SI_USER;
532 ASSERT_EQ(0, syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), SIGUSR1, &sent))
533 << "rt_tgsigqueueinfo failed: " << strerror(errno) << error_msg;
534 ASSERT_EQ(sent.si_code, received.si_code) << "rt_tgsigqueueinfo modified si_code, expected "
535 << sent.si_code << ", received " << received.si_code
536 << error_msg;
537}
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800538#endif
Josh Gaoc244fcb2016-03-29 14:34:03 -0700539
Josh Gaoc244fcb2016-03-29 14:34:03 -0700540TEST(signal, sigset_size) {
541 // The setjmp implementations for ARM, AArch64, x86, and x86_64 assume that sigset_t can fit in a
542 // long. This is true because ARM and x86 have broken rt signal support, and AArch64 and x86_64
543 // both have a SIGRTMAX defined as 64.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800544#if defined(__arm__) || defined(__aarch64__) || defined(__i386__) || defined(__x86_64__)
545#if defined(__BIONIC__)
Josh Gaoc244fcb2016-03-29 14:34:03 -0700546 static_assert(sizeof(sigset_t) <= sizeof(long), "sigset_t doesn't fit in a long");
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800547#endif
548 static_assert(sizeof(sigset64_t)*8 >= 64, "sigset64_t too small for real-time signals");
549#endif
Josh Gaoc244fcb2016-03-29 14:34:03 -0700550}
551
Greg Hackmann5375bf62016-02-29 12:35:33 -0800552TEST(signal, sigignore_EINVAL) {
553 errno = 0;
554 ASSERT_EQ(-1, sigignore(99999));
555 ASSERT_EQ(EINVAL, errno);
556}
557
558TEST(signal, sigignore) {
559 errno = 0;
560 EXPECT_EQ(-1, sigignore(SIGKILL));
561 EXPECT_EQ(errno, EINVAL);
562
563 errno = 0;
564 EXPECT_EQ(-1, sigignore(SIGSTOP));
565 EXPECT_EQ(errno, EINVAL);
566
567 ScopedSignalHandler sigalrm{SIGALRM};
568 ASSERT_EQ(0, sigignore(SIGALRM));
569
570 struct sigaction sa;
571 ASSERT_EQ(0, sigaction(SIGALRM, nullptr, &sa));
572 EXPECT_EQ(SIG_IGN, sa.sa_handler);
573}
574
575TEST(signal, sighold_EINVAL) {
576 errno = 0;
577 ASSERT_EQ(-1, sighold(99999));
578 ASSERT_EQ(EINVAL, errno);
579}
580
581TEST(signal, sigpause_EINVAL) {
582 errno = 0;
583 ASSERT_EQ(-1, sigpause(99999));
584 ASSERT_EQ(EINVAL, errno);
585}
586
587TEST(signal, sigrelse_EINVAL) {
588 errno = 0;
589 ASSERT_EQ(-1, sigpause(99999));
590 ASSERT_EQ(EINVAL, errno);
591}
592
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800593static void TestSigholdSigpauseSigrelse(int sig) {
594 static int signal_handler_call_count = 0;
595 ScopedSignalHandler ssh{sig, [](int) { signal_handler_call_count++; }};
596 SignalMaskRestorer mask_restorer;
Greg Hackmann5375bf62016-02-29 12:35:33 -0800597 sigset_t set;
598
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800599 // sighold(SIGALRM/SIGRTMIN) should add SIGALRM/SIGRTMIN to the signal mask ...
600 ASSERT_EQ(0, sighold(sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800601 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, 0, &set));
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800602 EXPECT_TRUE(sigismember(&set, sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800603
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800604 // ... preventing our SIGALRM/SIGRTMIN handler from running ...
605 raise(sig);
606 ASSERT_EQ(0, signal_handler_call_count);
607 // ... until sigpause(SIGALRM/SIGRTMIN) temporarily unblocks it.
608 ASSERT_EQ(-1, sigpause(sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800609 ASSERT_EQ(EINTR, errno);
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800610 ASSERT_EQ(1, signal_handler_call_count);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800611
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800612 if (sig >= SIGRTMIN && sizeof(void*) == 8) {
613 // But sigpause(SIGALRM/SIGRTMIN) shouldn't permanently unblock SIGALRM/SIGRTMIN.
614 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, 0, &set));
615 EXPECT_TRUE(sigismember(&set, sig));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800616
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800617 // Whereas sigrelse(SIGALRM/SIGRTMIN) should.
618 ASSERT_EQ(0, sigrelse(sig));
619 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, 0, &set));
620 EXPECT_FALSE(sigismember(&set, sig));
621 } else {
622 // sigismember won't work for SIGRTMIN on LP32.
623 }
624}
625
626TEST(signal, sighold_sigpause_sigrelse) {
627 TestSigholdSigpauseSigrelse(SIGALRM);
628}
629
630TEST(signal, sighold_sigpause_sigrelse_RT) {
631 TestSigholdSigpauseSigrelse(SIGRTMIN);
Greg Hackmann5375bf62016-02-29 12:35:33 -0800632}
633
634TEST(signal, sigset_EINVAL) {
635 errno = 0;
636 ASSERT_EQ(SIG_ERR, sigset(99999, SIG_DFL));
637 ASSERT_EQ(EINVAL, errno);
638}
639
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800640TEST(signal, sigset_RT) {
641 static int signal_handler_call_count = 0;
642 auto signal_handler = [](int) { signal_handler_call_count++; };
643 ScopedSignalHandler ssh{SIGRTMIN, signal_handler};
644 SignalMaskRestorer mask_restorer;
Greg Hackmann5375bf62016-02-29 12:35:33 -0800645
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800646 ASSERT_EQ(signal_handler, sigset(SIGRTMIN, SIG_HOLD));
647#if defined(__LP64__)
Greg Hackmann5375bf62016-02-29 12:35:33 -0800648 sigset_t set;
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800649 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
650 ASSERT_TRUE(sigismember(&set, SIGRTMIN));
651#endif
652
653 ASSERT_EQ(SIG_HOLD, sigset(SIGRTMIN, signal_handler));
654 ASSERT_EQ(signal_handler, sigset(SIGRTMIN, signal_handler));
655 ASSERT_EQ(0, signal_handler_call_count);
656 raise(SIGRTMIN);
657 ASSERT_EQ(1, signal_handler_call_count);
658}
659
660TEST(signal, sigset) {
661 static int signal_handler_call_count = 0;
662 auto signal_handler = [](int) { signal_handler_call_count++; };
663 ScopedSignalHandler ssh{SIGALRM, signal_handler};
664 SignalMaskRestorer mask_restorer;
665
666 ASSERT_EQ(0, signal_handler_call_count);
667 raise(SIGALRM);
668 ASSERT_EQ(1, signal_handler_call_count);
669
670 // Block SIGALRM so the next sigset(SIGARLM) call will return SIG_HOLD.
671 sigset_t set;
672 sigemptyset(&set);
673 sigaddset(&set, SIGALRM);
674 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &set, nullptr));
675
676 sigemptyset(&set);
677 ASSERT_EQ(SIG_HOLD, sigset(SIGALRM, signal_handler));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800678 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
679 EXPECT_FALSE(sigismember(&set, SIGALRM));
680
Elliott Hughes4b1c6e72018-01-24 18:54:38 -0800681 ASSERT_EQ(signal_handler, sigset(SIGALRM, SIG_IGN));
Greg Hackmann5375bf62016-02-29 12:35:33 -0800682 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
683 EXPECT_FALSE(sigismember(&set, SIGALRM));
684
685 ASSERT_EQ(SIG_IGN, sigset(SIGALRM, SIG_DFL));
686 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
687 EXPECT_FALSE(sigismember(&set, SIGALRM));
688
689 ASSERT_EQ(SIG_DFL, sigset(SIGALRM, SIG_HOLD));
690 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &set));
691 EXPECT_TRUE(sigismember(&set, SIGALRM));
692}
Elliott Hughes7532b322017-07-11 15:00:17 -0700693
694TEST(signal, killpg_EINVAL) {
695 // POSIX leaves pgrp <= 1 undefined, but glibc fails with EINVAL for < 0
696 // and passes 0 through to kill(2).
697 errno = 0;
698 ASSERT_EQ(-1, killpg(-1, SIGKILL));
699 ASSERT_EQ(EINVAL, errno);
700}