blob: 89b80883849cadd91893bd4e5c27ebe1ccbddb58 [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
17#include <gtest/gtest.h>
18
19#include <errno.h>
20#include <signal.h>
21
Christopher Ferris13613132013-10-28 15:24:04 -070022#include "ScopedSignalHandler.h"
23
Elliott Hughes40d105c2013-10-16 12:53:58 -070024static size_t SIGNAL_MIN() {
25 return 1; // Signals start at 1 (SIGHUP), not 0.
26}
27
28static size_t SIGNAL_MAX() {
29 size_t result = SIGRTMAX;
30
31#if defined(__BIONIC__) && !defined(__mips__) && !defined(__LP64__)
32 // 32-bit bionic's sigset_t is too small for ARM and x86: 32 bits instead of 64.
33 // This means you can't refer to any of the real-time signals.
34 // See http://b/3038348 and http://b/5828899.
35 result = 32;
36#else
37 // Otherwise, C libraries should be perfectly capable of using their largest signal.
38 if (sizeof(sigset_t) * 8 < static_cast<size_t>(SIGRTMAX)) {
39 abort();
40 }
41#endif
42
43 return result;
44}
45
Elliott Hughesda73f652012-11-30 16:40:55 -080046template <typename Fn>
47static void TestSigSet1(Fn fn) {
48 // NULL sigset_t*.
49 sigset_t* set_ptr = NULL;
50 errno = 0;
51 ASSERT_EQ(-1, fn(set_ptr));
52 ASSERT_EQ(EINVAL, errno);
53
54 // Non-NULL.
55 sigset_t set;
56 errno = 0;
57 ASSERT_EQ(0, fn(&set));
58 ASSERT_EQ(0, errno);
59}
60
61template <typename Fn>
62static void TestSigSet2(Fn fn) {
63 // NULL sigset_t*.
64 sigset_t* set_ptr = NULL;
65 errno = 0;
66 ASSERT_EQ(-1, fn(set_ptr, SIGSEGV));
67 ASSERT_EQ(EINVAL, errno);
68
69 sigset_t set;
70 sigemptyset(&set);
71
Elliott Hughesda73f652012-11-30 16:40:55 -080072 // Bad signal number: too small.
73 errno = 0;
74 ASSERT_EQ(-1, fn(&set, 0));
75 ASSERT_EQ(EINVAL, errno);
76
77 // Bad signal number: too high.
78 errno = 0;
Elliott Hughes40d105c2013-10-16 12:53:58 -070079 ASSERT_EQ(-1, fn(&set, SIGNAL_MAX() + 1));
Elliott Hughesda73f652012-11-30 16:40:55 -080080 ASSERT_EQ(EINVAL, errno);
81
82 // Good signal numbers, low and high ends of range.
83 errno = 0;
Elliott Hughes40d105c2013-10-16 12:53:58 -070084 ASSERT_EQ(0, fn(&set, SIGNAL_MIN()));
Elliott Hughesda73f652012-11-30 16:40:55 -080085 ASSERT_EQ(0, errno);
Elliott Hughes40d105c2013-10-16 12:53:58 -070086 ASSERT_EQ(0, fn(&set, SIGNAL_MAX()));
Elliott Hughesda73f652012-11-30 16:40:55 -080087 ASSERT_EQ(0, errno);
88}
89
90TEST(signal, sigismember_invalid) {
91 TestSigSet2(sigismember);
92}
93
94TEST(signal, sigaddset_invalid) {
95 TestSigSet2(sigaddset);
96}
97
98TEST(signal, sigdelset_invalid) {
99 TestSigSet2(sigdelset);
100}
101
102TEST(signal, sigemptyset_invalid) {
103 TestSigSet1(sigemptyset);
104}
105
106TEST(signal, sigfillset_invalid) {
107 TestSigSet1(sigfillset);
108}
Chris Dearmand8a5a6f2012-12-07 18:41:10 -0800109
110TEST(signal, raise_invalid) {
111 errno = 0;
112 ASSERT_EQ(-1, raise(-1));
113 ASSERT_EQ(EINVAL, errno);
114}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800115
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800116static void raise_in_signal_handler_helper(int signal_number) {
117 ASSERT_EQ(SIGALRM, signal_number);
118 static int count = 0;
119 if (++count == 1) {
120 raise(SIGALRM);
121 }
122}
123
124TEST(signal, raise_in_signal_handler) {
125 ScopedSignalHandler ssh(SIGALRM, raise_in_signal_handler_helper);
126 raise(SIGALRM);
127}
128
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800129static void HandleSIGALRM(int signal_number) {
130 ASSERT_EQ(SIGALRM, signal_number);
131}
132
133TEST(signal, sigwait) {
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800134 ScopedSignalHandler ssh(SIGALRM, HandleSIGALRM);
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800135
136 sigset_t wait_set;
137 sigemptyset(&wait_set);
138 sigaddset(&wait_set, SIGALRM);
139
140 alarm(1);
141
142 int received_signal;
143 errno = 0;
144 ASSERT_EQ(0, sigwait(&wait_set, &received_signal));
145 ASSERT_EQ(0, errno);
146 ASSERT_EQ(SIGALRM, received_signal);
147}
Elliott Hughes1f5af922013-10-15 18:01:56 -0700148
Elliott Hughes1728b232014-05-14 10:02:03 -0700149static int g_sigsuspend_test_helper_call_count = 0;
Elliott Hughes1f5af922013-10-15 18:01:56 -0700150
151static void SigSuspendTestHelper(int) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700152 ++g_sigsuspend_test_helper_call_count;
Elliott Hughes1f5af922013-10-15 18:01:56 -0700153}
154
Elliott Hughes40d105c2013-10-16 12:53:58 -0700155TEST(signal, sigsuspend_sigpending) {
Elliott Hughes1f5af922013-10-15 18:01:56 -0700156 // Block SIGALRM.
157 sigset_t just_SIGALRM;
158 sigemptyset(&just_SIGALRM);
159 sigaddset(&just_SIGALRM, SIGALRM);
160 sigset_t original_set;
161 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set));
162
Elliott Hughes11952072013-10-24 15:15:14 -0700163 ScopedSignalHandler ssh(SIGALRM, SigSuspendTestHelper);
164
Elliott Hughes40d105c2013-10-16 12:53:58 -0700165 // There should be no pending signals.
166 sigset_t pending;
167 sigemptyset(&pending);
168 ASSERT_EQ(0, sigpending(&pending));
169 for (size_t i = SIGNAL_MIN(); i <= SIGNAL_MAX(); ++i) {
170 EXPECT_FALSE(sigismember(&pending, i)) << i;
171 }
172
Elliott Hughes1f5af922013-10-15 18:01:56 -0700173 // Raise SIGALRM and check our signal handler wasn't called.
174 raise(SIGALRM);
Elliott Hughes1728b232014-05-14 10:02:03 -0700175 ASSERT_EQ(0, g_sigsuspend_test_helper_call_count);
Elliott Hughes1f5af922013-10-15 18:01:56 -0700176
Elliott Hughes40d105c2013-10-16 12:53:58 -0700177 // We should now have a pending SIGALRM but nothing else.
178 sigemptyset(&pending);
179 ASSERT_EQ(0, sigpending(&pending));
180 for (size_t i = SIGNAL_MIN(); i <= SIGNAL_MAX(); ++i) {
181 EXPECT_EQ((i == SIGALRM), sigismember(&pending, i));
182 }
183
Elliott Hughes1f5af922013-10-15 18:01:56 -0700184 // Use sigsuspend to block everything except SIGALRM...
185 sigset_t not_SIGALRM;
186 sigfillset(&not_SIGALRM);
187 sigdelset(&not_SIGALRM, SIGALRM);
188 ASSERT_EQ(-1, sigsuspend(&not_SIGALRM));
189 ASSERT_EQ(EINTR, errno);
190 // ...and check that we now receive our pending SIGALRM.
Elliott Hughes1728b232014-05-14 10:02:03 -0700191 ASSERT_EQ(1, g_sigsuspend_test_helper_call_count);
Elliott Hughes1f5af922013-10-15 18:01:56 -0700192
193 // Restore the original set.
Elliott Hughes11952072013-10-24 15:15:14 -0700194 ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughes1f5af922013-10-15 18:01:56 -0700195}
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700196
197static void EmptySignalHandler(int) {}
198static void EmptySignalAction(int, siginfo_t*, void*) {}
199
200TEST(signal, sigaction) {
201 // See what's currently set for SIGALRM.
Elliott Hughes11952072013-10-24 15:15:14 -0700202 struct sigaction original_sa;
203 memset(&original_sa, 0, sizeof(original_sa));
204 ASSERT_EQ(0, sigaction(SIGALRM, NULL, &original_sa));
205 ASSERT_TRUE(original_sa.sa_handler == NULL);
206 ASSERT_TRUE(original_sa.sa_sigaction == NULL);
207 ASSERT_TRUE(original_sa.sa_flags == 0);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700208
209 // Set a traditional sa_handler signal handler.
Elliott Hughes11952072013-10-24 15:15:14 -0700210 struct sigaction sa;
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700211 memset(&sa, 0, sizeof(sa));
212 sigaddset(&sa.sa_mask, SIGALRM);
213 sa.sa_flags = SA_ONSTACK;
214 sa.sa_handler = EmptySignalHandler;
215 ASSERT_EQ(0, sigaction(SIGALRM, &sa, NULL));
216
217 // Check that we can read it back.
218 memset(&sa, 0, sizeof(sa));
219 ASSERT_EQ(0, sigaction(SIGALRM, NULL, &sa));
220 ASSERT_TRUE(sa.sa_handler == EmptySignalHandler);
221 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
222 ASSERT_TRUE(sa.sa_flags == SA_ONSTACK);
223
224 // Set a new-style sa_sigaction signal handler.
225 memset(&sa, 0, sizeof(sa));
226 sigaddset(&sa.sa_mask, SIGALRM);
227 sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
228 sa.sa_sigaction = EmptySignalAction;
229 ASSERT_EQ(0, sigaction(SIGALRM, &sa, NULL));
230
231 // Check that we can read it back.
232 memset(&sa, 0, sizeof(sa));
233 ASSERT_EQ(0, sigaction(SIGALRM, NULL, &sa));
234 ASSERT_TRUE(sa.sa_sigaction == EmptySignalAction);
235 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
236 ASSERT_TRUE(sa.sa_flags == (SA_ONSTACK | SA_SIGINFO));
Elliott Hughes11952072013-10-24 15:15:14 -0700237
238 // Put everything back how it was.
239 ASSERT_EQ(0, sigaction(SIGALRM, &original_sa, NULL));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700240}
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800241
242TEST(signal, sys_signame) {
Elliott Hughes671e2362014-02-12 19:04:27 -0800243#if defined(__BIONIC__)
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800244 ASSERT_TRUE(sys_signame[0] == NULL);
245 ASSERT_STREQ("HUP", sys_signame[SIGHUP]);
246#else
247 GTEST_LOG_(INFO) << "This test does nothing.\n";
248#endif
249}
250
251TEST(signal, sys_siglist) {
252 ASSERT_TRUE(sys_siglist[0] == NULL);
253 ASSERT_STREQ("Hangup", sys_siglist[SIGHUP]);
254}
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700255
256TEST(signal, limits) {
257 // This comes from the kernel.
258 ASSERT_EQ(32, __SIGRTMIN);
259
260 // We reserve a non-zero number at the bottom for ourselves.
261 ASSERT_GT(SIGRTMIN, __SIGRTMIN);
262
263 // MIPS has more signals than everyone else.
264#if defined(__mips__)
265 ASSERT_EQ(128, __SIGRTMAX);
266#else
267 ASSERT_EQ(64, __SIGRTMAX);
268#endif
269
270 // We don't currently reserve any at the top.
271 ASSERT_EQ(SIGRTMAX, __SIGRTMAX);
272}