未验证 提交 dd041bef 编写于 作者: O openharmony_ci 提交者: Gitee

!1106 fix: 修复容器相关问题

Merge pull request !1106 from zhushengle/mem_free
......@@ -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;
}
......
......@@ -135,19 +135,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, "VMHeapRegionNmae: %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;
......@@ -166,11 +165,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);
......
......@@ -129,6 +129,10 @@ UINT32 OsCopyContainers(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent
{
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) {
......@@ -159,47 +163,45 @@ COPY_CONTAINERS:
return CopyContainers(flags, child, parent, processID);
}
#ifndef LOSCFG_PID_CONTAINER
STATIC VOID ContainersFree(LosProcessCB *processCB)
VOID OsContainerFree(LosProcessCB *processCB)
{
UINT32 intSave;
SCHEDULER_LOCK(intSave);
LOS_AtomicDec(&processCB->container->rc);
if (LOS_AtomicRead(&processCB->container->rc) == 0) {
(VOID)LOS_MemFree(m_aucSysMem1, processCB->container);
processCB->container = NULL;
}
SCHEDULER_UNLOCK(intSave);
}
#endif
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->container);
OsUtsContainerDestroy(processCB->container);
#endif
#ifdef LOSCFG_MNT_CONTAINER
OsMntContainersDestroy(processCB->container);
OsMntContainerDestroy(processCB->container);
#endif
#ifdef LOSCFG_IPC_CONTAINER
OsIpcContainersDestroy(processCB->container);
OsIpcContainerDestroy(processCB->container);
#endif
#ifdef LOSCFG_TIME_CONTAINER
OsTimeContainersDestroy(processCB);
OsTimeContainerDestroy(processCB);
#endif
#ifndef LOSCFG_PID_CONTAINER
ContainersFree(processCB);
UINT32 intSave;
SCHEDULER_LOCK(intSave);
OsContainerFree(processCB);
SCHEDULER_UNLOCK(intSave);
#endif
}
......@@ -240,50 +242,43 @@ UINT32 OsGetContainerID(Container *container, ContainerType type)
return OS_INVALID_VALUE;
}
STATIC VOID UnshareDeinitContainerCommon(UINT32 flags, Container *container)
STATIC VOID UnshareDeInitContainer(UINT32 flags, Container *container)
{
UINT32 intSave;
if (container == NULL) {
return;
}
#ifdef LOSCFG_PID_CONTAINER
UnshareDeInitPidContainer(container);
#endif
#ifdef LOSCFG_UTS_CONTAINER
if ((flags & CLONE_NEWUTS) != 0) {
OsUtsContainersDestroy(container);
OsUtsContainerDestroy(container);
}
#endif
#ifdef LOSCFG_MNT_CONTAINER
if ((flags & CLONE_NEWNS) != 0) {
OsMntContainersDestroy(container);
OsMntContainerDestroy(container);
}
#endif
#ifdef LOSCFG_IPC_CONTAINER
if ((flags & CLONE_NEWIPC) != 0) {
OsIpcContainersDestroy(container);
OsIpcContainerDestroy(container);
}
#endif
}
STATIC VOID UnshareDeinitContainer(UINT32 flags, Container *container)
{
if (container == NULL) {
return;
}
#ifdef LOSCFG_PID_CONTAINER
if ((container->pidForChildContainer != NULL) && (container->pidForChildContainer != container->pidContainer)) {
(VOID)LOS_MemFree(m_aucSysMem1, container->pidForChildContainer);
container->pidForChildContainer = NULL;
container->pidContainer = NULL;
}
#endif
UnshareDeinitContainerCommon(flags, container);
#ifdef LOSCFG_TIME_CONTAINER
if ((container->timeForChildContainer != NULL) && (container->timeForChildContainer != container->timeContainer)) {
(VOID)LOS_MemFree(m_aucSysMem1, container->timeForChildContainer);
container->timeForChildContainer = NULL;
container->timeContainer = NULL;
}
UnshareDeInitTimeContainer(container);
#endif
(VOID)LOS_MemFree(m_aucSysMem1, container);
SCHEDULER_LOCK(intSave);
LOS_AtomicDec(&container->rc);
if (LOS_AtomicRead(&container->rc) == 0) {
(VOID)LOS_MemFree(m_aucSysMem1, container);
}
SCHEDULER_UNLOCK(intSave);
}
STATIC UINT32 CreateNewContainers(UINT32 flags, LosProcessCB *curr, Container *newContainer)
......@@ -328,7 +323,9 @@ INT32 OsUnshare(UINT32 flags)
UINT32 intSave;
LosProcessCB *curr = OsCurrProcessGet();
Container *oldContainer = curr->container;
if (!(flags & (CLONE_NEWPID | CLONE_NEWTIME | CLONE_NEWUTS | CLONE_NEWNS | CLONE_NEWIPC))) {
UINT32 unshareFlags = CLONE_NEWPID | CLONE_NEWTIME | CLONE_NEWUTS | CLONE_NEWNS | CLONE_NEWIPC;
if (!(flags & unshareFlags) || ((flags & (~unshareFlags)) != 0)) {
return -EINVAL;
}
......@@ -347,18 +344,11 @@ INT32 OsUnshare(UINT32 flags)
curr->container = newContainer;
SCHEDULER_UNLOCK(intSave);
UnshareDeinitContainerCommon(flags, oldContainer);
SCHEDULER_LOCK(intSave);
LOS_AtomicDec(&oldContainer->rc);
if (LOS_AtomicRead(&oldContainer->rc) == 0) {
(VOID)LOS_MemFree(m_aucSysMem1, oldContainer);
}
SCHEDULER_UNLOCK(intSave);
UnshareDeInitContainer(flags, oldContainer);
return LOS_OK;
EXIT:
UnshareDeinitContainer(flags, newContainer);
UnshareDeInitContainer(flags, newContainer);
return -ret;
}
#endif
......@@ -148,7 +148,7 @@ UINT32 OsUnshareIpcContainer(UINTPTR flags, LosProcessCB *curr, Container *newCo
return LOS_OK;
}
VOID OsIpcContainersDestroy(Container *container)
VOID OsIpcContainerDestroy(Container *container)
{
UINT32 intSave;
if (container == NULL) {
......@@ -157,19 +157,23 @@ VOID OsIpcContainersDestroy(Container *container)
SCHEDULER_LOCK(intSave);
IpcContainer *ipcContainer = container->ipcContainer;
if (ipcContainer != NULL) {
LOS_AtomicDec(&ipcContainer->rc);
if (LOS_AtomicRead(&ipcContainer->rc) <= 0) {
g_currentIpcContainerNum--;
SCHEDULER_UNLOCK(intSave);
ShmDeinit();
container->ipcContainer = NULL;
(VOID)LOS_MemFree(m_aucSysMem1, ipcContainer->allQueue);
(VOID)LOS_MemFree(m_aucSysMem1, ipcContainer);
return;
}
if (ipcContainer == NULL) {
SCHEDULER_UNLOCK(intSave);
return;
}
LOS_AtomicDec(&ipcContainer->rc);
if (LOS_AtomicRead(&ipcContainer->rc) > 0) {
SCHEDULER_UNLOCK(intSave);
return;
}
g_currentIpcContainerNum--;
SCHEDULER_UNLOCK(intSave);
ShmDeinit();
container->ipcContainer = NULL;
(VOID)LOS_MemFree(m_aucSysMem1, ipcContainer->allQueue);
(VOID)LOS_MemFree(m_aucSysMem1, ipcContainer);
return;
}
......
......@@ -187,7 +187,7 @@ STATIC VOID FreeMountList(LIST_HEAD *mountList)
return;
}
VOID OsMntContainersDestroy(Container *container)
VOID OsMntContainerDestroy(Container *container)
{
UINT32 intSave;
if (container == NULL) {
......@@ -196,18 +196,22 @@ VOID OsMntContainersDestroy(Container *container)
SCHEDULER_LOCK(intSave);
MntContainer *mntContainer = container->mntContainer;
if (mntContainer != NULL) {
LOS_AtomicDec(&mntContainer->rc);
if (LOS_AtomicRead(&mntContainer->rc) <= 0) {
g_currentMntContainerNum--;
SCHEDULER_UNLOCK(intSave);
FreeMountList(&mntContainer->mountList);
container->mntContainer = NULL;
(VOID)LOS_MemFree(m_aucSysMem1, mntContainer);
return;
}
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;
}
......
......@@ -195,7 +195,7 @@ UINT32 OsAllocVtid(LosTaskCB *taskCB, const LosProcessCB *processCB)
return taskCB->taskID;
}
VOID OsPidContainersDestroyAllProcess(LosProcessCB *curr)
VOID OsPidContainerDestroyAllProcess(LosProcessCB *curr)
{
INT32 ret;
UINT32 intSave;
......@@ -264,35 +264,66 @@ 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(LosProcessCB *curr)
{
if (curr->container == NULL) {
return;
}
PidContainer *pidContainer = curr->container->pidContainer;
if (pidContainer == NULL) {
return;
}
FreeVpid(curr);
if (pidContainer != curr->container->pidForChildContainer) {
LOS_AtomicDec(&curr->container->pidForChildContainer->rc);
if (LOS_AtomicRead(&curr->container->pidForChildContainer->rc) <= 0) {
g_currentPidContainerNum--;
(VOID)LOS_MemFree(m_aucSysMem1, curr->container->pidForChildContainer);
curr->container->pidForChildContainer = NULL;
}
}
if (LOS_AtomicRead(&pidContainer->rc) <= 0) {
g_currentPidContainerNum--;
curr->container->pidContainer = NULL;
curr->container->pidForChildContainer = NULL;
(VOID)LOS_MemFree(m_aucSysMem1, pidContainer);
}
OsContainerFree(curr);
}
STATIC UINT32 CreatePidContainer(LosProcessCB *child, LosProcessCB *parent)
{
UINT32 intSave;
UINT32 ret;
PidContainer *newPidContainer = NULL;
UINT32 intSave;
PidContainer *parentContainer = parent->container->pidContainer;
if (parentContainer == parent->container->pidForChildContainer) {
newPidContainer = CreateNewPidContainer(parentContainer);
if (newPidContainer == NULL) {
return ENOMEM;
}
} else {
newPidContainer = parent->container->pidForChildContainer;
PidContainer *newPidContainer = CreateNewPidContainer(parentContainer);
if (newPidContainer == NULL) {
return ENOMEM;
}
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;
child->container->pidForChildContainer = newPidContainer;
ret = OsAllocSpecifiedVpidUnsafe(OS_USER_ROOT_PROCESS_ID, child, parent);
......@@ -300,6 +331,7 @@ STATIC UINT32 CreatePidContainer(LosProcessCB *child, LosProcessCB *parent)
g_currentPidContainerNum--;
FreeVpid(child);
child->container->pidContainer = NULL;
child->container->pidForChildContainer = NULL;
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, newPidContainer);
return ENOSPC;
......@@ -308,32 +340,21 @@ STATIC UINT32 CreatePidContainer(LosProcessCB *child, LosProcessCB *parent)
return LOS_OK;
}
VOID OsPidContainersDestroy(LosProcessCB *curr)
STATIC UINT32 UnshareCreatePidContainer(LosProcessCB *child, LosProcessCB *parent)
{
if (curr->container == NULL) {
return;
}
PidContainer *pidContainer = curr->container->pidContainer;
if (pidContainer != NULL) {
FreeVpid(curr);
if (LOS_AtomicRead(&pidContainer->rc) <= 0) {
g_currentPidContainerNum--;
if ((pidContainer != curr->container->pidForChildContainer) &&
(LOS_AtomicRead(&curr->container->pidForChildContainer->rc) <= 0)) {
(VOID)LOS_MemFree(m_aucSysMem1, curr->container->pidForChildContainer);
}
curr->container->pidContainer = NULL;
curr->container->pidForChildContainer = NULL;
(VOID)LOS_MemFree(m_aucSysMem1, pidContainer);
}
}
UINT32 ret;
LOS_AtomicDec(&curr->container->rc);
if (LOS_AtomicRead(&curr->container->rc) == 0) {
(VOID)LOS_MemFree(m_aucSysMem1, curr->container);
curr->container = NULL;
parent->container->pidForChildContainer->referenced = TRUE;
child->container->pidContainer = parent->container->pidForChildContainer;
child->container->pidForChildContainer = parent->container->pidForChildContainer;
ret = OsAllocSpecifiedVpidUnsafe(OS_USER_ROOT_PROCESS_ID, child, parent);
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)
......@@ -341,23 +362,36 @@ UINT32 OsCopyPidContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *pare
UINT32 ret;
UINT32 intSave;
if (!(flags & CLONE_NEWPID) && (parent->container->pidContainer == parent->container->pidForChildContainer)) {
SCHEDULER_LOCK(intSave);
child->container->pidContainer = parent->container->pidContainer;
child->container->pidForChildContainer = 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) || (parentPidContainerForChild->referenced == TRUE)) {
/* The current process is not executing unshare */
if (!(flags & CLONE_NEWPID)) {
child->container->pidContainer = parentPidContainer;
child->container->pidForChildContainer = parentPidContainer;
ret = OsAllocVpid(child);
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 = UnshareCreatePidContainer(child, parent);
SCHEDULER_UNLOCK(intSave);
if (ret != LOS_OK) {
return ret;
}
}
PidContainer *pidContainer = child->container->pidContainer;
......@@ -369,13 +403,38 @@ UINT32 OsCopyPidContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *pare
return LOS_OK;
}
VOID UnshareDeInitPidContainer(Container *container)
{
UINT32 intSave;
PidContainer *pidForChildContainer = NULL;
if (container == NULL) {
return;
}
SCHEDULER_LOCK(intSave);
if ((container->pidForChildContainer != NULL) && (container->pidContainer != container->pidForChildContainer)) {
LOS_AtomicDec(&container->pidForChildContainer->rc);
if (LOS_AtomicRead(&container->pidForChildContainer->rc) <= 0) {
g_currentPidContainerNum--;
pidForChildContainer = container->pidForChildContainer;
container->pidForChildContainer = NULL;
container->pidContainer = NULL;
}
}
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, pidForChildContainer);
}
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->pidContainer;
newContainer->pidForChildContainer = curr->container->pidForChildContainer;
if (newContainer->pidContainer != newContainer->pidForChildContainer) {
LOS_AtomicInc(&newContainer->pidForChildContainer->rc);
}
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
......@@ -398,8 +457,10 @@ UINT32 OsUnsharePidContainer(UINTPTR flags, LosProcessCB *curr, Container *newCo
return EINVAL;
}
g_currentPidContainerNum++;
newContainer->pidContainer = curr->container->pidContainer;
newContainer->pidForChildContainer = pidForChild;
LOS_AtomicSet(&pidForChild->rc, 1);
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
......
......@@ -56,30 +56,11 @@ STATIC TimeContainer *CreateNewTimeContainer(TimeContainer *parent)
STATIC UINT32 CreateTimeContainer(LosProcessCB *child, LosProcessCB *parent)
{
UINT32 intSave;
TimeContainer *parentContainer = parent->container->timeContainer;
if (parentContainer == parent->container->timeForChildContainer) {
TimeContainer *newTimeContainer = CreateNewTimeContainer(parentContainer);
if (newTimeContainer == NULL) {
return ENOMEM;
}
SCHEDULER_LOCK(intSave);
g_currentTimeContainerNum++;
(VOID)memcpy_s(&newTimeContainer->monotonic, sizeof(struct timespec64),
&parentContainer->monotonic, sizeof(struct timespec64));
child->container->timeContainer = newTimeContainer;
child->container->timeForChildContainer = newTimeContainer;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
SCHEDULER_LOCK(intSave);
TimeContainer *newTimeContainer = parent->container->timeForChildContainer;
g_currentTimeContainerNum++;
LOS_AtomicSet(&newTimeContainer->rc, 1);
if (!newTimeContainer->frozenOffsets) {
newTimeContainer->frozenOffsets = TRUE;
}
LOS_AtomicInc(&newTimeContainer->rc);
newTimeContainer->frozenOffsets = TRUE;
child->container->timeContainer = newTimeContainer;
child->container->timeForChildContainer = newTimeContainer;
SCHEDULER_UNLOCK(intSave);
......@@ -106,7 +87,7 @@ UINT32 OsCopyTimeContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *par
UINT32 intSave;
TimeContainer *currTimeContainer = parent->container->timeContainer;
if (!(flags & CLONE_NEWTIME) && (currTimeContainer == parent->container->timeForChildContainer)) {
if (currTimeContainer == parent->container->timeForChildContainer) {
SCHEDULER_LOCK(intSave);
LOS_AtomicInc(&currTimeContainer->rc);
child->container->timeContainer = currTimeContainer;
......@@ -125,6 +106,9 @@ UINT32 OsUnshareTimeContainer(UINTPTR flags, LosProcessCB *curr, Container *newC
SCHEDULER_LOCK(intSave);
newContainer->timeContainer = curr->container->timeContainer;
newContainer->timeForChildContainer = curr->container->timeForChildContainer;
if (newContainer->timeContainer != newContainer->timeForChildContainer) {
LOS_AtomicInc(&newContainer->timeForChildContainer->rc);
}
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
......@@ -141,38 +125,72 @@ UINT32 OsUnshareTimeContainer(UINTPTR flags, LosProcessCB *curr, Container *newC
return EINVAL;
}
(VOID)memcpy_s(&timeForChild->monotonic, sizeof(struct timespec64),
&curr->container->timeContainer->monotonic, sizeof(struct timespec64));
newContainer->timeContainer = curr->container->timeContainer;
newContainer->timeForChildContainer = timeForChild;
LOS_AtomicSet(&timeForChild->rc, 0);
g_currentTimeContainerNum++;
SCHEDULER_UNLOCK(intSave);
return LOS_OK;
}
VOID OsTimeContainersDestroy(LosProcessCB *curr)
VOID UnshareDeInitTimeContainer(Container *container)
{
UINT32 intSave;
TimeContainer *timeForChildContainer = NULL;
if (container == NULL) {
return;
}
SCHEDULER_LOCK(intSave);
if ((container->timeForChildContainer != NULL) && (container->timeForChildContainer != container->timeContainer)) {
LOS_AtomicDec(&container->timeForChildContainer->rc);
if (LOS_AtomicRead(&container->timeForChildContainer->rc) <= 0) {
g_currentTimeContainerNum--;
timeForChildContainer = container->timeForChildContainer;
container->timeForChildContainer = NULL;
container->timeContainer = NULL;
}
}
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, timeForChildContainer);
}
VOID OsTimeContainerDestroy(LosProcessCB *curr)
{
UINT32 intSave;
TimeContainer *timeContainer = NULL;
TimeContainer *timeForChild = NULL;
if (curr->container == NULL) {
return;
}
SCHEDULER_LOCK(intSave);
TimeContainer *timeContainer = curr->container->timeContainer;
if (timeContainer != NULL) {
LOS_AtomicDec(&timeContainer->rc);
if (LOS_AtomicRead(&timeContainer->rc) <= 0) {
if (curr->container->timeContainer == NULL) {
SCHEDULER_UNLOCK(intSave);
return;
}
if (curr->container->timeContainer != curr->container->timeForChildContainer) {
LOS_AtomicDec(&curr->container->timeForChildContainer->rc);
if (LOS_AtomicRead(&curr->container->timeForChildContainer->rc) <= 0) {
g_currentTimeContainerNum--;
curr->container->timeContainer = NULL;
SCHEDULER_UNLOCK(intSave);
if ((timeContainer != curr->container->timeForChildContainer) &&
(LOS_AtomicRead(&curr->container->timeForChildContainer->rc) <= 0)) {
(VOID)LOS_MemFree(m_aucSysMem1, curr->container->timeForChildContainer);
}
timeForChild = curr->container->timeForChildContainer;
curr->container->timeForChildContainer = NULL;
(VOID)LOS_MemFree(m_aucSysMem1, timeContainer);
return;
}
}
LOS_AtomicDec(&curr->container->timeContainer->rc);
if (LOS_AtomicRead(&curr->container->timeContainer->rc) <= 0) {
g_currentTimeContainerNum--;
timeContainer = curr->container->timeContainer;
curr->container->timeContainer = NULL;
curr->container->timeForChildContainer = NULL;
}
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, timeForChild);
(VOID)LOS_MemFree(m_aucSysMem1, timeContainer);
return;
}
......
......@@ -164,7 +164,7 @@ UINT32 OsUnshareUtsContainer(UINTPTR flags, LosProcessCB *curr, Container *newCo
return LOS_OK;
}
VOID OsUtsContainersDestroy(Container *container)
VOID OsUtsContainerDestroy(Container *container)
{
UINT32 intSave;
if (container == NULL) {
......@@ -173,17 +173,20 @@ VOID OsUtsContainersDestroy(Container *container)
SCHEDULER_LOCK(intSave);
UtsContainer *utsContainer = container->utsContainer;
if (utsContainer != NULL) {
LOS_AtomicDec(&utsContainer->rc);
if (LOS_AtomicRead(&utsContainer->rc) <= 0) {
g_currentUtsContainerNum--;
container->utsContainer = NULL;
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, utsContainer);
return;
}
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;
}
......
......@@ -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);
#endif
UINT32 pid = processCB->processID;
(VOID)memset_s(processCB, sizeof(LosProcessCB), 0, sizeof(LosProcessCB));
......
......@@ -89,6 +89,8 @@ 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);
......
......@@ -64,7 +64,7 @@ UINT32 OsCopyIpcContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *pare
UINT32 OsUnshareIpcContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
VOID OsIpcContainersDestroy(struct Container *container);
VOID OsIpcContainerDestroy(struct Container *container);
UINT32 OsGetIpcContainerID(IpcContainer *ipcContainer);
......
......@@ -55,7 +55,7 @@ UINT32 OsCopyMntContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *pare
UINT32 OsUnshareMntContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
VOID OsMntContainersDestroy(struct Container *container);
VOID OsMntContainerDestroy(struct Container *container);
UINT32 OsGetMntContainerID(MntContainer *mntContainer);
......
......@@ -52,6 +52,7 @@ typedef struct PidContainer {
Atomic rc;
Atomic level;
Atomic lock;
BOOL referenced;
UINT32 containerID;
struct PidContainer *parent;
struct ProcessGroup *rootPGroup;
......@@ -70,14 +71,16 @@ typedef struct PidContainer {
UINT32 OsAllocSpecifiedVpidUnsafe(UINT32 vpid, LosProcessCB *processCB, LosProcessCB *parent);
VOID OsPidContainersDestroyAllProcess(LosProcessCB *processCB);
VOID OsPidContainerDestroyAllProcess(LosProcessCB *processCB);
VOID OsPidContainersDestroy(LosProcessCB *curr);
VOID OsPidContainerDestroy(LosProcessCB *curr);
UINT32 OsCopyPidContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID);
UINT32 OsUnsharePidContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
VOID UnshareDeInitPidContainer(struct Container *container);
UINT32 OsInitRootPidContainer(PidContainer **pidContainer);
LosProcessCB *OsGetPCBFromVpid(UINT32 vpid);
......
......@@ -50,7 +50,9 @@ UINT32 OsCopyTimeContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *par
UINT32 OsUnshareTimeContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
VOID OsTimeContainersDestroy(LosProcessCB *curr);
VOID UnshareDeInitTimeContainer(struct Container *container);
VOID OsTimeContainerDestroy(LosProcessCB *curr);
UINT32 OsGetTimeContainerID(TimeContainer *timeContainer);
......
......@@ -52,7 +52,7 @@ UINT32 OsCopyUtsContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *pare
UINT32 OsUnshareUtsContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
VOID OsUtsContainersDestroy(struct Container *container);
VOID OsUtsContainerDestroy(struct Container *container);
struct utsname *OsGetCurrUtsName(VOID);
......
......@@ -162,6 +162,30 @@ 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();
}
#endif
#if defined(LOSCFG_USER_TEST_UTS_CONTAINER)
/**
......@@ -293,12 +317,22 @@ HWTEST_F(ContainerTest, ItMntContainer007, TestSize.Level0)
* @tc.require: issueI6APW2
* @tc.author:
*/
HWTEST_F(ContainerTest, ItContainerChroot001, TestSize.Level0)
{
ItContainerChroot001();
}
/**
* @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)
......@@ -500,6 +534,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
......
......@@ -138,12 +138,15 @@ private:
#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);
#endif
#if defined(LOSCFG_USER_TEST_UTS_CONTAINER)
void ItUtsContainer001(void);
......@@ -188,6 +191,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);
......
......@@ -39,6 +39,7 @@ 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",
"$TEST_UNITTEST_DIR/container/smoke/It_container_chroot_002.cpp",
]
sources_full = []
......@@ -50,12 +51,15 @@ if (defined(LOSCFG_USER_TEST_PID_CONTAINER)) {
"$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",
]
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",
......
......@@ -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);
if (ret != childPid) {
return EXIT_CODE_ERRNO_5;
}
ret = WIFEXITED(status);
if (ret == 0) {
return EXIT_CODE_ERRNO_6;
}
ret = WEXITSTATUS(status);
if (ret != CONTAINER_THIRD_PID) {
free(pstk);
return EXIT_CODE_ERRNO_5;
return EXIT_CODE_ERRNO_7;
}
free(pstk);
return childFunRet;
}
......
......@@ -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;
......
......@@ -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);
......
/*
* 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);
}
/*
* 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);
}
......@@ -102,7 +102,7 @@ static int ChildFunClone1(void *p)
if (pstk == NULL) {
return EXIT_CODE_ERRNO_4;
}
int childPid = clone(ChildFunClone2, (char *)pstk + STACK_SIZE, CLONE_NEWUTS | SIGCHLD, NULL);
int childPid = clone(ChildFunClone2, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
free(pstk);
if (childPid == -1) {
return EXIT_CODE_ERRNO_5;
......@@ -132,7 +132,6 @@ void ItPidContainer027(void)
{
void *pstk = malloc(STACK_SIZE);
ASSERT_TRUE(pstk != NULL);
pid_t parentPid = getpid();
int childPid = clone(ChildFunClone1, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
free(pstk);
......
......@@ -130,7 +130,6 @@ void ItPidContainer028(void)
{
void *pstk = malloc(STACK_SIZE);
ASSERT_TRUE(pstk != NULL);
pid_t parentPid = getpid();
int childPid = clone(ChildFunClone1, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
free(pstk);
......
/*
* 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, CLONE_NEWPID | 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(childPid, &status, 0);
if (ret != childPid) {
return EXIT_CODE_ERRNO_8;
}
ret = WIFEXITED(status);
if (ret == 0) {
return EXIT_CODE_ERRNO_9;
}
ret = WEXITSTATUS(status);
if (ret != 0) {
return EXIT_CODE_ERRNO_10;
}
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);
}
/*
* 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;
}
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(childPid, &status, 0);
if (ret != childPid) {
return EXIT_CODE_ERRNO_8;
}
ret = WIFEXITED(status);
if (ret == 0) {
return EXIT_CODE_ERRNO_9;
}
ret = WEXITSTATUS(status);
if (ret != 0) {
return EXIT_CODE_ERRNO_10;
}
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 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);
}
......@@ -61,7 +61,7 @@ void ItTimeContainer001(void)
auto linkBuffer2 = ReadlinkContainer(pid, containerType);
ret = linkBuffer.compare(linkBuffer2);
ASSERT_NE(ret, 0);
ASSERT_EQ(ret, 0);
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册