los_container.c 19.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
/*
 * 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"
Z
zhushengle 已提交
32
#include "internal.h"
Z
zhushengle 已提交
33 34 35
#ifdef LOSCFG_KERNEL_CONTAINER

STATIC Container g_rootContainer;
Z
zhushengle 已提交
36
STATIC ContainerLimit g_containerLimit;
37
STATIC Atomic g_containerCount = 0xF0000000U;
Z
zhushengle 已提交
38 39 40
#ifdef LOSCFG_USER_CONTAINER
STATIC Credentials *g_rootCredentials = NULL;
#endif
41 42 43 44 45

UINT32 OsAllocContainerID(VOID)
{
    return LOS_AtomicIncRet(&g_containerCount);
}
Z
zhushengle 已提交
46 47 48 49

VOID OsContainerInitSystemProcess(LosProcessCB *processCB)
{
    processCB->container = &g_rootContainer;
Z
zhushengle 已提交
50 51 52
#ifdef LOSCFG_USER_CONTAINER
    processCB->credentials = g_rootCredentials;
#endif
Z
zhushengle 已提交
53 54
    LOS_AtomicInc(&processCB->container->rc);
#ifdef LOSCFG_PID_CONTAINER
Z
zhushengle 已提交
55
    (VOID)OsAllocSpecifiedVpidUnsafe(processCB->processID, processCB->container->pidContainer, processCB, NULL);
Z
zhushengle 已提交
56 57 58 59
#endif
    return;
}

Z
zhushengle 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
UINT32 OsGetContainerLimit(ContainerType type)
{
    switch (type) {
#ifdef LOSCFG_PID_CONTAINER
        case PID_CONTAINER:
        case PID_CHILD_CONTAINER:
            return g_containerLimit.pidLimit;
#endif
#ifdef LOSCFG_USER_CONTAINER
        case USER_CONTAINER:
            return g_containerLimit.userLimit;
#endif
#ifdef LOSCFG_UTS_CONTAINER
        case UTS_CONTAINER:
            return g_containerLimit.utsLimit;
#endif
#ifdef LOSCFG_MNT_CONTAINER
        case MNT_CONTAINER:
            return g_containerLimit.mntLimit;
#endif
#ifdef LOSCFG_IPC_CONTAINER
        case IPC_CONTAINER:
            return g_containerLimit.ipcLimit;
#endif
#ifdef LOSCFG_TIME_CONTAINER
        case TIME_CONTAINER:
        case TIME_CHILD_CONTAINER:
            return g_containerLimit.timeLimit;
Z
zhushengle 已提交
88 89 90 91
#endif
#ifdef LOSCFG_NET_CONTAINER
        case NET_CONTAINER:
            return g_containerLimit.netLimit;
Z
zhushengle 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 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
#endif
        default:
            break;
    }
    return OS_INVALID_VALUE;
}

UINT32 OsContainerLimitCheck(ContainerType type, UINT32 *containerCount)
{
    UINT32 intSave;
    SCHEDULER_LOCK(intSave);
    if ((*containerCount) >= OsGetContainerLimit(type)) {
        SCHEDULER_UNLOCK(intSave);
        return EINVAL;
    }
    SCHEDULER_UNLOCK(intSave);
    return LOS_OK;
}

UINT32 OsSetContainerLimit(ContainerType type, UINT32 value)
{
    UINT32 intSave;

    if (value > LOSCFG_KERNEL_CONTAINER_DEFAULT_LIMIT) {
        return EINVAL;
    }

    SCHEDULER_LOCK(intSave);
    switch (type) {
#ifdef LOSCFG_PID_CONTAINER
        case PID_CONTAINER:
        case PID_CHILD_CONTAINER:
            g_containerLimit.pidLimit = value;
            break;
#endif
#ifdef LOSCFG_USER_CONTAINER
        case USER_CONTAINER:
            g_containerLimit.userLimit = value;
            break;
#endif
#ifdef LOSCFG_UTS_CONTAINER
        case UTS_CONTAINER:
            g_containerLimit.utsLimit = value;
            break;
#endif
#ifdef LOSCFG_MNT_CONTAINER
        case MNT_CONTAINER:
            g_containerLimit.mntLimit = value;
            break;
#endif
#ifdef LOSCFG_IPC_CONTAINER
        case IPC_CONTAINER:
            g_containerLimit.ipcLimit = value;
            break;
#endif
#ifdef LOSCFG_TIME_CONTAINER
        case TIME_CONTAINER:
        case TIME_CHILD_CONTAINER:
            g_containerLimit.timeLimit = value;
            break;
Z
zhushengle 已提交
152 153 154 155 156
#endif
#ifdef LOSCFG_NET_CONTAINER
        case NET_CONTAINER:
            g_containerLimit.netLimit = value;
            break;
Z
zhushengle 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
#endif
        default:
            SCHEDULER_UNLOCK(intSave);
            return EINVAL;
    }
    SCHEDULER_UNLOCK(intSave);
    return LOS_OK;
}

UINT32 OsGetContainerCount(ContainerType type)
{
    switch (type) {
#ifdef LOSCFG_PID_CONTAINER
        case PID_CONTAINER:
        case PID_CHILD_CONTAINER:
            return OsGetPidContainerCount();
#endif
#ifdef LOSCFG_USER_CONTAINER
        case USER_CONTAINER:
            return OsGetUserContainerCount();
#endif
#ifdef LOSCFG_UTS_CONTAINER
        case UTS_CONTAINER:
            return OsGetUtsContainerCount();
#endif
#ifdef LOSCFG_MNT_CONTAINER
        case MNT_CONTAINER:
            return OsGetMntContainerCount();
#endif
#ifdef LOSCFG_IPC_CONTAINER
        case IPC_CONTAINER:
            return OsGetIpcContainerCount();
#endif
#ifdef LOSCFG_TIME_CONTAINER
        case TIME_CONTAINER:
        case TIME_CHILD_CONTAINER:
            return OsGetTimeContainerCount();
Z
zhushengle 已提交
194 195 196 197
#endif
#ifdef LOSCFG_NET_CONTAINER
        case NET_CONTAINER:
            return OsGetNetContainerCount();
Z
zhushengle 已提交
198 199 200 201 202 203 204
#endif
        default:
            break;
    }
    return OS_INVALID_VALUE;
}

Z
zhushengle 已提交
205 206
VOID OsInitRootContainer(VOID)
{
Z
zhushengle 已提交
207
#ifdef LOSCFG_USER_CONTAINER
Z
zhushengle 已提交
208
    g_containerLimit.userLimit = LOSCFG_KERNEL_CONTAINER_DEFAULT_LIMIT;
Z
zhushengle 已提交
209 210
    OsInitRootUserCredentials(&g_rootCredentials);
#endif
Z
zhushengle 已提交
211
#ifdef LOSCFG_PID_CONTAINER
Z
zhushengle 已提交
212
    g_containerLimit.pidLimit = LOSCFG_KERNEL_CONTAINER_DEFAULT_LIMIT;
Z
zhushengle 已提交
213
    (VOID)OsInitRootPidContainer(&g_rootContainer.pidContainer);
Z
zhushengle 已提交
214
    g_rootContainer.pidForChildContainer = g_rootContainer.pidContainer;
Z
zhushengle 已提交
215 216
#endif
#ifdef LOSCFG_UTS_CONTAINER
Z
zhushengle 已提交
217
    g_containerLimit.utsLimit = LOSCFG_KERNEL_CONTAINER_DEFAULT_LIMIT;
Z
zhushengle 已提交
218
    (VOID)OsInitRootUtsContainer(&g_rootContainer.utsContainer);
219 220
#endif
#ifdef LOSCFG_MNT_CONTAINER
Z
zhushengle 已提交
221
    g_containerLimit.mntLimit = LOSCFG_KERNEL_CONTAINER_DEFAULT_LIMIT;
222
    (VOID)OsInitRootMntContainer(&g_rootContainer.mntContainer);
Z
zhushengle 已提交
223 224
#endif
#ifdef LOSCFG_IPC_CONTAINER
Z
zhushengle 已提交
225
    g_containerLimit.ipcLimit = LOSCFG_KERNEL_CONTAINER_DEFAULT_LIMIT;
Z
zhushengle 已提交
226
    (VOID)OsInitRootIpcContainer(&g_rootContainer.ipcContainer);
Z
zhushengle 已提交
227 228
#endif
#ifdef LOSCFG_TIME_CONTAINER
Z
zhushengle 已提交
229
    g_containerLimit.timeLimit = LOSCFG_KERNEL_CONTAINER_DEFAULT_LIMIT;
Z
zhushengle 已提交
230 231
    (VOID)OsInitRootTimeContainer(&g_rootContainer.timeContainer);
    g_rootContainer.timeForChildContainer = g_rootContainer.timeContainer;
Z
zhushengle 已提交
232 233 234 235
#endif
#ifdef LOSCFG_NET_CONTAINER
    g_containerLimit.netLimit = LOSCFG_KERNEL_CONTAINER_DEFAULT_LIMIT;
    (VOID)OsInitRootNetContainer(&g_rootContainer.netContainer);
Z
zhushengle 已提交
236 237 238 239 240 241
#endif
    return;
}

STATIC INLINE Container *CreateContainer(VOID)
{
Z
zhushengle 已提交
242
    Container *container = (Container *)LOS_MemAlloc(m_aucSysMem1, sizeof(Container));
Z
zhushengle 已提交
243 244 245 246 247 248 249 250 251 252
    if (container == NULL) {
        return NULL;
    }

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

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

Z
zhushengle 已提交
253
STATIC UINT32 CopyContainers(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID)
Z
zhushengle 已提交
254 255 256 257 258 259 260 261 262
{
    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 已提交
263 264 265 266 267 268
#ifdef LOSCFG_USER_CONTAINER
    ret = OsCopyCredentials(flags,  child, parent);
    if (ret != LOS_OK) {
        return ret;
    }
#endif
Z
zhushengle 已提交
269 270 271 272 273
#ifdef LOSCFG_UTS_CONTAINER
    ret = OsCopyUtsContainer(flags, child, parent);
    if (ret != LOS_OK) {
        return ret;
    }
274 275 276 277 278 279
#endif
#ifdef LOSCFG_MNT_CONTAINER
    ret = OsCopyMntContainer(flags, child, parent);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
280 281 282 283 284 285
#endif
#ifdef LOSCFG_IPC_CONTAINER
    ret = OsCopyIpcContainer(flags, child, parent);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
286 287 288 289 290 291
#endif
#ifdef LOSCFG_TIME_CONTAINER
    ret = OsCopyTimeContainer(flags, child, parent);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
292 293 294 295 296 297
#endif
#ifdef LOSCFG_NET_CONTAINER
    ret = OsCopyNetContainer(flags, child, parent);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
298
#endif
Z
zhushengle 已提交
299 300 301
    return ret;
}

Z
zhushengle 已提交
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
STATIC INLINE UINT32 GetContainerFlagsMask(VOID)
{
    UINT32 mask = 0;
#ifdef LOSCFG_PID_CONTAINER
    mask |= CLONE_NEWPID;
#endif
#ifdef LOSCFG_MNT_CONTAINER
    mask |= CLONE_NEWNS;
#endif
#ifdef LOSCFG_IPC_CONTAINER
    mask |= CLONE_NEWIPC;
#endif
#ifdef LOSCFG_USER_CONTAINER
    mask |= CLONE_NEWUSER;
#endif
#ifdef LOSCFG_TIME_CONTAINER
    mask |= CLONE_NEWTIME;
#endif
#ifdef LOSCFG_NET_CONTAINER
    mask |= CLONE_NEWNET;
#endif
#ifdef LOSCFG_UTS_CONTAINER
    mask |= CLONE_NEWUTS;
#endif
    return mask;
}

Z
zhushengle 已提交
329 330 331 332 333 334 335 336
/*
 * 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;

Z
zhushengle 已提交
337 338 339 340
#ifdef LOSCFG_TIME_CONTAINER
    flags &= ~CLONE_NEWTIME;
#endif

Z
zhushengle 已提交
341
    if (!(flags & GetContainerFlagsMask())) {
Z
zhushengle 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
#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);
}

Z
zhushengle 已提交
371
VOID OsContainerFree(LosProcessCB *processCB)
Z
zhushengle 已提交
372 373 374 375 376 377 378 379
{
    LOS_AtomicDec(&processCB->container->rc);
    if (LOS_AtomicRead(&processCB->container->rc) == 0) {
        (VOID)LOS_MemFree(m_aucSysMem1, processCB->container);
        processCB->container = NULL;
    }
}

Z
zhushengle 已提交
380
VOID OsOsContainersDestroyEarly(LosProcessCB *processCB)
Z
zhushengle 已提交
381 382 383
{
   /* All processes in the container must be destroyed before the container is destroyed. */
#ifdef LOSCFG_PID_CONTAINER
384
    if (processCB->processID == OS_USER_ROOT_PROCESS_ID) {
Z
zhushengle 已提交
385
        OsPidContainerDestroyAllProcess(processCB);
Z
zhushengle 已提交
386 387 388
    }
#endif

Z
zhushengle 已提交
389 390 391 392 393 394 395
#ifdef LOSCFG_MNT_CONTAINER
    OsMntContainerDestroy(processCB->container);
#endif
}

VOID OsContainersDestroy(LosProcessCB *processCB)
{
Z
zhushengle 已提交
396 397 398 399
#ifdef LOSCFG_USER_CONTAINER
    OsUserContainerDestroy(processCB);
#endif

Z
zhushengle 已提交
400
#ifdef LOSCFG_UTS_CONTAINER
Z
zhushengle 已提交
401
    OsUtsContainerDestroy(processCB->container);
Z
zhushengle 已提交
402 403
#endif

Z
zhushengle 已提交
404
#ifdef LOSCFG_IPC_CONTAINER
Z
zhushengle 已提交
405
    OsIpcContainerDestroy(processCB->container);
Z
zhushengle 已提交
406 407
#endif

Z
zhushengle 已提交
408
#ifdef LOSCFG_TIME_CONTAINER
Z
zhushengle 已提交
409
    OsTimeContainerDestroy(processCB->container);
Z
zhushengle 已提交
410 411
#endif

Z
zhushengle 已提交
412 413 414 415
#ifdef LOSCFG_NET_CONTAINER
    OsNetContainerDestroy(processCB->container);
#endif

Z
zhushengle 已提交
416
#ifndef LOSCFG_PID_CONTAINER
Z
zhushengle 已提交
417 418 419 420
    UINT32 intSave;
    SCHEDULER_LOCK(intSave);
    OsContainerFree(processCB);
    SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
421 422
#endif
}
423

Z
zhushengle 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
STATIC VOID DeInitContainers(UINT32 flags, Container *container, LosProcessCB *processCB)
{
    UINT32 intSave;
    if (container == NULL) {
        return;
    }

#ifdef LOSCFG_PID_CONTAINER
    SCHEDULER_LOCK(intSave);
    if ((flags & CLONE_NEWPID) != 0) {
        OsPidContainerDestroy(container, processCB);
    } else {
        OsPidContainerDestroy(container, NULL);
    }
    SCHEDULER_UNLOCK(intSave);
#endif

#ifdef LOSCFG_UTS_CONTAINER
    OsUtsContainerDestroy(container);
#endif
#ifdef LOSCFG_MNT_CONTAINER
    OsMntContainerDestroy(container);
#endif
#ifdef LOSCFG_IPC_CONTAINER
    OsIpcContainerDestroy(container);
#endif

#ifdef LOSCFG_TIME_CONTAINER
    OsTimeContainerDestroy(container);
#endif

Z
zhushengle 已提交
455 456 457 458
#ifdef LOSCFG_NET_CONTAINER
    OsNetContainerDestroy(container);
#endif

Z
zhushengle 已提交
459 460 461 462 463 464 465 466
    SCHEDULER_LOCK(intSave);
    LOS_AtomicDec(&container->rc);
    if (LOS_AtomicRead(&container->rc) == 0) {
        (VOID)LOS_MemFree(m_aucSysMem1, container);
    }
    SCHEDULER_UNLOCK(intSave);
}

Z
zhushengle 已提交
467
UINT32 OsGetContainerID(LosProcessCB *processCB, ContainerType type)
468
{
Z
zhushengle 已提交
469
    Container *container = processCB->container;
470 471 472 473 474
    if (container == NULL) {
        return OS_INVALID_VALUE;
    }

    switch (type) {
Z
zhushengle 已提交
475
#ifdef LOSCFG_PID_CONTAINER
476 477
        case PID_CONTAINER:
            return OsGetPidContainerID(container->pidContainer);
Z
zhushengle 已提交
478 479
        case PID_CHILD_CONTAINER:
            return OsGetPidContainerID(container->pidForChildContainer);
Z
zhushengle 已提交
480
#endif
Z
zhushengle 已提交
481 482 483 484
#ifdef LOSCFG_USER_CONTAINER
        case USER_CONTAINER:
            return OsGetUserContainerID(processCB->credentials);
#endif
Z
zhushengle 已提交
485
#ifdef LOSCFG_UTS_CONTAINER
486 487
        case UTS_CONTAINER:
            return OsGetUtsContainerID(container->utsContainer);
Z
zhushengle 已提交
488 489
#endif
#ifdef LOSCFG_MNT_CONTAINER
490 491
        case MNT_CONTAINER:
            return OsGetMntContainerID(container->mntContainer);
Z
zhushengle 已提交
492 493 494 495
#endif
#ifdef LOSCFG_IPC_CONTAINER
        case IPC_CONTAINER:
            return OsGetIpcContainerID(container->ipcContainer);
Z
zhushengle 已提交
496 497 498 499 500 501
#endif
#ifdef LOSCFG_TIME_CONTAINER
        case TIME_CONTAINER:
            return OsGetTimeContainerID(container->timeContainer);
        case TIME_CHILD_CONTAINER:
            return OsGetTimeContainerID(container->timeForChildContainer);
Z
zhushengle 已提交
502 503 504 505
#endif
#ifdef LOSCFG_NET_CONTAINER
        case NET_CONTAINER:
            return OsGetNetContainerID(container->netContainer);
Z
zhushengle 已提交
506
#endif
507 508 509 510 511
        default:
            break;
    }
    return OS_INVALID_VALUE;
}
Z
zhushengle 已提交
512

Z
zhushengle 已提交
513
STATIC UINT32 UnshareCreateNewContainers(UINT32 flags, LosProcessCB *curr, Container *newContainer)
Z
zhushengle 已提交
514
{
Z
zhushengle 已提交
515
    UINT32 ret = LOS_OK;
Z
zhushengle 已提交
516
#ifdef LOSCFG_PID_CONTAINER
Z
zhushengle 已提交
517 518 519 520
    ret = OsUnsharePidContainer(flags, curr, newContainer);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
521
#endif
Z
zhushengle 已提交
522
#ifdef LOSCFG_UTS_CONTAINER
Z
zhushengle 已提交
523 524 525
    ret = OsUnshareUtsContainer(flags, curr, newContainer);
    if (ret != LOS_OK) {
        return ret;
Z
zhushengle 已提交
526 527 528
    }
#endif
#ifdef LOSCFG_MNT_CONTAINER
Z
zhushengle 已提交
529 530 531
    ret = OsUnshareMntContainer(flags, curr, newContainer);
    if (ret != LOS_OK) {
        return ret;
Z
zhushengle 已提交
532 533 534
    }
#endif
#ifdef LOSCFG_IPC_CONTAINER
Z
zhushengle 已提交
535 536 537
    ret = OsUnshareIpcContainer(flags, curr, newContainer);
    if (ret != LOS_OK) {
        return ret;
Z
zhushengle 已提交
538 539 540
    }
#endif
#ifdef LOSCFG_TIME_CONTAINER
Z
zhushengle 已提交
541 542 543 544
    ret = OsUnshareTimeContainer(flags, curr, newContainer);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
545 546 547 548 549 550
#endif
#ifdef LOSCFG_NET_CONTAINER
    ret = OsUnshareNetContainer(flags, curr, newContainer);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
551
#endif
Z
zhushengle 已提交
552 553
    return ret;
}
Z
zhushengle 已提交
554

Z
zhushengle 已提交
555 556 557 558 559 560
INT32 OsUnshare(UINT32 flags)
{
    UINT32 ret;
    UINT32 intSave;
    LosProcessCB *curr = OsCurrProcessGet();
    Container *oldContainer = curr->container;
Z
zhushengle 已提交
561
    UINT32 unshareFlags = GetContainerFlagsMask();
Z
zhushengle 已提交
562 563 564 565
    if (!(flags & unshareFlags) || ((flags & (~unshareFlags)) != 0)) {
        return -EINVAL;
    }

Z
zhushengle 已提交
566 567 568 569 570 571 572 573 574 575
#ifdef LOSCFG_USER_CONTAINER
    ret = OsUnshareUserCredentials(flags, curr);
    if (ret != LOS_OK) {
        return ret;
    }
    if (flags == CLONE_NEWUSER) {
        return LOS_OK;
    }
#endif

Z
zhushengle 已提交
576 577 578
    Container *newContainer = CreateContainer();
    if (newContainer == NULL) {
        return -ENOMEM;
Z
zhushengle 已提交
579
    }
Z
zhushengle 已提交
580 581 582 583 584 585 586 587 588

    ret = UnshareCreateNewContainers(flags, curr, newContainer);
    if (ret != LOS_OK) {
        goto EXIT;
    }

    SCHEDULER_LOCK(intSave);
    oldContainer = curr->container;
    curr->container = newContainer;
Z
zhushengle 已提交
589
    SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
590 591 592 593 594 595 596

    DeInitContainers(flags, oldContainer, NULL);
    return LOS_OK;

EXIT:
    DeInitContainers(flags, newContainer, NULL);
    return -ret;
Z
zhushengle 已提交
597 598
}

Z
zhushengle 已提交
599 600 601 602 603 604 605 606 607 608
STATIC UINT32 SetNsGetFlagByContainerType(UINT32 containerType)
{
    if (containerType >= (UINT32)CONTAINER_MAX) {
        return 0;
    }
    ContainerType type = (ContainerType)containerType;
    switch (type) {
        case PID_CONTAINER:
        case PID_CHILD_CONTAINER:
            return CLONE_NEWPID;
Z
zhushengle 已提交
609 610
        case USER_CONTAINER:
            return CLONE_NEWUSER;
Z
zhushengle 已提交
611 612 613 614 615 616 617 618 619
        case UTS_CONTAINER:
            return CLONE_NEWUTS;
        case MNT_CONTAINER:
            return CLONE_NEWNS;
        case IPC_CONTAINER:
            return CLONE_NEWIPC;
        case TIME_CONTAINER:
        case TIME_CHILD_CONTAINER:
            return CLONE_NEWTIME;
Z
zhushengle 已提交
620 621
        case NET_CONTAINER:
            return CLONE_NEWNET;
Z
zhushengle 已提交
622 623 624 625 626 627 628
        default:
            break;
    }
    return 0;
}

STATIC UINT32 SetNsCreateNewContainers(UINT32 flags, Container *newContainer, Container *container)
Z
zhushengle 已提交
629 630 631
{
    UINT32 ret = LOS_OK;
#ifdef LOSCFG_PID_CONTAINER
Z
zhushengle 已提交
632
    ret = OsSetNsPidContainer(flags, container, newContainer);
Z
zhushengle 已提交
633 634 635 636 637
    if (ret != LOS_OK) {
        return ret;
    }
#endif
#ifdef LOSCFG_UTS_CONTAINER
Z
zhushengle 已提交
638
    ret = OsSetNsUtsContainer(flags, container, newContainer);
Z
zhushengle 已提交
639 640 641 642 643
    if (ret != LOS_OK) {
        return ret;
    }
#endif
#ifdef LOSCFG_MNT_CONTAINER
Z
zhushengle 已提交
644
    ret = OsSetNsMntContainer(flags, container, newContainer);
Z
zhushengle 已提交
645 646 647 648 649
    if (ret != LOS_OK) {
        return ret;
    }
#endif
#ifdef LOSCFG_IPC_CONTAINER
Z
zhushengle 已提交
650
    ret = OsSetNsIpcContainer(flags, container, newContainer);
Z
zhushengle 已提交
651 652 653 654 655
    if (ret != LOS_OK) {
        return ret;
    }
#endif
#ifdef LOSCFG_TIME_CONTAINER
Z
zhushengle 已提交
656
    ret = OsSetNsTimeContainer(flags, container, newContainer);
Z
zhushengle 已提交
657 658 659
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
660 661 662 663 664 665
#endif
#ifdef LOSCFG_NET_CONTAINER
    ret = OsSetNsNetContainer(flags, container, newContainer);
    if (ret != LOS_OK) {
        return ret;
    }
Z
zhushengle 已提交
666
#endif
Z
zhushengle 已提交
667
    return LOS_OK;
Z
zhushengle 已提交
668 669
}

Z
zhushengle 已提交
670
STATIC UINT32 SetNsParamCheck(INT32 fd, INT32 type, UINT32 *flag, LosProcessCB **target)
Z
zhushengle 已提交
671
{
Z
zhushengle 已提交
672
    UINT32 typeMask = GetContainerFlagsMask();
Z
zhushengle 已提交
673
    *flag = (UINT32)(type & typeMask);
Z
zhushengle 已提交
674
    UINT32 containerType = 0;
Z
zhushengle 已提交
675

Z
zhushengle 已提交
676
    if (((type & (~typeMask)) != 0)) {
Z
zhushengle 已提交
677
        return EINVAL;
Z
zhushengle 已提交
678 679
    }

Z
zhushengle 已提交
680 681
    LosProcessCB *processCB = (LosProcessCB *)ProcfsContainerGet(fd, &containerType);
    if (processCB == NULL) {
Z
zhushengle 已提交
682
        return EBADF;
Z
zhushengle 已提交
683 684
    }

Z
zhushengle 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
    if (*flag == 0) {
        *flag = SetNsGetFlagByContainerType(containerType);
    }

    if ((*flag == 0) || (*flag != SetNsGetFlagByContainerType(containerType))) {
        return EINVAL;
    }

    if (processCB == OsCurrProcessGet()) {
        return EINVAL;
    }
    *target = processCB;
    return LOS_OK;
}

INT32 OsSetNs(INT32 fd, INT32 type)
{
    UINT32 intSave, ret;
    UINT32 flag = 0;
    LosProcessCB *curr = OsCurrProcessGet();
    LosProcessCB *processCB = NULL;

    ret = SetNsParamCheck(fd, type, &flag, &processCB);
    if (ret != LOS_OK) {
        return -ret;
    }

#ifdef LOSCFG_USER_CONTAINER
    if (flag == CLONE_NEWUSER) {
        SCHEDULER_LOCK(intSave);
        if ((processCB->credentials == NULL) || (processCB->credentials->userContainer == NULL)) {
            SCHEDULER_UNLOCK(intSave);
            return -EBADF;
        }
        UserContainer *userContainer = processCB->credentials->userContainer;
        ret = OsSetNsUserContainer(userContainer, curr);
Z
zhushengle 已提交
721
        SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
722
        return ret;
Z
zhushengle 已提交
723
    }
Z
zhushengle 已提交
724
#endif
Z
zhushengle 已提交
725

Z
zhushengle 已提交
726 727 728
    Container *newContainer = CreateContainer();
    if (newContainer == NULL) {
        return -ENOMEM;
Z
zhushengle 已提交
729 730
    }

Z
zhushengle 已提交
731 732 733
    SCHEDULER_LOCK(intSave);
    Container *targetContainer = processCB->container;
    if (targetContainer == NULL) {
Z
zhushengle 已提交
734 735 736 737
        SCHEDULER_UNLOCK(intSave);
        return -EBADF;
    }

Z
zhushengle 已提交
738
    ret = SetNsCreateNewContainers(flag, newContainer, targetContainer);
Z
zhushengle 已提交
739
    if (ret != LOS_OK) {
Z
zhushengle 已提交
740
        SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
741 742 743
        goto EXIT;
    }

Z
zhushengle 已提交
744
    Container *oldContainer = curr->container;
Z
zhushengle 已提交
745 746
    curr->container = newContainer;
    SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
747
    DeInitContainers(flag, oldContainer, NULL);
Z
zhushengle 已提交
748 749 750
    return LOS_OK;

EXIT:
Z
zhushengle 已提交
751 752
    DeInitContainers(flag, newContainer, curr);
    return ret;
Z
zhushengle 已提交
753
}
Z
zhushengle 已提交
754
#endif