• 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_CMD_LEN = 20;
34 const int TEST_PARENT_SLEEP_TIME = 2;
35 const int TEST_CHILD_SLEEP_TIME  = 5;
ChildShell(void * p)36 static int ChildShell(void *p)
37 {
38     pid_t pid = (pid_t)(uintptr_t)p;
39     char param[TEST_CMD_LEN] = {0};
40     int ret = snprintf_s(param, TEST_CMD_LEN, TEST_CMD_LEN - 1, "-p %d", pid);
41     if (ret < 0) {
42         return errno;
43     }
44 
45     printf("\n###################### Pid %d thread info ####################\n", pid);
46     ret = execl("/bin/shell", "shell", "task", param, (char *)0);
47     if (ret < 0) {
48         return errno;
49     }
50     return 0;
51 }
52 
PthreadFunc(void * arg)53 static void *PthreadFunc(void *arg)
54 {
55     (void)arg;
56     sleep(TEST_CHILD_SLEEP_TIME);
57     return NULL;
58 }
59 
ChildFun(void * p)60 static int ChildFun(void *p)
61 {
62     (void)p;
63     int ret;
64 
65     pthread_t pthread[TEST_COUNT] = {0};
66     for (int count = 0; count < TEST_COUNT; count++) {
67         pthread_create(&pthread[count], NULL, PthreadFunc, NULL);
68     }
69 
70     pid_t pid1 = fork();
71     if (pid1 == 0) {
72         ret = ChildShell(reinterpret_cast<void *>(getppid()));
73         if (ret != 0) {
74             exit(EXIT_CODE_ERRNO_1);
75         }
76     }
77 
78     sleep(TEST_CHILD_SLEEP_TIME);
79 
80     ret = waitpid(pid1, NULL, 0);
81     if (ret != pid1) {
82         exit(EXIT_CODE_ERRNO_2);
83     }
84 
85     return 0;
86 }
87 
ItPidContainer019(void)88 void ItPidContainer019(void)
89 {
90     int status;
91     int ret;
92     pid_t pid;
93     void *pstk = malloc(STACK_SIZE);
94     ASSERT_TRUE(pstk != NULL);
95     int childPid = clone(ChildFun, (char *)pstk + STACK_SIZE, CLONE_NEWPID | SIGCHLD, NULL);
96     free(pstk);
97     ASSERT_NE(childPid, -1);
98 
99     sleep(TEST_PARENT_SLEEP_TIME);
100 
101     pid = fork();
102     if (pid == 0) {
103         ret = ChildShell((void *)childPid);
104         ASSERT_EQ(ret, 0);
105     }
106 
107     ret = waitpid(pid, &status, 0);
108     ASSERT_EQ(ret, pid);
109     ret = WIFEXITED(status);
110     ASSERT_NE(ret, 0);
111     ret = WEXITSTATUS(status);
112     ASSERT_EQ(ret, 0);
113 
114     ret = waitpid(childPid, &status, 0);
115     ASSERT_EQ(ret, childPid);
116     ret = WIFEXITED(status);
117     ASSERT_NE(ret, 0);
118     ret = WEXITSTATUS(status);
119     ASSERT_EQ(ret, 0);
120 }
121