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 static const int BUF_SIZE = 100;
33
ChildFun2(void * p)34 static int ChildFun2(void *p)
35 {
36 (void)p;
37 sleep(3); /* 3: delay 3s */
38 return 0;
39 }
40
ChildFun1(void * p)41 static int ChildFun1(void *p)
42 {
43 (void)p;
44 sleep(6); /* 6: delay 6s */
45 return 0;
46 }
47
ChildFun(void * p)48 static int ChildFun(void *p)
49 {
50 (void)p;
51 int status, ret;
52 const char *containerType = "pid";
53 const char *containerType1 = "pid_for_children";
54 char targetpath[BUF_SIZE] = {0};
55 auto linkBuffer1 = ReadlinkContainer(getpid(), containerType);
56
57 int childPid = clone(ChildFun1, NULL, CLONE_NEWPID | SIGCHLD, NULL);
58 if (childPid == -1) {
59 return EXIT_CODE_ERRNO_1;
60 }
61 auto linkBuffer2 = ReadlinkContainer(childPid, containerType);
62 ret = linkBuffer2.compare(linkBuffer1);
63 if (ret == 0) {
64 return EXIT_CODE_ERRNO_2;
65 }
66
67 ret = sprintf_s(targetpath, BUF_SIZE, "/proc/%d/container/pid", childPid);
68 if (ret < 0) {
69 return EXIT_CODE_ERRNO_16;
70 }
71 int fd = open(targetpath, O_RDONLY | O_CLOEXEC);
72 if (fd == -1) {
73 return EXIT_CODE_ERRNO_3;
74 }
75
76 ret = setns(fd, CLONE_NEWPID);
77 if (ret != 0) {
78 close(fd);
79 return EXIT_CODE_ERRNO_4;
80 }
81
82 close(fd);
83
84 auto linkBuffer6 = ReadlinkContainer(getpid(), containerType);
85 ret = linkBuffer6.compare(linkBuffer1);
86 if (ret != 0) {
87 return EXIT_CODE_ERRNO_17;
88 }
89
90 auto linkBuffer3 = ReadlinkContainer(getpid(), containerType1);
91 ret = linkBuffer3.compare(linkBuffer2);
92 if (ret != 0) {
93 return EXIT_CODE_ERRNO_5;
94 }
95
96 int childPid1 = clone(ChildFun2, NULL, SIGCHLD, NULL);
97 if (childPid1 == -1) {
98 return EXIT_CODE_ERRNO_6;
99 }
100
101 auto linkBuffer4 = ReadlinkContainer(childPid1, containerType);
102 ret = linkBuffer4.compare(linkBuffer3);
103 if (ret != 0) {
104 return EXIT_CODE_ERRNO_7;
105 }
106
107 int childPid2 = clone(ChildFun2, NULL, CLONE_NEWUTS | SIGCHLD, NULL);
108 if (childPid2 == -1) {
109 return EXIT_CODE_ERRNO_8;
110 }
111
112 auto linkBuffer5 = ReadlinkContainer(childPid2, containerType);
113 ret = linkBuffer5.compare(linkBuffer4);
114 if (ret != 0) {
115 return EXIT_CODE_ERRNO_9;
116 }
117
118 ret = WaitChild(childPid2, &status, EXIT_CODE_ERRNO_10, EXIT_CODE_ERRNO_11);
119 if (ret != 0) {
120 return ret;
121 }
122
123 ret = WaitChild(childPid1, &status, EXIT_CODE_ERRNO_12, EXIT_CODE_ERRNO_13);
124 if (ret != 0) {
125 return ret;
126 }
127 ret = WaitChild(childPid, &status, EXIT_CODE_ERRNO_14, EXIT_CODE_ERRNO_15);
128 if (ret != 0) {
129 return ret;
130 }
131 return 0;
132 }
133
ItPidContainer031(void)134 void ItPidContainer031(void)
135 {
136 int status;
137 int childPid = clone(ChildFun, NULL, CLONE_NEWPID | SIGCHLD, NULL);
138 ASSERT_NE(childPid, -1);
139
140 int ret = waitpid(childPid, &status, 0);
141 ASSERT_EQ(ret, childPid);
142 ret = WIFEXITED(status);
143 ASSERT_NE(ret, 0);
144 ret = WEXITSTATUS(status);
145 ASSERT_EQ(ret, 0);
146 }
147