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 const int TIMER_INTERVAL_3S = 2;
33
ChildFun(void * p)34 static int ChildFun(void *p)
35 {
36 (void)p;
37 sleep(TIMER_INTERVAL_3S);
38 return EXIT_CODE_ERRNO_3;
39 }
40
UtsContainerTest(void * arg)41 static int UtsContainerTest(void *arg)
42 {
43 (void)arg;
44 pid_t callerPid;
45 int childPid;
46 int fd = -1;
47 int ret, status, setFlag;
48 char targetpath[100];
49 const char *containerType = "uts";
50
51 callerPid = getpid();
52 childPid = clone(ChildFun, NULL, CLONE_NEWUTS | SIGCHLD, NULL);
53 if (childPid == -1) {
54 return EXIT_CODE_ERRNO_1;
55 }
56
57 auto linkBuffer1 = ReadlinkContainer(callerPid, containerType);
58 if (linkBuffer1.c_str() == NULL) {
59 return EXIT_CODE_ERRNO_2;
60 }
61
62 ret = sprintf_s(targetpath, sizeof(targetpath), "/proc/%d/container/uts", childPid);
63 if (ret == -1) {
64 return EXIT_CODE_ERRNO_4;
65 }
66
67 fd = open(targetpath, O_RDONLY | O_CLOEXEC);
68 if (fd == -1) {
69 return EXIT_CODE_ERRNO_5;
70 }
71
72 setFlag = CLONE_NEWUTS;
73 ret = setns(fd, setFlag);
74 (void)close(fd);
75 if (ret == -1) {
76 return EXIT_CODE_ERRNO_6;
77 }
78
79 auto linkBuffer2 = ReadlinkContainer(callerPid, containerType);
80
81 ret = linkBuffer2.compare(linkBuffer1);
82 if (ret == 0) {
83 return EXIT_CODE_ERRNO_7;
84 }
85
86 ret = waitpid(childPid, &status, 0);
87 if (ret != childPid) {
88 return EXIT_CODE_ERRNO_8;
89 }
90
91 int exitCode = WEXITSTATUS(status);
92 if (exitCode != EXIT_CODE_ERRNO_3) {
93 return EXIT_CODE_ERRNO_9;
94 }
95
96 ret = setns(fd, setFlag);
97 if (ret != -1) {
98 return EXIT_CODE_ERRNO_10;
99 }
100 return 0;
101 }
102
ItUtsContainer005(void)103 void ItUtsContainer005(void)
104 {
105 int ret;
106
107 int arg = CHILD_FUNC_ARG;
108 auto pid = CloneWrapper(UtsContainerTest, CLONE_NEWUTS, &arg);
109 ASSERT_NE(pid, -1);
110
111 int status;
112 ret = waitpid(pid, &status, 0);
113 ASSERT_EQ(ret, pid);
114
115 ret = WIFEXITED(status);
116 ASSERT_NE(ret, 0);
117
118 int exitCode = WEXITSTATUS(status);
119 ASSERT_EQ(exitCode, 0);
120 }
121