blob: 8fd8b72f7128dce4f6675c91385086e13e22cd0d [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
Elliott Hughes28ea2292014-09-04 13:54:42 -070017#include <signal.h>
Elliott Hughesda73f652012-11-30 16:40:55 -080018
19#include <errno.h>
Elliott Hughes28ea2292014-09-04 13:54:42 -070020#include <gtest/gtest.h>
Elliott Hughesda73f652012-11-30 16:40:55 -080021
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) {
Elliott Hughes28ea2292014-09-04 13:54:42 -0700201 // Both bionic and glibc set SA_RESTORER when talking to the kernel on arm,
202 // arm64, x86, and x86-64. The version of glibc we're using also doesn't
203 // define SA_RESTORER, but luckily it's the same value everywhere, and mips
204 // doesn't use the bit for anything.
Elliott Hughes22e2c9d2014-09-04 15:43:10 -0700205 static const unsigned sa_restorer = 0x4000000;
Elliott Hughes28ea2292014-09-04 13:54:42 -0700206
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700207 // See what's currently set for SIGALRM.
Elliott Hughes11952072013-10-24 15:15:14 -0700208 struct sigaction original_sa;
209 memset(&original_sa, 0, sizeof(original_sa));
210 ASSERT_EQ(0, sigaction(SIGALRM, NULL, &original_sa));
211 ASSERT_TRUE(original_sa.sa_handler == NULL);
212 ASSERT_TRUE(original_sa.sa_sigaction == NULL);
Elliott Hughes22e2c9d2014-09-04 15:43:10 -0700213 ASSERT_EQ(0U, original_sa.sa_flags & ~sa_restorer);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700214
215 // Set a traditional sa_handler signal handler.
Elliott Hughes11952072013-10-24 15:15:14 -0700216 struct sigaction sa;
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700217 memset(&sa, 0, sizeof(sa));
218 sigaddset(&sa.sa_mask, SIGALRM);
219 sa.sa_flags = SA_ONSTACK;
220 sa.sa_handler = EmptySignalHandler;
221 ASSERT_EQ(0, sigaction(SIGALRM, &sa, NULL));
222
223 // Check that we can read it back.
224 memset(&sa, 0, sizeof(sa));
225 ASSERT_EQ(0, sigaction(SIGALRM, NULL, &sa));
226 ASSERT_TRUE(sa.sa_handler == EmptySignalHandler);
227 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
Elliott Hughes22e2c9d2014-09-04 15:43:10 -0700228 ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK), sa.sa_flags & ~sa_restorer);
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700229
230 // Set a new-style sa_sigaction signal handler.
231 memset(&sa, 0, sizeof(sa));
232 sigaddset(&sa.sa_mask, SIGALRM);
233 sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
234 sa.sa_sigaction = EmptySignalAction;
235 ASSERT_EQ(0, sigaction(SIGALRM, &sa, NULL));
236
237 // Check that we can read it back.
238 memset(&sa, 0, sizeof(sa));
239 ASSERT_EQ(0, sigaction(SIGALRM, NULL, &sa));
240 ASSERT_TRUE(sa.sa_sigaction == EmptySignalAction);
241 ASSERT_TRUE((void*) sa.sa_sigaction == (void*) sa.sa_handler);
Elliott Hughes22e2c9d2014-09-04 15:43:10 -0700242 ASSERT_EQ(static_cast<unsigned>(SA_ONSTACK | SA_SIGINFO), sa.sa_flags & ~sa_restorer);
Elliott Hughes11952072013-10-24 15:15:14 -0700243
244 // Put everything back how it was.
245 ASSERT_EQ(0, sigaction(SIGALRM, &original_sa, NULL));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700246}
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800247
248TEST(signal, sys_signame) {
Elliott Hughes671e2362014-02-12 19:04:27 -0800249#if defined(__BIONIC__)
Elliott Hughesaa0ebda2014-02-11 19:57:06 -0800250 ASSERT_TRUE(sys_signame[0] == NULL);
251 ASSERT_STREQ("HUP", sys_signame[SIGHUP]);
252#else
253 GTEST_LOG_(INFO) << "This test does nothing.\n";
254#endif
255}
256
257TEST(signal, sys_siglist) {
258 ASSERT_TRUE(sys_siglist[0] == NULL);
259 ASSERT_STREQ("Hangup", sys_siglist[SIGHUP]);
260}
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700261
262TEST(signal, limits) {
263 // This comes from the kernel.
264 ASSERT_EQ(32, __SIGRTMIN);
265
266 // We reserve a non-zero number at the bottom for ourselves.
267 ASSERT_GT(SIGRTMIN, __SIGRTMIN);
268
269 // MIPS has more signals than everyone else.
270#if defined(__mips__)
271 ASSERT_EQ(128, __SIGRTMAX);
272#else
273 ASSERT_EQ(64, __SIGRTMAX);
274#endif
275
276 // We don't currently reserve any at the top.
277 ASSERT_EQ(SIGRTMAX, __SIGRTMAX);
278}