1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <sigchain.h>
17 #include <signal.h>
18 #include <wchar.h>
19 #include <stdlib.h>
20 #include "test.h"
21 #include "functionalext.h"
22 #include "sigchain_util.h"
23
24 static int g_count = 0;
25
26 /**
27 * @brief the special handler
28 */
sigchain_special_handler2(int signo,siginfo_t * siginfo,void * ucontext_raw)29 static bool sigchain_special_handler2(int signo, siginfo_t *siginfo, void *ucontext_raw)
30 {
31 g_count++;
32 EXPECT_EQ("sigchain_intercept_pthread_sigmask_001", signo, SIGUSR2);
33 return false;
34 }
35
36 /**
37 * @brief the signal handler
38 */
signal_handler2(int signo)39 static void signal_handler2(int signo)
40 {
41 g_count++;
42 EXPECT_EQ("sigchain_intercept_pthread_sigmask_001", signo, SIGUSR2);
43 }
44
45 /**
46 * @tc.name : sigchain_intercept_pthread_sigmask_001
47 * @tc.desc : The signal is registered with the special handler, test the influence of sigchain
48 * on pthread_sigmask.
49 * @tc.level : Level 0
50 */
sigchain_intercept_pthread_sigmask_001()51 static void sigchain_intercept_pthread_sigmask_001()
52 {
53 struct sigaction siga2 = {
54 .sa_handler = signal_handler2,
55 };
56 sigaction(SIGUSR2, &siga2, NULL);
57
58 struct signal_chain_action sigusr2 = {
59 .sca_sigaction = sigchain_special_handler2,
60 .sca_mask = {},
61 .sca_flags = 0,
62 };
63 add_special_signal_handler(SIGUSR2, &sigusr2);
64
65 sigset_t set = {0};
66 int signo[SIGCHIAN_TEST_SIGNAL_NUM_1] = {SIGUSR2};
67 SIGCHAIN_TEST_SET_MASK(set, "sigchain_intercept_pthread_sigmask_001", signo, SIGCHIAN_TEST_SIGNAL_NUM_1);
68 raise(SIGUSR2);
69 if (get_sigchain_mask_enable()) {
70 EXPECT_EQ("sigchain_intercept_pthread_sigmask_001", g_count, SIGCHIAN_TEST_SIGNAL_NUM_2);
71 } else {
72 EXPECT_EQ("sigchain_intercept_pthread_sigmask_001", g_count, 0);
73 }
74 }
75
76 /**
77 * @brief the special handler
78 */
sigchain_special_handler3(int signo,siginfo_t * siginfo,void * ucontext_raw)79 static bool sigchain_special_handler3(int signo, siginfo_t *siginfo, void *ucontext_raw)
80 {
81 g_count++;
82 EXPECT_FALSE("sigchain_intercept_pthread_sigmask_002", true);
83 return false;
84 }
85
86 /**
87 * @brief the signal handler
88 */
signal_handler3(int signo)89 static void signal_handler3(int signo)
90 {
91 g_count++;
92 EXPECT_EQ("sigchain_intercept_pthread_sigmask_002", signo, SIGPWR);
93 }
94
95 /**
96 * @tc.name : sigchain_intercept_pthread_sigmask_002
97 * @tc.desc : The signals are registered with the special handler, and remove the special handler.
98 * Test the influence of sigchain on pthread_sigmask.
99 * @tc.level : Level 0
100 */
sigchain_intercept_pthread_sigmask_002()101 static void sigchain_intercept_pthread_sigmask_002()
102 {
103 g_count = 0;
104 struct sigaction siga2 = {
105 .sa_handler = signal_handler3,
106 };
107 sigaction(SIGPWR, &siga2, NULL);
108
109 struct signal_chain_action sigsegv = {
110 .sca_sigaction = sigchain_special_handler3,
111 .sca_mask = {},
112 .sca_flags = 0,
113 };
114 add_special_signal_handler(SIGPWR, &sigsegv);
115
116 sigset_t set = {0};
117 int signo[SIGCHIAN_TEST_SIGNAL_NUM_1] = {SIGPWR};
118 SIGCHAIN_TEST_SET_MASK(set, "sigchain_intercept_pthread_sigmask_002", signo, SIGCHIAN_TEST_SIGNAL_NUM_1);
119
120 remove_special_signal_handler(SIGPWR, sigchain_special_handler3);
121
122 raise(SIGPWR);
123 if (get_sigchain_mask_enable()) {
124 EXPECT_EQ("sigchain_intercept_pthread_sigmask_002", g_count, SIGCHIAN_TEST_SIGNAL_NUM_1);
125 } else {
126 EXPECT_EQ("sigchain_intercept_pthread_sigmask_002", g_count, 0);
127 }
128 }
129
130 /**
131 * @brief the signal handler
132 */
signal_handler1(int signo)133 static void signal_handler1(int signo)
134 {
135 g_count++;
136 EXPECT_FALSE("sigchain_intercept_pthread_sigmask_001", true);
137 }
138
139 /**
140 * @tc.name : sigchain_intercept_pthread_sigmask_003
141 * @tc.desc : The signals are not registered with the special handler, test the influence of sigchain
142 * on pthread_sigmask.
143 * @tc.level : Level 0
144 */
sigchain_intercept_pthread_sigmask_003()145 static void sigchain_intercept_pthread_sigmask_003()
146 {
147 g_count = 0;
148 struct sigaction siga1 = {
149 .sa_handler = signal_handler1,
150 };
151 sigaction(SIGHUP, &siga1, NULL);
152
153 sigset_t set = {0};
154 int signo[SIGCHIAN_TEST_SIGNAL_NUM_1] = {SIGHUP};
155 SIGCHAIN_TEST_SET_MASK(set, "sigchain_intercept_pthread_sigmask_003", signo, SIGCHIAN_TEST_SIGNAL_NUM_1);
156 raise(SIGHUP);
157 EXPECT_EQ("sigchain_intercept_pthread_sigmask_003", g_count, 0);
158 }
159
160 /**
161 * @tc.name : sigchain_intercept_pthread_sigmask_004
162 * @tc.desc : The new set is null, call pthread_sigmask.
163 * @tc.level : Level 0
164 */
sigchain_intercept_pthread_sigmask_004()165 static void sigchain_intercept_pthread_sigmask_004()
166 {
167 int result = pthread_sigmask(SIG_BLOCK, NULL, NULL);
168 if (result != 0) {
169 EXPECT_FALSE("sigchain_intercept_pthread_sigmask_004", true);
170 }
171 }
172
main(void)173 int main(void)
174 {
175 sigchain_intercept_pthread_sigmask_001();
176 sigchain_intercept_pthread_sigmask_002();
177 sigchain_intercept_pthread_sigmask_003();
178 sigchain_intercept_pthread_sigmask_004();
179 return t_status;
180 }