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 <cstdio>
32 #include "It_container_test.h"
33
34 static int const configLen = 16;
35 static const int MAX_CONTAINER = 10;
36 static const int g_buffSize = 512;
37 static const int g_arryLen = 4;
38 static const int g_readLen = 254;
39
childFunc(void * arg)40 static int childFunc(void *arg)
41 {
42 (void)arg;
43 sleep(2); /* 2: delay 2s */
44
45 return 0;
46 }
47
ItMntContainer009(void)48 void ItMntContainer009(void)
49 {
50 std::string path = "/proc/sys/user/max_mnt_container";
51 char *array[g_arryLen] = { nullptr };
52 char buf[g_buffSize] = { 0 };
53
54 int ret = ReadFile(path.c_str(), buf);
55 ASSERT_NE(ret, -1);
56
57 GetLine(buf, g_arryLen, g_readLen, array);
58
59 int value = atoi(array[1] + strlen("limit: "));
60 ASSERT_EQ(value, MAX_CONTAINER);
61
62 int usedCount = atoi(array[2] + strlen("count: "));
63
64 (void)memset_s(buf, configLen, 0, configLen);
65 ret = sprintf_s(buf, configLen, "%d", usedCount + 1);
66 ASSERT_GT(ret, 0);
67
68 ret = WriteFile(path.c_str(), buf);
69 ASSERT_NE(ret, -1);
70
71 char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
72 -1, 0);
73 ASSERT_NE(stack, nullptr);
74 char *stackTop = stack + STACK_SIZE;
75
76 auto pid1 = clone(childFunc, stackTop, CLONE_NEWNS, NULL);
77 ASSERT_NE(pid1, -1);
78
79 auto pid2 = clone(childFunc, stackTop, CLONE_NEWNS, NULL);
80 ASSERT_EQ(pid2, -1);
81
82 ret = waitpid(pid1, NULL, 0);
83 ASSERT_EQ(ret, pid1);
84
85 (void)memset_s(buf, configLen, 0, configLen);
86 ret = sprintf_s(buf, configLen, "%d", value);
87 ASSERT_GT(ret, 0);
88
89 ret = WriteFile(path.c_str(), buf);
90 ASSERT_NE(ret, -1);
91
92 (void)memset_s(buf, configLen, 0, configLen);
93 ret = ReadFile(path.c_str(), buf);
94 ASSERT_NE(ret, -1);
95
96 GetLine(buf, g_arryLen, g_readLen, array);
97
98 value = atoi(array[1] + strlen("limit: "));
99 ASSERT_EQ(value, MAX_CONTAINER);
100 }
101