• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific prior written
16  * permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #include "It_container_test.h"
31 
32 const int TEST_COUNT = 5;
33 const int TEST_PARENT_SLEEP_TIME = 3;
Child(void * p)34 static int Child(void *p)
35 {
36     (void)p;
37     pid_t pid1 = fork();
38     if (pid1 == 0) {
39         pid_t pid3 = fork();
40         if (pid3 == 0) {
41             sleep(TEST_PARENT_SLEEP_TIME);
42             exit(0);
43         }
44 
45         sleep(TEST_PARENT_SLEEP_TIME);
46         if (getppid() != 1) {
47             exit(EXIT_CODE_ERRNO_3);
48         }
49         exit(0);
50     }
51 
52     pid_t pid2 = fork();
53     if (pid2 == 0) {
54         sleep(TEST_PARENT_SLEEP_TIME);
55 
56         if (getppid() != 1) {
57             exit(EXIT_CODE_ERRNO_4);
58         }
59         exit(0);
60     }
61 
62     exit(0);
63 }
64 
ChildFun(void * p)65 static int ChildFun(void *p)
66 {
67     (void)p;
68     int ret;
69     int count = 2;
70     int status = 0;
71 
72     pid_t pid1 = fork();
73     if (pid1 == 0) {
74         (void)Child(NULL);
75     }
76 
77     ret = waitpid(pid1, &status, 0);
78     if ((ret != pid1) || (WIFEXITED(status) == 0)) {
79         exit(EXIT_CODE_ERRNO_2);
80     }
81     if (WEXITSTATUS(status) != 0) {
82         exit(WEXITSTATUS(status));
83     }
84 
85     while (count > 0) {
86         ret = wait(&status);
87         if ((ret < 0) || (WIFEXITED(status) == 0)) {
88             exit(EXIT_CODE_ERRNO_3);
89         }
90         if (WEXITSTATUS(status) != 0) {
91             exit(WEXITSTATUS(status));
92         }
93         count--;
94     }
95 
96     return 0;
97 }
98 
ItPidContainer020(void)99 void ItPidContainer020(void)
100 {
101     int status;
102     int ret;
103     void *pstk = malloc(STACK_SIZE);
104     ASSERT_TRUE(pstk != NULL);
105     int childPid = clone(ChildFun, (char *)pstk + STACK_SIZE, CLONE_NEWPID | SIGCHLD, NULL);
106     free(pstk);
107     ASSERT_NE(childPid, -1);
108 
109     ret = waitpid(childPid, &status, 0);
110     ASSERT_EQ(ret, childPid);
111     ret = WIFEXITED(status);
112     ASSERT_NE(ret, 0);
113     ret = WEXITSTATUS(status);
114     ASSERT_EQ(ret, 0);
115 
116     sleep(5); /* 5: Wait for process resources to be reclaimed */
117 }
118