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
56 return NULL;
57 }
58
ThreadSetDfl(void * arg)59 static void *ThreadSetDfl(void *arg)
60 {
61 int retValue;
62
63 sigset_t set;
64 int sig;
65 retValue = sigemptyset(&set);
66 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
67 retValue = sigaddset(&set, SIGALRM);
68 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
69 retValue = sigaddset(&set, SIGUSR1);
70 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
71
72 retValue = sigwait(&set, &sig);
73 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
74
75 return NULL;
76 }
77
ThreadKill(void * arg)78 static void *ThreadKill(void *arg)
79 {
80 int retValue;
81
82 sigset_t set;
83 int sig;
84 retValue = sigemptyset(&set);
85 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
86 retValue = sigaddset(&set, SIGALRM);
87 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
88 retValue = sigaddset(&set, SIGUSR1);
89 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
90
91 retValue = sigwait(&set, &sig);
92 ICUNIT_ASSERT_EQUAL_NULL(retValue, 0, retValue);
93
94 return NULL;
95 }
96
TestMultiPthreadFatherProcessExit()97 static int TestMultiPthreadFatherProcessExit()
98 {
99 int status = 0;
100 int retValue;
101 int fpid, fpids;
102
103 pthread_t thread, thread1, thread2;
104 int father;
105
106 fpid = fork();
107 ICUNIT_ASSERT_WITHIN_EQUAL(fpid, 0, UINT_MAX, fpid);
108 if (fpid == 0) {
109 fpids = fork();
110 ICUNIT_ASSERT_WITHIN_EQUAL(fpids, 0, UINT_MAX, fpids);
111 if (fpids == 0) {
112 retValue = pthread_create(&thread, NULL, ThreadKill, 0);
113 if (retValue != 0) {
114 exit(retValue);
115 }
116 retValue = pthread_create(&thread2, NULL, ThreadSetFunc2, 0);
117 if (retValue != 0) {
118 exit(retValue);
119 }
120 retValue = pthread_create(&thread1, NULL, ThreadSetDfl, 0);
121 if (retValue != 0) {
122 exit(retValue);
123 }
124
125 father = getppid();
126 printf("fatherPid = %d\n", father);
127 sleep(1);
128 printf("child kill father = %d\n", father);
129 retValue = kill(father, SIGKILL);
130 if (retValue != 0) {
131 exit(retValue);
132 }
133 }
134 sleep(1);
135 retValue = waitpid(fpids, &status, 0);
136 // grandchild process kill father process, so child process is recovered by init process
137 // child process doesn't receive termination signal from grandchild process
138 // so waitpid() returns -1
139 ICUNIT_ASSERT_EQUAL(retValue, -1, retValue);
140 exit(1);
141 }
142 sleep(1);
143 retValue = waitpid(fpid, &status, 0);
144 ICUNIT_ASSERT_EQUAL(retValue, fpid, retValue);
145 ICUNIT_ASSERT_EQUAL(WIFEXITED(status), 0, WIFEXITED(status));
146 ICUNIT_ASSERT_EQUAL(WIFSIGNALED(status), 1, WIFSIGNALED(status));
147 ICUNIT_ASSERT_EQUAL(WTERMSIG(status), SIGKILL, WTERMSIG(status));
148 return 0;
149 }
150
ItPosixSignal017(void)151 void ItPosixSignal017(void)
152 {
153 TEST_ADD_CASE(__FUNCTION__, TestMultiPthreadFatherProcessExit, TEST_POSIX, TEST_SIGNAL, TEST_LEVEL0, TEST_FUNCTION);
154 }
155