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