diff --git a/fs/proc/os_adapt/mem_info.c b/fs/proc/os_adapt/mem_info.c index 076c308d74cf79504f0f904b57f490b9bc86eb7d..c67363aad02bf2660586034c31c7a280c7d711c6 100644 --- a/fs/proc/os_adapt/mem_info.c +++ b/fs/proc/os_adapt/mem_info.c @@ -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; } diff --git a/fs/proc/os_adapt/process_proc.c b/fs/proc/os_adapt/process_proc.c index f85f0c046c76a6039845c8443edb916dd6d63440..f32a58cf56f027d08e7b68b739bc03eabf7d0cd7 100644 --- a/fs/proc/os_adapt/process_proc.c +++ b/fs/proc/os_adapt/process_proc.c @@ -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); diff --git a/kernel/base/container/los_container.c b/kernel/base/container/los_container.c index 2c89e5953b211a14ddb131aa5f242d907eb8138f..d0ef1f1fd1528be28402b54250b7f58d8835294b 100644 --- a/kernel/base/container/los_container.c +++ b/kernel/base/container/los_container.c @@ -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 diff --git a/kernel/base/container/los_ipc_container.c b/kernel/base/container/los_ipc_container.c index 030112e43f7aaca116c6812a173a08887bb7f433..9acc08f8992389b6233618a8c320ddfdd631c929 100644 --- a/kernel/base/container/los_ipc_container.c +++ b/kernel/base/container/los_ipc_container.c @@ -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; } diff --git a/kernel/base/container/los_mnt_container.c b/kernel/base/container/los_mnt_container.c index 154a2d051c7f7019550f0e6a3958f8debca692bb..6b09e0832bc62d2b5fdc00fc313aea410e5700b9 100644 --- a/kernel/base/container/los_mnt_container.c +++ b/kernel/base/container/los_mnt_container.c @@ -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; } diff --git a/kernel/base/container/los_pid_container.c b/kernel/base/container/los_pid_container.c index 4af02dd6a2158e358f2277beab87e6de4f5dac1b..9b33500502d63b10e1dcf9a9cfea20f62474afc8 100644 --- a/kernel/base/container/los_pid_container.c +++ b/kernel/base/container/los_pid_container.c @@ -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; } diff --git a/kernel/base/container/los_time_container.c b/kernel/base/container/los_time_container.c index 781b9ffa0b606b8d5b80c91e385fea56c7ff33c8..cbbeccb17df344ea369077ae1ee9b69e96c9684b 100644 --- a/kernel/base/container/los_time_container.c +++ b/kernel/base/container/los_time_container.c @@ -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; } diff --git a/kernel/base/container/los_uts_container.c b/kernel/base/container/los_uts_container.c index 7bc91d2b215f51a302c11cedd3d9b25ae6fcc0c7..a6c69ef934973e20818cf5e30eda459c261a72fa 100644 --- a/kernel/base/container/los_uts_container.c +++ b/kernel/base/container/los_uts_container.c @@ -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; } diff --git a/kernel/base/core/los_process.c b/kernel/base/core/los_process.c index a2c35f09d73f1da770510e8d4d5c3da90bff93ed..e425a969a590c1d8773ec41b8e6b90ed95f8dc8a 100644 --- a/kernel/base/core/los_process.c +++ b/kernel/base/core/los_process.c @@ -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)); diff --git a/kernel/base/include/los_container_pri.h b/kernel/base/include/los_container_pri.h index a878323a3531d1c4c3e2e254c23fb71d46cee351..3870bcefcb823e2dbcc0052a1ec390e019a438a1 100644 --- a/kernel/base/include/los_container_pri.h +++ b/kernel/base/include/los_container_pri.h @@ -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); diff --git a/kernel/base/include/los_ipc_container_pri.h b/kernel/base/include/los_ipc_container_pri.h index 9439892558cc1769c8ef75d9d13781ccd0c37c75..df33098fe362abe1e5ccd483320d207378b47327 100644 --- a/kernel/base/include/los_ipc_container_pri.h +++ b/kernel/base/include/los_ipc_container_pri.h @@ -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); diff --git a/kernel/base/include/los_mnt_container_pri.h b/kernel/base/include/los_mnt_container_pri.h index af20c4a4e32bea062087498ac451e4e399342e73..b226daee89c96c28120f5041c392ffb2e87fd182 100644 --- a/kernel/base/include/los_mnt_container_pri.h +++ b/kernel/base/include/los_mnt_container_pri.h @@ -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); diff --git a/kernel/base/include/los_pid_container_pri.h b/kernel/base/include/los_pid_container_pri.h index 6d6ad3c3982395b14e6cf280c34713e2b9f725fc..e8c7301e5a9321b4879403c64183e4895a69198b 100644 --- a/kernel/base/include/los_pid_container_pri.h +++ b/kernel/base/include/los_pid_container_pri.h @@ -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); diff --git a/kernel/base/include/los_time_container_pri.h b/kernel/base/include/los_time_container_pri.h index 617c0fa1425049f55434eb9ade81dea04ca02f87..8b233eb59db9726396e0f31863d836b936b58e26 100644 --- a/kernel/base/include/los_time_container_pri.h +++ b/kernel/base/include/los_time_container_pri.h @@ -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); diff --git a/kernel/base/include/los_uts_container_pri.h b/kernel/base/include/los_uts_container_pri.h index c15e40799f3fcc269afc69293dae36656ee36975..1ef4870aa77e502fcd8945291918771f79325912 100644 --- a/kernel/base/include/los_uts_container_pri.h +++ b/kernel/base/include/los_uts_container_pri.h @@ -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); diff --git a/testsuites/unittest/container/It_container_test.cpp b/testsuites/unittest/container/It_container_test.cpp index 17e0fb12841eefade13b84c9e073dead50157b6b..bc81fe8aff236c82cb269b7f50a603a8d65e167d 100644 --- a/testsuites/unittest/container/It_container_test.cpp +++ b/testsuites/unittest/container/It_container_test.cpp @@ -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 diff --git a/testsuites/unittest/container/It_container_test.h b/testsuites/unittest/container/It_container_test.h index 04753a5c48d7ca6b470f8fb219d9825371bb035f..33cd7fc912e34d5e4992957463320dcb78c060d8 100644 --- a/testsuites/unittest/container/It_container_test.h +++ b/testsuites/unittest/container/It_container_test.h @@ -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); diff --git a/testsuites/unittest/container/config.gni b/testsuites/unittest/container/config.gni index 90c105e8b4ac5fd287b42862c6070d9b472f1223..dcc08186d898e5b4bdd9d99b08755d5e679ddb65 100644 --- a/testsuites/unittest/container/config.gni +++ b/testsuites/unittest/container/config.gni @@ -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", diff --git a/testsuites/unittest/container/full/It_pid_container_002.cpp b/testsuites/unittest/container/full/It_pid_container_002.cpp index 8a8393b5d9ce7c3b753a5159271fcc9869ba610a..a667139ba2224516305183f064daedd810bd9b2d 100644 --- a/testsuites/unittest/container/full/It_pid_container_002.cpp +++ b/testsuites/unittest/container/full/It_pid_container_002.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; } diff --git a/testsuites/unittest/container/full/It_pid_container_003.cpp b/testsuites/unittest/container/full/It_pid_container_003.cpp index 811b3f2b42e0109cdb89c1a880e9ecb6793654d1..ee181dff160b248e5ae16c8b5026dd70424b299e 100644 --- a/testsuites/unittest/container/full/It_pid_container_003.cpp +++ b/testsuites/unittest/container/full/It_pid_container_003.cpp @@ -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; diff --git a/testsuites/unittest/container/full/It_pid_container_004.cpp b/testsuites/unittest/container/full/It_pid_container_004.cpp index dd3af06931d14e1c2439471e4ca01fc66997cc14..290e3190efcbbfaad8452bb820bd304509ebf216 100644 --- a/testsuites/unittest/container/full/It_pid_container_004.cpp +++ b/testsuites/unittest/container/full/It_pid_container_004.cpp @@ -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); diff --git a/testsuites/unittest/container/full/It_pid_container_005.cpp b/testsuites/unittest/container/full/It_pid_container_005.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fa3b6f51c62e9fbb62fcd3ed294c03565bd4974 --- /dev/null +++ b/testsuites/unittest/container/full/It_pid_container_005.cpp @@ -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); +} diff --git a/testsuites/unittest/container/smoke/It_container_chroot_002.cpp b/testsuites/unittest/container/smoke/It_container_chroot_002.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0bbfcf6e97c47c11c71357697ba7a186d5b0171c --- /dev/null +++ b/testsuites/unittest/container/smoke/It_container_chroot_002.cpp @@ -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); +} diff --git a/testsuites/unittest/container/smoke/It_pid_container_027.cpp b/testsuites/unittest/container/smoke/It_pid_container_027.cpp index 5d54173f787bcb2a317c29148dbb5ad03e4ecb4f..85e654e3a41d5188cba0c4a9885b25c17c2833e3 100644 --- a/testsuites/unittest/container/smoke/It_pid_container_027.cpp +++ b/testsuites/unittest/container/smoke/It_pid_container_027.cpp @@ -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); diff --git a/testsuites/unittest/container/smoke/It_pid_container_028.cpp b/testsuites/unittest/container/smoke/It_pid_container_028.cpp index d846e17c49d2df9802e5c84c4c25a9aaaeafda35..878207f60c8be218e02ab39dee9335284ce1c5ee 100644 --- a/testsuites/unittest/container/smoke/It_pid_container_028.cpp +++ b/testsuites/unittest/container/smoke/It_pid_container_028.cpp @@ -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); diff --git a/testsuites/unittest/container/smoke/It_pid_container_029.cpp b/testsuites/unittest/container/smoke/It_pid_container_029.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a61d57c7835d4a777b79e2710c687db8d424ca32 --- /dev/null +++ b/testsuites/unittest/container/smoke/It_pid_container_029.cpp @@ -0,0 +1,141 @@ +/* + * 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); +} diff --git a/testsuites/unittest/container/smoke/It_pid_container_030.cpp b/testsuites/unittest/container/smoke/It_pid_container_030.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eb8294607d0bdad7403bd5cbf94ad891413a1d9 --- /dev/null +++ b/testsuites/unittest/container/smoke/It_pid_container_030.cpp @@ -0,0 +1,141 @@ +/* + * 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); +} diff --git a/testsuites/unittest/container/smoke/It_time_container_001.cpp b/testsuites/unittest/container/smoke/It_time_container_001.cpp index 93cdca7fc5c094d34dc240f3d5c1cca7b38ad56d..0fa88dc96d463f012cab0e48b979a6c01a4db48b 100644 --- a/testsuites/unittest/container/smoke/It_time_container_001.cpp +++ b/testsuites/unittest/container/smoke/It_time_container_001.cpp @@ -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);