• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sched.h>
32 #include "los_pid_container_pri.h"
33 #include "los_config.h"
34 #include "los_process_pri.h"
35 #include "los_container_pri.h"
36 
37 #ifdef LOSCFG_PID_CONTAINER
38 
39 STATIC UINT32 g_currentPidContainerNum;
40 STATIC LosProcessCB *g_defaultProcessCB = NULL;
41 STATIC LosTaskCB *g_defaultTaskCB = NULL;
42 
FreeVpid(LosProcessCB * processCB)43 STATIC VOID FreeVpid(LosProcessCB *processCB)
44 {
45     PidContainer *pidContainer = processCB->container->pidContainer;
46     UINT32 vpid = processCB->processID;
47 
48     while ((pidContainer != NULL) && !OS_PID_CHECK_INVALID(vpid)) {
49         ProcessVid *processVid = &pidContainer->pidArray[vpid];
50         processVid->cb = (UINTPTR)g_defaultProcessCB;
51         vpid = processVid->vpid;
52         processVid->vpid = OS_INVALID_VALUE;
53         LOS_ListTailInsert(&pidContainer->pidFreeList, &processVid->node);
54         LOS_AtomicDec(&pidContainer->rc);
55         PidContainer *parentPidContainer = pidContainer->parent;
56         if (LOS_AtomicRead(&pidContainer->rc) > 0) {
57             pidContainer = parentPidContainer;
58             continue;
59         }
60         g_currentPidContainerNum--;
61         (VOID)LOS_MemFree(m_aucSysMem1, pidContainer->rootPGroup);
62         (VOID)LOS_MemFree(m_aucSysMem1, pidContainer);
63         if (pidContainer == processCB->container->pidContainer) {
64             processCB->container->pidContainer = NULL;
65         }
66         pidContainer = parentPidContainer;
67     }
68 }
69 
OsGetFreeVpid(PidContainer * pidContainer)70 STATIC ProcessVid *OsGetFreeVpid(PidContainer *pidContainer)
71 {
72     if (LOS_ListEmpty(&pidContainer->pidFreeList)) {
73         return NULL;
74     }
75 
76     ProcessVid *vpid = LOS_DL_LIST_ENTRY(LOS_DL_LIST_FIRST(&pidContainer->pidFreeList), ProcessVid, node);
77     LOS_ListDelete(&vpid->node);
78     return vpid;
79 }
80 
OsAllocSpecifiedVpidUnsafe(UINT32 vpid,PidContainer * pidContainer,LosProcessCB * processCB,LosProcessCB * parent)81 UINT32 OsAllocSpecifiedVpidUnsafe(UINT32 vpid, PidContainer *pidContainer,
82                                   LosProcessCB *processCB, LosProcessCB *parent)
83 {
84     if ((pidContainer == NULL) || OS_PID_CHECK_INVALID(vpid)) {
85         return OS_INVALID_VALUE;
86     }
87 
88     if (LOS_AtomicRead(&pidContainer->lock) > 0) {
89         return OS_INVALID_VALUE;
90     }
91 
92     ProcessVid *processVid = &pidContainer->pidArray[vpid];
93     if (processVid->cb != (UINTPTR)g_defaultProcessCB) {
94         return OS_INVALID_VALUE;
95     }
96 
97     processVid->cb = (UINTPTR)processCB;
98     processVid->realParent = parent;
99     processCB->processID = vpid;
100     LOS_ListDelete(&processVid->node);
101     LOS_AtomicInc(&pidContainer->rc);
102 
103     if (vpid == OS_USER_ROOT_PROCESS_ID) {
104         if (parent != NULL) {
105             ProcessVid *vppidItem = &pidContainer->pidArray[0];
106             LOS_ListDelete(&vppidItem->node);
107             vppidItem->cb = (UINTPTR)parent;
108         }
109 
110         if (OsCreateProcessGroup(processCB) == NULL) {
111             return OS_INVALID_VALUE;
112         }
113     }
114 
115     pidContainer = pidContainer->parent;
116     while (pidContainer != NULL) {
117         ProcessVid *item = OsGetFreeVpid(pidContainer);
118         if (item == NULL) {
119             break;
120         }
121 
122         item->cb = (UINTPTR)processCB;
123         processVid->vpid = item->vid;
124         LOS_AtomicInc(&pidContainer->rc);
125         processVid = item;
126         pidContainer = pidContainer->parent;
127     }
128     return processCB->processID;
129 }
130 
OsAllocVpid(LosProcessCB * processCB,LosProcessCB * parent)131 STATIC UINT32 OsAllocVpid(LosProcessCB *processCB, LosProcessCB *parent)
132 {
133     ProcessVid *oldProcessVid = NULL;
134     PidContainer *pidContainer = processCB->container->pidContainer;
135     if ((pidContainer == NULL) || (LOS_AtomicRead(&pidContainer->lock) > 0)) {
136         return OS_INVALID_VALUE;
137     }
138 
139     processCB->processID = OS_INVALID_VALUE;
140     do {
141         ProcessVid *vpid = OsGetFreeVpid(pidContainer);
142         if (vpid == NULL) {
143             break;
144         }
145         vpid->cb = (UINTPTR)processCB;
146         if (processCB->processID == OS_INVALID_VALUE) {
147             processCB->processID = vpid->vid;
148             vpid->realParent = parent;
149         } else {
150             oldProcessVid->vpid = vpid->vid;
151         }
152         LOS_AtomicInc(&pidContainer->rc);
153         oldProcessVid = vpid;
154         pidContainer = pidContainer->parent;
155     } while (pidContainer != NULL);
156     return processCB->processID;
157 }
158 
OsGetFreeVtid(PidContainer * pidContainer)159 STATIC ProcessVid *OsGetFreeVtid(PidContainer *pidContainer)
160 {
161     if (LOS_ListEmpty(&pidContainer->tidFreeList)) {
162         return NULL;
163     }
164 
165     ProcessVid *vtid = LOS_DL_LIST_ENTRY(LOS_DL_LIST_FIRST(&pidContainer->tidFreeList), ProcessVid, node);
166     LOS_ListDelete(&vtid->node);
167     return vtid;
168 }
169 
OsFreeVtid(LosTaskCB * taskCB)170 VOID OsFreeVtid(LosTaskCB *taskCB)
171 {
172     PidContainer *pidContainer = taskCB->pidContainer;
173     UINT32 vtid = taskCB->taskID;
174 
175     while ((pidContainer != NULL) && !OS_TID_CHECK_INVALID(vtid)) {
176         ProcessVid *item = &pidContainer->tidArray[vtid];
177         item->cb = (UINTPTR)g_defaultTaskCB;
178         vtid = item->vpid;
179         item->vpid = OS_INVALID_VALUE;
180         item->realParent = NULL;
181         LOS_ListTailInsert(&pidContainer->tidFreeList, &item->node);
182         pidContainer = pidContainer->parent;
183     }
184     taskCB->pidContainer = NULL;
185 }
186 
OsAllocVtid(LosTaskCB * taskCB,const LosProcessCB * processCB)187 UINT32 OsAllocVtid(LosTaskCB *taskCB, const LosProcessCB *processCB)
188 {
189     PidContainer *pidContainer = processCB->container->pidContainer;
190     ProcessVid *oldTaskVid = NULL;
191 
192     do {
193         ProcessVid *item = OsGetFreeVtid(pidContainer);
194         if (item == NULL) {
195             return OS_INVALID_VALUE;
196         }
197 
198         if ((pidContainer->parent != NULL) && (item->vid == 0)) {
199             item->cb = (UINTPTR)OsCurrTaskGet();
200             item->vpid = OsCurrTaskGet()->taskID;
201             continue;
202         }
203 
204         item->cb = (UINTPTR)taskCB;
205         if (taskCB->pidContainer == NULL) {
206             taskCB->pidContainer = pidContainer;
207             taskCB->taskID = item->vid;
208         } else {
209             oldTaskVid->vpid = item->vid;
210         }
211         oldTaskVid = item;
212         pidContainer = pidContainer->parent;
213     } while (pidContainer != NULL);
214 
215     return taskCB->taskID;
216 }
217 
OsPidContainerDestroyAllProcess(LosProcessCB * curr)218 VOID OsPidContainerDestroyAllProcess(LosProcessCB *curr)
219 {
220     INT32 ret;
221     UINT32 intSave;
222     PidContainer *pidContainer = curr->container->pidContainer;
223     LOS_AtomicInc(&pidContainer->lock);
224 
225     for (UINT32 index = 2; index < LOSCFG_BASE_CORE_PROCESS_LIMIT; index++) { /* 2: ordinary process */
226         SCHEDULER_LOCK(intSave);
227         LosProcessCB *processCB = OS_PCB_FROM_PID(index);
228         if (OsProcessIsUnused(processCB) || (processCB->parentProcess == NULL)) {
229             SCHEDULER_UNLOCK(intSave);
230             continue;
231         }
232 
233         if (curr != processCB->parentProcess) {
234             LOS_ListDelete(&processCB->siblingList);
235             if (OsProcessIsInactive(processCB)) {
236                 LOS_ListTailInsert(&curr->exitChildList, &processCB->siblingList);
237             } else {
238                 LOS_ListTailInsert(&curr->childrenList, &processCB->siblingList);
239             }
240             processCB->parentProcess = curr;
241         }
242         SCHEDULER_UNLOCK(intSave);
243 
244         ret = OsKillLock(index, SIGKILL);
245         if (ret < 0) {
246             PRINT_ERR("Pid container kill all process failed, pid %u, errno=%d\n", index, -ret);
247         }
248 
249         ret = LOS_Wait(index, NULL, 0, NULL);
250         if (ret != index) {
251             PRINT_ERR("Pid container wait pid %d failed, errno=%d\n", index, -ret);
252         }
253     }
254 }
255 
CreateNewPidContainer(PidContainer * parent)256 STATIC PidContainer *CreateNewPidContainer(PidContainer *parent)
257 {
258     UINT32 index;
259     PidContainer *newPidContainer = (PidContainer *)LOS_MemAlloc(m_aucSysMem1, sizeof(PidContainer));
260     if (newPidContainer == NULL) {
261         return NULL;
262     }
263     (VOID)memset_s(newPidContainer, sizeof(PidContainer), 0, sizeof(PidContainer));
264 
265     newPidContainer->containerID = OsAllocContainerID();
266     LOS_ListInit(&newPidContainer->pidFreeList);
267     for (index = 0; index < LOSCFG_BASE_CORE_PROCESS_LIMIT; index++) {
268         ProcessVid *vpid = &newPidContainer->pidArray[index];
269         vpid->vid = index;
270         vpid->vpid = OS_INVALID_VALUE;
271         vpid->cb = (UINTPTR)g_defaultProcessCB;
272         LOS_ListTailInsert(&newPidContainer->pidFreeList, &vpid->node);
273     }
274 
275     LOS_ListInit(&newPidContainer->tidFreeList);
276     for (index = 0; index < LOSCFG_BASE_CORE_TSK_LIMIT; index++) {
277         ProcessVid *vtid = &newPidContainer->tidArray[index];
278         vtid->vid = index;
279         vtid->vpid = OS_INVALID_VALUE;
280         vtid->cb = (UINTPTR)g_defaultTaskCB;
281         LOS_ListTailInsert(&newPidContainer->tidFreeList, &vtid->node);
282     }
283 
284     newPidContainer->parent = parent;
285     if (parent != NULL) {
286         LOS_AtomicSet(&newPidContainer->level, parent->level + 1);
287         newPidContainer->referenced = FALSE;
288     } else {
289         LOS_AtomicSet(&newPidContainer->level, 0);
290         newPidContainer->referenced = TRUE;
291     }
292     return newPidContainer;
293 }
294 
OsPidContainerDestroy(Container * container,LosProcessCB * processCB)295 VOID OsPidContainerDestroy(Container *container, LosProcessCB *processCB)
296 {
297     if (container == NULL) {
298         return;
299     }
300 
301     PidContainer *pidContainer = container->pidContainer;
302     if (pidContainer == NULL) {
303         return;
304     }
305 
306     if (processCB != NULL) {
307         FreeVpid(processCB);
308     }
309 
310     if ((container->pidForChildContainer != NULL) && (pidContainer != container->pidForChildContainer)) {
311         LOS_AtomicDec(&container->pidForChildContainer->rc);
312         if (LOS_AtomicRead(&container->pidForChildContainer->rc) <= 0) {
313             g_currentPidContainerNum--;
314             (VOID)LOS_MemFree(m_aucSysMem1, container->pidForChildContainer);
315             container->pidForChildContainer = NULL;
316         }
317     }
318 
319     if ((container->pidContainer != NULL) && (LOS_AtomicRead(&pidContainer->rc) <= 0)) {
320         g_currentPidContainerNum--;
321         container->pidContainer = NULL;
322         container->pidForChildContainer = NULL;
323         (VOID)LOS_MemFree(m_aucSysMem1, pidContainer->rootPGroup);
324         (VOID)LOS_MemFree(m_aucSysMem1, pidContainer);
325     }
326 
327     if (processCB != NULL) {
328         OsContainerFree(processCB);
329     }
330 }
331 
CreatePidContainer(LosProcessCB * child,LosProcessCB * parent)332 STATIC UINT32 CreatePidContainer(LosProcessCB *child, LosProcessCB *parent)
333 {
334     UINT32 ret;
335     UINT32 intSave;
336 
337     PidContainer *parentContainer = parent->container->pidContainer;
338     PidContainer *newPidContainer = CreateNewPidContainer(parentContainer);
339     if (newPidContainer == NULL) {
340         return ENOMEM;
341     }
342 
343     SCHEDULER_LOCK(intSave);
344     if ((parentContainer->level + 1) >= PID_CONTAINER_LEVEL_LIMIT) {
345         (VOID)LOS_MemFree(m_aucSysMem1, newPidContainer);
346         SCHEDULER_UNLOCK(intSave);
347         return EINVAL;
348     }
349 
350     g_currentPidContainerNum++;
351     newPidContainer->referenced = TRUE;
352     child->container->pidContainer = newPidContainer;
353     child->container->pidForChildContainer = newPidContainer;
354     ret = OsAllocSpecifiedVpidUnsafe(OS_USER_ROOT_PROCESS_ID, newPidContainer, child, parent);
355     if (ret == OS_INVALID_VALUE) {
356         g_currentPidContainerNum--;
357         FreeVpid(child);
358         child->container->pidContainer = NULL;
359         child->container->pidForChildContainer = NULL;
360         SCHEDULER_UNLOCK(intSave);
361         (VOID)LOS_MemFree(m_aucSysMem1, newPidContainer);
362         return ENOSPC;
363     }
364     SCHEDULER_UNLOCK(intSave);
365     return LOS_OK;
366 }
367 
AllocVpidFormPidForChildContainer(LosProcessCB * child,LosProcessCB * parent)368 STATIC UINT32 AllocVpidFormPidForChildContainer(LosProcessCB *child, LosProcessCB *parent)
369 {
370     UINT32 ret;
371     PidContainer *pidContainer = parent->container->pidForChildContainer;
372     child->container->pidContainer = pidContainer;
373     child->container->pidForChildContainer = pidContainer;
374 
375     if (!pidContainer->referenced) {
376         pidContainer->referenced = TRUE;
377         ret = OsAllocSpecifiedVpidUnsafe(OS_USER_ROOT_PROCESS_ID, pidContainer, child, parent);
378     } else {
379         ret = OsAllocVpid(child, parent);
380         if (ret != OS_INVALID_VALUE) {
381             LosProcessCB *parentProcessCB = (LosProcessCB *)pidContainer->pidArray[OS_USER_ROOT_PROCESS_ID].cb;
382             child->parentProcess = parentProcessCB;
383             LOS_ListTailInsert(&parentProcessCB->childrenList, &child->siblingList);
384             child->pgroup = parentProcessCB->pgroup;
385             LOS_ListTailInsert(&parentProcessCB->pgroup->processList, &child->subordinateGroupList);
386         }
387     }
388 
389     if (ret == OS_INVALID_VALUE) {
390         FreeVpid(child);
391         child->container->pidContainer = NULL;
392         child->container->pidForChildContainer = NULL;
393         return ENOSPC;
394     }
395 
396     return LOS_OK;
397 }
398 
OsCopyPidContainer(UINTPTR flags,LosProcessCB * child,LosProcessCB * parent,UINT32 * processID)399 UINT32 OsCopyPidContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent, UINT32 *processID)
400 {
401     UINT32 ret;
402     UINT32 intSave;
403 
404     SCHEDULER_LOCK(intSave);
405     PidContainer *parentPidContainer = parent->container->pidContainer;
406     PidContainer *parentPidContainerForChild = parent->container->pidForChildContainer;
407     if (parentPidContainer == parentPidContainerForChild) {
408         /* The current process is not executing unshare */
409         if (!(flags & CLONE_NEWPID)) {
410             child->container->pidContainer = parentPidContainer;
411             child->container->pidForChildContainer = parentPidContainer;
412             ret = OsAllocVpid(child, parent);
413             SCHEDULER_UNLOCK(intSave);
414             if (ret == OS_INVALID_VALUE) {
415                 PRINT_ERR("[%s] alloc vpid failed\n", __FUNCTION__);
416                 return ENOSPC;
417             }
418             *processID = child->processID;
419             return LOS_OK;
420         }
421         SCHEDULER_UNLOCK(intSave);
422 
423         if (OsContainerLimitCheck(PID_CONTAINER, &g_currentPidContainerNum) != LOS_OK) {
424             return EPERM;
425         }
426 
427         ret = CreatePidContainer(child, parent);
428         if (ret != LOS_OK) {
429             return ret;
430         }
431     } else {
432         /* Create the first process after unshare */
433         ret = AllocVpidFormPidForChildContainer(child, parent);
434         SCHEDULER_UNLOCK(intSave);
435         if (ret != LOS_OK) {
436             return ret;
437         }
438     }
439 
440     PidContainer *pidContainer = child->container->pidContainer;
441     if (pidContainer->pidArray[child->processID].vpid == OS_INVALID_VALUE) {
442         *processID = child->processID;
443     } else {
444         *processID = pidContainer->pidArray[child->processID].vpid;
445     }
446     return LOS_OK;
447 }
448 
OsUnsharePidContainer(UINTPTR flags,LosProcessCB * curr,Container * newContainer)449 UINT32 OsUnsharePidContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
450 {
451     UINT32 intSave;
452     if (!(flags & CLONE_NEWPID)) {
453         SCHEDULER_LOCK(intSave);
454         newContainer->pidContainer = curr->container->pidContainer;
455         newContainer->pidForChildContainer = curr->container->pidForChildContainer;
456         if (newContainer->pidContainer != newContainer->pidForChildContainer) {
457             LOS_AtomicInc(&newContainer->pidForChildContainer->rc);
458         }
459         SCHEDULER_UNLOCK(intSave);
460         return LOS_OK;
461     }
462 
463     if (OsContainerLimitCheck(PID_CONTAINER, &g_currentPidContainerNum) != LOS_OK) {
464         return EPERM;
465     }
466 
467     PidContainer *pidForChild = CreateNewPidContainer(curr->container->pidContainer);
468     if (pidForChild == NULL) {
469         return ENOMEM;
470     }
471 
472     SCHEDULER_LOCK(intSave);
473     if (curr->container->pidContainer != curr->container->pidForChildContainer) {
474         SCHEDULER_UNLOCK(intSave);
475         (VOID)LOS_MemFree(m_aucSysMem1, pidForChild);
476         return EINVAL;
477     }
478 
479     if (pidForChild->level >= PID_CONTAINER_LEVEL_LIMIT) {
480         SCHEDULER_UNLOCK(intSave);
481         (VOID)LOS_MemFree(m_aucSysMem1, pidForChild);
482         return EINVAL;
483     }
484 
485     g_currentPidContainerNum++;
486     newContainer->pidContainer = curr->container->pidContainer;
487     newContainer->pidForChildContainer = pidForChild;
488     LOS_AtomicSet(&pidForChild->rc, 1);
489     SCHEDULER_UNLOCK(intSave);
490     return LOS_OK;
491 }
492 
OsSetNsPidContainer(UINT32 flags,Container * container,Container * newContainer)493 UINT32 OsSetNsPidContainer(UINT32 flags, Container *container, Container *newContainer)
494 {
495     UINT32 ret = LOS_OK;
496     LosProcessCB *curr = OsCurrProcessGet();
497     newContainer->pidContainer = curr->container->pidContainer;
498 
499     if (flags & CLONE_NEWPID) {
500         newContainer->pidForChildContainer = container->pidContainer;
501         LOS_AtomicInc(&container->pidContainer->rc);
502         return ret;
503     }
504 
505     newContainer->pidForChildContainer = curr->container->pidForChildContainer;
506     if (newContainer->pidContainer != newContainer->pidForChildContainer) {
507         LOS_AtomicInc(&newContainer->pidForChildContainer->rc);
508     }
509     return LOS_OK;
510 }
511 
OsInitRootPidContainer(PidContainer ** pidContainer)512 UINT32 OsInitRootPidContainer(PidContainer **pidContainer)
513 {
514     UINT32 intSave;
515     g_defaultTaskCB = OsGetDefaultTaskCB();
516     g_defaultProcessCB = OsGetDefaultProcessCB();
517 
518     PidContainer *newPidContainer = CreateNewPidContainer(NULL);
519     if (newPidContainer == NULL) {
520         return ENOMEM;
521     }
522 
523     SCHEDULER_LOCK(intSave);
524     g_currentPidContainerNum++;
525     *pidContainer = newPidContainer;
526     SCHEDULER_UNLOCK(intSave);
527     return LOS_OK;
528 }
529 
OsGetVpidFromCurrContainer(const LosProcessCB * processCB)530 UINT32 OsGetVpidFromCurrContainer(const LosProcessCB *processCB)
531 {
532     UINT32 vpid = processCB->processID;
533     PidContainer *pidContainer = processCB->container->pidContainer;
534     PidContainer *currPidContainer = OsCurrTaskGet()->pidContainer;
535     while (pidContainer != NULL) {
536         ProcessVid *vid = &pidContainer->pidArray[vpid];
537         if (currPidContainer != pidContainer) {
538             vpid = vid->vpid;
539             pidContainer = pidContainer->parent;
540             continue;
541         }
542 
543         return vid->vid;
544     }
545     return OS_INVALID_VALUE;
546 }
547 
OsGetVpidFromRootContainer(const LosProcessCB * processCB)548 UINT32 OsGetVpidFromRootContainer(const LosProcessCB *processCB)
549 {
550     UINT32 vpid = processCB->processID;
551     PidContainer *pidContainer = processCB->container->pidContainer;
552     while (pidContainer != NULL) {
553         ProcessVid *vid = &pidContainer->pidArray[vpid];
554         if (pidContainer->parent != NULL) {
555             vpid = vid->vpid;
556             pidContainer = pidContainer->parent;
557             continue;
558         }
559 
560         return vid->vid;
561     }
562     return OS_INVALID_VALUE;
563 }
564 
OsGetVtidFromCurrContainer(const LosTaskCB * taskCB)565 UINT32 OsGetVtidFromCurrContainer(const LosTaskCB *taskCB)
566 {
567     UINT32 vtid = taskCB->taskID;
568     PidContainer *pidContainer = taskCB->pidContainer;
569     PidContainer *currPidContainer = OsCurrTaskGet()->pidContainer;
570     while (pidContainer != NULL) {
571         ProcessVid *vid = &pidContainer->tidArray[vtid];
572         if (currPidContainer != pidContainer) {
573             vtid = vid->vpid;
574             pidContainer = pidContainer->parent;
575             continue;
576         }
577         return vid->vid;
578     }
579     return OS_INVALID_VALUE;
580 }
581 
OsGetPCBFromVpid(UINT32 vpid)582 LosProcessCB *OsGetPCBFromVpid(UINT32 vpid)
583 {
584     PidContainer *pidContainer = OsCurrTaskGet()->pidContainer;
585     ProcessVid *processVid = &pidContainer->pidArray[vpid];
586     return (LosProcessCB *)processVid->cb;
587 }
588 
OsGetTCBFromVtid(UINT32 vtid)589 LosTaskCB *OsGetTCBFromVtid(UINT32 vtid)
590 {
591     PidContainer *pidContainer = OsCurrTaskGet()->pidContainer;
592     ProcessVid *taskVid = &pidContainer->tidArray[vtid];
593     return (LosTaskCB *)taskVid->cb;
594 }
595 
OsPidContainerProcessParentIsRealParent(const LosProcessCB * processCB,const LosProcessCB * curr)596 BOOL OsPidContainerProcessParentIsRealParent(const LosProcessCB *processCB, const LosProcessCB *curr)
597 {
598     if (curr == processCB->parentProcess) {
599         return FALSE;
600     }
601 
602     PidContainer *pidContainer = processCB->container->pidContainer;
603     ProcessVid *processVid = &pidContainer->pidArray[processCB->processID];
604     if (processVid->realParent == curr) {
605         return TRUE;
606     }
607     return FALSE;
608 }
609 
OsGetPidContainerID(PidContainer * pidContainer)610 UINT32 OsGetPidContainerID(PidContainer *pidContainer)
611 {
612     if (pidContainer == NULL) {
613         return OS_INVALID_VALUE;
614     }
615 
616     return pidContainer->containerID;
617 }
618 
OsGetPidContainerCount(VOID)619 UINT32 OsGetPidContainerCount(VOID)
620 {
621     return g_currentPidContainerNum;
622 }
623 #endif
624