• 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 
ChildFunClone1(void * p)73 static int ChildFunClone1(void *p)
74 {
75     (void)p;
76     int ret;
77     int status;
78     const char *containerType = "pid";
79     const char *containerType1 = "pid_for_children";
80 
81     auto pid = getpid();
82     ret = unshare(CLONE_NEWPID);
83     if (ret == -1) {
84         return EXIT_CODE_ERRNO_1;
85     }
86     auto pid1 = getpid();
87     if (pid != pid1) {
88         return EXIT_CODE_ERRNO_2;
89     }
90 
91     auto linkBuffer = ReadlinkContainer(pid, containerType);
92     auto linkBuffer1 = ReadlinkContainer(pid, containerType1);
93     ret = linkBuffer.compare(linkBuffer1);
94     if (ret == 0) {
95         return EXIT_CODE_ERRNO_3;
96     }
97 
98     void *pstk = malloc(STACK_SIZE);
99     if (pstk == NULL) {
100         return EXIT_CODE_ERRNO_4;
101     }
102     int childPid = clone(ChildFunClone2, (char *)pstk + STACK_SIZE, CLONE_NEWUTS | SIGCHLD, NULL);
103     free(pstk);
104     if (childPid == -1) {
105         return EXIT_CODE_ERRNO_5;
106     }
107 
108     ret = waitpid(childPid, &status, 0);
109     if (ret != childPid) {
110         return EXIT_CODE_ERRNO_6;
111     }
112     ret = WIFEXITED(status);
113     if (ret == 0) {
114         return EXIT_CODE_ERRNO_7;
115     }
116     ret = WEXITSTATUS(status);
117     if (ret != 0) {
118         return EXIT_CODE_ERRNO_8;
119     }
120     return 0;
121 }
122 
ItPidContainer005(void)123 void ItPidContainer005(void)
124 {
125     int status;
126     int ret;
127     void *pstk = malloc(STACK_SIZE);
128     ASSERT_TRUE(pstk != NULL);
129 
130     int childPid = clone(ChildFunClone1, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
131     free(pstk);
132     ASSERT_NE(childPid, -1);
133 
134     ret = waitpid(childPid, &status, 0);
135     ASSERT_EQ(ret, childPid);
136     ret = WIFEXITED(status);
137     ASSERT_NE(ret, 0);
138     ret = WEXITSTATUS(status);
139     ASSERT_EQ(ret, 0);
140 }
141