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_FIRST_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 return 0;
50 }
51
ChildFunClone1(void * p)52 static int ChildFunClone1(void *p)
53 {
54 (void)p;
55 int ret;
56 int status;
57 const char *containerType = "pid";
58 const char *containerType1 = "pid_for_children";
59
60 auto pid = getpid();
61 ret = unshare(CLONE_NEWPID);
62 if (ret == -1) {
63 return EXIT_CODE_ERRNO_1;
64 }
65 auto pid1 = getpid();
66 if (pid != pid1) {
67 return EXIT_CODE_ERRNO_2;
68 }
69
70 auto linkBuffer = ReadlinkContainer(pid, containerType);
71 auto linkBuffer1 = ReadlinkContainer(pid, containerType1);
72 ret = linkBuffer.compare(linkBuffer1);
73 if (ret == 0) {
74 return EXIT_CODE_ERRNO_3;
75 }
76
77 void *pstk = malloc(STACK_SIZE);
78 if (pstk == NULL) {
79 return EXIT_CODE_ERRNO_4;
80 }
81 int childPid = clone(ChildFunClone2, (char *)pstk + STACK_SIZE, CLONE_NEWPID | SIGCHLD, NULL);
82 if (childPid != -1) {
83 free(pstk);
84 return EXIT_CODE_ERRNO_5;
85 }
86
87 int childPid1 = clone(ChildFunClone3, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
88 free(pstk);
89 if (childPid1 == -1) {
90 return EXIT_CODE_ERRNO_6;
91 }
92
93 ret = unshare(CLONE_NEWPID);
94 if (ret != -1) {
95 return EXIT_CODE_ERRNO_7;
96 }
97
98 ret = waitpid(childPid1, &status, 0);
99 if (ret != childPid1) {
100 return EXIT_CODE_ERRNO_11;
101 }
102 ret = WIFEXITED(status);
103 if (ret == 0) {
104 return EXIT_CODE_ERRNO_12;
105 }
106 ret = WEXITSTATUS(status);
107 if (ret != 0) {
108 return EXIT_CODE_ERRNO_13;
109 }
110 return 0;
111 }
112
ItPidContainer029(void)113 void ItPidContainer029(void)
114 {
115 void *pstk = malloc(STACK_SIZE);
116 ASSERT_TRUE(pstk != NULL);
117
118 int childPid = clone(ChildFunClone1, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
119 free(pstk);
120 ASSERT_NE(childPid, -1);
121
122 int status;
123 int ret = waitpid(childPid, &status, 0);
124 ASSERT_EQ(ret, childPid);
125 ret = WIFEXITED(status);
126 ASSERT_NE(ret, 0);
127 ret = WEXITSTATUS(status);
128 ASSERT_EQ(ret, 0);
129 }
130