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