los_ipc_container.c 6.0 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
    return CreateIpcContainer(child, parent);
Z
zhushengle 已提交
125 126
}

Z
zhushengle 已提交
127
UINT32 OsUnshareIpcContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
Z
zhushengle 已提交
128 129
{
    UINT32 intSave;
Z
zhushengle 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    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) {
Z
zhushengle 已提交
155 156 157 158
        return;
    }

    SCHEDULER_LOCK(intSave);
Z
zhushengle 已提交
159
    IpcContainer *ipcContainer = container->ipcContainer;
Z
zhushengle 已提交
160 161
    if (ipcContainer != NULL) {
        LOS_AtomicDec(&ipcContainer->rc);
Z
zhushengle 已提交
162
        if (LOS_AtomicRead(&ipcContainer->rc) <= 0) {
Z
zhushengle 已提交
163 164 165
            g_currentIpcContainerNum--;
            SCHEDULER_UNLOCK(intSave);
            ShmDeinit();
Z
zhushengle 已提交
166
            container->ipcContainer = NULL;
Z
zhushengle 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
            (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