Compare commits

...

26 Commits

Author SHA1 Message Date
openharmony_ci
ea825345c2 !1115 修复xts用例失败的问题
Merge pull request !1115 from zhangdengyu/fixXtsTestCase
2023-02-11 08:30:42 +00:00
zhangdengyu
fe6ab18cd9 feat:修复xts用例中ActsNFSTest失败的问题
1、在kernel/liteos_a/fs/proc/os_adapt/proc_vfs.c中VfsProcfsRead等接口中,
   使用spin_lock造成使用锁失败。

Close: #I6E3AU

Signed-off-by: zhangdengyu <zhangdengyu2@huawei.com>
Change-Id: I00e57d10bc4e4cf7cbc020903fe229a3aa303ae5
2023-02-11 15:09:08 +08:00
openharmony_ci
c0f5d437d8 !1112 feat: 支持setns接口
Merge pull request !1112 from zhushengle/setns
2023-02-09 12:13:51 +00:00
zhushengle
1ef64e26a1 feat: 支持setns接口
BREAKING CHANGE:
支持setns接口对外变更:
1.新增setns接口

Close #I6D9Y0

Signed-off-by: zhushengle <zhushengle@huawei.com>
Change-Id: I9aacf9e5b8463e8e6c743a0041a5b2b18fdb0e12
2023-02-09 13:52:54 +08:00
openharmony_ci
77b1b208d1 !1109 Fix : 删除冗余函数
Merge pull request !1109 from yinjiaming/fix
2023-02-08 07:04:45 +00:00
yinjiaming
d17bc9d4ea fix:删除冗余的函数
代码扫描工具发现代码中有被注释的多余函数,现删除。

Signed-off-by: yinjiaming <yinjiaming@huawei.com>
Change-Id: I26980061390924d53c77e467b2cab2f56b192a82
2023-02-07 16:46:31 +08:00
openharmony_ci
be50721826 !1108 feat: 支持pid容器内访问进程挂载目录
Merge pull request !1108 from zhushengle/proc_pid_dir
2023-02-07 04:07:53 +00:00
zhushengle
4e7d2edbe1 feat: 支持pid容器内访问进程挂载目录
Close #I6DCM8

Signed-off-by: zhushengle <zhushengle@huawei.com>
Change-Id: Ie1002ca48f9a73429e767136c1f11b1a8b137b48
2023-02-07 11:42:44 +08:00
openharmony_ci
58610ea87c !1104 Fix : 修复使用无符号数接收sprintf_s的结果
Merge pull request !1104 from yinjiaming/fix
2023-02-06 12:08:27 +00:00
openharmony_ci
c6ff38edd2 !1107 支持proc/self目录
Merge pull request !1107 from zhushengle/proc_self
2023-02-06 10:41:45 +00:00
zhushengle
1c18c48b1e feat: 支持proc/self目录
BREAKING CHANGE:
支持proc/self目录对外变更:
1.支持/proc/self挂载目录,用于获取进程信息

Close #I6CZ72

Signed-off-by: zhushengle <zhushengle@huawei.com>
Change-Id: I465278785bd9e27ddbf4d34ee7b5457e39f98b5b
2023-02-06 14:45:59 +08:00
openharmony_ci
bea394a75e !1103 fuzz测试用例
Merge pull request !1103 from 权飞燕/fuzz-master
2023-02-06 03:19:54 +00:00
gaorui
272c490618 增加fuzz测试用例
Change-Id: Ib07c89fe4fb840f2be4eec40a379a8f13e5ef2b6
Signed-off-by: gaorui <gaorui23@huawei.com>
2023-02-03 19:12:17 +08:00
openharmony_ci
dd041bef5a !1106 fix: 修复容器相关问题
Merge pull request !1106 from zhushengle/mem_free
2023-02-03 05:54:19 +00:00
zhushengle
fd925a8163 fix: 修复容器内存泄露问题
1.修复内存泄露
2.修复获取内核信息单位、描述错误

Close #I6CD5C
Signed-off-by: zhushengle <zhushengle@huawei.com>
Change-Id: I875bec616064517b21af8e69ef8d6e177ec01ce1
2023-02-03 12:21:26 +08:00
openharmony_ci
e8e21fcc3e !1091 LMS特性安全合规整改
Merge pull request !1091 from JerryH/lms
2023-02-02 01:42:15 +00:00
huangjieliang
c592fe73ec feat:LMS模块及堆内存模块非安全函数安全合规整改
Close #I68LEO

Signed-off-by: huangjieliang <huangjieliang@huawei.com>
Change-Id: I6f1a8e647220170b00f771117bb018e5f82997ed
2023-02-01 16:50:11 +08:00
yinjiaming
4174654bdd fix:修复使用无符号数接收sprintf_s的结果的问题
sprintf_s的返回值是有符号数,应使用有符号数来接收返回值

Signed-off-by: yinjiaming <yinjiaming@huawei.com>
Change-Id: Icb1248e0a75a77c1258f64dfa25eef0a7f5dd14e
2023-02-01 10:45:20 +08:00
openharmony_ci
f3a7a9c602 !1102 fix: 修复proc静态问题
Merge pull request !1102 from zhushengle/proc_fix
2023-01-31 02:13:26 +00:00
zhushengle
c8dbdaeb12 fix: 修复proc静态问题
1.proc存在静态问题
2.删除proc节点后,vnode未同步删除,导致系统崩溃

Close #I6BT1F

Signed-off-by: zhushengle <zhushengle@huawei.com>
Change-Id: I91deecfe1d055a4ad1c2d2cda65acf04e4c53d4c
2023-01-30 19:36:51 +08:00
openharmony_ci
1f05c6a24d !1101 feat: 支持unshare
Merge pull request !1101 from zhushengle/unshare
2023-01-30 08:41:37 +00:00
zhushengle
325b4a49e4 feta: 支持unshare接口
BREAKING CHANGE:
支持unshare接口对外变更
1.支持unshare接口,flags支持:CLONE_NEWPID, CLONE_NEWTIME, CLONE_NEWUTS

Close #I6BE5A

Signed-off-by: zhushengle <zhushengle@huawei.com>
Change-Id: Ib61abad2fa03a7100bf808e93830f2094fa1c5a6
2023-01-30 10:30:54 +08:00
openharmony_ci
1d1f1b06c3 !1100 支持time容器
Merge pull request !1100 from zhushengle/time_container
2023-01-19 07:48:27 +00:00
zhushengle
16ed05e844 feat: 支持time容器
BREAKING CHANGE:
支持ipc容器及增强对外变更:
1.clone 支持CLONE_NEWTIME
2.增加”/proc/[pid]/container/time" 用于查询容器信息
3.增加”/proc/[pid]/container/time_for_children" 用于查询容器信息
4.增加”/proc/[pid]/container/pid_for_children" 用于查询容器信息
5.增加”/proc/[pid]/time_offsets" 用于查询和配置time容器信息

Close #I6B0A3

Signed-off-by: zhushengle <zhushengle@huawei.com>
Change-Id: I54d79937ca608a10a4384f61e11c88757f833edf
2023-01-19 15:03:18 +08:00
openharmony_ci
7e0dfb79f7 !1099 feat: 支持IPC容器
Merge pull request !1099 from zhushengle/ipc_container
2023-01-18 06:58:09 +00:00
zhushengle
34814c58a3 feat: 支持IPC容器
BREAKING CHANGE:
支持ipc容器及增强对外变更:
1.clone 支持CLONE_NEWIPC
2.增加”/proc/[pid]/container/ipc" 用于查询容器信息

Close #I6AVMY

Signed-off-by: zhushengle <zhushengle@huawei.com>
Change-Id: I6a3c248d2d66a5342994c6e0b0aecddea8e32c72
2023-01-18 10:59:25 +08:00
159 changed files with 9089 additions and 640 deletions

View File

@@ -34,6 +34,7 @@ LITEOS_MENUCONFIG_H = rebase_path("$root_out_dir/config.h")
declare_args() {
tee_enable = false
liteos_name = "OHOS_Image"
liteos_container_enable = false
liteos_skip_make = false
liteos_is_mini = false
}
@@ -239,6 +240,21 @@ config("misc_config") {
asmflags = cflags
}
config("container_config") {
if (liteos_container_enable) {
cflags = [
"-DLOSCFG_KERNEL_CONTAINER",
"-DLOSCFG_PID_CONTAINER",
"-DLOSCFG_UTS_CONTAINER",
"-DLOSCFG_MNT_CONTAINER",
"-DLOSCFG_CHROOT",
"-DLOSCFG_IPC_CONTAINER",
"-DLOSCFG_TIME_CONTAINER",
"-DLOSCFG_PROC_PROCESS_DIR",
]
}
}
config("los_config") {
configs = [
":arch_config",
@@ -249,6 +265,7 @@ config("los_config") {
":ssp_config",
":warn_config",
":misc_config",
":container_config",
]
}

View File

@@ -58,6 +58,10 @@ APP_SUBDIRS += mksh
APP_SUBDIRS += toybox
endif
ifeq ($(LOSCFG_KERNEL_LMS), y)
APP_SUBDIRS += lms
endif
ifeq ($(LOSCFG_USER_INIT_DEBUG), y)
APP_SUBDIRS += init
endif

View File

@@ -231,7 +231,7 @@ int main(int argc, char * const *argv)
printf("\n############### Lms Test start ###############\n");
char *tmp = (char *)malloc(5000); /* 5000: test mem size */
if (tmp == NULL) {
return;
return -1;
}
LmsMallocTest();
LmsReallocTest();
@@ -246,4 +246,5 @@ int main(int argc, char * const *argv)
LmsFreeTest();
free(tmp);
printf("\n############### Lms Test End ###############\n");
return 0;
}

View File

@@ -1,6 +1,6 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
* Copyright (c) 2020-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:
@@ -74,6 +74,10 @@ extern "C" {
/* not support prio */
#define MQ_PRIO_MAX 1
#ifndef MAX_MQ_FD
#define MAX_MQ_FD CONFIG_NQUEUE_DESCRIPTORS
#endif
typedef union send_receive_t {
unsigned oth : 3;
unsigned grp : 6;
@@ -424,6 +428,7 @@ extern ssize_t mq_timedreceive(mqd_t personal, char *msg, size_t msgLen,
extern void MqueueRefer(int sysFd);
extern int OsMqNotify(mqd_t personal, const struct sigevent *sigev);
extern VOID OsMqueueCBDestroy(struct mqarray *queueTable);
#ifdef __cplusplus
#if __cplusplus

View File

@@ -1,6 +1,6 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
* Copyright (c) 2020-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:
@@ -44,16 +44,19 @@
#define FNONBLOCK O_NONBLOCK
#ifndef MAX_MQ_FD
#define MAX_MQ_FD CONFIG_NQUEUE_DESCRIPTORS
#endif
#ifndef LOSCFG_IPC_CONTAINER
/* GLOBALS */
STATIC fd_set g_queueFdSet;
STATIC struct mqarray g_queueTable[LOSCFG_BASE_IPC_QUEUE_LIMIT];
STATIC pthread_mutex_t g_mqueueMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
STATIC struct mqpersonal *g_mqPrivBuf[MAX_MQ_FD];
#define IPC_QUEUE_FD_SET g_queueFdSet
#define IPC_QUEUE_TABLE g_queueTable
#define IPC_QUEUE_MUTEX g_mqueueMutex
#define IPC_QUEUE_MQ_PRIV_BUF g_mqPrivBuf
#endif
/* LOCAL FUNCTIONS */
STATIC INLINE INT32 MqNameCheck(const CHAR *mqName)
{
@@ -96,12 +99,12 @@ STATIC INLINE struct mqarray *GetMqueueCBByName(const CHAR *name)
UINT32 mylen = strlen(name);
for (index = 0; index < LOSCFG_BASE_IPC_QUEUE_LIMIT; index++) {
if ((g_queueTable[index].mq_name == NULL) || (strlen(g_queueTable[index].mq_name) != mylen)) {
if ((IPC_QUEUE_TABLE[index].mq_name == NULL) || (strlen(IPC_QUEUE_TABLE[index].mq_name) != mylen)) {
continue;
}
if (strncmp(name, (const CHAR *)(g_queueTable[index].mq_name), mylen) == 0) {
return &(g_queueTable[index]);
if (strncmp(name, (const CHAR *)(IPC_QUEUE_TABLE[index].mq_name), mylen) == 0) {
return &(IPC_QUEUE_TABLE[index]);
}
}
return NULL;
@@ -170,8 +173,8 @@ STATIC struct mqpersonal *DoMqueueCreate(const struct mq_attr *attr, const CHAR
goto ERROUT;
}
if (g_queueTable[GET_QUEUE_INDEX(mqueueID)].mqcb == NULL) {
mqueueCB = &(g_queueTable[GET_QUEUE_INDEX(mqueueID)]);
if (IPC_QUEUE_TABLE[GET_QUEUE_INDEX(mqueueID)].mqcb == NULL) {
mqueueCB = &(IPC_QUEUE_TABLE[GET_QUEUE_INDEX(mqueueID)]);
mqueueCB->mq_id = mqueueID;
}
@@ -297,7 +300,7 @@ STATIC struct mqpersonal *MqGetPrivDataBuff(mqd_t personal)
errno = EBADF;
return NULL;
}
return g_mqPrivBuf[id];
return IPC_QUEUE_MQ_PRIV_BUF[id];
}
/**
@@ -311,13 +314,13 @@ STATIC struct mqpersonal *MqGetPrivDataBuff(mqd_t personal)
STATIC INT32 MqAllocSysFd(int maxfdp, struct mqpersonal *privateMqPersonal)
{
INT32 i;
fd_set *fdset = &g_queueFdSet;
fd_set *fdset = &IPC_QUEUE_FD_SET;
for (i = 0; i < maxfdp; i++) {
/* sysFd: used bit setting, and get the index of swtmrID buffer */
if (fdset && !(FD_ISSET(i + MQUEUE_FD_OFFSET, fdset))) {
FD_SET(i + MQUEUE_FD_OFFSET, fdset);
if (!g_mqPrivBuf[i]) {
g_mqPrivBuf[i] = privateMqPersonal;
if (!IPC_QUEUE_MQ_PRIV_BUF[i]) {
IPC_QUEUE_MQ_PRIV_BUF[i] = privateMqPersonal;
return i + MQUEUE_FD_OFFSET;
}
}
@@ -328,10 +331,10 @@ STATIC INT32 MqAllocSysFd(int maxfdp, struct mqpersonal *privateMqPersonal)
STATIC VOID MqFreeSysFd(mqd_t personal)
{
INT32 sysFd = (INT32)personal;
fd_set *fdset = &g_queueFdSet;
fd_set *fdset = &IPC_QUEUE_FD_SET;
if (fdset && FD_ISSET(sysFd, fdset)) {
FD_CLR(sysFd, fdset);
g_mqPrivBuf[sysFd - MQUEUE_FD_OFFSET] = NULL;
IPC_QUEUE_MQ_PRIV_BUF[sysFd - MQUEUE_FD_OFFSET] = NULL;
}
}
@@ -341,7 +344,7 @@ void MqueueRefer(int sysFd)
struct mqarray *mqueueCB = NULL;
struct mqpersonal *privateMqPersonal = NULL;
(VOID)pthread_mutex_lock(&g_mqueueMutex);
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
/* Get the personal sysFd and reset personal fd -1 */
privateMqPersonal = MqGetPrivDataBuff((mqd_t)sysFd);
if (privateMqPersonal == NULL) {
@@ -354,7 +357,7 @@ void MqueueRefer(int sysFd)
privateMqPersonal->mq_refcount++;
OUT_UNLOCK:
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
return;
}
@@ -498,7 +501,7 @@ mqd_t mq_open(const char *mqName, int openFlag, ...)
return (mqd_t)-1;
}
(VOID)pthread_mutex_lock(&g_mqueueMutex);
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
mqueueCB = GetMqueueCBByName(mqName);
if ((UINT32)openFlag & (UINT32)O_CREAT) {
if (mqueueCB != NULL) {
@@ -543,7 +546,7 @@ mqd_t mq_open(const char *mqName, int openFlag, ...)
mqFd = (mqd_t)sysFd;
}
OUT:
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
return mqFd;
}
@@ -552,7 +555,7 @@ int mq_close(mqd_t personal)
INT32 ret = -1;
struct mqpersonal *privateMqPersonal = NULL;
(VOID)pthread_mutex_lock(&g_mqueueMutex);
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
/* Get the personal sysFd and reset personal fd -1 */
privateMqPersonal = MqGetPrivDataBuff(personal);
@@ -577,7 +580,7 @@ int mq_close(mqd_t personal)
MqFreeSysFd(personal);
OUT_UNLOCK:
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
return ret;
}
@@ -596,10 +599,10 @@ int OsMqGetAttr(mqd_t personal, struct mq_attr *mqAttr)
return -1;
}
(VOID)pthread_mutex_lock(&g_mqueueMutex);
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
if (privateMqPersonal->mq_status != MQ_USE_MAGIC) {
errno = EBADF;
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
return -1;
}
@@ -608,7 +611,7 @@ int OsMqGetAttr(mqd_t personal, struct mq_attr *mqAttr)
mqAttr->mq_msgsize = mqueueCB->mqcb->queueSize - sizeof(UINT32);
mqAttr->mq_curmsgs = mqueueCB->mqcb->readWriteableCnt[OS_QUEUE_READ];
mqAttr->mq_flags = privateMqPersonal->mq_flags;
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
return 0;
}
@@ -626,10 +629,10 @@ int OsMqSetAttr(mqd_t personal, const struct mq_attr *mqSetAttr, struct mq_attr
return -1;
}
(VOID)pthread_mutex_lock(&g_mqueueMutex);
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
if (privateMqPersonal->mq_status != MQ_USE_MAGIC) {
errno = EBADF;
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
return -1;
}
@@ -641,7 +644,7 @@ int OsMqSetAttr(mqd_t personal, const struct mq_attr *mqSetAttr, struct mq_attr
if (((UINT32)mqSetAttr->mq_flags & (UINT32)FNONBLOCK) == (UINT32)FNONBLOCK) {
privateMqPersonal->mq_flags = (INT32)((UINT32)privateMqPersonal->mq_flags | (UINT32)FNONBLOCK);
}
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
return 0;
}
@@ -662,7 +665,7 @@ int mq_unlink(const char *mqName)
return -1;
}
(VOID)pthread_mutex_lock(&g_mqueueMutex);
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
mqueueCB = GetMqueueCBByName(mqName);
if (mqueueCB == NULL) {
errno = ENOENT;
@@ -675,11 +678,11 @@ int mq_unlink(const char *mqName)
ret = DoMqueueDelete(mqueueCB);
}
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
return ret;
ERROUT_UNLOCK:
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
return -1;
}
@@ -767,7 +770,7 @@ int mq_timedsend(mqd_t personal, const char *msg, size_t msgLen, unsigned int ms
OS_MQ_GOTO_ERROUT_IF(!MqParamCheck(personal, msg, msgLen), errno);
OS_MQ_GOTO_ERROUT_IF(msgPrio > (MQ_PRIO_MAX - 1), EINVAL);
(VOID)pthread_mutex_lock(&g_mqueueMutex);
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
privateMqPersonal = MqGetPrivDataBuff(personal);
OS_MQ_GOTO_ERROUT_UNLOCK_IF(privateMqPersonal == NULL || privateMqPersonal->mq_status != MQ_USE_MAGIC, EBADF);
@@ -781,7 +784,7 @@ int mq_timedsend(mqd_t personal, const char *msg, size_t msgLen, unsigned int ms
OS_MQ_GOTO_ERROUT_UNLOCK_IF(ConvertTimeout(privateMqPersonal->mq_flags, absTimeout, &absTicks) == -1, errno);
mqueueID = mqueueCB->mq_id;
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
if (LOS_ListEmpty(&mqueueCB->mqcb->readWriteList[OS_QUEUE_READ])) {
MqSendNotify(mqueueCB);
@@ -793,7 +796,7 @@ int mq_timedsend(mqd_t personal, const char *msg, size_t msgLen, unsigned int ms
}
return 0;
ERROUT_UNLOCK:
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
ERROUT:
return -1;
}
@@ -815,7 +818,7 @@ ssize_t mq_timedreceive(mqd_t personal, char *msg, size_t msgLen, unsigned int *
*msgPrio = 0;
}
(VOID)pthread_mutex_lock(&g_mqueueMutex);
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
privateMqPersonal = MqGetPrivDataBuff(personal);
if (privateMqPersonal == NULL || privateMqPersonal->mq_status != MQ_USE_MAGIC) {
errno = EBADF;
@@ -839,7 +842,7 @@ ssize_t mq_timedreceive(mqd_t personal, char *msg, size_t msgLen, unsigned int *
receiveLen = msgLen;
mqueueID = mqueueCB->mq_id;
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
err = LOS_QueueReadCopy(mqueueID, (VOID *)msg, &receiveLen, (UINT32)absTicks);
if (map_errno(err) == ENOERR) {
@@ -849,7 +852,7 @@ ssize_t mq_timedreceive(mqd_t personal, char *msg, size_t msgLen, unsigned int *
}
ERROUT_UNLOCK:
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
ERROUT:
return -1;
}
@@ -898,7 +901,7 @@ int OsMqNotify(mqd_t personal, const struct sigevent *sigev)
goto ERROUT;
}
(VOID)pthread_mutex_lock(&g_mqueueMutex);
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
privateMqPersonal = MqGetPrivDataBuff(personal);
if (privateMqPersonal == NULL) {
goto OUT_UNLOCK;
@@ -936,11 +939,26 @@ int OsMqNotify(mqd_t personal, const struct sigevent *sigev)
mqnotify->pid = LOS_GetCurrProcessID();
}
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
return 0;
OUT_UNLOCK:
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
ERROUT:
return -1;
}
VOID OsMqueueCBDestroy(struct mqarray *queueTable)
{
if (queueTable == NULL) {
return;
}
for (UINT32 index = 0; index < LOSCFG_BASE_IPC_QUEUE_LIMIT; index++) {
struct mqarray *mqueueCB = &(queueTable[index]);
if (mqueueCB->mq_name == NULL) {
continue;
}
(VOID)DoMqueueClose(mqueueCB->mq_personal);
}
}
#endif

View File

@@ -599,13 +599,22 @@ int clock_gettime(clockid_t clockID, struct timespec *tp)
switch (clockID) {
case CLOCK_MONOTONIC_RAW:
#ifdef LOSCFG_TIME_CONTAINER
tmp = OsTimeSpecAdd(hwTime, CLOCK_MONOTONIC_TIME_BASE);
tp->tv_sec = tmp.tv_sec;
tp->tv_nsec = tmp.tv_nsec;
#else
tp->tv_sec = hwTime.tv_sec;
tp->tv_nsec = hwTime.tv_nsec;
#endif
break;
case CLOCK_MONOTONIC:
LOS_SpinLockSave(&g_timeSpin, &intSave);
tmp = OsTimeSpecAdd(hwTime, g_accDeltaFromAdj);
LOS_SpinUnlockRestore(&g_timeSpin, intSave);
#ifdef LOSCFG_TIME_CONTAINER
tmp = OsTimeSpecAdd(tmp, CLOCK_MONOTONIC_TIME_BASE);
#endif
tp->tv_sec = tmp.tv_sec;
tp->tv_nsec = tmp.tv_nsec;
break;
@@ -639,7 +648,7 @@ int clock_gettime(clockid_t clockID, struct timespec *tp)
return 0;
ERROUT:
ERROUT:
TIME_RETURN(EINVAL);
}

View File

@@ -77,6 +77,10 @@ void ProcFsCacheInit(void);
void ProcFdInit(void);
#ifdef LOSCFG_KERNEL_CONTAINER
void *ProcfsContainerGet(int fd, unsigned int *containerType);
#endif
#ifdef __cplusplus
#if __cplusplus
}

View File

@@ -297,6 +297,7 @@ extern struct ProcDirEntry *ProcCreateData(const char *name, mode_t mode, struct
*/
extern void ProcFsInit(void);
extern struct ProcDirEntry *VnodeToEntry(struct Vnode *node);
#ifdef __cplusplus
#if __cplusplus
}

View File

@@ -42,13 +42,13 @@ static int SysMemInfoFill(struct SeqBuf *seqBuf, void *arg)
if (LOS_MemInfoGet(m_aucSysMem0, &mem) == LOS_NOK) {
return -EBADF;
}
(void)LosBufPrintf(seqBuf, "\nUsedSize:%25u KB\n", mem.totalUsedSize);
(void)LosBufPrintf(seqBuf, "FreeSize:%25u KB\n", mem.totalFreeSize);
(void)LosBufPrintf(seqBuf, "MaxFreeNodeSize:%18u KB\n", mem.maxFreeNodeSize);
(void)LosBufPrintf(seqBuf, "UsedNodeNum:%22u KB\n", mem.usedNodeNum);
(void)LosBufPrintf(seqBuf, "FreeNodeNum:%22u KB\n", mem.freeNodeNum);
(void)LosBufPrintf(seqBuf, "\nUsedSize: %u byte\n", mem.totalUsedSize);
(void)LosBufPrintf(seqBuf, "FreeSize: %u byte\n", mem.totalFreeSize);
(void)LosBufPrintf(seqBuf, "MaxFreeNodeSize: %u byte\n", mem.maxFreeNodeSize);
(void)LosBufPrintf(seqBuf, "UsedNodeNum: %u\n", mem.usedNodeNum);
(void)LosBufPrintf(seqBuf, "FreeNodeNum: %u\n", mem.freeNodeNum);
#ifdef LOSCFG_MEM_WATERLINE
(void)LosBufPrintf(seqBuf, "UsageWaterLine:%19u KB\n", mem.freeNodeNum);
(void)LosBufPrintf(seqBuf, "UsageWaterLine: %u byte\n", mem.usageWaterLine);
#endif
return 0;
}

View File

@@ -45,7 +45,7 @@
static struct VnodeOps g_procfsVops;
static struct file_operations_vfs g_procfsFops;
static struct ProcDirEntry *VnodeToEntry(struct Vnode *node)
struct ProcDirEntry *VnodeToEntry(struct Vnode *node)
{
return (struct ProcDirEntry *)(node->data);
}
@@ -125,10 +125,16 @@ int VfsProcfsRead(struct file *filep, char *buffer, size_t buflen)
return -EINVAL;
}
VnodeHold();
entry = VnodeToEntry(filep->f_vnode);
if (entry == NULL) {
VnodeDrop();
return -EPERM;
}
size = (ssize_t)ReadProcFile(entry, (void *)buffer, buflen);
filep->f_pos = entry->pf->fPos;
VnodeDrop();
return size;
}
@@ -140,10 +146,16 @@ int VfsProcfsWrite(struct file *filep, const char *buffer, size_t buflen)
return -EINVAL;
}
VnodeHold();
entry = VnodeToEntry(filep->f_vnode);
if (entry == NULL) {
VnodeDrop();
return -EPERM;
}
size = (ssize_t)WriteProcFile(entry, (void *)buffer, buflen);
filep->f_pos = entry->pf->fPos;
VnodeDrop();
return size;
}
@@ -156,6 +168,7 @@ int VfsProcfsLookup(struct Vnode *parent, const char *name, int len, struct Vnod
if (entry == NULL) {
return -ENODATA;
}
entry = entry->subdir;
while (1) {
if (entry == NULL) {
@@ -214,11 +227,15 @@ int VfsProcfsUnmount(void *handle, struct Vnode **blkdriver)
int VfsProcfsStat(struct Vnode *node, struct stat *buf)
{
VnodeHold();
struct ProcDirEntry *entry = VnodeToEntry(node);
if (entry == NULL) {
VnodeDrop();
return -EPERM;
}
(void)memset_s(buf, sizeof(struct stat), 0, sizeof(struct stat));
buf->st_mode = entry->mode;
VnodeDrop();
return LOS_OK;
}
@@ -226,9 +243,7 @@ int VfsProcfsReaddir(struct Vnode *node, struct fs_dirent_s *dir)
{
int result;
char *buffer = NULL;
int buflen = NAME_MAX;
unsigned int min_size;
unsigned int dst_name_size;
unsigned int minSize, dstNameSize;
struct ProcDirEntry *pde = NULL;
int i = 0;
@@ -238,28 +253,35 @@ int VfsProcfsReaddir(struct Vnode *node, struct fs_dirent_s *dir)
if (node->type != VNODE_TYPE_DIR) {
return -ENOTDIR;
}
VnodeHold();
pde = VnodeToEntry(node);
if (pde == NULL) {
VnodeDrop();
return -EPERM;
}
while (i < dir->read_cnt) {
buffer = (char *)zalloc(sizeof(char) * NAME_MAX);
if (buffer == NULL) {
VnodeDrop();
PRINT_ERR("malloc failed\n");
return -ENOMEM;
}
result = ReadProcFile(pde, (void *)buffer, buflen);
result = ReadProcFile(pde, (void *)buffer, NAME_MAX);
if (result != ENOERR) {
free(buffer);
break;
}
dst_name_size = sizeof(dir->fd_dir[i].d_name);
min_size = (dst_name_size < NAME_MAX) ? dst_name_size : NAME_MAX;
result = strncpy_s(dir->fd_dir[i].d_name, dst_name_size, buffer, min_size);
dstNameSize = sizeof(dir->fd_dir[i].d_name);
minSize = (dstNameSize < NAME_MAX) ? dstNameSize : NAME_MAX;
result = strncpy_s(dir->fd_dir[i].d_name, dstNameSize, buffer, minSize);
if (result != EOK) {
VnodeDrop();
free(buffer);
return -ENAMETOOLONG;
}
dir->fd_dir[i].d_name[dst_name_size - 1] = '\0';
dir->fd_dir[i].d_name[dstNameSize - 1] = '\0';
dir->fd_position++;
dir->fd_dir[i].d_off = dir->fd_position;
dir->fd_dir[i].d_reclen = (uint16_t)sizeof(struct dirent);
@@ -267,21 +289,26 @@ int VfsProcfsReaddir(struct Vnode *node, struct fs_dirent_s *dir)
i++;
free(buffer);
}
VnodeDrop();
return i;
}
int VfsProcfsOpendir(struct Vnode *node, struct fs_dirent_s *dir)
{
VnodeHold();
struct ProcDirEntry *pde = VnodeToEntry(node);
if (pde == NULL) {
VnodeDrop();
return -EINVAL;
}
pde->pdirCurrent = pde->subdir;
if (pde->pf == NULL) {
VnodeDrop();
return -EINVAL;
}
pde->pf->fPos = 0;
VnodeDrop();
return LOS_OK;
}
@@ -290,8 +317,14 @@ int VfsProcfsOpen(struct file *filep)
if (filep == NULL) {
return -EINVAL;
}
VnodeHold();
struct Vnode *node = filep->f_vnode;
struct ProcDirEntry *pde = VnodeToEntry(node);
if (pde == NULL) {
VnodeDrop();
return -EPERM;
}
if (ProcOpen(pde->pf) != OK) {
return -ENOMEM;
}
@@ -303,6 +336,7 @@ int VfsProcfsOpen(struct file *filep)
pde->pf->fPos = 0;
}
filep->f_priv = (void *)pde;
VnodeDrop();
return LOS_OK;
}
@@ -312,15 +346,22 @@ int VfsProcfsClose(struct file *filep)
if (filep == NULL) {
return -EINVAL;
}
VnodeHold();
struct Vnode *node = filep->f_vnode;
struct ProcDirEntry *pde = VnodeToEntry(node);
if (pde == NULL) {
VnodeDrop();
return -EPERM;
}
pde->pf->fPos = 0;
if ((pde->procFileOps != NULL) && (pde->procFileOps->release != NULL)) {
result = pde->procFileOps->release((struct Vnode *)pde, pde->pf);
}
LosBufRelease(pde->pf->sbuf);
pde->pf->sbuf = NULL;
VnodeDrop();
return result;
}
@@ -345,6 +386,10 @@ ssize_t VfsProcfsReadlink(struct Vnode *vnode, char *buffer, size_t bufLen)
}
struct ProcDirEntry *pde = VnodeToEntry(vnode);
if (pde == NULL) {
return -EPERM;
}
if ((pde->procFileOps != NULL) && (pde->procFileOps->readLink != NULL)) {
result = pde->procFileOps->readLink(pde, buffer, bufLen);
}

View File

@@ -34,6 +34,8 @@
#include "proc_fs.h"
#include "internal.h"
#include "los_process_pri.h"
#include "user_copy.h"
#include "los_memory.h"
#ifdef LOSCFG_PROC_PROCESS_DIR
#include "los_vm_dump.h"
@@ -58,18 +60,30 @@ struct ProcessData {
unsigned int type;
};
static LosProcessCB *ProcGetProcessCB(struct ProcessData *data)
{
if (data->process != 0) {
return (LosProcessCB *)data->process;
}
return OsCurrProcessGet();
}
#define PROC_PID_PRIVILEGE 7
#define PROC_PID_DIR_LEN 100
#ifdef LOSCFG_KERNEL_CONTAINER
static ssize_t ProcessContainerLink(unsigned int containerID, ContainerType type, char *buffer, size_t bufLen)
{
ssize_t count = -1;
if (type == PID_CONTAINER) {
if ((type == PID_CONTAINER) || (type == PID_CHILD_CONTAINER)) {
count = snprintf_s(buffer, bufLen, bufLen - 1, "'pid:[%u]'", containerID);
} else if (type == UTS_CONTAINER) {
count = snprintf_s(buffer, bufLen, bufLen - 1, "'uts:[%u]'", containerID);
} else if (type == MNT_CONTAINER) {
count = snprintf_s(buffer, bufLen, bufLen - 1, "'mnt:[%u]'", containerID);
} else if (type == IPC_CONTAINER) {
count = snprintf_s(buffer, bufLen, bufLen - 1, "'ipc:[%u]'", containerID);
} else if ((type == TIME_CONTAINER) || (type == TIME_CHILD_CONTAINER)) {
count = snprintf_s(buffer, bufLen, bufLen - 1, "'time:[%u]'", containerID);
}
if (count < 0) {
@@ -89,7 +103,7 @@ static ssize_t ProcessContainerReadLink(struct ProcDirEntry *entry, char *buffer
if (data == NULL) {
return -EINVAL;
}
LosProcessCB *processCB = (LosProcessCB *)data->process;
LosProcessCB *processCB = ProcGetProcessCB(data);
SCHEDULER_LOCK(intSave);
UINT32 containerID = OsGetContainerID(processCB->container, (ContainerType)data->type);
SCHEDULER_UNLOCK(intSave);
@@ -106,6 +120,38 @@ static ssize_t ProcessContainerReadLink(struct ProcDirEntry *entry, char *buffer
static const struct ProcFileOperations PID_CONTAINER_FOPS = {
.readLink = ProcessContainerReadLink,
};
void *ProcfsContainerGet(int fd, unsigned int *containerType)
{
if ((fd <= 0) || (containerType == NULL)) {
return NULL;
}
VnodeHold();
struct Vnode *vnode = VnodeFind(fd);
if (vnode == NULL) {
VnodeDrop();
return NULL;
}
struct ProcDirEntry *entry = VnodeToEntry(vnode);
if (entry == NULL) {
VnodeDrop();
return NULL;
}
struct ProcessData *data = (struct ProcessData *)entry->data;
if (data == NULL) {
VnodeDrop();
return NULL;
}
void *processCB = (void *)ProcGetProcessCB(data);
*containerType = data->type;
VnodeDrop();
return processCB;
}
#endif /* LOSCFG_KERNEL_CONTAINER */
static int ProcessMemInfoRead(struct SeqBuf *seqBuf, LosProcessCB *pcb)
@@ -129,19 +175,18 @@ static int ProcessMemInfoRead(struct SeqBuf *seqBuf, LosProcessCB *pcb)
(void)memcpy_s(heap, sizeof(LosVmMapRegion), pcb->vmSpace->heap, sizeof(LosVmMapRegion));
SCHEDULER_UNLOCK(intSave);
(void)LosBufPrintf(seqBuf, "\nVMSpaceSize: %u KB\n", vmSpace->size);
(void)LosBufPrintf(seqBuf, "VMRegionSize: %u KB\n", heap->range.size);
(void)LosBufPrintf(seqBuf, "RegionFlags: %s\n", OsGetRegionNameOrFilePath(heap));
(void)LosBufPrintf(seqBuf, "ShmidAboutSharedRegion: %u\n", heap->shmid);
(void)LosBufPrintf(seqBuf, "VMSpaceRorkFlags: 0x%x\n", heap->forkFlags);
(void)LosBufPrintf(seqBuf, "VMRegionRype: 0x%x\n", heap->regionType);
(void)LosBufPrintf(seqBuf, "VMSpaceMappingAreaSize: %u KB\n", vmSpace->mapSize);
(void)LosBufPrintf(seqBuf, "TLB Asid: %u\n", vmSpace->archMmu.asid);
(void)LosBufPrintf(seqBuf, "\nVMSpaceSize: %u byte\n", vmSpace->size);
(void)LosBufPrintf(seqBuf, "VMSpaceMapSize: %u byte\n", vmSpace->mapSize);
(void)LosBufPrintf(seqBuf, "VM TLB Asid: %u\n", vmSpace->archMmu.asid);
(void)LosBufPrintf(seqBuf, "VMHeapSize: %u byte\n", heap->range.size);
(void)LosBufPrintf(seqBuf, "VMHeapRegionName: %s\n", OsGetRegionNameOrFilePath(heap));
(void)LosBufPrintf(seqBuf, "VMHeapRegionType: 0x%x\n", heap->regionType);
(void)LOS_MemFree(m_aucSysMem1, vmSpace);
return 0;
}
#ifdef LOSCFG_KERNEL_CPUP
#define TIME_CYCLE_TO_US(time) ((((UINT64)time) * OS_NS_PER_CYCLE) / OS_SYS_NS_PER_US)
static int ProcessCpupRead(struct SeqBuf *seqBuf, LosProcessCB *pcb)
{
unsigned int intSave;
@@ -160,11 +205,10 @@ static int ProcessCpupRead(struct SeqBuf *seqBuf, LosProcessCB *pcb)
(void)memcpy_s(processCpup, sizeof(OsCpupBase), pcb->processCpup, sizeof(OsCpupBase));
SCHEDULER_UNLOCK(intSave);
(void)LosBufPrintf(seqBuf, "\nTotalRunningTime: %lu\n", processCpup->allTime);
(void)LosBufPrintf(seqBuf, "StartTime: %lu\n", processCpup->startTime);
(void)LosBufPrintf(seqBuf, "HistoricalRunningTime: ");
(void)LosBufPrintf(seqBuf, "\nTotalRunningTime: %lu us\n", TIME_CYCLE_TO_US(processCpup->allTime));
(void)LosBufPrintf(seqBuf, "HistoricalRunningTime:(us) ");
for (UINT32 i = 0; i < OS_CPUP_HISTORY_RECORD_NUM + 1; i++) {
(void)LosBufPrintf(seqBuf, "%lu ", processCpup->historyTime[i]);
(void)LosBufPrintf(seqBuf, "%lu ", TIME_CYCLE_TO_US(processCpup->historyTime[i]));
}
(void)LosBufPrintf(seqBuf, "\n");
(void)LOS_MemFree(m_aucSysMem1, processCpup);
@@ -172,6 +216,110 @@ static int ProcessCpupRead(struct SeqBuf *seqBuf, LosProcessCB *pcb)
}
#endif
#ifdef LOSCFG_TIME_CONTAINER
static const CHAR *g_monotonic = "monotonic";
#define DECIMAL_BASE 10
static int ProcTimeContainerRead(struct SeqBuf *m, void *v)
{
int ret;
unsigned int intSave;
struct timespec64 offsets = {0};
if ((m == NULL) || (v == NULL)) {
return -EINVAL;
}
struct ProcessData *data = (struct ProcessData *)v;
SCHEDULER_LOCK(intSave);
LosProcessCB *processCB = ProcGetProcessCB(data);
ret = OsGetTimeContainerMonotonic(processCB, &offsets);
SCHEDULER_UNLOCK(intSave);
if (ret != LOS_OK) {
return -ret;
}
LosBufPrintf(m, "monotonic %lld %ld\n", offsets.tv_sec, offsets.tv_nsec);
return 0;
}
static int ProcSetTimensOffset(const char *buf, LosProcessCB *processCB)
{
unsigned int intSave;
struct timespec64 offsets;
char *endptr = NULL;
offsets.tv_sec = strtoll(buf, &endptr, DECIMAL_BASE);
offsets.tv_nsec = strtoll(endptr, NULL, DECIMAL_BASE);
if (offsets.tv_nsec >= OS_SYS_NS_PER_SECOND) {
return -EACCES;
}
SCHEDULER_LOCK(intSave);
unsigned int ret = OsSetTimeContainerMonotonic(processCB, &offsets);
SCHEDULER_UNLOCK(intSave);
if (ret != LOS_OK) {
return -ret;
}
return 0;
}
static int ProcTimeContainerWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
{
(void)ppos;
char *kbuf = NULL;
int ret;
if ((pf == NULL) || (count <= 0)) {
return -EINVAL;
}
struct ProcDirEntry *entry = pf->pPDE;
if (entry == NULL) {
return -EINVAL;
}
struct ProcessData *data = (struct ProcessData *)entry->data;
if (data == NULL) {
return -EINVAL;
}
if (LOS_IsUserAddressRange((VADDR_T)(UINTPTR)buf, count)) {
kbuf = LOS_MemAlloc(m_aucSysMem1, count + 1);
if (kbuf == NULL) {
return -ENOMEM;
}
if (LOS_ArchCopyFromUser(kbuf, buf, count) != 0) {
(VOID)LOS_MemFree(m_aucSysMem1, kbuf);
return -EFAULT;
}
kbuf[count] = '\0';
buf = kbuf;
}
ret = strncmp(buf, g_monotonic, strlen(g_monotonic));
if (ret != 0) {
(VOID)LOS_MemFree(m_aucSysMem1, kbuf);
return -EINVAL;
}
buf += strlen(g_monotonic);
ret = ProcSetTimensOffset(buf, ProcGetProcessCB(data));
if (ret < 0) {
(VOID)LOS_MemFree(m_aucSysMem1, kbuf);
return ret;
}
(VOID)LOS_MemFree(m_aucSysMem1, kbuf);
return count;
}
static const struct ProcFileOperations TIME_CONTAINER_FOPS = {
.read = ProcTimeContainerRead,
.write = ProcTimeContainerWrite,
};
#endif
static int ProcProcessRead(struct SeqBuf *m, void *v)
{
if ((m == NULL) || (v == NULL)) {
@@ -180,10 +328,10 @@ static int ProcProcessRead(struct SeqBuf *m, void *v)
struct ProcessData *data = (struct ProcessData *)v;
switch (data->type) {
case PROC_PID_MEM:
return ProcessMemInfoRead(m, (LosProcessCB *)data->process);
return ProcessMemInfoRead(m, ProcGetProcessCB(data));
#ifdef LOSCFG_KERNEL_CPUP
case PROC_PID_CPUP:
return ProcessCpupRead(m, (LosProcessCB *)data->process);
return ProcessCpupRead(m, ProcGetProcessCB(data));
#endif
default:
break;
@@ -198,7 +346,7 @@ static const struct ProcFileOperations PID_FOPS = {
static struct ProcProcess g_procProcess[] = {
{
.name = NULL,
.mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH,
.mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IROTH,
.type = PROC_PID,
.fileOps = &PID_FOPS
@@ -233,6 +381,12 @@ static struct ProcProcess g_procProcess[] = {
.type = PID_CONTAINER,
.fileOps = &PID_CONTAINER_FOPS
},
{
.name = "container/pid_for_children",
.mode = S_IFLNK,
.type = PID_CHILD_CONTAINER,
.fileOps = &PID_CONTAINER_FOPS
},
#endif
#ifdef LOSCFG_UTS_CONTAINER
{
@@ -250,6 +404,34 @@ static struct ProcProcess g_procProcess[] = {
.fileOps = &PID_CONTAINER_FOPS
},
#endif
#ifdef LOSCFG_IPC_CONTAINER
{
.name = "container/ipc",
.mode = S_IFLNK,
.type = IPC_CONTAINER,
.fileOps = &PID_CONTAINER_FOPS
},
#endif
#ifdef LOSCFG_TIME_CONTAINER
{
.name = "container/time",
.mode = S_IFLNK,
.type = TIME_CONTAINER,
.fileOps = &PID_CONTAINER_FOPS
},
{
.name = "container/time_for_children",
.mode = S_IFLNK,
.type = TIME_CHILD_CONTAINER,
.fileOps = &PID_CONTAINER_FOPS
},
{
.name = "time_offsets",
.mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH,
.type = TIME_CONTAINER,
.fileOps = &TIME_CONTAINER_FOPS
},
#endif
#endif
};
@@ -269,10 +451,18 @@ static struct ProcDirEntry *ProcCreatePorcess(UINT32 pid, struct ProcProcess *po
if (data == NULL) {
return NULL;
}
if (porcess->name != NULL) {
ret = snprintf_s(pidName, PROC_PID_DIR_LEN, PROC_PID_DIR_LEN - 1, "%u/%s", pid, porcess->name);
if (pid != OS_INVALID_VALUE) {
if (porcess->name != NULL) {
ret = snprintf_s(pidName, PROC_PID_DIR_LEN, PROC_PID_DIR_LEN - 1, "%u/%s", pid, porcess->name);
} else {
ret = snprintf_s(pidName, PROC_PID_DIR_LEN, PROC_PID_DIR_LEN - 1, "%u", pid);
}
} else {
ret = snprintf_s(pidName, PROC_PID_DIR_LEN, PROC_PID_DIR_LEN - 1, "%u", pid);
if (porcess->name != NULL) {
ret = snprintf_s(pidName, PROC_PID_DIR_LEN, PROC_PID_DIR_LEN - 1, "%s/%s", "self", porcess->name);
} else {
ret = snprintf_s(pidName, PROC_PID_DIR_LEN, PROC_PID_DIR_LEN - 1, "%s", "self");
}
}
if (ret < 0) {
free(data);
@@ -306,9 +496,12 @@ int ProcCreateProcessDir(UINT32 pid, uintptr_t process)
}
}
SCHEDULER_LOCK(intSave);
((LosProcessCB *)process)->procDir = pidDir;
SCHEDULER_UNLOCK(intSave);
if (process != 0) {
SCHEDULER_LOCK(intSave);
((LosProcessCB *)process)->procDir = pidDir;
SCHEDULER_UNLOCK(intSave);
}
return 0;
CREATE_ERROR:
@@ -340,7 +533,12 @@ void ProcProcessInit(void)
pde->procFileOps = &PROCESS_PROC_FOPS;
#ifdef LOSCFG_PROC_PROCESS_DIR
int ret = ProcCreateProcessDir(OS_USER_ROOT_PROCESS_ID, (uintptr_t)OsGetUserInitProcess());
int ret = ProcCreateProcessDir(OS_INVALID_VALUE, 0);
if (ret < 0) {
PRINT_ERR("Create proc process self dir failed!\n");
}
ret = ProcCreateProcessDir(OS_USER_ROOT_PROCESS_ID, (uintptr_t)OsGetUserInitProcess());
if (ret < 0) {
PRINT_ERR("Create proc process %d dir failed!\n", OS_USER_ROOT_PROCESS_ID);
}

View File

@@ -351,11 +351,12 @@ static struct ProcDirEntry *ProcCreateFile(struct ProcDirEntry *parent, const ch
}
pn->procFileOps = procFileOps;
pn->type = VNODE_TYPE_REG;
#ifdef LOSCFG_PROC_PROCESS_DIR
if (S_ISLNK(mode)) {
pn->type = VNODE_TYPE_LNK;
} else {
pn->type = VNODE_TYPE_REG;
pn->type = VNODE_TYPE_VIR_LNK;
}
#endif
ret = ProcAddNode(parent, pn);
if (ret != 0) {
free(pn->pf);
@@ -378,11 +379,33 @@ struct ProcDirEntry *CreateProcEntry(const char *name, mode_t mode, struct ProcD
return pde;
}
static void ProcEntryClearVnode(struct ProcDirEntry *entry)
{
struct Vnode *item = NULL;
struct Vnode *nextItem = NULL;
VnodeHold();
LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, GetVnodeActiveList(), struct Vnode, actFreeEntry) {
if ((struct ProcDirEntry *)item->data != entry) {
continue;
}
if (VnodeFree(item) != LOS_OK) {
PRINT_ERR("ProcEntryClearVnode free failed, entry: %s\n", entry->name);
}
}
VnodeDrop();
return;
}
static void FreeProcEntry(struct ProcDirEntry *entry)
{
if (entry == NULL) {
return;
}
ProcEntryClearVnode(entry);
if (entry->pf != NULL) {
free(entry->pf);
entry->pf = NULL;
@@ -562,21 +585,17 @@ static int ProcRead(struct ProcDirEntry *pde, char *buf, size_t len)
struct ProcDirEntry *OpenProcFile(const char *fileName, int flags, ...)
{
unsigned int intSave;
struct ProcDirEntry *pn = ProcFindEntry(fileName);
if (pn == NULL) {
return NULL;
}
SCHEDULER_LOCK(intSave);
if (S_ISREG(pn->mode) && (pn->count != 1)) {
SCHEDULER_UNLOCK(intSave);
return NULL;
}
pn->flags = (unsigned int)(pn->flags) | (unsigned int)flags;
atomic_set(&pn->count, PROC_INUSE);
SCHEDULER_UNLOCK(intSave);
if (ProcOpen(pn->pf) != OK) {
return NULL;
}

View File

@@ -101,6 +101,9 @@ enum VnodeType {
VNODE_TYPE_BCHR, /* block char mix device */
VNODE_TYPE_FIFO, /* pipe */
VNODE_TYPE_LNK, /* link */
#ifdef LOSCFG_PROC_PROCESS_DIR
VNODE_TYPE_VIR_LNK, /* virtual link */
#endif
};
struct fs_dirent_s;
@@ -189,4 +192,7 @@ LIST_HEAD* GetVnodeActiveList(void);
LIST_HEAD* GetVnodeVirtualList(void);
int VnodeClearCache(void);
struct Vnode *GetCurrRootVnode(void);
#ifdef LOSCFG_PROC_PROCESS_DIR
struct Vnode *VnodeFind(int fd);
#endif
#endif /* !_VNODE_H_ */

View File

@@ -716,7 +716,8 @@ void VnodeMemoryDump(void)
PRINTK("Vnode memory size = %d(B)\n", vnodeCount * sizeof(struct Vnode));
}
struct Vnode *GetVnode(INT32 fd)
#ifdef LOSCFG_PROC_PROCESS_DIR
struct Vnode *VnodeFind(int fd)
{
INT32 sysFd;
@@ -734,6 +735,7 @@ struct Vnode *GetVnode(INT32 fd)
return files_get_openfile((int)sysFd);
}
#endif
LIST_HEAD* GetVnodeFreeList()
{

View File

@@ -74,7 +74,7 @@ config KERNEL_CONTAINER
depends on KERNEL_VM
config PID_CONTAINER
bool "Enable PID container Feature"
bool "Enable pid container Feature"
default n
depends on KERNEL_CONTAINER
@@ -84,7 +84,7 @@ config UTS_CONTAINER
depends on KERNEL_CONTAINER
config MNT_CONTAINER
bool "Enable MNT container Feature"
bool "Enable mnt container Feature"
default n
depends on KERNEL_CONTAINER
@@ -93,6 +93,16 @@ config CHROOT
default n
depends on MNT_CONTAINER
config IPC_CONTAINER
bool "Enable ipc container Feature"
default n
depends on KERNEL_CONTAINER
config TIME_CONTAINER
bool "Enable time container"
default n
depends on KERNEL_CONTAINER
######################### config options of extended #####################
source "kernel/extended/Kconfig"

View File

@@ -33,8 +33,10 @@ module_name = get_path_info(rebase_path("."), "name")
kernel_module(module_name) {
sources = [
"container/los_container.c",
"container/los_ipc_container.c",
"container/los_mnt_container.c",
"container/los_pid_container.c",
"container/los_time_container.c",
"container/los_uts_container.c",
"core/los_bitmap.c",
"core/los_info.c",

View File

@@ -29,6 +29,7 @@
*/
#include "los_container_pri.h"
#include "los_process_pri.h"
#include "internal.h"
#ifdef LOSCFG_KERNEL_CONTAINER
STATIC Container g_rootContainer;
@@ -44,7 +45,7 @@ VOID OsContainerInitSystemProcess(LosProcessCB *processCB)
processCB->container = &g_rootContainer;
LOS_AtomicInc(&processCB->container->rc);
#ifdef LOSCFG_PID_CONTAINER
(VOID)OsAllocSpecifiedVpidUnsafe(processCB->processID, processCB, NULL);
(VOID)OsAllocSpecifiedVpidUnsafe(processCB->processID, processCB->container->pidContainer, processCB, NULL);
#endif
return;
}
@@ -53,19 +54,27 @@ VOID OsInitRootContainer(VOID)
{
#ifdef LOSCFG_PID_CONTAINER
(VOID)OsInitRootPidContainer(&g_rootContainer.pidContainer);
g_rootContainer.pidForChildContainer = g_rootContainer.pidContainer;
#endif
#ifdef LOSCFG_UTS_CONTAINER
(VOID)OsInitRootUtsContainer(&g_rootContainer.utsContainer);
#endif
#ifdef LOSCFG_MNT_CONTAINER
(VOID)OsInitRootMntContainer(&g_rootContainer.mntContainer);
#endif
#ifdef LOSCFG_IPC_CONTAINER
(VOID)OsInitRootIpcContainer(&g_rootContainer.ipcContainer);
#endif
#ifdef LOSCFG_TIME_CONTAINER
(VOID)OsInitRootTimeContainer(&g_rootContainer.timeContainer);
g_rootContainer.timeForChildContainer = g_rootContainer.timeContainer;
#endif
return;
}
STATIC INLINE Container *CreateContainer(VOID)
{
Container *container = LOS_MemAlloc(m_aucSysMem1, sizeof(Container));
Container *container = (Container *)LOS_MemAlloc(m_aucSysMem1, sizeof(Container));
if (container == NULL) {
return NULL;
}
@@ -76,27 +85,9 @@ STATIC INLINE Container *CreateContainer(VOID)
return container;
}
/*
* called from clone. This now handles copy for Container and all
* namespaces therein.
*/
UINT32 OsCopyContainers(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID)
STATIC UINT32 CopyContainers(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID)
{
UINT32 intSave;
UINT32 ret = LOS_OK;
if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWPID | CLONE_NEWNET))) {
SCHEDULER_LOCK(intSave);
child->container = parent->container;
LOS_AtomicInc(&child->container->rc);
SCHEDULER_UNLOCK(intSave);
} else {
child->container = CreateContainer();
if (child->container == NULL) {
return ENOMEM;
}
}
/* Pid container initialization must precede other container initialization. */
#ifdef LOSCFG_PID_CONTAINER
ret = OsCopyPidContainer(flags, child, parent, processID);
@@ -115,36 +106,145 @@ UINT32 OsCopyContainers(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent
if (ret != LOS_OK) {
return ret;
}
#endif
#ifdef LOSCFG_IPC_CONTAINER
ret = OsCopyIpcContainer(flags, child, parent);
if (ret != LOS_OK) {
return ret;
}
#endif
#ifdef LOSCFG_TIME_CONTAINER
ret = OsCopyTimeContainer(flags, child, parent);
if (ret != LOS_OK) {
return ret;
}
#endif
return ret;
}
/*
* called from clone. This now handles copy for Container and all
* namespaces therein.
*/
UINT32 OsCopyContainers(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID)
{
UINT32 intSave;
#ifdef LOSCFG_TIME_CONTAINER
flags &= ~CLONE_NEWTIME;
#endif
if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWPID | CLONE_NEWNET | CLONE_NEWTIME))) {
#ifdef LOSCFG_PID_CONTAINER
if (parent->container->pidContainer != parent->container->pidForChildContainer) {
goto CREATE_CONTAINER;
}
#endif
#ifdef LOSCFG_TIME_CONTAINER
if (parent->container->timeContainer != parent->container->timeForChildContainer) {
goto CREATE_CONTAINER;
}
#endif
SCHEDULER_LOCK(intSave);
child->container = parent->container;
LOS_AtomicInc(&child->container->rc);
SCHEDULER_UNLOCK(intSave);
goto COPY_CONTAINERS;
}
#if defined(LOSCFG_PID_CONTAINER) || defined(LOSCFG_TIME_CONTAINER)
CREATE_CONTAINER:
#endif
child->container = CreateContainer();
if (child->container == NULL) {
return ENOMEM;
}
COPY_CONTAINERS:
return CopyContainers(flags, child, parent, processID);
}
VOID OsContainerFree(LosProcessCB *processCB)
{
LOS_AtomicDec(&processCB->container->rc);
if (LOS_AtomicRead(&processCB->container->rc) == 0) {
(VOID)LOS_MemFree(m_aucSysMem1, processCB->container);
processCB->container = NULL;
}
}
VOID OsContainersDestroy(LosProcessCB *processCB)
{
/* All processes in the container must be destroyed before the container is destroyed. */
#ifdef LOSCFG_PID_CONTAINER
if (processCB->processID == OS_USER_ROOT_PROCESS_ID) {
OsPidContainersDestroyAllProcess(processCB);
OsPidContainerDestroyAllProcess(processCB);
}
#endif
#ifdef LOSCFG_UTS_CONTAINER
OsUtsContainersDestroy(processCB);
OsUtsContainerDestroy(processCB->container);
#endif
#ifdef LOSCFG_MNT_CONTAINER
OsMntContainersDestroy(processCB);
OsMntContainerDestroy(processCB->container);
#endif
#ifdef LOSCFG_IPC_CONTAINER
OsIpcContainerDestroy(processCB->container);
#endif
#ifdef LOSCFG_TIME_CONTAINER
OsTimeContainerDestroy(processCB->container);
#endif
#ifndef LOSCFG_PID_CONTAINER
LOS_AtomicDec(&curr->container->rc);
if (LOS_AtomicRead(&processCB->container->rc) == 1) {
(VOID)LOS_MemFree(m_aucSysMem1, processCB->container);
processCB->container = NULL;
}
UINT32 intSave;
SCHEDULER_LOCK(intSave);
OsContainerFree(processCB);
SCHEDULER_UNLOCK(intSave);
#endif
}
STATIC VOID DeInitContainers(UINT32 flags, Container *container, LosProcessCB *processCB)
{
UINT32 intSave;
if (container == NULL) {
return;
}
#ifdef LOSCFG_PID_CONTAINER
SCHEDULER_LOCK(intSave);
if ((flags & CLONE_NEWPID) != 0) {
OsPidContainerDestroy(container, processCB);
} else {
OsPidContainerDestroy(container, NULL);
}
SCHEDULER_UNLOCK(intSave);
#endif
#ifdef LOSCFG_UTS_CONTAINER
OsUtsContainerDestroy(container);
#endif
#ifdef LOSCFG_MNT_CONTAINER
OsMntContainerDestroy(container);
#endif
#ifdef LOSCFG_IPC_CONTAINER
OsIpcContainerDestroy(container);
#endif
#ifdef LOSCFG_TIME_CONTAINER
OsTimeContainerDestroy(container);
#endif
SCHEDULER_LOCK(intSave);
LOS_AtomicDec(&container->rc);
if (LOS_AtomicRead(&container->rc) == 0) {
(VOID)LOS_MemFree(m_aucSysMem1, container);
}
SCHEDULER_UNLOCK(intSave);
}
UINT32 OsGetContainerID(Container *container, ContainerType type)
{
if (container == NULL) {
@@ -152,15 +252,221 @@ UINT32 OsGetContainerID(Container *container, ContainerType type)
}
switch (type) {
#ifdef LOSCFG_PID_CONTAINER
case PID_CONTAINER:
return OsGetPidContainerID(container->pidContainer);
case PID_CHILD_CONTAINER:
return OsGetPidContainerID(container->pidForChildContainer);
#endif
#ifdef LOSCFG_UTS_CONTAINER
case UTS_CONTAINER:
return OsGetUtsContainerID(container->utsContainer);
#endif
#ifdef LOSCFG_MNT_CONTAINER
case MNT_CONTAINER:
return OsGetMntContainerID(container->mntContainer);
#endif
#ifdef LOSCFG_IPC_CONTAINER
case IPC_CONTAINER:
return OsGetIpcContainerID(container->ipcContainer);
#endif
#ifdef LOSCFG_TIME_CONTAINER
case TIME_CONTAINER:
return OsGetTimeContainerID(container->timeContainer);
case TIME_CHILD_CONTAINER:
return OsGetTimeContainerID(container->timeForChildContainer);
#endif
default:
break;
}
return OS_INVALID_VALUE;
}
STATIC UINT32 UnshareCreateNewContainers(UINT32 flags, LosProcessCB *curr, Container *newContainer)
{
UINT32 ret = LOS_OK;
#ifdef LOSCFG_PID_CONTAINER
ret = OsUnsharePidContainer(flags, curr, newContainer);
if (ret != LOS_OK) {
return ret;
}
#endif
#ifdef LOSCFG_UTS_CONTAINER
ret = OsUnshareUtsContainer(flags, curr, newContainer);
if (ret != LOS_OK) {
return ret;
}
#endif
#ifdef LOSCFG_MNT_CONTAINER
ret = OsUnshareMntContainer(flags, curr, newContainer);
if (ret != LOS_OK) {
return ret;
}
#endif
#ifdef LOSCFG_IPC_CONTAINER
ret = OsUnshareIpcContainer(flags, curr, newContainer);
if (ret != LOS_OK) {
return ret;
}
#endif
#ifdef LOSCFG_TIME_CONTAINER
ret = OsUnshareTimeContainer(flags, curr, newContainer);
if (ret != LOS_OK) {
return ret;
}
#endif
return ret;
}
INT32 OsUnshare(UINT32 flags)
{
UINT32 ret;
UINT32 intSave;
LosProcessCB *curr = OsCurrProcessGet();
Container *oldContainer = curr->container;
UINT32 unshareFlags = CLONE_NEWPID | CLONE_NEWTIME | CLONE_NEWUTS | CLONE_NEWNS | CLONE_NEWIPC;
if (!(flags & unshareFlags) || ((flags & (~unshareFlags)) != 0)) {
return -EINVAL;
}
Container *newContainer = CreateContainer();
if (newContainer == NULL) {
return -ENOMEM;
}
ret = UnshareCreateNewContainers(flags, curr, newContainer);
if (ret != LOS_OK) {
goto EXIT;
}
SCHEDULER_LOCK(intSave);
oldContainer = curr->container;
curr->container = newContainer;
SCHEDULER_UNLOCK(intSave);
DeInitContainers(flags, oldContainer, NULL);
return LOS_OK;
EXIT:
DeInitContainers(flags, newContainer, NULL);
return -ret;
}
STATIC UINT32 SetNsGetFlagByContainerType(UINT32 containerType)
{
if (containerType >= (UINT32)CONTAINER_MAX) {
return 0;
}
ContainerType type = (ContainerType)containerType;
switch (type) {
case PID_CONTAINER:
case PID_CHILD_CONTAINER:
return CLONE_NEWPID;
case UTS_CONTAINER:
return CLONE_NEWUTS;
case MNT_CONTAINER:
return CLONE_NEWNS;
case IPC_CONTAINER:
return CLONE_NEWIPC;
case TIME_CONTAINER:
case TIME_CHILD_CONTAINER:
return CLONE_NEWTIME;
default:
break;
}
return 0;
}
STATIC UINT32 SetNsCreateNewContainers(UINT32 flags, Container *newContainer, Container *container)
{
UINT32 ret = LOS_OK;
#ifdef LOSCFG_PID_CONTAINER
ret = OsSetNsPidContainer(flags, container, newContainer);
if (ret != LOS_OK) {
return ret;
}
#endif
#ifdef LOSCFG_UTS_CONTAINER
ret = OsSetNsUtsContainer(flags, container, newContainer);
if (ret != LOS_OK) {
return ret;
}
#endif
#ifdef LOSCFG_MNT_CONTAINER
ret = OsSetNsMntContainer(flags, container, newContainer);
if (ret != LOS_OK) {
return ret;
}
#endif
#ifdef LOSCFG_IPC_CONTAINER
ret = OsSetNsIpcContainer(flags, container, newContainer);
if (ret != LOS_OK) {
return ret;
}
#endif
#ifdef LOSCFG_TIME_CONTAINER
ret = OsSetNsTimeContainer(flags, container, newContainer);
if (ret != LOS_OK) {
return ret;
}
#endif
return LOS_OK;
}
INT32 OsSetNs(INT32 fd, INT32 type)
{
UINT32 intSave;
UINT32 typeMask = CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWIPC | CLONE_NEWTIME;
UINT32 containerType = 0;
UINT32 flag = (UINT32)(type & typeMask);
LosProcessCB *curr = OsCurrProcessGet();
if (((type & (~typeMask)) != 0)) {
return -EINVAL;
}
Container *newContainer = CreateContainer();
if (newContainer == NULL) {
return -ENOMEM;
}
LosProcessCB *processCB = (LosProcessCB *)ProcfsContainerGet(fd, &containerType);
if (processCB == NULL) {
(VOID)LOS_MemFree(m_aucSysMem1, newContainer);
return -EBADF;
}
SCHEDULER_LOCK(intSave);
Container *targetContainer = processCB->container;
if (targetContainer == NULL) {
SCHEDULER_UNLOCK(intSave);
return -EBADF;
}
if (flag == 0) {
flag = SetNsGetFlagByContainerType(containerType);
}
if ((flag == 0) || (flag != SetNsGetFlagByContainerType(containerType)) || (targetContainer == curr->container)) {
SCHEDULER_UNLOCK(intSave);
return -EBADF;
}
UINT32 ret = SetNsCreateNewContainers(flag, newContainer, targetContainer);
if (ret != LOS_OK) {
SCHEDULER_UNLOCK(intSave);
goto EXIT;
}
Container *oldContainer = curr->container;
curr->container = newContainer;
SCHEDULER_UNLOCK(intSave);
DeInitContainers(flag, oldContainer, NULL);
return LOS_OK;
EXIT:
DeInitContainers(flag, newContainer, curr);
return ret;
}
#endif

View File

@@ -0,0 +1,209 @@
/*
* 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.
*/
#ifdef LOSCFG_IPC_CONTAINER
#include "los_ipc_container_pri.h"
#include "los_config.h"
#include "los_queue_pri.h"
#include "los_vm_shm_pri.h"
#include "los_process_pri.h"
#include "vnode.h"
#include "proc_fs.h"
#include "pthread.h"
#define IPC_INIT_NUM 3
STATIC UINT32 g_currentIpcContainerNum = 0;
STATIC IpcContainer *CreateNewIpcContainer(IpcContainer *parent)
{
pthread_mutexattr_t attr;
UINT32 size = sizeof(IpcContainer);
IpcContainer *ipcContainer = (IpcContainer *)LOS_MemAlloc(m_aucSysMem1, size);
if (ipcContainer == NULL) {
return NULL;
}
(VOID)memset_s(ipcContainer, size, 0, size);
ipcContainer->allQueue = OsAllQueueCBInit(&ipcContainer->freeQueueList);
if (ipcContainer->allQueue == NULL) {
(VOID)LOS_MemFree(m_aucSysMem1, ipcContainer);
return NULL;
}
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
pthread_mutex_init(&ipcContainer->mqueueMutex, &attr);
ipcContainer->shmSegs = OsShmCBInit(&ipcContainer->sysvShmMux, &ipcContainer->shmInfo,
&ipcContainer->shmUsedPageCount);
if (ipcContainer->shmSegs == NULL) {
(VOID)LOS_MemFree(m_aucSysMem1, ipcContainer->allQueue);
(VOID)LOS_MemFree(m_aucSysMem1, ipcContainer);
return NULL;
}
ipcContainer->containerID = OsAllocContainerID();
if (parent != NULL) {
LOS_AtomicSet(&ipcContainer->rc, 1);
} else {
LOS_AtomicSet(&ipcContainer->rc, 3); /* 3: Three system processes */
}
return ipcContainer;
}
STATIC UINT32 CreateIpcContainer(LosProcessCB *child, LosProcessCB *parent)
{
UINT32 intSave;
IpcContainer *parentContainer = parent->container->ipcContainer;
IpcContainer *newIpcContainer = CreateNewIpcContainer(parentContainer);
if (newIpcContainer == NULL) {
return ENOMEM;
}
SCHEDULER_LOCK(intSave);
g_currentIpcContainerNum++;
child->container->ipcContainer = newIpcContainer;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
UINT32 OsInitRootIpcContainer(IpcContainer **ipcContainer)
{
UINT32 intSave;
IpcContainer *newIpcContainer = CreateNewIpcContainer(NULL);
if (newIpcContainer == NULL) {
return ENOMEM;
}
SCHEDULER_LOCK(intSave);
g_currentIpcContainerNum++;
*ipcContainer = newIpcContainer;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
UINT32 OsCopyIpcContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent)
{
UINT32 intSave;
IpcContainer *currIpcContainer = parent->container->ipcContainer;
if (!(flags & CLONE_NEWIPC)) {
SCHEDULER_LOCK(intSave);
LOS_AtomicInc(&currIpcContainer->rc);
child->container->ipcContainer = currIpcContainer;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
return CreateIpcContainer(child, parent);
}
UINT32 OsUnshareIpcContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
{
UINT32 intSave;
IpcContainer *parentContainer = curr->container->ipcContainer;
if (!(flags & CLONE_NEWIPC)) {
SCHEDULER_LOCK(intSave);
newContainer->ipcContainer = parentContainer;
LOS_AtomicInc(&parentContainer->rc);
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
IpcContainer *ipcContainer = CreateNewIpcContainer(parentContainer);
if (ipcContainer == NULL) {
return ENOMEM;
}
SCHEDULER_LOCK(intSave);
newContainer->ipcContainer = ipcContainer;
g_currentIpcContainerNum++;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
UINT32 OsSetNsIpcContainer(UINT32 flags, Container *container, Container *newContainer)
{
if (flags & CLONE_NEWIPC) {
newContainer->ipcContainer = container->ipcContainer;
LOS_AtomicInc(&container->ipcContainer->rc);
return LOS_OK;
}
newContainer->ipcContainer = OsCurrProcessGet()->container->ipcContainer;
LOS_AtomicInc(&newContainer->ipcContainer->rc);
return LOS_OK;
}
VOID OsIpcContainerDestroy(Container *container)
{
UINT32 intSave;
if (container == NULL) {
return;
}
SCHEDULER_LOCK(intSave);
IpcContainer *ipcContainer = container->ipcContainer;
if (ipcContainer == NULL) {
SCHEDULER_UNLOCK(intSave);
return;
}
LOS_AtomicDec(&ipcContainer->rc);
if (LOS_AtomicRead(&ipcContainer->rc) > 0) {
SCHEDULER_UNLOCK(intSave);
return;
}
g_currentIpcContainerNum--;
container->ipcContainer = NULL;
SCHEDULER_UNLOCK(intSave);
OsShmCBDestroy(ipcContainer->shmSegs, &ipcContainer->shmInfo, &ipcContainer->sysvShmMux);
ipcContainer->shmSegs = NULL;
OsMqueueCBDestroy(ipcContainer->queueTable);
(VOID)LOS_MemFree(m_aucSysMem1, ipcContainer->allQueue);
(VOID)LOS_MemFree(m_aucSysMem1, ipcContainer);
return;
}
UINT32 OsGetIpcContainerID(IpcContainer *ipcContainer)
{
if (ipcContainer == NULL) {
return OS_INVALID_VALUE;
}
return ipcContainer->containerID;
}
IpcContainer *OsGetCurrIpcContainer(VOID)
{
return OsCurrProcessGet()->container->ipcContainer;
}
#endif

View File

@@ -44,20 +44,50 @@ LIST_HEAD *GetContainerMntList(VOID)
return &OsCurrProcessGet()->container->mntContainer->mountList;
}
STATIC UINT32 CreateMntContainer(MntContainer **newMntContainer)
STATIC MntContainer *CreateNewMntContainer(MntContainer *parent)
{
UINT32 intSave;
MntContainer *mntContainer = (MntContainer *)LOS_MemAlloc(m_aucSysMem1, sizeof(MntContainer));
if (mntContainer == NULL) {
return ENOMEM;
return NULL;
}
mntContainer->containerID = OsAllocContainerID();
LOS_AtomicSet(&mntContainer->rc, 1);
LOS_ListInit(&mntContainer->mountList);
if (parent != NULL) {
LOS_AtomicSet(&mntContainer->rc, 1);
} else {
LOS_AtomicSet(&mntContainer->rc, 3); /* 3: Three system processes */
}
return mntContainer;
}
STATIC UINT32 CreateMntContainer(LosProcessCB *child, LosProcessCB *parent)
{
UINT32 intSave;
MntContainer *parentContainer = parent->container->mntContainer;
MntContainer *newMntContainer = CreateNewMntContainer(parentContainer);
if (newMntContainer == NULL) {
return ENOMEM;
}
SCHEDULER_LOCK(intSave);
g_currentMntContainerNum++;
*newMntContainer = mntContainer;
child->container->mntContainer = newMntContainer;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
UINT32 OsInitRootMntContainer(MntContainer **mntContainer)
{
UINT32 intSave;
MntContainer *newMntContainer = CreateNewMntContainer(NULL);
if (newMntContainer == NULL) {
return ENOMEM;
}
SCHEDULER_LOCK(intSave);
g_currentMntContainerNum++;
*mntContainer = newMntContainer;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
@@ -94,7 +124,7 @@ UINT32 OsCopyMntContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *pare
return LOS_OK;
}
ret = CreateMntContainer(&child->container->mntContainer);
ret = CreateMntContainer(child, parent);
if (ret != LOS_OK) {
return ret;
}
@@ -102,6 +132,51 @@ UINT32 OsCopyMntContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *pare
return CopyMountList(currMntContainer, child->container->mntContainer);
}
UINT32 OsUnshareMntContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
{
UINT32 intSave;
UINT32 ret;
MntContainer *parentContainer = curr->container->mntContainer;
if (!(flags & CLONE_NEWNS)) {
SCHEDULER_LOCK(intSave);
newContainer->mntContainer = parentContainer;
LOS_AtomicInc(&parentContainer->rc);
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
MntContainer *mntContainer = CreateNewMntContainer(parentContainer);
if (mntContainer == NULL) {
return ENOMEM;
}
ret = CopyMountList(parentContainer, mntContainer);
if (ret != LOS_OK) {
(VOID)LOS_MemFree(m_aucSysMem1, mntContainer);
return ret;
}
SCHEDULER_LOCK(intSave);
newContainer->mntContainer = mntContainer;
g_currentMntContainerNum++;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
UINT32 OsSetNsMntContainer(UINT32 flags, Container *container, Container *newContainer)
{
if (flags & CLONE_NEWNS) {
newContainer->mntContainer = container->mntContainer;
LOS_AtomicInc(&container->mntContainer->rc);
return LOS_OK;
}
newContainer->mntContainer = OsCurrProcessGet()->container->mntContainer;
LOS_AtomicInc(&newContainer->mntContainer->rc);
return LOS_OK;
}
STATIC VOID FreeMountList(LIST_HEAD *mountList)
{
struct Mount *mnt = NULL;
@@ -126,26 +201,31 @@ STATIC VOID FreeMountList(LIST_HEAD *mountList)
return;
}
VOID OsMntContainersDestroy(LosProcessCB *curr)
VOID OsMntContainerDestroy(Container *container)
{
UINT32 intSave;
if (curr->container == NULL) {
if (container == NULL) {
return;
}
SCHEDULER_LOCK(intSave);
MntContainer *mntContainer = curr->container->mntContainer;
if (mntContainer != NULL) {
if (LOS_AtomicRead(&mntContainer->rc) == 0) {
g_currentMntContainerNum--;
FreeMountList(&mntContainer->mountList);
curr->container->mntContainer = NULL;
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, mntContainer);
return;
}
MntContainer *mntContainer = container->mntContainer;
if (mntContainer == NULL) {
SCHEDULER_UNLOCK(intSave);
return;
}
LOS_AtomicDec(&mntContainer->rc);
if (LOS_AtomicRead(&mntContainer->rc) > 0) {
SCHEDULER_UNLOCK(intSave);
return;
}
g_currentMntContainerNum--;
SCHEDULER_UNLOCK(intSave);
FreeMountList(&mntContainer->mountList);
container->mntContainer = NULL;
(VOID)LOS_MemFree(m_aucSysMem1, mntContainer);
return;
}
@@ -157,9 +237,4 @@ UINT32 OsGetMntContainerID(MntContainer *mntContainer)
return mntContainer->containerID;
}
UINT32 OsInitRootMntContainer(MntContainer **mntContainer)
{
return CreateMntContainer(mntContainer);
}
#endif

View File

@@ -67,9 +67,9 @@ STATIC ProcessVid *OsGetFreeVpid(PidContainer *pidContainer)
return vpid;
}
UINT32 OsAllocSpecifiedVpidUnsafe(UINT32 vpid, LosProcessCB *processCB, LosProcessCB *parent)
UINT32 OsAllocSpecifiedVpidUnsafe(UINT32 vpid, PidContainer *pidContainer,
LosProcessCB *processCB, LosProcessCB *parent)
{
PidContainer *pidContainer = processCB->container->pidContainer;
if ((pidContainer == NULL) || OS_PID_CHECK_INVALID(vpid)) {
return OS_INVALID_VALUE;
}
@@ -84,14 +84,21 @@ UINT32 OsAllocSpecifiedVpidUnsafe(UINT32 vpid, LosProcessCB *processCB, LosProce
}
processVid->cb = (UINTPTR)processCB;
processVid->realParent = parent;
processCB->processID = vpid;
LOS_ListDelete(&processVid->node);
LOS_AtomicInc(&pidContainer->rc);
if ((vpid == OS_USER_ROOT_PROCESS_ID) && (parent != NULL)) {
ProcessVid *vppidItem = &pidContainer->pidArray[0];
LOS_ListDelete(&vppidItem->node);
vppidItem->cb = (UINTPTR)parent;
if (vpid == OS_USER_ROOT_PROCESS_ID) {
if (parent != NULL) {
ProcessVid *vppidItem = &pidContainer->pidArray[0];
LOS_ListDelete(&vppidItem->node);
vppidItem->cb = (UINTPTR)parent;
}
if (OsCreateProcessGroup(processCB) == NULL) {
return OS_INVALID_VALUE;
}
}
pidContainer = pidContainer->parent;
@@ -110,7 +117,7 @@ UINT32 OsAllocSpecifiedVpidUnsafe(UINT32 vpid, LosProcessCB *processCB, LosProce
return processCB->processID;
}
STATIC UINT32 OsAllocVpid(LosProcessCB *processCB)
STATIC UINT32 OsAllocVpid(LosProcessCB *processCB, LosProcessCB *parent)
{
ProcessVid *oldProcessVid = NULL;
PidContainer *pidContainer = processCB->container->pidContainer;
@@ -127,6 +134,7 @@ STATIC UINT32 OsAllocVpid(LosProcessCB *processCB)
vpid->cb = (UINTPTR)processCB;
if (processCB->processID == OS_INVALID_VALUE) {
processCB->processID = vpid->vid;
vpid->realParent = parent;
} else {
oldProcessVid->vpid = vpid->vid;
}
@@ -158,6 +166,7 @@ VOID OsFreeVtid(LosTaskCB *taskCB)
item->cb = (UINTPTR)g_defaultTaskCB;
vtid = item->vpid;
item->vpid = OS_INVALID_VALUE;
item->realParent = NULL;
LOS_ListTailInsert(&pidContainer->tidFreeList, &item->node);
pidContainer = pidContainer->parent;
}
@@ -195,7 +204,7 @@ UINT32 OsAllocVtid(LosTaskCB *taskCB, const LosProcessCB *processCB)
return taskCB->taskID;
}
VOID OsPidContainersDestroyAllProcess(LosProcessCB *curr)
VOID OsPidContainerDestroyAllProcess(LosProcessCB *curr)
{
INT32 ret;
UINT32 intSave;
@@ -264,16 +273,56 @@ STATIC PidContainer *CreateNewPidContainer(PidContainer *parent)
newPidContainer->parent = parent;
if (parent != NULL) {
LOS_AtomicSet(&newPidContainer->level, parent->level + 1);
newPidContainer->referenced = FALSE;
} else {
LOS_AtomicSet(&newPidContainer->level, 0);
newPidContainer->referenced = TRUE;
}
return newPidContainer;
}
VOID OsPidContainerDestroy(Container *container, LosProcessCB *processCB)
{
if (container == NULL) {
return;
}
PidContainer *pidContainer = container->pidContainer;
if (pidContainer == NULL) {
return;
}
if (processCB != NULL) {
FreeVpid(processCB);
}
if ((container->pidForChildContainer != NULL) && (pidContainer != container->pidForChildContainer)) {
LOS_AtomicDec(&container->pidForChildContainer->rc);
if (LOS_AtomicRead(&container->pidForChildContainer->rc) <= 0) {
g_currentPidContainerNum--;
(VOID)LOS_MemFree(m_aucSysMem1, container->pidForChildContainer);
container->pidForChildContainer = NULL;
}
}
if (LOS_AtomicRead(&pidContainer->rc) <= 0) {
g_currentPidContainerNum--;
container->pidContainer = NULL;
container->pidForChildContainer = NULL;
(VOID)LOS_MemFree(m_aucSysMem1, pidContainer->rootPGroup);
(VOID)LOS_MemFree(m_aucSysMem1, pidContainer);
}
if (processCB != NULL) {
OsContainerFree(processCB);
}
}
STATIC UINT32 CreatePidContainer(LosProcessCB *child, LosProcessCB *parent)
{
UINT32 intSave;
UINT32 ret;
UINT32 intSave;
PidContainer *parentContainer = parent->container->pidContainer;
PidContainer *newPidContainer = CreateNewPidContainer(parentContainer);
if (newPidContainer == NULL) {
@@ -282,18 +331,21 @@ STATIC UINT32 CreatePidContainer(LosProcessCB *child, LosProcessCB *parent)
SCHEDULER_LOCK(intSave);
if ((parentContainer->level + 1) >= PID_CONTAINER_LEVEL_LIMIT) {
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, newPidContainer);
SCHEDULER_UNLOCK(intSave);
return EINVAL;
}
g_currentPidContainerNum++;
newPidContainer->referenced = TRUE;
child->container->pidContainer = newPidContainer;
ret = OsAllocSpecifiedVpidUnsafe(OS_USER_ROOT_PROCESS_ID, child, parent);
child->container->pidForChildContainer = newPidContainer;
ret = OsAllocSpecifiedVpidUnsafe(OS_USER_ROOT_PROCESS_ID, newPidContainer, child, parent);
if (ret == OS_INVALID_VALUE) {
g_currentPidContainerNum--;
FreeVpid(child);
child->container->pidContainer = NULL;
child->container->pidForChildContainer = NULL;
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, newPidContainer);
return ENOSPC;
@@ -302,27 +354,35 @@ STATIC UINT32 CreatePidContainer(LosProcessCB *child, LosProcessCB *parent)
return LOS_OK;
}
VOID OsPidContainersDestroy(LosProcessCB *curr)
STATIC UINT32 AllocVpidFormPidForChildContainer(LosProcessCB *child, LosProcessCB *parent)
{
if (curr->container == NULL) {
return;
}
UINT32 ret;
PidContainer *pidContainer = parent->container->pidForChildContainer;
child->container->pidContainer = pidContainer;
child->container->pidForChildContainer = pidContainer;
PidContainer *pidContainer = curr->container->pidContainer;
if (pidContainer != NULL) {
FreeVpid(curr);
if (LOS_AtomicRead(&pidContainer->rc) == 0) {
g_currentPidContainerNum--;
(VOID)LOS_MemFree(m_aucSysMem1, pidContainer);
curr->container->pidContainer = NULL;
if (!pidContainer->referenced) {
pidContainer->referenced = TRUE;
ret = OsAllocSpecifiedVpidUnsafe(OS_USER_ROOT_PROCESS_ID, pidContainer, child, parent);
} else {
ret = OsAllocVpid(child, parent);
if (ret != OS_INVALID_VALUE) {
LosProcessCB *parentProcessCB = (LosProcessCB *)pidContainer->pidArray[OS_USER_ROOT_PROCESS_ID].cb;
child->parentProcess = parentProcessCB;
LOS_ListTailInsert(&parentProcessCB->childrenList, &child->siblingList);
child->pgroup = parentProcessCB->pgroup;
LOS_ListTailInsert(&parentProcessCB->pgroup->processList, &child->subordinateGroupList);
}
}
LOS_AtomicDec(&curr->container->rc);
if (LOS_AtomicRead(&curr->container->rc) == 0) {
(VOID)LOS_MemFree(m_aucSysMem1, curr->container);
curr->container = NULL;
if (ret == OS_INVALID_VALUE) {
FreeVpid(child);
child->container->pidContainer = NULL;
child->container->pidForChildContainer = NULL;
return ENOSPC;
}
return LOS_OK;
}
UINT32 OsCopyPidContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID)
@@ -330,22 +390,36 @@ UINT32 OsCopyPidContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *pare
UINT32 ret;
UINT32 intSave;
if (!(flags & CLONE_NEWPID)) {
SCHEDULER_LOCK(intSave);
child->container->pidContainer = parent->container->pidContainer;
ret = OsAllocVpid(child);
SCHEDULER_UNLOCK(intSave);
if (ret == OS_INVALID_VALUE) {
PRINT_ERR("[%s] alloc vpid failed\n", __FUNCTION__);
return ENOSPC;
SCHEDULER_LOCK(intSave);
PidContainer *parentPidContainer = parent->container->pidContainer;
PidContainer *parentPidContainerForChild = parent->container->pidForChildContainer;
if (parentPidContainer == parentPidContainerForChild) {
/* The current process is not executing unshare */
if (!(flags & CLONE_NEWPID)) {
child->container->pidContainer = parentPidContainer;
child->container->pidForChildContainer = parentPidContainer;
ret = OsAllocVpid(child, parent);
SCHEDULER_UNLOCK(intSave);
if (ret == OS_INVALID_VALUE) {
PRINT_ERR("[%s] alloc vpid failed\n", __FUNCTION__);
return ENOSPC;
}
*processID = child->processID;
return LOS_OK;
}
*processID = child->processID;
return LOS_OK;
}
SCHEDULER_UNLOCK(intSave);
ret = CreatePidContainer(child, parent);
if (ret != LOS_OK) {
return ret;
ret = CreatePidContainer(child, parent);
if (ret != LOS_OK) {
return ret;
}
} else {
/* Create the first process after unshare */
ret = AllocVpidFormPidForChildContainer(child, parent);
SCHEDULER_UNLOCK(intSave);
if (ret != LOS_OK) {
return ret;
}
}
PidContainer *pidContainer = child->container->pidContainer;
@@ -357,6 +431,65 @@ UINT32 OsCopyPidContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *pare
return LOS_OK;
}
UINT32 OsUnsharePidContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
{
UINT32 intSave;
if (!(flags & CLONE_NEWPID)) {
SCHEDULER_LOCK(intSave);
newContainer->pidContainer = curr->container->pidContainer;
newContainer->pidForChildContainer = curr->container->pidForChildContainer;
if (newContainer->pidContainer != newContainer->pidForChildContainer) {
LOS_AtomicInc(&newContainer->pidForChildContainer->rc);
}
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
PidContainer *pidForChild = CreateNewPidContainer(curr->container->pidContainer);
if (pidForChild == NULL) {
return ENOMEM;
}
SCHEDULER_LOCK(intSave);
if (curr->container->pidContainer != curr->container->pidForChildContainer) {
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, pidForChild);
return EINVAL;
}
if (pidForChild->level >= PID_CONTAINER_LEVEL_LIMIT) {
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, pidForChild);
return EINVAL;
}
g_currentPidContainerNum++;
newContainer->pidContainer = curr->container->pidContainer;
newContainer->pidForChildContainer = pidForChild;
LOS_AtomicSet(&pidForChild->rc, 1);
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
UINT32 OsSetNsPidContainer(UINT32 flags, Container *container, Container *newContainer)
{
UINT32 ret = LOS_OK;
LosProcessCB *curr = OsCurrProcessGet();
newContainer->pidContainer = curr->container->pidContainer;
if (flags & CLONE_NEWPID) {
newContainer->pidForChildContainer = container->pidContainer;
LOS_AtomicInc(&container->pidContainer->rc);
return ret;
}
newContainer->pidForChildContainer = curr->container->pidForChildContainer;
if (newContainer->pidContainer != newContainer->pidForChildContainer) {
LOS_AtomicInc(&newContainer->pidForChildContainer->rc);
}
return LOS_OK;
}
UINT32 OsInitRootPidContainer(PidContainer **pidContainer)
{
UINT32 intSave;
@@ -441,6 +574,20 @@ LosTaskCB *OsGetTCBFromVtid(UINT32 vtid)
return (LosTaskCB *)taskVid->cb;
}
BOOL OsPidContainerProcessParentIsRealParent(const LosProcessCB *processCB, const LosProcessCB *curr)
{
if (curr == processCB->parentProcess) {
return FALSE;
}
PidContainer *pidContainer = processCB->container->pidContainer;
ProcessVid *processVid = &pidContainer->pidArray[processCB->processID];
if (processVid->realParent == curr) {
return TRUE;
}
return FALSE;
}
UINT32 OsGetPidContainerID(PidContainer *pidContainer)
{
if (pidContainer == NULL) {

View File

@@ -0,0 +1,245 @@
/*
* 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 "los_time_container_pri.h"
#include "los_process_pri.h"
#ifdef LOSCFG_TIME_CONTAINER
STATIC UINT32 g_currentTimeContainerNum;
STATIC TimeContainer *CreateNewTimeContainer(TimeContainer *parent)
{
UINT32 size = sizeof(TimeContainer);
TimeContainer *timeContainer = (TimeContainer *)LOS_MemAlloc(m_aucSysMem1, size);
if (timeContainer == NULL) {
return NULL;
}
(VOID)memset_s(timeContainer, size, 0, size);
timeContainer->containerID = OsAllocContainerID();
if (parent != NULL) {
timeContainer->frozenOffsets = FALSE;
LOS_AtomicSet(&timeContainer->rc, 1);
} else {
timeContainer->frozenOffsets = TRUE;
LOS_AtomicSet(&timeContainer->rc, 3); /* 3: Three system processes */
}
return timeContainer;
}
STATIC UINT32 CreateTimeContainer(LosProcessCB *child, LosProcessCB *parent)
{
UINT32 intSave;
SCHEDULER_LOCK(intSave);
TimeContainer *newTimeContainer = parent->container->timeForChildContainer;
LOS_AtomicInc(&newTimeContainer->rc);
newTimeContainer->frozenOffsets = TRUE;
child->container->timeContainer = newTimeContainer;
child->container->timeForChildContainer = newTimeContainer;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
UINT32 OsInitRootTimeContainer(TimeContainer **timeContainer)
{
UINT32 intSave;
TimeContainer *newTimeContainer = CreateNewTimeContainer(NULL);
if (newTimeContainer == NULL) {
return ENOMEM;
}
SCHEDULER_LOCK(intSave);
*timeContainer = newTimeContainer;
g_currentTimeContainerNum++;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
UINT32 OsCopyTimeContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent)
{
UINT32 intSave;
TimeContainer *currTimeContainer = parent->container->timeContainer;
if (currTimeContainer == parent->container->timeForChildContainer) {
SCHEDULER_LOCK(intSave);
LOS_AtomicInc(&currTimeContainer->rc);
child->container->timeContainer = currTimeContainer;
child->container->timeForChildContainer = currTimeContainer;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
return CreateTimeContainer(child, parent);
}
UINT32 OsUnshareTimeContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
{
UINT32 intSave;
if (!(flags & CLONE_NEWTIME)) {
SCHEDULER_LOCK(intSave);
newContainer->timeContainer = curr->container->timeContainer;
newContainer->timeForChildContainer = curr->container->timeForChildContainer;
LOS_AtomicInc(&newContainer->timeContainer->rc);
if (newContainer->timeContainer != newContainer->timeForChildContainer) {
LOS_AtomicInc(&newContainer->timeForChildContainer->rc);
}
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
TimeContainer *timeForChild = CreateNewTimeContainer(curr->container->timeContainer);
if (timeForChild == NULL) {
return ENOMEM;
}
SCHEDULER_LOCK(intSave);
if (curr->container->timeContainer != curr->container->timeForChildContainer) {
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, timeForChild);
return EINVAL;
}
(VOID)memcpy_s(&timeForChild->monotonic, sizeof(struct timespec64),
&curr->container->timeContainer->monotonic, sizeof(struct timespec64));
newContainer->timeContainer = curr->container->timeContainer;
LOS_AtomicInc(&newContainer->timeContainer->rc);
newContainer->timeForChildContainer = timeForChild;
g_currentTimeContainerNum++;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
UINT32 OsSetNsTimeContainer(UINT32 flags, Container *container, Container *newContainer)
{
LosProcessCB *curr = OsCurrProcessGet();
if (flags & CLONE_NEWTIME) {
container->timeContainer->frozenOffsets = TRUE;
newContainer->timeContainer = container->timeContainer;
newContainer->timeForChildContainer = container->timeContainer;
LOS_AtomicInc(&container->timeContainer->rc);
return LOS_OK;
}
newContainer->timeContainer = curr->container->timeContainer;
LOS_AtomicInc(&curr->container->timeContainer->rc);
newContainer->timeForChildContainer = curr->container->timeForChildContainer;
if (curr->container->timeContainer != curr->container->timeForChildContainer) {
LOS_AtomicInc(&curr->container->timeForChildContainer->rc);
}
return LOS_OK;
}
VOID OsTimeContainerDestroy(Container *container)
{
UINT32 intSave;
TimeContainer *timeContainer = NULL;
TimeContainer *timeForChild = NULL;
if (container == NULL) {
return;
}
SCHEDULER_LOCK(intSave);
if (container->timeContainer == NULL) {
SCHEDULER_UNLOCK(intSave);
return;
}
if ((container->timeForChildContainer != NULL) && (container->timeContainer != container->timeForChildContainer)) {
LOS_AtomicDec(&container->timeForChildContainer->rc);
if (LOS_AtomicRead(&container->timeForChildContainer->rc) <= 0) {
g_currentTimeContainerNum--;
timeForChild = container->timeForChildContainer;
container->timeForChildContainer = NULL;
}
}
LOS_AtomicDec(&container->timeContainer->rc);
if (LOS_AtomicRead(&container->timeContainer->rc) <= 0) {
g_currentTimeContainerNum--;
timeContainer = container->timeContainer;
container->timeContainer = NULL;
container->timeForChildContainer = NULL;
}
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, timeForChild);
(VOID)LOS_MemFree(m_aucSysMem1, timeContainer);
return;
}
UINT32 OsGetTimeContainerID(TimeContainer *timeContainer)
{
if (timeContainer == NULL) {
return OS_INVALID_VALUE;
}
return timeContainer->containerID;
}
TimeContainer *OsGetCurrTimeContainer(VOID)
{
return OsCurrProcessGet()->container->timeContainer;
}
UINT32 OsGetTimeContainerMonotonic(LosProcessCB *processCB, struct timespec64 *offsets)
{
if ((processCB == NULL) || (offsets == NULL)) {
return EINVAL;
}
if (OsProcessIsInactive(processCB)) {
return ESRCH;
}
TimeContainer *timeContainer = processCB->container->timeForChildContainer;
(VOID)memcpy_s(offsets, sizeof(struct timespec64), &timeContainer->monotonic, sizeof(struct timespec64));
return LOS_OK;
}
UINT32 OsSetTimeContainerMonotonic(LosProcessCB *processCB, struct timespec64 *offsets)
{
if ((processCB == NULL) || (offsets == NULL)) {
return EINVAL;
}
if (OsProcessIsInactive(processCB)) {
return ESRCH;
}
TimeContainer *timeContainer = processCB->container->timeForChildContainer;
if (timeContainer->frozenOffsets) {
return EACCES;
}
timeContainer->monotonic.tv_sec = offsets->tv_sec;
timeContainer->monotonic.tv_nsec = offsets->tv_nsec;
return LOS_OK;
}
#endif

View File

@@ -36,81 +36,9 @@
STATIC UINT32 g_currentUtsContainerNum;
STATIC UINT32 CreateUtsContainer(UtsContainer **newUtsContainer)
{
UINT32 intSave;
UINT32 size = sizeof(UtsContainer);
UtsContainer *utsContainer = LOS_MemAlloc(m_aucSysMem1, size);
if (utsContainer == NULL) {
return ENOMEM;
}
(VOID)memset_s(utsContainer, sizeof(UtsContainer), 0, sizeof(UtsContainer));
utsContainer->containerID = OsAllocContainerID();
LOS_AtomicSet(&utsContainer->rc, 1);
SCHEDULER_LOCK(intSave);
g_currentUtsContainerNum += 1;
*newUtsContainer = utsContainer;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
UINT32 OsCopyUtsContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent)
{
UINT32 intSave;
UINT32 ret;
UtsContainer *newUtsContainer = NULL;
UtsContainer *currUtsContainer = parent->container->utsContainer;
if (!(flags & CLONE_NEWUTS)) {
SCHEDULER_LOCK(intSave);
LOS_AtomicInc(&currUtsContainer->rc);
child->container->utsContainer = currUtsContainer;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
ret = CreateUtsContainer(&newUtsContainer);
if (ret != LOS_OK) {
return ret;
}
SCHEDULER_LOCK(intSave);
(VOID)memcpy_s(&newUtsContainer->utsName, sizeof(newUtsContainer->utsName),
&currUtsContainer->utsName, sizeof(currUtsContainer->utsName));
child->container->utsContainer = newUtsContainer;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
VOID OsUtsContainersDestroy(LosProcessCB *curr)
{
UINT32 intSave;
if (curr->container == NULL) {
return;
}
SCHEDULER_LOCK(intSave);
UtsContainer *utsContainer = curr->container->utsContainer;
if (utsContainer != NULL) {
if (LOS_AtomicRead(&utsContainer->rc) == 0) {
g_currentUtsContainerNum--;
curr->container->utsContainer = NULL;
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, utsContainer);
return;
}
}
SCHEDULER_UNLOCK(intSave);
return;
}
STATIC UINT32 InitUtsContainer(struct utsname *name)
{
UINT32 ret;
ret = sprintf_s(name->sysname, sizeof(name->sysname), "%s", KERNEL_NAME);
INT32 ret = sprintf_s(name->sysname, sizeof(name->sysname), "%s", KERNEL_NAME);
if (ret < 0) {
return LOS_NOK;
}
@@ -137,14 +65,143 @@ STATIC UINT32 InitUtsContainer(struct utsname *name)
return LOS_OK;
}
UINT32 OsInitRootUtsContainer(UtsContainer **utsContainer)
STATIC UtsContainer *CreateNewUtsContainer(UtsContainer *parent)
{
UINT32 ret = CreateUtsContainer(utsContainer);
UINT32 ret;
UINT32 size = sizeof(UtsContainer);
UtsContainer *utsContainer = (UtsContainer *)LOS_MemAlloc(m_aucSysMem1, size);
if (utsContainer == NULL) {
return NULL;
}
(VOID)memset_s(utsContainer, sizeof(UtsContainer), 0, sizeof(UtsContainer));
utsContainer->containerID = OsAllocContainerID();
if (parent != NULL) {
LOS_AtomicSet(&utsContainer->rc, 1);
return utsContainer;
}
LOS_AtomicSet(&utsContainer->rc, 3); /* 3: Three system processes */
ret = InitUtsContainer(&utsContainer->utsName);
if (ret != LOS_OK) {
return ret;
(VOID)LOS_MemFree(m_aucSysMem1, utsContainer);
return NULL;
}
return utsContainer;
}
STATIC UINT32 CreateUtsContainer(LosProcessCB *child, LosProcessCB *parent)
{
UINT32 intSave;
UtsContainer *parentContainer = parent->container->utsContainer;
UtsContainer *newUtsContainer = CreateNewUtsContainer(parentContainer);
if (newUtsContainer == NULL) {
return ENOMEM;
}
return InitUtsContainer(&(*utsContainer)->utsName);
SCHEDULER_LOCK(intSave);
g_currentUtsContainerNum++;
(VOID)memcpy_s(&newUtsContainer->utsName, sizeof(newUtsContainer->utsName),
&parentContainer->utsName, sizeof(parentContainer->utsName));
child->container->utsContainer = newUtsContainer;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
UINT32 OsInitRootUtsContainer(UtsContainer **utsContainer)
{
UINT32 intSave;
UtsContainer *newUtsContainer = CreateNewUtsContainer(NULL);
if (newUtsContainer == NULL) {
return ENOMEM;
}
SCHEDULER_LOCK(intSave);
g_currentUtsContainerNum++;
*utsContainer = newUtsContainer;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
UINT32 OsCopyUtsContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent)
{
UINT32 intSave;
UtsContainer *currUtsContainer = parent->container->utsContainer;
if (!(flags & CLONE_NEWUTS)) {
SCHEDULER_LOCK(intSave);
LOS_AtomicInc(&currUtsContainer->rc);
child->container->utsContainer = currUtsContainer;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
return CreateUtsContainer(child, parent);
}
UINT32 OsUnshareUtsContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
{
UINT32 intSave;
UtsContainer *parentContainer = curr->container->utsContainer;
if (!(flags & CLONE_NEWUTS)) {
SCHEDULER_LOCK(intSave);
newContainer->utsContainer = parentContainer;
LOS_AtomicInc(&parentContainer->rc);
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
UtsContainer *utsContainer = CreateNewUtsContainer(parentContainer);
if (utsContainer == NULL) {
return ENOMEM;
}
SCHEDULER_LOCK(intSave);
newContainer->utsContainer = utsContainer;
g_currentUtsContainerNum++;
(VOID)memcpy_s(&utsContainer->utsName, sizeof(utsContainer->utsName),
&parentContainer->utsName, sizeof(parentContainer->utsName));
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
UINT32 OsSetNsUtsContainer(UINT32 flags, Container *container, Container *newContainer)
{
if (flags & CLONE_NEWUTS) {
newContainer->utsContainer = container->utsContainer;
LOS_AtomicInc(&container->utsContainer->rc);
return LOS_OK;
}
newContainer->utsContainer = OsCurrProcessGet()->container->utsContainer;
LOS_AtomicInc(&newContainer->utsContainer->rc);
return LOS_OK;
}
VOID OsUtsContainerDestroy(Container *container)
{
UINT32 intSave;
if (container == NULL) {
return;
}
SCHEDULER_LOCK(intSave);
UtsContainer *utsContainer = container->utsContainer;
if (utsContainer == NULL) {
SCHEDULER_UNLOCK(intSave);
return;
}
LOS_AtomicDec(&utsContainer->rc);
if (LOS_AtomicRead(&utsContainer->rc) > 0) {
SCHEDULER_UNLOCK(intSave);
return;
}
g_currentUtsContainerNum--;
container->utsContainer = NULL;
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, utsContainer);
return;
}
struct utsname *OsGetCurrUtsName(VOID)

View File

@@ -71,7 +71,7 @@ LITE_OS_SEC_BSS ProcessGroup *g_processGroup = NULL;
STATIC INLINE VOID OsInsertPCBToFreeList(LosProcessCB *processCB)
{
#ifdef LOSCFG_PID_CONTAINER
OsPidContainersDestroy(processCB);
OsPidContainerDestroy(processCB->container, processCB);
#endif
UINT32 pid = processCB->processID;
(VOID)memset_s(processCB, sizeof(LosProcessCB), 0, sizeof(LosProcessCB));
@@ -133,7 +133,7 @@ UINT32 OsProcessAddNewTask(UINTPTR processID, LosTaskCB *taskCB, SchedParam *par
return LOS_OK;
}
STATIC ProcessGroup *CreateProcessGroup(LosProcessCB *processCB)
ProcessGroup *OsCreateProcessGroup(LosProcessCB *processCB)
{
ProcessGroup *pgroup = LOS_MemAlloc(m_aucSysMem1, sizeof(ProcessGroup));
if (pgroup == NULL) {
@@ -163,9 +163,15 @@ STATIC VOID ExitProcessGroup(LosProcessCB *processCB, ProcessGroup **pgroup)
LosProcessCB *pgroupCB = OS_GET_PGROUP_LEADER(processCB->pgroup);
LOS_ListDelete(&processCB->subordinateGroupList);
if (LOS_ListEmpty(&processCB->pgroup->processList) && LOS_ListEmpty(&processCB->pgroup->exitProcessList)) {
LOS_ListDelete(&processCB->pgroup->groupList);
#ifdef LOSCFG_PID_CONTAINER
if (processCB->pgroup != OS_ROOT_PGRP(processCB)) {
#endif
LOS_ListDelete(&processCB->pgroup->groupList);
*pgroup = processCB->pgroup;
#ifdef LOSCFG_PID_CONTAINER
}
#endif
pgroupCB->processStatus &= ~OS_PROCESS_FLAG_GROUP_LEADER;
*pgroup = processCB->pgroup;
if (OsProcessIsUnused(pgroupCB) && !(pgroupCB->processStatus & OS_PROCESS_FLAG_EXIT)) {
LOS_ListDelete(&pgroupCB->pendList);
OsInsertPCBToFreeList(pgroupCB);
@@ -398,13 +404,6 @@ LITE_OS_SEC_TEXT VOID OsProcessResourcesToFree(LosProcessCB *processCB)
}
#endif
#ifdef LOSCFG_FS_VFS
if (OsProcessIsUserMode(processCB)) {
delete_files(processCB->files);
}
processCB->files = NULL;
#endif
#ifdef LOSCFG_SECURITY_CAPABILITY
if (processCB->user != NULL) {
(VOID)LOS_MemFree(m_aucSysMem1, processCB->user);
@@ -446,6 +445,13 @@ LITE_OS_SEC_TEXT VOID OsProcessResourcesToFree(LosProcessCB *processCB)
OsContainersDestroy(processCB);
#endif
#ifdef LOSCFG_FS_VFS
if (OsProcessIsUserMode(processCB)) {
delete_files(processCB->files);
}
processCB->files = NULL;
#endif
if (processCB->resourceLimit != NULL) {
(VOID)LOS_MemFree((VOID *)m_aucSysMem0, processCB->resourceLimit);
processCB->resourceLimit = NULL;
@@ -855,7 +861,7 @@ STATIC UINT32 OsSystemProcessInit(LosProcessCB *processCB, UINT32 flags, const C
}
#endif
ProcessGroup *pgroup = CreateProcessGroup(processCB);
ProcessGroup *pgroup = OsCreateProcessGroup(processCB);
if (pgroup == NULL) {
ret = LOS_ENOMEM;
goto EXIT;
@@ -1090,32 +1096,53 @@ STATIC VOID OsWaitInsertWaitListInOrder(LosTaskCB *runTask, LosProcessCB *proces
return;
}
STATIC UINT32 WaitFindSpecifiedProcess(UINT32 pid, LosTaskCB *runTask,
const LosProcessCB *processCB, LosProcessCB **childCB)
{
if (OS_PID_CHECK_INVALID((UINT32)pid)) {
return LOS_ECHILD;
}
LosProcessCB *waitProcess = OS_PCB_FROM_PID(pid);
if (OsProcessIsUnused(waitProcess)) {
return LOS_ECHILD;
}
#ifdef LOSCFG_PID_CONTAINER
if (OsPidContainerProcessParentIsRealParent(waitProcess, processCB)) {
*childCB = (LosProcessCB *)processCB;
return LOS_OK;
}
#endif
/* Wait for the child process whose process number is pid. */
*childCB = OsFindExitChildProcess(processCB, waitProcess);
if (*childCB != NULL) {
return LOS_OK;
}
if (OsFindChildProcess(processCB, waitProcess) != LOS_OK) {
return LOS_ECHILD;
}
runTask->waitFlag = OS_PROCESS_WAIT_PRO;
runTask->waitID = (UINTPTR)waitProcess;
return LOS_OK;
}
STATIC UINT32 OsWaitSetFlag(const LosProcessCB *processCB, INT32 pid, LosProcessCB **child)
{
UINT32 ret;
LosProcessCB *childCB = NULL;
LosTaskCB *runTask = OsCurrTaskGet();
if (pid > 0) {
if (OS_PID_CHECK_INVALID((UINT32)pid)) {
return LOS_ECHILD;
ret = WaitFindSpecifiedProcess((UINT32)pid, runTask, processCB, &childCB);
if (ret != LOS_OK) {
return ret;
}
LosProcessCB *waitProcess = OS_PCB_FROM_PID(pid);
if (OsProcessIsUnused(waitProcess)) {
return LOS_ECHILD;
}
/* Wait for the child process whose process number is pid. */
childCB = OsFindExitChildProcess(processCB, waitProcess);
if (childCB != NULL) {
goto WAIT_BACK;
}
if (OsFindChildProcess(processCB, waitProcess) != LOS_OK) {
return LOS_ECHILD;
}
runTask->waitFlag = OS_PROCESS_WAIT_PRO;
runTask->waitID = (UINTPTR)waitProcess;
} else if (pid == 0) {
/* Wait for any child process in the same process group */
childCB = OsFindGroupExitProcess(processCB->pgroup, OS_INVALID_VALUE);
@@ -1254,6 +1281,15 @@ STATIC INT32 OsWait(INT32 pid, USER INT32 *status, USER siginfo_t *info, UINT32
}
if (childCB != NULL) {
#ifdef LOSCFG_PID_CONTAINER
if (childCB == processCB) {
SCHEDULER_UNLOCK(intSave);
if (status != NULL) {
(VOID)LOS_ArchCopyToUser((VOID *)status, (const VOID *)(&ret), sizeof(INT32));
}
return pid;
}
#endif
return (INT32)OsWaitRecycleChildProcess(childCB, intSave, status, info);
}
@@ -1419,7 +1455,7 @@ STATIC UINT32 OsSetProcessGroupIDUnsafe(UINT32 pid, UINT32 gid, ProcessGroup **p
return LOS_OK;
}
newPGroup = CreateProcessGroup(pgroupCB);
newPGroup = OsCreateProcessGroup(pgroupCB);
if (newPGroup == NULL) {
LOS_ListTailInsert(&oldPGroup->processList, &processCB->subordinateGroupList);
processCB->pgroup = oldPGroup;
@@ -1850,30 +1886,26 @@ STATIC UINT32 OsCopyTask(UINT32 flags, LosProcessCB *childProcessCB, const CHAR
STATIC UINT32 OsCopyParent(UINT32 flags, LosProcessCB *childProcessCB, LosProcessCB *runProcessCB)
{
UINT32 ret;
UINT32 intSave;
LosProcessCB *parentProcessCB = NULL;
SCHEDULER_LOCK(intSave);
if (flags & CLONE_PARENT) {
parentProcessCB = runProcessCB->parentProcess;
} else {
parentProcessCB = runProcessCB;
if (childProcessCB->parentProcess == NULL) {
if (flags & CLONE_PARENT) {
parentProcessCB = runProcessCB->parentProcess;
} else {
parentProcessCB = runProcessCB;
}
childProcessCB->parentProcess = parentProcessCB;
LOS_ListTailInsert(&parentProcessCB->childrenList, &childProcessCB->siblingList);
}
childProcessCB->parentProcess = parentProcessCB;
LOS_ListTailInsert(&parentProcessCB->childrenList, &childProcessCB->siblingList);
if (!(flags & CLONE_NEWPID)) {
if (childProcessCB->pgroup == NULL) {
childProcessCB->pgroup = parentProcessCB->pgroup;
LOS_ListTailInsert(&parentProcessCB->pgroup->processList, &childProcessCB->subordinateGroupList);
} else {
if (CreateProcessGroup(childProcessCB) == NULL) {
SCHEDULER_UNLOCK(intSave);
return LOS_ENOMEM;
}
}
ret = OsCopyUser(childProcessCB, parentProcessCB);
SCHEDULER_UNLOCK(intSave);
return ret;
return LOS_OK;
}
STATIC UINT32 OsCopyMM(UINT32 flags, LosProcessCB *childProcessCB, LosProcessCB *runProcessCB)
@@ -1893,7 +1925,7 @@ STATIC UINT32 OsCopyMM(UINT32 flags, LosProcessCB *childProcessCB, LosProcessCB
return LOS_OK;
}
status = LOS_VmSpaceClone(runProcessCB->vmSpace, childProcessCB->vmSpace);
status = LOS_VmSpaceClone(flags, runProcessCB->vmSpace, childProcessCB->vmSpace);
if (status != LOS_OK) {
return LOS_ENOMEM;
}
@@ -1906,11 +1938,20 @@ STATIC UINT32 OsCopyFile(UINT32 flags, LosProcessCB *childProcessCB, LosProcessC
if (flags & CLONE_FILES) {
childProcessCB->files = runProcessCB->files;
} else {
#ifdef LOSCFG_IPC_CONTAINER
if (flags & CLONE_NEWIPC) {
OsCurrTaskGet()->cloneIpc = TRUE;
}
#endif
childProcessCB->files = dup_fd(runProcessCB->files);
#ifdef LOSCFG_IPC_CONTAINER
OsCurrTaskGet()->cloneIpc = FALSE;
#endif
}
if (childProcessCB->files == NULL) {
return LOS_ENOMEM;
}
#ifdef LOSCFG_PROC_PROCESS_DIR
INT32 ret = ProcCreateProcessDir(OsGetRootPid(childProcessCB), (UINTPTR)childProcessCB);
if (ret < 0) {
@@ -1930,11 +1971,6 @@ STATIC UINT32 OsForkInitPCB(UINT32 flags, LosProcessCB *child, const CHAR *name,
UINT32 ret;
LosProcessCB *run = OsCurrProcessGet();
ret = OsInitPCB(child, run->processMode, name);
if (ret != LOS_OK) {
return ret;
}
ret = OsCopyParent(flags, child, run);
if (ret != LOS_OK) {
return ret;
@@ -1971,6 +2007,11 @@ STATIC UINT32 OsCopyProcessResources(UINT32 flags, LosProcessCB *child, LosProce
{
UINT32 ret;
ret = OsCopyUser(child, run);
if (ret != LOS_OK) {
return ret;
}
ret = OsCopyMM(flags, child, run);
if (ret != LOS_OK) {
return ret;
@@ -1993,7 +2034,6 @@ STATIC UINT32 OsCopyProcessResources(UINT32 flags, LosProcessCB *child, LosProce
#ifdef LOSCFG_SECURITY_CAPABILITY
OsCopyCapability(run, child);
#endif
return LOS_OK;
}
@@ -2008,6 +2048,11 @@ STATIC INT32 OsCopyProcess(UINT32 flags, const CHAR *name, UINTPTR sp, UINT32 si
}
processID = child->processID;
ret = OsInitPCB(child, run->processMode, name);
if (ret != LOS_OK) {
goto ERROR_INIT;
}
#ifdef LOSCFG_KERNEL_CONTAINER
ret = OsCopyContainers(flags, child, run, &processID);
if (ret != LOS_OK) {
@@ -2050,10 +2095,18 @@ LITE_OS_SEC_TEXT INT32 OsClone(UINT32 flags, UINTPTR sp, UINT32 size)
#ifdef LOSCFG_KERNEL_CONTAINER
#ifdef LOSCFG_PID_CONTAINER
cloneFlag |= CLONE_NEWPID;
LosProcessCB *curr = OsCurrProcessGet();
if (((flags & CLONE_NEWPID) != 0) && ((flags & (CLONE_PARENT | CLONE_THREAD)) != 0)) {
return -LOS_EINVAL;
}
if (OS_PROCESS_PID_FOR_CONTAINER_CHECK(curr) && ((flags & CLONE_NEWPID) != 0)) {
return -LOS_EINVAL;
}
if (OS_PROCESS_PID_FOR_CONTAINER_CHECK(curr) && ((flags & (CLONE_PARENT | CLONE_THREAD)) != 0)) {
return -LOS_EINVAL;
}
#endif
#ifdef LOSCFG_UTS_CONTAINER
cloneFlag |= CLONE_NEWUTS;
@@ -2061,6 +2114,15 @@ LITE_OS_SEC_TEXT INT32 OsClone(UINT32 flags, UINTPTR sp, UINT32 size)
#ifdef LOSCFG_MNT_CONTAINER
cloneFlag |= CLONE_NEWNS;
#endif
#ifdef LOSCFG_IPC_CONTAINER
cloneFlag |= CLONE_NEWIPC;
if (((flags & CLONE_NEWIPC) != 0) && ((flags & CLONE_FILES) != 0)) {
return -LOS_EINVAL;
}
#endif
#ifdef LOSCFG_TIME_CONTAINER
cloneFlag |= CLONE_NEWTIME;
#endif
#endif
if (flags & (~cloneFlag)) {

View File

@@ -42,18 +42,30 @@
#ifdef LOSCFG_MNT_CONTAINER
#include "los_mnt_container_pri.h"
#endif
#ifdef LOSCFG_IPC_CONTAINER
#include "los_ipc_container_pri.h"
#endif
#ifdef LOSCFG_TIME_CONTAINER
#include "los_time_container_pri.h"
#endif
typedef enum {
CONTAINER = 0,
PID_CONTAINER,
PID_CHILD_CONTAINER,
UTS_CONTAINER,
MNT_CONTAINER,
IPC_CONTAINER,
TIME_CONTAINER,
TIME_CHILD_CONTAINER,
CONTAINER_MAX,
} ContainerType;
typedef struct Container {
Atomic rc;
#ifdef LOSCFG_PID_CONTAINER
struct PidContainer *pidContainer;
struct PidContainer *pidForChildContainer;
#endif
#ifdef LOSCFG_UTS_CONTAINER
struct UtsContainer *utsContainer;
@@ -61,6 +73,13 @@ typedef struct Container {
#ifdef LOSCFG_MNT_CONTAINER
struct MntContainer *mntContainer;
#endif
#ifdef LOSCFG_IPC_CONTAINER
struct IpcContainer *ipcContainer;
#endif
#ifdef LOSCFG_TIME_CONTAINER
struct TimeContainer *timeContainer;
struct TimeContainer *timeForChildContainer;
#endif
} Container;
VOID OsContainerInitSystemProcess(LosProcessCB *processCB);
@@ -71,8 +90,15 @@ UINT32 OsCopyContainers(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent
VOID OsContainersDestroy(LosProcessCB *processCB);
VOID OsContainerFree(LosProcessCB *processCB);
UINT32 OsAllocContainerID(VOID);
UINT32 OsGetContainerID(Container *container, ContainerType type);
INT32 OsUnshare(UINT32 flags);
INT32 OsSetNs(INT32 fd, INT32 type);
#endif
#endif /* _LOS_CONTAINER_PRI_H */

View File

@@ -0,0 +1,96 @@
/*
* 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.
*/
#ifndef _LOS_IPC_CONTAINER_PRI_H
#define _LOS_IPC_CONTAINER_PRI_H
#include "los_atomic.h"
#include "los_list.h"
#include "mqueue.h"
#include "fs/file.h"
#ifdef LOSCFG_IPC_CONTAINER
struct shmIDSource;
struct Container;
typedef struct TagQueueCB LosQueueCB;
typedef struct OsMux LosMux;
typedef LosMux pthread_mutex_t;
typedef struct ProcessCB LosProcessCB;
typedef struct IpcContainer {
Atomic rc;
LosQueueCB *allQueue;
LOS_DL_LIST freeQueueList;
fd_set queueFdSet;
struct mqarray queueTable[LOSCFG_BASE_IPC_QUEUE_LIMIT];
pthread_mutex_t mqueueMutex;
struct mqpersonal *mqPrivBuf[MAX_MQ_FD];
struct shminfo shmInfo;
LosMux sysvShmMux;
struct shmIDSource *shmSegs;
UINT32 shmUsedPageCount;
UINT32 containerID;
} IpcContainer;
UINT32 OsInitRootIpcContainer(IpcContainer **ipcContainer);
UINT32 OsCopyIpcContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent);
UINT32 OsUnshareIpcContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
UINT32 OsSetNsIpcContainer(UINT32 flags, struct Container *container, struct Container *newContainer);
VOID OsIpcContainerDestroy(struct Container *container);
UINT32 OsGetIpcContainerID(IpcContainer *ipcContainer);
IpcContainer *OsGetCurrIpcContainer(VOID);
#define IPC_ALL_QUEUE (OsGetCurrIpcContainer()->allQueue)
#define FREE_QUEUE_LIST (OsGetCurrIpcContainer()->freeQueueList)
#define IPC_QUEUE_FD_SET (OsGetCurrIpcContainer()->queueFdSet)
#define IPC_QUEUE_TABLE (OsGetCurrIpcContainer()->queueTable)
#define IPC_QUEUE_MUTEX (OsGetCurrIpcContainer()->mqueueMutex)
#define IPC_QUEUE_MQ_PRIV_BUF (OsGetCurrIpcContainer()->mqPrivBuf)
#define IPC_SHM_INFO (OsGetCurrIpcContainer()->shmInfo)
#define IPC_SHM_SYS_VSHM_MUTEX (OsGetCurrIpcContainer()->sysvShmMux)
#define IPC_SHM_SEGS (OsGetCurrIpcContainer()->shmSegs)
#define IPC_SHM_USED_PAGE_COUNT (OsGetCurrIpcContainer()->shmUsedPageCount)
#endif
#endif /* _LOS_IPC_CONTAINER_PRI_H */

View File

@@ -39,6 +39,7 @@
#ifdef LOSCFG_MNT_CONTAINER
typedef struct ProcessCB LosProcessCB;
struct Container;
typedef struct MntContainer {
Atomic rc;
@@ -52,7 +53,11 @@ UINT32 OsInitRootMntContainer(MntContainer **mntContainer);
UINT32 OsCopyMntContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent);
VOID OsMntContainersDestroy(LosProcessCB *curr);
UINT32 OsUnshareMntContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
UINT32 OsSetNsMntContainer(UINT32 flags, struct Container *container, struct Container *newContainer);
VOID OsMntContainerDestroy(struct Container *container);
UINT32 OsGetMntContainerID(MntContainer *mntContainer);

View File

@@ -37,11 +37,13 @@
typedef struct TagTaskCB LosTaskCB;
typedef struct ProcessCB LosProcessCB;
struct ProcessGroup;
struct Container;
typedef struct {
UINT32 vid; /* Virtual ID */
UINT32 vpid; /* Virtual parent ID */
UINTPTR cb; /* Control block */
LosProcessCB *realParent; /* process real parent */
LOS_DL_LIST node;
} ProcessVid;
@@ -51,6 +53,7 @@ typedef struct PidContainer {
Atomic rc;
Atomic level;
Atomic lock;
BOOL referenced;
UINT32 containerID;
struct PidContainer *parent;
struct ProcessGroup *rootPGroup;
@@ -67,14 +70,23 @@ typedef struct PidContainer {
#define OS_PROCESS_CONTAINER_CHECK(processCB, currProcessCB) \
((processCB)->container->pidContainer != (currProcessCB)->container->pidContainer)
UINT32 OsAllocSpecifiedVpidUnsafe(UINT32 vpid, LosProcessCB *processCB, LosProcessCB *parent);
#define OS_PROCESS_PID_FOR_CONTAINER_CHECK(processCB) \
(((processCB)->container->pidContainer != (processCB)->container->pidForChildContainer) && \
((processCB)->container->pidForChildContainer->referenced == FALSE))
VOID OsPidContainersDestroyAllProcess(LosProcessCB *processCB);
UINT32 OsAllocSpecifiedVpidUnsafe(UINT32 vpid, PidContainer *pidContainer,
LosProcessCB *processCB, LosProcessCB *parent);
VOID OsPidContainersDestroy(LosProcessCB *curr);
VOID OsPidContainerDestroyAllProcess(LosProcessCB *processCB);
VOID OsPidContainerDestroy(struct Container *container, LosProcessCB *processCB);
UINT32 OsCopyPidContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID);
UINT32 OsUnsharePidContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
UINT32 OsSetNsPidContainer(UINT32 flags, struct Container *container, struct Container *newContainer);
UINT32 OsInitRootPidContainer(PidContainer **pidContainer);
LosProcessCB *OsGetPCBFromVpid(UINT32 vpid);
@@ -93,4 +105,5 @@ UINT32 OsAllocVtid(LosTaskCB *taskCB, const LosProcessCB *processCB);
UINT32 OsGetPidContainerID(PidContainer *pidContainer);
BOOL OsPidContainerProcessParentIsRealParent(const LosProcessCB *processCB, const LosProcessCB *curr);
#endif /* _LOS_PID_CONTAINER_PRI_H */

View File

@@ -131,7 +131,7 @@ typedef struct ProcessCB {
#endif
struct rlimit *resourceLimit;
#ifdef LOSCFG_KERNEL_CONTAINER
struct Container *container;
Container *container;
#endif
#ifdef LOSCFG_PROC_PROCESS_DIR
struct ProcDirEntry *procDir;
@@ -541,6 +541,7 @@ extern VOID OsDeleteTaskFromProcess(LosTaskCB *taskCB);
extern VOID OsProcessThreadGroupDestroy(VOID);
extern UINT32 OsGetProcessGroupCB(UINT32 pid, UINTPTR *ppgroupLeader);
extern LosProcessCB *OsGetDefaultProcessCB(VOID);
extern ProcessGroup *OsCreateProcessGroup(LosProcessCB *processCB);
#ifdef __cplusplus
#if __cplusplus
}

View File

@@ -65,7 +65,7 @@ typedef enum {
* @ingroup los_queue
* Queue information block structure
*/
typedef struct {
typedef struct TagQueueCB {
UINT8 *queueHandle; /**< Pointer to a queue handle */
UINT16 queueState; /**< Queue state */
UINT16 queueLen; /**< Queue length */
@@ -108,6 +108,9 @@ typedef struct {
* Queue information control block
*/
extern LosQueueCB *g_allQueue;
#ifndef LOSCFG_IPC_CONTAINER
#define IPC_ALL_QUEUE g_allQueue
#endif
/**
* @ingroup los_queue
@@ -137,7 +140,7 @@ extern LosQueueCB *g_allQueue;
* Obtain a handle of the queue that has a specified ID.
*
*/
#define GET_QUEUE_HANDLE(queueID) (((LosQueueCB *)g_allQueue) + GET_QUEUE_INDEX(queueID))
#define GET_QUEUE_HANDLE(queueID) (((LosQueueCB *)IPC_ALL_QUEUE) + GET_QUEUE_INDEX(queueID))
/**
* @ingroup los_queue
@@ -196,6 +199,8 @@ extern VOID *OsQueueMailAlloc(UINT32 queueID, VOID *mailPool, UINT32 timeout);
*/
extern UINT32 OsQueueMailFree(UINT32 queueID, VOID *mailPool, VOID *mailMem);
extern LosQueueCB *OsAllQueueCBInit(LOS_DL_LIST *freeQueueList);
extern UINT32 OsQueueInit(VOID);
#ifdef __cplusplus

View File

@@ -428,6 +428,9 @@ typedef struct TagTaskCB {
#ifdef LOSCFG_PID_CONTAINER
PidContainer *pidContainer;
#endif
#ifdef LOSCFG_IPC_CONTAINER
BOOL cloneIpc;
#endif
} LosTaskCB;
STATIC INLINE BOOL OsTaskIsRunning(const LosTaskCB *taskCB)

View File

@@ -0,0 +1,68 @@
/*
* Copyright (c) 2022-2022 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.
*/
#ifndef _LOS_TIME_CONTAINER_PRI_H
#define _LOS_TIME_CONTAINER_PRI_H
#include "time.h"
#include "los_atomic.h"
#ifdef LOSCFG_TIME_CONTAINER
typedef struct ProcessCB LosProcessCB;
struct Container;
typedef struct TimeContainer {
Atomic rc;
BOOL frozenOffsets;
struct timespec64 monotonic;
UINT32 containerID;
} TimeContainer;
UINT32 OsInitRootTimeContainer(TimeContainer **timeContainer);
UINT32 OsCopyTimeContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent);
UINT32 OsUnshareTimeContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
UINT32 OsSetNsTimeContainer(UINT32 flags, struct Container *container, struct Container *newContainer);
VOID OsTimeContainerDestroy(struct Container *container);
UINT32 OsGetTimeContainerID(TimeContainer *timeContainer);
TimeContainer *OsGetCurrTimeContainer(VOID);
UINT32 OsGetTimeContainerMonotonic(LosProcessCB *processCB, struct timespec64 *offsets);
UINT32 OsSetTimeContainerMonotonic(LosProcessCB *processCB, struct timespec64 *offsets);
#define CLOCK_MONOTONIC_TIME_BASE (OsGetCurrTimeContainer()->monotonic)
#endif
#endif /* _LOS_TIME_CONTAINER_PRI_H */

View File

@@ -38,6 +38,8 @@
#ifdef LOSCFG_UTS_CONTAINER
typedef struct ProcessCB LosProcessCB;
struct Container;
typedef struct UtsContainer {
Atomic rc;
UINT32 containerID;
@@ -48,10 +50,15 @@ UINT32 OsInitRootUtsContainer(UtsContainer **utsContainer);
UINT32 OsCopyUtsContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent);
VOID OsUtsContainersDestroy(LosProcessCB *curr);
UINT32 OsUnshareUtsContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
UINT32 OsSetNsUtsContainer(UINT32 flags, struct Container *container, struct Container *newContainer);
VOID OsUtsContainerDestroy(struct Container *container);
struct utsname *OsGetCurrUtsName(VOID);
UINT32 OsGetUtsContainerID(UtsContainer *utsContainer);
#endif
#endif /* _LOS_UTS_CONTAINER_PRI_H */

View File

@@ -1,6 +1,6 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
* Copyright (c) 2020-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:
@@ -290,7 +290,7 @@ STATUS_T LOS_VmSpaceFree(LosVmSpace *space);
STATUS_T LOS_VaddrToPaddrMmap(LosVmSpace *space, VADDR_T vaddr, PADDR_T paddr, size_t len, UINT32 flags);
BOOL OsUserVmSpaceInit(LosVmSpace *vmSpace, VADDR_T *virtTtb);
LosVmSpace *OsCreateUserVmSpace(VOID);
STATUS_T LOS_VmSpaceClone(LosVmSpace *oldVmSpace, LosVmSpace *newVmSpace);
STATUS_T LOS_VmSpaceClone(UINT32 cloneFlags, LosVmSpace *oldVmSpace, LosVmSpace *newVmSpace);
LosMux *OsGVmSpaceMuxGet(VOID);
STATUS_T OsUnMMap(LosVmSpace *space, VADDR_T addr, size_t size);
STATUS_T OsVmSpaceRegionFree(LosVmSpace *space);

View File

@@ -1,6 +1,6 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
* Copyright (c) 2020-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:
@@ -34,17 +34,36 @@
#include "los_typedef.h"
#include "los_vm_map.h"
#include "los_process_pri.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
/* The upper limit size of total shared memory is default 16M */
#define SHM_MAX_PAGES 4096
#define SHM_MAX (SHM_MAX_PAGES * PAGE_SIZE)
#define SHM_MIN 1
#define SHM_MNI 192
#define SHM_SEG 128
#define SHM_ALL (SHM_MAX_PAGES)
struct shmIDSource {
struct shmid_ds ds;
UINT32 status;
LOS_DL_LIST node;
#ifdef LOSCFG_SHELL
CHAR ownerName[OS_PCB_NAME_LEN];
#endif
};
VOID OsShmFork(LosVmSpace *space, LosVmMapRegion *oldRegion, LosVmMapRegion *newRegion);
VOID OsShmRegionFree(LosVmSpace *space, LosVmMapRegion *region);
BOOL OsIsShmRegion(LosVmMapRegion *region);
struct shmIDSource *OsShmCBInit(LosMux *sysvShmMux, struct shminfo *shmInfo, UINT32 *shmUsedPageCount);
VOID OsShmCBDestroy(struct shmIDSource *shmSegs, struct shminfo *shmInfo, LosMux *sysvShmMux);
#ifdef __cplusplus
#if __cplusplus
}

View File

@@ -1,6 +1,6 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved.
* Copyright (c) 2020-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:
@@ -37,14 +37,51 @@
#include "los_mp.h"
#include "los_percpu_pri.h"
#include "los_hook.h"
#ifdef LOSCFG_IPC_CONTAINER
#include "los_ipc_container_pri.h"
#endif
#ifdef LOSCFG_BASE_IPC_QUEUE
#if (LOSCFG_BASE_IPC_QUEUE_LIMIT <= 0)
#error "queue maxnum cannot be zero"
#endif /* LOSCFG_BASE_IPC_QUEUE_LIMIT <= 0 */
#ifndef LOSCFG_IPC_CONTAINER
LITE_OS_SEC_BSS LosQueueCB *g_allQueue = NULL;
LITE_OS_SEC_BSS STATIC LOS_DL_LIST g_freeQueueList;
#define FREE_QUEUE_LIST g_freeQueueList
#endif
LITE_OS_SEC_TEXT_INIT LosQueueCB *OsAllQueueCBInit(LOS_DL_LIST *freeQueueList)
{
UINT32 index;
if (freeQueueList == NULL) {
return NULL;
}
UINT32 size = LOSCFG_BASE_IPC_QUEUE_LIMIT * sizeof(LosQueueCB);
/* system resident memory, don't free */
LosQueueCB *allQueue = (LosQueueCB *)LOS_MemAlloc(m_aucSysMem0, size);
if (allQueue == NULL) {
return NULL;
}
(VOID)memset_s(allQueue, size, 0, size);
LOS_ListInit(freeQueueList);
for (index = 0; index < LOSCFG_BASE_IPC_QUEUE_LIMIT; index++) {
LosQueueCB *queueNode = ((LosQueueCB *)allQueue) + index;
queueNode->queueID = index;
LOS_ListTailInsert(freeQueueList, &queueNode->readWriteList[OS_QUEUE_WRITE]);
}
#ifndef LOSCFG_IPC_CONTAINER
if (OsQueueDbgInitHook() != LOS_OK) {
(VOID)LOS_MemFree(m_aucSysMem0, allQueue);
return NULL;
}
#endif
return allQueue;
}
/*
* Description : queue initial
@@ -52,27 +89,12 @@ LITE_OS_SEC_BSS STATIC LOS_DL_LIST g_freeQueueList;
*/
LITE_OS_SEC_TEXT_INIT UINT32 OsQueueInit(VOID)
{
LosQueueCB *queueNode = NULL;
UINT32 index;
UINT32 size;
size = LOSCFG_BASE_IPC_QUEUE_LIMIT * sizeof(LosQueueCB);
/* system resident memory, don't free */
g_allQueue = (LosQueueCB *)LOS_MemAlloc(m_aucSysMem0, size);
#ifndef LOSCFG_IPC_CONTAINER
g_allQueue = OsAllQueueCBInit(&g_freeQueueList);
if (g_allQueue == NULL) {
return LOS_ERRNO_QUEUE_NO_MEMORY;
}
(VOID)memset_s(g_allQueue, size, 0, size);
LOS_ListInit(&g_freeQueueList);
for (index = 0; index < LOSCFG_BASE_IPC_QUEUE_LIMIT; index++) {
queueNode = ((LosQueueCB *)g_allQueue) + index;
queueNode->queueID = index;
LOS_ListTailInsert(&g_freeQueueList, &queueNode->readWriteList[OS_QUEUE_WRITE]);
}
if (OsQueueDbgInitHook() != LOS_OK) {
return LOS_ERRNO_QUEUE_NO_MEMORY;
}
#endif
return LOS_OK;
}
@@ -111,14 +133,14 @@ LITE_OS_SEC_TEXT_INIT UINT32 LOS_QueueCreate(CHAR *queueName, UINT16 len, UINT32
}
SCHEDULER_LOCK(intSave);
if (LOS_ListEmpty(&g_freeQueueList)) {
if (LOS_ListEmpty(&FREE_QUEUE_LIST)) {
SCHEDULER_UNLOCK(intSave);
OsQueueCheckHook();
(VOID)LOS_MemFree(m_aucSysMem1, queue);
return LOS_ERRNO_QUEUE_CB_UNAVAILABLE;
}
unusedQueue = LOS_DL_LIST_FIRST(&g_freeQueueList);
unusedQueue = LOS_DL_LIST_FIRST(&FREE_QUEUE_LIST);
LOS_ListDelete(unusedQueue);
queueCB = GET_QUEUE_LIST(unusedQueue);
queueCB->queueLen = len;
@@ -433,7 +455,7 @@ LITE_OS_SEC_TEXT_INIT UINT32 LOS_QueueDelete(UINT32 queueID)
queueCB->queueID = SET_QUEUE_ID(GET_QUEUE_COUNT(queueCB->queueID) + 1, GET_QUEUE_INDEX(queueCB->queueID));
OsQueueDbgUpdateHook(queueCB->queueID, NULL);
LOS_ListTailInsert(&g_freeQueueList, &queueCB->readWriteList[OS_QUEUE_WRITE]);
LOS_ListTailInsert(&FREE_QUEUE_LIST, &queueCB->readWriteList[OS_QUEUE_WRITE]);
SCHEDULER_UNLOCK(intSave);
OsHookCall(LOS_HOOK_TYPE_QUEUE_DELETE, queueCB);
ret = LOS_MemFree(m_aucSysMem1, (VOID *)queue);

View File

@@ -837,6 +837,9 @@ STATIC UINT32 OsMemPoolInit(VOID *pool, UINT32 size)
struct OsMemPoolHead *poolHead = (struct OsMemPoolHead *)pool;
struct OsMemNodeHead *newNode = NULL;
struct OsMemNodeHead *endNode = NULL;
(VOID)memset_s(poolHead, sizeof(struct OsMemPoolHead), 0, sizeof(struct OsMemPoolHead));
#ifdef LOSCFG_KERNEL_LMS
UINT32 resize = 0;
if (g_lms != NULL) {
@@ -848,7 +851,6 @@ STATIC UINT32 OsMemPoolInit(VOID *pool, UINT32 size)
size = (resize == 0) ? size : resize;
}
#endif
(VOID)memset(poolHead, 0, sizeof(struct OsMemPoolHead));
LOS_SpinInit(&poolHead->spinlock);
poolHead->info.pool = pool;
@@ -885,9 +887,14 @@ STATIC UINT32 OsMemPoolInit(VOID *pool, UINT32 size)
}
#ifdef LOSCFG_MEM_MUL_POOL
STATIC VOID OsMemPoolDeinit(VOID *pool)
STATIC VOID OsMemPoolDeinit(const VOID *pool, UINT32 size)
{
(VOID)memset(pool, 0, sizeof(struct OsMemPoolHead));
#ifdef LOSCFG_KERNEL_LMS
if (g_lms != NULL) {
g_lms->deInit(pool);
}
#endif
(VOID)memset_s(pool, size, 0, sizeof(struct OsMemPoolHead));
}
STATIC UINT32 OsMemPoolAdd(VOID *pool, UINT32 size)
@@ -961,7 +968,7 @@ UINT32 LOS_MemInit(VOID *pool, UINT32 size)
#ifdef LOSCFG_MEM_MUL_POOL
if (OsMemPoolAdd(pool, size)) {
(VOID)OsMemPoolDeinit(pool);
(VOID)OsMemPoolDeInit(pool, size);
return OS_ERROR;
}
#endif
@@ -973,17 +980,21 @@ UINT32 LOS_MemInit(VOID *pool, UINT32 size)
#ifdef LOSCFG_MEM_MUL_POOL
UINT32 LOS_MemDeInit(VOID *pool)
{
if (pool == NULL) {
struct OsMemPoolHead *tmpPool = (struct OsMemPoolHead *)pool;
if ((tmpPool == NULL) ||
(tmpPool->info.pool != pool) ||
(tmpPool->info.totalSize <= OS_MEM_MIN_POOL_SIZE)) {
return OS_ERROR;
}
if (OsMemPoolDelete(pool)) {
if (OsMemPoolDelete(tmpPool)) {
return OS_ERROR;
}
OsMemPoolDeinit(pool);
OsMemPoolDeInit(tmpPool, tmpPool->info.totalSize);
OsHookCall(LOS_HOOK_TYPE_MEM_DEINIT, pool);
OsHookCall(LOS_HOOK_TYPE_MEM_DEINIT, tmpPool);
return LOS_OK;
}
@@ -1896,7 +1907,7 @@ UINT32 LOS_MemInfoGet(VOID *pool, LOS_MEM_POOL_STATUS *poolStatus)
return LOS_NOK;
}
(VOID)memset(poolStatus, 0, sizeof(LOS_MEM_POOL_STATUS));
(VOID)memset_s(poolStatus, sizeof(LOS_MEM_POOL_STATUS), 0, sizeof(LOS_MEM_POOL_STATUS));
struct OsMemNodeHead *tmpNode = NULL;
struct OsMemNodeHead *endNode = NULL;
@@ -2070,5 +2081,3 @@ BOOL OsMemIsHeapNode(const VOID *ptr)
#endif
return FALSE;
}

View File

@@ -35,6 +35,9 @@
#include "los_queue_pri.h"
#include "los_swtmr_pri.h"
#include "los_task_pri.h"
#ifdef LOSCFG_IPC_CONTAINER
#include "los_ipc_container_pri.h"
#endif
#ifdef LOSCFG_SHELL
#include "shcmd.h"
@@ -88,7 +91,7 @@ UINT32 OsShellCmdQueueCntGet(VOID)
LosQueueCB *queueCB = NULL;
intSave = LOS_IntLock();
queueCB = g_allQueue;
queueCB = IPC_ALL_QUEUE;
for (loop = 0; loop < LOSCFG_BASE_IPC_QUEUE_LIMIT; loop++, queueCB++) {
if (queueCB->queueState == OS_QUEUE_INUSED) {
queueCnt++;

View File

@@ -1,6 +1,6 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
* Copyright (c) 2020-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:
@@ -241,20 +241,15 @@ STATIC BOOL OsVmSpaceParamCheck(const LosVmSpace *vmSpace)
return TRUE;
}
STATUS_T LOS_VmSpaceClone(LosVmSpace *oldVmSpace, LosVmSpace *newVmSpace)
STATUS_T LOS_VmSpaceClone(UINT32 cloneFlags, LosVmSpace *oldVmSpace, LosVmSpace *newVmSpace)
{
LosVmMapRegion *oldRegion = NULL;
LosVmMapRegion *newRegion = NULL;
LosRbNode *pstRbNode = NULL;
LosRbNode *pstRbNodeNext = NULL;
STATUS_T ret = LOS_OK;
UINT32 numPages;
PADDR_T paddr;
VADDR_T vaddr;
UINT32 intSave;
LosVmPage *page = NULL;
UINT32 flags;
UINT32 i;
UINT32 flags, i, intSave, numPages;
if ((OsVmSpaceParamCheck(oldVmSpace) == FALSE) || (OsVmSpaceParamCheck(newVmSpace) == FALSE)) {
return LOS_ERRNO_VM_INVALID_ARGS;
@@ -270,8 +265,13 @@ STATUS_T LOS_VmSpaceClone(LosVmSpace *oldVmSpace, LosVmSpace *newVmSpace)
newVmSpace->heapNow = oldVmSpace->heapNow;
(VOID)LOS_MuxAcquire(&oldVmSpace->regionMux);
RB_SCAN_SAFE(&oldVmSpace->regionRbTree, pstRbNode, pstRbNodeNext)
oldRegion = (LosVmMapRegion *)pstRbNode;
newRegion = OsVmRegionDup(newVmSpace, oldRegion, oldRegion->range.base, oldRegion->range.size);
LosVmMapRegion *oldRegion = (LosVmMapRegion *)pstRbNode;
#if defined(LOSCFG_KERNEL_SHM) && defined(LOSCFG_IPC_CONTAINER)
if ((oldRegion->regionFlags & VM_MAP_REGION_FLAG_SHM) && (cloneFlags & CLONE_NEWIPC)) {
continue;
}
#endif
LosVmMapRegion *newRegion = OsVmRegionDup(newVmSpace, oldRegion, oldRegion->range.base, oldRegion->range.size);
if (newRegion == NULL) {
VM_ERR("dup new region failed");
ret = LOS_ERRNO_VM_NO_MEMORY;

View File

@@ -1,6 +1,6 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
* Copyright (c) 2020-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:
@@ -33,7 +33,6 @@
#include "string.h"
#include "time.h"
#include "sys/types.h"
#include "sys/shm.h"
#include "sys/stat.h"
#include "los_config.h"
#include "los_init.h"
@@ -46,6 +45,8 @@
#include "los_process.h"
#include "los_process_pri.h"
#include "user_copy.h"
#include "los_vm_shm_pri.h"
#include "sys/shm.h"
#ifdef LOSCFG_SHELL
#include "shcmd.h"
#include "shell.h"
@@ -54,20 +55,6 @@
#ifdef LOSCFG_KERNEL_SHM
STATIC LosMux g_sysvShmMux;
/* private macro */
#define SYSV_SHM_LOCK() (VOID)LOS_MuxLock(&g_sysvShmMux, LOS_WAIT_FOREVER)
#define SYSV_SHM_UNLOCK() (VOID)LOS_MuxUnlock(&g_sysvShmMux)
/* The upper limit size of total shared memory is default 16M */
#define SHM_MAX_PAGES 4096
#define SHM_MAX (SHM_MAX_PAGES * PAGE_SIZE)
#define SHM_MIN 1
#define SHM_MNI 192
#define SHM_SEG 128
#define SHM_ALL (SHM_MAX_PAGES)
#define SHM_SEG_FREE 0x2000
#define SHM_SEG_USED 0x4000
#define SHM_SEG_REMOVE 0x8000
@@ -91,57 +78,75 @@ STATIC LosMux g_sysvShmMux;
#define SHM_GROUPE_TO_USER 3
#define SHM_OTHER_TO_USER 6
struct shmIDSource {
struct shmid_ds ds;
UINT32 status;
LOS_DL_LIST node;
#ifdef LOSCFG_SHELL
CHAR ownerName[OS_PCB_NAME_LEN];
#endif
};
#ifndef LOSCFG_IPC_CONTAINER
STATIC LosMux g_sysvShmMux;
/* private data */
STATIC struct shminfo g_shmInfo = {
.shmmax = SHM_MAX,
.shmmin = SHM_MIN,
.shmmni = SHM_MNI,
.shmseg = SHM_SEG,
.shmall = SHM_ALL,
};
STATIC struct shminfo g_shmInfo;
STATIC struct shmIDSource *g_shmSegs = NULL;
STATIC UINT32 g_shmUsedPageCount;
UINT32 ShmInit(VOID)
#define IPC_SHM_INFO g_shmInfo
#define IPC_SHM_SYS_VSHM_MUTEX g_sysvShmMux
#define IPC_SHM_SEGS g_shmSegs
#define IPC_SHM_USED_PAGE_COUNT g_shmUsedPageCount
#endif
/* private macro */
#define SYSV_SHM_LOCK() (VOID)LOS_MuxLock(&IPC_SHM_SYS_VSHM_MUTEX, LOS_WAIT_FOREVER)
#define SYSV_SHM_UNLOCK() (VOID)LOS_MuxUnlock(&IPC_SHM_SYS_VSHM_MUTEX)
struct shmIDSource *OsShmCBInit(LosMux *sysvShmMux, struct shminfo *shmInfo, UINT32 *shmUsedPageCount)
{
UINT32 ret;
UINT32 i;
ret = LOS_MuxInit(&g_sysvShmMux, NULL);
if ((sysvShmMux == NULL) || (shmInfo == NULL) || (shmUsedPageCount == NULL)) {
return NULL;
}
ret = LOS_MuxInit(sysvShmMux, NULL);
if (ret != LOS_OK) {
goto ERROR;
}
g_shmSegs = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, sizeof(struct shmIDSource) * g_shmInfo.shmmni);
if (g_shmSegs == NULL) {
(VOID)LOS_MuxDestroy(&g_sysvShmMux);
shmInfo->shmmax = SHM_MAX;
shmInfo->shmmin = SHM_MIN;
shmInfo->shmmni = SHM_MNI;
shmInfo->shmseg = SHM_SEG;
shmInfo->shmall = SHM_ALL;
struct shmIDSource *shmSegs = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, sizeof(struct shmIDSource) * shmInfo->shmmni);
if (shmSegs == NULL) {
(VOID)LOS_MuxDestroy(sysvShmMux);
goto ERROR;
}
(VOID)memset_s(g_shmSegs, (sizeof(struct shmIDSource) * g_shmInfo.shmmni),
0, (sizeof(struct shmIDSource) * g_shmInfo.shmmni));
(VOID)memset_s(shmSegs, (sizeof(struct shmIDSource) * shmInfo->shmmni),
0, (sizeof(struct shmIDSource) * shmInfo->shmmni));
for (i = 0; i < g_shmInfo.shmmni; i++) {
g_shmSegs[i].status = SHM_SEG_FREE;
g_shmSegs[i].ds.shm_perm.seq = i + 1;
LOS_ListInit(&g_shmSegs[i].node);
for (i = 0; i < shmInfo->shmmni; i++) {
shmSegs[i].status = SHM_SEG_FREE;
shmSegs[i].ds.shm_perm.seq = i + 1;
LOS_ListInit(&shmSegs[i].node);
}
g_shmUsedPageCount = 0;
*shmUsedPageCount = 0;
return LOS_OK;
return shmSegs;
ERROR:
VM_ERR("ShmInit fail\n");
return LOS_NOK;
return NULL;
}
UINT32 ShmInit(VOID)
{
#ifndef LOSCFG_IPC_CONTAINER
g_shmSegs = OsShmCBInit(&IPC_SHM_SYS_VSHM_MUTEX, &IPC_SHM_INFO, &IPC_SHM_USED_PAGE_COUNT);
if (g_shmSegs == NULL) {
return LOS_NOK;
}
#endif
return LOS_OK;
}
LOS_MODULE_INIT(ShmInit, LOS_INIT_LEVEL_VM_COMPLETE);
@@ -150,10 +155,10 @@ UINT32 ShmDeinit(VOID)
{
UINT32 ret;
(VOID)LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, g_shmSegs);
g_shmSegs = NULL;
(VOID)LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, IPC_SHM_SEGS);
IPC_SHM_SEGS = NULL;
ret = LOS_MuxDestroy(&g_sysvShmMux);
ret = LOS_MuxDestroy(&IPC_SHM_SYS_VSHM_MUTEX);
if (ret != LOS_OK) {
return -1;
}
@@ -195,18 +200,19 @@ STATIC INT32 ShmAllocSeg(key_t key, size_t size, INT32 shmflg)
struct shmIDSource *seg = NULL;
size_t count;
if ((size == 0) || (size < g_shmInfo.shmmin) ||
(size > g_shmInfo.shmmax)) {
if ((size == 0) || (size < IPC_SHM_INFO.shmmin) ||
(size > IPC_SHM_INFO.shmmax)) {
return -EINVAL;
}
size = LOS_Align(size, PAGE_SIZE);
if ((g_shmUsedPageCount + (size >> PAGE_SHIFT)) > g_shmInfo.shmall) {
if ((IPC_SHM_USED_PAGE_COUNT + (size >> PAGE_SHIFT)) > IPC_SHM_INFO.shmall) {
return -ENOMEM;
}
for (i = 0; i < g_shmInfo.shmmni; i++) {
if (g_shmSegs[i].status & SHM_SEG_FREE) {
g_shmSegs[i].status &= ~SHM_SEG_FREE;
for (i = 0; i < IPC_SHM_INFO.shmmni; i++) {
if (IPC_SHM_SEGS[i].status & SHM_SEG_FREE) {
IPC_SHM_SEGS[i].status &= ~SHM_SEG_FREE;
segNum = i;
break;
}
@@ -216,15 +222,16 @@ STATIC INT32 ShmAllocSeg(key_t key, size_t size, INT32 shmflg)
return -ENOSPC;
}
seg = &g_shmSegs[segNum];
seg = &IPC_SHM_SEGS[segNum];
count = LOS_PhysPagesAlloc(size >> PAGE_SHIFT, &seg->node);
if (count != (size >> PAGE_SHIFT)) {
(VOID)LOS_PhysPagesFree(&seg->node);
seg->status = SHM_SEG_FREE;
return -ENOMEM;
}
ShmSetSharedFlag(seg);
g_shmUsedPageCount += size >> PAGE_SHIFT;
IPC_SHM_USED_PAGE_COUNT += size >> PAGE_SHIFT;
seg->status |= SHM_SEG_USED;
seg->ds.shm_perm.mode = (UINT32)shmflg & ACCESSPERMS;
@@ -247,7 +254,7 @@ STATIC INT32 ShmAllocSeg(key_t key, size_t size, INT32 shmflg)
return segNum;
}
STATIC INLINE VOID ShmFreeSeg(struct shmIDSource *seg)
STATIC INLINE VOID ShmFreeSeg(struct shmIDSource *seg, UINT32 *shmUsedPageCount)
{
UINT32 count;
@@ -257,7 +264,9 @@ STATIC INLINE VOID ShmFreeSeg(struct shmIDSource *seg)
VM_ERR("free physical pages failed, count = %d, size = %d", count, seg->ds.shm_segsz >> PAGE_SHIFT);
return;
}
g_shmUsedPageCount -= seg->ds.shm_segsz >> PAGE_SHIFT;
if (shmUsedPageCount != NULL) {
(*shmUsedPageCount) -= seg->ds.shm_segsz >> PAGE_SHIFT;
}
seg->status = SHM_SEG_FREE;
LOS_ListInit(&seg->node);
}
@@ -267,8 +276,8 @@ STATIC INT32 ShmFindSegByKey(key_t key)
INT32 i;
struct shmIDSource *seg = NULL;
for (i = 0; i < g_shmInfo.shmmni; i++) {
seg = &g_shmSegs[i];
for (i = 0; i < IPC_SHM_INFO.shmmni; i++) {
seg = &IPC_SHM_SEGS[i];
if ((seg->status & SHM_SEG_USED) &&
(seg->ds.shm_perm.key == key)) {
return i;
@@ -280,7 +289,7 @@ STATIC INT32 ShmFindSegByKey(key_t key)
STATIC INT32 ShmSegValidCheck(INT32 segNum, size_t size, INT32 shmFlg)
{
struct shmIDSource *seg = &g_shmSegs[segNum];
struct shmIDSource *seg = &IPC_SHM_SEGS[segNum];
if (size > seg->ds.shm_segsz) {
return -EINVAL;
@@ -298,12 +307,12 @@ STATIC struct shmIDSource *ShmFindSeg(int shmid)
{
struct shmIDSource *seg = NULL;
if ((shmid < 0) || (shmid >= g_shmInfo.shmmni)) {
if ((shmid < 0) || (shmid >= IPC_SHM_INFO.shmmni)) {
set_errno(EINVAL);
return NULL;
}
seg = &g_shmSegs[shmid];
seg = &IPC_SHM_SEGS[shmid];
if ((seg->status & SHM_SEG_FREE) || ((seg->ds.shm_nattch == 0) && (seg->status & SHM_SEG_REMOVE))) {
set_errno(EIDRM);
return NULL;
@@ -364,7 +373,7 @@ VOID OsShmRegionFree(LosVmSpace *space, LosVmMapRegion *region)
ShmPagesRefDec(seg);
seg->ds.shm_nattch--;
if (seg->ds.shm_nattch <= 0 && (seg->status & SHM_SEG_REMOVE)) {
ShmFreeSeg(seg);
ShmFreeSeg(seg, &IPC_SHM_USED_PAGE_COUNT);
} else {
seg->ds.shm_dtime = time(NULL);
seg->ds.shm_lpid = LOS_GetCurrProcessID(); /* may not be the space's PID. */
@@ -383,8 +392,8 @@ STATIC INT32 ShmSegUsedCount(VOID)
INT32 count = 0;
struct shmIDSource *seg = NULL;
for (i = 0; i < g_shmInfo.shmmni; i++) {
seg = &g_shmSegs[i];
for (i = 0; i < IPC_SHM_INFO.shmmni; i++) {
seg = &IPC_SHM_SEGS[i];
if (seg->status & SHM_SEG_USED) {
count++;
}
@@ -672,16 +681,16 @@ INT32 ShmCtl(INT32 shmid, INT32 cmd, struct shmid_ds *buf)
seg->status |= SHM_SEG_REMOVE;
if (seg->ds.shm_nattch <= 0) {
ShmFreeSeg(seg);
ShmFreeSeg(seg, &IPC_SHM_USED_PAGE_COUNT);
}
break;
case IPC_INFO:
ret = LOS_ArchCopyToUser(buf, &g_shmInfo, sizeof(struct shminfo));
ret = LOS_ArchCopyToUser(buf, &IPC_SHM_INFO, sizeof(struct shminfo));
if (ret != 0) {
ret = EFAULT;
goto ERROR;
}
ret = g_shmInfo.shmmni;
ret = IPC_SHM_INFO.shmmni;
break;
case SHM_INFO:
shmInfo.shm_rss = 0;
@@ -695,7 +704,7 @@ INT32 ShmCtl(INT32 shmid, INT32 cmd, struct shmid_ds *buf)
ret = EFAULT;
goto ERROR;
}
ret = g_shmInfo.shmmni;
ret = IPC_SHM_INFO.shmmni;
break;
default:
VM_ERR("the cmd(%d) is not supported!", cmd);
@@ -758,7 +767,7 @@ INT32 ShmDt(const VOID *shmaddr)
seg->ds.shm_nattch--;
if ((seg->ds.shm_nattch <= 0) &&
(seg->status & SHM_SEG_REMOVE)) {
ShmFreeSeg(seg);
ShmFreeSeg(seg, &IPC_SHM_USED_PAGE_COUNT);
} else {
seg->ds.shm_dtime = time(NULL);
seg->ds.shm_lpid = LOS_GetCurrProcessID();
@@ -775,6 +784,27 @@ ERROR:
return -1;
}
VOID OsShmCBDestroy(struct shmIDSource *shmSegs, struct shminfo *shmInfo, LosMux *sysvShmMux)
{
if ((shmSegs == NULL) || (shmInfo == NULL) || (sysvShmMux == NULL)) {
return;
}
for (UINT32 index = 0; index < shmInfo->shmmni; index++) {
struct shmIDSource *seg = &shmSegs[index];
if (seg->status == SHM_SEG_FREE) {
continue;
}
(VOID)LOS_MuxLock(sysvShmMux, LOS_WAIT_FOREVER);
ShmFreeSeg(seg, NULL);
(VOID)LOS_MuxUnlock(sysvShmMux);
}
(VOID)LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, shmSegs);
(VOID)LOS_MuxDestroy(sysvShmMux);
}
#ifdef LOSCFG_SHELL
STATIC VOID OsShmInfoCmd(VOID)
{
@@ -784,8 +814,8 @@ STATIC VOID OsShmInfoCmd(VOID)
PRINTK("\r\n------- Shared Memory Segments -------\n");
PRINTK("key shmid perms bytes nattch status owner\n");
SYSV_SHM_LOCK();
for (i = 0; i < g_shmInfo.shmmni; i++) {
seg = &g_shmSegs[i];
for (i = 0; i < IPC_SHM_INFO.shmmni; i++) {
seg = &IPC_SHM_SEGS[i];
if (!(seg->status & SHM_SEG_USED)) {
continue;
}
@@ -801,7 +831,7 @@ STATIC VOID OsShmDeleteCmd(INT32 shmid)
{
struct shmIDSource *seg = NULL;
if ((shmid < 0) || (shmid >= g_shmInfo.shmmni)) {
if ((shmid < 0) || (shmid >= IPC_SHM_INFO.shmmni)) {
PRINT_ERR("shmid is invalid: %d\n", shmid);
return;
}
@@ -814,7 +844,7 @@ STATIC VOID OsShmDeleteCmd(INT32 shmid)
}
if (seg->ds.shm_nattch <= 0) {
ShmFreeSeg(seg);
ShmFreeSeg(seg, &IPC_SHM_USED_PAGE_COUNT);
}
SYSV_SHM_UNLOCK();
}

View File

@@ -80,20 +80,25 @@ EXIT:
STATIC LmsMemListNode *OsLmsGetPoolNodeFromAddr(UINTPTR addr)
{
LmsMemListNode *current = NULL;
LmsMemListNode *previous = NULL;
LOS_DL_LIST *listHead = &g_lmsCheckPoolList;
if (LOS_ListEmpty(&g_lmsCheckPoolList)) {
goto EXIT;
return NULL;
}
LOS_DL_LIST_FOR_EACH_ENTRY(current, listHead, LmsMemListNode, node) {
if ((addr >= current->poolAddr) && (addr < current->poolAddr + current->poolSize)) {
return current;
if ((addr < current->poolAddr) || (addr >= (current->poolAddr + current->poolSize))) {
continue;
}
if ((previous == NULL) ||
((previous->poolAddr <= current->poolAddr) &&
((current->poolAddr + current->poolSize) <= (previous->poolAddr + previous->poolSize)))) {
previous = current;
}
}
EXIT:
return NULL;
return previous;
}
STATIC LmsMemListNode *OsLmsCheckPoolCreate(VOID)
@@ -123,18 +128,12 @@ UINT32 LOS_LmsCheckPoolAdd(const VOID *pool, UINT32 size)
LMS_LOCK(intSave);
lmsPoolNode = OsLmsGetPoolNodeFromAddr((UINTPTR)pool);
if (lmsPoolNode != NULL) { /* if pool range already on checklist */
if (lmsPoolNode->poolAddr != (UINTPTR)pool) { /* pool is a subset of lmsPoolNode->poolAddr */
/* do not add it again, just return */
PRINT_DEBUG("[LMS]pool %p already on lms checklist !\n", pool);
LMS_UNLOCK(intSave);
return size; /* return size indicate the shadow memory init successful */
} else { /* Re-initialize the same pool, maybe with different size */
/* delete the old node, then add a new one */
lmsPoolNode->used = LMS_POOL_UNUSED;
LOS_ListDelete(&(lmsPoolNode->node));
}
lmsPoolNode = OsLmsGetPoolNode(pool);
if (lmsPoolNode != NULL) { /* if pool already on checklist */
/* Re-initialize the same pool, maybe with different size */
/* delete the old node, then add a new one */
lmsPoolNode->used = LMS_POOL_UNUSED;
LOS_ListDelete(&(lmsPoolNode->node));
}
lmsPoolNode = OsLmsCheckPoolCreate();
@@ -150,7 +149,8 @@ UINT32 LOS_LmsCheckPoolAdd(const VOID *pool, UINT32 size)
lmsPoolNode->shadowStart = (UINTPTR)poolAddr + realSize;
lmsPoolNode->shadowSize = poolAddr + size - lmsPoolNode->shadowStart;
/* init shadow value */
(VOID)memset((VOID *)lmsPoolNode->shadowStart, LMS_SHADOW_AFTERFREE_U8, lmsPoolNode->shadowSize);
(VOID)memset_s((VOID *)lmsPoolNode->shadowStart, lmsPoolNode->shadowSize,
LMS_SHADOW_AFTERFREE_U8, lmsPoolNode->shadowSize);
LOS_ListAdd(&g_lmsCheckPoolList, &(lmsPoolNode->node));
@@ -179,10 +179,11 @@ Release:
STATIC UINT32 OsLmsInit(VOID)
{
(VOID)memset(g_lmsCheckPoolArray, 0, sizeof(g_lmsCheckPoolArray));
(VOID)memset_s(g_lmsCheckPoolArray, sizeof(g_lmsCheckPoolArray), 0, sizeof(g_lmsCheckPoolArray));
LOS_ListInit(&g_lmsCheckPoolList);
static LmsHook hook = {
.init = LOS_LmsCheckPoolAdd,
.deInit = LOS_LmsCheckPoolDel,
.mallocMark = OsLmsLosMallocMark,
.freeMark = OsLmsLosFreeMark,
.simpleMark = OsLmsSimpleMark,
@@ -595,7 +596,7 @@ VOID OsLmsReportError(UINTPTR p, UINT32 size, UINT32 errMod)
(VOID)LOS_AtomicAdd(&g_checkDepth, 1);
LMS_LOCK(intSave);
(VOID)memset(&info, 0, sizeof(LmsAddrInfo));
(VOID)memset_s(&info, sizeof(LmsAddrInfo), 0, sizeof(LmsAddrInfo));
PRINT_ERR("***** Kernel Address Sanitizer Error Detected Start *****\n");
@@ -766,4 +767,4 @@ VOID __asan_handle_no_return(VOID)
return;
}
LOS_MODULE_INIT(OsLmsInit, LOS_INIT_LEVEL_KMOD_PREVM);
LOS_MODULE_INIT(OsLmsInit, LOS_INIT_LEVEL_KMOD_PREVM);

View File

@@ -95,6 +95,7 @@ typedef struct {
typedef struct {
UINT32 (*init)(const VOID *pool, UINT32 size);
VOID (*deInit)(const VOID *pool);
VOID (*mallocMark)(const VOID *curNodeStart, const VOID *nextNodeStart, UINT32 nodeHeadSize);
VOID (*freeMark)(const VOID *curNodeStart, const VOID *nextNodeStart, UINT32 nodeHeadSize);
VOID (*simpleMark)(UINTPTR startAddr, UINTPTR endAddr, UINT32 value);
@@ -132,4 +133,4 @@ extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_handle_no_return(VOID);
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_LMS_PRI_H */
#endif /* _LOS_LMS_PRI_H */

View File

@@ -131,6 +131,10 @@ Lreturn:
pop {r4}
bx lr
Lfunc_end:
#if defined(LOSCFG_KERNEL_LMS)
.size __memset, Lfunc_end - __memset
#else
.size memset, Lfunc_end - memset
#endif
.cantunwind
.fnend @ -- End function

View File

@@ -291,17 +291,135 @@ ssize_t SysWrite(int fd, const void *buf, size_t nbytes)
return ret;
}
// int vfs_normalize_path(const char *directory, const char *filename, char **pathname)
#ifdef LOSCFG_PID_CONTAINER
#ifdef LOSCFG_PROC_PROCESS_DIR
#define PROCESS_DIR_ROOT "/proc"
static char *NextName(char *pos, uint8_t *len)
{
char *name = NULL;
while (*pos != 0 && *pos == '/') {
pos++;
}
if (*pos == '\0') {
return NULL;
}
name = (char *)pos;
while (*pos != '\0' && *pos != '/') {
pos++;
}
*len = pos - name;
return name;
}
static unsigned int ProcRealProcessIDGet(unsigned int pid)
{
unsigned int intSave;
if (OS_PID_CHECK_INVALID(pid)) {
return 0;
}
SCHEDULER_LOCK(intSave);
LosProcessCB *pcb = OsGetPCBFromVpid(pid);
if (pcb == NULL) {
SCHEDULER_UNLOCK(intSave);
return 0;
}
int rootPid = OsGetRootPid(pcb);
SCHEDULER_UNLOCK(intSave);
if ((rootPid == OS_INVALID_VALUE) || (rootPid == pid)) {
return 0;
}
return rootPid;
}
static int ProcRealProcessDirGet(char *path)
{
char pidBuf[PATH_MAX] = {0};
char *fullPath = NULL;
uint8_t strLen = 0;
int pid, rootPid;
int ret = vfs_normalize_path(NULL, path, &fullPath);
if (ret < 0) {
return ret;
}
int procLen = strlen(PROCESS_DIR_ROOT);
if (strncmp(fullPath, PROCESS_DIR_ROOT, procLen) != 0) {
free(fullPath);
return 0;
}
char *pidStr = NextName(fullPath + procLen, &strLen);
if (pidStr == NULL) {
free(fullPath);
return 0;
}
if ((*pidStr <= '0') || (*pidStr > '9')) {
free(fullPath);
return 0;
}
if (memcpy_s(pidBuf, PATH_MAX, pidStr, strLen) != EOK) {
free(fullPath);
return 0;
}
pidBuf[strLen] = '\0';
pid = atoi(pidBuf);
if (pid == 0) {
free(fullPath);
return 0;
}
rootPid = ProcRealProcessIDGet((unsigned)pid);
if (rootPid == 0) {
free(fullPath);
return 0;
}
if (snprintf_s(path, PATH_MAX + 1, PATH_MAX, "/proc/%d%s", rootPid, (pidStr + strLen)) < 0) {
free(fullPath);
return -EFAULT;
}
free(fullPath);
return 0;
}
#endif
#endif
static int GetPath(const char *path, char **pathRet)
{
int ret = UserPathCopy(path, pathRet);
if (ret != 0) {
return ret;
}
#ifdef LOSCFG_PID_CONTAINER
#ifdef LOSCFG_PROC_PROCESS_DIR
ret = ProcRealProcessDirGet(*pathRet);
if (ret != 0) {
return ret;
}
#endif
#endif
return 0;
}
int SysOpen(const char *path, int oflags, ...)
{
int ret;
int procFd;
int procFd = -1;
mode_t mode = DEFAULT_FILE_MODE; /* 0666: File read-write properties. */
char *pathRet = NULL;
if (path != NULL) {
ret = UserPathCopy(path, &pathRet);
ret = GetPath(path, &pathRet);
if (ret != 0) {
return ret;
goto ERROUT;
}
}
@@ -432,6 +550,15 @@ ssize_t SysReadlink(const char *pathname, char *buf, size_t bufsize)
if (ret != 0) {
goto OUT;
}
#ifdef LOSCFG_PID_CONTAINER
#ifdef LOSCFG_PROC_PROCESS_DIR
ret = ProcRealProcessDirGet(pathRet);
if (ret != 0) {
goto OUT;
}
#endif
#endif
}
if (!LOS_IsUserAddressRange((vaddr_t)(UINTPTR)buf, bufsize)) {
@@ -1840,6 +1967,15 @@ ssize_t SysReadlinkat(int dirfd, const char *pathname, char *buf, size_t bufsize
if (ret != 0) {
goto OUT;
}
#ifdef LOSCFG_PID_CONTAINER
#ifdef LOSCFG_PROC_PROCESS_DIR
ret = ProcRealProcessDirGet(pathRet);
if (ret != 0) {
goto OUT;
}
#endif
#endif
}
if (dirfd != AT_FDCWD) {

View File

@@ -85,6 +85,8 @@ extern int SysWaitid(idtype_t type, int pid, USER siginfo_t *info, int options,
extern int SysFork(void);
extern int SysVfork(void);
extern int SysClone(int flags, void *stack, int *parentTid, unsigned long tls, int *childTid);
extern int SysUnshare(int flags);
extern int SysSetns(int fd, int type);
extern unsigned int SysGetPID(void);
extern unsigned int SysGetPPID(void);
extern int SysSetGroupID(unsigned int gid);

View File

@@ -369,6 +369,24 @@ int SysClone(int flags, void *stack, int *parentTid, unsigned long tls, int *chi
return OsClone((UINT32)flags, (UINTPTR)stack, 0);
}
int SysUnshare(int flags)
{
#ifdef LOSCFG_KERNEL_CONTAINER
return OsUnshare(flags);
#else
return -ENOSYS;
#endif
}
int SysSetns(int fd, int type)
{
#ifdef LOSCFG_KERNEL_CONTAINER
return OsSetNs(fd, type);
#else
return -ENOSYS;
#endif
}
unsigned int SysGetPPID(void)
{
#ifdef LOSCFG_PID_CONTAINER

View File

@@ -140,6 +140,8 @@ SYSCALL_HAND_DEF(__NR_exit, SysThreadExit, void, ARG_NUM_1)
SYSCALL_HAND_DEF(__NR_fork, SysFork, int, ARG_NUM_0)
SYSCALL_HAND_DEF(__NR_vfork, SysVfork, int, ARG_NUM_0)
SYSCALL_HAND_DEF(__NR_clone, SysClone, int, ARG_NUM_5)
SYSCALL_HAND_DEF(__NR_unshare, SysUnshare, int, ARG_NUM_1)
SYSCALL_HAND_DEF(__NR_setns, SysSetns, int, ARG_NUM_2)
SYSCALL_HAND_DEF(__NR_getpid, SysGetPID, unsigned int, ARG_NUM_0)
SYSCALL_HAND_DEF(__NR_pause, SysPause, int, ARG_NUM_0)

View File

@@ -167,5 +167,10 @@ group("unittest") {
deps += [ "container:liteos_a_container_unittest" ]
}
}
# fuzz test
if (LOSCFG_USER_FUZZ_TEST == true) {
deps += [ "fuzz:liteos_a_fuzztest" ]
}
}
}

View File

@@ -55,7 +55,6 @@ static int Testcase(VOID)
ICUNIT_GOTO_EQUAL(ds.shm_perm.uid, getuid(), ds.shm_perm.uid, ERROR_OUT);
ret = shmctl(shmid, SHM_STAT, &ds);
// ICUNIT_GOTO_EQUAL(ret, 0x10000, ret, ERROR_OUT);
ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, ERROR_OUT);
ICUNIT_GOTO_NOT_EQUAL(ret, 0, ret, ERROR_OUT);
@@ -70,7 +69,7 @@ static int Testcase(VOID)
ICUNIT_GOTO_EQUAL(info.shmmax, 0x1000000, info.shmmax, ERROR_OUT);
ICUNIT_GOTO_EQUAL(info.shmmin, 1, info.shmmin, ERROR_OUT);
ICUNIT_GOTO_EQUAL(info.shmmni, 192, info.shmmni, ERROR_OUT);
ICUNIT_GOTO_EQUAL(info.shmseg, 128, info.shmseg, ERROR_OUT);
ICUNIT_GOTO_EQUAL(info.shmseg, 128, info.shmseg, ERROR_OUT); // 128: expected value of shmseg
ICUNIT_GOTO_EQUAL(info.shmall, 0x1000, info.shmall, ERROR_OUT);
ret = shmdt(shm);

View File

@@ -29,6 +29,10 @@
import("$root_out_dir/config.gni")
TEST_UNITTEST_DIR = rebase_path(".")
declare_args() {
liteos_container_test_enable = false
liteos_fuzz_test_enable = false
}
TEST_LEVEL_LOW = 1
TEST_LEVEL_MIDDLE = 3
@@ -127,7 +131,7 @@ LOSCFG_USER_TEST_SECURITY_REUGID = true
LOSCFG_USER_TEST_SECURITY_VID = true
LOSCFG_USER_TEST_PROCESS_FS = false
if (defined(LOSCFG_PROC_PROCESS_DIR)) {
if (defined(LOSCFG_PROC_PROCESS_DIR) || liteos_container_test_enable == true) {
LOSCFG_USER_TEST_PROCESS_FS = true
}
@@ -136,15 +140,29 @@ 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_IPC_CONTAINER = false
LOSCFG_USER_TEST_TIME_CONTAINER = false
if (defined(LOSCFG_KERNEL_CONTAINER) || liteos_container_test_enable == true) {
LOSCFG_USER_TEST_CONTAINER = true
if (defined(LOSCFG_PID_CONTAINER)) {
if (defined(LOSCFG_PID_CONTAINER) || liteos_container_test_enable == true) {
LOSCFG_USER_TEST_PID_CONTAINER = true
}
if (defined(LOSCFG_UTS_CONTAINER)) {
if (defined(LOSCFG_UTS_CONTAINER) || liteos_container_test_enable == true) {
LOSCFG_USER_TEST_UTS_CONTAINER = true
}
if (defined(LOSCFG_MNT_CONTAINER)) {
if (defined(LOSCFG_MNT_CONTAINER) || liteos_container_test_enable == true) {
LOSCFG_USER_TEST_MNT_CONTAINER = true
}
if (defined(LOSCFG_IPC_CONTAINER) || liteos_container_test_enable == true) {
LOSCFG_USER_TEST_IPC_CONTAINER = true
}
if (defined(LOSCFG_TIME_CONTAINER) || liteos_container_test_enable == true) {
LOSCFG_USER_TEST_TIME_CONTAINER = true
}
}
########## fuzz test ##########
LOSCFG_USER_FUZZ_TEST = false
if (liteos_fuzz_test_enable == true) {
LOSCFG_USER_FUZZ_TEST = true
}

View File

@@ -41,6 +41,12 @@ config("container_config") {
if (defined(LOSCFG_USER_TEST_MNT_CONTAINER)) {
cflags += [ "-DLOSCFG_USER_TEST_MNT_CONTAINER" ]
}
if (defined(LOSCFG_USER_TEST_IPC_CONTAINER)) {
cflags += [ "-DLOSCFG_USER_TEST_IPC_CONTAINER" ]
}
if (defined(LOSCFG_USER_TEST_TIME_CONTAINER)) {
cflags += [ "-DLOSCFG_USER_TEST_TIME_CONTAINER" ]
}
cflags_cc = cflags
}

View File

@@ -65,6 +65,29 @@ pid_t CloneWrapper(int (*func)(void *), int flag, void *args)
return pid;
}
int WaitChild(pid_t pid, int *status, int errNo1, int errNo2)
{
int ret = waitpid(pid, status, 0);
if (ret != pid) {
printf("[ERR] WaitChild pid=%d return pid=%d\n", pid, ret);
return errNo1;
}
if (status == nullptr) {
return 0;
}
ret = WIFEXITED(*status);
if (ret == 0) {
printf("[ERR] WaitChild pid=%d WIFEXITED(status)=%d\n", pid, WIFEXITED(*status));
return errNo2;
}
ret = WEXITSTATUS(*status);
if (ret != 0) {
printf("[ERR] WaitChild pid=%d WEXITSTATUS(status)=%d\n", pid, WEXITSTATUS(*status));
return errNo2;
}
return 0;
}
std::string GenContainerLinkPath(int pid, const std::string& containerType)
{
std::ostringstream buf;
@@ -74,24 +97,12 @@ std::string GenContainerLinkPath(int pid, const std::string& containerType)
std::string ReadlinkContainer(int pid, const std::string& containerType)
{
char buf[PATH_MAX];
char buf[PATH_MAX] = {0};
auto path = GenContainerLinkPath(pid, containerType);
struct stat sb;
int ret = lstat(path.data(), &sb);
if (ret == -1) {
throw std::exception();
}
size_t bufsiz = sb.st_size + 1;
if (sb.st_size == 0) {
bufsiz = PATH_MAX;
}
(void)memset_s(buf, PATH_MAX, 0, PATH_MAX);
ssize_t nbytes = readlink(path.c_str(), buf, bufsiz);
ssize_t nbytes = readlink(path.c_str(), buf, PATH_MAX);
if (nbytes == -1) {
throw std::exception();
printf("pid %d, ReadlinkContainer readlink %s failed, errno=%d\n", getpid(), path.c_str(), errno);
return path.c_str();
}
return buf;
}
@@ -126,6 +137,90 @@ HWTEST_F(ContainerTest, ItPidContainer023, TestSize.Level0)
{
ItPidContainer023();
}
/**
* @tc.name: Container_Pid_Test_025
* @tc.desc: pid container function test case
* @tc.type: FUNC
* @tc.require: issueI68LVW
* @tc.author:
*/
HWTEST_F(ContainerTest, ItPidContainer025, TestSize.Level0)
{
ItPidContainer025();
}
/**
* @tc.name: Container_Pid_Test_026
* @tc.desc: pid container function test case
* @tc.type: FUNC
* @tc.require: issueI68LVW
* @tc.author:
*/
HWTEST_F(ContainerTest, ItPidContainer026, TestSize.Level0)
{
ItPidContainer026();
}
/**
* @tc.name: Container_Pid_Test_027
* @tc.desc: pid container function test case
* @tc.type: FUNC
* @tc.require: issueI6BE5A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItPidContainer027, TestSize.Level0)
{
ItPidContainer027();
}
/**
* @tc.name: Container_Pid_Test_028
* @tc.desc: pid container function test case
* @tc.type: FUNC
* @tc.require: issueI6BE5A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItPidContainer028, TestSize.Level0)
{
ItPidContainer028();
}
/**
* @tc.name: Container_Pid_Test_029
* @tc.desc: pid container function test case
* @tc.type: FUNC
* @tc.require: issueI6BE5A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItPidContainer029, TestSize.Level0)
{
ItPidContainer029();
}
/**
* @tc.name: Container_Pid_Test_030
* @tc.desc: pid container function test case
* @tc.type: FUNC
* @tc.require: issueI6BE5A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItPidContainer030, TestSize.Level0)
{
ItPidContainer030();
}
/**
* @tc.name: Container_Pid_Test_031
* @tc.desc: pid container function test case
* @tc.type: FUNC
* @tc.require: issueI6D9Y0
* @tc.author:
*/
HWTEST_F(ContainerTest, ItPidContainer031, TestSize.Level0)
{
ItPidContainer031();
}
#endif
#if defined(LOSCFG_USER_TEST_UTS_CONTAINER)
/**
@@ -151,6 +246,42 @@ HWTEST_F(ContainerTest, ItUtsContainer002, TestSize.Level0)
{
ItUtsContainer002();
}
/**
* @tc.name: Container_UTS_Test_004
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6BE5A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUtsContainer004, TestSize.Level0)
{
ItUtsContainer004();
}
/**
* @tc.name: Container_UTS_Test_005
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6D9Y0
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUtsContainer005, TestSize.Level0)
{
ItUtsContainer005();
}
/**
* @tc.name: Container_UTS_Test_006
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6D9Y0
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUtsContainer006, TestSize.Level0)
{
ItUtsContainer006();
}
#endif
#if defined(LOSCFG_USER_TEST_MNT_CONTAINER)
@@ -202,6 +333,54 @@ HWTEST_F(ContainerTest, ItMntContainer004, TestSize.Level0)
ItMntContainer004();
}
/**
* @tc.name: Container_MNT_Test_005
* @tc.desc: mnt container function test case
* @tc.type: FUNC
* @tc.require: issueI6BE5A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItMntContainer005, TestSize.Level0)
{
ItMntContainer005();
}
/**
* @tc.name: Container_MNT_Test_006
* @tc.desc: mnt container function test case
* @tc.type: FUNC
* @tc.require: issueI6BE5A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItMntContainer006, TestSize.Level0)
{
ItMntContainer006();
}
/**
* @tc.name: Container_MNT_Test_007
* @tc.desc: mnt container function test case
* @tc.type: FUNC
* @tc.require: issueI6BE5A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItMntContainer007, TestSize.Level0)
{
ItMntContainer007();
}
/**
* @tc.name: Container_MNT_Test_008
* @tc.desc: mnt container function test case
* @tc.type: FUNC
* @tc.require: issueI6D9Y0
* @tc.author:
*/
HWTEST_F(ContainerTest, ItMntContainer008, TestSize.Level0)
{
ItMntContainer008();
}
/**
* @tc.name: chroot_Test_001
* @tc.desc: chroot function test case
@@ -209,13 +388,207 @@ HWTEST_F(ContainerTest, ItMntContainer004, TestSize.Level0)
* @tc.require: issueI6APW2
* @tc.author:
*/
HWTEST_F(ContainerTest, ItContainerChroot001, TestSize.Level0)
{
ItContainerChroot001();
}
#endif /* LOSCFG_MNT_CONTAINER */
/**
* @tc.name: chroot_Test_002
* @tc.desc: chroot function test case
* @tc.type: FUNC
* @tc.require: issueI6APW2
* @tc.author:
*/
HWTEST_F(ContainerTest, ItContainerChroot002, TestSize.Level0)
{
ItContainerChroot002();
}
#endif /* LOSCFG_USER_TEST_MNT_CONTAINER */
#if defined(LOSCFG_USER_TEST_IPC_CONTAINER)
/**
* @tc.name: Container_IPC_Test_001
* @tc.desc: ipc container function test case
* @tc.type: FUNC
* @tc.require: issueI6AVMY
* @tc.author:
*/
HWTEST_F(ContainerTest, ItIpcContainer001, TestSize.Level0)
{
ItIpcContainer001();
}
/**
* @tc.name: Container_IPC_Test_002
* @tc.desc: ipc container function test case
* @tc.type: FUNC
* @tc.require: issueI6D9Y0
* @tc.author:
*/
HWTEST_F(ContainerTest, ItIpcContainer002, TestSize.Level0)
{
ItIpcContainer002();
}
/**
* @tc.name: Container_IPC_Test_003
* @tc.desc: ipc container function test case
* @tc.type: FUNC
* @tc.require: issueI6BE5A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItIpcContainer003, TestSize.Level0)
{
ItIpcContainer003();
}
/**
* @tc.name: Container_IPC_Test_004
* @tc.desc: ipc container function test case
* @tc.type: FUNC
* @tc.require: issueI6AVMY
* @tc.author:
*/
HWTEST_F(ContainerTest, ItIpcContainer004, TestSize.Level0)
{
ItIpcContainer004();
}
/**
* @tc.name: Container_IPC_Test_005
* @tc.desc: ipc container function test case
* @tc.type: FUNC
* @tc.require: issueI6BE5A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItIpcContainer005, TestSize.Level0)
{
ItIpcContainer005();
}
/**
* @tc.name: Container_IPC_Test_006
* @tc.desc: ipc container function test case
* @tc.type: FUNC
* @tc.require: issueI6D9Y0
* @tc.author:
*/
HWTEST_F(ContainerTest, ItIpcContainer006, TestSize.Level0)
{
ItIpcContainer006();
}
#endif
#if defined(LOSCFG_USER_TEST_TIME_CONTAINER)
/**
* @tc.name: Container_TIME_Test_001
* @tc.desc: time container function test case
* @tc.type: FUNC
* @tc.require: issueI6B0A3
* @tc.author:
*/
HWTEST_F(ContainerTest, ItTimeContainer001, TestSize.Level0)
{
ItTimeContainer001();
}
/**
* @tc.name: Container_TIME_Test_002
* @tc.desc: time container function test case
* @tc.type: FUNC
* @tc.require: issueI6BE5A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItTimeContainer002, TestSize.Level0)
{
ItTimeContainer002();
}
/**
* @tc.name: Container_TIME_Test_003
* @tc.desc: time container function test case
* @tc.type: FUNC
* @tc.require: issueI6D9Y0
* @tc.author:
*/
HWTEST_F(ContainerTest, ItTimeContainer003, TestSize.Level0)
{
ItTimeContainer003();
}
/**
* @tc.name: Container_TIME_Test_004
* @tc.desc: time container function test case
* @tc.type: FUNC
* @tc.require: issueI6BE5A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItTimeContainer004, TestSize.Level0)
{
ItTimeContainer004();
}
/**
* @tc.name: Container_TIME_Test_005
* @tc.desc: time container function test case
* @tc.type: FUNC
* @tc.require: issueI6BE5A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItTimeContainer005, TestSize.Level0)
{
ItTimeContainer005();
}
/*
* @tc.name: Container_TIME_Test_007
* @tc.desc: time container function test case
* @tc.type: FUNC
* @tc.require: issueI6B0A3
* @tc.author:
*/
HWTEST_F(ContainerTest, ItTimeContainer007, TestSize.Level0)
{
ItTimeContainer007();
}
/**
* @tc.name: Container_TIME_Test_008
* @tc.desc: time container function test case
* @tc.type: FUNC
* @tc.require: issueI6BE5A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItTimeContainer008, TestSize.Level0)
{
ItTimeContainer008();
}
/**
* @tc.name: Container_TIME_Test_009
* @tc.desc: time container function test case
* @tc.type: FUNC
* @tc.require: issueI6B0A3
* @tc.author:
*/
HWTEST_F(ContainerTest, ItTimeContainer009, TestSize.Level0)
{
ItTimeContainer009();
}
/**
* @tc.name: Container_TIME_Test_010
* @tc.desc: time container function test case
* @tc.type: FUNC
* @tc.require: issueI6B0A3
* @tc.author:
*/
HWTEST_F(ContainerTest, ItTimeContainer010, TestSize.Level0)
{
ItTimeContainer010();
}
#endif
#endif /* LOSCFG_USER_TEST_SMOKE */
#if defined(LOSCFG_USER_TEST_FULL)
@@ -268,6 +641,20 @@ HWTEST_F(ContainerTest, ItPidContainer004, TestSize.Level0)
ItPidContainer004();
}
#if defined(LOSCFG_USER_TEST_UTS_CONTAINER)
/**
* @tc.name: Container_Pid_Test_005
* @tc.desc: pid container function test case
* @tc.type: FUNC
* @tc.require: issueI68LVW
* @tc.author:
*/
HWTEST_F(ContainerTest, ItPidContainer005, TestSize.Level0)
{
ItPidContainer005();
}
#endif
/**
* @tc.name: Container_Pid_Test_006
* @tc.desc: pid container function test case

View File

@@ -35,9 +35,15 @@
#include <iostream>
#include <sstream>
#include <regex>
#include <csignal>
#include <sys/syscall.h>
#include <sys/capability.h>
#include <cstring>
#include "osTest.h"
#include "mqueue.h"
#include "sys/time.h"
#include "sys/shm.h"
#include "sys/types.h"
const int EXIT_CODE_ERRNO_1 = 1;
const int EXIT_CODE_ERRNO_2 = 2;
@@ -55,11 +61,19 @@ const int EXIT_CODE_ERRNO_13 = 13;
const int EXIT_CODE_ERRNO_14 = 14;
const int EXIT_CODE_ERRNO_15 = 15;
const int EXIT_CODE_ERRNO_16 = 16;
const int EXIT_CODE_ERRNO_17 = 17;
const int EXIT_CODE_ERRNO_255 = 255;
const int CONTAINER_FIRST_PID = 1;
const int CONTAINER_SECOND_PID = 2;
const int CONTAINER_THIRD_PID = 3;
const int MQUEUE_TEST_SIZE = 50;
const int MQUEUE_TEST_MAX_MSG = 255;
const int SHM_TEST_DATA_SIZE = 1024;
const int SHM_TEST_KEY1 = 1234;
const int SHM_TEST_OPEN_PERM = 0666;
const int CLONE_STACK_MMAP_FLAG = MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK;
extern const char *USERDATA_DIR_NAME;
extern const char *ACCESS_FILE_NAME;
@@ -71,23 +85,78 @@ extern const int BIT_ON_RETURN_VALUE;
extern const int STACK_SIZE;
extern const int CHILD_FUNC_ARG;
const int MQUEUE_STANDARD_NAME_LENGTH = 255;
extern "C" {
#define CLONE_NEWTIME 0x00000080
}
int ChildFunction(void *args);
pid_t CloneWrapper(int (*func)(void *), int flag, void *args);
int WaitChild(pid_t pid, int *status, int errNo1, int errNo2);
std::string GenContainerLinkPath(int pid, const std::string& containerType);
extern std::string ReadlinkContainer(int pid, const std::string& containerType);
class MQueueFinalizer {
public:
explicit MQueueFinalizer(mqd_t mqueueParent, const std::string& mqname)
{
m_mqueueParent = mqueueParent;
m_mqname = mqname;
}
~MQueueFinalizer()
{
if (m_mqueueParent >= 0) {
mq_close(m_mqueueParent);
mq_unlink(m_mqname.c_str());
}
}
private:
mqd_t m_mqueueParent;
std::string m_mqname;
};
class ShmFinalizer {
public:
explicit ShmFinalizer(void* shm, int shmid)
{
m_shm = shm;
m_shmid = shmid;
}
~ShmFinalizer()
{
shmdt(m_shm);
shmctl(m_shmid, IPC_RMID, nullptr);
}
private:
void* m_shm;
int m_shmid;
};
#if defined(LOSCFG_USER_TEST_SMOKE)
void ItContainer001(void);
void ItContainerChroot001(void);
void ItContainerChroot002(void);
#if defined(LOSCFG_USER_TEST_PID_CONTAINER)
void ItPidContainer023(void);
void ItPidContainer025(void);
void ItPidContainer026(void);
void ItPidContainer027(void);
void ItPidContainer028(void);
void ItPidContainer029(void);
void ItPidContainer030(void);
void ItPidContainer031(void);
#endif
#if defined(LOSCFG_USER_TEST_UTS_CONTAINER)
void ItUtsContainer001(void);
void ItUtsContainer002(void);
void ItUtsContainer004(void);
void ItUtsContainer005(void);
void ItUtsContainer006(void);
#endif
#if defined(LOSCFG_USER_TEST_MNT_CONTAINER)
void ItMntContainer001(void);
@@ -99,6 +168,26 @@ void ItMntContainer006(void);
void ItMntContainer007(void);
void ItMntContainer008(void);
#endif
#if defined(LOSCFG_USER_TEST_IPC_CONTAINER)
void ItIpcContainer001(void);
void ItIpcContainer002(void);
void ItIpcContainer003(void);
void ItIpcContainer004(void);
void ItIpcContainer005(void);
void ItIpcContainer006(void);
#endif
#if defined(LOSCFG_USER_TEST_TIME_CONTAINER)
void ItTimeContainer001(void);
void ItTimeContainer002(void);
void ItTimeContainer003(void);
void ItTimeContainer004(void);
void ItTimeContainer005(void);
void ItTimeContainer006(void);
void ItTimeContainer007(void);
void ItTimeContainer008(void);
void ItTimeContainer009(void);
void ItTimeContainer010(void);
#endif
#endif
#if defined(LOSCFG_USER_TEST_FULL)
@@ -107,6 +196,7 @@ void ItPidContainer001(void);
void ItPidContainer002(void);
void ItPidContainer003(void);
void ItPidContainer004(void);
void ItPidContainer005(void);
void ItPidContainer006(void);
void ItPidContainer007(void);
void ItPidContainer008(void);

View File

@@ -36,21 +36,27 @@ common_include_dirs = [
sources_entry = [ "$TEST_UNITTEST_DIR/container/It_container_test.cpp" ]
sources_smoke = [
"$TEST_UNITTEST_DIR/container/smoke/It_container_001.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_container_chroot_001.cpp",
]
sources_smoke = [ "$TEST_UNITTEST_DIR/container/smoke/It_container_001.cpp" ]
sources_full = []
if (defined(LOSCFG_USER_TEST_PID_CONTAINER)) {
sources_smoke +=
[ "$TEST_UNITTEST_DIR/container/smoke/It_pid_container_023.cpp" ]
sources_smoke += [
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_023.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_025.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_026.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_027.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_028.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_029.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_030.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_031.cpp",
]
sources_full += [
"$TEST_UNITTEST_DIR/container/full/It_pid_container_001.cpp",
"$TEST_UNITTEST_DIR/container/full/It_pid_container_002.cpp",
"$TEST_UNITTEST_DIR/container/full/It_pid_container_003.cpp",
"$TEST_UNITTEST_DIR/container/full/It_pid_container_004.cpp",
"$TEST_UNITTEST_DIR/container/full/It_pid_container_005.cpp",
"$TEST_UNITTEST_DIR/container/full/It_pid_container_006.cpp",
"$TEST_UNITTEST_DIR/container/full/It_pid_container_007.cpp",
"$TEST_UNITTEST_DIR/container/full/It_pid_container_008.cpp",
@@ -75,12 +81,17 @@ if (defined(LOSCFG_USER_TEST_UTS_CONTAINER)) {
sources_smoke += [
"$TEST_UNITTEST_DIR/container/smoke/It_uts_container_001.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_uts_container_002.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_uts_container_004.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_uts_container_005.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_uts_container_006.cpp",
]
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_container_chroot_001.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_container_chroot_002.cpp",
"$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",
@@ -91,3 +102,29 @@ if (defined(LOSCFG_USER_TEST_MNT_CONTAINER)) {
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_008.cpp",
]
}
if (defined(LOSCFG_USER_TEST_IPC_CONTAINER)) {
sources_smoke += [
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_001.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_002.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_003.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_004.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_005.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_006.cpp",
]
}
if (defined(LOSCFG_USER_TEST_TIME_CONTAINER)) {
sources_smoke += [
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_001.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_002.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_003.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_004.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_005.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_006.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_007.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_008.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_009.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_010.cpp",
]
}

View File

@@ -54,20 +54,23 @@ static int ChildFunClone3(void *p)
}
childPid = clone(ChildFun, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
free(pstk);
if (childPid == -1) {
free(pstk);
return EXIT_CODE_ERRNO_4;
}
ret = waitpid(childPid, &status, 0);
ret = WIFEXITED(status);
ret = WEXITSTATUS(status);
if (ret != CONTAINER_THIRD_PID) {
free(pstk);
if (ret != childPid) {
return EXIT_CODE_ERRNO_5;
}
free(pstk);
ret = WIFEXITED(status);
if (ret == 0) {
return EXIT_CODE_ERRNO_6;
}
ret = WEXITSTATUS(status);
if (ret != CONTAINER_THIRD_PID) {
return EXIT_CODE_ERRNO_7;
}
return childFunRet;
}
@@ -107,25 +110,26 @@ static int ChildFunClone1(void *p)
int ret;
pid_t pid = getpid();
int childFunRet = (int)pid;
void *pstk = malloc(STACK_SIZE);
if (pstk == NULL) {
return EXIT_CODE_ERRNO_2;
}
int childPid = clone(ChildFunClone2, (char *)pstk + STACK_SIZE, CLONE_NEWPID | SIGCHLD, NULL);
char *containerType = "pid";
int childPid = clone(ChildFunClone2, NULL, CLONE_NEWPID | SIGCHLD, NULL);
if (childPid == -1) {
free(pstk);
return EXIT_CODE_ERRNO_3;
}
auto linkBuffer = ReadlinkContainer(childPid, containerType);
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType);
ret = linkBuffer.compare(linkBuffer1);
if (ret == 0) {
(void)waitpid(childPid, &status, 0);
return EXIT_CODE_ERRNO_5;
}
ret = waitpid(childPid, &status, 0);
ret = WIFEXITED(status);
ret = WEXITSTATUS(status);
if (ret != CONTAINER_FIRST_PID) {
free(pstk);
return EXIT_CODE_ERRNO_4;
}
free(pstk);
return childFunRet;
}

View File

@@ -41,7 +41,7 @@ static int ChildFunClone2()
if (pstk == NULL) {
return -1;
}
int childPid = clone(ChildFun, (char *)pstk + STACK_SIZE, CLONE_NEWUTS | SIGCHLD, NULL);
int childPid = clone(ChildFun, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
free(pstk);
return childPid;

View File

@@ -70,11 +70,17 @@ static int ChildFunClone1(void *p)
}
ret = waitpid(childPid, &status, 0);
if (ret != childPid) {
return EXIT_CODE_ERRNO_4;
}
ret = WIFEXITED(status);
if (ret == 0) {
return EXIT_CODE_ERRNO_5;
}
ret = WEXITSTATUS(status);
if (ret != 0) {
free(pstk);
return EXIT_CODE_ERRNO_4;
return EXIT_CODE_ERRNO_6;
}
free(pstk);

View File

@@ -0,0 +1,140 @@
/*
* 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"
static int ChildFunClone3(void *p)
{
(void)p;
auto pid = getpid();
if (pid != CONTAINER_SECOND_PID) {
return EXIT_CODE_ERRNO_1;
}
return 0;
}
static int ChildFunClone2(void *p)
{
(void)p;
auto pid = getpid();
if (pid != CONTAINER_FIRST_PID) {
return EXIT_CODE_ERRNO_1;
}
int ret;
int status;
void *pstk = malloc(STACK_SIZE);
if (pstk == NULL) {
return EXIT_CODE_ERRNO_2;
}
int childPid = clone(ChildFunClone3, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
if (childPid == -1) {
free(pstk);
return EXIT_CODE_ERRNO_3;
}
ret = waitpid(childPid, &status, 0);
ret = WIFEXITED(status);
ret = WEXITSTATUS(status);
if (ret != 0) {
free(pstk);
return EXIT_CODE_ERRNO_4;
}
free(pstk);
return 0;
}
static int ChildFunClone1(void *p)
{
(void)p;
int ret;
int status;
const char *containerType = "pid";
const char *containerType1 = "pid_for_children";
auto pid = getpid();
ret = unshare(CLONE_NEWPID);
if (ret == -1) {
return EXIT_CODE_ERRNO_1;
}
auto pid1 = getpid();
if (pid != pid1) {
return EXIT_CODE_ERRNO_2;
}
auto linkBuffer = ReadlinkContainer(pid, containerType);
auto linkBuffer1 = ReadlinkContainer(pid, containerType1);
ret = linkBuffer.compare(linkBuffer1);
if (ret == 0) {
return EXIT_CODE_ERRNO_3;
}
void *pstk = malloc(STACK_SIZE);
if (pstk == NULL) {
return EXIT_CODE_ERRNO_4;
}
int childPid = clone(ChildFunClone2, (char *)pstk + STACK_SIZE, CLONE_NEWUTS | SIGCHLD, NULL);
free(pstk);
if (childPid == -1) {
return EXIT_CODE_ERRNO_5;
}
ret = waitpid(childPid, &status, 0);
if (ret != childPid) {
return EXIT_CODE_ERRNO_6;
}
ret = WIFEXITED(status);
if (ret == 0) {
return EXIT_CODE_ERRNO_7;
}
ret = WEXITSTATUS(status);
if (ret != 0) {
return EXIT_CODE_ERRNO_8;
}
return 0;
}
void ItPidContainer005(void)
{
int status;
int ret;
void *pstk = malloc(STACK_SIZE);
ASSERT_TRUE(pstk != NULL);
int childPid = clone(ChildFunClone1, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
free(pstk);
ASSERT_NE(childPid, -1);
ret = waitpid(childPid, &status, 0);
ASSERT_EQ(ret, childPid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
}

View File

@@ -35,6 +35,7 @@ static int ChildFun(void *p)
int ret;
int currGid = getpgrp();
if (currGid != CONTAINER_FIRST_PID) {
printf("ChildFun pid %d currGid %d\n", getpid(), currGid);
return EXIT_CODE_ERRNO_1;
}

View File

@@ -0,0 +1,118 @@
/*
* 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 OpendirCheck(void)
{
DIR *dir = opendir("/proc");
if (dir == nullptr) {
return EXIT_CODE_ERRNO_1;
}
closedir(dir);
return 0;
}
static int ChildFunc(void *arg)
{
int ret = 0;
ret = OpendirCheck();
if (ret == 0) {
return EXIT_CODE_ERRNO_1;
}
return 0;
}
static int TestFunc(void *arg)
{
int ret = 0;
int fd;
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
if (stack == nullptr) {
return EXIT_CODE_ERRNO_1;
}
char *stackTop = stack + STACK_SIZE;
ret = OpendirCheck();
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
ret = chroot("/system/etc");
if (ret != 0) {
return EXIT_CODE_ERRNO_3;
}
ret = OpendirCheck();
if (ret == 0) {
return EXIT_CODE_ERRNO_4;
}
fd = open("/PCID.sc", O_RDONLY);
if (fd == -1) {
return EXIT_CODE_ERRNO_5;
}
close(fd);
sleep(1);
auto pid = clone(ChildFunc, stackTop, SIGCHLD, arg);
if (pid == -1) {
return EXIT_CODE_ERRNO_6;
}
int status;
ret = waitpid(pid, &status, 0);
ret = WIFEXITED(status);
int exitCode = WEXITSTATUS(status);
if (exitCode != 0) {
return EXIT_CODE_ERRNO_7;
}
return 0;
}
void ItContainerChroot002(void)
{
int ret = 0;
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
ASSERT_TRUE(stack != nullptr);
char *stackTop = stack + STACK_SIZE;
int arg = CHILD_FUNC_ARG;
auto pid = clone(TestFunc, stackTop, SIGCHLD, &arg);
ASSERT_NE(pid, -1);
int status;
ret = waitpid(pid, &status, 0);
ret = WIFEXITED(status);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
}

View File

@@ -0,0 +1,129 @@
/*
* 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 <fstream>
#include <iostream>
#include "It_container_test.h"
using namespace std;
static int childFunc(void *arg)
{
int ret;
(void)arg;
mqd_t mqueueChild;
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
char mqname[MQUEUE_STANDARD_NAME_LENGTH] = "/testMQueue001";
const char msgptr[] = "childMsgs";
struct mq_attr attr = { 0 };
attr.mq_msgsize = MQUEUE_TEST_SIZE;
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
mqueueChild = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, &attr);
if (mqueueChild == (mqd_t)-1) {
goto EXIT1;
}
ret = mq_send(mqueueChild, msgptr, strlen(msgptr), 0);
if (ret != 0) {
goto EXIT1;
}
ret = mq_receive(mqueueChild, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
if (ret == -1) {
goto EXIT1;
}
if (strncmp(msgrcd, msgptr, strlen(msgptr)) != 0) {
goto EXIT1;
}
EXIT1:
if (mqueueChild >= 0) {
ret = mq_close(mqueueChild);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
ret = mq_unlink(mqname);
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
}
return EXIT_CODE_ERRNO_7;
}
void ItIpcContainer001(void)
{
uint32_t ret;
int status;
int exitCode;
pid_t pid;
mqd_t mqueueParent;
int arg = CHILD_FUNC_ARG;
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
char mqname[MQUEUE_STANDARD_NAME_LENGTH] = "/testMQueue001";
const char msgptr[] = "parentMsg";
struct mq_attr attr = { 0 };
attr.mq_msgsize = MQUEUE_TEST_SIZE;
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
mqueueParent = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, NULL);
MQueueFinalizer mQueueFinalizer(mqueueParent, mqname);
ASSERT_NE(mqueueParent, (mqd_t)-1);
(void)memset_s(&attr, sizeof(attr), 0, sizeof(attr));
ret = mq_getattr(mqueueParent, &attr);
ASSERT_EQ(ret, 0);
attr.mq_flags |= O_NONBLOCK;
ret = mq_setattr(mqueueParent, &attr, NULL);
ASSERT_EQ(ret, 0);
ret = mq_getattr(mqueueParent, &attr);
ASSERT_EQ(ret, 0);
ret = mq_send(mqueueParent, msgptr, strlen(msgptr), 0);
ASSERT_EQ(ret, 0);
pid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
ASSERT_NE(pid, -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, EXIT_CODE_ERRNO_7);
ret = mq_receive(mqueueParent, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
ASSERT_NE(ret, -1);
ASSERT_STREQ(msgrcd, msgptr);
}

View File

@@ -0,0 +1,151 @@
/*
* 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 <fstream>
#include <iostream>
#include "It_container_test.h"
using namespace std;
static int childFunc(void *arg)
{
int ret;
(void)arg;
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
char mqname[] = "/testMQueue003";
const char msgptr[] = "childMsgs";
struct mq_attr attr = { 0 };
mqd_t mqueue;
attr.mq_msgsize = MQUEUE_TEST_SIZE;
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
mqueue = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, &attr);
if (mqueue == (mqd_t)-1) {
goto EXIT1;
}
ret = mq_send(mqueue, msgptr, strlen(msgptr), 0);
if (ret != 0) {
goto EXIT1;
}
ret = mq_receive(mqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
if (ret != strlen(msgptr)) {
goto EXIT1;
}
if (strncmp(msgrcd, msgptr, strlen(msgptr)) != 0) {
goto EXIT1;
}
EXIT1:
if (mqueue >= 0) {
ret = mq_close(mqueue);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
ret = mq_unlink(mqname);
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
}
return EXIT_CODE_ERRNO_7;
}
void ItIpcContainer002(void)
{
uint32_t ret;
int status;
int exitCode;
mqd_t mqueue;
pid_t childPid;
int arg = CHILD_FUNC_ARG;
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
struct mq_attr attr = { 0 };
attr.mq_msgsize = MQUEUE_TEST_SIZE;
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
char mqname[] = "/testMQueue004";
const char msgptr[] = "parentMsg";
std::string containerType = "ipc";
std::string filePath;
int fd;
int parentPid;
std::string parentlink;
std::string childlink;
mqueue = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, NULL);
MQueueFinalizer mQueueFinalizer(mqueue, mqname);
ASSERT_NE(mqueue, (mqd_t)-1);
(void)memset_s(&attr, sizeof(attr), 0, sizeof(attr));
ret = mq_getattr(mqueue, &attr);
ASSERT_EQ(ret, 0);
attr.mq_flags |= O_NONBLOCK;
ret = mq_setattr(mqueue, &attr, NULL);
ASSERT_EQ(ret, 0);
ret = mq_getattr(mqueue, &attr);
ASSERT_EQ(ret, 0);
ret = mq_send(mqueue, msgptr, strlen(msgptr), 0);
ASSERT_EQ(ret, 0);
ret = mq_close(mqueue);
ASSERT_EQ(ret, 0);
ret = mq_unlink(mqname);
ASSERT_EQ(ret, 0);
childPid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
ASSERT_NE(childPid, -1);
parentPid = getpid();
parentlink = ReadlinkContainer(parentPid, containerType);
childlink = ReadlinkContainer(childPid, containerType);
filePath = GenContainerLinkPath(childPid, containerType);
fd = open(filePath.c_str(), O_RDONLY);
ASSERT_NE(fd, -1);
ret = setns(fd, CLONE_NEWIPC);
ASSERT_NE(ret, -1);
ret = close(fd);
ASSERT_NE(ret, -1);
ret = waitpid(childPid, &status, 0);
ASSERT_EQ(ret, childPid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, EXIT_CODE_ERRNO_7);
}

View File

@@ -0,0 +1,163 @@
/*
* 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 <fstream>
#include <iostream>
#include "It_container_test.h"
using namespace std;
static int childFunc(void *arg)
{
int ret;
(void)arg;
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
char mqname[] = "/testMQueue005";
const char msgptr[] = "childMsg";
struct mq_attr attr = { 0 };
mqd_t mqueue;
attr.mq_msgsize = MQUEUE_TEST_SIZE;
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
mqueue = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, &attr);
if (mqueue == (mqd_t)-1) {
goto EXIT1;
}
ret = mq_send(mqueue, msgptr, strlen(msgptr), 0);
if (ret != 0) {
goto EXIT1;
}
ret = mq_receive(mqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
if (ret != strlen(msgptr)) {
goto EXIT1;
}
if (strncmp(msgrcd, msgptr, strlen(msgptr)) != 0) {
goto EXIT1;
}
EXIT1:
if (mqueue >= 0) {
ret = mq_close(mqueue);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
ret = mq_unlink(mqname);
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
}
return EXIT_CODE_ERRNO_7;
}
static void IpcContainerUnshare(void)
{
int status, exitCode, ret;
int arg = CHILD_FUNC_ARG;
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
struct mq_attr attr = { 0 };
attr.mq_msgsize = MQUEUE_TEST_SIZE;
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
struct sigevent notification;
notification.sigev_notify = 5; /* 5: test data */
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
char mqname[] = "/testMQueue006";
const char msgptr[] = "parentMsg";
ret = unshare(CLONE_NEWIPC);
ASSERT_EQ(ret, 0);
mqd_t mqueue = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, &attr);
MQueueFinalizer mQueueFinalizer(mqueue, mqname);
ASSERT_NE(mqueue, (mqd_t)-1);
ret = mq_notify(mqueue, &notification);
ASSERT_EQ(ret, -1);
ASSERT_EQ(errno, EINVAL);
notification.sigev_notify = SIGEV_THREAD;
ret = mq_notify(mqueue, &notification);
ASSERT_EQ(ret, -1);
ASSERT_EQ(errno, ENOTSUP);
notification.sigev_notify = SIGEV_NONE;
ret = mq_notify(-1, &notification);
ASSERT_EQ(ret, -1);
ASSERT_EQ(errno, EBADF);
ret = mq_notify(mqueue, &notification);
ASSERT_EQ(ret, 0);
(void)memset_s(&attr, sizeof(attr), 0, sizeof(attr));
ret = mq_getattr(mqueue, &attr);
ASSERT_EQ(ret, 0);
attr.mq_flags |= O_NONBLOCK;
ret = mq_setattr(mqueue, &attr, NULL);
ASSERT_EQ(ret, 0);
ret = mq_getattr(mqueue, &attr);
ASSERT_EQ(ret, 0);
ret = mq_send(mqueue, msgptr, strlen(msgptr), 0);
ASSERT_EQ(ret, 0);
pid_t pid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
ASSERT_NE(pid, -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(pid, 0);
exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, EXIT_CODE_ERRNO_7);
ret = mq_notify(mqueue, nullptr);
ASSERT_EQ(ret, 0);
ret = mq_receive(mqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
ASSERT_EQ(ret, strlen(msgptr));
ASSERT_STREQ(msgrcd, msgptr);
}
void ItIpcContainer003(void)
{
auto pid = fork();
ASSERT_TRUE(pid != -1);
if (pid == 0) {
IpcContainerUnshare();
exit(0);
}
auto ret = waitpid(pid, NULL, 0);
ASSERT_EQ(ret, pid);
}

View File

@@ -0,0 +1,217 @@
/*
* 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 const char *containerType = "ipc";
struct shared_use_st {
char test[SHM_TEST_DATA_SIZE];
};
static int childFunc1(void *arg)
{
struct shared_use_st *shared = NULL;
const char testBuf[] = "child test shm";
int ret;
(void)arg;
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
if (shmid == -1) {
return EXIT_CODE_ERRNO_1;
}
void *shm = shmat(shmid, 0, 0);
if (shm == reinterpret_cast<void *>(-1)) {
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_2;
}
shared = (struct shared_use_st *)shm;
ret = strncmp(shared->test, testBuf, strlen(testBuf));
if (ret != 0) {
shmdt(shm);
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_3;
}
ret = shmdt(shm);
if (ret == -1) {
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_4;
}
return 0;
}
static int childFunc(void *arg)
{
const char testBuf[] = "parent test shm";
const char testBuf1[] = "child test shm";
int ret, status, pid, exitCode;
(void)arg;
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
if (stack == nullptr) {
return EXIT_CODE_ERRNO_17;
}
char *stackTop = stack + STACK_SIZE;
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
if (shmid == -1) {
return EXIT_CODE_ERRNO_1;
}
void *shm = shmat(shmid, 0, 0);
if (shm == reinterpret_cast<void *>(-1)) {
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_2;
}
struct shared_use_st *shared = (struct shared_use_st *)shm;
ret = strncmp(shared->test, testBuf, strlen(testBuf));
if (ret == 0) {
ret = EXIT_CODE_ERRNO_3;
goto EXIT;
}
ret = memcpy_s(shared->test, sizeof(struct shared_use_st), testBuf1, sizeof(testBuf1));
if (ret != 0) {
ret = EXIT_CODE_ERRNO_4;
goto EXIT;
}
pid = clone(childFunc1, stackTop, SIGCHLD, &arg);
if (pid == -1) {
ret = EXIT_CODE_ERRNO_5;
goto EXIT;
}
ret = waitpid(pid, &status, 0);
if (ret != pid) {
ret = EXIT_CODE_ERRNO_6;
goto EXIT;
}
ret = WIFEXITED(status);
if (ret == 0) {
ret = EXIT_CODE_ERRNO_7;
goto EXIT;
}
exitCode = WEXITSTATUS(status);
if (exitCode != 0) {
ret = EXIT_CODE_ERRNO_8;
goto EXIT;
}
ret = shmdt(shm);
if (ret == -1) {
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_9;
}
ret = shmctl(shmid, IPC_RMID, 0);
if (ret == -1) {
return EXIT_CODE_ERRNO_10;
}
return 0;
EXIT:
shmdt(shm);
shmctl(shmid, IPC_RMID, 0);
return ret;
}
void ItIpcContainer004(void)
{
const char testBuf[] = "parent test shm";
int pid, exitCode, status, ret;
void *shm = NULL;
struct shmid_ds ds = {};
struct shminfo info = {};
int arg = CHILD_FUNC_ARG;
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
ShmFinalizer ShmFinalizer(shm, shmid);
ASSERT_NE(shmid, -1);
shm = shmat(shmid, 0, 0);
ASSERT_NE((int)shm, -1);
struct shared_use_st *shared = (struct shared_use_st *)shm;
ret = memcpy_s(shared->test, sizeof(struct shared_use_st), testBuf, sizeof(testBuf));
ASSERT_EQ(ret, 0);
pid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
ASSERT_NE(pid, -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
ret = shmctl(shmid, IPC_STAT, &ds);
ASSERT_EQ(ret, 0);
ASSERT_EQ(ds.shm_segsz, PAGE_SIZE);
ASSERT_EQ(ds.shm_nattch, 1);
ASSERT_EQ(ds.shm_cpid, getpid());
ASSERT_EQ(ds.shm_lpid, getpid());
ASSERT_EQ(ds.shm_perm.uid, getuid());
ret = shmctl(shmid, SHM_STAT, &ds);
ASSERT_NE(ret, -1);
ASSERT_NE(ret, 0);
ds.shm_perm.uid = getuid();
ds.shm_perm.gid = getgid();
ds.shm_perm.mode = 0;
ret = shmctl(shmid, IPC_SET, &ds);
ASSERT_EQ(ret, 0);
ret = shmctl(shmid, IPC_INFO, (struct shmid_ds *)&info);
ASSERT_EQ(ret, 192); /* 192: test value */
ASSERT_EQ(info.shmmax, 0x1000000); /* 0x1000000: Shared memory information */
ASSERT_EQ(info.shmmin, 1); /* 1: Shared memory information */
ASSERT_EQ(info.shmmni, 192); /* 192: Shared memory information */
ASSERT_EQ(info.shmseg, 128); /* 128: Shared memory information */
ASSERT_EQ(info.shmall, 0x1000); /* 0x1000: Shared memory information */
ret = shmdt(shm);
ASSERT_NE(ret, -1);
ret = shmctl(shmid, IPC_RMID, NULL);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,187 @@
/*
* 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 const char testBuf[] = "test shm";
struct shared_use_st {
char test[SHM_TEST_DATA_SIZE];
};
static int childFunc(void *arg)
{
struct shared_use_st *shared = NULL;
int ret;
(void)arg;
char *containerType = "ipc";
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
ret = unshare(CLONE_NEWIPC);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType);
ret = linkBuffer.compare(linkBuffer1);
if (ret == 0) {
return EXIT_CODE_ERRNO_2;
}
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
if (shmid == -1) {
return EXIT_CODE_ERRNO_3;
}
void *shm = shmat(shmid, 0, 0);
if (shm == reinterpret_cast<void *>(-1)) {
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_4;
}
shared = (struct shared_use_st *)shm;
ret = strncmp(shared->test, testBuf, strlen(testBuf));
if (ret == 0) {
shmdt(shm);
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_5;
}
ret = shmdt(shm);
if (ret == -1) {
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_6;
}
ret = shmctl(shmid, IPC_RMID, 0);
if (ret == -1) {
return EXIT_CODE_ERRNO_7;
}
return 0;
}
static int testChild(void *arg)
{
(void)arg;
int ret;
struct shared_use_st *shared = NULL;
int status;
int exitCode;
int pid;
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
if (stack == nullptr) {
return EXIT_CODE_ERRNO_17;
}
char *stackTop = stack + STACK_SIZE;
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
if (shmid == -1) {
return EXIT_CODE_ERRNO_8;
}
void *shm = shmat(shmid, 0, 0);
if (shm == reinterpret_cast<void *>(-1)) {
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_9;
}
shared = (struct shared_use_st *)shm;
ret = memcpy_s(shared->test, sizeof(struct shared_use_st), testBuf, sizeof(testBuf));
if (ret != 0) {
shmdt(shm);
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_10;
}
pid = clone(childFunc, stackTop, SIGCHLD, &arg);
if (pid == -1) {
shmdt(shm);
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_11;
}
ret = waitpid(pid, &status, 0);
if (ret != pid) {
shmdt(shm);
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_12;
}
ret = WIFEXITED(status);
if (ret == 0) {
shmdt(shm);
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_13;
}
exitCode = WEXITSTATUS(status);
if (exitCode != 0) {
shmdt(shm);
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_14;
}
ret = shmdt(shm);
if (ret == -1) {
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_15;
}
ret = shmctl(shmid, IPC_RMID, 0);
if (ret == -1) {
return EXIT_CODE_ERRNO_16;
}
return 0;
}
void ItIpcContainer005(void)
{
int ret;
int status;
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
int arg = CHILD_FUNC_ARG;
auto pid = clone(testChild, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
ASSERT_NE(pid, -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
}

View File

@@ -0,0 +1,184 @@
/*
* 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 const char testBuf[] = "test shm";
static const char *containerType = "ipc";
struct shared_use_st {
char test[SHM_TEST_DATA_SIZE];
};
static int childFunc1(void *arg)
{
struct shared_use_st *shared = NULL;
int ret;
(void)arg;
const int sleep_num = 3; /* 3: delay */
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
if (shmid == -1) {
return EXIT_CODE_ERRNO_1;
}
void *shm = shmat(shmid, 0, 0);
if (shm == reinterpret_cast<void *>(-1)) {
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_2;
}
shared = (struct shared_use_st *)shm;
sleep(sleep_num);
ret = strncmp(shared->test, testBuf, strlen(testBuf));
if (ret != 0) {
shmdt(shm);
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_3;
}
ret = shmdt(shm);
if (ret == -1) {
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_4;
}
return 0;
}
static int childFunc(void *arg)
{
struct shared_use_st *shared = NULL;
int ret;
(void)arg;
int status;
int exitCode;
const int sleep_num = 1;
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
if (stack == nullptr) {
return EXIT_CODE_ERRNO_17;
}
char *stackTop = stack + STACK_SIZE;
auto pid = clone(childFunc1, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
if (pid == -1) {
return EXIT_CODE_ERRNO_6;
}
std::string filePath = GenContainerLinkPath(pid, containerType);
int fd = open(filePath.c_str(), O_RDONLY);
if (fd == -1) {
return EXIT_CODE_ERRNO_7;
}
sleep(sleep_num);
ret = setns(fd, CLONE_NEWIPC);
if (ret == -1) {
close(fd);
return EXIT_CODE_ERRNO_8;
}
close(fd);
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType);
auto linkBuffer2 = ReadlinkContainer(pid, containerType);
ret = linkBuffer.compare(linkBuffer1);
if (ret == 0) {
return EXIT_CODE_ERRNO_9;
}
ret = linkBuffer1.compare(linkBuffer2);
if (ret != 0) {
return EXIT_CODE_ERRNO_10;
}
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
if (shmid == -1) {
return EXIT_CODE_ERRNO_11;
}
void *shm = shmat(shmid, 0, 0);
if (shm == reinterpret_cast<void *>(-1)) {
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_12;
}
shared = (struct shared_use_st *)shm;
ret = memcpy_s(shared->test, sizeof(struct shared_use_st), testBuf, sizeof(testBuf));
if (ret != 0) {
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_13;
}
ret = waitpid(pid, &status, 0);
if (ret != pid) {
shmdt(shm);
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_14;
}
exitCode = WEXITSTATUS(status);
if (exitCode != 0) {
shmdt(shm);
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_15;
}
ret = shmdt(shm);
if (ret == -1) {
shmctl(shmid, IPC_RMID, 0);
return EXIT_CODE_ERRNO_16;
}
ret = shmctl(shmid, IPC_RMID, 0);
if (ret == -1) {
return EXIT_CODE_ERRNO_255;
}
return 0;
}
void ItIpcContainer006(void)
{
int ret;
int status;
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
int arg = CHILD_FUNC_ARG;
auto pid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
ASSERT_NE(pid, -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
}

View File

@@ -34,7 +34,6 @@ static int ChildFunc(void *arg)
{
int ret;
int value = *((int *)arg);
if (value != CHILD_FUNC_ARG) {
return EXIT_CODE_ERRNO_1;
}

View File

@@ -32,7 +32,7 @@
/*
* mount container unshare test: unshare in current pcb with NEW_NS
*/
void ItMntContainer005(void)
static void MntContainerUnshare(void)
{
int ret;
@@ -56,4 +56,22 @@ void ItMntContainer005(void)
ret = access(ACCESS_FILE_NAME, F_OK);
ASSERT_EQ(ret, 0);
exit(0);
}
void ItMntContainer005(void)
{
int status = 0;
auto pid = fork();
ASSERT_TRUE(pid != -1);
if (pid == 0) {
MntContainerUnshare();
exit(EXIT_CODE_ERRNO_1);
}
auto ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,69 @@
/*
* 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"
static int childFunc(void *arg)
{
(void)arg;
return 0;
}
void ItPidContainer025(void)
{
int ret;
int status;
char *containerType = "pid";
char *containerType1 = "pid_for_children";
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
std::cout << getpid() << std::endl;
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType1);
ret = linkBuffer.compare(linkBuffer1);
ASSERT_EQ(ret, 0);
auto pid = clone(childFunc, stackTop, CLONE_NEWPID | SIGCHLD, NULL);
ASSERT_TRUE(pid != -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
auto linkBuffer2 = ReadlinkContainer(getpid(), containerType);
ret = linkBuffer.compare(linkBuffer2);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,70 @@
/*
* 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"
static int childFunc(void *arg)
{
(void)arg;
sleep(1);
return 0;
}
void ItPidContainer026(void)
{
int ret;
int status;
char *containerType = "pid";
char *containerType1 = "pid_for_children";
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
std::cout << getpid() << std::endl;
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType1);
ret = linkBuffer.compare(linkBuffer1);
ASSERT_EQ(ret, 0);
auto pid = clone(childFunc, stackTop, CLONE_NEWPID | SIGCHLD, NULL);
ASSERT_TRUE(pid != -1);
auto linkBuffer2 = ReadlinkContainer(pid, containerType);
ret = linkBuffer.compare(linkBuffer2);
ASSERT_NE(ret, 0);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
}

View File

@@ -0,0 +1,147 @@
/*
* 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"
static int ChildFunClone3(void *p)
{
(void)p;
auto pid = getpid();
if (pid != CONTAINER_SECOND_PID) {
return EXIT_CODE_ERRNO_1;
}
return 0;
}
static int ChildFunClone2(void *p)
{
(void)p;
auto pid = getpid();
if (pid != CONTAINER_FIRST_PID) {
return EXIT_CODE_ERRNO_1;
}
int ret;
int status;
void *pstk = malloc(STACK_SIZE);
if (pstk == NULL) {
return EXIT_CODE_ERRNO_2;
}
int childPid = clone(ChildFunClone3, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
if (childPid == -1) {
free(pstk);
return EXIT_CODE_ERRNO_3;
}
ret = waitpid(childPid, &status, 0);
ret = WIFEXITED(status);
ret = WEXITSTATUS(status);
if (ret != 0) {
free(pstk);
return EXIT_CODE_ERRNO_4;
}
free(pstk);
return 0;
}
static int ChildFunClone1(void *p)
{
(void)p;
int ret;
int status;
const char *containerType = "pid";
const char *containerType1 = "pid_for_children";
auto pid = getpid();
ret = unshare(CLONE_NEWPID);
if (ret == -1) {
return EXIT_CODE_ERRNO_1;
}
auto pid1 = getpid();
if (pid != pid1) {
return EXIT_CODE_ERRNO_2;
}
auto linkBuffer = ReadlinkContainer(pid, containerType);
auto linkBuffer1 = ReadlinkContainer(pid, containerType1);
ret = linkBuffer.compare(linkBuffer1);
if (ret == 0) {
printf("linkBuffer: %s linkBuffer1: %s\n", linkBuffer.c_str(), linkBuffer1.c_str());
return EXIT_CODE_ERRNO_3;
}
void *pstk = malloc(STACK_SIZE);
if (pstk == NULL) {
return EXIT_CODE_ERRNO_4;
}
int childPid = clone(ChildFunClone2, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
free(pstk);
if (childPid == -1) {
return EXIT_CODE_ERRNO_5;
}
ret = unshare(CLONE_NEWPID);
if (ret != -1) {
return EXIT_CODE_ERRNO_6;
}
ret = waitpid(childPid, &status, 0);
if (ret != childPid) {
return EXIT_CODE_ERRNO_7;
}
ret = WIFEXITED(status);
if (ret == 0) {
return EXIT_CODE_ERRNO_8;
}
ret = WEXITSTATUS(status);
if (ret != 0) {
return EXIT_CODE_ERRNO_9;
}
return 0;
}
void ItPidContainer027(void)
{
void *pstk = malloc(STACK_SIZE);
ASSERT_TRUE(pstk != NULL);
int childPid = clone(ChildFunClone1, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
free(pstk);
ASSERT_NE(childPid, -1);
int status;
int ret = waitpid(childPid, &status, 0);
ASSERT_EQ(ret, childPid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,145 @@
/*
* 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"
static int ChildFunClone3(void *p)
{
(void)p;
auto pid = getpid();
if (pid != CONTAINER_SECOND_PID) {
return EXIT_CODE_ERRNO_1;
}
return 0;
}
static int ChildFunClone2(void *p)
{
(void)p;
auto pid = getpid();
if (pid != CONTAINER_FIRST_PID) {
return EXIT_CODE_ERRNO_1;
}
int ret;
int status;
void *pstk = malloc(STACK_SIZE);
if (pstk == NULL) {
return EXIT_CODE_ERRNO_2;
}
int childPid = clone(ChildFunClone3, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
if (childPid == -1) {
free(pstk);
return EXIT_CODE_ERRNO_3;
}
ret = waitpid(childPid, &status, 0);
ret = WIFEXITED(status);
ret = WEXITSTATUS(status);
if (ret != 0) {
free(pstk);
return EXIT_CODE_ERRNO_4;
}
free(pstk);
return 0;
}
static int ChildFunClone1(void *p)
{
(void)p;
int ret;
int status;
const char *containerType = "pid";
const char *containerType1 = "pid_for_children";
auto pid = getpid();
ret = unshare(CLONE_NEWPID);
if (ret == -1) {
return EXIT_CODE_ERRNO_1;
}
auto pid1 = getpid();
if (pid != pid1) {
return EXIT_CODE_ERRNO_2;
}
auto linkBuffer = ReadlinkContainer(pid, containerType);
auto linkBuffer1 = ReadlinkContainer(pid, containerType1);
ret = linkBuffer.compare(linkBuffer1);
if (ret == 0) {
return EXIT_CODE_ERRNO_3;
}
void *pstk = malloc(STACK_SIZE);
if (pstk == NULL) {
return EXIT_CODE_ERRNO_4;
}
int childPid = clone(ChildFunClone2, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
free(pstk);
if (childPid == -1) {
return EXIT_CODE_ERRNO_5;
}
ret = unshare(CLONE_NEWPID);
if (ret != -1) {
return EXIT_CODE_ERRNO_6;
}
ret = waitpid(childPid, &status, 0);
if (ret != childPid) {
return EXIT_CODE_ERRNO_7;
}
ret = WIFEXITED(status);
if (ret == 0) {
return EXIT_CODE_ERRNO_8;
}
ret = WEXITSTATUS(status);
if (ret != 0) {
return EXIT_CODE_ERRNO_9;
}
return 0;
}
void ItPidContainer028(void)
{
void *pstk = malloc(STACK_SIZE);
ASSERT_TRUE(pstk != NULL);
int childPid = clone(ChildFunClone1, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
free(pstk);
ASSERT_NE(childPid, -1);
int status;
int ret = waitpid(childPid, &status, 0);
ASSERT_EQ(ret, childPid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,129 @@
/*
* 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"
static int ChildFunClone3(void *p)
{
(void)p;
auto pid = getpid();
if (pid != CONTAINER_FIRST_PID) {
return EXIT_CODE_ERRNO_1;
}
return 0;
}
static int ChildFunClone2(void *p)
{
(void)p;
auto pid = getpid();
if (pid != CONTAINER_FIRST_PID) {
return EXIT_CODE_ERRNO_1;
}
return 0;
}
static int ChildFunClone1(void *p)
{
(void)p;
int ret;
int status;
const char *containerType = "pid";
const char *containerType1 = "pid_for_children";
auto pid = getpid();
ret = unshare(CLONE_NEWPID);
if (ret == -1) {
return EXIT_CODE_ERRNO_1;
}
auto pid1 = getpid();
if (pid != pid1) {
return EXIT_CODE_ERRNO_2;
}
auto linkBuffer = ReadlinkContainer(pid, containerType);
auto linkBuffer1 = ReadlinkContainer(pid, containerType1);
ret = linkBuffer.compare(linkBuffer1);
if (ret == 0) {
return EXIT_CODE_ERRNO_3;
}
void *pstk = malloc(STACK_SIZE);
if (pstk == NULL) {
return EXIT_CODE_ERRNO_4;
}
int childPid = clone(ChildFunClone2, (char *)pstk + STACK_SIZE, CLONE_NEWPID | SIGCHLD, NULL);
if (childPid != -1) {
free(pstk);
return EXIT_CODE_ERRNO_5;
}
int childPid1 = clone(ChildFunClone3, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
free(pstk);
if (childPid1 == -1) {
return EXIT_CODE_ERRNO_6;
}
ret = unshare(CLONE_NEWPID);
if (ret != -1) {
return EXIT_CODE_ERRNO_7;
}
ret = waitpid(childPid1, &status, 0);
if (ret != childPid1) {
return EXIT_CODE_ERRNO_11;
}
ret = WIFEXITED(status);
if (ret == 0) {
return EXIT_CODE_ERRNO_12;
}
ret = WEXITSTATUS(status);
if (ret != 0) {
return EXIT_CODE_ERRNO_13;
}
return 0;
}
void ItPidContainer029(void)
{
void *pstk = malloc(STACK_SIZE);
ASSERT_TRUE(pstk != NULL);
int childPid = clone(ChildFunClone1, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
free(pstk);
ASSERT_NE(childPid, -1);
int status;
int ret = waitpid(childPid, &status, 0);
ASSERT_EQ(ret, childPid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,135 @@
/*
* 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"
static int ChildFunClone3(void *p)
{
(void)p;
auto pid = getpid();
if (pid != CONTAINER_SECOND_PID) {
return EXIT_CODE_ERRNO_1;
}
sleep(2); /* dealy 2s */
return 0;
}
static int ChildFunClone2(void *p)
{
(void)p;
auto pid = getpid();
if (pid != CONTAINER_FIRST_PID) {
return EXIT_CODE_ERRNO_1;
}
sleep(2); /* dealy 2s */
return 0;
}
static int ChildFunClone1(void *p)
{
(void)p;
int ret, status;
const char *containerType = "pid";
const char *containerType1 = "pid_for_children";
auto pid = getpid();
ret = unshare(CLONE_NEWPID);
if (ret == -1) {
return EXIT_CODE_ERRNO_1;
}
auto pid1 = getpid();
if (pid != pid1) {
return EXIT_CODE_ERRNO_2;
}
auto linkBuffer = ReadlinkContainer(pid, containerType);
auto linkBuffer1 = ReadlinkContainer(pid, containerType1);
ret = linkBuffer.compare(linkBuffer1);
if (ret == 0) {
return EXIT_CODE_ERRNO_3;
}
void *pstk = malloc(STACK_SIZE);
if (pstk == NULL) {
return EXIT_CODE_ERRNO_4;
}
int childPid = clone(ChildFunClone2, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
if (childPid == -1) {
free(pstk);
return EXIT_CODE_ERRNO_5;
}
auto linkBuffer2 = ReadlinkContainer(childPid, containerType);
int childPid1 = clone(ChildFunClone3, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
free(pstk);
if (childPid1 == -1) {
return EXIT_CODE_ERRNO_6;
}
auto linkBuffer3 = ReadlinkContainer(childPid1, containerType);
ret = linkBuffer3.compare(linkBuffer2);
if (ret != 0) {
return EXIT_CODE_ERRNO_7;
}
ret = unshare(CLONE_NEWPID);
if (ret != -1) {
return EXIT_CODE_ERRNO_8;
}
ret = WaitChild(childPid1, &status, EXIT_CODE_ERRNO_9, EXIT_CODE_ERRNO_10);
if (ret != 0) {
return ret;
}
ret = WaitChild(childPid, &status, EXIT_CODE_ERRNO_11, EXIT_CODE_ERRNO_12);
if (ret != 0) {
return ret;
}
return 0;
}
void ItPidContainer030(void)
{
void *pstk = malloc(STACK_SIZE);
ASSERT_TRUE(pstk != NULL);
int childPid = clone(ChildFunClone1, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
free(pstk);
ASSERT_NE(childPid, -1);
int status;
int ret = waitpid(childPid, &status, 0);
ASSERT_EQ(ret, childPid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,146 @@
/*
* 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"
static const int BUF_SIZE = 100;
static int ChildFun2(void *p)
{
(void)p;
sleep(3); /* 3: delay 3s */
return 0;
}
static int ChildFun1(void *p)
{
(void)p;
sleep(6); /* 6: delay 6s */
return 0;
}
static int ChildFun(void *p)
{
(void)p;
int status, ret;
const char *containerType = "pid";
const char *containerType1 = "pid_for_children";
char targetpath[BUF_SIZE] = {0};
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType);
int childPid = clone(ChildFun1, NULL, CLONE_NEWPID | SIGCHLD, NULL);
if (childPid == -1) {
return EXIT_CODE_ERRNO_1;
}
auto linkBuffer2 = ReadlinkContainer(childPid, containerType);
ret = linkBuffer2.compare(linkBuffer1);
if (ret == 0) {
return EXIT_CODE_ERRNO_2;
}
ret = sprintf_s(targetpath, BUF_SIZE, "/proc/%d/container/pid", childPid);
if (ret < 0) {
return EXIT_CODE_ERRNO_16;
}
int fd = open(targetpath, O_RDONLY | O_CLOEXEC);
if (fd == -1) {
return EXIT_CODE_ERRNO_3;
}
ret = setns(fd, CLONE_NEWPID);
if (ret != 0) {
close(fd);
return EXIT_CODE_ERRNO_4;
}
close(fd);
auto linkBuffer6 = ReadlinkContainer(getpid(), containerType);
ret = linkBuffer6.compare(linkBuffer1);
if (ret != 0) {
return EXIT_CODE_ERRNO_17;
}
auto linkBuffer3 = ReadlinkContainer(getpid(), containerType1);
ret = linkBuffer3.compare(linkBuffer2);
if (ret != 0) {
return EXIT_CODE_ERRNO_5;
}
int childPid1 = clone(ChildFun2, NULL, SIGCHLD, NULL);
if (childPid1 == -1) {
return EXIT_CODE_ERRNO_6;
}
auto linkBuffer4 = ReadlinkContainer(childPid1, containerType);
ret = linkBuffer4.compare(linkBuffer3);
if (ret != 0) {
return EXIT_CODE_ERRNO_7;
}
int childPid2 = clone(ChildFun2, NULL, CLONE_NEWUTS | SIGCHLD, NULL);
if (childPid2 == -1) {
return EXIT_CODE_ERRNO_8;
}
auto linkBuffer5 = ReadlinkContainer(childPid2, containerType);
ret = linkBuffer5.compare(linkBuffer4);
if (ret != 0) {
return EXIT_CODE_ERRNO_9;
}
ret = WaitChild(childPid2, &status, EXIT_CODE_ERRNO_10, EXIT_CODE_ERRNO_11);
if (ret != 0) {
return ret;
}
ret = WaitChild(childPid1, &status, EXIT_CODE_ERRNO_12, EXIT_CODE_ERRNO_13);
if (ret != 0) {
return ret;
}
ret = WaitChild(childPid, &status, EXIT_CODE_ERRNO_14, EXIT_CODE_ERRNO_15);
if (ret != 0) {
return ret;
}
return 0;
}
void ItPidContainer031(void)
{
int status;
int childPid = clone(ChildFun, NULL, CLONE_NEWPID | SIGCHLD, NULL);
ASSERT_NE(childPid, -1);
int ret = waitpid(childPid, &status, 0);
ASSERT_EQ(ret, childPid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,74 @@
/*
* 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"
static int childFunc(void *arg)
{
int value = *((int*)arg);
if (value != CHILD_FUNC_ARG) {
return EXIT_CODE_ERRNO_1;
}
sleep(1);
return 0;
}
void ItTimeContainer001(void)
{
int ret;
int status;
char *containerType = "time";
char *containerType1 = "time_for_children";
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
std::cout << getpid() << std::endl;
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType1);
ret = linkBuffer.compare(linkBuffer1);
ASSERT_EQ(ret, 0);
int arg = CHILD_FUNC_ARG;
auto pid = clone(childFunc, stackTop, CLONE_NEWTIME | SIGCHLD, &arg);
ASSERT_TRUE(pid != -1);
auto linkBuffer2 = ReadlinkContainer(pid, containerType);
ret = linkBuffer.compare(linkBuffer2);
ASSERT_EQ(ret, 0);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
}

View File

@@ -0,0 +1,165 @@
/*
* 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 STR_LEN = 50;
const int SEC = 600;
const int NSEC = 800000000;
static int childFunc(void *arg)
{
char path[STR_LEN];
char timeOff[STR_LEN];
char readBuf[STR_LEN];
int ret;
ret = sprintf_s(timeOff, STR_LEN, "monotonic %d %d", SEC, NSEC);
if (ret <= 0) {
return EXIT_CODE_ERRNO_4;
}
ret = sprintf_s(path, STR_LEN, "/proc/%d/time_offsets", getpid());
if (ret <= 0) {
return EXIT_CODE_ERRNO_5;
}
int fd = open(path, O_RDWR);
if (fd == -1) {
return EXIT_CODE_ERRNO_1;
}
ret = read(fd, readBuf, STR_LEN);
if (ret == -1) {
close(fd);
return EXIT_CODE_ERRNO_2;
}
close(fd);
ret = strncmp(timeOff, readBuf, strlen(timeOff));
if (ret != 0) {
return EXIT_CODE_ERRNO_3;
}
return 0;
}
static int WriteProcTime(int pid)
{
int ret = 0;
char path[STR_LEN];
char timeOff[STR_LEN];
ret = sprintf_s(timeOff, STR_LEN, "monotonic %d %d", SEC, NSEC);
if (ret <= 0) {
return EXIT_CODE_ERRNO_6;
}
ret = sprintf_s(path, STR_LEN, "/proc/%d/time_offsets", pid);
if (ret <= 0) {
return EXIT_CODE_ERRNO_7;
}
int strLen = strlen(timeOff);
int fd = open(path, O_WRONLY);
if (ret == -1) {
return EXIT_CODE_ERRNO_8;
}
ret = write(fd, timeOff, strLen);
if (ret != strLen) {
close(fd);
return EXIT_CODE_ERRNO_9;
}
close(fd);
return 0;
}
static void TimeContainerUnshare(void)
{
int ret;
int status;
char *containerType = "time";
char *containerType1 = "time_for_children";
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
ret = unshare(CLONE_NEWTIME);
ASSERT_EQ(ret, 0);
ret = unshare(CLONE_NEWTIME);
ASSERT_EQ(ret, -1);
ret = WriteProcTime(getpid());
ASSERT_EQ(ret, 0);
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType1);
ret = linkBuffer.compare(linkBuffer1);
ASSERT_TRUE(ret != 0);
int arg = CHILD_FUNC_ARG;
auto pid = clone(childFunc, stackTop, CLONE_NEWTIME | SIGCHLD, &arg);
ASSERT_TRUE(pid != -1);
auto linkBuffer2 = ReadlinkContainer(pid, containerType);
ret = linkBuffer1.compare(linkBuffer2);
ASSERT_EQ(ret, 0);
ret = unshare(CLONE_NEWTIME);
ASSERT_EQ(ret, -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_TRUE(ret != 0);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
exit(0);
}
void ItTimeContainer002(void)
{
int status = 0;
auto pid = fork();
ASSERT_TRUE(pid != -1);
if (pid == 0) {
TimeContainerUnshare();
exit(EXIT_CODE_ERRNO_1);
}
auto ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,119 @@
/*
* 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"
static int childFunc(void *arg)
{
int ret;
char *containerType = "time";
char *containerType1 = "time_for_children";
char pid_link[100]; /* 100: test len */
char targetpath[100]; /* 100: test len */
auto linkBuffer = ReadlinkContainer(getppid(), containerType);
int fd;
ret = sprintf_s(pid_link, 100, "%s", linkBuffer.c_str()); /* 100: test len */
if (ret <= 0) {
return EXIT_CODE_ERRNO_4;
}
ret = sprintf_s(targetpath, 100, "/proc/%d/container/time", getppid()); /* 100: test len */
if (ret <= 0) {
return EXIT_CODE_ERRNO_5;
}
fd = open(targetpath, O_RDONLY | O_CLOEXEC);
if (fd == -1) {
return EXIT_CODE_ERRNO_1;
}
ret = setns(fd, CLONE_NEWTIME);
if (ret != 0) {
close(fd);
return EXIT_CODE_ERRNO_2;
}
close(fd);
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType);
auto linkBuffer2 = ReadlinkContainer(getpid(), containerType1);
ret = linkBuffer1.compare(linkBuffer2);
EXPECT_EQ(ret, 0);
if (ret != 0) {
return EXIT_CODE_ERRNO_3;
}
return 0;
}
static void TimeContainerUnshare(void)
{
int ret;
int status;
char *containerType = "time";
char *containerType1 = "time_for_children";
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
ret = unshare(CLONE_NEWTIME);
ASSERT_EQ(ret, 0);
auto linkBuffer = ReadlinkContainer(getpid(), containerType1);
int arg = CHILD_FUNC_ARG;
auto pid = clone(childFunc, stackTop, CLONE_NEWTIME | SIGCHLD, &arg);
ASSERT_TRUE(pid != -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_TRUE(ret != 0);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
exit(0);
}
void ItTimeContainer003(void)
{
int status = 0;
auto pid = fork();
ASSERT_TRUE(pid != -1);
if (pid == 0) {
TimeContainerUnshare();
exit(EXIT_CODE_ERRNO_1);
}
auto ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,133 @@
/*
* 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 STR_LEN = 50;
const int SEC = 172800;
const int NSEC = 800000000;
static int childFunc(void *arg)
{
int ret;
struct timespec tp = *((struct timespec *)arg);
struct timespec tp1;
ret = clock_gettime(CLOCK_MONOTONIC_RAW, &tp1);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
ret = ((tp1.tv_sec >= tp.tv_sec + SEC));
if (ret != 1) {
return EXIT_CODE_ERRNO_2;
}
return 0;
}
static int WriteProcTime(int pid)
{
int ret;
char path[STR_LEN];
char timeOff[STR_LEN];
ret = sprintf_s(timeOff, STR_LEN, "monotonic %d %d", SEC, NSEC);
if (ret <= 0) {
return EXIT_CODE_ERRNO_5;
}
ret = sprintf_s(path, STR_LEN, "/proc/%d/time_offsets", pid);
if (ret <= 0) {
return EXIT_CODE_ERRNO_6;
}
int strLen = strlen(timeOff);
int fd = open(path, O_WRONLY);
if (fd == -1) {
return EXIT_CODE_ERRNO_3;
}
ret = write(fd, timeOff, strLen);
if (ret != strLen) {
close(fd);
return EXIT_CODE_ERRNO_4;
}
close(fd);
return 0;
}
static void TimeContainerUnshare(void)
{
int ret;
int status;
struct timespec tp;
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
ret = unshare(CLONE_NEWTIME);
ASSERT_EQ(ret, 0);
ret = WriteProcTime(getpid());
ASSERT_EQ(ret, 0);
ret = clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
ASSERT_EQ(ret, 0);
auto pid = clone(childFunc, stackTop, CLONE_NEWTIME | SIGCHLD, &tp);
ASSERT_TRUE(pid != -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
exit(0);
}
void ItTimeContainer004(void)
{
int status = 0;
auto pid = fork();
ASSERT_TRUE(pid != -1);
if (pid == 0) {
TimeContainerUnshare();
exit(EXIT_CODE_ERRNO_1);
}
auto ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,133 @@
/*
* 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 STR_LEN = 50;
const int SEC = 172800;
const int NSEC = 800000000;
static int childFunc(void *arg)
{
int ret;
struct timespec tp = *((struct timespec *)arg);
struct timespec tp1 = {0};
ret = clock_gettime(CLOCK_MONOTONIC, &tp1);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
ret = ((tp1.tv_sec >= tp.tv_sec + SEC));
if (ret != 1) {
return EXIT_CODE_ERRNO_2;
}
return 0;
}
static int WriteProcTime(int pid)
{
int ret;
char path[STR_LEN];
char timeOff[STR_LEN];
ret = sprintf_s(timeOff, STR_LEN, "monotonic %d %d", SEC, NSEC);
if (ret <= 0) {
return EXIT_CODE_ERRNO_5;
}
ret = sprintf_s(path, STR_LEN, "/proc/%d/time_offsets", pid);
if (ret <= 0) {
return EXIT_CODE_ERRNO_5;
}
int strLen = strlen(timeOff);
int fd = open(path, O_WRONLY);
if (fd == -1) {
return EXIT_CODE_ERRNO_3;
}
ret = write(fd, timeOff, strLen);
if (ret != strLen) {
close(fd);
return EXIT_CODE_ERRNO_4;
}
close(fd);
return 0;
}
static void TimeContainerUnshare(void)
{
int ret;
int status;
struct timespec tp = {0};
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
ret = unshare(CLONE_NEWTIME);
ASSERT_EQ(ret, 0);
ret = WriteProcTime(getpid());
ASSERT_EQ(ret, 0);
ret = clock_gettime(CLOCK_MONOTONIC, &tp);
ASSERT_EQ(ret, 0);
auto pid = clone(childFunc, stackTop, CLONE_NEWTIME | SIGCHLD, &tp);
ASSERT_TRUE(pid != -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_TRUE(ret != 0);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
exit(0);
}
void ItTimeContainer005(void)
{
int status = 0;
auto pid = fork();
ASSERT_TRUE(pid != -1);
if (pid == 0) {
TimeContainerUnshare();
exit(EXIT_CODE_ERRNO_1);
}
auto ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,52 @@
/*
* 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 MAX_TIME_CONTAINER = 64;
const int STR_LEN = 100;
void ItTimeContainer006(void)
{
int ret;
char *fileName = "/proc/sys/user/max_time_container";
FILE *fp = nullptr;
char strBuf[STR_LEN] = {0};
fp = fopen(fileName, "rb");
ASSERT_TRUE(fp != 0);
ret = fread(strBuf, 1, STR_LEN, fp);
ASSERT_TRUE(ret != -1);
ret = atoi(strBuf);
ASSERT_EQ(ret, MAX_TIME_CONTAINER);
(void)fclose(fp);
}

View File

@@ -0,0 +1,73 @@
/*
* 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 STR_LEN = 50;
const int SEC = 172800;
const int NSEC = 80000000;
static int WriteProcTime(int pid)
{
int ret;
char path[STR_LEN] = {0};
char timeOff[STR_LEN] = {0};
ret = sprintf_s(timeOff, STR_LEN, "monotonic %d %d", SEC, NSEC);
if (ret < 0) {
return EXIT_CODE_ERRNO_3;
}
ret = sprintf_s(path, STR_LEN, "/proc/%d/time_offsets", pid);
if (ret < 0) {
return EXIT_CODE_ERRNO_4;
}
int strLen = strlen(timeOff);
int fd = open(path, O_WRONLY);
if (fd == -1) {
return EXIT_CODE_ERRNO_1;
}
ret = write(fd, timeOff, strLen);
if (ret != -1) {
close(fd);
return EXIT_CODE_ERRNO_2;
}
close(fd);
return 0;
}
void ItTimeContainer007(void)
{
int ret;
ret = WriteProcTime(getpid());
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"
const int STR_LEN = 50;
const int SEC = 172800;
const long long NSEC = 800000000000;
static int WriteProcTime(int pid)
{
int ret;
char path[STR_LEN] = {0};
char timeOff[STR_LEN] = {0};
ret = sprintf_s(timeOff, STR_LEN, "monotonic %d %lld", SEC, NSEC);
if (ret < 0) {
return EXIT_CODE_ERRNO_3;
}
ret = sprintf_s(path, STR_LEN, "/proc/%d/time_offsets", pid);
if (ret < 0) {
return EXIT_CODE_ERRNO_3;
}
int strLen = strlen(timeOff);
int fd = open(path, O_WRONLY);
if (fd == -1) {
return EXIT_CODE_ERRNO_1;
}
ret = write(fd, timeOff, strLen);
if (ret != -1) {
close(fd);
return EXIT_CODE_ERRNO_2;
}
close(fd);
return 0;
}
static void TimeContainerUnshare(void)
{
int ret = unshare(CLONE_NEWTIME);
ASSERT_EQ(ret, 0);
ret = WriteProcTime(getpid());
ASSERT_EQ(ret, 0);
exit(0);
}
void ItTimeContainer008(void)
{
int status = 0;
auto pid = fork();
ASSERT_TRUE(pid != -1);
if (pid == 0) {
TimeContainerUnshare();
exit(EXIT_CODE_ERRNO_1);
}
auto ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,75 @@
/*
* 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 <iostream>
#include "It_container_test.h"
static std::string GenTimeLinkPath(int pid)
{
std::ostringstream buf;
buf << "/proc/" << pid << "/container/time";
return buf.str();
}
static std::string ReadlinkTime(int pid)
{
auto path = GenTimeLinkPath(pid);
struct stat sb;
int ret = lstat(path.data(), &sb);
if (ret == -1) {
perror("lstat");
return std::string();
}
auto bufsiz = sb.st_size + 1;
if (sb.st_size == 0) {
bufsiz = PATH_MAX;
}
std::vector<char> buf(bufsiz);
auto nbytes = readlink(path.c_str(), buf.data(), bufsiz);
if (nbytes == -1) {
perror("readlink");
return std::string();
}
return buf.data();
}
void ItTimeContainer009(void)
{
auto timelink = ReadlinkTime(getpid());
std::cout << "Contents of the time link is: " << timelink << std::endl;
std::regex reg("'time:\\[[0-9]+\\]'");
bool ret = std::regex_match(timelink, reg);
ASSERT_TRUE(ret);
}

View File

@@ -0,0 +1,73 @@
/*
* 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"
static int childFunc(void *arg)
{
int value = *((int*)arg);
if (value != CHILD_FUNC_ARG) {
return EXIT_CODE_ERRNO_1;
}
return 0;
}
void ItTimeContainer010(void)
{
int ret;
int status;
char *containerType = "time";
char *containerType1 = "time_for_children";
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
std::cout << getpid() << std::endl;
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType1);
ret = linkBuffer.compare(linkBuffer1);
ASSERT_EQ(ret, 0);
int arg = CHILD_FUNC_ARG;
auto pid = clone(childFunc, stackTop, CLONE_NEWTIME | SIGCHLD, &arg);
ASSERT_TRUE(pid != -1);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
auto linkBuffer2 = ReadlinkContainer(getpid(), containerType);
ret = linkBuffer.compare(linkBuffer2);
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/utsname.h"
static int ChildFunc(void *arg)
{
(void)arg;
int ret;
ret = unshare(CLONE_NEWUTS);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
struct utsname newName;
ret = uname(&newName);
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
const char *name = "TestHostName";
ret = sethostname(name, strlen(name));
if (ret != 0) {
return EXIT_CODE_ERRNO_3;
}
struct utsname newName1;
ret = uname(&newName1);
if (ret != 0) {
return EXIT_CODE_ERRNO_4;
}
ret = strcmp(newName.nodename, newName1.nodename);
if (ret == 0) {
return EXIT_CODE_ERRNO_5;
}
return 0;
}
void ItUtsContainer004(void)
{
int ret;
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
ASSERT_TRUE(stack != NULL);
char *stackTop = stack + STACK_SIZE;
struct utsname oldName;
ret = uname(&oldName);
ASSERT_EQ(ret, 0);
auto pid = clone(ChildFunc, stackTop, SIGCHLD, NULL);
(void)munmap(stack, STACK_SIZE);
ASSERT_NE(pid, -1);
int status;
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
struct utsname oldName1;
ret = uname(&oldName1);
ASSERT_EQ(ret, 0);
ret = strcmp(oldName.nodename, oldName1.nodename);
ASSERT_EQ(ret, 0);
}

View File

@@ -0,0 +1,91 @@
/*
* 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 TIMER_INTERVAL_3S = 2;
static int ChildFun(void *p)
{
(void)p;
sleep(TIMER_INTERVAL_3S);
return EXIT_CODE_ERRNO_3;
}
void ItUtsContainer005(void)
{
pid_t callerPid;
int childPid;
int fd = -1;
int ret;
int status;
int setFlag;
char targetpath[100];
char old_uts_link[100];
char new_uts_link[100];
const char *containerType = "uts";
callerPid = getpid();
childPid = clone(ChildFun, NULL, CLONE_NEWUTS | SIGCHLD, NULL);
ASSERT_NE(childPid, -1);
auto linkBuffer = ReadlinkContainer(callerPid, containerType);
ASSERT_TRUE(linkBuffer.c_str() != NULL);
ret = sprintf_s(old_uts_link, sizeof(old_uts_link), "%s", linkBuffer.c_str());
ASSERT_NE(ret, -1);
ret = sprintf_s(targetpath, sizeof(targetpath), "/proc/%d/container/uts", childPid);
ASSERT_NE(ret, -1);
fd = open(targetpath, O_RDONLY | O_CLOEXEC);
ASSERT_NE(fd, -1);
setFlag = CLONE_NEWUTS;
ret = setns(fd, setFlag);
ASSERT_NE(ret, -1);
/* NOTE: close fd, otherwise test fail */
ret = close(fd);
fd = -1;
ASSERT_NE(ret, -1);
linkBuffer = ReadlinkContainer(callerPid, containerType);
ret = sprintf_s(new_uts_link, sizeof(new_uts_link), "%s", linkBuffer.c_str());
ASSERT_NE(ret, -1);
ASSERT_STRNE(old_uts_link, new_uts_link);
ret = waitpid(childPid, &status, 0);
ASSERT_EQ(ret, childPid);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, EXIT_CODE_ERRNO_3);
ret = setns(fd, setFlag);
ASSERT_EQ(ret, -1);
}

View File

@@ -0,0 +1,64 @@
/*
* 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"
void ItUtsContainer006(void)
{
std::string containerType = "uts";
int parentPid = getpid();
auto parentlink = ReadlinkContainer(parentPid, containerType);
int childsPid = CloneWrapper(ChildFunction, CLONE_NEWUTS, NULL);
ASSERT_NE(childsPid, -1);
auto childlink = ReadlinkContainer(childsPid, containerType);
std::string filePath = GenContainerLinkPath(childsPid, containerType);
int fd = open(filePath.c_str(), O_RDONLY);
ASSERT_NE(fd, -1);
int ret = setns(fd, CLONE_NEWUTS);
ASSERT_NE(ret, -1);
(void)close(fd);
auto parentlink1 = ReadlinkContainer(parentPid, containerType);
ret = parentlink.compare(parentlink1);
ASSERT_NE(ret, 0);
ret = parentlink1.compare(childlink);
ASSERT_EQ(ret, 0);
int status;
ret = waitpid(childsPid, &status, 0);
ASSERT_EQ(ret, childsPid);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
}

View File

@@ -63,13 +63,11 @@ static int LiteIpcTest(void)
/* testing mmap liteipc mem pool with different size and flag */
retptr = mmap(nullptr, 1024 * 4096, PROT_READ, MAP_PRIVATE, fd, 0);
ICUNIT_ASSERT_EQUAL(static_cast<int>(static_cast<intptr_t>(retptr)), -1, retptr);
//retptr = mmap(nullptr, 0, PROT_READ, MAP_PRIVATE, fd, 0);
//ICUNIT_ASSERT_EQUAL((int)(intptr_t)retptr, -1, retptr);
retptr = mmap(nullptr, -1, PROT_READ, MAP_PRIVATE, fd, 0);
ICUNIT_ASSERT_EQUAL(static_cast<int>(static_cast<intptr_t>(retptr)), -1, retptr);
retptr = mmap(nullptr, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
ICUNIT_ASSERT_EQUAL(static_cast<int>(static_cast<intptr_t>(retptr)), -1, retptr);
retptr = mmap(nullptr, 4096, PROT_READ, MAP_SHARED, fd, 0);
retptr = mmap(nullptr, 4096, PROT_READ, MAP_SHARED, fd, 0); // 4096: length of mapped memory
ICUNIT_ASSERT_EQUAL(static_cast<int>(static_cast<intptr_t>(retptr)), -1, retptr);
retptr = mmap(nullptr, 1, PROT_READ, MAP_PRIVATE, fd, 0);
@@ -81,7 +79,7 @@ static int LiteIpcTest(void)
char buf[10] = {0};
ret = read(fd, buf, 10);
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
ret = write(fd, buf, 10);
ret = write(fd, buf, 10); // 10: size of buf
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
/* before set cms, testing ioctl cmd */
@@ -96,7 +94,7 @@ static int LiteIpcTest(void)
sleep(2);
/* after set cms, testing set cms cmd */
ret = ioctl(fd, IPC_SET_CMS, 200);
ret = ioctl(fd, IPC_SET_CMS, 200); // 200: use 200 for set cms cmd testing
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
exit(0);

View File

@@ -60,17 +60,6 @@ static int TestCase()
exit(0);
}
/* sig = SIGTERM;
ret = sigaction(sig, (struct sigaction *)1, &oldAct);
printf("ret == %d\n", ret);
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
ICUNIT_ASSERT_EQUAL(errno, EFAULT, errno);
ret = sigaction(sig, &sigAct, (struct sigaction *)1);
printf("ret === %d\n", ret);
ICUNIT_ASSERT_EQUAL(ret, -1, ret);
ICUNIT_ASSERT_EQUAL(errno, EFAULT, errno); */
ret = waitpid(fpid, &status, 0);
ICUNIT_ASSERT_EQUAL(ret, fpid, ret);
ICUNIT_ASSERT_EQUAL(WEXITSTATUS(status), 0, WEXITSTATUS(status));

View File

@@ -0,0 +1,88 @@
# Copyright (c) 2022-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.
import("//build/lite/config/test.gni")
fuzztest("liteos_a_user_fuzz_test") {
output_extension = "bin"
sources = [
"adjtime_fuzzer.cpp",
"chroot_fuzzer.cpp",
"clone_fuzzer.cpp",
"epoll_create_fuzzer.cpp",
"epoll_ctl_fuzzer.cpp",
"epoll_wait_fuzzer.cpp",
"fesetenv_fuzzer.cpp",
"getrlimit_fuzzer.cpp",
"main.cpp",
"mlock_fuzzer.cpp",
"mlockall_fuzzer.cpp",
"posix_spawn_file_actions_addchdir_np_fuzzer.cpp",
"posix_spawn_file_actions_adddup2_fuzzer.cpp",
"posix_spawn_file_actions_addfchdir_np_fuzzer.cpp",
"posix_spawn_file_actions_addopen_fuzzer.cpp",
"posix_spawn_file_actions_destroy_fuzzer.cpp",
"posix_spawn_file_actions_init_fuzzer.cpp",
"posix_spawn_fuzzer.cpp",
"posix_spawnattr_destroy_fuzzer.cpp",
"posix_spawnattr_getflags_fuzzer.cpp",
"posix_spawnattr_getpgroup_fuzzer.cpp",
"posix_spawnattr_getschedparam_fuzzer.cpp",
"posix_spawnattr_getschedpolicy_fuzzer.cpp",
"posix_spawnattr_getsigdefault_fuzzer.cpp",
"posix_spawnattr_getsigmask_fuzzer.cpp",
"posix_spawnattr_init_fuzzer.cpp",
"posix_spawnattr_setflags_fuzzer.cpp",
"posix_spawnattr_setpgroup_fuzzer.cpp",
"posix_spawnattr_setschedparam_fuzzer.cpp",
"posix_spawnattr_setschedpolicy_fuzzer.cpp",
"posix_spawnattr_setsigdefault_fuzzer.cpp",
"posix_spawnattr_setsigmask_fuzzer.cpp",
"posix_spawnp_fuzzer.cpp",
"pthread_mutex_consistent_fuzzer.cpp",
"pthread_mutex_getprioceiling_fuzzer.cpp",
"pthread_mutexattr_setprotocol_fuzzer.cpp",
"pthread_mutexattr_setrobust_fuzzer.cpp",
"pthread_mutexattr_settype_fuzzer.cpp",
"pthread_setconcurrency_fuzzer.cpp",
"readlink_fuzzer.cpp",
"readlinkat_fuzzer.cpp",
"sem_open_fuzzer.cpp",
"sethostname_fuzzer.cpp",
"setns_fuzzer.cpp",
"syslog_fuzzer.cpp",
"system_fuzzer.cpp",
"times_fuzzer.cpp",
"unshare_fuzzer.cpp",
]
include_dirs = []
deps = []
}
group("liteos_a_fuzztest") {
deps = [ ":liteos_a_user_fuzz_test" ]
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2022-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 <sys/time.h>
#include <secodeFuzz.h>
#include "fuzzertest.h"
void TestAdjtime(void)
{
struct timeval fa;
printf("start----TestAdjtime\n");
DT_Enable_Support_Loop(1);
DT_FUZZ_START(0, FREQUENCY, const_cast<char *>("TestAdjtime"), 0) {
char *delta = DT_SetGetFixBlob(&g_Element[0], sizeof(struct timeval), sizeof(struct timeval), (char *)&fa);
char *olddelta = DT_SetGetFixBlob(&g_Element[1], sizeof(struct timeval), sizeof(struct timeval), (char *)&fa);
adjtime((struct timeval *)delta, (struct timeval *)olddelta);
}
DT_FUZZ_END()
printf("end----TestAdjtime\n");
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright (c) 2022-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 <secodeFuzz.h>
#include <unistd.h>
#include "fuzzertest.h"
void TestChroot(void)
{
printf("start----TestChroot\n");
DT_Enable_Support_Loop(1);
DT_FUZZ_START(0, FREQUENCY, const_cast<char *>("TestChroot"), 0) {
char *buf = DT_SetGetString(&g_Element[0], LENGTH_STRING, MAXLENGTH_STRING, INITVALUE_STRING);
chroot(buf);
}
DT_FUZZ_END()
printf("end----TestChroot\n");
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright (c) 2022-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 <secodeFuzz.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstdio>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sched.h>
#include "fuzzertest.h"
static int childFunc(void *)
{
return 0;
}
void TestClone(void)
{
printf("start----TestClone\n");
DT_Enable_Support_Loop(1);
DT_FUZZ_START(0, FREQUENCY, const_cast<char *>("TestClone"), 0) {
s32 number = *(s32 *)DT_SetGetS32(&g_Element[0], INITVALUE_S32);
pid_t pid = clone(childFunc, NULL, number, NULL);
waitpid(pid, NULL, 0);
}
DT_FUZZ_END()
printf("end----TestClone\n");
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright (c) 2022-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 <secodeFuzz.h>
#include <sys/epoll.h>
#include "fuzzertest.h"
void TestEpollCreate(void)
{
printf("start----TestEpollCreate\n");
DT_Enable_Support_Loop(1);
DT_FUZZ_START(0, FREQUENCY, const_cast<char *>("TestEpollCreate"), 0) {
s32 number = *(s32 *)DT_SetGetS32(&g_Element[0], INITVALUE_S32);
epoll_create(number);
}
DT_FUZZ_END()
printf("end----TestEpollCreate\n");
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright (c) 2022-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 <secodeFuzz.h>
#include <sys/epoll.h>
#include "fuzzertest.h"
void TestEpollCtl(void)
{
struct epoll_event fa;
printf("start----TestEpollCtl\n");
DT_Enable_Support_Loop(1);
DT_FUZZ_START(0, FREQUENCY, const_cast<char *>("TestEpollCtl"), 0) {
s32 number0 = *(s32 *)DT_SetGetS32(&g_Element[0], INITVALUE_S32);
s32 number1 = *(s32 *)DT_SetGetS32(&g_Element[1], INITVALUE_S32);
s32 number2 = *(s32 *)DT_SetGetS32(&g_Element[2], INITVALUE_S32);
char *datainput = DT_SetGetFixBlob(&g_Element[3], sizeof(struct epoll_event), sizeof(struct epoll_event),
(char *)&fa);
epoll_ctl(number0, number1, number2, (struct epoll_event *)datainput);
}
DT_FUZZ_END()
printf("end----TestEpollCtl\n");
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright (c) 2022-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 <secodeFuzz.h>
#include <sys/epoll.h>
#include "fuzzertest.h"
void TestEpollWait(void)
{
struct epoll_event fa;
printf("start----TestEpollWait\n");
DT_Enable_Support_Loop(1);
DT_FUZZ_START(0, FREQUENCY, const_cast<char *>("TestEpollWait"), 0) {
s32 number0 = *(s32 *)DT_SetGetS32(&g_Element[0], INITVALUE_S32);
s32 number1 = *(s32 *)DT_SetGetS32(&g_Element[1], INITVALUE_S32);
s32 number2 = *(s32 *)DT_SetGetS32(&g_Element[2], INITVALUE_S32);
char *datainput = DT_SetGetFixBlob(&g_Element[3], sizeof(struct epoll_event), sizeof(struct epoll_event),
(char *)&fa);
epoll_wait(number0, (struct epoll_event *)datainput, number1, number2);
}
DT_FUZZ_END()
printf("end----TestEpollWait\n");
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2022-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 <secodeFuzz.h>
#include <fenv.h>
#include "fuzzertest.h"
void TestFesetenv(void)
{
fenv_t fa;
printf("start----TestFesetenv\n");
DT_Enable_Support_Loop(1);
DT_FUZZ_START(0, FREQUENCY, const_cast<char *>("TestFesetenv"), 0) {
char *envp = DT_SetGetFixBlob(&g_Element[0], sizeof(fenv_t), sizeof(fenv_t), (char *)&fa);
fesetenv((fenv_t *)envp);
}
DT_FUZZ_END()
printf("end----TestFesetenv\n");
}

View File

@@ -0,0 +1,25 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _FUZZERTEST_H
#define _FUZZERTEST_H
#define FREQUENCY 100000
#define INITVALUE_S32 0x123456
#define INITVALUE_STRING "123456"
#define LENGTH_STRING 6
#define MAXLENGTH_STRING 10000
#endif

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2022-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 <secodeFuzz.h>
#include <sys/resource.h>
#include "fuzzertest.h"
void TestGetrlimit(void)
{
struct rlimit fa;
printf("start----TestGetrlimit\n");
DT_Enable_Support_Loop(1);
DT_FUZZ_START(0, FREQUENCY, const_cast<char *>("TestGetrlimit"), 0) {
s32 resource = *(s32 *)DT_SetGetS32(&g_Element[0], INITVALUE_S32);
char *datainput = DT_SetGetFixBlob(&g_Element[1], sizeof(struct rlimit), sizeof(struct rlimit), (char *)&fa);
getrlimit(resource, (struct rlimit *)datainput);
}
DT_FUZZ_END()
printf("end----TestGetrlimit\n");
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright (c) 2022-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 <secodeFuzz.h>
extern void TestPosixSpawnFileActionsAddchdirNp(void);
extern void TestPosixSpawnFileActionsAdddup2(void);
extern void TestPosixSpawnFileActionsAddfchdirNp(void);
extern void TestPosixSpawnFileActionsAddopen(void);
extern void TestPosixSpawnFileActionsDestroy(void);
extern void TestPosixSpawnFileActionsInit(void);
extern void TestPosixSpawn(void);
extern void TestPosixSpawnattrDestroy(void);
extern void TestPosixSpawnattrGetflags(void);
extern void TestPosixSpawnattrGetpgroup(void);
extern void TestPosixSpawnattrGetschedparam(void);
extern void TestPosixSpawnattrGetschedpolicy(void);
extern void TestPosixSpawnattrGetsigdefault(void);
extern void TestPosixSpawnattrGetsigmask(void);
extern void TestPosixSpawnattrInit(void);
extern void TestPosixSpawnattrSetflags(void);
extern void TestPosixSpawnattrSetpgroup(void);
extern void TestPosixSpawnattrSetschedparam(void);
extern void TestPosixSpawnattrSetschedpolicy(void);
extern void TestPosixSpawnattrSetsigdefault(void);
extern void TestPosixSpawnattrSetsigmask(void);
extern void TestPosixSpawnp(void);
extern void TestAdjtime(void);
extern void TestFesetenv(void);
extern void TestGetrlimit(void);
extern void TestReadlink(void);
extern void TestReadlinkat(void);
extern void TestSyslog(void);
extern void TestSystem(void);
extern void TestTimes(void);
extern void TestClone(void);
extern void TestEpollCreate(void);
extern void TestEpollCtl(void);
extern void TestEpollWait(void);
extern void TestMlock(void);
extern void TestMlockall(void);
extern void TestPthreadMutexConsistent(void);
extern void TestPthreadMutexGetprioceiling(void);
extern void TestPthreadMutexattrSetprotocol(void);
extern void TestPthreadMutexattrSetrobust(void);
extern void TestPthreadMutexattrSettype(void);
extern void TestPthreadSetconcurrency(void);
extern void TestSetns(void);
extern void TestUnshare(void);
extern void TestSemOpen(void);
extern void TestChroot(void);
extern void TestSethostname(void);
int main()
{
DT_Set_Report_Path("/storage/");
TestReadlink();
TestReadlinkat();
TestSethostname();
TestClone();
TestUnshare();
TestChroot();
TestSetns();
return 0;
}

Some files were not shown because too many files have changed in this diff Show More