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
SigPrint(int sig)34 static void SigPrint(int sig)
35 {
36 (void)sig;
37 printf("%s%d\n", __FUNCTION__, __LINE__);
38 }
39
ThreadSetFunc2(void * arg)40 static void *ThreadSetFunc2(void *arg)
41 {
42 int retValue;
43
44 sigset_t set;
45 int sig;
46 retValue = sigemptyset(&set);
47 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
48 retValue = sigaddset(&set, SIGALRM);
49 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
50 retValue = sigaddset(&set, SIGUSR1);
51 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
52
53 retValue = sigwait(&set, &sig);
54 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
55 return NULL;
56 }
57
ThreadSetDfl(void * arg)58 static void *ThreadSetDfl(void *arg)
59 {
60 int retValue;
61
62 sigset_t set;
63 int sig;
64 retValue = sigemptyset(&set);
65 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
66 retValue = sigaddset(&set, SIGALRM);
67 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
68 retValue = sigaddset(&set, SIGUSR1);
69 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
70
71 retValue = sigwait(&set, &sig);
72 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
73 return NULL;
74 }
75
ThreadKill(void * arg)76 static void *ThreadKill(void *arg)
77 {
78 (void)arg;
79 while (1) {
80 sleep(1);
81 }
82 }
83
TestMultiPthreadKillPendNormal()84 static int TestMultiPthreadKillPendNormal()
85 {
86 int status, retValue, fpid;
87 pthread_t thread, thread1, thread2;
88
89 fpid = fork();
90 if (fpid == 0) {
91 retValue = pthread_create(&thread1, NULL, ThreadSetDfl, 0);
92 if (retValue != 0) {
93 exit(retValue);
94 }
95 retValue = pthread_create(&thread2, NULL, ThreadSetFunc2, 0);
96 if (retValue != 0) {
97 exit(retValue);
98 }
99 retValue = pthread_create(&thread, NULL, ThreadKill, 0);
100 if (retValue != 0) {
101 exit(retValue);
102 }
103
104 pthread_join(thread, NULL);
105 pthread_join(thread1, NULL);
106 pthread_join(thread2, NULL);
107
108 sigset_t set;
109 int sig;
110 retValue = sigemptyset(&set);
111 if (retValue != 0) {
112 exit(retValue);
113 }
114 retValue = sigaddset(&set, SIGALRM);
115 if (retValue != 0) {
116 exit(retValue);
117 }
118 retValue = sigaddset(&set, SIGUSR1);
119 if (retValue != 0) {
120 exit(retValue);
121 }
122
123 retValue = sigwait(&set, &sig);
124 if (retValue != 0) {
125 exit(retValue);
126 }
127 printf("here exit\n");
128 exit(0);
129 }
130 sleep(1);
131 /* Father kill child, then it will wake child */
132 retValue = kill(fpid, SIGKILL);
133 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
134 printf("father wait child dead\n");
135 retValue = waitpid(fpid, &status, 0);
136 ICUNIT_ASSERT_EQUAL(retValue, fpid, retValue);
137 printf("status = %d\n", WEXITSTATUS(status));
138 ICUNIT_ASSERT_EQUAL(WIFEXITED(status), 0, WIFEXITED(status));
139 ICUNIT_ASSERT_EQUAL(WIFSIGNALED(status), 1, WIFSIGNALED(status));
140 ICUNIT_ASSERT_EQUAL(WTERMSIG(status), SIGKILL, WTERMSIG(status));
141 return 0;
142 }
143
ItPosixSignal016(void)144 void ItPosixSignal016(void)
145 {
146 TEST_ADD_CASE(__FUNCTION__, TestMultiPthreadKillPendNormal, TEST_POSIX, TEST_SIGNAL, TEST_LEVEL0, TEST_FUNCTION);
147 }
148