1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include "it_test_signal.h"
32 #include "signal.h"
33
34 #include "pthread.h"
35
Sigprint(int sig)36 static void Sigprint(int sig)
37 {
38 (void)sig;
39 printf("enter sighandle : -------------------------\n");
40 }
Sigprint111(int sig)41 static void Sigprint111(int sig)
42 {
43 (void)sig;
44 printf("enter sighandle : ---111----------------------\n");
45 }
46
ThreadFunc7(void * arg)47 static void *ThreadFunc7(void *arg)
48 {
49 (void)arg;
50 int retval;
51 sigset_t set, oldset;
52 sighandler_t sigret;
53 printf("-----------------------------\n");
54 sleep(1);
55 raise(SIGUSR2);
56 sleep(1);
57 sigret = signal(SIGUSR2, Sigprint);
58
59 sleep(1);
60 raise(SIGUSR2);
61 retval = sigemptyset(&set);
62 if (retval == -1) {
63 exit(-1);
64 }
65 printf("line = %d\n", __LINE__);
66 retval = sigemptyset(&oldset);
67 if (retval == -1) {
68 exit(-1);
69 }
70 printf("line = %d\n", __LINE__);
71 retval = sigprocmask(SIG_SETMASK, &set, &oldset);
72 if (retval == -1) {
73 exit(-1);
74 }
75 retval = sigismember(&oldset, SIGTERM);
76 printf("th SIGTERM is in oldset:%d\n", retval);
77 if (retval != 1) {
78 exit(-1);
79 }
80 retval = sigismember(&oldset, SIGUSR1);
81 printf("th SIGUSR1 is in oldset:%d\n", retval);
82 if (retval != 1) {
83 exit(-1);
84 }
85 retval = sigismember(&oldset, SIGUSR2);
86 printf("th SIGUSR2 is in oldset:%d\n", retval);
87 if (retval != 0) {
88 exit(-1);
89 }
90 retval = sigprocmask(SIG_SETMASK, &oldset, &set);
91 if (retval == -1) {
92 exit(-1);
93 }
94 sleep(1);
95 printf("-----------------------------\n");
96 retval = sigemptyset(&set);
97 if (retval == -1) {
98 exit(-1);
99 }
100 retval = sigemptyset(&oldset);
101 if (retval == -1) {
102 exit(-1);
103 }
104 retval = sigprocmask(SIG_SETMASK, &set, &oldset);
105 if (retval == -1) {
106 exit(-1);
107 }
108 retval = sigismember(&oldset, SIGTERM);
109 printf("SIGTERM is in oldset:%d\n", retval);
110 if (retval != 1) {
111 exit(-1);
112 }
113 retval = sigismember(&oldset, SIGUSR1);
114 printf("SIGUSR1 is in oldset:%d\n", retval);
115 if (retval != 1) {
116 exit(-1);
117 }
118 retval = sigismember(&oldset, SIGUSR2);
119 printf("SIGUSR2 is in oldset:%d\n", retval);
120 if (retval != 0) {
121 exit(-1);
122 }
123
124 printf("-----------------newthread7 is finished------------\n");
125 return nullptr;
126 }
127
TestCase(VOID)128 static UINT32 TestCase(VOID)
129 {
130 int ret, retval;
131 void *res = nullptr;
132 pthread_t newPthread, newPthread1;
133 sigset_t set, oldset;
134 sighandler_t sigret;
135 sigret = signal(SIGUSR2, Sigprint111);
136 retval = sigemptyset(&set);
137 if (retval == -1) {
138 printf("error: sigemptyset \n");
139 return -1;
140 }
141 retval = sigemptyset(&oldset);
142 if (retval == -1) {
143 printf("error: sigemptyset \n");
144 return -1;
145 }
146 retval = sigaddset(&set, SIGTERM);
147 if (retval == -1) {
148 printf("error: sigemptyset \n");
149 return -1;
150 }
151 retval = sigismember(&set, SIGTERM);
152 printf("SIGTERM is in set:%d\n", retval);
153 ICUNIT_ASSERT_EQUAL(retval, 1, retval);
154 retval = sigaddset(&set, SIGUSR1);
155 if (retval == -1) {
156 printf("error: sigemptyset \n");
157 return -1;
158 }
159 retval = sigismember(&set, SIGUSR1);
160 printf("SIGUSR1 is in set:%d\n", retval);
161 ICUNIT_ASSERT_EQUAL(retval, 1, retval);
162 retval = sigismember(&set, SIGUSR2);
163 ICUNIT_ASSERT_EQUAL(retval, 0, retval);
164 printf("SIGUSR2 is in set:%d\n", retval);
165 retval = sigprocmask(SIG_SETMASK, &set, &oldset);
166 printf("line = %d\n", __LINE__);
167 if (retval == -1) {
168 printf("error: sigemptyset \n");
169 return -1;
170 }
171 printf("line = %d\n", __LINE__);
172 raise(SIGUSR2);
173 printf("line = %d\n", __LINE__);
174 sleep(1);
175 ret = pthread_create(&newPthread1, nullptr, ThreadFunc7, nullptr);
176 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
177 printf("line = %d\n", __LINE__);
178 sleep(1);
179 raise(SIGUSR2);
180 retval = sigaddset(&set, SIGUSR2);
181 if (retval == -1) {
182 printf("error: sigemptyset \n");
183 return -1;
184 }
185 printf("line = %d\n", __LINE__);
186 retval = sigdelset(&set, SIGUSR1);
187 if (retval == -1) {
188 printf("error: sigemptyset \n");
189 return -1;
190 }
191 printf("line = %d\n", __LINE__);
192 retval = sigismember(&set, SIGTERM);
193 printf("f SIGTERM is in set:%d\n", retval);
194 ICUNIT_ASSERT_EQUAL(retval, 1, retval);
195 retval = sigismember(&set, SIGUSR1);
196 printf("f SIGUSR1 is in set:%d\n", retval);
197 ICUNIT_ASSERT_EQUAL(retval, 0, retval);
198 retval = sigismember(&set, SIGUSR2);
199 printf("f SIGUSR2 is in set:%d\n", retval);
200 ICUNIT_ASSERT_EQUAL(retval, 1, retval);
201 retval = sigprocmask(SIG_SETMASK, &set, nullptr);
202 if (retval == -1) {
203 printf("error: sigemptyset \n");
204 return -1;
205 }
206 ret = pthread_join(newPthread1, &res);
207 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
208
209 retval = sigprocmask(SIG_SETMASK, &oldset, &set);
210 if (retval == -1) {
211 printf("error: sigemptyset \n");
212 return -1;
213 }
214
215 printf("-----------------main is finished------------\n");
216
217 return 0;
218 }
219
ItPosixSignal041(void)220 void ItPosixSignal041(void)
221 {
222 TEST_ADD_CASE(__FUNCTION__, TestCase, TEST_POSIX, TEST_SIGNAL, TEST_LEVEL0, TEST_FUNCTION);
223 }
224