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 /* Sample : Father process want to free zombine child process */
SigChildResponse(int signo)36 void SigChildResponse(int signo)
37 {
38 wait(nullptr);
39 }
40
41 /* Register SIGCHLD, through signal to restore the child memory */
TestSigKillWaitFromSigChild()42 static int TestSigKillWaitFromSigChild()
43 {
44 void (*ret)(int);
45 int sig = SIGINT;
46 int fpid, fpids, status, retValue;
47 ret = signal(sig, SIG_IGN);
48 ICUNIT_ASSERT_NOT_EQUAL(ret, NULL, ret);
49
50 ret = signal(sig, SigChildResponse);
51 ICUNIT_ASSERT_NOT_EQUAL(ret, NULL, ret);
52 fpid = fork();
53 ICUNIT_ASSERT_WITHIN_EQUAL(fpid, 0, UINT_MAX, fpid);
54 if (fpid == 0) {
55 fpids = fork();
56 if (fpids == 0) {
57 while (1) {
58 sleep(2); // 2, sleep 10 second.
59 }
60 }
61 usleep(10000); // 10000, Used to calculate the delay time.
62 retValue = kill(fpids, SIGKILL);
63 if (retValue != 0) {
64 exit(retValue);
65 }
66 retValue = waitpid(fpids, &status, 0);
67 if (retValue != fpids) {
68 exit(retValue);
69 }
70 exit(0);
71 }
72 retValue = waitpid(fpid, &status, 0);
73 ICUNIT_ASSERT_EQUAL(retValue, fpid, retValue);
74 ICUNIT_ASSERT_EQUAL(WEXITSTATUS(status), 0, WEXITSTATUS(status));
75
76 ret = signal(sig, SIG_DFL);
77 ICUNIT_ASSERT_NOT_EQUAL(ret, NULL, ret);
78
79 return 0;
80 }
81
ItPosixSignal008(void)82 void ItPosixSignal008(void)
83 {
84 TEST_ADD_CASE("IT_POSIX_SIGNAL_008", TestSigKillWaitFromSigChild, TEST_POSIX, TEST_SIGNAL, TEST_LEVEL0,
85 TEST_FUNCTION);
86 }
87