• 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 #include "It_container_test.h"
31 
32 const int TEST_COUNT = 5;
33 const int TEST_PARENT_SLEEP_TIME = 2;
34 const int TEST_CHILD_SLEEP_TIME  = 5;
ChildShell(void * p)35 static int ChildShell(void *p)
36 {
37     printf("\n###################### %s pid container process info ####################\n", (char *)p);
38     int ret = execl("/bin/shell", "shell", "task", "-a", (char *)0);
39     if (ret < 0) {
40         return errno;
41     }
42     return 0;
43 }
44 
PthreadFunc(void * arg)45 static void *PthreadFunc(void *arg)
46 {
47     (void)arg;
48     sleep(TEST_CHILD_SLEEP_TIME);
49     return NULL;
50 }
51 
Child2(void * p)52 static int Child2(void *p)
53 {
54     (void)p;
55     sleep(TEST_CHILD_SLEEP_TIME);
56     exit(0);
57 }
58 
Child1(void * p)59 static int Child1(void *p)
60 {
61     (void)p;
62     int ret;
63     pid_t pid = fork();
64     if (pid == 0) {
65         sleep(TEST_CHILD_SLEEP_TIME);
66         exit(0);
67     }
68 
69     sleep(TEST_CHILD_SLEEP_TIME);
70 
71     ret = waitpid(pid, NULL, 0);
72     if (ret != pid) {
73         exit(EXIT_CODE_ERRNO_5);
74     }
75     return 0;
76 }
77 
ChildFun(void * p)78 static int ChildFun(void *p)
79 {
80     (void)p;
81     int ret;
82 
83     pid_t pid1 = fork();
84     if (pid1 == 0) {
85         ret = Child1(NULL);
86         exit(ret);
87     }
88 
89     pid_t pid2 = fork();
90     if (pid2 == 0) {
91         (void)Child2(NULL);
92     }
93 
94     pid_t pid3 = fork();
95     if (pid3 == 0) {
96         ret = ChildShell(static_cast<void *>(const_cast<char*>("Child")));
97         if (ret != 0) {
98             exit(EXIT_CODE_ERRNO_1);
99         }
100         exit(0);
101     }
102 
103     sleep(TEST_CHILD_SLEEP_TIME);
104 
105     ret = waitpid(pid1, NULL, 0);
106     if (ret != pid1) {
107         exit(EXIT_CODE_ERRNO_2);
108     }
109 
110     ret = waitpid(pid2, NULL, 0);
111     if (ret != pid2) {
112         exit(EXIT_CODE_ERRNO_3);
113     }
114 
115     ret = waitpid(pid3, NULL, 0);
116     if (ret != pid3) {
117         exit(EXIT_CODE_ERRNO_4);
118     }
119 
120     return 0;
121 }
122 
ItPidContainer018(void)123 void ItPidContainer018(void)
124 {
125     int status;
126     int ret;
127     pid_t pid;
128     void *pstk = malloc(STACK_SIZE);
129     ASSERT_TRUE(pstk != NULL);
130     int childPid = clone(ChildFun, (char *)pstk + STACK_SIZE, CLONE_NEWPID | SIGCHLD, NULL);
131     free(pstk);
132     ASSERT_NE(childPid, -1);
133 
134     sleep(TEST_PARENT_SLEEP_TIME);
135 
136     pid = fork();
137     if (pid == 0) {
138         ret = ChildShell(static_cast<void *>(const_cast<char*>("Parent")));
139         ASSERT_EQ(ret, 0);
140     }
141 
142     ret = waitpid(pid, &status, 0);
143     ASSERT_EQ(ret, pid);
144     ret = WIFEXITED(status);
145     ASSERT_NE(ret, 0);
146     ret = WEXITSTATUS(status);
147     ASSERT_EQ(ret, 0);
148 
149     ret = waitpid(childPid, &status, 0);
150     ASSERT_EQ(ret, childPid);
151     ret = WIFEXITED(status);
152     ASSERT_NE(ret, 0);
153     ret = WEXITSTATUS(status);
154     ASSERT_EQ(ret, 0);
155 }
156