los_ipc_container.c 6.9 KB
Newer Older
Z
zhushengle 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*
 * 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;

Z
zhushengle 已提交
44
STATIC IpcContainer *CreateNewIpcContainer(IpcContainer *parent)
Z
zhushengle 已提交
45 46 47 48 49
{
    pthread_mutexattr_t attr;
    UINT32 size = sizeof(IpcContainer);
    IpcContainer *ipcContainer = (IpcContainer *)LOS_MemAlloc(m_aucSysMem1, size);
    if (ipcContainer == NULL) {
Z
zhushengle 已提交
50
        return NULL;
Z
zhushengle 已提交
51 52 53 54 55 56
    }
    (VOID)memset_s(ipcContainer, size, 0, size);

    ipcContainer->allQueue = OsAllQueueCBInit(&ipcContainer->freeQueueList);
    if (ipcContainer->allQueue == NULL) {
        (VOID)LOS_MemFree(m_aucSysMem1, ipcContainer);
Z
zhushengle 已提交
57
        return NULL;
Z
zhushengle 已提交
58 59 60 61 62 63 64 65 66 67
    }
    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);
Z
zhushengle 已提交
68
        return NULL;
Z
zhushengle 已提交
69 70 71
    }
    ipcContainer->containerID = OsAllocContainerID();

Z
zhushengle 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    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;
    }

Z
zhushengle 已提交
89
    SCHEDULER_LOCK(intSave);
Z
zhushengle 已提交
90 91
    g_currentIpcContainerNum++;
    child->container->ipcContainer = newIpcContainer;
Z
zhushengle 已提交
92 93 94 95 96 97
    SCHEDULER_UNLOCK(intSave);
    return LOS_OK;
}

UINT32 OsInitRootIpcContainer(IpcContainer **ipcContainer)
{
Z
zhushengle 已提交
98 99 100 101 102 103 104 105 106 107 108
    UINT32 intSave;
    IpcContainer *newIpcContainer = CreateNewIpcContainer(NULL);
    if (newIpcContainer == NULL) {
        return ENOMEM;
    }

    SCHEDULER_LOCK(intSave);
    g_currentIpcContainerNum++;
    *ipcContainer = newIpcContainer;
    SCHEDULER_UNLOCK(intSave);
    return LOS_OK;
Z
zhushengle 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
}

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;
    }

Z
zhushengle 已提交
124 125 126 127
    if (OsContainerLimitCheck(IPC_CONTAINER, &g_currentIpcContainerNum) != LOS_OK) {
        return EPERM;
    }

Z
zhushengle 已提交
128
    return CreateIpcContainer(child, parent);
Z
zhushengle 已提交
129 130
}

Z
zhushengle 已提交
131
UINT32 OsUnshareIpcContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
Z
zhushengle 已提交
132 133
{
    UINT32 intSave;
Z
zhushengle 已提交
134 135 136 137 138
    IpcContainer *parentContainer = curr->container->ipcContainer;

    if (!(flags & CLONE_NEWIPC)) {
        SCHEDULER_LOCK(intSave);
        newContainer->ipcContainer = parentContainer;
Z
zhushengle 已提交
139
        LOS_AtomicInc(&parentContainer->rc);
Z
zhushengle 已提交
140 141 142 143
        SCHEDULER_UNLOCK(intSave);
        return LOS_OK;
    }

Z
zhushengle 已提交
144 145 146 147
    if (OsContainerLimitCheck(IPC_CONTAINER, &g_currentIpcContainerNum) != LOS_OK) {
        return EPERM;
    }

Z
zhushengle 已提交
148 149 150 151 152 153 154 155 156 157 158 159
    IpcContainer *ipcContainer = CreateNewIpcContainer(parentContainer);
    if (ipcContainer == NULL) {
        return ENOMEM;
    }

    SCHEDULER_LOCK(intSave);
    newContainer->ipcContainer = ipcContainer;
    g_currentIpcContainerNum++;
    SCHEDULER_UNLOCK(intSave);
    return LOS_OK;
}

Z
zhushengle 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172
UINT32 OsSetNsIpcContainer(UINT32 flags, Container *container, Container *newContainer)
{
    if (flags & CLONE_NEWIPC) {
        newContainer->ipcContainer = container->ipcContainer;
        LOS_AtomicInc(&container->ipcContainer->rc);
        return LOS_OK;
    }

    newContainer->ipcContainer = OsCurrProcessGet()->container->ipcContainer;
    LOS_AtomicInc(&newContainer->ipcContainer->rc);
    return LOS_OK;
}

Z
zhushengle 已提交
173
VOID OsIpcContainerDestroy(Container *container)
Z
zhushengle 已提交
174 175 176
{
    UINT32 intSave;
    if (container == NULL) {
Z
zhushengle 已提交
177 178 179 180
        return;
    }

    SCHEDULER_LOCK(intSave);
Z
zhushengle 已提交
181
    IpcContainer *ipcContainer = container->ipcContainer;
Z
zhushengle 已提交
182 183 184 185 186 187 188 189 190
    if (ipcContainer == NULL) {
        SCHEDULER_UNLOCK(intSave);
        return;
    }

    LOS_AtomicDec(&ipcContainer->rc);
    if (LOS_AtomicRead(&ipcContainer->rc) > 0) {
        SCHEDULER_UNLOCK(intSave);
        return;
Z
zhushengle 已提交
191
    }
Z
zhushengle 已提交
192 193 194

    g_currentIpcContainerNum--;
    container->ipcContainer = NULL;
Z
zhushengle 已提交
195 196 197 198
    SCHEDULER_UNLOCK(intSave);
    OsShmCBDestroy(ipcContainer->shmSegs, &ipcContainer->shmInfo, &ipcContainer->sysvShmMux);
    ipcContainer->shmSegs = NULL;
    OsMqueueCBDestroy(ipcContainer->queueTable);
Z
zhushengle 已提交
199 200
    (VOID)LOS_MemFree(m_aucSysMem1, ipcContainer->allQueue);
    (VOID)LOS_MemFree(m_aucSysMem1, ipcContainer);
Z
zhushengle 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    return;
}

UINT32 OsGetIpcContainerID(IpcContainer *ipcContainer)
{
    if (ipcContainer == NULL) {
        return OS_INVALID_VALUE;
    }

    return ipcContainer->containerID;
}

IpcContainer *OsGetCurrIpcContainer(VOID)
{
    return OsCurrProcessGet()->container->ipcContainer;
}
Z
zhushengle 已提交
217 218 219 220 221

UINT32 OsGetIpcContainerCount(VOID)
{
    return g_currentIpcContainerNum;
}
Z
zhushengle 已提交
222
#endif