los_pid_container.c 20.3 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 44 45 46 47 48 49 50 51 52 53 54
/*
 * 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 <sched.h>
#include "los_pid_container_pri.h"
#include "los_config.h"
#include "los_process_pri.h"
#include "los_container_pri.h"

#ifdef LOSCFG_PID_CONTAINER

STATIC UINT32 g_currentPidContainerNum;
STATIC LosProcessCB *g_defaultProcessCB = NULL;
STATIC LosTaskCB *g_defaultTaskCB = NULL;

STATIC VOID FreeVpid(LosProcessCB *processCB)
{
    PidContainer *pidContainer = processCB->container->pidContainer;
    UINT32 vpid = processCB->processID;

    while ((pidContainer != NULL) && !OS_PID_CHECK_INVALID(vpid)) {
        ProcessVid *processVid = &pidContainer->pidArray[vpid];
        processVid->cb = (UINTPTR)g_defaultProcessCB;
        vpid = processVid->vpid;
        processVid->vpid = OS_INVALID_VALUE;
        LOS_ListTailInsert(&pidContainer->pidFreeList, &processVid->node);
        LOS_AtomicDec(&pidContainer->rc);
Z
zhushengle 已提交
55 56 57 58 59 60 61 62 63 64 65 66
        PidContainer *parentPidContainer = pidContainer->parent;
        if (LOS_AtomicRead(&pidContainer->rc) > 0) {
            pidContainer = parentPidContainer;
            continue;
        }
        g_currentPidContainerNum--;
        (VOID)LOS_MemFree(m_aucSysMem1, pidContainer->rootPGroup);
        (VOID)LOS_MemFree(m_aucSysMem1, pidContainer);
        if (pidContainer == processCB->container->pidContainer) {
            processCB->container->pidContainer = NULL;
        }
        pidContainer = parentPidContainer;
Z
zhushengle 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80
    }
}

STATIC ProcessVid *OsGetFreeVpid(PidContainer *pidContainer)
{
    if (LOS_ListEmpty(&pidContainer->pidFreeList)) {
        return NULL;
    }

    ProcessVid *vpid = LOS_DL_LIST_ENTRY(LOS_DL_LIST_FIRST(&pidContainer->pidFreeList), ProcessVid, node);
    LOS_ListDelete(&vpid->node);
    return vpid;
}

Z
zhushengle 已提交
81 82
UINT32 OsAllocSpecifiedVpidUnsafe(UINT32 vpid, PidContainer *pidContainer,
                                  LosProcessCB *processCB, LosProcessCB *parent)
Z
zhushengle 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
{
    if ((pidContainer == NULL) || OS_PID_CHECK_INVALID(vpid)) {
        return OS_INVALID_VALUE;
    }

    if (LOS_AtomicRead(&pidContainer->lock) > 0) {
        return OS_INVALID_VALUE;
    }

    ProcessVid *processVid = &pidContainer->pidArray[vpid];
    if (processVid->cb != (UINTPTR)g_defaultProcessCB) {
        return OS_INVALID_VALUE;
    }

    processVid->cb = (UINTPTR)processCB;
Z
zhushengle 已提交
98
    processVid->realParent = parent;
Z
zhushengle 已提交
99 100 101 102
    processCB->processID = vpid;
    LOS_ListDelete(&processVid->node);
    LOS_AtomicInc(&pidContainer->rc);

Z
zhushengle 已提交
103 104 105 106 107 108 109 110 111 112
    if (vpid == OS_USER_ROOT_PROCESS_ID) {
        if (parent != NULL) {
            ProcessVid *vppidItem = &pidContainer->pidArray[0];
            LOS_ListDelete(&vppidItem->node);
            vppidItem->cb = (UINTPTR)parent;
        }

        if (OsCreateProcessGroup(processCB) == NULL) {
            return OS_INVALID_VALUE;
        }
Z
zhushengle 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    }

    pidContainer = pidContainer->parent;
    while (pidContainer != NULL) {
        ProcessVid *item = OsGetFreeVpid(pidContainer);
        if (item == NULL) {
            break;
        }

        item->cb = (UINTPTR)processCB;
        processVid->vpid = item->vid;
        LOS_AtomicInc(&pidContainer->rc);
        processVid = item;
        pidContainer = pidContainer->parent;
    }
    return processCB->processID;
}

Z
zhushengle 已提交
131
STATIC UINT32 OsAllocVpid(LosProcessCB *processCB, LosProcessCB *parent)
Z
zhushengle 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
{
    ProcessVid *oldProcessVid = NULL;
    PidContainer *pidContainer = processCB->container->pidContainer;
    if ((pidContainer == NULL) || (LOS_AtomicRead(&pidContainer->lock) > 0)) {
        return OS_INVALID_VALUE;
    }

    processCB->processID = OS_INVALID_VALUE;
    do {
        ProcessVid *vpid = OsGetFreeVpid(pidContainer);
        if (vpid == NULL) {
            break;
        }
        vpid->cb = (UINTPTR)processCB;
        if (processCB->processID == OS_INVALID_VALUE) {
            processCB->processID = vpid->vid;
Z
zhushengle 已提交
148
            vpid->realParent = parent;
Z
zhushengle 已提交
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 176 177 178 179
        } else {
            oldProcessVid->vpid = vpid->vid;
        }
        LOS_AtomicInc(&pidContainer->rc);
        oldProcessVid = vpid;
        pidContainer = pidContainer->parent;
    } while (pidContainer != NULL);
    return processCB->processID;
}

STATIC ProcessVid *OsGetFreeVtid(PidContainer *pidContainer)
{
    if (LOS_ListEmpty(&pidContainer->tidFreeList)) {
        return NULL;
    }

    ProcessVid *vtid = LOS_DL_LIST_ENTRY(LOS_DL_LIST_FIRST(&pidContainer->tidFreeList), ProcessVid, node);
    LOS_ListDelete(&vtid->node);
    return vtid;
}

VOID OsFreeVtid(LosTaskCB *taskCB)
{
    PidContainer *pidContainer = taskCB->pidContainer;
    UINT32 vtid = taskCB->taskID;

    while ((pidContainer != NULL) && !OS_TID_CHECK_INVALID(vtid)) {
        ProcessVid *item = &pidContainer->tidArray[vtid];
        item->cb = (UINTPTR)g_defaultTaskCB;
        vtid = item->vpid;
        item->vpid = OS_INVALID_VALUE;
Z
zhushengle 已提交
180
        item->realParent = NULL;
Z
zhushengle 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        LOS_ListTailInsert(&pidContainer->tidFreeList, &item->node);
        pidContainer = pidContainer->parent;
    }
    taskCB->pidContainer = NULL;
}

UINT32 OsAllocVtid(LosTaskCB *taskCB, const LosProcessCB *processCB)
{
    PidContainer *pidContainer = processCB->container->pidContainer;
    ProcessVid *oldTaskVid = NULL;

    do {
        ProcessVid *item = OsGetFreeVtid(pidContainer);
        if (item == NULL) {
            return OS_INVALID_VALUE;
        }

        if ((pidContainer->parent != NULL) && (item->vid == 0)) {
            item->cb = (UINTPTR)OsCurrTaskGet();
            item->vpid = OsCurrTaskGet()->taskID;
            continue;
        }

        item->cb = (UINTPTR)taskCB;
        if (taskCB->pidContainer == NULL) {
            taskCB->pidContainer = pidContainer;
            taskCB->taskID = item->vid;
        } else {
            oldTaskVid->vpid = item->vid;
        }
        oldTaskVid = item;
        pidContainer = pidContainer->parent;
    } while (pidContainer != NULL);

    return taskCB->taskID;
}

Z
zhushengle 已提交
218
VOID OsPidContainerDestroyAllProcess(LosProcessCB *curr)
Z
zhushengle 已提交
219 220 221 222 223 224 225 226 227
{
    INT32 ret;
    UINT32 intSave;
    PidContainer *pidContainer = curr->container->pidContainer;
    LOS_AtomicInc(&pidContainer->lock);

    for (UINT32 index = 2; index < LOSCFG_BASE_CORE_PROCESS_LIMIT; index++) { /* 2: ordinary process */
        SCHEDULER_LOCK(intSave);
        LosProcessCB *processCB = OS_PCB_FROM_PID(index);
Z
zhushengle 已提交
228
        if (OsProcessIsUnused(processCB) || (processCB->parentProcess == NULL)) {
Z
zhushengle 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
            SCHEDULER_UNLOCK(intSave);
            continue;
        }

        if (curr != processCB->parentProcess) {
            LOS_ListDelete(&processCB->siblingList);
            if (OsProcessIsInactive(processCB)) {
                LOS_ListTailInsert(&curr->exitChildList, &processCB->siblingList);
            } else {
                LOS_ListTailInsert(&curr->childrenList, &processCB->siblingList);
            }
            processCB->parentProcess = curr;
        }
        SCHEDULER_UNLOCK(intSave);

        ret = OsKillLock(index, SIGKILL);
        if (ret < 0) {
            PRINT_ERR("Pid container kill all process failed, pid %u, errno=%d\n", index, -ret);
        }

        ret = LOS_Wait(index, NULL, 0, NULL);
        if (ret != index) {
            PRINT_ERR("Pid container wait pid %d failed, errno=%d\n", index, -ret);
        }
    }
}

STATIC PidContainer *CreateNewPidContainer(PidContainer *parent)
{
    UINT32 index;
    PidContainer *newPidContainer = (PidContainer *)LOS_MemAlloc(m_aucSysMem1, sizeof(PidContainer));
    if (newPidContainer == NULL) {
        return NULL;
    }
    (VOID)memset_s(newPidContainer, sizeof(PidContainer), 0, sizeof(PidContainer));

265
    newPidContainer->containerID = OsAllocContainerID();
Z
zhushengle 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    LOS_ListInit(&newPidContainer->pidFreeList);
    for (index = 0; index < LOSCFG_BASE_CORE_PROCESS_LIMIT; index++) {
        ProcessVid *vpid = &newPidContainer->pidArray[index];
        vpid->vid = index;
        vpid->vpid = OS_INVALID_VALUE;
        vpid->cb = (UINTPTR)g_defaultProcessCB;
        LOS_ListTailInsert(&newPidContainer->pidFreeList, &vpid->node);
    }

    LOS_ListInit(&newPidContainer->tidFreeList);
    for (index = 0; index < LOSCFG_BASE_CORE_TSK_LIMIT; index++) {
        ProcessVid *vtid = &newPidContainer->tidArray[index];
        vtid->vid = index;
        vtid->vpid = OS_INVALID_VALUE;
        vtid->cb = (UINTPTR)g_defaultTaskCB;
        LOS_ListTailInsert(&newPidContainer->tidFreeList, &vtid->node);
    }

    newPidContainer->parent = parent;
    if (parent != NULL) {
        LOS_AtomicSet(&newPidContainer->level, parent->level + 1);
Z
zhushengle 已提交
287
        newPidContainer->referenced = FALSE;
Z
zhushengle 已提交
288 289
    } else {
        LOS_AtomicSet(&newPidContainer->level, 0);
Z
zhushengle 已提交
290
        newPidContainer->referenced = TRUE;
Z
zhushengle 已提交
291 292 293 294
    }
    return newPidContainer;
}

Z
zhushengle 已提交
295
VOID OsPidContainerDestroy(Container *container, LosProcessCB *processCB)
Z
zhushengle 已提交
296
{
Z
zhushengle 已提交
297
    if (container == NULL) {
Z
zhushengle 已提交
298 299 300
        return;
    }

Z
zhushengle 已提交
301
    PidContainer *pidContainer = container->pidContainer;
Z
zhushengle 已提交
302 303 304 305
    if (pidContainer == NULL) {
        return;
    }

Z
zhushengle 已提交
306 307 308
    if (processCB != NULL) {
        FreeVpid(processCB);
    }
Z
zhushengle 已提交
309

Z
zhushengle 已提交
310 311 312
    if ((container->pidForChildContainer != NULL) && (pidContainer != container->pidForChildContainer)) {
        LOS_AtomicDec(&container->pidForChildContainer->rc);
        if (LOS_AtomicRead(&container->pidForChildContainer->rc) <= 0) {
Z
zhushengle 已提交
313
            g_currentPidContainerNum--;
Z
zhushengle 已提交
314 315
            (VOID)LOS_MemFree(m_aucSysMem1, container->pidForChildContainer);
            container->pidForChildContainer = NULL;
Z
zhushengle 已提交
316 317 318
        }
    }

Z
zhushengle 已提交
319
    if ((container->pidContainer != NULL) && (LOS_AtomicRead(&pidContainer->rc) <= 0)) {
Z
zhushengle 已提交
320
        g_currentPidContainerNum--;
Z
zhushengle 已提交
321 322 323
        container->pidContainer = NULL;
        container->pidForChildContainer = NULL;
        (VOID)LOS_MemFree(m_aucSysMem1, pidContainer->rootPGroup);
Z
zhushengle 已提交
324 325 326
        (VOID)LOS_MemFree(m_aucSysMem1, pidContainer);
    }

Z
zhushengle 已提交
327 328 329
    if (processCB != NULL) {
        OsContainerFree(processCB);
    }
Z
zhushengle 已提交
330 331
}

Z
zhushengle 已提交
332 333 334
STATIC UINT32 CreatePidContainer(LosProcessCB *child, LosProcessCB *parent)
{
    UINT32 ret;
Z
zhushengle 已提交
335 336
    UINT32 intSave;

Z
zhushengle 已提交
337
    PidContainer *parentContainer = parent->container->pidContainer;
Z
zhushengle 已提交
338 339 340
    PidContainer *newPidContainer = CreateNewPidContainer(parentContainer);
    if (newPidContainer == NULL) {
        return ENOMEM;
Z
zhushengle 已提交
341 342 343 344 345
    }

    SCHEDULER_LOCK(intSave);
    if ((parentContainer->level + 1) >= PID_CONTAINER_LEVEL_LIMIT) {
        (VOID)LOS_MemFree(m_aucSysMem1, newPidContainer);
Z
zhushengle 已提交
346
        SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
347 348 349 350
        return EINVAL;
    }

    g_currentPidContainerNum++;
Z
zhushengle 已提交
351
    newPidContainer->referenced = TRUE;
Z
zhushengle 已提交
352
    child->container->pidContainer = newPidContainer;
Z
zhushengle 已提交
353
    child->container->pidForChildContainer = newPidContainer;
Z
zhushengle 已提交
354
    ret = OsAllocSpecifiedVpidUnsafe(OS_USER_ROOT_PROCESS_ID, newPidContainer, child, parent);
Z
zhushengle 已提交
355 356 357 358
    if (ret == OS_INVALID_VALUE) {
        g_currentPidContainerNum--;
        FreeVpid(child);
        child->container->pidContainer = NULL;
Z
zhushengle 已提交
359
        child->container->pidForChildContainer = NULL;
Z
zhushengle 已提交
360 361 362 363 364 365 366 367
        SCHEDULER_UNLOCK(intSave);
        (VOID)LOS_MemFree(m_aucSysMem1, newPidContainer);
        return ENOSPC;
    }
    SCHEDULER_UNLOCK(intSave);
    return LOS_OK;
}

Z
zhushengle 已提交
368
STATIC UINT32 AllocVpidFormPidForChildContainer(LosProcessCB *child, LosProcessCB *parent)
Z
zhushengle 已提交
369
{
Z
zhushengle 已提交
370
    UINT32 ret;
Z
zhushengle 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    PidContainer *pidContainer = parent->container->pidForChildContainer;
    child->container->pidContainer = pidContainer;
    child->container->pidForChildContainer = pidContainer;

    if (!pidContainer->referenced) {
        pidContainer->referenced = TRUE;
        ret = OsAllocSpecifiedVpidUnsafe(OS_USER_ROOT_PROCESS_ID, pidContainer, child, parent);
    } else {
        ret = OsAllocVpid(child, parent);
        if (ret != OS_INVALID_VALUE) {
            LosProcessCB *parentProcessCB = (LosProcessCB *)pidContainer->pidArray[OS_USER_ROOT_PROCESS_ID].cb;
            child->parentProcess = parentProcessCB;
            LOS_ListTailInsert(&parentProcessCB->childrenList, &child->siblingList);
            child->pgroup = parentProcessCB->pgroup;
            LOS_ListTailInsert(&parentProcessCB->pgroup->processList, &child->subordinateGroupList);
        }
    }
Z
zhushengle 已提交
388

Z
zhushengle 已提交
389 390 391 392 393
    if (ret == OS_INVALID_VALUE) {
        FreeVpid(child);
        child->container->pidContainer = NULL;
        child->container->pidForChildContainer = NULL;
        return ENOSPC;
Z
zhushengle 已提交
394
    }
Z
zhushengle 已提交
395

Z
zhushengle 已提交
396
    return LOS_OK;
Z
zhushengle 已提交
397 398 399 400 401
}

UINT32 OsCopyPidContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID)
{
    UINT32 ret;
Z
zhushengle 已提交
402
    UINT32 intSave;
Z
zhushengle 已提交
403

Z
zhushengle 已提交
404 405 406
    SCHEDULER_LOCK(intSave);
    PidContainer *parentPidContainer = parent->container->pidContainer;
    PidContainer *parentPidContainerForChild = parent->container->pidForChildContainer;
Z
zhushengle 已提交
407
    if (parentPidContainer == parentPidContainerForChild) {
Z
zhushengle 已提交
408 409 410 411
        /* The current process is not executing unshare */
        if (!(flags & CLONE_NEWPID)) {
            child->container->pidContainer = parentPidContainer;
            child->container->pidForChildContainer = parentPidContainer;
Z
zhushengle 已提交
412
            ret = OsAllocVpid(child, parent);
Z
zhushengle 已提交
413 414 415 416 417 418 419
            SCHEDULER_UNLOCK(intSave);
            if (ret == OS_INVALID_VALUE) {
                PRINT_ERR("[%s] alloc vpid failed\n", __FUNCTION__);
                return ENOSPC;
            }
            *processID = child->processID;
            return LOS_OK;
Z
zhushengle 已提交
420
        }
Z
zhushengle 已提交
421
        SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
422

Z
zhushengle 已提交
423 424 425 426
        if (OsContainerLimitCheck(PID_CONTAINER, &g_currentPidContainerNum) != LOS_OK) {
            return EPERM;
        }

Z
zhushengle 已提交
427 428 429 430 431 432
        ret = CreatePidContainer(child, parent);
        if (ret != LOS_OK) {
            return ret;
        }
    } else {
        /* Create the first process after unshare */
Z
zhushengle 已提交
433
        ret = AllocVpidFormPidForChildContainer(child, parent);
Z
zhushengle 已提交
434 435 436 437
        SCHEDULER_UNLOCK(intSave);
        if (ret != LOS_OK) {
            return ret;
        }
Z
zhushengle 已提交
438 439 440 441 442 443 444 445 446 447 448
    }

    PidContainer *pidContainer = child->container->pidContainer;
    if (pidContainer->pidArray[child->processID].vpid == OS_INVALID_VALUE) {
        *processID = child->processID;
    } else {
        *processID = pidContainer->pidArray[child->processID].vpid;
    }
    return LOS_OK;
}

Z
zhushengle 已提交
449 450 451 452 453 454
UINT32 OsUnsharePidContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
{
    UINT32 intSave;
    if (!(flags & CLONE_NEWPID)) {
        SCHEDULER_LOCK(intSave);
        newContainer->pidContainer = curr->container->pidContainer;
Z
zhushengle 已提交
455 456 457 458
        newContainer->pidForChildContainer = curr->container->pidForChildContainer;
        if (newContainer->pidContainer != newContainer->pidForChildContainer) {
            LOS_AtomicInc(&newContainer->pidForChildContainer->rc);
        }
Z
zhushengle 已提交
459 460 461 462
        SCHEDULER_UNLOCK(intSave);
        return LOS_OK;
    }

Z
zhushengle 已提交
463 464 465 466
    if (OsContainerLimitCheck(PID_CONTAINER, &g_currentPidContainerNum) != LOS_OK) {
        return EPERM;
    }

Z
zhushengle 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    PidContainer *pidForChild = CreateNewPidContainer(curr->container->pidContainer);
    if (pidForChild == NULL) {
        return ENOMEM;
    }

    SCHEDULER_LOCK(intSave);
    if (curr->container->pidContainer != curr->container->pidForChildContainer) {
        SCHEDULER_UNLOCK(intSave);
        (VOID)LOS_MemFree(m_aucSysMem1, pidForChild);
        return EINVAL;
    }

    if (pidForChild->level >= PID_CONTAINER_LEVEL_LIMIT) {
        SCHEDULER_UNLOCK(intSave);
        (VOID)LOS_MemFree(m_aucSysMem1, pidForChild);
        return EINVAL;
    }

Z
zhushengle 已提交
485
    g_currentPidContainerNum++;
Z
zhushengle 已提交
486 487
    newContainer->pidContainer = curr->container->pidContainer;
    newContainer->pidForChildContainer = pidForChild;
Z
zhushengle 已提交
488
    LOS_AtomicSet(&pidForChild->rc, 1);
Z
zhushengle 已提交
489 490 491 492
    SCHEDULER_UNLOCK(intSave);
    return LOS_OK;
}

Z
zhushengle 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
UINT32 OsSetNsPidContainer(UINT32 flags, Container *container, Container *newContainer)
{
    UINT32 ret = LOS_OK;
    LosProcessCB *curr = OsCurrProcessGet();
    newContainer->pidContainer = curr->container->pidContainer;

    if (flags & CLONE_NEWPID) {
        newContainer->pidForChildContainer = container->pidContainer;
        LOS_AtomicInc(&container->pidContainer->rc);
        return ret;
    }

    newContainer->pidForChildContainer = curr->container->pidForChildContainer;
    if (newContainer->pidContainer != newContainer->pidForChildContainer) {
        LOS_AtomicInc(&newContainer->pidForChildContainer->rc);
    }
    return LOS_OK;
}

Z
zhushengle 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
UINT32 OsInitRootPidContainer(PidContainer **pidContainer)
{
    UINT32 intSave;
    g_defaultTaskCB = OsGetDefaultTaskCB();
    g_defaultProcessCB = OsGetDefaultProcessCB();

    PidContainer *newPidContainer = CreateNewPidContainer(NULL);
    if (newPidContainer == NULL) {
        return ENOMEM;
    }

    SCHEDULER_LOCK(intSave);
    g_currentPidContainerNum++;
    *pidContainer = newPidContainer;
    SCHEDULER_UNLOCK(intSave);
    return LOS_OK;
}

UINT32 OsGetVpidFromCurrContainer(const LosProcessCB *processCB)
{
    UINT32 vpid = processCB->processID;
    PidContainer *pidContainer = processCB->container->pidContainer;
    PidContainer *currPidContainer = OsCurrTaskGet()->pidContainer;
    while (pidContainer != NULL) {
        ProcessVid *vid = &pidContainer->pidArray[vpid];
        if (currPidContainer != pidContainer) {
            vpid = vid->vpid;
            pidContainer = pidContainer->parent;
            continue;
        }

        return vid->vid;
    }
    return OS_INVALID_VALUE;
}

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
UINT32 OsGetVpidFromRootContainer(const LosProcessCB *processCB)
{
    UINT32 vpid = processCB->processID;
    PidContainer *pidContainer = processCB->container->pidContainer;
    while (pidContainer != NULL) {
        ProcessVid *vid = &pidContainer->pidArray[vpid];
        if (pidContainer->parent != NULL) {
            vpid = vid->vpid;
            pidContainer = pidContainer->parent;
            continue;
        }

        return vid->vid;
    }
    return OS_INVALID_VALUE;
}

Z
zhushengle 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
UINT32 OsGetVtidFromCurrContainer(const LosTaskCB *taskCB)
{
    UINT32 vtid = taskCB->taskID;
    PidContainer *pidContainer = taskCB->pidContainer;
    PidContainer *currPidContainer = OsCurrTaskGet()->pidContainer;
    while (pidContainer != NULL) {
        ProcessVid *vid = &pidContainer->tidArray[vtid];
        if (currPidContainer != pidContainer) {
            vtid = vid->vpid;
            pidContainer = pidContainer->parent;
            continue;
        }
        return vid->vid;
    }
    return OS_INVALID_VALUE;
}

LosProcessCB *OsGetPCBFromVpid(UINT32 vpid)
{
    PidContainer *pidContainer = OsCurrTaskGet()->pidContainer;
    ProcessVid *processVid = &pidContainer->pidArray[vpid];
    return (LosProcessCB *)processVid->cb;
}

LosTaskCB *OsGetTCBFromVtid(UINT32 vtid)
{
    PidContainer *pidContainer = OsCurrTaskGet()->pidContainer;
    ProcessVid *taskVid = &pidContainer->tidArray[vtid];
    return (LosTaskCB *)taskVid->cb;
}

Z
zhushengle 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609
BOOL OsPidContainerProcessParentIsRealParent(const LosProcessCB *processCB, const LosProcessCB *curr)
{
    if (curr == processCB->parentProcess) {
        return FALSE;
    }

    PidContainer *pidContainer = processCB->container->pidContainer;
    ProcessVid *processVid = &pidContainer->pidArray[processCB->processID];
    if (processVid->realParent == curr) {
        return TRUE;
    }
    return FALSE;
}

610 611 612 613 614 615 616 617
UINT32 OsGetPidContainerID(PidContainer *pidContainer)
{
    if (pidContainer == NULL) {
        return OS_INVALID_VALUE;
    }

    return pidContainer->containerID;
}
Z
zhushengle 已提交
618 619 620 621 622

UINT32 OsGetPidContainerCount(VOID)
{
    return g_currentPidContainerNum;
}
Z
zhushengle 已提交
623
#endif