• 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     auto pid1 = getpid();
88     if (pid != pid1) {
89         return EXIT_CODE_ERRNO_2;
90     }
91 
92     auto linkBuffer = ReadlinkContainer(pid, containerType);
93     auto linkBuffer1 = ReadlinkContainer(pid, containerType1);
94     ret = linkBuffer.compare(linkBuffer1);
95     if (ret == 0) {
96         return EXIT_CODE_ERRNO_3;
97     }
98 
99     void *pstk = malloc(STACK_SIZE);
100     if (pstk == NULL) {
101         return EXIT_CODE_ERRNO_4;
102     }
103     int childPid = clone(ChildFunClone2, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
104     free(pstk);
105     if (childPid == -1) {
106         return EXIT_CODE_ERRNO_5;
107     }
108 
109     ret = unshare(CLONE_NEWPID);
110     if (ret != -1) {
111         return EXIT_CODE_ERRNO_6;
112     }
113 
114     ret = waitpid(childPid, &status, 0);
115     if (ret != childPid) {
116         return EXIT_CODE_ERRNO_7;
117     }
118     ret = WIFEXITED(status);
119     if (ret == 0) {
120         return EXIT_CODE_ERRNO_8;
121     }
122     ret = WEXITSTATUS(status);
123     if (ret != 0) {
124         return EXIT_CODE_ERRNO_9;
125     }
126     return 0;
127 }
128 
ItPidContainer028(void)129 void ItPidContainer028(void)
130 {
131     void *pstk = malloc(STACK_SIZE);
132     ASSERT_TRUE(pstk != NULL);
133 
134     int childPid = clone(ChildFunClone1, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
135     free(pstk);
136     ASSERT_NE(childPid, -1);
137 
138     int status;
139     int ret = waitpid(childPid, &status, 0);
140     ASSERT_EQ(ret, childPid);
141     ret = WIFEXITED(status);
142     ASSERT_NE(ret, 0);
143     ret = WEXITSTATUS(status);
144     ASSERT_EQ(ret, 0);
145 }
146