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
31 #include <sched.h>
32 #include <unistd.h>
33 #include <string>
34 #include <iostream>
35 #include "It_container_test.h"
36
37 const int EXIT_TRUE_CODE = 255;
38 const int MAX_PID_RANGE = 100000;
39
childFunc(void * arg)40 static int childFunc(void *arg)
41 {
42 (void)arg;
43 std::string containerType = "user";
44 std::string childlink;
45 std::string filePath;
46 std::string parentlink1;
47 int fd;
48 int ret;
49 int status;
50 int parentPid = getpid();
51
52 auto parentlink = ReadlinkContainer(parentPid, containerType);
53 int childsPid = CloneWrapper(ChildFunction, CLONE_NEWUSER, NULL);
54 if (childsPid == -1) {
55 return EXIT_CODE_ERRNO_1;
56 }
57
58 childlink = ReadlinkContainer(childsPid, containerType);
59 filePath = GenContainerLinkPath(childsPid, containerType);
60 fd = open(filePath.c_str(), O_RDONLY);
61 if (fd == -1) {
62 return EXIT_CODE_ERRNO_2;
63 }
64
65 ret = setns(fd, CLONE_NEWUSER);
66 if (ret == -1) {
67 close(fd);
68 return EXIT_CODE_ERRNO_3;
69 }
70
71 parentlink1 = ReadlinkContainer(parentPid, containerType);
72
73 close(fd);
74
75 ret = waitpid(childsPid, &status, 0);
76 if (ret != childsPid) {
77 return EXIT_CODE_ERRNO_4;
78 }
79
80 ret = parentlink.compare(parentlink1);
81 if (ret == 0) {
82 return EXIT_CODE_ERRNO_5;
83 }
84
85 ret = parentlink1.compare(childlink);
86 if (ret != 0) {
87 return EXIT_CODE_ERRNO_6;
88 }
89
90 exit(EXIT_TRUE_CODE);
91 }
92
ItUserContainer004(void)93 void ItUserContainer004(void)
94 {
95
96 int ret;
97 int status = 0;
98 int arg = CHILD_FUNC_ARG;
99 auto pid = CloneWrapper(childFunc, SIGCHLD, &arg);
100 ASSERT_NE(pid, -1);
101
102 ret = waitpid(pid, &status, 0);
103 ASSERT_EQ(ret, pid);
104
105 status = WEXITSTATUS(status);
106 ASSERT_EQ(status, EXIT_TRUE_CODE);
107 }
108