• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SigHandler(int sig)34 static void SigHandler(int sig) // �źŴ������
35 {
36     if (sig == SIGINT) {
37         printf("SIGINT sig\n");
38     } else if (sig == SIGQUIT) {
39         printf("SIGQUIT sig\n");
40     } else {
41         printf("SIGUSR1 sig\n");
42     }
43 }
44 
TestSigSuspend()45 static int TestSigSuspend()
46 {
47     int status, retValue;
48     int fpid = fork();
49     ICUNIT_ASSERT_WITHIN_EQUAL(fpid, 0, UINT_MAX, fpid);
50     if (fpid == 0) {
51         sigset_t newset, old, wait;
52         struct sigaction act;
53         printf("SIGUSR1 sig\n");
54         act.sa_handler = SigHandler;
55         retValue = sigemptyset(&act.sa_mask);
56         if (retValue != 0) {
57             exit(retValue);
58         }
59         act.sa_flags = 0;
60         sigaction(SIGINT, &act, nullptr);
61         sigaction(SIGQUIT, &act, nullptr);
62         sigaction(SIGUSR1, &act, nullptr);
63         sigaction(SIGALRM, &act, nullptr);
64 
65         retValue = sigemptyset(&newset);
66         if (retValue != 0) {
67             exit(retValue);
68         }
69         retValue = sigaddset(&newset, SIGALRM);
70         if (retValue != 0) {
71             exit(retValue);
72         }
73         retValue = sigprocmask(SIG_BLOCK, &newset, &old);
74         if (retValue != 0) {
75             exit(retValue);
76         }
77         printf("newset 1 = %x\n", newset.__bits[0]);
78         printf("old 1 = %x\n", old.__bits[0]);
79 
80         retValue = sigemptyset(&newset);
81         if (retValue != 0) {
82             exit(retValue);
83         }
84         retValue = sigaddset(&newset, SIGQUIT);
85         if (retValue != 0) {
86             exit(retValue);
87         }
88         retValue = sigprocmask(SIG_BLOCK, &newset, &old);
89         if (retValue != 0) {
90             exit(retValue);
91         }
92         printf("newset 2 = %x\n", newset.__bits[0]);
93         printf("old 2 = %x\n", old.__bits[0]);
94 
95         retValue = sigemptyset(&newset);
96         if (retValue != 0) {
97             exit(retValue);
98         }
99         retValue = sigaddset(&newset, SIGINT);
100         if (retValue != 0) {
101             exit(retValue);
102         }
103 
104         retValue = sigprocmask(SIG_BLOCK, &newset, &old);
105         if (retValue != 0) {
106             exit(retValue);
107         }
108         printf("newset 1 = %x\n", newset.__bits[0]);
109         printf("old 1 = %x\n", old.__bits[0]);
110 
111         retValue = sigemptyset(&wait);
112         if (retValue != 0) {
113             exit(retValue);
114         }
115         retValue = sigaddset(&wait, SIGUSR1);
116         if (retValue != 0) {
117             exit(retValue);
118         }
119         printf("wait = %x\n", wait.__bits[0]);
120 
121         if (sigsuspend(&wait) != -1) {
122             printf("sigsuspend error\n");
123             return -1;
124         }
125         printf("After sigsuspend\n");
126         if (retValue != 0) {
127             exit(retValue);
128         }
129         printf("old 2= %x\n", old.__bits[0]);
130 
131         sigset_t pending;
132         retValue = sigemptyset(&pending);
133         if (retValue != 0) {
134             exit(retValue);
135         }
136         printf("pending 1= %x\n", pending.__bits[0]);
137         retValue = raise(SIGINT);
138         if (retValue != 0) {
139             exit(retValue);
140         }
141         retValue = sigpending(&pending);
142         if (retValue != 0) {
143             exit(retValue);
144         }
145         printf("pending 2= %x\n", pending.__bits[0]);
146 
147         retValue = raise(SIGALRM);
148         if (retValue != 0) {
149             exit(retValue);
150         }
151         retValue = sigpending(&pending);
152         if (retValue != 0) {
153             exit(retValue);
154         }
155         printf("pending 3= %x\n", pending.__bits[0]);
156         exit(0);
157     }
158 
159     sleep(1);
160     printf("kill SIGUSR1\n");
161     retValue = kill(fpid, SIGUSR1);
162     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
163     sleep(1);
164     printf("kill SIGINT\n");
165     retValue = kill(fpid, SIGINT);
166     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
167     retValue = waitpid(fpid, &status, 0);
168     ICUNIT_ASSERT_EQUAL(retValue, fpid, retValue);
169     ICUNIT_ASSERT_EQUAL(WEXITSTATUS(status), 0, WEXITSTATUS(status));
170 
171     sigset_t new1, old1;
172     struct sigaction act;
173 
174     fpid = fork();
175     if (fpid == 0) {
176         act.sa_handler = SigHandler;
177         retValue = sigemptyset(&act.sa_mask);
178         act.sa_flags = 0;
179         sigaction(SIGINT, &act, nullptr);
180         sigaction(SIGQUIT, &act, nullptr);
181         sigaction(SIGUSR1, &act, nullptr);
182 
183         retValue = sigemptyset(&new1);
184         if (retValue != 0) {
185             exit(retValue);
186         }
187         retValue = sigaddset(&new1, SIGINT);
188         if (retValue != 0) {
189             exit(retValue);
190         }
191         retValue = sigprocmask(SIG_BLOCK, &new1, &old1);
192         if (retValue != 0) {
193             exit(retValue);
194         }
195         printf("new 1 = %x\n", new1.__bits[0]);
196         printf("old 1 = %x\n", old1.__bits[0]);
197 
198         retValue = kill(getpid(), SIGINT);
199         if (retValue != 0) {
200             exit(retValue);
201         }
202         retValue = raise(SIGUSR1);
203         if (retValue != 0) {
204             exit(retValue);
205         }
206         printf("raise 1 = %x\n", new1.__bits[0]);
207 
208         exit(0);
209     }
210 
211     retValue = waitpid(fpid, &status, 0);
212     ICUNIT_ASSERT_EQUAL(retValue, fpid, retValue);
213     ICUNIT_ASSERT_EQUAL(WEXITSTATUS(status), 0, WEXITSTATUS(status));
214 
215     return 0;
216 }
217 
ItPosixSignal020(void)218 void ItPosixSignal020(void)
219 {
220     TEST_ADD_CASE(__FUNCTION__, TestSigSuspend, TEST_POSIX, TEST_SIGNAL, TEST_LEVEL0, TEST_FUNCTION);
221 }
222