feat: 支持mnt容器及增强能力

BREAKING CHANGE:
支持mnt容器及增强对外变更:
1.clone 支持CLONE_NEWNS
2.增加”/proc/[pid]/container/mnt" 用于查询容器信息
3.新增chroot接口

Close #I6APW2

Signed-off-by: zhushengle <zhushengle@huawei.com>
Change-Id: I5e8843a0f59bb5b0e6a66a6810dc552e8184a03e
This commit is contained in:
zhushengle
2023-01-16 15:28:40 +08:00
parent d4794ebc38
commit e2d903b663
32 changed files with 1402 additions and 17 deletions

View File

@@ -135,6 +135,7 @@ if (defined(LOSCFG_PROC_PROCESS_DIR)) {
LOSCFG_USER_TEST_CONTAINER = false
LOSCFG_USER_TEST_PID_CONTAINER = false
LOSCFG_USER_TEST_UTS_CONTAINER = false
LOSCFG_USER_TEST_MNT_CONTAINER = false
if (defined(LOSCFG_KERNEL_CONTAINER)) {
LOSCFG_USER_TEST_CONTAINER = true
if (defined(LOSCFG_PID_CONTAINER)) {
@@ -143,4 +144,7 @@ if (defined(LOSCFG_KERNEL_CONTAINER)) {
if (defined(LOSCFG_UTS_CONTAINER)) {
LOSCFG_USER_TEST_UTS_CONTAINER = true
}
if (defined(LOSCFG_MNT_CONTAINER)) {
LOSCFG_USER_TEST_MNT_CONTAINER = true
}
}

View File

@@ -38,6 +38,9 @@ config("container_config") {
if (defined(LOSCFG_USER_TEST_UTS_CONTAINER)) {
cflags += [ "-DLOSCFG_USER_TEST_UTS_CONTAINER" ]
}
if (defined(LOSCFG_USER_TEST_MNT_CONTAINER)) {
cflags += [ "-DLOSCFG_USER_TEST_MNT_CONTAINER" ]
}
cflags_cc = cflags
}

View File

@@ -34,6 +34,7 @@
const char *USERDATA_DIR_NAME = "/userdata";
const char *ACCESS_FILE_NAME = "/userdata/mntcontainertest";
const char *MNT_ACCESS_FILE_NAME = "/mntcontainertest";
const char *USERDATA_DEV_NAME = "/dev/mmcblk0p2";
const char *FS_TYPE = "vfat";
@@ -138,6 +139,7 @@ HWTEST_F(ContainerTest, ItUtsContainer001, TestSize.Level0)
{
ItUtsContainer001();
}
/**
* @tc.name: Container_UTS_Test_002
* @tc.desc: uts container function test case
@@ -150,7 +152,71 @@ HWTEST_F(ContainerTest, ItUtsContainer002, TestSize.Level0)
ItUtsContainer002();
}
#endif
#endif
#if defined(LOSCFG_USER_TEST_MNT_CONTAINER)
/**
* @tc.name: Container_MNT_Test_001
* @tc.desc: mnt container function test case
* @tc.type: FUNC
* @tc.require: issueI6APW2
* @tc.author:
*/
HWTEST_F(ContainerTest, ItMntContainer001, TestSize.Level0)
{
ItMntContainer001();
}
/**
* @tc.name: Container_MNT_Test_002
* @tc.desc: mnt container function test case
* @tc.type: FUNC
* @tc.require: issueI6APW2
* @tc.author:
*/
HWTEST_F(ContainerTest, ItMntContainer002, TestSize.Level0)
{
ItMntContainer002();
}
/**
* @tc.name: Container_MNT_Test_003
* @tc.desc: mnt container function test case
* @tc.type: FUNC
* @tc.require: issueI6APW2
* @tc.author:
*/
HWTEST_F(ContainerTest, ItMntContainer003, TestSize.Level0)
{
ItMntContainer003();
}
/**
* @tc.name: Container_MNT_Test_004
* @tc.desc: mnt container function test case
* @tc.type: FUNC
* @tc.require: issueI6APW2
* @tc.author:
*/
HWTEST_F(ContainerTest, ItMntContainer004, TestSize.Level0)
{
ItMntContainer004();
}
/**
* @tc.name: chroot_Test_001
* @tc.desc: chroot function test case
* @tc.type: FUNC
* @tc.require: issueI6APW2
* @tc.author:
*/
HWTEST_F(ContainerTest, ItContainerChroot001, TestSize.Level0)
{
ItContainerChroot001();
}
#endif /* LOSCFG_MNT_CONTAINER */
#endif /* LOSCFG_USER_TEST_SMOKE */
#if defined(LOSCFG_USER_TEST_FULL)
#if defined(LOSCFG_USER_TEST_PID_CONTAINER)

View File

@@ -63,6 +63,7 @@ const int CONTAINER_THIRD_PID = 3;
extern const char *USERDATA_DIR_NAME;
extern const char *ACCESS_FILE_NAME;
extern const char *MNT_ACCESS_FILE_NAME;
extern const char *USERDATA_DEV_NAME;
extern const char *FS_TYPE;
@@ -80,6 +81,7 @@ extern std::string ReadlinkContainer(int pid, const std::string& containerType);
#if defined(LOSCFG_USER_TEST_SMOKE)
void ItContainer001(void);
void ItContainerChroot001(void);
#if defined(LOSCFG_USER_TEST_PID_CONTAINER)
void ItPidContainer023(void);
#endif
@@ -87,6 +89,16 @@ void ItPidContainer023(void);
void ItUtsContainer001(void);
void ItUtsContainer002(void);
#endif
#if defined(LOSCFG_USER_TEST_MNT_CONTAINER)
void ItMntContainer001(void);
void ItMntContainer002(void);
void ItMntContainer003(void);
void ItMntContainer004(void);
void ItMntContainer005(void);
void ItMntContainer006(void);
void ItMntContainer007(void);
void ItMntContainer008(void);
#endif
#endif
#if defined(LOSCFG_USER_TEST_FULL)

View File

@@ -36,7 +36,11 @@ common_include_dirs = [
sources_entry = [ "$TEST_UNITTEST_DIR/container/It_container_test.cpp" ]
sources_smoke = [ "$TEST_UNITTEST_DIR/container/smoke/It_container_001.cpp" ]
sources_smoke = [
"$TEST_UNITTEST_DIR/container/smoke/It_container_001.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_container_chroot_001.cpp",
]
sources_full = []
if (defined(LOSCFG_USER_TEST_PID_CONTAINER)) {
@@ -75,3 +79,15 @@ if (defined(LOSCFG_USER_TEST_UTS_CONTAINER)) {
sources_full +=
[ "$TEST_UNITTEST_DIR/container/full/It_uts_container_003.cpp" ]
}
if (defined(LOSCFG_USER_TEST_MNT_CONTAINER)) {
sources_smoke += [
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_001.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_002.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_003.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_004.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_005.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_006.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_007.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_008.cpp",
]
}

View File

@@ -0,0 +1,106 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_container_test.h"
using namespace std;
static int ChildFunc(void *arg)
{
int ret;
int value = *((int *)arg);
if (value != CHILD_FUNC_ARG) {
return EXIT_CODE_ERRNO_1;
}
ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, FS_TYPE, 0, nullptr);
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
ret = access(ACCESS_FILE_NAME, F_OK);
if (ret != 0) {
return EXIT_CODE_ERRNO_3;
}
ret = chroot(USERDATA_DIR_NAME);
if (ret != 0) {
return EXIT_CODE_ERRNO_4;
}
ret = access(ACCESS_FILE_NAME, F_OK);
if (ret == 0) {
return EXIT_CODE_ERRNO_5;
}
ret = access(MNT_ACCESS_FILE_NAME, F_OK);
if (ret != 0) {
return EXIT_CODE_ERRNO_6;
}
return 0;
}
void ItContainerChroot001(void)
{
int ret;
int status = 0;
int childReturn = 0;
int arg = CHILD_FUNC_ARG;
char *stack = nullptr;
char *stackTop = nullptr;
stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
ASSERT_NE(stack, MAP_FAILED);
ret = umount(USERDATA_DIR_NAME);
ASSERT_EQ(ret, 0);
stackTop = stack + STACK_SIZE;
auto pid = clone(ChildFunc, stackTop, CLONE_NEWNS, &arg);
ASSERT_NE(pid, -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_NE(ret, 0);
ret = umount(USERDATA_DIR_NAME);
ASSERT_NE(ret, 0);
ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, FS_TYPE, 0, nullptr);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,97 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_container_test.h"
#include "sys/mount.h"
static int ChildFunc(void *arg)
{
int ret;
int value = *((int *)arg);
if (value != CHILD_FUNC_ARG) {
return EXIT_CODE_ERRNO_1;
}
ret = access(ACCESS_FILE_NAME, F_OK);
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
ret = umount(USERDATA_DIR_NAME);
if (ret != 0) {
return EXIT_CODE_ERRNO_3;
}
ret = access(ACCESS_FILE_NAME, F_OK);
if (ret == 0) {
return EXIT_CODE_ERRNO_4;
}
return 0;
}
/* mount container clone test: mount in parent, clone without NEW_NS, umount in child */
void ItMntContainer001(void)
{
int ret;
int status = 0;
int childReturn = 0;
int arg = CHILD_FUNC_ARG;
char *stack = nullptr;
char *stackTop = nullptr;
stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
ASSERT_NE(stack, MAP_FAILED);
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_EQ(ret, 0);
stackTop = stack + STACK_SIZE;
auto pid = clone(ChildFunc, stackTop, 0, &arg);
ASSERT_NE(pid, -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_NE(ret, 0);
ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, FS_TYPE, 0, nullptr);
ASSERT_EQ(ret, 0);
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,100 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_container_test.h"
#include "sys/mount.h"
static int ChildFunc(void *arg)
{
int ret;
int value = *((int *)arg);
if (value != CHILD_FUNC_ARG) {
return EXIT_CODE_ERRNO_1;
}
ret = access(ACCESS_FILE_NAME, F_OK);
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
ret = umount(USERDATA_DIR_NAME);
if (ret != 0) {
return EXIT_CODE_ERRNO_3;
}
ret = access(ACCESS_FILE_NAME, F_OK);
if (ret == 0) {
return EXIT_CODE_ERRNO_4;
}
ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, FS_TYPE, 0, nullptr);
if (ret != 0) {
return EXIT_CODE_ERRNO_5;
}
ret = umount(USERDATA_DIR_NAME);
if (ret != 0) {
return EXIT_CODE_ERRNO_6;
}
return 0;
}
/* mount container clone test: mount in parent, clone with NEW_NS, umount in child */
void ItMntContainer002(void)
{
int ret;
int status = 0;
int childReturn = 0;
int arg = CHILD_FUNC_ARG;
char *stack = nullptr;
char *stackTop = nullptr;
stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
ASSERT_NE(stack, MAP_FAILED);
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_EQ(ret, 0);
stackTop = stack + STACK_SIZE;
auto pid = clone(ChildFunc, stackTop, CLONE_NEWNS, &arg);
ASSERT_NE(pid, -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_container_test.h"
#include "sys/mount.h"
static int ChildFunc(void *arg)
{
int ret;
int value = *((int *)arg);
if (value != CHILD_FUNC_ARG) {
return EXIT_CODE_ERRNO_1;
}
ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, FS_TYPE, 0, nullptr);
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
ret = access(ACCESS_FILE_NAME, F_OK);
if (ret != 0) {
return EXIT_CODE_ERRNO_3;
}
return 0;
}
/* mount container clone test: clone without NEW_NS, mount in child */
void ItMntContainer003(void)
{
int ret;
int status = 0;
int childReturn = 0;
int arg = CHILD_FUNC_ARG;
char *stack = nullptr;
char *stackTop = nullptr;
stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
ASSERT_NE(stack, MAP_FAILED);
ret = umount(USERDATA_DIR_NAME);
ASSERT_EQ(ret, 0);
stackTop = stack + STACK_SIZE;
auto pid = clone(ChildFunc, stackTop, 0, &arg);
ASSERT_NE(pid, -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_EQ(ret, 0);
ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, FS_TYPE, 0, nullptr);
ASSERT_NE(ret, 0);
ret = umount(USERDATA_DIR_NAME);
ASSERT_EQ(ret, 0);
ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, FS_TYPE, 0, nullptr);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_container_test.h"
#include "sys/mount.h"
static int ChildFunc(void *arg)
{
int ret;
int value = *((int *)arg);
if (value != CHILD_FUNC_ARG) {
return EXIT_CODE_ERRNO_1;
}
ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, FS_TYPE, 0, nullptr);
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
ret = access(ACCESS_FILE_NAME, F_OK);
if (ret != 0) {
return EXIT_CODE_ERRNO_3;
}
return 0;
}
/* mount container clone test: clone with NEW_NS, mount in child */
void ItMntContainer004(void)
{
int ret;
int status = 0;
int childReturn = 0;
int arg = CHILD_FUNC_ARG;
char *stack = nullptr;
char *stackTop = nullptr;
stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
ASSERT_NE(stack, MAP_FAILED);
ret = umount(USERDATA_DIR_NAME);
ASSERT_EQ(ret, 0);
stackTop = stack + STACK_SIZE;
auto pid = clone(ChildFunc, stackTop, CLONE_NEWNS, &arg);
ASSERT_NE(pid, -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_NE(ret, 0);
ret = umount(USERDATA_DIR_NAME);
ASSERT_NE(ret, 0);
ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, FS_TYPE, 0, nullptr);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_container_test.h"
/*
* mount container unshare test: unshare in current pcb with NEW_NS
*/
void ItMntContainer005(void)
{
int ret;
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_EQ(ret, 0);
ret = unshare(CLONE_NEWNS);
ASSERT_EQ(ret, 0);
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_EQ(ret, 0);
ret = umount(USERDATA_DIR_NAME);
ASSERT_EQ(ret, 0);
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_NE(ret, 0);
ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, FS_TYPE, 0, nullptr);
ASSERT_EQ(ret, 0);
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_container_test.h"
#include "sys/mount.h"
static int ChildFunc(void *arg)
{
int ret;
int value = *((int *)arg);
if (value != CHILD_FUNC_ARG) {
return EXIT_CODE_ERRNO_1;
}
ret = access(ACCESS_FILE_NAME, F_OK);
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
ret = unshare(CLONE_NEWNS);
if (ret != 0) {
return EXIT_CODE_ERRNO_3;
}
ret = access(ACCESS_FILE_NAME, F_OK);
if (ret != 0) {
return EXIT_CODE_ERRNO_4;
}
ret = umount(USERDATA_DIR_NAME);
if (ret != 0) {
return EXIT_CODE_ERRNO_5;
}
ret = access(ACCESS_FILE_NAME, F_OK);
if (ret == 0) {
return EXIT_CODE_ERRNO_6;
}
return 0;
}
/*
* mount container unshare test: mount in parent, child clone without NEW_NS,
* unshare in child with NEW_NS, umount in child.
*/
void ItMntContainer006(void)
{
int ret;
int status = 0;
int childReturn = 0;
int arg = CHILD_FUNC_ARG;
char *stack = nullptr;
char *stackTop = nullptr;
stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
ASSERT_NE(stack, MAP_FAILED);
stackTop = stack + STACK_SIZE;
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_EQ(ret, 0);
auto pid = clone(ChildFunc, stackTop, 0, &arg);
ASSERT_NE(pid, -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,100 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_container_test.h"
#include "sys/mount.h"
static int ChildFunc(void *arg)
{
int ret;
int value = *((int *)arg);
if (value != CHILD_FUNC_ARG) {
return EXIT_CODE_ERRNO_1;
}
ret = unshare(CLONE_NEWNS);
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, FS_TYPE, 0, nullptr);
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
ret = access(ACCESS_FILE_NAME, F_OK);
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
return 0;
}
/*
* mount container unshare test: child clone without NEW_NS,
* unshare in child with NEW_NS, umount in child
*/
void ItMntContainer007(void)
{
int ret;
int status = 0;
int childReturn = 0;
int arg = CHILD_FUNC_ARG;
char *stack = nullptr;
char *stackTop = nullptr;
stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
ASSERT_NE(stack, MAP_FAILED);
ret = umount(USERDATA_DIR_NAME);
ASSERT_EQ(ret, 0);
stackTop = stack + STACK_SIZE;
auto pid = clone(ChildFunc, stackTop, 0, &arg);
ASSERT_NE(pid, -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_NE(ret, 0);
ret = umount(USERDATA_DIR_NAME);
ASSERT_NE(ret, 0);
ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, FS_TYPE, 0, nullptr);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_container_test.h"
const int CHILD_FUNC_EXITCODE = 3;
const int PATH_LENGTH = 100;
static int ChildFun(void *p)
{
(void)p;
const int sleepSecond = 2;
(void)getpid();
(void)sleep(sleepSecond);
return CHILD_FUNC_EXITCODE;
}
void ItMntContainer008(void)
{
pid_t parentPid;
int childPid;
void *pstk = nullptr;
int fd;
int ret;
int status;
char targetpath[PATH_LENGTH];
char old_mnt_link[PATH_LENGTH];
char new_mnt_link[PATH_LENGTH];
std::string containerType = "mnt";
int setFlag = CLONE_NEWNS;
parentPid = getpid();
pstk = malloc(STACK_SIZE);
ASSERT_NE(pstk, nullptr);
childPid = clone(ChildFun, (char *)pstk + STACK_SIZE, CLONE_NEWNS, nullptr);
ASSERT_NE(childPid, -1);
auto linkBuffer = ReadlinkContainer(parentPid, containerType.c_str());
ret = sprintf_s(old_mnt_link, PATH_LENGTH, "%s", linkBuffer.c_str());
ASSERT_GT(ret, 0);
ret = sprintf_s(targetpath, PATH_LENGTH, "/proc/%d/container/mnt", childPid);
ASSERT_GT(ret, 0);
fd = open(targetpath, O_RDONLY | O_CLOEXEC);
ASSERT_NE(fd, -1);
ret = setns(fd, setFlag);
ASSERT_EQ(ret, 0);
ret = close(fd);
ASSERT_EQ(ret, 0);
linkBuffer = ReadlinkContainer(parentPid, containerType.c_str());
ret = sprintf_s(new_mnt_link, PATH_LENGTH, "%s", linkBuffer.c_str());
ASSERT_GT(ret, 0);
ASSERT_STRNE(old_mnt_link, new_mnt_link);
ret = waitpid(childPid, &status, 0);
ASSERT_EQ(ret, childPid);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, CHILD_FUNC_EXITCODE);
}

View File

@@ -47,7 +47,7 @@ static void WriteContainer(const char *filepath, int value)
{
PrintTest("writeproc %d >> %s\n", value, filepath);
int fd = open(filepath, O_WRONLY);
ASSERT_EQ(fd, -1);
ASSERT_NE(fd, -1);
char buf[configLen];
size_t twd = sprintf_s(buf, configLen, "%d", value);
ASSERT_GT(twd, 0);
@@ -78,7 +78,7 @@ static void ReadContainer(std::string strFile, int value)
static void ErrWriteContainer0(const char *filepath)
{
int fd = open(filepath, O_WRONLY);
ASSERT_EQ(fd, -1);
ASSERT_NE(fd, -1);
char buf[configLen];
int invalidNum = 0;
size_t twd1 = sprintf_s(buf, configLen, "%d", invalidNum);
@@ -92,7 +92,7 @@ static void ErrWriteContainer0(const char *filepath)
static void ErrWriteContainer65(const char *filepath)
{
int fd = open(filepath, O_WRONLY);
ASSERT_EQ(fd, -1);
ASSERT_NE(fd, -1);
char buf[configLen];
int invalidNum = 65;
size_t twd2 = sprintf_s(buf, configLen, "%d", invalidNum);