los_container.c 10.4 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
/*
 * 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_container_pri.h"
#include "los_process_pri.h"
#ifdef LOSCFG_KERNEL_CONTAINER

STATIC Container g_rootContainer;
35 36 37 38 39 40
STATIC Atomic g_containerCount = 0xF0000000U;

UINT32 OsAllocContainerID(VOID)
{
    return LOS_AtomicIncRet(&g_containerCount);
}
Z
zhushengle 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54

VOID OsContainerInitSystemProcess(LosProcessCB *processCB)
{
    processCB->container = &g_rootContainer;
    LOS_AtomicInc(&processCB->container->rc);
#ifdef LOSCFG_PID_CONTAINER
    (VOID)OsAllocSpecifiedVpidUnsafe(processCB->processID, processCB, NULL);
#endif
    return;
}

VOID OsInitRootContainer(VOID)
{
#ifdef LOSCFG_PID_CONTAINER
Z
zhushengle 已提交
55
    (VOID)OsInitRootPidContainer(&g_rootContainer.pidContainer);
Z
zhushengle 已提交
56
    g_rootContainer.pidForChildContainer = g_rootContainer.pidContainer;
Z
zhushengle 已提交
57 58 59
#endif
#ifdef LOSCFG_UTS_CONTAINER
    (VOID)OsInitRootUtsContainer(&g_rootContainer.utsContainer);
60 61 62
#endif
#ifdef LOSCFG_MNT_CONTAINER
    (VOID)OsInitRootMntContainer(&g_rootContainer.mntContainer);
Z
zhushengle 已提交
63 64 65
#endif
#ifdef LOSCFG_IPC_CONTAINER
    (VOID)OsInitRootIpcContainer(&g_rootContainer.ipcContainer);
Z
zhushengle 已提交
66 67 68 69
#endif
#ifdef LOSCFG_TIME_CONTAINER
    (VOID)OsInitRootTimeContainer(&g_rootContainer.timeContainer);
    g_rootContainer.timeForChildContainer = g_rootContainer.timeContainer;
Z
zhushengle 已提交
70 71 72 73 74 75
#endif
    return;
}

STATIC INLINE Container *CreateContainer(VOID)
{
Z
zhushengle 已提交
76
    Container *container = (Container *)LOS_MemAlloc(m_aucSysMem1, sizeof(Container));
Z
zhushengle 已提交
77 78 79 80 81 82 83 84 85 86
    if (container == NULL) {
        return NULL;
    }

    (VOID)memset_s(container, sizeof(Container), 0, sizeof(Container));

    LOS_AtomicInc(&container->rc);
    return container;
}

Z
zhushengle 已提交
87
STATIC UINT32 CopyContainers(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID)
Z
zhushengle 已提交
88 89 90 91 92 93 94 95 96
{
    UINT32 ret = LOS_OK;
    /* Pid container initialization must precede other container initialization. */
#ifdef LOSCFG_PID_CONTAINER
    ret = OsCopyPidContainer(flags, child, parent, processID);
    if (ret != LOS_OK) {
        return ret;
    }
#endif
Z
zhushengle 已提交
97 98 99 100 101
#ifdef LOSCFG_UTS_CONTAINER
    ret = OsCopyUtsContainer(flags, child, parent);
    if (ret != LOS_OK) {
        return ret;
    }
102 103 104 105 106 107
#endif
#ifdef LOSCFG_MNT_CONTAINER
    ret = OsCopyMntContainer(flags, child, parent);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
108 109 110 111 112 113
#endif
#ifdef LOSCFG_IPC_CONTAINER
    ret = OsCopyIpcContainer(flags, child, parent);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
114 115 116 117 118 119
#endif
#ifdef LOSCFG_TIME_CONTAINER
    ret = OsCopyTimeContainer(flags, child, parent);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
120
#endif
Z
zhushengle 已提交
121 122 123
    return ret;
}

Z
zhushengle 已提交
124 125 126 127 128 129 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
/*
 * 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

Z
zhushengle 已提交
176 177 178 179
VOID OsContainersDestroy(LosProcessCB *processCB)
{
   /* All processes in the container must be destroyed before the container is destroyed. */
#ifdef LOSCFG_PID_CONTAINER
180
    if (processCB->processID == OS_USER_ROOT_PROCESS_ID) {
Z
zhushengle 已提交
181 182 183 184
        OsPidContainersDestroyAllProcess(processCB);
    }
#endif

Z
zhushengle 已提交
185
#ifdef LOSCFG_UTS_CONTAINER
Z
zhushengle 已提交
186
    OsUtsContainersDestroy(processCB->container);
Z
zhushengle 已提交
187 188
#endif

189
#ifdef LOSCFG_MNT_CONTAINER
Z
zhushengle 已提交
190
    OsMntContainersDestroy(processCB->container);
191 192
#endif

Z
zhushengle 已提交
193
#ifdef LOSCFG_IPC_CONTAINER
Z
zhushengle 已提交
194
    OsIpcContainersDestroy(processCB->container);
Z
zhushengle 已提交
195 196
#endif

Z
zhushengle 已提交
197 198 199 200
#ifdef LOSCFG_TIME_CONTAINER
    OsTimeContainersDestroy(processCB);
#endif

Z
zhushengle 已提交
201
#ifndef LOSCFG_PID_CONTAINER
Z
zhushengle 已提交
202
    ContainersFree(processCB);
Z
zhushengle 已提交
203 204
#endif
}
205 206 207 208 209 210 211 212

UINT32 OsGetContainerID(Container *container, ContainerType type)
{
    if (container == NULL) {
        return OS_INVALID_VALUE;
    }

    switch (type) {
Z
zhushengle 已提交
213
#ifdef LOSCFG_PID_CONTAINER
214 215
        case PID_CONTAINER:
            return OsGetPidContainerID(container->pidContainer);
Z
zhushengle 已提交
216 217
        case PID_CHILD_CONTAINER:
            return OsGetPidContainerID(container->pidForChildContainer);
Z
zhushengle 已提交
218 219
#endif
#ifdef LOSCFG_UTS_CONTAINER
220 221
        case UTS_CONTAINER:
            return OsGetUtsContainerID(container->utsContainer);
Z
zhushengle 已提交
222 223
#endif
#ifdef LOSCFG_MNT_CONTAINER
224 225
        case MNT_CONTAINER:
            return OsGetMntContainerID(container->mntContainer);
Z
zhushengle 已提交
226 227 228 229
#endif
#ifdef LOSCFG_IPC_CONTAINER
        case IPC_CONTAINER:
            return OsGetIpcContainerID(container->ipcContainer);
Z
zhushengle 已提交
230 231 232 233 234 235
#endif
#ifdef LOSCFG_TIME_CONTAINER
        case TIME_CONTAINER:
            return OsGetTimeContainerID(container->timeContainer);
        case TIME_CHILD_CONTAINER:
            return OsGetTimeContainerID(container->timeForChildContainer);
Z
zhushengle 已提交
236
#endif
237 238 239 240 241
        default:
            break;
    }
    return OS_INVALID_VALUE;
}
Z
zhushengle 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

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;
}
Z
zhushengle 已提交
364
#endif