Compare commits
8 Commits
weekly_202
...
weekly_202
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f3a7a9c602 | ||
|
|
c8dbdaeb12 | ||
|
|
1f05c6a24d | ||
|
|
325b4a49e4 | ||
|
|
1d1f1b06c3 | ||
|
|
16ed05e844 | ||
|
|
7e0dfb79f7 | ||
|
|
34814c58a3 |
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
@@ -74,6 +74,10 @@ extern "C" {
|
||||
/* not support prio */
|
||||
#define MQ_PRIO_MAX 1
|
||||
|
||||
#ifndef MAX_MQ_FD
|
||||
#define MAX_MQ_FD CONFIG_NQUEUE_DESCRIPTORS
|
||||
#endif
|
||||
|
||||
typedef union send_receive_t {
|
||||
unsigned oth : 3;
|
||||
unsigned grp : 6;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
@@ -44,16 +44,19 @@
|
||||
|
||||
#define FNONBLOCK O_NONBLOCK
|
||||
|
||||
#ifndef MAX_MQ_FD
|
||||
#define MAX_MQ_FD CONFIG_NQUEUE_DESCRIPTORS
|
||||
#endif
|
||||
|
||||
#ifndef LOSCFG_IPC_CONTAINER
|
||||
/* GLOBALS */
|
||||
STATIC fd_set g_queueFdSet;
|
||||
STATIC struct mqarray g_queueTable[LOSCFG_BASE_IPC_QUEUE_LIMIT];
|
||||
STATIC pthread_mutex_t g_mqueueMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
|
||||
STATIC struct mqpersonal *g_mqPrivBuf[MAX_MQ_FD];
|
||||
|
||||
#define IPC_QUEUE_FD_SET g_queueFdSet
|
||||
#define IPC_QUEUE_TABLE g_queueTable
|
||||
#define IPC_QUEUE_MUTEX g_mqueueMutex
|
||||
#define IPC_QUEUE_MQ_PRIV_BUF g_mqPrivBuf
|
||||
#endif
|
||||
|
||||
/* LOCAL FUNCTIONS */
|
||||
STATIC INLINE INT32 MqNameCheck(const CHAR *mqName)
|
||||
{
|
||||
@@ -96,12 +99,12 @@ STATIC INLINE struct mqarray *GetMqueueCBByName(const CHAR *name)
|
||||
UINT32 mylen = strlen(name);
|
||||
|
||||
for (index = 0; index < LOSCFG_BASE_IPC_QUEUE_LIMIT; index++) {
|
||||
if ((g_queueTable[index].mq_name == NULL) || (strlen(g_queueTable[index].mq_name) != mylen)) {
|
||||
if ((IPC_QUEUE_TABLE[index].mq_name == NULL) || (strlen(IPC_QUEUE_TABLE[index].mq_name) != mylen)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strncmp(name, (const CHAR *)(g_queueTable[index].mq_name), mylen) == 0) {
|
||||
return &(g_queueTable[index]);
|
||||
if (strncmp(name, (const CHAR *)(IPC_QUEUE_TABLE[index].mq_name), mylen) == 0) {
|
||||
return &(IPC_QUEUE_TABLE[index]);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
@@ -170,8 +173,8 @@ STATIC struct mqpersonal *DoMqueueCreate(const struct mq_attr *attr, const CHAR
|
||||
goto ERROUT;
|
||||
}
|
||||
|
||||
if (g_queueTable[GET_QUEUE_INDEX(mqueueID)].mqcb == NULL) {
|
||||
mqueueCB = &(g_queueTable[GET_QUEUE_INDEX(mqueueID)]);
|
||||
if (IPC_QUEUE_TABLE[GET_QUEUE_INDEX(mqueueID)].mqcb == NULL) {
|
||||
mqueueCB = &(IPC_QUEUE_TABLE[GET_QUEUE_INDEX(mqueueID)]);
|
||||
mqueueCB->mq_id = mqueueID;
|
||||
}
|
||||
|
||||
@@ -297,7 +300,7 @@ STATIC struct mqpersonal *MqGetPrivDataBuff(mqd_t personal)
|
||||
errno = EBADF;
|
||||
return NULL;
|
||||
}
|
||||
return g_mqPrivBuf[id];
|
||||
return IPC_QUEUE_MQ_PRIV_BUF[id];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -311,13 +314,13 @@ STATIC struct mqpersonal *MqGetPrivDataBuff(mqd_t personal)
|
||||
STATIC INT32 MqAllocSysFd(int maxfdp, struct mqpersonal *privateMqPersonal)
|
||||
{
|
||||
INT32 i;
|
||||
fd_set *fdset = &g_queueFdSet;
|
||||
fd_set *fdset = &IPC_QUEUE_FD_SET;
|
||||
for (i = 0; i < maxfdp; i++) {
|
||||
/* sysFd: used bit setting, and get the index of swtmrID buffer */
|
||||
if (fdset && !(FD_ISSET(i + MQUEUE_FD_OFFSET, fdset))) {
|
||||
FD_SET(i + MQUEUE_FD_OFFSET, fdset);
|
||||
if (!g_mqPrivBuf[i]) {
|
||||
g_mqPrivBuf[i] = privateMqPersonal;
|
||||
if (!IPC_QUEUE_MQ_PRIV_BUF[i]) {
|
||||
IPC_QUEUE_MQ_PRIV_BUF[i] = privateMqPersonal;
|
||||
return i + MQUEUE_FD_OFFSET;
|
||||
}
|
||||
}
|
||||
@@ -328,10 +331,10 @@ STATIC INT32 MqAllocSysFd(int maxfdp, struct mqpersonal *privateMqPersonal)
|
||||
STATIC VOID MqFreeSysFd(mqd_t personal)
|
||||
{
|
||||
INT32 sysFd = (INT32)personal;
|
||||
fd_set *fdset = &g_queueFdSet;
|
||||
fd_set *fdset = &IPC_QUEUE_FD_SET;
|
||||
if (fdset && FD_ISSET(sysFd, fdset)) {
|
||||
FD_CLR(sysFd, fdset);
|
||||
g_mqPrivBuf[sysFd - MQUEUE_FD_OFFSET] = NULL;
|
||||
IPC_QUEUE_MQ_PRIV_BUF[sysFd - MQUEUE_FD_OFFSET] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -341,7 +344,7 @@ void MqueueRefer(int sysFd)
|
||||
struct mqarray *mqueueCB = NULL;
|
||||
struct mqpersonal *privateMqPersonal = NULL;
|
||||
|
||||
(VOID)pthread_mutex_lock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
|
||||
/* Get the personal sysFd and reset personal fd -1 */
|
||||
privateMqPersonal = MqGetPrivDataBuff((mqd_t)sysFd);
|
||||
if (privateMqPersonal == NULL) {
|
||||
@@ -354,7 +357,7 @@ void MqueueRefer(int sysFd)
|
||||
|
||||
privateMqPersonal->mq_refcount++;
|
||||
OUT_UNLOCK:
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -498,7 +501,7 @@ mqd_t mq_open(const char *mqName, int openFlag, ...)
|
||||
return (mqd_t)-1;
|
||||
}
|
||||
|
||||
(VOID)pthread_mutex_lock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
|
||||
mqueueCB = GetMqueueCBByName(mqName);
|
||||
if ((UINT32)openFlag & (UINT32)O_CREAT) {
|
||||
if (mqueueCB != NULL) {
|
||||
@@ -543,7 +546,7 @@ mqd_t mq_open(const char *mqName, int openFlag, ...)
|
||||
mqFd = (mqd_t)sysFd;
|
||||
}
|
||||
OUT:
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
return mqFd;
|
||||
}
|
||||
|
||||
@@ -552,7 +555,7 @@ int mq_close(mqd_t personal)
|
||||
INT32 ret = -1;
|
||||
struct mqpersonal *privateMqPersonal = NULL;
|
||||
|
||||
(VOID)pthread_mutex_lock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
|
||||
|
||||
/* Get the personal sysFd and reset personal fd -1 */
|
||||
privateMqPersonal = MqGetPrivDataBuff(personal);
|
||||
@@ -577,7 +580,7 @@ int mq_close(mqd_t personal)
|
||||
MqFreeSysFd(personal);
|
||||
|
||||
OUT_UNLOCK:
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -596,10 +599,10 @@ int OsMqGetAttr(mqd_t personal, struct mq_attr *mqAttr)
|
||||
return -1;
|
||||
}
|
||||
|
||||
(VOID)pthread_mutex_lock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
|
||||
if (privateMqPersonal->mq_status != MQ_USE_MAGIC) {
|
||||
errno = EBADF;
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -608,7 +611,7 @@ int OsMqGetAttr(mqd_t personal, struct mq_attr *mqAttr)
|
||||
mqAttr->mq_msgsize = mqueueCB->mqcb->queueSize - sizeof(UINT32);
|
||||
mqAttr->mq_curmsgs = mqueueCB->mqcb->readWriteableCnt[OS_QUEUE_READ];
|
||||
mqAttr->mq_flags = privateMqPersonal->mq_flags;
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -626,10 +629,10 @@ int OsMqSetAttr(mqd_t personal, const struct mq_attr *mqSetAttr, struct mq_attr
|
||||
return -1;
|
||||
}
|
||||
|
||||
(VOID)pthread_mutex_lock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
|
||||
if (privateMqPersonal->mq_status != MQ_USE_MAGIC) {
|
||||
errno = EBADF;
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -641,7 +644,7 @@ int OsMqSetAttr(mqd_t personal, const struct mq_attr *mqSetAttr, struct mq_attr
|
||||
if (((UINT32)mqSetAttr->mq_flags & (UINT32)FNONBLOCK) == (UINT32)FNONBLOCK) {
|
||||
privateMqPersonal->mq_flags = (INT32)((UINT32)privateMqPersonal->mq_flags | (UINT32)FNONBLOCK);
|
||||
}
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -662,7 +665,7 @@ int mq_unlink(const char *mqName)
|
||||
return -1;
|
||||
}
|
||||
|
||||
(VOID)pthread_mutex_lock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
|
||||
mqueueCB = GetMqueueCBByName(mqName);
|
||||
if (mqueueCB == NULL) {
|
||||
errno = ENOENT;
|
||||
@@ -675,11 +678,11 @@ int mq_unlink(const char *mqName)
|
||||
ret = DoMqueueDelete(mqueueCB);
|
||||
}
|
||||
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
return ret;
|
||||
|
||||
ERROUT_UNLOCK:
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -767,7 +770,7 @@ int mq_timedsend(mqd_t personal, const char *msg, size_t msgLen, unsigned int ms
|
||||
OS_MQ_GOTO_ERROUT_IF(!MqParamCheck(personal, msg, msgLen), errno);
|
||||
OS_MQ_GOTO_ERROUT_IF(msgPrio > (MQ_PRIO_MAX - 1), EINVAL);
|
||||
|
||||
(VOID)pthread_mutex_lock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
|
||||
privateMqPersonal = MqGetPrivDataBuff(personal);
|
||||
|
||||
OS_MQ_GOTO_ERROUT_UNLOCK_IF(privateMqPersonal == NULL || privateMqPersonal->mq_status != MQ_USE_MAGIC, EBADF);
|
||||
@@ -781,7 +784,7 @@ int mq_timedsend(mqd_t personal, const char *msg, size_t msgLen, unsigned int ms
|
||||
|
||||
OS_MQ_GOTO_ERROUT_UNLOCK_IF(ConvertTimeout(privateMqPersonal->mq_flags, absTimeout, &absTicks) == -1, errno);
|
||||
mqueueID = mqueueCB->mq_id;
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
|
||||
if (LOS_ListEmpty(&mqueueCB->mqcb->readWriteList[OS_QUEUE_READ])) {
|
||||
MqSendNotify(mqueueCB);
|
||||
@@ -793,7 +796,7 @@ int mq_timedsend(mqd_t personal, const char *msg, size_t msgLen, unsigned int ms
|
||||
}
|
||||
return 0;
|
||||
ERROUT_UNLOCK:
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
ERROUT:
|
||||
return -1;
|
||||
}
|
||||
@@ -815,7 +818,7 @@ ssize_t mq_timedreceive(mqd_t personal, char *msg, size_t msgLen, unsigned int *
|
||||
*msgPrio = 0;
|
||||
}
|
||||
|
||||
(VOID)pthread_mutex_lock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
|
||||
privateMqPersonal = MqGetPrivDataBuff(personal);
|
||||
if (privateMqPersonal == NULL || privateMqPersonal->mq_status != MQ_USE_MAGIC) {
|
||||
errno = EBADF;
|
||||
@@ -839,7 +842,7 @@ ssize_t mq_timedreceive(mqd_t personal, char *msg, size_t msgLen, unsigned int *
|
||||
|
||||
receiveLen = msgLen;
|
||||
mqueueID = mqueueCB->mq_id;
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
|
||||
err = LOS_QueueReadCopy(mqueueID, (VOID *)msg, &receiveLen, (UINT32)absTicks);
|
||||
if (map_errno(err) == ENOERR) {
|
||||
@@ -849,7 +852,7 @@ ssize_t mq_timedreceive(mqd_t personal, char *msg, size_t msgLen, unsigned int *
|
||||
}
|
||||
|
||||
ERROUT_UNLOCK:
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
ERROUT:
|
||||
return -1;
|
||||
}
|
||||
@@ -898,7 +901,7 @@ int OsMqNotify(mqd_t personal, const struct sigevent *sigev)
|
||||
goto ERROUT;
|
||||
}
|
||||
|
||||
(VOID)pthread_mutex_lock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
|
||||
privateMqPersonal = MqGetPrivDataBuff(personal);
|
||||
if (privateMqPersonal == NULL) {
|
||||
goto OUT_UNLOCK;
|
||||
@@ -936,10 +939,10 @@ int OsMqNotify(mqd_t personal, const struct sigevent *sigev)
|
||||
mqnotify->pid = LOS_GetCurrProcessID();
|
||||
}
|
||||
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
return 0;
|
||||
OUT_UNLOCK:
|
||||
(VOID)pthread_mutex_unlock(&g_mqueueMutex);
|
||||
(VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
|
||||
ERROUT:
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -599,13 +599,22 @@ int clock_gettime(clockid_t clockID, struct timespec *tp)
|
||||
|
||||
switch (clockID) {
|
||||
case CLOCK_MONOTONIC_RAW:
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
tmp = OsTimeSpecAdd(hwTime, CLOCK_MONOTONIC_TIME_BASE);
|
||||
tp->tv_sec = tmp.tv_sec;
|
||||
tp->tv_nsec = tmp.tv_nsec;
|
||||
#else
|
||||
tp->tv_sec = hwTime.tv_sec;
|
||||
tp->tv_nsec = hwTime.tv_nsec;
|
||||
#endif
|
||||
break;
|
||||
case CLOCK_MONOTONIC:
|
||||
LOS_SpinLockSave(&g_timeSpin, &intSave);
|
||||
tmp = OsTimeSpecAdd(hwTime, g_accDeltaFromAdj);
|
||||
LOS_SpinUnlockRestore(&g_timeSpin, intSave);
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
tmp = OsTimeSpecAdd(tmp, CLOCK_MONOTONIC_TIME_BASE);
|
||||
#endif
|
||||
tp->tv_sec = tmp.tv_sec;
|
||||
tp->tv_nsec = tmp.tv_nsec;
|
||||
break;
|
||||
@@ -639,7 +648,7 @@ int clock_gettime(clockid_t clockID, struct timespec *tp)
|
||||
|
||||
return 0;
|
||||
|
||||
ERROUT:
|
||||
ERROUT:
|
||||
TIME_RETURN(EINVAL);
|
||||
}
|
||||
|
||||
|
||||
@@ -125,10 +125,18 @@ int VfsProcfsRead(struct file *filep, char *buffer, size_t buflen)
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
VnodeHold();
|
||||
entry = VnodeToEntry(filep->f_vnode);
|
||||
if (entry == NULL) {
|
||||
VnodeDrop();
|
||||
return -EPERM;
|
||||
}
|
||||
|
||||
spin_lock(&entry->pdeUnloadLock);
|
||||
size = (ssize_t)ReadProcFile(entry, (void *)buffer, buflen);
|
||||
filep->f_pos = entry->pf->fPos;
|
||||
|
||||
spin_unlock(&entry->pdeUnloadLock);
|
||||
VnodeDrop();
|
||||
return size;
|
||||
}
|
||||
|
||||
@@ -140,10 +148,18 @@ int VfsProcfsWrite(struct file *filep, const char *buffer, size_t buflen)
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
VnodeHold();
|
||||
entry = VnodeToEntry(filep->f_vnode);
|
||||
if (entry == NULL) {
|
||||
VnodeDrop();
|
||||
return -EPERM;
|
||||
}
|
||||
|
||||
spin_lock(&entry->pdeUnloadLock);
|
||||
size = (ssize_t)WriteProcFile(entry, (void *)buffer, buflen);
|
||||
filep->f_pos = entry->pf->fPos;
|
||||
|
||||
spin_unlock(&entry->pdeUnloadLock);
|
||||
VnodeDrop();
|
||||
return size;
|
||||
}
|
||||
|
||||
@@ -156,9 +172,12 @@ int VfsProcfsLookup(struct Vnode *parent, const char *name, int len, struct Vnod
|
||||
if (entry == NULL) {
|
||||
return -ENODATA;
|
||||
}
|
||||
|
||||
spin_lock(&procfsLock);
|
||||
entry = entry->subdir;
|
||||
while (1) {
|
||||
if (entry == NULL) {
|
||||
spin_unlock(&procfsLock);
|
||||
return -ENOENT;
|
||||
}
|
||||
if (EntryMatch(name, len, entry)) {
|
||||
@@ -166,6 +185,7 @@ int VfsProcfsLookup(struct Vnode *parent, const char *name, int len, struct Vnod
|
||||
}
|
||||
entry = entry->next;
|
||||
}
|
||||
spin_unlock(&procfsLock);
|
||||
|
||||
*vpp = EntryToVnode(entry);
|
||||
if ((*vpp) == NULL) {
|
||||
@@ -214,11 +234,17 @@ int VfsProcfsUnmount(void *handle, struct Vnode **blkdriver)
|
||||
|
||||
int VfsProcfsStat(struct Vnode *node, struct stat *buf)
|
||||
{
|
||||
VnodeHold();
|
||||
struct ProcDirEntry *entry = VnodeToEntry(node);
|
||||
|
||||
if (entry == NULL) {
|
||||
VnodeDrop();
|
||||
return -EPERM;
|
||||
}
|
||||
(void)memset_s(buf, sizeof(struct stat), 0, sizeof(struct stat));
|
||||
spin_lock(&entry->pdeUnloadLock);
|
||||
buf->st_mode = entry->mode;
|
||||
|
||||
spin_unlock(&entry->pdeUnloadLock);
|
||||
VnodeDrop();
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
@@ -226,9 +252,7 @@ int VfsProcfsReaddir(struct Vnode *node, struct fs_dirent_s *dir)
|
||||
{
|
||||
int result;
|
||||
char *buffer = NULL;
|
||||
int buflen = NAME_MAX;
|
||||
unsigned int min_size;
|
||||
unsigned int dst_name_size;
|
||||
unsigned int minSize, dstNameSize;
|
||||
struct ProcDirEntry *pde = NULL;
|
||||
int i = 0;
|
||||
|
||||
@@ -238,28 +262,38 @@ int VfsProcfsReaddir(struct Vnode *node, struct fs_dirent_s *dir)
|
||||
if (node->type != VNODE_TYPE_DIR) {
|
||||
return -ENOTDIR;
|
||||
}
|
||||
VnodeHold();
|
||||
pde = VnodeToEntry(node);
|
||||
if (pde == NULL) {
|
||||
VnodeDrop();
|
||||
return -EPERM;
|
||||
}
|
||||
|
||||
spin_lock(&pde->pdeUnloadLock);
|
||||
while (i < dir->read_cnt) {
|
||||
buffer = (char *)zalloc(sizeof(char) * NAME_MAX);
|
||||
if (buffer == NULL) {
|
||||
spin_unlock(&pde->pdeUnloadLock);
|
||||
VnodeDrop();
|
||||
PRINT_ERR("malloc failed\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
result = ReadProcFile(pde, (void *)buffer, buflen);
|
||||
result = ReadProcFile(pde, (void *)buffer, NAME_MAX);
|
||||
if (result != ENOERR) {
|
||||
free(buffer);
|
||||
break;
|
||||
}
|
||||
dst_name_size = sizeof(dir->fd_dir[i].d_name);
|
||||
min_size = (dst_name_size < NAME_MAX) ? dst_name_size : NAME_MAX;
|
||||
result = strncpy_s(dir->fd_dir[i].d_name, dst_name_size, buffer, min_size);
|
||||
dstNameSize = sizeof(dir->fd_dir[i].d_name);
|
||||
minSize = (dstNameSize < NAME_MAX) ? dstNameSize : NAME_MAX;
|
||||
result = strncpy_s(dir->fd_dir[i].d_name, dstNameSize, buffer, minSize);
|
||||
if (result != EOK) {
|
||||
spin_unlock(&pde->pdeUnloadLock);
|
||||
VnodeDrop();
|
||||
free(buffer);
|
||||
return -ENAMETOOLONG;
|
||||
}
|
||||
dir->fd_dir[i].d_name[dst_name_size - 1] = '\0';
|
||||
dir->fd_dir[i].d_name[dstNameSize - 1] = '\0';
|
||||
dir->fd_position++;
|
||||
dir->fd_dir[i].d_off = dir->fd_position;
|
||||
dir->fd_dir[i].d_reclen = (uint16_t)sizeof(struct dirent);
|
||||
@@ -267,21 +301,30 @@ int VfsProcfsReaddir(struct Vnode *node, struct fs_dirent_s *dir)
|
||||
i++;
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
spin_unlock(&pde->pdeUnloadLock);
|
||||
VnodeDrop();
|
||||
return i;
|
||||
}
|
||||
|
||||
int VfsProcfsOpendir(struct Vnode *node, struct fs_dirent_s *dir)
|
||||
{
|
||||
VnodeHold();
|
||||
struct ProcDirEntry *pde = VnodeToEntry(node);
|
||||
if (pde == NULL) {
|
||||
VnodeDrop();
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
spin_lock(&pde->pdeUnloadLock);
|
||||
pde->pdirCurrent = pde->subdir;
|
||||
if (pde->pf == NULL) {
|
||||
spin_unlock(&pde->pdeUnloadLock);
|
||||
VnodeDrop();
|
||||
return -EINVAL;
|
||||
}
|
||||
pde->pf->fPos = 0;
|
||||
spin_unlock(&pde->pdeUnloadLock);
|
||||
VnodeDrop();
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
@@ -290,9 +333,17 @@ int VfsProcfsOpen(struct file *filep)
|
||||
if (filep == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
VnodeHold();
|
||||
struct Vnode *node = filep->f_vnode;
|
||||
struct ProcDirEntry *pde = VnodeToEntry(node);
|
||||
if (pde == NULL) {
|
||||
VnodeDrop();
|
||||
return -EPERM;
|
||||
}
|
||||
|
||||
spin_lock(&pde->pdeUnloadLock);
|
||||
if (ProcOpen(pde->pf) != OK) {
|
||||
spin_unlock(&pde->pdeUnloadLock);
|
||||
return -ENOMEM;
|
||||
}
|
||||
if (S_ISREG(pde->mode) && (pde->procFileOps != NULL) && (pde->procFileOps->open != NULL)) {
|
||||
@@ -303,6 +354,8 @@ int VfsProcfsOpen(struct file *filep)
|
||||
pde->pf->fPos = 0;
|
||||
}
|
||||
filep->f_priv = (void *)pde;
|
||||
spin_unlock(&pde->pdeUnloadLock);
|
||||
VnodeDrop();
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
@@ -312,15 +365,24 @@ int VfsProcfsClose(struct file *filep)
|
||||
if (filep == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
VnodeHold();
|
||||
struct Vnode *node = filep->f_vnode;
|
||||
struct ProcDirEntry *pde = VnodeToEntry(node);
|
||||
if (pde == NULL) {
|
||||
VnodeDrop();
|
||||
return -EPERM;
|
||||
}
|
||||
|
||||
spin_lock(&pde->pdeUnloadLock);
|
||||
pde->pf->fPos = 0;
|
||||
if ((pde->procFileOps != NULL) && (pde->procFileOps->release != NULL)) {
|
||||
result = pde->procFileOps->release((struct Vnode *)pde, pde->pf);
|
||||
}
|
||||
LosBufRelease(pde->pf->sbuf);
|
||||
pde->pf->sbuf = NULL;
|
||||
|
||||
spin_unlock(&pde->pdeUnloadLock);
|
||||
VnodeDrop();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -345,9 +407,15 @@ ssize_t VfsProcfsReadlink(struct Vnode *vnode, char *buffer, size_t bufLen)
|
||||
}
|
||||
|
||||
struct ProcDirEntry *pde = VnodeToEntry(vnode);
|
||||
if (pde == NULL) {
|
||||
return -EPERM;
|
||||
}
|
||||
|
||||
spin_lock(&pde->pdeUnloadLock);
|
||||
if ((pde->procFileOps != NULL) && (pde->procFileOps->readLink != NULL)) {
|
||||
result = pde->procFileOps->readLink(pde, buffer, bufLen);
|
||||
}
|
||||
spin_unlock(&pde->pdeUnloadLock);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,8 @@
|
||||
#include "proc_fs.h"
|
||||
#include "internal.h"
|
||||
#include "los_process_pri.h"
|
||||
#include "user_copy.h"
|
||||
#include "los_memory.h"
|
||||
|
||||
#ifdef LOSCFG_PROC_PROCESS_DIR
|
||||
#include "los_vm_dump.h"
|
||||
@@ -64,12 +66,16 @@ struct ProcessData {
|
||||
static ssize_t ProcessContainerLink(unsigned int containerID, ContainerType type, char *buffer, size_t bufLen)
|
||||
{
|
||||
ssize_t count = -1;
|
||||
if (type == PID_CONTAINER) {
|
||||
if ((type == PID_CONTAINER) || (type == PID_CHILD_CONTAINER)) {
|
||||
count = snprintf_s(buffer, bufLen, bufLen - 1, "'pid:[%u]'", containerID);
|
||||
} else if (type == UTS_CONTAINER) {
|
||||
count = snprintf_s(buffer, bufLen, bufLen - 1, "'uts:[%u]'", containerID);
|
||||
} else if (type == MNT_CONTAINER) {
|
||||
count = snprintf_s(buffer, bufLen, bufLen - 1, "'mnt:[%u]'", containerID);
|
||||
} else if (type == IPC_CONTAINER) {
|
||||
count = snprintf_s(buffer, bufLen, bufLen - 1, "'ipc:[%u]'", containerID);
|
||||
} else if ((type == TIME_CONTAINER) || (type == TIME_CHILD_CONTAINER)) {
|
||||
count = snprintf_s(buffer, bufLen, bufLen - 1, "'time:[%u]'", containerID);
|
||||
}
|
||||
|
||||
if (count < 0) {
|
||||
@@ -172,6 +178,110 @@ static int ProcessCpupRead(struct SeqBuf *seqBuf, LosProcessCB *pcb)
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
static const CHAR *g_monotonic = "monotonic";
|
||||
#define DECIMAL_BASE 10
|
||||
|
||||
static int ProcTimeContainerRead(struct SeqBuf *m, void *v)
|
||||
{
|
||||
int ret;
|
||||
unsigned int intSave;
|
||||
struct timespec64 offsets = {0};
|
||||
|
||||
if ((m == NULL) || (v == NULL)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
struct ProcessData *data = (struct ProcessData *)v;
|
||||
SCHEDULER_LOCK(intSave);
|
||||
LosProcessCB *processCB = (LosProcessCB *)data->process;
|
||||
ret = OsGetTimeContainerMonotonic(processCB, &offsets);
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
if (ret != LOS_OK) {
|
||||
return -ret;
|
||||
}
|
||||
|
||||
LosBufPrintf(m, "monotonic %lld %ld\n", offsets.tv_sec, offsets.tv_nsec);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ProcSetTimensOffset(const char *buf, LosProcessCB *processCB)
|
||||
{
|
||||
unsigned int intSave;
|
||||
struct timespec64 offsets;
|
||||
char *endptr = NULL;
|
||||
|
||||
offsets.tv_sec = strtoll(buf, &endptr, DECIMAL_BASE);
|
||||
offsets.tv_nsec = strtoll(endptr, NULL, DECIMAL_BASE);
|
||||
if (offsets.tv_nsec >= OS_SYS_NS_PER_SECOND) {
|
||||
return -EACCES;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
unsigned int ret = OsSetTimeContainerMonotonic(processCB, &offsets);
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
if (ret != LOS_OK) {
|
||||
return -ret;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ProcTimeContainerWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
|
||||
{
|
||||
(void)ppos;
|
||||
char *kbuf = NULL;
|
||||
int ret;
|
||||
|
||||
if ((pf == NULL) || (count <= 0)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
struct ProcDirEntry *entry = pf->pPDE;
|
||||
if (entry == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
struct ProcessData *data = (struct ProcessData *)entry->data;
|
||||
if (data == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (LOS_IsUserAddressRange((VADDR_T)(UINTPTR)buf, count)) {
|
||||
kbuf = LOS_MemAlloc(m_aucSysMem1, count + 1);
|
||||
if (kbuf == NULL) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
if (LOS_ArchCopyFromUser(kbuf, buf, count) != 0) {
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, kbuf);
|
||||
return -EFAULT;
|
||||
}
|
||||
kbuf[count] = '\0';
|
||||
buf = kbuf;
|
||||
}
|
||||
|
||||
ret = strncmp(buf, g_monotonic, strlen(g_monotonic));
|
||||
if (ret != 0) {
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, kbuf);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
buf += strlen(g_monotonic);
|
||||
ret = ProcSetTimensOffset(buf, (LosProcessCB *)data->process);
|
||||
if (ret < 0) {
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, kbuf);
|
||||
return ret;
|
||||
}
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, kbuf);
|
||||
return count;
|
||||
}
|
||||
|
||||
static const struct ProcFileOperations TIME_CONTAINER_FOPS = {
|
||||
.read = ProcTimeContainerRead,
|
||||
.write = ProcTimeContainerWrite,
|
||||
};
|
||||
#endif
|
||||
|
||||
static int ProcProcessRead(struct SeqBuf *m, void *v)
|
||||
{
|
||||
if ((m == NULL) || (v == NULL)) {
|
||||
@@ -198,7 +308,7 @@ static const struct ProcFileOperations PID_FOPS = {
|
||||
static struct ProcProcess g_procProcess[] = {
|
||||
{
|
||||
.name = NULL,
|
||||
.mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH,
|
||||
.mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IROTH,
|
||||
.type = PROC_PID,
|
||||
.fileOps = &PID_FOPS
|
||||
|
||||
@@ -233,6 +343,12 @@ static struct ProcProcess g_procProcess[] = {
|
||||
.type = PID_CONTAINER,
|
||||
.fileOps = &PID_CONTAINER_FOPS
|
||||
},
|
||||
{
|
||||
.name = "container/pid_for_children",
|
||||
.mode = S_IFLNK,
|
||||
.type = PID_CHILD_CONTAINER,
|
||||
.fileOps = &PID_CONTAINER_FOPS
|
||||
},
|
||||
#endif
|
||||
#ifdef LOSCFG_UTS_CONTAINER
|
||||
{
|
||||
@@ -250,6 +366,34 @@ static struct ProcProcess g_procProcess[] = {
|
||||
.fileOps = &PID_CONTAINER_FOPS
|
||||
},
|
||||
#endif
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
{
|
||||
.name = "container/ipc",
|
||||
.mode = S_IFLNK,
|
||||
.type = IPC_CONTAINER,
|
||||
.fileOps = &PID_CONTAINER_FOPS
|
||||
},
|
||||
#endif
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
{
|
||||
.name = "container/time",
|
||||
.mode = S_IFLNK,
|
||||
.type = TIME_CONTAINER,
|
||||
.fileOps = &PID_CONTAINER_FOPS
|
||||
},
|
||||
{
|
||||
.name = "container/time_for_children",
|
||||
.mode = S_IFLNK,
|
||||
.type = TIME_CHILD_CONTAINER,
|
||||
.fileOps = &PID_CONTAINER_FOPS
|
||||
},
|
||||
{
|
||||
.name = "time_offsets",
|
||||
.mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH,
|
||||
.type = TIME_CONTAINER,
|
||||
.fileOps = &TIME_CONTAINER_FOPS
|
||||
},
|
||||
#endif
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
@@ -378,11 +378,34 @@ struct ProcDirEntry *CreateProcEntry(const char *name, mode_t mode, struct ProcD
|
||||
return pde;
|
||||
}
|
||||
|
||||
static void ProcEntryClearVnode(struct ProcDirEntry *entry)
|
||||
{
|
||||
struct Vnode *item = NULL;
|
||||
struct Vnode *nextItem = NULL;
|
||||
|
||||
VnodeHold();
|
||||
LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, GetVnodeActiveList(), struct Vnode, actFreeEntry) {
|
||||
if ((struct ProcDirEntry *)item->data != entry) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (VnodeFree(item) != LOS_OK) {
|
||||
PRINT_ERR("ProcEntryClearVnode free failed, entry: %s : 0x%x \n", entry->name, item);
|
||||
}
|
||||
}
|
||||
VnodeDrop();
|
||||
return;
|
||||
}
|
||||
|
||||
static void FreeProcEntry(struct ProcDirEntry *entry)
|
||||
{
|
||||
if (entry == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
ProcEntryClearVnode(entry);
|
||||
|
||||
spin_lock(&entry->pdeUnloadLock);
|
||||
if (entry->pf != NULL) {
|
||||
free(entry->pf);
|
||||
entry->pf = NULL;
|
||||
@@ -391,6 +414,7 @@ static void FreeProcEntry(struct ProcDirEntry *entry)
|
||||
free(entry->data);
|
||||
entry->data = NULL;
|
||||
}
|
||||
spin_unlock(&entry->pdeUnloadLock);
|
||||
free(entry);
|
||||
}
|
||||
|
||||
@@ -562,22 +586,21 @@ static int ProcRead(struct ProcDirEntry *pde, char *buf, size_t len)
|
||||
|
||||
struct ProcDirEntry *OpenProcFile(const char *fileName, int flags, ...)
|
||||
{
|
||||
unsigned int intSave;
|
||||
struct ProcDirEntry *pn = ProcFindEntry(fileName);
|
||||
if (pn == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
spin_lock(&pn->pdeUnloadLock);
|
||||
if (S_ISREG(pn->mode) && (pn->count != 1)) {
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
spin_unlock(&pn->pdeUnloadLock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pn->flags = (unsigned int)(pn->flags) | (unsigned int)flags;
|
||||
atomic_set(&pn->count, PROC_INUSE);
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
if (ProcOpen(pn->pf) != OK) {
|
||||
spin_unlock(&pn->pdeUnloadLock);
|
||||
return NULL;
|
||||
}
|
||||
if (S_ISREG(pn->mode) && (pn->procFileOps != NULL) && (pn->procFileOps->open != NULL)) {
|
||||
@@ -587,6 +610,7 @@ struct ProcDirEntry *OpenProcFile(const char *fileName, int flags, ...)
|
||||
pn->pdirCurrent = pn->subdir;
|
||||
pn->pf->fPos = 0;
|
||||
}
|
||||
spin_unlock(&pn->pdeUnloadLock);
|
||||
|
||||
return pn;
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ config KERNEL_CONTAINER
|
||||
depends on KERNEL_VM
|
||||
|
||||
config PID_CONTAINER
|
||||
bool "Enable PID container Feature"
|
||||
bool "Enable pid container Feature"
|
||||
default n
|
||||
depends on KERNEL_CONTAINER
|
||||
|
||||
@@ -84,7 +84,7 @@ config UTS_CONTAINER
|
||||
depends on KERNEL_CONTAINER
|
||||
|
||||
config MNT_CONTAINER
|
||||
bool "Enable MNT container Feature"
|
||||
bool "Enable mnt container Feature"
|
||||
default n
|
||||
depends on KERNEL_CONTAINER
|
||||
|
||||
@@ -93,6 +93,16 @@ config CHROOT
|
||||
default n
|
||||
depends on MNT_CONTAINER
|
||||
|
||||
config IPC_CONTAINER
|
||||
bool "Enable ipc container Feature"
|
||||
default n
|
||||
depends on KERNEL_CONTAINER
|
||||
|
||||
config TIME_CONTAINER
|
||||
bool "Enable time container"
|
||||
default n
|
||||
depends on KERNEL_CONTAINER
|
||||
|
||||
######################### config options of extended #####################
|
||||
source "kernel/extended/Kconfig"
|
||||
|
||||
|
||||
@@ -33,8 +33,10 @@ module_name = get_path_info(rebase_path("."), "name")
|
||||
kernel_module(module_name) {
|
||||
sources = [
|
||||
"container/los_container.c",
|
||||
"container/los_ipc_container.c",
|
||||
"container/los_mnt_container.c",
|
||||
"container/los_pid_container.c",
|
||||
"container/los_time_container.c",
|
||||
"container/los_uts_container.c",
|
||||
"core/los_bitmap.c",
|
||||
"core/los_info.c",
|
||||
|
||||
@@ -53,19 +53,27 @@ VOID OsInitRootContainer(VOID)
|
||||
{
|
||||
#ifdef LOSCFG_PID_CONTAINER
|
||||
(VOID)OsInitRootPidContainer(&g_rootContainer.pidContainer);
|
||||
g_rootContainer.pidForChildContainer = g_rootContainer.pidContainer;
|
||||
#endif
|
||||
#ifdef LOSCFG_UTS_CONTAINER
|
||||
(VOID)OsInitRootUtsContainer(&g_rootContainer.utsContainer);
|
||||
#endif
|
||||
#ifdef LOSCFG_MNT_CONTAINER
|
||||
(VOID)OsInitRootMntContainer(&g_rootContainer.mntContainer);
|
||||
#endif
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
(VOID)OsInitRootIpcContainer(&g_rootContainer.ipcContainer);
|
||||
#endif
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
(VOID)OsInitRootTimeContainer(&g_rootContainer.timeContainer);
|
||||
g_rootContainer.timeForChildContainer = g_rootContainer.timeContainer;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
STATIC INLINE Container *CreateContainer(VOID)
|
||||
{
|
||||
Container *container = LOS_MemAlloc(m_aucSysMem1, sizeof(Container));
|
||||
Container *container = (Container *)LOS_MemAlloc(m_aucSysMem1, sizeof(Container));
|
||||
if (container == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -76,27 +84,9 @@ STATIC INLINE Container *CreateContainer(VOID)
|
||||
return container;
|
||||
}
|
||||
|
||||
/*
|
||||
* called from clone. This now handles copy for Container and all
|
||||
* namespaces therein.
|
||||
*/
|
||||
UINT32 OsCopyContainers(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID)
|
||||
STATIC UINT32 CopyContainers(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID)
|
||||
{
|
||||
UINT32 intSave;
|
||||
UINT32 ret = LOS_OK;
|
||||
|
||||
if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWPID | CLONE_NEWNET))) {
|
||||
SCHEDULER_LOCK(intSave);
|
||||
child->container = parent->container;
|
||||
LOS_AtomicInc(&child->container->rc);
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
} else {
|
||||
child->container = CreateContainer();
|
||||
if (child->container == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
/* Pid container initialization must precede other container initialization. */
|
||||
#ifdef LOSCFG_PID_CONTAINER
|
||||
ret = OsCopyPidContainer(flags, child, parent, processID);
|
||||
@@ -115,10 +105,74 @@ UINT32 OsCopyContainers(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent
|
||||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
ret = OsCopyIpcContainer(flags, child, parent);
|
||||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
ret = OsCopyTimeContainer(flags, child, parent);
|
||||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* called from clone. This now handles copy for Container and all
|
||||
* namespaces therein.
|
||||
*/
|
||||
UINT32 OsCopyContainers(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID)
|
||||
{
|
||||
UINT32 intSave;
|
||||
|
||||
if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWPID | CLONE_NEWNET | CLONE_NEWTIME))) {
|
||||
#ifdef LOSCFG_PID_CONTAINER
|
||||
if (parent->container->pidContainer != parent->container->pidForChildContainer) {
|
||||
goto CREATE_CONTAINER;
|
||||
}
|
||||
#endif
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
if (parent->container->timeContainer != parent->container->timeForChildContainer) {
|
||||
goto CREATE_CONTAINER;
|
||||
}
|
||||
#endif
|
||||
SCHEDULER_LOCK(intSave);
|
||||
child->container = parent->container;
|
||||
LOS_AtomicInc(&child->container->rc);
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
goto COPY_CONTAINERS;
|
||||
}
|
||||
|
||||
#if defined(LOSCFG_PID_CONTAINER) || defined(LOSCFG_TIME_CONTAINER)
|
||||
CREATE_CONTAINER:
|
||||
#endif
|
||||
child->container = CreateContainer();
|
||||
if (child->container == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
COPY_CONTAINERS:
|
||||
return CopyContainers(flags, child, parent, processID);
|
||||
}
|
||||
|
||||
#ifndef LOSCFG_PID_CONTAINER
|
||||
STATIC VOID ContainersFree(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. */
|
||||
@@ -129,19 +183,23 @@ VOID OsContainersDestroy(LosProcessCB *processCB)
|
||||
#endif
|
||||
|
||||
#ifdef LOSCFG_UTS_CONTAINER
|
||||
OsUtsContainersDestroy(processCB);
|
||||
OsUtsContainersDestroy(processCB->container);
|
||||
#endif
|
||||
|
||||
#ifdef LOSCFG_MNT_CONTAINER
|
||||
OsMntContainersDestroy(processCB);
|
||||
OsMntContainersDestroy(processCB->container);
|
||||
#endif
|
||||
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
OsIpcContainersDestroy(processCB->container);
|
||||
#endif
|
||||
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
OsTimeContainersDestroy(processCB);
|
||||
#endif
|
||||
|
||||
#ifndef LOSCFG_PID_CONTAINER
|
||||
LOS_AtomicDec(&curr->container->rc);
|
||||
if (LOS_AtomicRead(&processCB->container->rc) == 1) {
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, processCB->container);
|
||||
processCB->container = NULL;
|
||||
}
|
||||
ContainersFree(processCB);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -152,15 +210,155 @@ UINT32 OsGetContainerID(Container *container, ContainerType type)
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
#ifdef LOSCFG_PID_CONTAINER
|
||||
case PID_CONTAINER:
|
||||
return OsGetPidContainerID(container->pidContainer);
|
||||
case PID_CHILD_CONTAINER:
|
||||
return OsGetPidContainerID(container->pidForChildContainer);
|
||||
#endif
|
||||
#ifdef LOSCFG_UTS_CONTAINER
|
||||
case UTS_CONTAINER:
|
||||
return OsGetUtsContainerID(container->utsContainer);
|
||||
#endif
|
||||
#ifdef LOSCFG_MNT_CONTAINER
|
||||
case MNT_CONTAINER:
|
||||
return OsGetMntContainerID(container->mntContainer);
|
||||
#endif
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
case IPC_CONTAINER:
|
||||
return OsGetIpcContainerID(container->ipcContainer);
|
||||
#endif
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
case TIME_CONTAINER:
|
||||
return OsGetTimeContainerID(container->timeContainer);
|
||||
case TIME_CHILD_CONTAINER:
|
||||
return OsGetTimeContainerID(container->timeForChildContainer);
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return OS_INVALID_VALUE;
|
||||
}
|
||||
|
||||
STATIC VOID UnshareDeinitContainerCommon(UINT32 flags, Container *container)
|
||||
{
|
||||
#ifdef LOSCFG_UTS_CONTAINER
|
||||
if ((flags & CLONE_NEWUTS) != 0) {
|
||||
OsUtsContainersDestroy(container);
|
||||
}
|
||||
#endif
|
||||
#ifdef LOSCFG_MNT_CONTAINER
|
||||
if ((flags & CLONE_NEWNS) != 0) {
|
||||
OsMntContainersDestroy(container);
|
||||
}
|
||||
#endif
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
if ((flags & CLONE_NEWIPC) != 0) {
|
||||
OsIpcContainersDestroy(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;
|
||||
}
|
||||
#endif
|
||||
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, container);
|
||||
}
|
||||
|
||||
STATIC UINT32 CreateNewContainers(UINT32 flags, LosProcessCB *curr, Container *newContainer)
|
||||
{
|
||||
UINT32 ret = LOS_OK;
|
||||
#ifdef LOSCFG_PID_CONTAINER
|
||||
ret = OsUnsharePidContainer(flags, curr, newContainer);
|
||||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#ifdef LOSCFG_UTS_CONTAINER
|
||||
ret = OsUnshareUtsContainer(flags, curr, newContainer);
|
||||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#ifdef LOSCFG_MNT_CONTAINER
|
||||
ret = OsUnshareMntContainer(flags, curr, newContainer);
|
||||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
ret = OsUnshareIpcContainer(flags, curr, newContainer);
|
||||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
ret = OsUnshareTimeContainer(flags, curr, newContainer);
|
||||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
INT32 OsUnshare(UINT32 flags)
|
||||
{
|
||||
UINT32 ret;
|
||||
UINT32 intSave;
|
||||
LosProcessCB *curr = OsCurrProcessGet();
|
||||
Container *oldContainer = curr->container;
|
||||
if (!(flags & (CLONE_NEWPID | CLONE_NEWTIME | CLONE_NEWUTS | CLONE_NEWNS | CLONE_NEWIPC))) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
Container *newContainer = CreateContainer();
|
||||
if (newContainer == NULL) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
ret = CreateNewContainers(flags, curr, newContainer);
|
||||
if (ret != LOS_OK) {
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
oldContainer = curr->container;
|
||||
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);
|
||||
return LOS_OK;
|
||||
|
||||
EXIT:
|
||||
UnshareDeinitContainer(flags, newContainer);
|
||||
return -ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
189
kernel/base/container/los_ipc_container.c
Normal file
189
kernel/base/container/los_ipc_container.c
Normal file
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
#include "los_ipc_container_pri.h"
|
||||
#include "los_config.h"
|
||||
#include "los_queue_pri.h"
|
||||
#include "los_vm_shm_pri.h"
|
||||
#include "los_process_pri.h"
|
||||
#include "vnode.h"
|
||||
#include "proc_fs.h"
|
||||
#include "pthread.h"
|
||||
|
||||
#define IPC_INIT_NUM 3
|
||||
|
||||
STATIC UINT32 g_currentIpcContainerNum = 0;
|
||||
|
||||
STATIC IpcContainer *CreateNewIpcContainer(IpcContainer *parent)
|
||||
{
|
||||
pthread_mutexattr_t attr;
|
||||
UINT32 size = sizeof(IpcContainer);
|
||||
IpcContainer *ipcContainer = (IpcContainer *)LOS_MemAlloc(m_aucSysMem1, size);
|
||||
if (ipcContainer == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
(VOID)memset_s(ipcContainer, size, 0, size);
|
||||
|
||||
ipcContainer->allQueue = OsAllQueueCBInit(&ipcContainer->freeQueueList);
|
||||
if (ipcContainer->allQueue == NULL) {
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, ipcContainer);
|
||||
return NULL;
|
||||
}
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
|
||||
pthread_mutex_init(&ipcContainer->mqueueMutex, &attr);
|
||||
|
||||
ipcContainer->shmSegs = OsShmCBInit(&ipcContainer->sysvShmMux, &ipcContainer->shmInfo,
|
||||
&ipcContainer->shmUsedPageCount);
|
||||
if (ipcContainer->shmSegs == NULL) {
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, ipcContainer->allQueue);
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, ipcContainer);
|
||||
return NULL;
|
||||
}
|
||||
ipcContainer->containerID = OsAllocContainerID();
|
||||
|
||||
if (parent != NULL) {
|
||||
LOS_AtomicSet(&ipcContainer->rc, 1);
|
||||
} else {
|
||||
LOS_AtomicSet(&ipcContainer->rc, 3); /* 3: Three system processes */
|
||||
}
|
||||
return ipcContainer;
|
||||
}
|
||||
|
||||
STATIC UINT32 CreateIpcContainer(LosProcessCB *child, LosProcessCB *parent)
|
||||
{
|
||||
UINT32 intSave;
|
||||
IpcContainer *parentContainer = parent->container->ipcContainer;
|
||||
IpcContainer *newIpcContainer = CreateNewIpcContainer(parentContainer);
|
||||
if (newIpcContainer == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
g_currentIpcContainerNum++;
|
||||
child->container->ipcContainer = newIpcContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
UINT32 OsInitRootIpcContainer(IpcContainer **ipcContainer)
|
||||
{
|
||||
UINT32 intSave;
|
||||
IpcContainer *newIpcContainer = CreateNewIpcContainer(NULL);
|
||||
if (newIpcContainer == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
g_currentIpcContainerNum++;
|
||||
*ipcContainer = newIpcContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
UINT32 OsCopyIpcContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent)
|
||||
{
|
||||
UINT32 intSave;
|
||||
IpcContainer *currIpcContainer = parent->container->ipcContainer;
|
||||
|
||||
if (!(flags & CLONE_NEWIPC)) {
|
||||
SCHEDULER_LOCK(intSave);
|
||||
LOS_AtomicInc(&currIpcContainer->rc);
|
||||
child->container->ipcContainer = currIpcContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
return CreateIpcContainer(child, parent);
|
||||
}
|
||||
|
||||
UINT32 OsUnshareIpcContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
|
||||
{
|
||||
UINT32 intSave;
|
||||
IpcContainer *parentContainer = curr->container->ipcContainer;
|
||||
|
||||
if (!(flags & CLONE_NEWIPC)) {
|
||||
SCHEDULER_LOCK(intSave);
|
||||
newContainer->ipcContainer = parentContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
IpcContainer *ipcContainer = CreateNewIpcContainer(parentContainer);
|
||||
if (ipcContainer == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
newContainer->ipcContainer = ipcContainer;
|
||||
g_currentIpcContainerNum++;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
VOID OsIpcContainersDestroy(Container *container)
|
||||
{
|
||||
UINT32 intSave;
|
||||
if (container == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return;
|
||||
}
|
||||
|
||||
UINT32 OsGetIpcContainerID(IpcContainer *ipcContainer)
|
||||
{
|
||||
if (ipcContainer == NULL) {
|
||||
return OS_INVALID_VALUE;
|
||||
}
|
||||
|
||||
return ipcContainer->containerID;
|
||||
}
|
||||
|
||||
IpcContainer *OsGetCurrIpcContainer(VOID)
|
||||
{
|
||||
return OsCurrProcessGet()->container->ipcContainer;
|
||||
}
|
||||
#endif
|
||||
@@ -44,20 +44,50 @@ LIST_HEAD *GetContainerMntList(VOID)
|
||||
return &OsCurrProcessGet()->container->mntContainer->mountList;
|
||||
}
|
||||
|
||||
STATIC UINT32 CreateMntContainer(MntContainer **newMntContainer)
|
||||
STATIC MntContainer *CreateNewMntContainer(MntContainer *parent)
|
||||
{
|
||||
UINT32 intSave;
|
||||
MntContainer *mntContainer = (MntContainer *)LOS_MemAlloc(m_aucSysMem1, sizeof(MntContainer));
|
||||
if (mntContainer == NULL) {
|
||||
return ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
mntContainer->containerID = OsAllocContainerID();
|
||||
LOS_AtomicSet(&mntContainer->rc, 1);
|
||||
LOS_ListInit(&mntContainer->mountList);
|
||||
|
||||
if (parent != NULL) {
|
||||
LOS_AtomicSet(&mntContainer->rc, 1);
|
||||
} else {
|
||||
LOS_AtomicSet(&mntContainer->rc, 3); /* 3: Three system processes */
|
||||
}
|
||||
return mntContainer;
|
||||
}
|
||||
|
||||
STATIC UINT32 CreateMntContainer(LosProcessCB *child, LosProcessCB *parent)
|
||||
{
|
||||
UINT32 intSave;
|
||||
MntContainer *parentContainer = parent->container->mntContainer;
|
||||
MntContainer *newMntContainer = CreateNewMntContainer(parentContainer);
|
||||
if (newMntContainer == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
g_currentMntContainerNum++;
|
||||
*newMntContainer = mntContainer;
|
||||
child->container->mntContainer = newMntContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
UINT32 OsInitRootMntContainer(MntContainer **mntContainer)
|
||||
{
|
||||
UINT32 intSave;
|
||||
MntContainer *newMntContainer = CreateNewMntContainer(NULL);
|
||||
if (newMntContainer == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
g_currentMntContainerNum++;
|
||||
*mntContainer = newMntContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
@@ -94,7 +124,7 @@ UINT32 OsCopyMntContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *pare
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
ret = CreateMntContainer(&child->container->mntContainer);
|
||||
ret = CreateMntContainer(child, parent);
|
||||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
}
|
||||
@@ -102,6 +132,37 @@ UINT32 OsCopyMntContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *pare
|
||||
return CopyMountList(currMntContainer, child->container->mntContainer);
|
||||
}
|
||||
|
||||
UINT32 OsUnshareMntContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
|
||||
{
|
||||
UINT32 intSave;
|
||||
UINT32 ret;
|
||||
MntContainer *parentContainer = curr->container->mntContainer;
|
||||
|
||||
if (!(flags & CLONE_NEWNS)) {
|
||||
SCHEDULER_LOCK(intSave);
|
||||
newContainer->mntContainer = parentContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
MntContainer *mntContainer = CreateNewMntContainer(parentContainer);
|
||||
if (mntContainer == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
ret = CopyMountList(parentContainer, mntContainer);
|
||||
if (ret != LOS_OK) {
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, mntContainer);
|
||||
return ret;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
newContainer->mntContainer = mntContainer;
|
||||
g_currentMntContainerNum++;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
STATIC VOID FreeMountList(LIST_HEAD *mountList)
|
||||
{
|
||||
struct Mount *mnt = NULL;
|
||||
@@ -126,21 +187,22 @@ STATIC VOID FreeMountList(LIST_HEAD *mountList)
|
||||
return;
|
||||
}
|
||||
|
||||
VOID OsMntContainersDestroy(LosProcessCB *curr)
|
||||
VOID OsMntContainersDestroy(Container *container)
|
||||
{
|
||||
UINT32 intSave;
|
||||
if (curr->container == NULL) {
|
||||
if (container == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
MntContainer *mntContainer = curr->container->mntContainer;
|
||||
MntContainer *mntContainer = container->mntContainer;
|
||||
if (mntContainer != NULL) {
|
||||
if (LOS_AtomicRead(&mntContainer->rc) == 0) {
|
||||
LOS_AtomicDec(&mntContainer->rc);
|
||||
if (LOS_AtomicRead(&mntContainer->rc) <= 0) {
|
||||
g_currentMntContainerNum--;
|
||||
FreeMountList(&mntContainer->mountList);
|
||||
curr->container->mntContainer = NULL;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
FreeMountList(&mntContainer->mountList);
|
||||
container->mntContainer = NULL;
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, mntContainer);
|
||||
return;
|
||||
}
|
||||
@@ -157,9 +219,4 @@ UINT32 OsGetMntContainerID(MntContainer *mntContainer)
|
||||
|
||||
return mntContainer->containerID;
|
||||
}
|
||||
|
||||
UINT32 OsInitRootMntContainer(MntContainer **mntContainer)
|
||||
{
|
||||
return CreateMntContainer(mntContainer);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -274,10 +274,15 @@ STATIC UINT32 CreatePidContainer(LosProcessCB *child, LosProcessCB *parent)
|
||||
{
|
||||
UINT32 intSave;
|
||||
UINT32 ret;
|
||||
PidContainer *newPidContainer = NULL;
|
||||
PidContainer *parentContainer = parent->container->pidContainer;
|
||||
PidContainer *newPidContainer = CreateNewPidContainer(parentContainer);
|
||||
if (newPidContainer == NULL) {
|
||||
return ENOMEM;
|
||||
if (parentContainer == parent->container->pidForChildContainer) {
|
||||
newPidContainer = CreateNewPidContainer(parentContainer);
|
||||
if (newPidContainer == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
} else {
|
||||
newPidContainer = parent->container->pidForChildContainer;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
@@ -289,6 +294,7 @@ STATIC UINT32 CreatePidContainer(LosProcessCB *child, LosProcessCB *parent)
|
||||
|
||||
g_currentPidContainerNum++;
|
||||
child->container->pidContainer = newPidContainer;
|
||||
child->container->pidForChildContainer = newPidContainer;
|
||||
ret = OsAllocSpecifiedVpidUnsafe(OS_USER_ROOT_PROCESS_ID, child, parent);
|
||||
if (ret == OS_INVALID_VALUE) {
|
||||
g_currentPidContainerNum--;
|
||||
@@ -311,10 +317,15 @@ VOID OsPidContainersDestroy(LosProcessCB *curr)
|
||||
PidContainer *pidContainer = curr->container->pidContainer;
|
||||
if (pidContainer != NULL) {
|
||||
FreeVpid(curr);
|
||||
if (LOS_AtomicRead(&pidContainer->rc) == 0) {
|
||||
if (LOS_AtomicRead(&pidContainer->rc) <= 0) {
|
||||
g_currentPidContainerNum--;
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, pidContainer);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,9 +341,10 @@ UINT32 OsCopyPidContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *pare
|
||||
UINT32 ret;
|
||||
UINT32 intSave;
|
||||
|
||||
if (!(flags & CLONE_NEWPID)) {
|
||||
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) {
|
||||
@@ -357,6 +369,41 @@ UINT32 OsCopyPidContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *pare
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
UINT32 OsUnsharePidContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
|
||||
{
|
||||
UINT32 intSave;
|
||||
if (!(flags & CLONE_NEWPID)) {
|
||||
SCHEDULER_LOCK(intSave);
|
||||
newContainer->pidContainer = curr->container->pidContainer;
|
||||
newContainer->pidForChildContainer = curr->container->pidContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
PidContainer *pidForChild = CreateNewPidContainer(curr->container->pidContainer);
|
||||
if (pidForChild == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
if (curr->container->pidContainer != curr->container->pidForChildContainer) {
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, pidForChild);
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
if (pidForChild->level >= PID_CONTAINER_LEVEL_LIMIT) {
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, pidForChild);
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
newContainer->pidContainer = curr->container->pidContainer;
|
||||
newContainer->pidForChildContainer = pidForChild;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
UINT32 OsInitRootPidContainer(PidContainer **pidContainer)
|
||||
{
|
||||
UINT32 intSave;
|
||||
|
||||
227
kernel/base/container/los_time_container.c
Normal file
227
kernel/base/container/los_time_container.c
Normal file
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "los_time_container_pri.h"
|
||||
#include "los_process_pri.h"
|
||||
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
STATIC UINT32 g_currentTimeContainerNum;
|
||||
|
||||
STATIC TimeContainer *CreateNewTimeContainer(TimeContainer *parent)
|
||||
{
|
||||
UINT32 size = sizeof(TimeContainer);
|
||||
TimeContainer *timeContainer = (TimeContainer *)LOS_MemAlloc(m_aucSysMem1, size);
|
||||
if (timeContainer == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
(VOID)memset_s(timeContainer, size, 0, size);
|
||||
|
||||
timeContainer->containerID = OsAllocContainerID();
|
||||
if (parent != NULL) {
|
||||
timeContainer->frozenOffsets = FALSE;
|
||||
LOS_AtomicSet(&timeContainer->rc, 1);
|
||||
} else {
|
||||
timeContainer->frozenOffsets = TRUE;
|
||||
LOS_AtomicSet(&timeContainer->rc, 3); /* 3: Three system processes */
|
||||
}
|
||||
return timeContainer;
|
||||
}
|
||||
|
||||
STATIC UINT32 CreateTimeContainer(LosProcessCB *child, LosProcessCB *parent)
|
||||
{
|
||||
UINT32 intSave;
|
||||
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;
|
||||
}
|
||||
child->container->timeContainer = newTimeContainer;
|
||||
child->container->timeForChildContainer = newTimeContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
UINT32 OsInitRootTimeContainer(TimeContainer **timeContainer)
|
||||
{
|
||||
UINT32 intSave;
|
||||
TimeContainer *newTimeContainer = CreateNewTimeContainer(NULL);
|
||||
if (newTimeContainer == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
*timeContainer = newTimeContainer;
|
||||
g_currentTimeContainerNum++;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
UINT32 OsCopyTimeContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent)
|
||||
{
|
||||
UINT32 intSave;
|
||||
TimeContainer *currTimeContainer = parent->container->timeContainer;
|
||||
|
||||
if (!(flags & CLONE_NEWTIME) && (currTimeContainer == parent->container->timeForChildContainer)) {
|
||||
SCHEDULER_LOCK(intSave);
|
||||
LOS_AtomicInc(&currTimeContainer->rc);
|
||||
child->container->timeContainer = currTimeContainer;
|
||||
child->container->timeForChildContainer = currTimeContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
return CreateTimeContainer(child, parent);
|
||||
}
|
||||
|
||||
UINT32 OsUnshareTimeContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
|
||||
{
|
||||
UINT32 intSave;
|
||||
if (!(flags & CLONE_NEWTIME)) {
|
||||
SCHEDULER_LOCK(intSave);
|
||||
newContainer->timeContainer = curr->container->timeContainer;
|
||||
newContainer->timeForChildContainer = curr->container->timeForChildContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
TimeContainer *timeForChild = CreateNewTimeContainer(curr->container->timeContainer);
|
||||
if (timeForChild == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
if (curr->container->timeContainer != curr->container->timeForChildContainer) {
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, timeForChild);
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
newContainer->timeContainer = curr->container->timeContainer;
|
||||
newContainer->timeForChildContainer = timeForChild;
|
||||
LOS_AtomicSet(&timeForChild->rc, 0);
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
VOID OsTimeContainersDestroy(LosProcessCB *curr)
|
||||
{
|
||||
UINT32 intSave;
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
curr->container->timeForChildContainer = NULL;
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, timeContainer);
|
||||
return;
|
||||
}
|
||||
}
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return;
|
||||
}
|
||||
|
||||
UINT32 OsGetTimeContainerID(TimeContainer *timeContainer)
|
||||
{
|
||||
if (timeContainer == NULL) {
|
||||
return OS_INVALID_VALUE;
|
||||
}
|
||||
|
||||
return timeContainer->containerID;
|
||||
}
|
||||
|
||||
TimeContainer *OsGetCurrTimeContainer(VOID)
|
||||
{
|
||||
return OsCurrProcessGet()->container->timeContainer;
|
||||
}
|
||||
|
||||
UINT32 OsGetTimeContainerMonotonic(LosProcessCB *processCB, struct timespec64 *offsets)
|
||||
{
|
||||
if ((processCB == NULL) || (offsets == NULL)) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
if (OsProcessIsInactive(processCB)) {
|
||||
return ESRCH;
|
||||
}
|
||||
|
||||
TimeContainer *timeContainer = processCB->container->timeForChildContainer;
|
||||
(VOID)memcpy_s(offsets, sizeof(struct timespec64), &timeContainer->monotonic, sizeof(struct timespec64));
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
UINT32 OsSetTimeContainerMonotonic(LosProcessCB *processCB, struct timespec64 *offsets)
|
||||
{
|
||||
if ((processCB == NULL) || (offsets == NULL)) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
if (OsProcessIsInactive(processCB)) {
|
||||
return ESRCH;
|
||||
}
|
||||
|
||||
TimeContainer *timeContainer = processCB->container->timeForChildContainer;
|
||||
if (timeContainer->frozenOffsets) {
|
||||
return EACCES;
|
||||
}
|
||||
|
||||
timeContainer->monotonic.tv_sec = offsets->tv_sec;
|
||||
timeContainer->monotonic.tv_nsec = offsets->tv_nsec;
|
||||
return LOS_OK;
|
||||
}
|
||||
#endif
|
||||
@@ -36,81 +36,9 @@
|
||||
|
||||
STATIC UINT32 g_currentUtsContainerNum;
|
||||
|
||||
STATIC UINT32 CreateUtsContainer(UtsContainer **newUtsContainer)
|
||||
{
|
||||
UINT32 intSave;
|
||||
UINT32 size = sizeof(UtsContainer);
|
||||
UtsContainer *utsContainer = LOS_MemAlloc(m_aucSysMem1, size);
|
||||
if (utsContainer == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
(VOID)memset_s(utsContainer, sizeof(UtsContainer), 0, sizeof(UtsContainer));
|
||||
|
||||
utsContainer->containerID = OsAllocContainerID();
|
||||
LOS_AtomicSet(&utsContainer->rc, 1);
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
g_currentUtsContainerNum += 1;
|
||||
*newUtsContainer = utsContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
UINT32 OsCopyUtsContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent)
|
||||
{
|
||||
UINT32 intSave;
|
||||
UINT32 ret;
|
||||
UtsContainer *newUtsContainer = NULL;
|
||||
UtsContainer *currUtsContainer = parent->container->utsContainer;
|
||||
|
||||
if (!(flags & CLONE_NEWUTS)) {
|
||||
SCHEDULER_LOCK(intSave);
|
||||
LOS_AtomicInc(&currUtsContainer->rc);
|
||||
child->container->utsContainer = currUtsContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
ret = CreateUtsContainer(&newUtsContainer);
|
||||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
(VOID)memcpy_s(&newUtsContainer->utsName, sizeof(newUtsContainer->utsName),
|
||||
&currUtsContainer->utsName, sizeof(currUtsContainer->utsName));
|
||||
child->container->utsContainer = newUtsContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
VOID OsUtsContainersDestroy(LosProcessCB *curr)
|
||||
{
|
||||
UINT32 intSave;
|
||||
if (curr->container == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
UtsContainer *utsContainer = curr->container->utsContainer;
|
||||
if (utsContainer != NULL) {
|
||||
if (LOS_AtomicRead(&utsContainer->rc) == 0) {
|
||||
g_currentUtsContainerNum--;
|
||||
curr->container->utsContainer = NULL;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, utsContainer);
|
||||
return;
|
||||
}
|
||||
}
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return;
|
||||
}
|
||||
|
||||
STATIC UINT32 InitUtsContainer(struct utsname *name)
|
||||
{
|
||||
UINT32 ret;
|
||||
|
||||
ret = sprintf_s(name->sysname, sizeof(name->sysname), "%s", KERNEL_NAME);
|
||||
UINT32 ret = sprintf_s(name->sysname, sizeof(name->sysname), "%s", KERNEL_NAME);
|
||||
if (ret < 0) {
|
||||
return LOS_NOK;
|
||||
}
|
||||
@@ -137,14 +65,126 @@ STATIC UINT32 InitUtsContainer(struct utsname *name)
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
UINT32 OsInitRootUtsContainer(UtsContainer **utsContainer)
|
||||
STATIC UtsContainer *CreateNewUtsContainer(UtsContainer *parent)
|
||||
{
|
||||
UINT32 ret = CreateUtsContainer(utsContainer);
|
||||
UINT32 ret;
|
||||
UINT32 size = sizeof(UtsContainer);
|
||||
UtsContainer *utsContainer = (UtsContainer *)LOS_MemAlloc(m_aucSysMem1, size);
|
||||
if (utsContainer == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
(VOID)memset_s(utsContainer, sizeof(UtsContainer), 0, sizeof(UtsContainer));
|
||||
|
||||
utsContainer->containerID = OsAllocContainerID();
|
||||
if (parent != NULL) {
|
||||
LOS_AtomicSet(&utsContainer->rc, 1);
|
||||
return utsContainer;
|
||||
}
|
||||
LOS_AtomicSet(&utsContainer->rc, 3); /* 3: Three system processes */
|
||||
ret = InitUtsContainer(&utsContainer->utsName);
|
||||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, utsContainer);
|
||||
return NULL;
|
||||
}
|
||||
return utsContainer;
|
||||
}
|
||||
|
||||
STATIC UINT32 CreateUtsContainer(LosProcessCB *child, LosProcessCB *parent)
|
||||
{
|
||||
UINT32 intSave;
|
||||
UtsContainer *parentContainer = parent->container->utsContainer;
|
||||
UtsContainer *newUtsContainer = CreateNewUtsContainer(parentContainer);
|
||||
if (newUtsContainer == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
return InitUtsContainer(&(*utsContainer)->utsName);
|
||||
SCHEDULER_LOCK(intSave);
|
||||
g_currentUtsContainerNum++;
|
||||
(VOID)memcpy_s(&newUtsContainer->utsName, sizeof(newUtsContainer->utsName),
|
||||
&parentContainer->utsName, sizeof(parentContainer->utsName));
|
||||
child->container->utsContainer = newUtsContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
UINT32 OsInitRootUtsContainer(UtsContainer **utsContainer)
|
||||
{
|
||||
UINT32 intSave;
|
||||
UtsContainer *newUtsContainer = CreateNewUtsContainer(NULL);
|
||||
if (newUtsContainer == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
g_currentUtsContainerNum++;
|
||||
*utsContainer = newUtsContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
UINT32 OsCopyUtsContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent)
|
||||
{
|
||||
UINT32 intSave;
|
||||
UtsContainer *currUtsContainer = parent->container->utsContainer;
|
||||
|
||||
if (!(flags & CLONE_NEWUTS)) {
|
||||
SCHEDULER_LOCK(intSave);
|
||||
LOS_AtomicInc(&currUtsContainer->rc);
|
||||
child->container->utsContainer = currUtsContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
return CreateUtsContainer(child, parent);
|
||||
}
|
||||
|
||||
UINT32 OsUnshareUtsContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
|
||||
{
|
||||
UINT32 intSave;
|
||||
UtsContainer *parentContainer = curr->container->utsContainer;
|
||||
|
||||
if (!(flags & CLONE_NEWUTS)) {
|
||||
SCHEDULER_LOCK(intSave);
|
||||
newContainer->utsContainer = parentContainer;
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
UtsContainer *utsContainer = CreateNewUtsContainer(parentContainer);
|
||||
if (utsContainer == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
newContainer->utsContainer = utsContainer;
|
||||
g_currentUtsContainerNum++;
|
||||
(VOID)memcpy_s(&utsContainer->utsName, sizeof(utsContainer->utsName),
|
||||
&parentContainer->utsName, sizeof(parentContainer->utsName));
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
VOID OsUtsContainersDestroy(Container *container)
|
||||
{
|
||||
UINT32 intSave;
|
||||
if (container == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
return;
|
||||
}
|
||||
|
||||
struct utsname *OsGetCurrUtsName(VOID)
|
||||
|
||||
@@ -398,13 +398,6 @@ LITE_OS_SEC_TEXT VOID OsProcessResourcesToFree(LosProcessCB *processCB)
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef LOSCFG_FS_VFS
|
||||
if (OsProcessIsUserMode(processCB)) {
|
||||
delete_files(processCB->files);
|
||||
}
|
||||
processCB->files = NULL;
|
||||
#endif
|
||||
|
||||
#ifdef LOSCFG_SECURITY_CAPABILITY
|
||||
if (processCB->user != NULL) {
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, processCB->user);
|
||||
@@ -446,6 +439,13 @@ LITE_OS_SEC_TEXT VOID OsProcessResourcesToFree(LosProcessCB *processCB)
|
||||
OsContainersDestroy(processCB);
|
||||
#endif
|
||||
|
||||
#ifdef LOSCFG_FS_VFS
|
||||
if (OsProcessIsUserMode(processCB)) {
|
||||
delete_files(processCB->files);
|
||||
}
|
||||
processCB->files = NULL;
|
||||
#endif
|
||||
|
||||
if (processCB->resourceLimit != NULL) {
|
||||
(VOID)LOS_MemFree((VOID *)m_aucSysMem0, processCB->resourceLimit);
|
||||
processCB->resourceLimit = NULL;
|
||||
@@ -1893,7 +1893,7 @@ STATIC UINT32 OsCopyMM(UINT32 flags, LosProcessCB *childProcessCB, LosProcessCB
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
status = LOS_VmSpaceClone(runProcessCB->vmSpace, childProcessCB->vmSpace);
|
||||
status = LOS_VmSpaceClone(flags, runProcessCB->vmSpace, childProcessCB->vmSpace);
|
||||
if (status != LOS_OK) {
|
||||
return LOS_ENOMEM;
|
||||
}
|
||||
@@ -1906,11 +1906,20 @@ STATIC UINT32 OsCopyFile(UINT32 flags, LosProcessCB *childProcessCB, LosProcessC
|
||||
if (flags & CLONE_FILES) {
|
||||
childProcessCB->files = runProcessCB->files;
|
||||
} else {
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
if (flags & CLONE_NEWIPC) {
|
||||
OsCurrTaskGet()->cloneIpc = TRUE;
|
||||
}
|
||||
#endif
|
||||
childProcessCB->files = dup_fd(runProcessCB->files);
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
OsCurrTaskGet()->cloneIpc = FALSE;
|
||||
#endif
|
||||
}
|
||||
if (childProcessCB->files == NULL) {
|
||||
return LOS_ENOMEM;
|
||||
}
|
||||
|
||||
#ifdef LOSCFG_PROC_PROCESS_DIR
|
||||
INT32 ret = ProcCreateProcessDir(OsGetRootPid(childProcessCB), (UINTPTR)childProcessCB);
|
||||
if (ret < 0) {
|
||||
@@ -1993,7 +2002,6 @@ STATIC UINT32 OsCopyProcessResources(UINT32 flags, LosProcessCB *child, LosProce
|
||||
#ifdef LOSCFG_SECURITY_CAPABILITY
|
||||
OsCopyCapability(run, child);
|
||||
#endif
|
||||
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
@@ -2061,6 +2069,15 @@ LITE_OS_SEC_TEXT INT32 OsClone(UINT32 flags, UINTPTR sp, UINT32 size)
|
||||
#ifdef LOSCFG_MNT_CONTAINER
|
||||
cloneFlag |= CLONE_NEWNS;
|
||||
#endif
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
cloneFlag |= CLONE_NEWIPC;
|
||||
if (((flags & CLONE_NEWIPC) != 0) && ((flags & CLONE_FILES) != 0)) {
|
||||
return -LOS_EINVAL;
|
||||
}
|
||||
#endif
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
cloneFlag |= CLONE_NEWTIME;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if (flags & (~cloneFlag)) {
|
||||
|
||||
@@ -42,18 +42,29 @@
|
||||
#ifdef LOSCFG_MNT_CONTAINER
|
||||
#include "los_mnt_container_pri.h"
|
||||
#endif
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
#include "los_ipc_container_pri.h"
|
||||
#endif
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
#include "los_time_container_pri.h"
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
CONTAINER = 0,
|
||||
PID_CONTAINER,
|
||||
PID_CHILD_CONTAINER,
|
||||
UTS_CONTAINER,
|
||||
MNT_CONTAINER,
|
||||
IPC_CONTAINER,
|
||||
TIME_CONTAINER,
|
||||
TIME_CHILD_CONTAINER,
|
||||
} ContainerType;
|
||||
|
||||
typedef struct Container {
|
||||
Atomic rc;
|
||||
#ifdef LOSCFG_PID_CONTAINER
|
||||
struct PidContainer *pidContainer;
|
||||
struct PidContainer *pidForChildContainer;
|
||||
#endif
|
||||
#ifdef LOSCFG_UTS_CONTAINER
|
||||
struct UtsContainer *utsContainer;
|
||||
@@ -61,6 +72,13 @@ typedef struct Container {
|
||||
#ifdef LOSCFG_MNT_CONTAINER
|
||||
struct MntContainer *mntContainer;
|
||||
#endif
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
struct IpcContainer *ipcContainer;
|
||||
#endif
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
struct TimeContainer *timeContainer;
|
||||
struct TimeContainer *timeForChildContainer;
|
||||
#endif
|
||||
} Container;
|
||||
|
||||
VOID OsContainerInitSystemProcess(LosProcessCB *processCB);
|
||||
@@ -74,5 +92,8 @@ VOID OsContainersDestroy(LosProcessCB *processCB);
|
||||
UINT32 OsAllocContainerID(VOID);
|
||||
|
||||
UINT32 OsGetContainerID(Container *container, ContainerType type);
|
||||
|
||||
INT32 OsUnshare(UINT32 flags);
|
||||
|
||||
#endif
|
||||
#endif /* _LOS_CONTAINER_PRI_H */
|
||||
|
||||
94
kernel/base/include/los_ipc_container_pri.h
Normal file
94
kernel/base/include/los_ipc_container_pri.h
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef _LOS_IPC_CONTAINER_PRI_H
|
||||
#define _LOS_IPC_CONTAINER_PRI_H
|
||||
|
||||
#include "los_atomic.h"
|
||||
#include "los_list.h"
|
||||
#include "mqueue.h"
|
||||
#include "fs/file.h"
|
||||
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
struct shmIDSource;
|
||||
struct Container;
|
||||
typedef struct TagQueueCB LosQueueCB;
|
||||
typedef struct OsMux LosMux;
|
||||
typedef LosMux pthread_mutex_t;
|
||||
typedef struct ProcessCB LosProcessCB;
|
||||
|
||||
typedef struct IpcContainer {
|
||||
Atomic rc;
|
||||
LosQueueCB *allQueue;
|
||||
LOS_DL_LIST freeQueueList;
|
||||
fd_set queueFdSet;
|
||||
struct mqarray queueTable[LOSCFG_BASE_IPC_QUEUE_LIMIT];
|
||||
pthread_mutex_t mqueueMutex;
|
||||
struct mqpersonal *mqPrivBuf[MAX_MQ_FD];
|
||||
struct shminfo shmInfo;
|
||||
LosMux sysvShmMux;
|
||||
struct shmIDSource *shmSegs;
|
||||
UINT32 shmUsedPageCount;
|
||||
UINT32 containerID;
|
||||
} IpcContainer;
|
||||
|
||||
UINT32 OsInitRootIpcContainer(IpcContainer **ipcContainer);
|
||||
|
||||
UINT32 OsCopyIpcContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent);
|
||||
|
||||
UINT32 OsUnshareIpcContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
|
||||
|
||||
VOID OsIpcContainersDestroy(struct Container *container);
|
||||
|
||||
UINT32 OsGetIpcContainerID(IpcContainer *ipcContainer);
|
||||
|
||||
IpcContainer *OsGetCurrIpcContainer(VOID);
|
||||
|
||||
#define IPC_ALL_QUEUE (OsGetCurrIpcContainer()->allQueue)
|
||||
|
||||
#define FREE_QUEUE_LIST (OsGetCurrIpcContainer()->freeQueueList)
|
||||
|
||||
#define IPC_QUEUE_FD_SET (OsGetCurrIpcContainer()->queueFdSet)
|
||||
|
||||
#define IPC_QUEUE_TABLE (OsGetCurrIpcContainer()->queueTable)
|
||||
|
||||
#define IPC_QUEUE_MUTEX (OsGetCurrIpcContainer()->mqueueMutex)
|
||||
|
||||
#define IPC_QUEUE_MQ_PRIV_BUF (OsGetCurrIpcContainer()->mqPrivBuf)
|
||||
|
||||
#define IPC_SHM_INFO (OsGetCurrIpcContainer()->shmInfo)
|
||||
|
||||
#define IPC_SHM_SYS_VSHM_MUTEX (OsGetCurrIpcContainer()->sysvShmMux)
|
||||
|
||||
#define IPC_SHM_SEGS (OsGetCurrIpcContainer()->shmSegs)
|
||||
|
||||
#define IPC_SHM_USED_PAGE_COUNT (OsGetCurrIpcContainer()->shmUsedPageCount)
|
||||
|
||||
#endif
|
||||
#endif /* _LOS_IPC_CONTAINER_PRI_H */
|
||||
@@ -39,6 +39,7 @@
|
||||
|
||||
#ifdef LOSCFG_MNT_CONTAINER
|
||||
typedef struct ProcessCB LosProcessCB;
|
||||
struct Container;
|
||||
|
||||
typedef struct MntContainer {
|
||||
Atomic rc;
|
||||
@@ -52,7 +53,9 @@ UINT32 OsInitRootMntContainer(MntContainer **mntContainer);
|
||||
|
||||
UINT32 OsCopyMntContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent);
|
||||
|
||||
VOID OsMntContainersDestroy(LosProcessCB *curr);
|
||||
UINT32 OsUnshareMntContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
|
||||
|
||||
VOID OsMntContainersDestroy(struct Container *container);
|
||||
|
||||
UINT32 OsGetMntContainerID(MntContainer *mntContainer);
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
typedef struct TagTaskCB LosTaskCB;
|
||||
typedef struct ProcessCB LosProcessCB;
|
||||
struct ProcessGroup;
|
||||
struct Container;
|
||||
|
||||
typedef struct {
|
||||
UINT32 vid; /* Virtual ID */
|
||||
@@ -75,6 +76,8 @@ VOID OsPidContainersDestroy(LosProcessCB *curr);
|
||||
|
||||
UINT32 OsCopyPidContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID);
|
||||
|
||||
UINT32 OsUnsharePidContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
|
||||
|
||||
UINT32 OsInitRootPidContainer(PidContainer **pidContainer);
|
||||
|
||||
LosProcessCB *OsGetPCBFromVpid(UINT32 vpid);
|
||||
|
||||
@@ -131,7 +131,7 @@ typedef struct ProcessCB {
|
||||
#endif
|
||||
struct rlimit *resourceLimit;
|
||||
#ifdef LOSCFG_KERNEL_CONTAINER
|
||||
struct Container *container;
|
||||
Container *container;
|
||||
#endif
|
||||
#ifdef LOSCFG_PROC_PROCESS_DIR
|
||||
struct ProcDirEntry *procDir;
|
||||
|
||||
@@ -65,7 +65,7 @@ typedef enum {
|
||||
* @ingroup los_queue
|
||||
* Queue information block structure
|
||||
*/
|
||||
typedef struct {
|
||||
typedef struct TagQueueCB {
|
||||
UINT8 *queueHandle; /**< Pointer to a queue handle */
|
||||
UINT16 queueState; /**< Queue state */
|
||||
UINT16 queueLen; /**< Queue length */
|
||||
@@ -108,6 +108,9 @@ typedef struct {
|
||||
* Queue information control block
|
||||
*/
|
||||
extern LosQueueCB *g_allQueue;
|
||||
#ifndef LOSCFG_IPC_CONTAINER
|
||||
#define IPC_ALL_QUEUE g_allQueue
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @ingroup los_queue
|
||||
@@ -137,7 +140,7 @@ extern LosQueueCB *g_allQueue;
|
||||
* Obtain a handle of the queue that has a specified ID.
|
||||
*
|
||||
*/
|
||||
#define GET_QUEUE_HANDLE(queueID) (((LosQueueCB *)g_allQueue) + GET_QUEUE_INDEX(queueID))
|
||||
#define GET_QUEUE_HANDLE(queueID) (((LosQueueCB *)IPC_ALL_QUEUE) + GET_QUEUE_INDEX(queueID))
|
||||
|
||||
/**
|
||||
* @ingroup los_queue
|
||||
@@ -196,6 +199,8 @@ extern VOID *OsQueueMailAlloc(UINT32 queueID, VOID *mailPool, UINT32 timeout);
|
||||
*/
|
||||
extern UINT32 OsQueueMailFree(UINT32 queueID, VOID *mailPool, VOID *mailMem);
|
||||
|
||||
extern LosQueueCB *OsAllQueueCBInit(LOS_DL_LIST *freeQueueList);
|
||||
|
||||
extern UINT32 OsQueueInit(VOID);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -428,6 +428,9 @@ typedef struct TagTaskCB {
|
||||
#ifdef LOSCFG_PID_CONTAINER
|
||||
PidContainer *pidContainer;
|
||||
#endif
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
BOOL cloneIpc;
|
||||
#endif
|
||||
} LosTaskCB;
|
||||
|
||||
STATIC INLINE BOOL OsTaskIsRunning(const LosTaskCB *taskCB)
|
||||
|
||||
66
kernel/base/include/los_time_container_pri.h
Normal file
66
kernel/base/include/los_time_container_pri.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _LOS_TIME_CONTAINER_PRI_H
|
||||
#define _LOS_TIME_CONTAINER_PRI_H
|
||||
#include "time.h"
|
||||
#include "los_atomic.h"
|
||||
|
||||
#ifdef LOSCFG_TIME_CONTAINER
|
||||
typedef struct ProcessCB LosProcessCB;
|
||||
struct Container;
|
||||
|
||||
typedef struct TimeContainer {
|
||||
Atomic rc;
|
||||
BOOL frozenOffsets;
|
||||
struct timespec64 monotonic;
|
||||
UINT32 containerID;
|
||||
} TimeContainer;
|
||||
|
||||
UINT32 OsInitRootTimeContainer(TimeContainer **timeContainer);
|
||||
|
||||
UINT32 OsCopyTimeContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent);
|
||||
|
||||
UINT32 OsUnshareTimeContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
|
||||
|
||||
VOID OsTimeContainersDestroy(LosProcessCB *curr);
|
||||
|
||||
UINT32 OsGetTimeContainerID(TimeContainer *timeContainer);
|
||||
|
||||
TimeContainer *OsGetCurrTimeContainer(VOID);
|
||||
|
||||
UINT32 OsGetTimeContainerMonotonic(LosProcessCB *processCB, struct timespec64 *offsets);
|
||||
|
||||
UINT32 OsSetTimeContainerMonotonic(LosProcessCB *processCB, struct timespec64 *offsets);
|
||||
|
||||
#define CLOCK_MONOTONIC_TIME_BASE (OsGetCurrTimeContainer()->monotonic)
|
||||
|
||||
#endif
|
||||
#endif /* _LOS_TIME_CONTAINER_PRI_H */
|
||||
@@ -38,6 +38,8 @@
|
||||
#ifdef LOSCFG_UTS_CONTAINER
|
||||
|
||||
typedef struct ProcessCB LosProcessCB;
|
||||
struct Container;
|
||||
|
||||
typedef struct UtsContainer {
|
||||
Atomic rc;
|
||||
UINT32 containerID;
|
||||
@@ -48,7 +50,9 @@ UINT32 OsInitRootUtsContainer(UtsContainer **utsContainer);
|
||||
|
||||
UINT32 OsCopyUtsContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent);
|
||||
|
||||
VOID OsUtsContainersDestroy(LosProcessCB *curr);
|
||||
UINT32 OsUnshareUtsContainer(UINTPTR flags, LosProcessCB *curr, struct Container *newContainer);
|
||||
|
||||
VOID OsUtsContainersDestroy(struct Container *container);
|
||||
|
||||
struct utsname *OsGetCurrUtsName(VOID);
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
@@ -290,7 +290,7 @@ STATUS_T LOS_VmSpaceFree(LosVmSpace *space);
|
||||
STATUS_T LOS_VaddrToPaddrMmap(LosVmSpace *space, VADDR_T vaddr, PADDR_T paddr, size_t len, UINT32 flags);
|
||||
BOOL OsUserVmSpaceInit(LosVmSpace *vmSpace, VADDR_T *virtTtb);
|
||||
LosVmSpace *OsCreateUserVmSpace(VOID);
|
||||
STATUS_T LOS_VmSpaceClone(LosVmSpace *oldVmSpace, LosVmSpace *newVmSpace);
|
||||
STATUS_T LOS_VmSpaceClone(UINT32 cloneFlags, LosVmSpace *oldVmSpace, LosVmSpace *newVmSpace);
|
||||
LosMux *OsGVmSpaceMuxGet(VOID);
|
||||
STATUS_T OsUnMMap(LosVmSpace *space, VADDR_T addr, size_t size);
|
||||
STATUS_T OsVmSpaceRegionFree(LosVmSpace *space);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
@@ -34,17 +34,35 @@
|
||||
|
||||
#include "los_typedef.h"
|
||||
#include "los_vm_map.h"
|
||||
|
||||
#include "los_process_pri.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
#endif /* __cplusplus */
|
||||
|
||||
/* The upper limit size of total shared memory is default 16M */
|
||||
#define SHM_MAX_PAGES 4096
|
||||
#define SHM_MAX (SHM_MAX_PAGES * PAGE_SIZE)
|
||||
#define SHM_MIN 1
|
||||
#define SHM_MNI 192
|
||||
#define SHM_SEG 128
|
||||
#define SHM_ALL (SHM_MAX_PAGES)
|
||||
|
||||
struct shmIDSource {
|
||||
struct shmid_ds ds;
|
||||
UINT32 status;
|
||||
LOS_DL_LIST node;
|
||||
#ifdef LOSCFG_SHELL
|
||||
CHAR ownerName[OS_PCB_NAME_LEN];
|
||||
#endif
|
||||
};
|
||||
|
||||
VOID OsShmFork(LosVmSpace *space, LosVmMapRegion *oldRegion, LosVmMapRegion *newRegion);
|
||||
VOID OsShmRegionFree(LosVmSpace *space, LosVmMapRegion *region);
|
||||
BOOL OsIsShmRegion(LosVmMapRegion *region);
|
||||
|
||||
struct shmIDSource *OsShmCBInit(LosMux *sysvShmMux, struct shminfo *shmInfo, UINT32 *shmUsedPageCount);
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
@@ -37,14 +37,51 @@
|
||||
#include "los_mp.h"
|
||||
#include "los_percpu_pri.h"
|
||||
#include "los_hook.h"
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
#include "los_ipc_container_pri.h"
|
||||
#endif
|
||||
|
||||
#ifdef LOSCFG_BASE_IPC_QUEUE
|
||||
#if (LOSCFG_BASE_IPC_QUEUE_LIMIT <= 0)
|
||||
#error "queue maxnum cannot be zero"
|
||||
#endif /* LOSCFG_BASE_IPC_QUEUE_LIMIT <= 0 */
|
||||
|
||||
#ifndef LOSCFG_IPC_CONTAINER
|
||||
LITE_OS_SEC_BSS LosQueueCB *g_allQueue = NULL;
|
||||
LITE_OS_SEC_BSS STATIC LOS_DL_LIST g_freeQueueList;
|
||||
#define FREE_QUEUE_LIST g_freeQueueList
|
||||
#endif
|
||||
|
||||
LITE_OS_SEC_TEXT_INIT LosQueueCB *OsAllQueueCBInit(LOS_DL_LIST *freeQueueList)
|
||||
{
|
||||
UINT32 index;
|
||||
|
||||
if (freeQueueList == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
UINT32 size = LOSCFG_BASE_IPC_QUEUE_LIMIT * sizeof(LosQueueCB);
|
||||
/* system resident memory, don't free */
|
||||
LosQueueCB *allQueue = (LosQueueCB *)LOS_MemAlloc(m_aucSysMem0, size);
|
||||
if (allQueue == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
(VOID)memset_s(allQueue, size, 0, size);
|
||||
LOS_ListInit(freeQueueList);
|
||||
for (index = 0; index < LOSCFG_BASE_IPC_QUEUE_LIMIT; index++) {
|
||||
LosQueueCB *queueNode = ((LosQueueCB *)allQueue) + index;
|
||||
queueNode->queueID = index;
|
||||
LOS_ListTailInsert(freeQueueList, &queueNode->readWriteList[OS_QUEUE_WRITE]);
|
||||
}
|
||||
|
||||
#ifndef LOSCFG_IPC_CONTAINER
|
||||
if (OsQueueDbgInitHook() != LOS_OK) {
|
||||
(VOID)LOS_MemFree(m_aucSysMem0, allQueue);
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
return allQueue;
|
||||
}
|
||||
|
||||
/*
|
||||
* Description : queue initial
|
||||
@@ -52,27 +89,12 @@ LITE_OS_SEC_BSS STATIC LOS_DL_LIST g_freeQueueList;
|
||||
*/
|
||||
LITE_OS_SEC_TEXT_INIT UINT32 OsQueueInit(VOID)
|
||||
{
|
||||
LosQueueCB *queueNode = NULL;
|
||||
UINT32 index;
|
||||
UINT32 size;
|
||||
|
||||
size = LOSCFG_BASE_IPC_QUEUE_LIMIT * sizeof(LosQueueCB);
|
||||
/* system resident memory, don't free */
|
||||
g_allQueue = (LosQueueCB *)LOS_MemAlloc(m_aucSysMem0, size);
|
||||
#ifndef LOSCFG_IPC_CONTAINER
|
||||
g_allQueue = OsAllQueueCBInit(&g_freeQueueList);
|
||||
if (g_allQueue == NULL) {
|
||||
return LOS_ERRNO_QUEUE_NO_MEMORY;
|
||||
}
|
||||
(VOID)memset_s(g_allQueue, size, 0, size);
|
||||
LOS_ListInit(&g_freeQueueList);
|
||||
for (index = 0; index < LOSCFG_BASE_IPC_QUEUE_LIMIT; index++) {
|
||||
queueNode = ((LosQueueCB *)g_allQueue) + index;
|
||||
queueNode->queueID = index;
|
||||
LOS_ListTailInsert(&g_freeQueueList, &queueNode->readWriteList[OS_QUEUE_WRITE]);
|
||||
}
|
||||
|
||||
if (OsQueueDbgInitHook() != LOS_OK) {
|
||||
return LOS_ERRNO_QUEUE_NO_MEMORY;
|
||||
}
|
||||
#endif
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
@@ -111,14 +133,14 @@ LITE_OS_SEC_TEXT_INIT UINT32 LOS_QueueCreate(CHAR *queueName, UINT16 len, UINT32
|
||||
}
|
||||
|
||||
SCHEDULER_LOCK(intSave);
|
||||
if (LOS_ListEmpty(&g_freeQueueList)) {
|
||||
if (LOS_ListEmpty(&FREE_QUEUE_LIST)) {
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
OsQueueCheckHook();
|
||||
(VOID)LOS_MemFree(m_aucSysMem1, queue);
|
||||
return LOS_ERRNO_QUEUE_CB_UNAVAILABLE;
|
||||
}
|
||||
|
||||
unusedQueue = LOS_DL_LIST_FIRST(&g_freeQueueList);
|
||||
unusedQueue = LOS_DL_LIST_FIRST(&FREE_QUEUE_LIST);
|
||||
LOS_ListDelete(unusedQueue);
|
||||
queueCB = GET_QUEUE_LIST(unusedQueue);
|
||||
queueCB->queueLen = len;
|
||||
@@ -433,7 +455,7 @@ LITE_OS_SEC_TEXT_INIT UINT32 LOS_QueueDelete(UINT32 queueID)
|
||||
queueCB->queueID = SET_QUEUE_ID(GET_QUEUE_COUNT(queueCB->queueID) + 1, GET_QUEUE_INDEX(queueCB->queueID));
|
||||
OsQueueDbgUpdateHook(queueCB->queueID, NULL);
|
||||
|
||||
LOS_ListTailInsert(&g_freeQueueList, &queueCB->readWriteList[OS_QUEUE_WRITE]);
|
||||
LOS_ListTailInsert(&FREE_QUEUE_LIST, &queueCB->readWriteList[OS_QUEUE_WRITE]);
|
||||
SCHEDULER_UNLOCK(intSave);
|
||||
OsHookCall(LOS_HOOK_TYPE_QUEUE_DELETE, queueCB);
|
||||
ret = LOS_MemFree(m_aucSysMem1, (VOID *)queue);
|
||||
|
||||
@@ -35,6 +35,9 @@
|
||||
#include "los_queue_pri.h"
|
||||
#include "los_swtmr_pri.h"
|
||||
#include "los_task_pri.h"
|
||||
#ifdef LOSCFG_IPC_CONTAINER
|
||||
#include "los_ipc_container_pri.h"
|
||||
#endif
|
||||
|
||||
#ifdef LOSCFG_SHELL
|
||||
#include "shcmd.h"
|
||||
@@ -88,7 +91,7 @@ UINT32 OsShellCmdQueueCntGet(VOID)
|
||||
LosQueueCB *queueCB = NULL;
|
||||
|
||||
intSave = LOS_IntLock();
|
||||
queueCB = g_allQueue;
|
||||
queueCB = IPC_ALL_QUEUE;
|
||||
for (loop = 0; loop < LOSCFG_BASE_IPC_QUEUE_LIMIT; loop++, queueCB++) {
|
||||
if (queueCB->queueState == OS_QUEUE_INUSED) {
|
||||
queueCnt++;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
@@ -241,20 +241,15 @@ STATIC BOOL OsVmSpaceParamCheck(const LosVmSpace *vmSpace)
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
STATUS_T LOS_VmSpaceClone(LosVmSpace *oldVmSpace, LosVmSpace *newVmSpace)
|
||||
STATUS_T LOS_VmSpaceClone(UINT32 cloneFlags, LosVmSpace *oldVmSpace, LosVmSpace *newVmSpace)
|
||||
{
|
||||
LosVmMapRegion *oldRegion = NULL;
|
||||
LosVmMapRegion *newRegion = NULL;
|
||||
LosRbNode *pstRbNode = NULL;
|
||||
LosRbNode *pstRbNodeNext = NULL;
|
||||
STATUS_T ret = LOS_OK;
|
||||
UINT32 numPages;
|
||||
PADDR_T paddr;
|
||||
VADDR_T vaddr;
|
||||
UINT32 intSave;
|
||||
LosVmPage *page = NULL;
|
||||
UINT32 flags;
|
||||
UINT32 i;
|
||||
UINT32 flags, i, intSave, numPages;
|
||||
|
||||
if ((OsVmSpaceParamCheck(oldVmSpace) == FALSE) || (OsVmSpaceParamCheck(newVmSpace) == FALSE)) {
|
||||
return LOS_ERRNO_VM_INVALID_ARGS;
|
||||
@@ -270,8 +265,13 @@ STATUS_T LOS_VmSpaceClone(LosVmSpace *oldVmSpace, LosVmSpace *newVmSpace)
|
||||
newVmSpace->heapNow = oldVmSpace->heapNow;
|
||||
(VOID)LOS_MuxAcquire(&oldVmSpace->regionMux);
|
||||
RB_SCAN_SAFE(&oldVmSpace->regionRbTree, pstRbNode, pstRbNodeNext)
|
||||
oldRegion = (LosVmMapRegion *)pstRbNode;
|
||||
newRegion = OsVmRegionDup(newVmSpace, oldRegion, oldRegion->range.base, oldRegion->range.size);
|
||||
LosVmMapRegion *oldRegion = (LosVmMapRegion *)pstRbNode;
|
||||
#if defined(LOSCFG_KERNEL_SHM) && defined(LOSCFG_IPC_CONTAINER)
|
||||
if ((oldRegion->regionFlags & VM_MAP_REGION_FLAG_SHM) && (cloneFlags & CLONE_NEWIPC)) {
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
LosVmMapRegion *newRegion = OsVmRegionDup(newVmSpace, oldRegion, oldRegion->range.base, oldRegion->range.size);
|
||||
if (newRegion == NULL) {
|
||||
VM_ERR("dup new region failed");
|
||||
ret = LOS_ERRNO_VM_NO_MEMORY;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
@@ -33,7 +33,6 @@
|
||||
#include "string.h"
|
||||
#include "time.h"
|
||||
#include "sys/types.h"
|
||||
#include "sys/shm.h"
|
||||
#include "sys/stat.h"
|
||||
#include "los_config.h"
|
||||
#include "los_init.h"
|
||||
@@ -46,6 +45,8 @@
|
||||
#include "los_process.h"
|
||||
#include "los_process_pri.h"
|
||||
#include "user_copy.h"
|
||||
#include "los_vm_shm_pri.h"
|
||||
#include "sys/shm.h"
|
||||
#ifdef LOSCFG_SHELL
|
||||
#include "shcmd.h"
|
||||
#include "shell.h"
|
||||
@@ -54,20 +55,6 @@
|
||||
|
||||
#ifdef LOSCFG_KERNEL_SHM
|
||||
|
||||
STATIC LosMux g_sysvShmMux;
|
||||
|
||||
/* private macro */
|
||||
#define SYSV_SHM_LOCK() (VOID)LOS_MuxLock(&g_sysvShmMux, LOS_WAIT_FOREVER)
|
||||
#define SYSV_SHM_UNLOCK() (VOID)LOS_MuxUnlock(&g_sysvShmMux)
|
||||
|
||||
/* The upper limit size of total shared memory is default 16M */
|
||||
#define SHM_MAX_PAGES 4096
|
||||
#define SHM_MAX (SHM_MAX_PAGES * PAGE_SIZE)
|
||||
#define SHM_MIN 1
|
||||
#define SHM_MNI 192
|
||||
#define SHM_SEG 128
|
||||
#define SHM_ALL (SHM_MAX_PAGES)
|
||||
|
||||
#define SHM_SEG_FREE 0x2000
|
||||
#define SHM_SEG_USED 0x4000
|
||||
#define SHM_SEG_REMOVE 0x8000
|
||||
@@ -91,57 +78,75 @@ STATIC LosMux g_sysvShmMux;
|
||||
#define SHM_GROUPE_TO_USER 3
|
||||
#define SHM_OTHER_TO_USER 6
|
||||
|
||||
struct shmIDSource {
|
||||
struct shmid_ds ds;
|
||||
UINT32 status;
|
||||
LOS_DL_LIST node;
|
||||
#ifdef LOSCFG_SHELL
|
||||
CHAR ownerName[OS_PCB_NAME_LEN];
|
||||
#endif
|
||||
};
|
||||
#ifndef LOSCFG_IPC_CONTAINER
|
||||
STATIC LosMux g_sysvShmMux;
|
||||
|
||||
/* private data */
|
||||
STATIC struct shminfo g_shmInfo = {
|
||||
.shmmax = SHM_MAX,
|
||||
.shmmin = SHM_MIN,
|
||||
.shmmni = SHM_MNI,
|
||||
.shmseg = SHM_SEG,
|
||||
.shmall = SHM_ALL,
|
||||
};
|
||||
|
||||
STATIC struct shminfo g_shmInfo;
|
||||
STATIC struct shmIDSource *g_shmSegs = NULL;
|
||||
STATIC UINT32 g_shmUsedPageCount;
|
||||
|
||||
UINT32 ShmInit(VOID)
|
||||
#define IPC_SHM_INFO g_shmInfo
|
||||
#define IPC_SHM_SYS_VSHM_MUTEX g_sysvShmMux
|
||||
#define IPC_SHM_SEGS g_shmSegs
|
||||
#define IPC_SHM_USED_PAGE_COUNT g_shmUsedPageCount
|
||||
#endif
|
||||
|
||||
/* private macro */
|
||||
#define SYSV_SHM_LOCK() (VOID)LOS_MuxLock(&IPC_SHM_SYS_VSHM_MUTEX, LOS_WAIT_FOREVER)
|
||||
#define SYSV_SHM_UNLOCK() (VOID)LOS_MuxUnlock(&IPC_SHM_SYS_VSHM_MUTEX)
|
||||
|
||||
struct shmIDSource *OsShmCBInit(LosMux *sysvShmMux, struct shminfo *shmInfo, UINT32 *shmUsedPageCount)
|
||||
{
|
||||
UINT32 ret;
|
||||
UINT32 i;
|
||||
|
||||
ret = LOS_MuxInit(&g_sysvShmMux, NULL);
|
||||
if ((sysvShmMux == NULL) || (shmInfo == NULL) || (shmUsedPageCount == NULL)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = LOS_MuxInit(sysvShmMux, NULL);
|
||||
if (ret != LOS_OK) {
|
||||
goto ERROR;
|
||||
}
|
||||
|
||||
g_shmSegs = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, sizeof(struct shmIDSource) * g_shmInfo.shmmni);
|
||||
if (g_shmSegs == NULL) {
|
||||
(VOID)LOS_MuxDestroy(&g_sysvShmMux);
|
||||
shmInfo->shmmax = SHM_MAX;
|
||||
shmInfo->shmmin = SHM_MIN;
|
||||
shmInfo->shmmni = SHM_MNI;
|
||||
shmInfo->shmseg = SHM_SEG;
|
||||
shmInfo->shmall = SHM_ALL;
|
||||
|
||||
struct shmIDSource *shmSegs = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, sizeof(struct shmIDSource) * shmInfo->shmmni);
|
||||
if (shmSegs == NULL) {
|
||||
(VOID)LOS_MuxDestroy(sysvShmMux);
|
||||
goto ERROR;
|
||||
}
|
||||
(VOID)memset_s(g_shmSegs, (sizeof(struct shmIDSource) * g_shmInfo.shmmni),
|
||||
0, (sizeof(struct shmIDSource) * g_shmInfo.shmmni));
|
||||
(VOID)memset_s(shmSegs, (sizeof(struct shmIDSource) * shmInfo->shmmni),
|
||||
0, (sizeof(struct shmIDSource) * shmInfo->shmmni));
|
||||
|
||||
for (i = 0; i < g_shmInfo.shmmni; i++) {
|
||||
g_shmSegs[i].status = SHM_SEG_FREE;
|
||||
g_shmSegs[i].ds.shm_perm.seq = i + 1;
|
||||
LOS_ListInit(&g_shmSegs[i].node);
|
||||
for (i = 0; i < shmInfo->shmmni; i++) {
|
||||
shmSegs[i].status = SHM_SEG_FREE;
|
||||
shmSegs[i].ds.shm_perm.seq = i + 1;
|
||||
LOS_ListInit(&shmSegs[i].node);
|
||||
}
|
||||
g_shmUsedPageCount = 0;
|
||||
*shmUsedPageCount = 0;
|
||||
|
||||
return LOS_OK;
|
||||
return shmSegs;
|
||||
|
||||
ERROR:
|
||||
VM_ERR("ShmInit fail\n");
|
||||
return LOS_NOK;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
UINT32 ShmInit(VOID)
|
||||
{
|
||||
#ifndef LOSCFG_IPC_CONTAINER
|
||||
g_shmSegs = OsShmCBInit(&IPC_SHM_SYS_VSHM_MUTEX, &IPC_SHM_INFO, &IPC_SHM_USED_PAGE_COUNT);
|
||||
if (g_shmSegs == NULL) {
|
||||
return LOS_NOK;
|
||||
}
|
||||
#endif
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
LOS_MODULE_INIT(ShmInit, LOS_INIT_LEVEL_VM_COMPLETE);
|
||||
@@ -150,10 +155,10 @@ UINT32 ShmDeinit(VOID)
|
||||
{
|
||||
UINT32 ret;
|
||||
|
||||
(VOID)LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, g_shmSegs);
|
||||
g_shmSegs = NULL;
|
||||
(VOID)LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, IPC_SHM_SEGS);
|
||||
IPC_SHM_SEGS = NULL;
|
||||
|
||||
ret = LOS_MuxDestroy(&g_sysvShmMux);
|
||||
ret = LOS_MuxDestroy(&IPC_SHM_SYS_VSHM_MUTEX);
|
||||
if (ret != LOS_OK) {
|
||||
return -1;
|
||||
}
|
||||
@@ -195,18 +200,18 @@ STATIC INT32 ShmAllocSeg(key_t key, size_t size, INT32 shmflg)
|
||||
struct shmIDSource *seg = NULL;
|
||||
size_t count;
|
||||
|
||||
if ((size == 0) || (size < g_shmInfo.shmmin) ||
|
||||
(size > g_shmInfo.shmmax)) {
|
||||
if ((size == 0) || (size < IPC_SHM_INFO.shmmin) ||
|
||||
(size > IPC_SHM_INFO.shmmax)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
size = LOS_Align(size, PAGE_SIZE);
|
||||
if ((g_shmUsedPageCount + (size >> PAGE_SHIFT)) > g_shmInfo.shmall) {
|
||||
if ((IPC_SHM_USED_PAGE_COUNT + (size >> PAGE_SHIFT)) > IPC_SHM_INFO.shmall) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
for (i = 0; i < g_shmInfo.shmmni; i++) {
|
||||
if (g_shmSegs[i].status & SHM_SEG_FREE) {
|
||||
g_shmSegs[i].status &= ~SHM_SEG_FREE;
|
||||
for (i = 0; i < IPC_SHM_INFO.shmmni; i++) {
|
||||
if (IPC_SHM_SEGS[i].status & SHM_SEG_FREE) {
|
||||
IPC_SHM_SEGS[i].status &= ~SHM_SEG_FREE;
|
||||
segNum = i;
|
||||
break;
|
||||
}
|
||||
@@ -216,7 +221,7 @@ STATIC INT32 ShmAllocSeg(key_t key, size_t size, INT32 shmflg)
|
||||
return -ENOSPC;
|
||||
}
|
||||
|
||||
seg = &g_shmSegs[segNum];
|
||||
seg = &IPC_SHM_SEGS[segNum];
|
||||
count = LOS_PhysPagesAlloc(size >> PAGE_SHIFT, &seg->node);
|
||||
if (count != (size >> PAGE_SHIFT)) {
|
||||
(VOID)LOS_PhysPagesFree(&seg->node);
|
||||
@@ -224,7 +229,7 @@ STATIC INT32 ShmAllocSeg(key_t key, size_t size, INT32 shmflg)
|
||||
return -ENOMEM;
|
||||
}
|
||||
ShmSetSharedFlag(seg);
|
||||
g_shmUsedPageCount += size >> PAGE_SHIFT;
|
||||
IPC_SHM_USED_PAGE_COUNT += size >> PAGE_SHIFT;
|
||||
|
||||
seg->status |= SHM_SEG_USED;
|
||||
seg->ds.shm_perm.mode = (UINT32)shmflg & ACCESSPERMS;
|
||||
@@ -257,7 +262,7 @@ STATIC INLINE VOID ShmFreeSeg(struct shmIDSource *seg)
|
||||
VM_ERR("free physical pages failed, count = %d, size = %d", count, seg->ds.shm_segsz >> PAGE_SHIFT);
|
||||
return;
|
||||
}
|
||||
g_shmUsedPageCount -= seg->ds.shm_segsz >> PAGE_SHIFT;
|
||||
IPC_SHM_USED_PAGE_COUNT -= seg->ds.shm_segsz >> PAGE_SHIFT;
|
||||
seg->status = SHM_SEG_FREE;
|
||||
LOS_ListInit(&seg->node);
|
||||
}
|
||||
@@ -267,8 +272,8 @@ STATIC INT32 ShmFindSegByKey(key_t key)
|
||||
INT32 i;
|
||||
struct shmIDSource *seg = NULL;
|
||||
|
||||
for (i = 0; i < g_shmInfo.shmmni; i++) {
|
||||
seg = &g_shmSegs[i];
|
||||
for (i = 0; i < IPC_SHM_INFO.shmmni; i++) {
|
||||
seg = &IPC_SHM_SEGS[i];
|
||||
if ((seg->status & SHM_SEG_USED) &&
|
||||
(seg->ds.shm_perm.key == key)) {
|
||||
return i;
|
||||
@@ -280,7 +285,7 @@ STATIC INT32 ShmFindSegByKey(key_t key)
|
||||
|
||||
STATIC INT32 ShmSegValidCheck(INT32 segNum, size_t size, INT32 shmFlg)
|
||||
{
|
||||
struct shmIDSource *seg = &g_shmSegs[segNum];
|
||||
struct shmIDSource *seg = &IPC_SHM_SEGS[segNum];
|
||||
|
||||
if (size > seg->ds.shm_segsz) {
|
||||
return -EINVAL;
|
||||
@@ -298,12 +303,12 @@ STATIC struct shmIDSource *ShmFindSeg(int shmid)
|
||||
{
|
||||
struct shmIDSource *seg = NULL;
|
||||
|
||||
if ((shmid < 0) || (shmid >= g_shmInfo.shmmni)) {
|
||||
if ((shmid < 0) || (shmid >= IPC_SHM_INFO.shmmni)) {
|
||||
set_errno(EINVAL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
seg = &g_shmSegs[shmid];
|
||||
seg = &IPC_SHM_SEGS[shmid];
|
||||
if ((seg->status & SHM_SEG_FREE) || ((seg->ds.shm_nattch == 0) && (seg->status & SHM_SEG_REMOVE))) {
|
||||
set_errno(EIDRM);
|
||||
return NULL;
|
||||
@@ -383,8 +388,8 @@ STATIC INT32 ShmSegUsedCount(VOID)
|
||||
INT32 count = 0;
|
||||
struct shmIDSource *seg = NULL;
|
||||
|
||||
for (i = 0; i < g_shmInfo.shmmni; i++) {
|
||||
seg = &g_shmSegs[i];
|
||||
for (i = 0; i < IPC_SHM_INFO.shmmni; i++) {
|
||||
seg = &IPC_SHM_SEGS[i];
|
||||
if (seg->status & SHM_SEG_USED) {
|
||||
count++;
|
||||
}
|
||||
@@ -676,12 +681,12 @@ INT32 ShmCtl(INT32 shmid, INT32 cmd, struct shmid_ds *buf)
|
||||
}
|
||||
break;
|
||||
case IPC_INFO:
|
||||
ret = LOS_ArchCopyToUser(buf, &g_shmInfo, sizeof(struct shminfo));
|
||||
ret = LOS_ArchCopyToUser(buf, &IPC_SHM_INFO, sizeof(struct shminfo));
|
||||
if (ret != 0) {
|
||||
ret = EFAULT;
|
||||
goto ERROR;
|
||||
}
|
||||
ret = g_shmInfo.shmmni;
|
||||
ret = IPC_SHM_INFO.shmmni;
|
||||
break;
|
||||
case SHM_INFO:
|
||||
shmInfo.shm_rss = 0;
|
||||
@@ -695,7 +700,7 @@ INT32 ShmCtl(INT32 shmid, INT32 cmd, struct shmid_ds *buf)
|
||||
ret = EFAULT;
|
||||
goto ERROR;
|
||||
}
|
||||
ret = g_shmInfo.shmmni;
|
||||
ret = IPC_SHM_INFO.shmmni;
|
||||
break;
|
||||
default:
|
||||
VM_ERR("the cmd(%d) is not supported!", cmd);
|
||||
@@ -784,8 +789,8 @@ STATIC VOID OsShmInfoCmd(VOID)
|
||||
PRINTK("\r\n------- Shared Memory Segments -------\n");
|
||||
PRINTK("key shmid perms bytes nattch status owner\n");
|
||||
SYSV_SHM_LOCK();
|
||||
for (i = 0; i < g_shmInfo.shmmni; i++) {
|
||||
seg = &g_shmSegs[i];
|
||||
for (i = 0; i < IPC_SHM_INFO.shmmni; i++) {
|
||||
seg = &IPC_SHM_SEGS[i];
|
||||
if (!(seg->status & SHM_SEG_USED)) {
|
||||
continue;
|
||||
}
|
||||
@@ -801,7 +806,7 @@ STATIC VOID OsShmDeleteCmd(INT32 shmid)
|
||||
{
|
||||
struct shmIDSource *seg = NULL;
|
||||
|
||||
if ((shmid < 0) || (shmid >= g_shmInfo.shmmni)) {
|
||||
if ((shmid < 0) || (shmid >= IPC_SHM_INFO.shmmni)) {
|
||||
PRINT_ERR("shmid is invalid: %d\n", shmid);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -85,6 +85,7 @@ extern int SysWaitid(idtype_t type, int pid, USER siginfo_t *info, int options,
|
||||
extern int SysFork(void);
|
||||
extern int SysVfork(void);
|
||||
extern int SysClone(int flags, void *stack, int *parentTid, unsigned long tls, int *childTid);
|
||||
extern int SysUnshare(int flags);
|
||||
extern unsigned int SysGetPID(void);
|
||||
extern unsigned int SysGetPPID(void);
|
||||
extern int SysSetGroupID(unsigned int gid);
|
||||
|
||||
@@ -369,6 +369,15 @@ int SysClone(int flags, void *stack, int *parentTid, unsigned long tls, int *chi
|
||||
return OsClone((UINT32)flags, (UINTPTR)stack, 0);
|
||||
}
|
||||
|
||||
int SysUnshare(int flags)
|
||||
{
|
||||
#ifdef LOSCFG_KERNEL_CONTAINER
|
||||
return OsUnshare(flags);
|
||||
#else
|
||||
return -ENOSYS;
|
||||
#endif
|
||||
}
|
||||
|
||||
unsigned int SysGetPPID(void)
|
||||
{
|
||||
#ifdef LOSCFG_PID_CONTAINER
|
||||
|
||||
@@ -140,6 +140,7 @@ SYSCALL_HAND_DEF(__NR_exit, SysThreadExit, void, ARG_NUM_1)
|
||||
SYSCALL_HAND_DEF(__NR_fork, SysFork, int, ARG_NUM_0)
|
||||
SYSCALL_HAND_DEF(__NR_vfork, SysVfork, int, ARG_NUM_0)
|
||||
SYSCALL_HAND_DEF(__NR_clone, SysClone, int, ARG_NUM_5)
|
||||
SYSCALL_HAND_DEF(__NR_unshare, SysUnshare, int, ARG_NUM_1)
|
||||
SYSCALL_HAND_DEF(__NR_getpid, SysGetPID, unsigned int, ARG_NUM_0)
|
||||
SYSCALL_HAND_DEF(__NR_pause, SysPause, int, ARG_NUM_0)
|
||||
|
||||
|
||||
@@ -136,6 +136,8 @@ LOSCFG_USER_TEST_CONTAINER = false
|
||||
LOSCFG_USER_TEST_PID_CONTAINER = false
|
||||
LOSCFG_USER_TEST_UTS_CONTAINER = false
|
||||
LOSCFG_USER_TEST_MNT_CONTAINER = false
|
||||
LOSCFG_USER_TEST_IPC_CONTAINER = false
|
||||
LOSCFG_USER_TEST_TIME_CONTAINER = false
|
||||
if (defined(LOSCFG_KERNEL_CONTAINER)) {
|
||||
LOSCFG_USER_TEST_CONTAINER = true
|
||||
if (defined(LOSCFG_PID_CONTAINER)) {
|
||||
@@ -147,4 +149,10 @@ if (defined(LOSCFG_KERNEL_CONTAINER)) {
|
||||
if (defined(LOSCFG_MNT_CONTAINER)) {
|
||||
LOSCFG_USER_TEST_MNT_CONTAINER = true
|
||||
}
|
||||
if (defined(LOSCFG_IPC_CONTAINER)) {
|
||||
LOSCFG_USER_TEST_IPC_CONTAINER = true
|
||||
}
|
||||
if (defined(LOSCFG_TIME_CONTAINER)) {
|
||||
LOSCFG_USER_TEST_TIME_CONTAINER = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,12 @@ config("container_config") {
|
||||
if (defined(LOSCFG_USER_TEST_MNT_CONTAINER)) {
|
||||
cflags += [ "-DLOSCFG_USER_TEST_MNT_CONTAINER" ]
|
||||
}
|
||||
if (defined(LOSCFG_USER_TEST_IPC_CONTAINER)) {
|
||||
cflags += [ "-DLOSCFG_USER_TEST_IPC_CONTAINER" ]
|
||||
}
|
||||
if (defined(LOSCFG_USER_TEST_TIME_CONTAINER)) {
|
||||
cflags += [ "-DLOSCFG_USER_TEST_TIME_CONTAINER" ]
|
||||
}
|
||||
cflags_cc = cflags
|
||||
}
|
||||
|
||||
|
||||
@@ -74,24 +74,12 @@ std::string GenContainerLinkPath(int pid, const std::string& containerType)
|
||||
|
||||
std::string ReadlinkContainer(int pid, const std::string& containerType)
|
||||
{
|
||||
char buf[PATH_MAX];
|
||||
char buf[PATH_MAX] = {0};
|
||||
auto path = GenContainerLinkPath(pid, containerType);
|
||||
|
||||
struct stat sb;
|
||||
int ret = lstat(path.data(), &sb);
|
||||
if (ret == -1) {
|
||||
throw std::exception();
|
||||
}
|
||||
|
||||
size_t bufsiz = sb.st_size + 1;
|
||||
if (sb.st_size == 0) {
|
||||
bufsiz = PATH_MAX;
|
||||
}
|
||||
|
||||
(void)memset_s(buf, PATH_MAX, 0, PATH_MAX);
|
||||
ssize_t nbytes = readlink(path.c_str(), buf, bufsiz);
|
||||
ssize_t nbytes = readlink(path.c_str(), buf, PATH_MAX);
|
||||
if (nbytes == -1) {
|
||||
throw std::exception();
|
||||
printf("pid %d, ReadlinkContainer readlink %s failed, errno=%d\n", getpid(), path.c_str(), errno);
|
||||
return path.c_str();
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
@@ -126,6 +114,54 @@ HWTEST_F(ContainerTest, ItPidContainer023, TestSize.Level0)
|
||||
{
|
||||
ItPidContainer023();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_Pid_Test_025
|
||||
* @tc.desc: pid container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI68LVW
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItPidContainer025, TestSize.Level0)
|
||||
{
|
||||
ItPidContainer025();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_Pid_Test_026
|
||||
* @tc.desc: pid container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI68LVW
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItPidContainer026, TestSize.Level0)
|
||||
{
|
||||
ItPidContainer026();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_Pid_Test_027
|
||||
* @tc.desc: pid container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6BE5A
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItPidContainer027, TestSize.Level0)
|
||||
{
|
||||
ItPidContainer027();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_Pid_Test_028
|
||||
* @tc.desc: pid container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6BE5A
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItPidContainer028, TestSize.Level0)
|
||||
{
|
||||
ItPidContainer028();
|
||||
}
|
||||
#endif
|
||||
#if defined(LOSCFG_USER_TEST_UTS_CONTAINER)
|
||||
/**
|
||||
@@ -151,6 +187,18 @@ HWTEST_F(ContainerTest, ItUtsContainer002, TestSize.Level0)
|
||||
{
|
||||
ItUtsContainer002();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_UTS_Test_004
|
||||
* @tc.desc: uts container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6BE5A
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItUtsContainer004, TestSize.Level0)
|
||||
{
|
||||
ItUtsContainer004();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(LOSCFG_USER_TEST_MNT_CONTAINER)
|
||||
@@ -202,6 +250,42 @@ HWTEST_F(ContainerTest, ItMntContainer004, TestSize.Level0)
|
||||
ItMntContainer004();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_MNT_Test_005
|
||||
* @tc.desc: mnt container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6BE5A
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItMntContainer005, TestSize.Level0)
|
||||
{
|
||||
ItMntContainer005();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_MNT_Test_006
|
||||
* @tc.desc: mnt container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6BE5A
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItMntContainer006, TestSize.Level0)
|
||||
{
|
||||
ItMntContainer006();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_MNT_Test_007
|
||||
* @tc.desc: mnt container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6BE5A
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItMntContainer007, TestSize.Level0)
|
||||
{
|
||||
ItMntContainer007();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: chroot_Test_001
|
||||
* @tc.desc: chroot function test case
|
||||
@@ -215,7 +299,155 @@ HWTEST_F(ContainerTest, ItContainerChroot001, TestSize.Level0)
|
||||
ItContainerChroot001();
|
||||
}
|
||||
|
||||
#endif /* LOSCFG_MNT_CONTAINER */
|
||||
#endif /* LOSCFG_USER_TEST_MNT_CONTAINER */
|
||||
|
||||
#if defined(LOSCFG_USER_TEST_IPC_CONTAINER)
|
||||
/**
|
||||
* @tc.name: Container_IPC_Test_001
|
||||
* @tc.desc: ipc container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6AVMY
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItIpcContainer001, TestSize.Level0)
|
||||
{
|
||||
ItIpcContainer001();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_IPC_Test_003
|
||||
* @tc.desc: ipc container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6BE5A
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItIpcContainer003, TestSize.Level0)
|
||||
{
|
||||
ItIpcContainer003();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_IPC_Test_004
|
||||
* @tc.desc: ipc container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6AVMY
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItIpcContainer004, TestSize.Level0)
|
||||
{
|
||||
ItIpcContainer004();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_IPC_Test_005
|
||||
* @tc.desc: ipc container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6BE5A
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItIpcContainer005, TestSize.Level0)
|
||||
{
|
||||
ItIpcContainer005();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(LOSCFG_USER_TEST_TIME_CONTAINER)
|
||||
/**
|
||||
* @tc.name: Container_TIME_Test_001
|
||||
* @tc.desc: time container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6B0A3
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItTimeContainer001, TestSize.Level0)
|
||||
{
|
||||
ItTimeContainer001();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_TIME_Test_002
|
||||
* @tc.desc: time container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6BE5A
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItTimeContainer002, TestSize.Level0)
|
||||
{
|
||||
ItTimeContainer002();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_TIME_Test_004
|
||||
* @tc.desc: time container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6BE5A
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItTimeContainer004, TestSize.Level0)
|
||||
{
|
||||
ItTimeContainer004();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_TIME_Test_005
|
||||
* @tc.desc: time container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6BE5A
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItTimeContainer005, TestSize.Level0)
|
||||
{
|
||||
ItTimeContainer005();
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: Container_TIME_Test_007
|
||||
* @tc.desc: time container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6B0A3
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItTimeContainer007, TestSize.Level0)
|
||||
{
|
||||
ItTimeContainer007();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_TIME_Test_008
|
||||
* @tc.desc: time container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6BE5A
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItTimeContainer008, TestSize.Level0)
|
||||
{
|
||||
ItTimeContainer008();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_TIME_Test_009
|
||||
* @tc.desc: time container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6B0A3
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItTimeContainer009, TestSize.Level0)
|
||||
{
|
||||
ItTimeContainer009();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_TIME_Test_010
|
||||
* @tc.desc: time container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6B0A3
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItTimeContainer010, TestSize.Level0)
|
||||
{
|
||||
ItTimeContainer010();
|
||||
}
|
||||
#endif
|
||||
#endif /* LOSCFG_USER_TEST_SMOKE */
|
||||
|
||||
#if defined(LOSCFG_USER_TEST_FULL)
|
||||
|
||||
@@ -35,9 +35,15 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <regex>
|
||||
#include <csignal>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/capability.h>
|
||||
#include <cstring>
|
||||
#include "osTest.h"
|
||||
#include "mqueue.h"
|
||||
#include "sys/time.h"
|
||||
#include "sys/shm.h"
|
||||
#include "sys/types.h"
|
||||
|
||||
const int EXIT_CODE_ERRNO_1 = 1;
|
||||
const int EXIT_CODE_ERRNO_2 = 2;
|
||||
@@ -55,11 +61,19 @@ const int EXIT_CODE_ERRNO_13 = 13;
|
||||
const int EXIT_CODE_ERRNO_14 = 14;
|
||||
const int EXIT_CODE_ERRNO_15 = 15;
|
||||
const int EXIT_CODE_ERRNO_16 = 16;
|
||||
const int EXIT_CODE_ERRNO_17 = 17;
|
||||
const int EXIT_CODE_ERRNO_255 = 255;
|
||||
const int CONTAINER_FIRST_PID = 1;
|
||||
const int CONTAINER_SECOND_PID = 2;
|
||||
const int CONTAINER_THIRD_PID = 3;
|
||||
|
||||
const int MQUEUE_TEST_SIZE = 50;
|
||||
const int MQUEUE_TEST_MAX_MSG = 255;
|
||||
|
||||
const int SHM_TEST_DATA_SIZE = 1024;
|
||||
const int SHM_TEST_KEY1 = 1234;
|
||||
const int SHM_TEST_OPEN_PERM = 0666;
|
||||
const int CLONE_STACK_MMAP_FLAG = MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK;
|
||||
|
||||
extern const char *USERDATA_DIR_NAME;
|
||||
extern const char *ACCESS_FILE_NAME;
|
||||
@@ -71,6 +85,12 @@ extern const int BIT_ON_RETURN_VALUE;
|
||||
extern const int STACK_SIZE;
|
||||
extern const int CHILD_FUNC_ARG;
|
||||
|
||||
const int MQUEUE_STANDARD_NAME_LENGTH = 255;
|
||||
|
||||
extern "C" {
|
||||
#define CLONE_NEWTIME 0x00000080
|
||||
}
|
||||
|
||||
int ChildFunction(void *args);
|
||||
|
||||
pid_t CloneWrapper(int (*func)(void *), int flag, void *args);
|
||||
@@ -79,15 +99,56 @@ std::string GenContainerLinkPath(int pid, const std::string& containerType);
|
||||
|
||||
extern std::string ReadlinkContainer(int pid, const std::string& containerType);
|
||||
|
||||
class MQueueFinalizer {
|
||||
public:
|
||||
explicit MQueueFinalizer(mqd_t mqueueParent, const std::string& mqname)
|
||||
{
|
||||
m_mqueueParent = mqueueParent;
|
||||
m_mqname = mqname;
|
||||
}
|
||||
~MQueueFinalizer()
|
||||
{
|
||||
if (m_mqueueParent >= 0) {
|
||||
mq_close(m_mqueueParent);
|
||||
mq_unlink(m_mqname.c_str());
|
||||
}
|
||||
}
|
||||
private:
|
||||
mqd_t m_mqueueParent;
|
||||
std::string m_mqname;
|
||||
};
|
||||
|
||||
class ShmFinalizer {
|
||||
public:
|
||||
explicit ShmFinalizer(void* shm, int shmid)
|
||||
{
|
||||
m_shm = shm;
|
||||
m_shmid = shmid;
|
||||
}
|
||||
~ShmFinalizer()
|
||||
{
|
||||
shmdt(m_shm);
|
||||
shmctl(m_shmid, IPC_RMID, nullptr);
|
||||
}
|
||||
private:
|
||||
void* m_shm;
|
||||
int m_shmid;
|
||||
};
|
||||
|
||||
#if defined(LOSCFG_USER_TEST_SMOKE)
|
||||
void ItContainer001(void);
|
||||
void ItContainerChroot001(void);
|
||||
#if defined(LOSCFG_USER_TEST_PID_CONTAINER)
|
||||
void ItPidContainer023(void);
|
||||
void ItPidContainer025(void);
|
||||
void ItPidContainer026(void);
|
||||
void ItPidContainer027(void);
|
||||
void ItPidContainer028(void);
|
||||
#endif
|
||||
#if defined(LOSCFG_USER_TEST_UTS_CONTAINER)
|
||||
void ItUtsContainer001(void);
|
||||
void ItUtsContainer002(void);
|
||||
void ItUtsContainer004(void);
|
||||
#endif
|
||||
#if defined(LOSCFG_USER_TEST_MNT_CONTAINER)
|
||||
void ItMntContainer001(void);
|
||||
@@ -99,6 +160,26 @@ void ItMntContainer006(void);
|
||||
void ItMntContainer007(void);
|
||||
void ItMntContainer008(void);
|
||||
#endif
|
||||
#if defined(LOSCFG_USER_TEST_IPC_CONTAINER)
|
||||
void ItIpcContainer001(void);
|
||||
void ItIpcContainer002(void);
|
||||
void ItIpcContainer003(void);
|
||||
void ItIpcContainer004(void);
|
||||
void ItIpcContainer005(void);
|
||||
void ItIpcContainer006(void);
|
||||
#endif
|
||||
#if defined(LOSCFG_USER_TEST_TIME_CONTAINER)
|
||||
void ItTimeContainer001(void);
|
||||
void ItTimeContainer002(void);
|
||||
void ItTimeContainer003(void);
|
||||
void ItTimeContainer004(void);
|
||||
void ItTimeContainer005(void);
|
||||
void ItTimeContainer006(void);
|
||||
void ItTimeContainer007(void);
|
||||
void ItTimeContainer008(void);
|
||||
void ItTimeContainer009(void);
|
||||
void ItTimeContainer010(void);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(LOSCFG_USER_TEST_FULL)
|
||||
|
||||
@@ -44,8 +44,13 @@ sources_smoke = [
|
||||
sources_full = []
|
||||
|
||||
if (defined(LOSCFG_USER_TEST_PID_CONTAINER)) {
|
||||
sources_smoke +=
|
||||
[ "$TEST_UNITTEST_DIR/container/smoke/It_pid_container_023.cpp" ]
|
||||
sources_smoke += [
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_023.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_025.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_026.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_027.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_028.cpp",
|
||||
]
|
||||
sources_full += [
|
||||
"$TEST_UNITTEST_DIR/container/full/It_pid_container_001.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/full/It_pid_container_002.cpp",
|
||||
@@ -75,6 +80,7 @@ if (defined(LOSCFG_USER_TEST_UTS_CONTAINER)) {
|
||||
sources_smoke += [
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_uts_container_001.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_uts_container_002.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_uts_container_004.cpp",
|
||||
]
|
||||
sources_full +=
|
||||
[ "$TEST_UNITTEST_DIR/container/full/It_uts_container_003.cpp" ]
|
||||
@@ -91,3 +97,29 @@ if (defined(LOSCFG_USER_TEST_MNT_CONTAINER)) {
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_008.cpp",
|
||||
]
|
||||
}
|
||||
|
||||
if (defined(LOSCFG_USER_TEST_IPC_CONTAINER)) {
|
||||
sources_smoke += [
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_001.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_002.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_003.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_004.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_005.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_006.cpp",
|
||||
]
|
||||
}
|
||||
|
||||
if (defined(LOSCFG_USER_TEST_TIME_CONTAINER)) {
|
||||
sources_smoke += [
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_001.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_002.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_003.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_004.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_005.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_006.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_007.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_008.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_009.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_time_container_010.cpp",
|
||||
]
|
||||
}
|
||||
|
||||
129
testsuites/unittest/container/smoke/It_ipc_container_001.cpp
Normal file
129
testsuites/unittest/container/smoke/It_ipc_container_001.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include "It_container_test.h"
|
||||
using namespace std;
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
int ret;
|
||||
(void)arg;
|
||||
mqd_t mqueueChild;
|
||||
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
|
||||
char mqname[MQUEUE_STANDARD_NAME_LENGTH] = "/testMQueue001";
|
||||
const char msgptr[] = "childMsgs";
|
||||
struct mq_attr attr = { 0 };
|
||||
attr.mq_msgsize = MQUEUE_TEST_SIZE;
|
||||
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
|
||||
mqueueChild = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, &attr);
|
||||
if (mqueueChild == (mqd_t)-1) {
|
||||
goto EXIT1;
|
||||
}
|
||||
ret = mq_send(mqueueChild, msgptr, strlen(msgptr), 0);
|
||||
if (ret != 0) {
|
||||
goto EXIT1;
|
||||
}
|
||||
ret = mq_receive(mqueueChild, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
|
||||
if (ret == -1) {
|
||||
goto EXIT1;
|
||||
}
|
||||
if (strncmp(msgrcd, msgptr, strlen(msgptr)) != 0) {
|
||||
goto EXIT1;
|
||||
}
|
||||
|
||||
EXIT1:
|
||||
if (mqueueChild >= 0) {
|
||||
ret = mq_close(mqueueChild);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
ret = mq_unlink(mqname);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
}
|
||||
return EXIT_CODE_ERRNO_7;
|
||||
}
|
||||
|
||||
void ItIpcContainer001(void)
|
||||
{
|
||||
uint32_t ret;
|
||||
int status;
|
||||
int exitCode;
|
||||
pid_t pid;
|
||||
mqd_t mqueueParent;
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
|
||||
char mqname[MQUEUE_STANDARD_NAME_LENGTH] = "/testMQueue001";
|
||||
const char msgptr[] = "parentMsg";
|
||||
struct mq_attr attr = { 0 };
|
||||
attr.mq_msgsize = MQUEUE_TEST_SIZE;
|
||||
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
|
||||
|
||||
mqueueParent = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, NULL);
|
||||
MQueueFinalizer mQueueFinalizer(mqueueParent, mqname);
|
||||
|
||||
ASSERT_NE(mqueueParent, (mqd_t)-1);
|
||||
|
||||
(void)memset_s(&attr, sizeof(attr), 0, sizeof(attr));
|
||||
ret = mq_getattr(mqueueParent, &attr);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
attr.mq_flags |= O_NONBLOCK;
|
||||
ret = mq_setattr(mqueueParent, &attr, NULL);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = mq_getattr(mqueueParent, &attr);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = mq_send(mqueueParent, msgptr, strlen(msgptr), 0);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
pid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
|
||||
ASSERT_NE(pid, -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, EXIT_CODE_ERRNO_7);
|
||||
|
||||
ret = mq_receive(mqueueParent, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
|
||||
ASSERT_NE(ret, -1);
|
||||
ASSERT_STREQ(msgrcd, msgptr);
|
||||
}
|
||||
145
testsuites/unittest/container/smoke/It_ipc_container_002.cpp
Normal file
145
testsuites/unittest/container/smoke/It_ipc_container_002.cpp
Normal file
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include "It_container_test.h"
|
||||
using namespace std;
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
int ret;
|
||||
(void)arg;
|
||||
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
|
||||
char mqname[] = "/testMQueue003";
|
||||
const char msgptr[] = "childMsgs";
|
||||
struct mq_attr attr = { 0 };
|
||||
mqd_t mqueue;
|
||||
attr.mq_msgsize = MQUEUE_TEST_SIZE;
|
||||
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
|
||||
|
||||
mqueue = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, &attr);
|
||||
if (mqueue == (mqd_t)-1) {
|
||||
goto EXIT1;
|
||||
}
|
||||
ret = mq_send(mqueue, msgptr, strlen(msgptr), 0);
|
||||
if (ret != 0) {
|
||||
goto EXIT1;
|
||||
}
|
||||
ret = mq_receive(mqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
|
||||
if (ret != strlen(msgptr)) {
|
||||
goto EXIT1;
|
||||
}
|
||||
if (strncmp(msgrcd, msgptr, strlen(msgptr)) != 0) {
|
||||
goto EXIT1;
|
||||
}
|
||||
|
||||
EXIT1:
|
||||
if (mqueue >= 0) {
|
||||
ret = mq_close(mqueue);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
ret = mq_unlink(mqname);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
}
|
||||
return EXIT_CODE_ERRNO_7;
|
||||
}
|
||||
|
||||
void ItIpcContainer002(void)
|
||||
{
|
||||
uint32_t ret;
|
||||
int status;
|
||||
int exitCode;
|
||||
mqd_t mqueue;
|
||||
pid_t childPid;
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
struct mq_attr attr = { 0 };
|
||||
attr.mq_msgsize = MQUEUE_TEST_SIZE;
|
||||
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
|
||||
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
|
||||
char mqname[] = "/testMQueue004";
|
||||
const char msgptr[] = "parentMsg";
|
||||
std::string containerType = "ipc";
|
||||
std::string filePath;
|
||||
int fd;
|
||||
int parentPid;
|
||||
std::string parentlink;
|
||||
std::string childlink;
|
||||
|
||||
mqueue = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, NULL);
|
||||
MQueueFinalizer mQueueFinalizer(mqueue, mqname);
|
||||
|
||||
ASSERT_NE(mqueue, (mqd_t)-1);
|
||||
|
||||
(void)memset_s(&attr, sizeof(attr), 0, sizeof(attr));
|
||||
ret = mq_getattr(mqueue, &attr);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
attr.mq_flags |= O_NONBLOCK;
|
||||
ret = mq_setattr(mqueue, &attr, NULL);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = mq_getattr(mqueue, &attr);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = mq_send(mqueue, msgptr, strlen(msgptr), 0);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
childPid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
|
||||
ASSERT_NE(childPid, -1);
|
||||
|
||||
parentPid = getpid();
|
||||
parentlink = ReadlinkContainer(parentPid, containerType);
|
||||
childlink = ReadlinkContainer(childPid, containerType);
|
||||
filePath = GenContainerLinkPath(childPid, containerType);
|
||||
fd = open(filePath.c_str(), O_RDONLY);
|
||||
ASSERT_NE(fd, -1);
|
||||
|
||||
ret = setns(fd, CLONE_NEWIPC);
|
||||
ASSERT_NE(ret, -1);
|
||||
|
||||
ret = close(fd);
|
||||
ASSERT_NE(ret, -1);
|
||||
|
||||
ret = waitpid(childPid, &status, 0);
|
||||
ASSERT_EQ(ret, childPid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, EXIT_CODE_ERRNO_7);
|
||||
}
|
||||
163
testsuites/unittest/container/smoke/It_ipc_container_003.cpp
Normal file
163
testsuites/unittest/container/smoke/It_ipc_container_003.cpp
Normal file
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include "It_container_test.h"
|
||||
using namespace std;
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
int ret;
|
||||
(void)arg;
|
||||
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
|
||||
char mqname[] = "/testMQueue005";
|
||||
const char msgptr[] = "childMsg";
|
||||
struct mq_attr attr = { 0 };
|
||||
mqd_t mqueue;
|
||||
attr.mq_msgsize = MQUEUE_TEST_SIZE;
|
||||
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
|
||||
|
||||
mqueue = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, &attr);
|
||||
if (mqueue == (mqd_t)-1) {
|
||||
goto EXIT1;
|
||||
}
|
||||
ret = mq_send(mqueue, msgptr, strlen(msgptr), 0);
|
||||
if (ret != 0) {
|
||||
goto EXIT1;
|
||||
}
|
||||
ret = mq_receive(mqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
|
||||
if (ret != strlen(msgptr)) {
|
||||
goto EXIT1;
|
||||
}
|
||||
if (strncmp(msgrcd, msgptr, strlen(msgptr)) != 0) {
|
||||
goto EXIT1;
|
||||
}
|
||||
|
||||
EXIT1:
|
||||
if (mqueue >= 0) {
|
||||
ret = mq_close(mqueue);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
ret = mq_unlink(mqname);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
}
|
||||
return EXIT_CODE_ERRNO_7;
|
||||
}
|
||||
|
||||
static void IpcContainerUnshare(void)
|
||||
{
|
||||
int status, exitCode, ret;
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
struct mq_attr attr = { 0 };
|
||||
attr.mq_msgsize = MQUEUE_TEST_SIZE;
|
||||
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
|
||||
struct sigevent notification;
|
||||
notification.sigev_notify = 5; /* 5: test data */
|
||||
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
|
||||
char mqname[] = "/testMQueue006";
|
||||
const char msgptr[] = "parentMsg";
|
||||
|
||||
ret = unshare(CLONE_NEWIPC);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
mqd_t mqueue = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, &attr);
|
||||
MQueueFinalizer mQueueFinalizer(mqueue, mqname);
|
||||
|
||||
ASSERT_NE(mqueue, (mqd_t)-1);
|
||||
|
||||
ret = mq_notify(mqueue, ¬ification);
|
||||
ASSERT_EQ(ret, -1);
|
||||
ASSERT_EQ(errno, EINVAL);
|
||||
|
||||
notification.sigev_notify = SIGEV_THREAD;
|
||||
ret = mq_notify(mqueue, ¬ification);
|
||||
ASSERT_EQ(ret, -1);
|
||||
ASSERT_EQ(errno, ENOTSUP);
|
||||
|
||||
notification.sigev_notify = SIGEV_NONE;
|
||||
|
||||
ret = mq_notify(-1, ¬ification);
|
||||
ASSERT_EQ(ret, -1);
|
||||
ASSERT_EQ(errno, EBADF);
|
||||
|
||||
ret = mq_notify(mqueue, ¬ification);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
(void)memset_s(&attr, sizeof(attr), 0, sizeof(attr));
|
||||
ret = mq_getattr(mqueue, &attr);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
attr.mq_flags |= O_NONBLOCK;
|
||||
ret = mq_setattr(mqueue, &attr, NULL);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = mq_getattr(mqueue, &attr);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = mq_send(mqueue, msgptr, strlen(msgptr), 0);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
pid_t pid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
|
||||
ASSERT_NE(pid, -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(pid, 0);
|
||||
|
||||
exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, EXIT_CODE_ERRNO_7);
|
||||
|
||||
ret = mq_notify(mqueue, nullptr);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = mq_receive(mqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
|
||||
ASSERT_EQ(ret, strlen(msgptr));
|
||||
ASSERT_STREQ(msgrcd, msgptr);
|
||||
}
|
||||
|
||||
void ItIpcContainer003(void)
|
||||
{
|
||||
auto pid = fork();
|
||||
ASSERT_TRUE(pid != -1);
|
||||
if (pid == 0) {
|
||||
IpcContainerUnshare();
|
||||
exit(0);
|
||||
}
|
||||
auto ret = waitpid(pid, NULL, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
}
|
||||
217
testsuites/unittest/container/smoke/It_ipc_container_004.cpp
Normal file
217
testsuites/unittest/container/smoke/It_ipc_container_004.cpp
Normal file
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
using namespace std;
|
||||
|
||||
static const char *containerType = "ipc";
|
||||
|
||||
struct shared_use_st {
|
||||
char test[SHM_TEST_DATA_SIZE];
|
||||
};
|
||||
|
||||
static int childFunc1(void *arg)
|
||||
{
|
||||
struct shared_use_st *shared = NULL;
|
||||
const char testBuf[] = "child test shm";
|
||||
int ret;
|
||||
(void)arg;
|
||||
|
||||
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
|
||||
if (shmid == -1) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
void *shm = shmat(shmid, 0, 0);
|
||||
if (shm == reinterpret_cast<void *>(-1)) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
shared = (struct shared_use_st *)shm;
|
||||
ret = strncmp(shared->test, testBuf, strlen(testBuf));
|
||||
if (ret != 0) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
ret = shmdt(shm);
|
||||
if (ret == -1) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_4;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
const char testBuf[] = "parent test shm";
|
||||
const char testBuf1[] = "child test shm";
|
||||
int ret, status, pid, exitCode;
|
||||
(void)arg;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
if (stack == nullptr) {
|
||||
return EXIT_CODE_ERRNO_17;
|
||||
}
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
|
||||
|
||||
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
|
||||
if (shmid == -1) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
void *shm = shmat(shmid, 0, 0);
|
||||
if (shm == reinterpret_cast<void *>(-1)) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
|
||||
struct shared_use_st *shared = (struct shared_use_st *)shm;
|
||||
ret = strncmp(shared->test, testBuf, strlen(testBuf));
|
||||
if (ret == 0) {
|
||||
ret = EXIT_CODE_ERRNO_3;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
ret = memcpy_s(shared->test, sizeof(struct shared_use_st), testBuf1, sizeof(testBuf1));
|
||||
if (ret != 0) {
|
||||
ret = EXIT_CODE_ERRNO_4;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
pid = clone(childFunc1, stackTop, SIGCHLD, &arg);
|
||||
if (pid == -1) {
|
||||
ret = EXIT_CODE_ERRNO_5;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
if (ret != pid) {
|
||||
ret = EXIT_CODE_ERRNO_6;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
if (ret == 0) {
|
||||
ret = EXIT_CODE_ERRNO_7;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
exitCode = WEXITSTATUS(status);
|
||||
if (exitCode != 0) {
|
||||
ret = EXIT_CODE_ERRNO_8;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
ret = shmdt(shm);
|
||||
if (ret == -1) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_9;
|
||||
}
|
||||
|
||||
ret = shmctl(shmid, IPC_RMID, 0);
|
||||
if (ret == -1) {
|
||||
return EXIT_CODE_ERRNO_10;
|
||||
}
|
||||
|
||||
return 0;
|
||||
EXIT:
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ItIpcContainer004(void)
|
||||
{
|
||||
const char testBuf[] = "parent test shm";
|
||||
int pid, exitCode, status, ret;
|
||||
void *shm = NULL;
|
||||
struct shmid_ds ds = {};
|
||||
struct shminfo info = {};
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
|
||||
ShmFinalizer ShmFinalizer(shm, shmid);
|
||||
ASSERT_NE(shmid, -1);
|
||||
|
||||
shm = shmat(shmid, 0, 0);
|
||||
ASSERT_NE((int)shm, -1);
|
||||
|
||||
struct shared_use_st *shared = (struct shared_use_st *)shm;
|
||||
ret = memcpy_s(shared->test, sizeof(struct shared_use_st), testBuf, sizeof(testBuf));
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
pid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
|
||||
ASSERT_NE(pid, -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
|
||||
ret = shmctl(shmid, IPC_STAT, &ds);
|
||||
ASSERT_EQ(ret, 0);
|
||||
ASSERT_EQ(ds.shm_segsz, PAGE_SIZE);
|
||||
ASSERT_EQ(ds.shm_nattch, 1);
|
||||
ASSERT_EQ(ds.shm_cpid, getpid());
|
||||
ASSERT_EQ(ds.shm_lpid, getpid());
|
||||
ASSERT_EQ(ds.shm_perm.uid, getuid());
|
||||
|
||||
ret = shmctl(shmid, SHM_STAT, &ds);
|
||||
ASSERT_NE(ret, -1);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
ds.shm_perm.uid = getuid();
|
||||
ds.shm_perm.gid = getgid();
|
||||
ds.shm_perm.mode = 0;
|
||||
ret = shmctl(shmid, IPC_SET, &ds);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = shmctl(shmid, IPC_INFO, (struct shmid_ds *)&info);
|
||||
ASSERT_EQ(ret, 192); /* 192: test value */
|
||||
ASSERT_EQ(info.shmmax, 0x1000000); /* 0x1000000: Shared memory information */
|
||||
ASSERT_EQ(info.shmmin, 1); /* 1: Shared memory information */
|
||||
ASSERT_EQ(info.shmmni, 192); /* 192: Shared memory information */
|
||||
ASSERT_EQ(info.shmseg, 128); /* 128: Shared memory information */
|
||||
ASSERT_EQ(info.shmall, 0x1000); /* 0x1000: Shared memory information */
|
||||
|
||||
ret = shmdt(shm);
|
||||
ASSERT_NE(ret, -1);
|
||||
|
||||
ret = shmctl(shmid, IPC_RMID, NULL);
|
||||
ASSERT_EQ(ret, 0);
|
||||
}
|
||||
187
testsuites/unittest/container/smoke/It_ipc_container_005.cpp
Normal file
187
testsuites/unittest/container/smoke/It_ipc_container_005.cpp
Normal file
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
using namespace std;
|
||||
|
||||
static const char testBuf[] = "test shm";
|
||||
struct shared_use_st {
|
||||
char test[SHM_TEST_DATA_SIZE];
|
||||
};
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
struct shared_use_st *shared = NULL;
|
||||
int ret;
|
||||
(void)arg;
|
||||
char *containerType = "ipc";
|
||||
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
|
||||
|
||||
ret = unshare(CLONE_NEWIPC);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType);
|
||||
ret = linkBuffer.compare(linkBuffer1);
|
||||
if (ret == 0) {
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
|
||||
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
|
||||
if (shmid == -1) {
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
|
||||
void *shm = shmat(shmid, 0, 0);
|
||||
if (shm == reinterpret_cast<void *>(-1)) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_4;
|
||||
}
|
||||
|
||||
shared = (struct shared_use_st *)shm;
|
||||
ret = strncmp(shared->test, testBuf, strlen(testBuf));
|
||||
if (ret == 0) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_5;
|
||||
}
|
||||
|
||||
ret = shmdt(shm);
|
||||
if (ret == -1) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_6;
|
||||
}
|
||||
|
||||
ret = shmctl(shmid, IPC_RMID, 0);
|
||||
if (ret == -1) {
|
||||
return EXIT_CODE_ERRNO_7;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int testChild(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
int ret;
|
||||
struct shared_use_st *shared = NULL;
|
||||
int status;
|
||||
int exitCode;
|
||||
int pid;
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
if (stack == nullptr) {
|
||||
return EXIT_CODE_ERRNO_17;
|
||||
}
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
|
||||
if (shmid == -1) {
|
||||
return EXIT_CODE_ERRNO_8;
|
||||
}
|
||||
|
||||
void *shm = shmat(shmid, 0, 0);
|
||||
if (shm == reinterpret_cast<void *>(-1)) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_9;
|
||||
}
|
||||
|
||||
shared = (struct shared_use_st *)shm;
|
||||
ret = memcpy_s(shared->test, sizeof(struct shared_use_st), testBuf, sizeof(testBuf));
|
||||
if (ret != 0) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_10;
|
||||
}
|
||||
|
||||
pid = clone(childFunc, stackTop, SIGCHLD, &arg);
|
||||
if (pid == -1) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_11;
|
||||
}
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
if (ret != pid) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_12;
|
||||
}
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
if (ret == 0) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_13;
|
||||
}
|
||||
|
||||
exitCode = WEXITSTATUS(status);
|
||||
if (exitCode != 0) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_14;
|
||||
}
|
||||
|
||||
ret = shmdt(shm);
|
||||
if (ret == -1) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_15;
|
||||
}
|
||||
|
||||
ret = shmctl(shmid, IPC_RMID, 0);
|
||||
if (ret == -1) {
|
||||
return EXIT_CODE_ERRNO_16;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ItIpcContainer005(void)
|
||||
{
|
||||
int ret;
|
||||
int status;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
auto pid = clone(testChild, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
|
||||
ASSERT_NE(pid, -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
int exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
}
|
||||
184
testsuites/unittest/container/smoke/It_ipc_container_006.cpp
Normal file
184
testsuites/unittest/container/smoke/It_ipc_container_006.cpp
Normal file
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
using namespace std;
|
||||
|
||||
static const char testBuf[] = "test shm";
|
||||
static const char *containerType = "ipc";
|
||||
struct shared_use_st {
|
||||
char test[SHM_TEST_DATA_SIZE];
|
||||
};
|
||||
|
||||
static int childFunc1(void *arg)
|
||||
{
|
||||
struct shared_use_st *shared = NULL;
|
||||
int ret;
|
||||
(void)arg;
|
||||
const int sleep_num = 3; /* 3: delay */
|
||||
|
||||
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
|
||||
if (shmid == -1) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
void *shm = shmat(shmid, 0, 0);
|
||||
if (shm == reinterpret_cast<void *>(-1)) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
shared = (struct shared_use_st *)shm;
|
||||
sleep(sleep_num);
|
||||
ret = strncmp(shared->test, testBuf, strlen(testBuf));
|
||||
if (ret != 0) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
|
||||
ret = shmdt(shm);
|
||||
if (ret == -1) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_4;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
struct shared_use_st *shared = NULL;
|
||||
int ret;
|
||||
(void)arg;
|
||||
int status;
|
||||
int exitCode;
|
||||
const int sleep_num = 1;
|
||||
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
if (stack == nullptr) {
|
||||
return EXIT_CODE_ERRNO_17;
|
||||
}
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
auto pid = clone(childFunc1, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
|
||||
if (pid == -1) {
|
||||
return EXIT_CODE_ERRNO_6;
|
||||
}
|
||||
|
||||
std::string filePath = GenContainerLinkPath(pid, containerType);
|
||||
int fd = open(filePath.c_str(), O_RDONLY);
|
||||
if (fd == -1) {
|
||||
return EXIT_CODE_ERRNO_7;
|
||||
}
|
||||
sleep(sleep_num);
|
||||
ret = setns(fd, CLONE_NEWIPC);
|
||||
if (ret == -1) {
|
||||
close(fd);
|
||||
return EXIT_CODE_ERRNO_8;
|
||||
}
|
||||
close(fd);
|
||||
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType);
|
||||
auto linkBuffer2 = ReadlinkContainer(pid, containerType);
|
||||
ret = linkBuffer.compare(linkBuffer1);
|
||||
if (ret == 0) {
|
||||
return EXIT_CODE_ERRNO_9;
|
||||
}
|
||||
ret = linkBuffer1.compare(linkBuffer2);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_10;
|
||||
}
|
||||
|
||||
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
|
||||
if (shmid == -1) {
|
||||
return EXIT_CODE_ERRNO_11;
|
||||
}
|
||||
|
||||
void *shm = shmat(shmid, 0, 0);
|
||||
if (shm == reinterpret_cast<void *>(-1)) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_12;
|
||||
}
|
||||
|
||||
shared = (struct shared_use_st *)shm;
|
||||
|
||||
ret = memcpy_s(shared->test, sizeof(struct shared_use_st), testBuf, sizeof(testBuf));
|
||||
if (ret != 0) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_13;
|
||||
}
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
if (ret != pid) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_14;
|
||||
}
|
||||
|
||||
exitCode = WEXITSTATUS(status);
|
||||
if (exitCode != 0) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_15;
|
||||
}
|
||||
|
||||
ret = shmdt(shm);
|
||||
if (ret == -1) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_16;
|
||||
}
|
||||
|
||||
ret = shmctl(shmid, IPC_RMID, 0);
|
||||
if (ret == -1) {
|
||||
return EXIT_CODE_ERRNO_255;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ItIpcContainer006(void)
|
||||
{
|
||||
int ret;
|
||||
int status;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
|
||||
ASSERT_NE(pid, -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
int exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
}
|
||||
@@ -34,7 +34,6 @@ static int ChildFunc(void *arg)
|
||||
{
|
||||
int ret;
|
||||
int value = *((int *)arg);
|
||||
|
||||
if (value != CHILD_FUNC_ARG) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
/*
|
||||
* mount container unshare test: unshare in current pcb with NEW_NS
|
||||
*/
|
||||
void ItMntContainer005(void)
|
||||
static void MntContainerUnshare(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
@@ -56,4 +56,22 @@ void ItMntContainer005(void)
|
||||
|
||||
ret = access(ACCESS_FILE_NAME, F_OK);
|
||||
ASSERT_EQ(ret, 0);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void ItMntContainer005(void)
|
||||
{
|
||||
int status = 0;
|
||||
auto pid = fork();
|
||||
ASSERT_TRUE(pid != -1);
|
||||
if (pid == 0) {
|
||||
MntContainerUnshare();
|
||||
exit(EXIT_CODE_ERRNO_1);
|
||||
}
|
||||
auto ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
ret = WEXITSTATUS(status);
|
||||
ASSERT_EQ(ret, 0);
|
||||
}
|
||||
|
||||
69
testsuites/unittest/container/smoke/It_pid_container_025.cpp
Normal file
69
testsuites/unittest/container/smoke/It_pid_container_025.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ItPidContainer025(void)
|
||||
{
|
||||
int ret;
|
||||
int status;
|
||||
char *containerType = "pid";
|
||||
char *containerType1 = "pid_for_children";
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
std::cout << getpid() << std::endl;
|
||||
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
|
||||
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType1);
|
||||
ret = linkBuffer.compare(linkBuffer1);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWPID | SIGCHLD, NULL);
|
||||
ASSERT_TRUE(pid != -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
int exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
|
||||
auto linkBuffer2 = ReadlinkContainer(getpid(), containerType);
|
||||
ret = linkBuffer.compare(linkBuffer2);
|
||||
ASSERT_EQ(ret, 0);
|
||||
}
|
||||
70
testsuites/unittest/container/smoke/It_pid_container_026.cpp
Normal file
70
testsuites/unittest/container/smoke/It_pid_container_026.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
sleep(1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ItPidContainer026(void)
|
||||
{
|
||||
int ret;
|
||||
int status;
|
||||
char *containerType = "pid";
|
||||
char *containerType1 = "pid_for_children";
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
std::cout << getpid() << std::endl;
|
||||
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
|
||||
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType1);
|
||||
ret = linkBuffer.compare(linkBuffer1);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWPID | SIGCHLD, NULL);
|
||||
ASSERT_TRUE(pid != -1);
|
||||
|
||||
auto linkBuffer2 = ReadlinkContainer(pid, containerType);
|
||||
ret = linkBuffer.compare(linkBuffer2);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
int exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
}
|
||||
148
testsuites/unittest/container/smoke/It_pid_container_027.cpp
Normal file
148
testsuites/unittest/container/smoke/It_pid_container_027.cpp
Normal file
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
|
||||
static int ChildFunClone3(void *p)
|
||||
{
|
||||
(void)p;
|
||||
auto pid = getpid();
|
||||
if (pid != CONTAINER_SECOND_PID) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ChildFunClone2(void *p)
|
||||
{
|
||||
(void)p;
|
||||
auto pid = getpid();
|
||||
if (pid != CONTAINER_FIRST_PID) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
int ret;
|
||||
int status;
|
||||
void *pstk = malloc(STACK_SIZE);
|
||||
if (pstk == NULL) {
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
int childPid = clone(ChildFunClone3, (char *)pstk + STACK_SIZE, SIGCHLD, NULL);
|
||||
if (childPid == -1) {
|
||||
free(pstk);
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
|
||||
ret = waitpid(childPid, &status, 0);
|
||||
ret = WIFEXITED(status);
|
||||
ret = WEXITSTATUS(status);
|
||||
if (ret != 0) {
|
||||
free(pstk);
|
||||
return EXIT_CODE_ERRNO_4;
|
||||
}
|
||||
|
||||
free(pstk);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int ChildFunClone1(void *p)
|
||||
{
|
||||
(void)p;
|
||||
int ret;
|
||||
int status;
|
||||
const char *containerType = "pid";
|
||||
const char *containerType1 = "pid_for_children";
|
||||
|
||||
auto pid = getpid();
|
||||
ret = unshare(CLONE_NEWPID);
|
||||
if (ret == -1) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
auto pid1 = getpid();
|
||||
if (pid != pid1) {
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
|
||||
auto linkBuffer = ReadlinkContainer(pid, containerType);
|
||||
auto linkBuffer1 = ReadlinkContainer(pid, containerType1);
|
||||
ret = linkBuffer.compare(linkBuffer1);
|
||||
if (ret == 0) {
|
||||
printf("linkBuffer: %s linkBuffer1: %s\n", linkBuffer.c_str(), linkBuffer1.c_str());
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
|
||||
void *pstk = malloc(STACK_SIZE);
|
||||
if (pstk == NULL) {
|
||||
return EXIT_CODE_ERRNO_4;
|
||||
}
|
||||
int childPid = clone(ChildFunClone2, (char *)pstk + STACK_SIZE, CLONE_NEWUTS | SIGCHLD, NULL);
|
||||
free(pstk);
|
||||
if (childPid == -1) {
|
||||
return EXIT_CODE_ERRNO_5;
|
||||
}
|
||||
|
||||
ret = unshare(CLONE_NEWPID);
|
||||
if (ret != -1) {
|
||||
return EXIT_CODE_ERRNO_6;
|
||||
}
|
||||
|
||||
ret = waitpid(childPid, &status, 0);
|
||||
if (ret != childPid) {
|
||||
return EXIT_CODE_ERRNO_7;
|
||||
}
|
||||
ret = WIFEXITED(status);
|
||||
if (ret == 0) {
|
||||
return EXIT_CODE_ERRNO_8;
|
||||
}
|
||||
ret = WEXITSTATUS(status);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_9;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ItPidContainer027(void)
|
||||
{
|
||||
void *pstk = malloc(STACK_SIZE);
|
||||
ASSERT_TRUE(pstk != NULL);
|
||||
pid_t parentPid = getpid();
|
||||
|
||||
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);
|
||||
}
|
||||
146
testsuites/unittest/container/smoke/It_pid_container_028.cpp
Normal file
146
testsuites/unittest/container/smoke/It_pid_container_028.cpp
Normal file
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
|
||||
static 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_NEWPID | SIGCHLD, NULL);
|
||||
free(pstk);
|
||||
if (childPid == -1) {
|
||||
return EXIT_CODE_ERRNO_5;
|
||||
}
|
||||
|
||||
ret = unshare(CLONE_NEWPID);
|
||||
if (ret != -1) {
|
||||
return EXIT_CODE_ERRNO_6;
|
||||
}
|
||||
|
||||
ret = waitpid(childPid, &status, 0);
|
||||
if (ret != childPid) {
|
||||
return EXIT_CODE_ERRNO_7;
|
||||
}
|
||||
ret = WIFEXITED(status);
|
||||
if (ret == 0) {
|
||||
return EXIT_CODE_ERRNO_8;
|
||||
}
|
||||
ret = WEXITSTATUS(status);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_9;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ItPidContainer028(void)
|
||||
{
|
||||
void *pstk = malloc(STACK_SIZE);
|
||||
ASSERT_TRUE(pstk != NULL);
|
||||
pid_t parentPid = getpid();
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
int value = *((int*)arg);
|
||||
if (value != CHILD_FUNC_ARG) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
sleep(1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ItTimeContainer001(void)
|
||||
{
|
||||
int ret;
|
||||
int status;
|
||||
char *containerType = "time";
|
||||
char *containerType1 = "time_for_children";
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
std::cout << getpid() << std::endl;
|
||||
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
|
||||
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType1);
|
||||
ret = linkBuffer.compare(linkBuffer1);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWTIME | SIGCHLD, &arg);
|
||||
ASSERT_TRUE(pid != -1);
|
||||
|
||||
auto linkBuffer2 = ReadlinkContainer(pid, containerType);
|
||||
ret = linkBuffer.compare(linkBuffer2);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
int exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
}
|
||||
165
testsuites/unittest/container/smoke/It_time_container_002.cpp
Normal file
165
testsuites/unittest/container/smoke/It_time_container_002.cpp
Normal file
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
|
||||
const int STR_LEN = 50;
|
||||
const int SEC = 600;
|
||||
const int NSEC = 800000000;
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
char path[STR_LEN];
|
||||
char timeOff[STR_LEN];
|
||||
char readBuf[STR_LEN];
|
||||
int ret;
|
||||
|
||||
ret = sprintf_s(timeOff, STR_LEN, "monotonic %d %d", SEC, NSEC);
|
||||
if (ret <= 0) {
|
||||
return EXIT_CODE_ERRNO_4;
|
||||
}
|
||||
ret = sprintf_s(path, STR_LEN, "/proc/%d/time_offsets", getpid());
|
||||
if (ret <= 0) {
|
||||
return EXIT_CODE_ERRNO_5;
|
||||
}
|
||||
|
||||
int fd = open(path, O_RDWR);
|
||||
if (fd == -1) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
ret = read(fd, readBuf, STR_LEN);
|
||||
if (ret == -1) {
|
||||
close(fd);
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
close(fd);
|
||||
ret = strncmp(timeOff, readBuf, strlen(timeOff));
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int WriteProcTime(int pid)
|
||||
{
|
||||
int ret = 0;
|
||||
char path[STR_LEN];
|
||||
char timeOff[STR_LEN];
|
||||
|
||||
ret = sprintf_s(timeOff, STR_LEN, "monotonic %d %d", SEC, NSEC);
|
||||
if (ret <= 0) {
|
||||
return EXIT_CODE_ERRNO_6;
|
||||
}
|
||||
ret = sprintf_s(path, STR_LEN, "/proc/%d/time_offsets", pid);
|
||||
if (ret <= 0) {
|
||||
return EXIT_CODE_ERRNO_7;
|
||||
}
|
||||
|
||||
int strLen = strlen(timeOff);
|
||||
int fd = open(path, O_WRONLY);
|
||||
if (ret == -1) {
|
||||
return EXIT_CODE_ERRNO_8;
|
||||
}
|
||||
|
||||
ret = write(fd, timeOff, strLen);
|
||||
if (ret != strLen) {
|
||||
close(fd);
|
||||
return EXIT_CODE_ERRNO_9;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void TimeContainerUnshare(void)
|
||||
{
|
||||
int ret;
|
||||
int status;
|
||||
char *containerType = "time";
|
||||
char *containerType1 = "time_for_children";
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
ret = unshare(CLONE_NEWTIME);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = unshare(CLONE_NEWTIME);
|
||||
ASSERT_EQ(ret, -1);
|
||||
|
||||
ret = WriteProcTime(getpid());
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
|
||||
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType1);
|
||||
ret = linkBuffer.compare(linkBuffer1);
|
||||
ASSERT_TRUE(ret != 0);
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWTIME | SIGCHLD, &arg);
|
||||
ASSERT_TRUE(pid != -1);
|
||||
|
||||
auto linkBuffer2 = ReadlinkContainer(pid, containerType);
|
||||
ret = linkBuffer1.compare(linkBuffer2);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = unshare(CLONE_NEWTIME);
|
||||
ASSERT_EQ(ret, -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_TRUE(ret != 0);
|
||||
|
||||
int exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void ItTimeContainer002(void)
|
||||
{
|
||||
int status = 0;
|
||||
auto pid = fork();
|
||||
ASSERT_TRUE(pid != -1);
|
||||
if (pid == 0) {
|
||||
TimeContainerUnshare();
|
||||
exit(EXIT_CODE_ERRNO_1);
|
||||
}
|
||||
auto ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
ret = WEXITSTATUS(status);
|
||||
ASSERT_EQ(ret, 0);
|
||||
}
|
||||
119
testsuites/unittest/container/smoke/It_time_container_003.cpp
Normal file
119
testsuites/unittest/container/smoke/It_time_container_003.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
int ret;
|
||||
char *containerType = "time";
|
||||
char *containerType1 = "time_for_children";
|
||||
char pid_link[100]; /* 100: test len */
|
||||
char targetpath[100]; /* 100: test len */
|
||||
auto linkBuffer = ReadlinkContainer(getppid(), containerType);
|
||||
int fd;
|
||||
|
||||
ret = sprintf_s(pid_link, 100, "%s", linkBuffer.c_str()); /* 100: test len */
|
||||
if (ret <= 0) {
|
||||
return EXIT_CODE_ERRNO_4;
|
||||
}
|
||||
ret = sprintf_s(targetpath, 100, "/proc/%d/container/time", getppid()); /* 100: test len */
|
||||
if (ret <= 0) {
|
||||
return EXIT_CODE_ERRNO_5;
|
||||
}
|
||||
fd = open(targetpath, O_RDONLY | O_CLOEXEC);
|
||||
if (fd == -1) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
ret = setns(fd, CLONE_NEWTIME);
|
||||
if (ret != 0) {
|
||||
close(fd);
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
close(fd);
|
||||
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType);
|
||||
auto linkBuffer2 = ReadlinkContainer(getpid(), containerType1);
|
||||
ret = linkBuffer1.compare(linkBuffer2);
|
||||
EXPECT_EQ(ret, 0);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void TimeContainerUnshare(void)
|
||||
{
|
||||
int ret;
|
||||
int status;
|
||||
char *containerType = "time";
|
||||
char *containerType1 = "time_for_children";
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
ret = unshare(CLONE_NEWTIME);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
auto linkBuffer = ReadlinkContainer(getpid(), containerType1);
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWTIME | SIGCHLD, &arg);
|
||||
ASSERT_TRUE(pid != -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_TRUE(ret != 0);
|
||||
|
||||
int exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void ItTimeContainer003(void)
|
||||
{
|
||||
int status = 0;
|
||||
auto pid = fork();
|
||||
ASSERT_TRUE(pid != -1);
|
||||
if (pid == 0) {
|
||||
TimeContainerUnshare();
|
||||
exit(EXIT_CODE_ERRNO_1);
|
||||
}
|
||||
auto ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
ret = WEXITSTATUS(status);
|
||||
ASSERT_EQ(ret, 0);
|
||||
}
|
||||
|
||||
133
testsuites/unittest/container/smoke/It_time_container_004.cpp
Normal file
133
testsuites/unittest/container/smoke/It_time_container_004.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
|
||||
const int STR_LEN = 50;
|
||||
const int SEC = 172800;
|
||||
const int NSEC = 800000000;
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
int ret;
|
||||
struct timespec tp = *((struct timespec *)arg);
|
||||
struct timespec tp1;
|
||||
|
||||
ret = clock_gettime(CLOCK_MONOTONIC_RAW, &tp1);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
ret = ((tp1.tv_sec >= tp.tv_sec + SEC));
|
||||
if (ret != 1) {
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
static int WriteProcTime(int pid)
|
||||
{
|
||||
int ret;
|
||||
char path[STR_LEN];
|
||||
char timeOff[STR_LEN];
|
||||
|
||||
ret = sprintf_s(timeOff, STR_LEN, "monotonic %d %d", SEC, NSEC);
|
||||
if (ret <= 0) {
|
||||
return EXIT_CODE_ERRNO_5;
|
||||
}
|
||||
ret = sprintf_s(path, STR_LEN, "/proc/%d/time_offsets", pid);
|
||||
if (ret <= 0) {
|
||||
return EXIT_CODE_ERRNO_6;
|
||||
}
|
||||
int strLen = strlen(timeOff);
|
||||
int fd = open(path, O_WRONLY);
|
||||
if (fd == -1) {
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
|
||||
ret = write(fd, timeOff, strLen);
|
||||
if (ret != strLen) {
|
||||
close(fd);
|
||||
return EXIT_CODE_ERRNO_4;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void TimeContainerUnshare(void)
|
||||
{
|
||||
int ret;
|
||||
int status;
|
||||
struct timespec tp;
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
ret = unshare(CLONE_NEWTIME);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = WriteProcTime(getpid());
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWTIME | SIGCHLD, &tp);
|
||||
ASSERT_TRUE(pid != -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
int exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void ItTimeContainer004(void)
|
||||
{
|
||||
int status = 0;
|
||||
auto pid = fork();
|
||||
ASSERT_TRUE(pid != -1);
|
||||
if (pid == 0) {
|
||||
TimeContainerUnshare();
|
||||
exit(EXIT_CODE_ERRNO_1);
|
||||
}
|
||||
auto ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
ret = WEXITSTATUS(status);
|
||||
ASSERT_EQ(ret, 0);
|
||||
}
|
||||
133
testsuites/unittest/container/smoke/It_time_container_005.cpp
Normal file
133
testsuites/unittest/container/smoke/It_time_container_005.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
|
||||
const int STR_LEN = 50;
|
||||
const int SEC = 172800;
|
||||
const int NSEC = 800000000;
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
int ret;
|
||||
struct timespec tp = *((struct timespec *)arg);
|
||||
struct timespec tp1 = {0};
|
||||
|
||||
ret = clock_gettime(CLOCK_MONOTONIC, &tp1);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
ret = ((tp1.tv_sec >= tp.tv_sec + SEC));
|
||||
if (ret != 1) {
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
static int WriteProcTime(int pid)
|
||||
{
|
||||
int ret;
|
||||
char path[STR_LEN];
|
||||
char timeOff[STR_LEN];
|
||||
|
||||
ret = sprintf_s(timeOff, STR_LEN, "monotonic %d %d", SEC, NSEC);
|
||||
if (ret <= 0) {
|
||||
return EXIT_CODE_ERRNO_5;
|
||||
}
|
||||
ret = sprintf_s(path, STR_LEN, "/proc/%d/time_offsets", pid);
|
||||
if (ret <= 0) {
|
||||
return EXIT_CODE_ERRNO_5;
|
||||
}
|
||||
int strLen = strlen(timeOff);
|
||||
int fd = open(path, O_WRONLY);
|
||||
if (fd == -1) {
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
|
||||
ret = write(fd, timeOff, strLen);
|
||||
if (ret != strLen) {
|
||||
close(fd);
|
||||
return EXIT_CODE_ERRNO_4;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void TimeContainerUnshare(void)
|
||||
{
|
||||
int ret;
|
||||
int status;
|
||||
struct timespec tp = {0};
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
ret = unshare(CLONE_NEWTIME);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = WriteProcTime(getpid());
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = clock_gettime(CLOCK_MONOTONIC, &tp);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWTIME | SIGCHLD, &tp);
|
||||
ASSERT_TRUE(pid != -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_TRUE(ret != 0);
|
||||
|
||||
int exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void ItTimeContainer005(void)
|
||||
{
|
||||
int status = 0;
|
||||
auto pid = fork();
|
||||
ASSERT_TRUE(pid != -1);
|
||||
if (pid == 0) {
|
||||
TimeContainerUnshare();
|
||||
exit(EXIT_CODE_ERRNO_1);
|
||||
}
|
||||
auto ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
ret = WEXITSTATUS(status);
|
||||
ASSERT_EQ(ret, 0);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
|
||||
const int MAX_TIME_CONTAINER = 64;
|
||||
const int STR_LEN = 100;
|
||||
|
||||
void ItTimeContainer006(void)
|
||||
{
|
||||
int ret;
|
||||
char *fileName = "/proc/sys/user/max_time_container";
|
||||
FILE *fp = nullptr;
|
||||
char strBuf[STR_LEN] = {0};
|
||||
|
||||
fp = fopen(fileName, "rb");
|
||||
ASSERT_TRUE(fp != 0);
|
||||
|
||||
ret = fread(strBuf, 1, STR_LEN, fp);
|
||||
ASSERT_TRUE(ret != -1);
|
||||
|
||||
ret = atoi(strBuf);
|
||||
ASSERT_EQ(ret, MAX_TIME_CONTAINER);
|
||||
|
||||
(void)fclose(fp);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
|
||||
const int STR_LEN = 50;
|
||||
const int SEC = 172800;
|
||||
const int NSEC = 80000000;
|
||||
|
||||
static int WriteProcTime(int pid)
|
||||
{
|
||||
int ret;
|
||||
char path[STR_LEN] = {0};
|
||||
char timeOff[STR_LEN] = {0};
|
||||
|
||||
ret = sprintf_s(timeOff, STR_LEN, "monotonic %d %d", SEC, NSEC);
|
||||
if (ret < 0) {
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
ret = sprintf_s(path, STR_LEN, "/proc/%d/time_offsets", pid);
|
||||
if (ret < 0) {
|
||||
return EXIT_CODE_ERRNO_4;
|
||||
}
|
||||
|
||||
int strLen = strlen(timeOff);
|
||||
int fd = open(path, O_WRONLY);
|
||||
if (fd == -1) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
ret = write(fd, timeOff, strLen);
|
||||
if (ret != -1) {
|
||||
close(fd);
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ItTimeContainer007(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = WriteProcTime(getpid());
|
||||
ASSERT_EQ(ret, 0);
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
|
||||
const int STR_LEN = 50;
|
||||
const int SEC = 172800;
|
||||
const long long NSEC = 800000000000;
|
||||
|
||||
static int WriteProcTime(int pid)
|
||||
{
|
||||
int ret;
|
||||
char path[STR_LEN] = {0};
|
||||
char timeOff[STR_LEN] = {0};
|
||||
|
||||
ret = sprintf_s(timeOff, STR_LEN, "monotonic %d %lld", SEC, NSEC);
|
||||
if (ret < 0) {
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
ret = sprintf_s(path, STR_LEN, "/proc/%d/time_offsets", pid);
|
||||
if (ret < 0) {
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
int strLen = strlen(timeOff);
|
||||
int fd = open(path, O_WRONLY);
|
||||
if (fd == -1) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
ret = write(fd, timeOff, strLen);
|
||||
if (ret != -1) {
|
||||
close(fd);
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void TimeContainerUnshare(void)
|
||||
{
|
||||
int ret = unshare(CLONE_NEWTIME);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = WriteProcTime(getpid());
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void ItTimeContainer008(void)
|
||||
{
|
||||
int status = 0;
|
||||
auto pid = fork();
|
||||
ASSERT_TRUE(pid != -1);
|
||||
if (pid == 0) {
|
||||
TimeContainerUnshare();
|
||||
exit(EXIT_CODE_ERRNO_1);
|
||||
}
|
||||
auto ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
ret = WEXITSTATUS(status);
|
||||
ASSERT_EQ(ret, 0);
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include "It_container_test.h"
|
||||
|
||||
static std::string GenTimeLinkPath(int pid)
|
||||
{
|
||||
std::ostringstream buf;
|
||||
buf << "/proc/" << pid << "/container/time";
|
||||
return buf.str();
|
||||
}
|
||||
|
||||
static std::string ReadlinkTime(int pid)
|
||||
{
|
||||
auto path = GenTimeLinkPath(pid);
|
||||
struct stat sb;
|
||||
|
||||
int ret = lstat(path.data(), &sb);
|
||||
if (ret == -1) {
|
||||
perror("lstat");
|
||||
return std::string();
|
||||
}
|
||||
|
||||
auto bufsiz = sb.st_size + 1;
|
||||
if (sb.st_size == 0) {
|
||||
bufsiz = PATH_MAX;
|
||||
}
|
||||
|
||||
std::vector<char> buf(bufsiz);
|
||||
auto nbytes = readlink(path.c_str(), buf.data(), bufsiz);
|
||||
if (nbytes == -1) {
|
||||
perror("readlink");
|
||||
return std::string();
|
||||
}
|
||||
|
||||
return buf.data();
|
||||
}
|
||||
|
||||
void ItTimeContainer009(void)
|
||||
{
|
||||
auto timelink = ReadlinkTime(getpid());
|
||||
std::cout << "Contents of the time link is: " << timelink << std::endl;
|
||||
|
||||
std::regex reg("'time:\\[[0-9]+\\]'");
|
||||
bool ret = std::regex_match(timelink, reg);
|
||||
ASSERT_TRUE(ret);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
int value = *((int*)arg);
|
||||
if (value != CHILD_FUNC_ARG) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ItTimeContainer010(void)
|
||||
{
|
||||
int ret;
|
||||
int status;
|
||||
char *containerType = "time";
|
||||
char *containerType1 = "time_for_children";
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
std::cout << getpid() << std::endl;
|
||||
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
|
||||
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType1);
|
||||
ret = linkBuffer.compare(linkBuffer1);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWTIME | SIGCHLD, &arg);
|
||||
ASSERT_TRUE(pid != -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
int exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
|
||||
auto linkBuffer2 = ReadlinkContainer(getpid(), containerType);
|
||||
ret = linkBuffer.compare(linkBuffer2);
|
||||
ASSERT_EQ(ret, 0);
|
||||
}
|
||||
97
testsuites/unittest/container/smoke/It_uts_container_004.cpp
Normal file
97
testsuites/unittest/container/smoke/It_uts_container_004.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
#include "sys/utsname.h"
|
||||
|
||||
static int ChildFunc(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
int ret;
|
||||
|
||||
ret = unshare(CLONE_NEWUTS);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
struct utsname newName;
|
||||
ret = uname(&newName);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
|
||||
const char *name = "TestHostName";
|
||||
ret = sethostname(name, strlen(name));
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
|
||||
struct utsname newName1;
|
||||
ret = uname(&newName1);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_4;
|
||||
}
|
||||
|
||||
ret = strcmp(newName.nodename, newName1.nodename);
|
||||
if (ret == 0) {
|
||||
return EXIT_CODE_ERRNO_5;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ItUtsContainer004(void)
|
||||
{
|
||||
int ret;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
ASSERT_TRUE(stack != NULL);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
struct utsname oldName;
|
||||
|
||||
ret = uname(&oldName);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
auto pid = clone(ChildFunc, stackTop, SIGCHLD, NULL);
|
||||
(void)munmap(stack, STACK_SIZE);
|
||||
ASSERT_NE(pid, -1);
|
||||
|
||||
int status;
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
int exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
|
||||
struct utsname oldName1;
|
||||
ret = uname(&oldName1);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = strcmp(oldName.nodename, oldName1.nodename);
|
||||
ASSERT_EQ(ret, 0);
|
||||
}
|
||||
@@ -54,4 +54,12 @@ extern void ItProcessFs012(void);
|
||||
extern void ItProcessFs013(void);
|
||||
extern void ItProcessFs014(void);
|
||||
extern void ItProcessFs015(void);
|
||||
extern void ItProcessFs015(void);
|
||||
extern void ItProcessFs016(void);
|
||||
extern void ItProcessFs017(void);
|
||||
extern void ItProcessFs018(void);
|
||||
extern void ItProcessFs019(void);
|
||||
extern void ItProcessFs020(void);
|
||||
extern void ItProcessFs021(void);
|
||||
extern void ItProcessFs022(void);
|
||||
#endif
|
||||
|
||||
@@ -61,6 +61,13 @@ process_fs_sources_smoke = [
|
||||
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_013.cpp",
|
||||
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_014.cpp",
|
||||
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_015.cpp",
|
||||
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_016.cpp",
|
||||
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_017.cpp",
|
||||
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_018.cpp",
|
||||
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_019.cpp",
|
||||
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_020.cpp",
|
||||
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_021.cpp",
|
||||
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_022.cpp",
|
||||
]
|
||||
|
||||
process_fs_sources_full = []
|
||||
|
||||
@@ -36,7 +36,7 @@ VOID PrintTest(const CHAR *fmt, ...)
|
||||
{
|
||||
#ifdef PRINT_TEST
|
||||
va_list ap;
|
||||
if (g_osLkHook != NULL) {
|
||||
if (g_osLkHook != nullptr) {
|
||||
va_start(ap, fmt);
|
||||
printf(fmt, ap);
|
||||
va_end(ap);
|
||||
@@ -141,6 +141,18 @@ HWTEST_F(ProcessFsTest, ItProcessFs007, TestSize.Level0)
|
||||
ItProcessFs007();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Process_fs_Test_008
|
||||
* @tc.desc: Process mount directory test
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6APW2
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ProcessFsTest, ItProcessFs008, TestSize.Level0)
|
||||
{
|
||||
ItProcessFs008();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Process_fs_Test_010
|
||||
* @tc.desc: Process mount directory test
|
||||
@@ -200,5 +212,29 @@ HWTEST_F(ProcessFsTest, ItProcessFs015, TestSize.Level0)
|
||||
{
|
||||
ItProcessFs015();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Process_fs_Test_021
|
||||
* @tc.desc: Process mount directory test
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6AVMY
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ProcessFsTest, ItProcessFs021, TestSize.Level0)
|
||||
{
|
||||
ItProcessFs021();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Process_fs_Test_022
|
||||
* @tc.desc: Process mount directory test
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6B0A3
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ProcessFsTest, ItProcessFs022, TestSize.Level0)
|
||||
{
|
||||
ItProcessFs022();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -46,8 +46,6 @@ static int childFunc(void *arg)
|
||||
|
||||
void ItProcessFs016(void)
|
||||
{
|
||||
const int CONFIG_FILE_LEN = 1024;
|
||||
char szFile[CONFIG_FILE_LEN] = {0};
|
||||
std::string path = "/proc/sys/user/max_uts_container";
|
||||
int fd = open(path.c_str(), O_WRONLY);
|
||||
ASSERT_NE(fd, -1);
|
||||
@@ -59,9 +57,9 @@ void ItProcessFs016(void)
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
ASSERT_NE(stack, NULL);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWUTS, &arg);
|
||||
|
||||
@@ -46,8 +46,6 @@ static int childFunc(void *arg)
|
||||
|
||||
void ItProcessFs017(void)
|
||||
{
|
||||
const int CONFIG_FILE_LEN = 1024;
|
||||
char szFile[CONFIG_FILE_LEN] = {0};
|
||||
std::string path = "/proc/sys/user/max_user_container";
|
||||
int fd = open(path.c_str(), O_WRONLY);
|
||||
ASSERT_NE(fd, -1);
|
||||
@@ -60,9 +58,9 @@ void ItProcessFs017(void)
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
ASSERT_NE(stack, NULL);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWUSER, &arg);
|
||||
|
||||
@@ -46,8 +46,6 @@ static int childFunc(void *arg)
|
||||
|
||||
void ItProcessFs018(void)
|
||||
{
|
||||
const int CONFIG_FILE_LEN = 1024;
|
||||
char szFile[CONFIG_FILE_LEN] = {0};
|
||||
std::string path = "/proc/sys/user/max_mnt_container";
|
||||
int fd = open(path.c_str(), O_WRONLY);
|
||||
ASSERT_NE(fd, -1);
|
||||
@@ -60,9 +58,9 @@ void ItProcessFs018(void)
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
ASSERT_NE(stack, NULL);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWNS, &arg);
|
||||
|
||||
@@ -46,8 +46,6 @@ static int childFunc(void *arg)
|
||||
|
||||
void ItProcessFs019(void)
|
||||
{
|
||||
const int CONFIG_FILE_LEN = 1024;
|
||||
char szFile[CONFIG_FILE_LEN] = {0};
|
||||
std::string path = "/proc/sys/user/max_pid_container";
|
||||
int fd = open(path.c_str(), O_WRONLY);
|
||||
ASSERT_NE(fd, -1);
|
||||
@@ -60,9 +58,9 @@ void ItProcessFs019(void)
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
ASSERT_NE(stack, NULL);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWPID, &arg);
|
||||
|
||||
@@ -46,23 +46,21 @@ static int childFunc(void *arg)
|
||||
|
||||
void ItProcessFs020(void)
|
||||
{
|
||||
const int CONFIG_FILE_LEN = 1024;
|
||||
char szFile[CONFIG_FILE_LEN] = {0};
|
||||
std::string path = "/proc/sys/user/max_net_container";
|
||||
int fd = open(path.c_str(), O_WRONLY);
|
||||
ASSERT_NE(fd, -1);
|
||||
|
||||
char buf[configLen];
|
||||
size_t ret = sprintf(buf, configLen, "%d", invalidNum);
|
||||
size_t ret = sprintf_s(buf, configLen, "%d", invalidNum);
|
||||
ASSERT_GT(ret, 0);
|
||||
ret = write(fd, buf, (strlen(buf) + 1));
|
||||
ASSERT_NE(ret, -1);
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
ASSERT_NE(stack, NULL);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWNET, &arg);
|
||||
|
||||
49
testsuites/unittest/process/fs/smoke/It_process_fs_021.cpp
Normal file
49
testsuites/unittest/process/fs/smoke/It_process_fs_021.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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 <sched.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <climits>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include "It_process_fs_test.h"
|
||||
|
||||
void ItProcessFs021(void)
|
||||
{
|
||||
auto path = GenProcPidContainerPath(getpid(), "ipc");
|
||||
std::vector<char> buf(PATH_MAX);
|
||||
auto nbytes = readlink(path.c_str(), buf.data(), PATH_MAX);
|
||||
ASSERT_NE(nbytes, -1);
|
||||
|
||||
std::regex reg("'ipc:\\[[0-9]+\\]'");
|
||||
bool ret = std::regex_match(buf.data(), reg);
|
||||
ASSERT_EQ(ret, true);
|
||||
}
|
||||
49
testsuites/unittest/process/fs/smoke/It_process_fs_022.cpp
Normal file
49
testsuites/unittest/process/fs/smoke/It_process_fs_022.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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 <sched.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <climits>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include "It_process_fs_test.h"
|
||||
|
||||
void ItProcessFs022(void)
|
||||
{
|
||||
auto path = GenProcPidContainerPath(getpid(), "time");
|
||||
std::vector<char> buf(PATH_MAX);
|
||||
auto nbytes = readlink(path.c_str(), buf.data(), PATH_MAX);
|
||||
ASSERT_NE(nbytes, -1);
|
||||
|
||||
std::regex reg("'time:\\[[0-9]+\\]'");
|
||||
bool ret = std::regex_match(buf.data(), reg);
|
||||
ASSERT_EQ(ret, true);
|
||||
}
|
||||
Reference in New Issue
Block a user