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 
ChildFun(void *p)32 static int ChildFun(void *p)
33 {
34     (void)p;
35     return getpid();
36 }
37 
ChildFunClone3(void *p)38 static int ChildFunClone3(void *p)
39 {
40     (void)p;
41     int childPid;
42     int status;
43     int ret;
44     pid_t pid = getpid();
45     int childFunRet = (int)pid;
46     void *pstk = malloc(STACK_SIZE);
47     if (pstk == NULL) {
48         return EXIT_CODE_ERRNO_1;
49     }
50     pid_t parent_pid = getppid();
51     if (parent_pid != CONTAINER_FIRST_PID) {
52         free(pstk);
53         return EXIT_CODE_ERRNO_3;
54     }
55 
56     childPid = clone(ChildFun, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
57     free(pstk);
58     if (childPid == -1) {
59         return EXIT_CODE_ERRNO_4;
60     }
61 
62     ret = waitpid(childPid, &status, 0);
63     if (ret != childPid) {
64         return EXIT_CODE_ERRNO_5;
65     }
66     ret = WIFEXITED(status);
67     if (ret == 0) {
68         return EXIT_CODE_ERRNO_6;
69     }
70     ret = WEXITSTATUS(status);
71     if (ret != CONTAINER_THIRD_PID) {
72         return EXIT_CODE_ERRNO_7;
73     }
74     return childFunRet;
75 }
76 
ChildFunClone2(void *p)77 static int ChildFunClone2(void *p)
78 {
79     (void)p;
80     int status;
81     int ret;
82     pid_t pid = getpid();
83     int childFunRet = (int)pid;
84     void *pstk = malloc(STACK_SIZE);
85     if (pstk == NULL) {
86         return EXIT_CODE_ERRNO_2;
87     }
88     int childPid = clone(ChildFunClone3, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
89     if (childPid == -1) {
90         free(pstk);
91         return EXIT_CODE_ERRNO_3;
92     }
93 
94     ret = wait(&status);
95     ret = WIFEXITED(status);
96     ret = WEXITSTATUS(status);
97     if (ret != CONTAINER_SECOND_PID) {
98         free(pstk);
99         return EXIT_CODE_ERRNO_4;
100     }
101 
102     free(pstk);
103     return childFunRet;
104 }
105 
ChildFunClone1(void *p)106 static int ChildFunClone1(void *p)
107 {
108     (void)p;
109     int status;
110     int ret;
111     pid_t pid = getpid();
112     int childFunRet = (int)pid;
113     char *containerType = "pid";
114     int childPid = clone(ChildFunClone2, NULL, CLONE_NEWPID | SIGCHLD, NULL);
115     if (childPid == -1) {
116         return EXIT_CODE_ERRNO_3;
117     }
118 
119     auto linkBuffer = ReadlinkContainer(childPid, containerType);
120     auto linkBuffer1 = ReadlinkContainer(getpid(), containerType);
121     ret = linkBuffer.compare(linkBuffer1);
122     if (ret == 0) {
123         (void)waitpid(childPid, &status, 0);
124         return EXIT_CODE_ERRNO_5;
125     }
126 
127     ret = waitpid(childPid, &status, 0);
128     ret = WIFEXITED(status);
129     ret = WEXITSTATUS(status);
130     if (ret != CONTAINER_FIRST_PID) {
131         return EXIT_CODE_ERRNO_4;
132     }
133     return childFunRet;
134 }
135 
ItPidContainer002(void)136 void ItPidContainer002(void)
137 {
138     int status;
139     int ret;
140     void *pstk = malloc(STACK_SIZE);
141     ASSERT_TRUE(pstk != NULL);
142     int childPid = clone(ChildFunClone1, (char *)pstk + STACK_SIZE, CLONE_NEWPID | SIGCHLD, NULL);
143     free(pstk);
144     ASSERT_NE(childPid, -1);
145 
146     ret = waitpid(childPid, &status, 0);
147     ASSERT_EQ(ret, childPid);
148     ret = WIFEXITED(status);
149     ASSERT_NE(ret, 0);
150     ret = WEXITSTATUS(status);
151     ASSERT_EQ(ret, CONTAINER_FIRST_PID);
152 }
153