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
/*
 * 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.
 */
30 31

#ifdef LOSCFG_KERNEL_CONTAINER
Z
zhushengle 已提交
32 33
#include "los_container_pri.h"
#include "los_process_pri.h"
Z
zhushengle 已提交
34
#include "internal.h"
Z
zhushengle 已提交
35 36

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

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

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

Z
zhushengle 已提交
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 88
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 已提交
89 90 91 92
#endif
#ifdef LOSCFG_NET_CONTAINER
        case NET_CONTAINER:
            return g_containerLimit.netLimit;
Z
zhushengle 已提交
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 152
#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 已提交
153 154 155 156 157
#endif
#ifdef LOSCFG_NET_CONTAINER
        case NET_CONTAINER:
            g_containerLimit.netLimit = value;
            break;
Z
zhushengle 已提交
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 194
#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 已提交
195 196 197 198
#endif
#ifdef LOSCFG_NET_CONTAINER
        case NET_CONTAINER:
            return OsGetNetContainerCount();
Z
zhushengle 已提交
199 200 201 202 203 204 205
#endif
        default:
            break;
    }
    return OS_INVALID_VALUE;
}

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

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

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

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

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

Z
zhushengle 已提交
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
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 已提交
330 331 332 333 334 335 336 337
/*
 * 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 已提交
338 339 340 341
#ifdef LOSCFG_TIME_CONTAINER
    flags &= ~CLONE_NEWTIME;
#endif

Z
zhushengle 已提交
342
    if (!(flags & GetContainerFlagsMask())) {
Z
zhushengle 已提交
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 371
#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 已提交
372
VOID OsContainerFree(LosProcessCB *processCB)
Z
zhushengle 已提交
373 374 375 376 377 378 379 380
{
    LOS_AtomicDec(&processCB->container->rc);
    if (LOS_AtomicRead(&processCB->container->rc) == 0) {
        (VOID)LOS_MemFree(m_aucSysMem1, processCB->container);
        processCB->container = NULL;
    }
}

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

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

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

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

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

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

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

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

Z
zhushengle 已提交
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 455
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 已提交
456 457 458 459
#ifdef LOSCFG_NET_CONTAINER
    OsNetContainerDestroy(container);
#endif

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

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

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

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

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

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

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

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

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

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

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

Z
zhushengle 已提交
600 601 602 603 604 605 606 607 608 609
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 已提交
610 611
        case USER_CONTAINER:
            return CLONE_NEWUSER;
Z
zhushengle 已提交
612 613 614 615 616 617 618 619 620
        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 已提交
621 622
        case NET_CONTAINER:
            return CLONE_NEWNET;
Z
zhushengle 已提交
623 624 625 626 627 628 629
        default:
            break;
    }
    return 0;
}

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

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

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

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

Z
zhushengle 已提交
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 721
    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 已提交
722
        SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
723
        return ret;
Z
zhushengle 已提交
724
    }
Z
zhushengle 已提交
725
#endif
Z
zhushengle 已提交
726

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

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

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

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

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