1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <errno.h>
#include <signal.h>
template <typename Fn>
static void TestSigSet1(Fn fn) {
// NULL sigset_t*.
sigset_t* set_ptr = NULL;
errno = 0;
ASSERT_EQ(-1, fn(set_ptr));
ASSERT_EQ(EINVAL, errno);
// Non-NULL.
sigset_t set;
errno = 0;
ASSERT_EQ(0, fn(&set));
ASSERT_EQ(0, errno);
}
template <typename Fn>
static void TestSigSet2(Fn fn) {
// NULL sigset_t*.
sigset_t* set_ptr = NULL;
errno = 0;
ASSERT_EQ(-1, fn(set_ptr, SIGSEGV));
ASSERT_EQ(EINVAL, errno);
sigset_t set;
sigemptyset(&set);
int min_signal = SIGHUP;
int max_signal = SIGRTMAX;
#if defined(__BIONIC__) && !defined(__mips__)
// bionic's sigset_t is too small for ARM and x86: 32 bits instead of 64.
// This means you can't refer to any of the real-time signals.
// See http://b/3038348 and http://b/5828899.
max_signal = 32;
#else
// Other C libraries (or bionic for MIPS) are perfectly capable of using their largest signal.
ASSERT_GE(sizeof(sigset_t) * 8, static_cast<size_t>(SIGRTMAX));
#endif
// Bad signal number: too small.
errno = 0;
ASSERT_EQ(-1, fn(&set, 0));
ASSERT_EQ(EINVAL, errno);
// Bad signal number: too high.
errno = 0;
ASSERT_EQ(-1, fn(&set, max_signal + 1));
ASSERT_EQ(EINVAL, errno);
// Good signal numbers, low and high ends of range.
errno = 0;
ASSERT_EQ(0, fn(&set, min_signal));
ASSERT_EQ(0, errno);
ASSERT_EQ(0, fn(&set, max_signal));
ASSERT_EQ(0, errno);
}
class ScopedSignalHandler {
public:
ScopedSignalHandler(int signal_number, void (*handler)(int)) : signal_number_(signal_number) {
sigemptyset(&action_.sa_mask);
action_.sa_flags = 0;
action_.sa_handler = handler;
sigaction(signal_number_, &action_, &old_action_);
}
~ScopedSignalHandler() {
sigaction(signal_number_, &old_action_, NULL);
}
private:
struct sigaction action_;
struct sigaction old_action_;
const int signal_number_;
};
TEST(signal, sigismember_invalid) {
TestSigSet2(sigismember);
}
TEST(signal, sigaddset_invalid) {
TestSigSet2(sigaddset);
}
TEST(signal, sigdelset_invalid) {
TestSigSet2(sigdelset);
}
TEST(signal, sigemptyset_invalid) {
TestSigSet1(sigemptyset);
}
TEST(signal, sigfillset_invalid) {
TestSigSet1(sigfillset);
}
TEST(signal, raise_invalid) {
errno = 0;
ASSERT_EQ(-1, raise(-1));
ASSERT_EQ(EINVAL, errno);
}
static void raise_in_signal_handler_helper(int signal_number) {
ASSERT_EQ(SIGALRM, signal_number);
static int count = 0;
if (++count == 1) {
raise(SIGALRM);
}
}
TEST(signal, raise_in_signal_handler) {
ScopedSignalHandler ssh(SIGALRM, raise_in_signal_handler_helper);
raise(SIGALRM);
}
static void HandleSIGALRM(int signal_number) {
ASSERT_EQ(SIGALRM, signal_number);
}
TEST(signal, sigwait) {
ScopedSignalHandler ssh(SIGALRM, HandleSIGALRM);
sigset_t wait_set;
sigemptyset(&wait_set);
sigaddset(&wait_set, SIGALRM);
alarm(1);
int received_signal;
errno = 0;
ASSERT_EQ(0, sigwait(&wait_set, &received_signal));
ASSERT_EQ(0, errno);
ASSERT_EQ(SIGALRM, received_signal);
}
static int gSigSuspendTestHelperCallCount = 0;
static void SigSuspendTestHelper(int) {
++gSigSuspendTestHelperCallCount;
}
TEST(signal, sigsuspend) {
ScopedSignalHandler ssh(SIGALRM, SigSuspendTestHelper);
// Block SIGALRM.
sigset_t just_SIGALRM;
sigemptyset(&just_SIGALRM);
sigaddset(&just_SIGALRM, SIGALRM);
sigset_t original_set;
ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set));
// Raise SIGALRM and check our signal handler wasn't called.
raise(SIGALRM);
ASSERT_EQ(0, gSigSuspendTestHelperCallCount);
// Use sigsuspend to block everything except SIGALRM...
sigset_t not_SIGALRM;
sigfillset(¬_SIGALRM);
sigdelset(¬_SIGALRM, SIGALRM);
ASSERT_EQ(-1, sigsuspend(¬_SIGALRM));
ASSERT_EQ(EINTR, errno);
// ...and check that we now receive our pending SIGALRM.
ASSERT_EQ(1, gSigSuspendTestHelperCallCount);
// Restore the original set.
assert(0 == sigprocmask(SIG_SETMASK, &original_set, NULL));
}
|