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 <sys/stat.h>
31 #include <iostream>
32 #include <regex>
33 #include <fcntl.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include "It_container_test.h"
37
38 static const char *CONTAINER_TYPE = "net";
39 static const int SLEEP_SECONDS = 2;
40 static const int TEST_PATH_MAX = 100;
41
NewnetChildFun(void *)42 static int NewnetChildFun(void *)
43 {
44 sleep(SLEEP_SECONDS);
45 return 0;
46 }
47
ChildFun(void * p)48 static int ChildFun(void *p)
49 {
50 (void)p;
51 int ret;
52 int status;
53 char path[TEST_PATH_MAX];
54
55 char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
56 EXPECT_STRNE(stack, nullptr);
57 char *stackTop = stack + STACK_SIZE;
58 int arg = CHILD_FUNC_ARG;
59 auto childPid = clone(NewnetChildFun, stackTop, SIGCHLD | CLONE_NEWNET, &arg);
60 if (childPid == -1) {
61 return EXIT_CODE_ERRNO_1;
62 }
63 auto oldReadLink = ReadlinkContainer(getpid(), CONTAINER_TYPE);
64
65 if (sprintf_s(path, TEST_PATH_MAX, "/proc/%d/container/net", childPid) < 0) {
66 (void)waitpid(childPid, &status, 0);
67 return EXIT_CODE_ERRNO_8;
68 }
69 int fd = open(path, O_RDONLY | O_CLOEXEC);
70 if (fd < 0) {
71 return EXIT_CODE_ERRNO_2;
72 }
73
74 ret = setns(fd, CLONE_NEWNET);
75 if (ret != 0) {
76 (void)close(fd);
77 return EXIT_CODE_ERRNO_3;
78 }
79
80 ret = close(fd);
81 if (ret != 0) {
82 return EXIT_CODE_ERRNO_4;
83 }
84
85 auto newReadLink = ReadlinkContainer(getpid(), CONTAINER_TYPE);
86
87 ret = strcmp(oldReadLink.c_str(), newReadLink.c_str());
88 if (ret == 0) {
89 return EXIT_CODE_ERRNO_5;
90 }
91
92 ret = waitpid(childPid, &status, 0);
93 if (ret != childPid) {
94 return EXIT_CODE_ERRNO_6;
95 }
96
97 int exitCode = WEXITSTATUS(status);
98 if (exitCode != 0) {
99 return EXIT_CODE_ERRNO_7;
100 }
101
102 return 0;
103 }
104
ItNetContainer004(void)105 void ItNetContainer004(void)
106 {
107 int status;
108
109 char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
110 EXPECT_STRNE(stack, nullptr);
111 char *stackTop = stack + STACK_SIZE;
112 int arg = CHILD_FUNC_ARG;
113 auto childPid = clone(ChildFun, stackTop, SIGCHLD | CLONE_NEWNET, &arg);
114 ASSERT_NE(childPid, -1);
115
116 int ret = waitpid(childPid, &status, 0);
117 ASSERT_EQ(ret, childPid);
118
119 int exitCode = WEXITSTATUS(status);
120 ASSERT_EQ(exitCode, 0);
121 }
122