• 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 #include "sys/wait.h"
34 
35 static const int SIG_TEST_COUNT = 3;
36 static int g_sigCount = 0;
SigPrint(int sig)37 static void SigPrint(int sig)
38 {
39     g_sigCount++;
40 }
41 
TestRaiseMuliti()42 static int TestRaiseMuliti()
43 {
44     int sig = SIGTERM;
45     int count = 0;
46     void (*ret)(int);
47     int retValue;
48 
49     g_sigCount = 0;
50     ret = signal(sig, SigPrint);
51     ICUNIT_ASSERT_NOT_EQUAL(ret, NULL, ret);
52 
53     while (1) {
54         retValue = raise(sig);
55         ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
56         usleep(10000); // 10000, Used to calculate the delay time.
57         count++;
58         if (count >= SIG_TEST_COUNT) {
59             break;
60         }
61     }
62 
63     return g_sigCount;
64 }
65 
TestCase(void)66 static int TestCase(void)
67 {
68     int count = TestRaiseMuliti();
69     ICUNIT_ASSERT_EQUAL(count, SIG_TEST_COUNT, count);
70 
71     int pid = fork();
72     if (pid == 0) {
73         int retValue;
74         printf("sigprocmask(1, NULL, NULL);\n");
75         retValue = sigprocmask(1, NULL, NULL);
76         if (retValue != 0) {
77             printf("errline  = %d\n", __LINE__);
78             exit(-1);
79         }
80         printf("raise(SIGSTOP);\n");
81         raise(SIGSTOP);
82 
83         int ret = kill(10, 0); // 10, kill process pid.
84         if (retValue != -1 || errno != ESRCH) {
85             exit(-1);
86         }
87 
88         ret = kill(99999, 0); // 99999, kill process pid.
89         if (retValue != -1 || errno != ESRCH) {
90             printf("errline  = %d\n", __LINE__);
91             exit(-1);
92         }
93         ret = kill(10, 31); // 10, kill process pid; 31, signal.
94         if (retValue != -1 || errno != EINVAL) {
95             printf("errline  = %d\n", __LINE__);
96             exit(-1);
97         }
98         ret = kill(10, 32); // 10, kill process pid; 32, signal.
99         if (retValue != -1 || errno != EINVAL) {
100             printf("errline  = %d\n", __LINE__);
101             exit(-1);
102         }
103 
104         ret = kill(2, 32); // 2, kill process pid; 32, signal.
105         if (retValue != -1 || errno != EINVAL) {
106             printf("errline  = %d\n", __LINE__);
107             exit(-1);
108         }
109 
110         printf("test EPERM begin\n");
111         ret = kill(2, 5); // 2, kill process pid; 5, signal.
112         if (retValue != -1 || errno != EPERM) {
113             printf("errline  = %d\n", __LINE__);
114             exit(-1);
115         }
116 
117         ret = kill(3, 5); // 3, kill process pid; 5, signal.
118         if (retValue != -1 || errno != EPERM) {
119             printf("errline  = %d\n", __LINE__);
120             exit(-1);
121         }
122 
123         ret = kill(0, 5); // 5, kill sigal num.
124         if (retValue != -1 || errno != EPERM) {
125             printf("errline  = %d\n", __LINE__);
126             exit(-1);
127         }
128 
129         ret = kill(1, 5); // 5, kill signal num .
130         if (retValue != -1 || errno != EPERM) {
131             printf("errline  = %d\n", __LINE__);
132             exit(-1);
133         }
134 
135         printf("test kill ok\n");
136         retValue = raise(SIGSTOP);
137         if (retValue != 0) {
138             printf("errline  = %d\n", __LINE__);
139             exit(-1);
140         }
141         int status, rt;
142         signal(SIGALRM, SigPrint);
143         sigset_t sigmask, oldmask, pending;
144         sigemptyset(&sigmask);
145         sigemptyset(&oldmask);
146         sigemptyset(&pending);
147         sigpending(&pending);
148         if (sigisemptyset(&pending) != 1) {
149             printf("errline  = %d\n", __LINE__);
150             exit(-1);
151         }
152         sigaddset(&sigmask, SIGALRM);
153         sigaddset(&sigmask, SIGUSR1);
154         sigprocmask(SIG_BLOCK, &sigmask, &oldmask);
155         sigpending(&pending);
156         if (sigisemptyset(&pending) != 1) {
157             printf("errline  = %d\n", __LINE__);
158             exit(-1);
159         }
160         if (sigisemptyset(&oldmask) != 1) {
161             printf("errline  = %d\n", __LINE__);
162             exit(-1);
163         }
164 
165         printf("1 pending=%lu\n", pending.__bits[0]);
166         printf("1 oldmask=%lu\n", oldmask.__bits[0]);
167         printf("before raise\n");
168         raise(SIGALRM);
169         printf("after raise\n");
170         sigpending(&pending);
171         if (sigismember(&pending, SIGALRM) != 1) {
172             printf("errline  = %d\n", __LINE__);
173             exit(-1);
174         }
175         printf("pending=%d,sigismem = %lu\n", pending.__bits[0], sigismember(&pending, SIGALRM));
176         exit(0);
177     }
178     sleep(1);
179     int status;
180     signal(SIGALRM, SIG_DFL);
181     int retValue = waitpid(pid, &status, 0);
182     ICUNIT_ASSERT_EQUAL(retValue, pid, retValue);
183     return 0;
184 }
185 
ItPosixSignal001(void)186 void ItPosixSignal001(void)
187 {
188     TEST_ADD_CASE("IT_POSIX_SIGNAL_001", TestCase, TEST_POSIX, TEST_SIGNAL, TEST_LEVEL0, TEST_FUNCTION);
189 }
190