1 /*
2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <csignal>
20 #include <cstring>
21 #include <vector>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <gtest/gtest.h>
25 #include <sys/epoll.h>
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28 #include <sys/syscall.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <csignal>
32 #include "securec.h"
33
34 using namespace testing::ext;
35
36 static const int EXIT_VALUE = 123;
37
38 class HatsWaitidTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp();
43 void TearDown();
44 private:
45 };
SetUp()46 void HatsWaitidTest::SetUp()
47 {
48 }
49
TearDown()50 void HatsWaitidTest::TearDown()
51 {
52 }
53
SetUpTestCase()54 void HatsWaitidTest::SetUpTestCase()
55 {
56 }
57
TearDownTestCase()58 void HatsWaitidTest::TearDownTestCase()
59 {
60 }
61
62 /*
63 * @tc.number : SUB_KERNEL_SYSCALL_WAITID_0100
64 * @tc.name : WaitidAllChildProcessSuccess_0001
65 * @tc.desc : Waitid wait all child process and WEXITED success.
66 * @tc.size : MediumTest
67 * @tc.type : Function
68 * @tc.level : Level 2
69 */
70 HWTEST_F(HatsWaitidTest, WaitidAllChildProcessSuccess_0001, Function | MediumTest | Level2)
71 {
72 siginfo_t info;
73
74 pid_t pid = fork();
75 if (pid == 0) {
76 exit(EXIT_VALUE);
77 }
78
79 int ret = waitid(P_ALL, 0, &info, WEXITED);
80 EXPECT_EQ(ret, 0);
81 EXPECT_EQ(info.si_pid, pid);
82 EXPECT_EQ(info.si_status, EXIT_VALUE);
83 EXPECT_EQ(info.si_signo, SIGCHLD);
84 EXPECT_EQ(info.si_code, CLD_EXITED);
85 }
86
87 /*
88 * @tc.number : SUB_KERNEL_SYSCALL_WAITID_0200
89 * @tc.name : WaitidGroupChildProcessFailed_0002
90 * @tc.desc : waitid group child process and WEXITED failed.
91 * @tc.size : MediumTest
92 * @tc.type : Function
93 * @tc.level : Level 2
94 */
95 HWTEST_F(HatsWaitidTest, WaitidGroupChildProcessFailed_0002, Function | MediumTest | Level2)
96 {
97 errno = 0;
98 siginfo_t info;
99 pid_t pidGroup;
100
101 pidGroup = getpgid(0);
102
103 int ret = waitid(P_PGID, pidGroup + 1, &info, WEXITED);
104 EXPECT_EQ(ret, -1);
105 EXPECT_EQ(errno, ECHILD);
106 }
107
108 /*
109 * @tc.number : SUB_KERNEL_SYSCALL_WAITID_0300
110 * @tc.name : WaitidGroupChildProcessSuccess_0003
111 * @tc.desc : waitid group child process and WEXITED success.
112 * @tc.size : MediumTest
113 * @tc.type : Function
114 * @tc.level : Level 2
115 */
116 HWTEST_F(HatsWaitidTest, WaitidGroupChildProcessSuccess_0003, Function | MediumTest | Level2)
117 {
118 siginfo_t info;
119 pid_t pid;
120 pid_t pidChild;
121 pid_t pidGroup;
122
123 pid = fork();
124 if (pid == 0) {
125 pidChild = getpid();
126 exit(0);
127 }
128
129 pidGroup = getpgid(0);
130
131 int ret = waitid(P_PGID, pidGroup, &info, WEXITED);
132 EXPECT_EQ(ret, 0);
133 EXPECT_EQ(info.si_pid, pid);
134 EXPECT_EQ(info.si_status, 0);
135 EXPECT_EQ(info.si_signo, SIGCHLD);
136 EXPECT_EQ(info.si_code, CLD_EXITED);
137 }
138
139 /*
140 * @tc.number : SUB_KERNEL_SYSCALL_WAITID_0400
141 * @tc.name : WaitidChildProcessSuccess_0004
142 * @tc.desc : waitid child process and WEXITED success.
143 * @tc.size : MediumTest
144 * @tc.type : Function
145 * @tc.level : Level 2
146 */
147 HWTEST_F(HatsWaitidTest, WaitidChildProcessSuccess_0004, Function | MediumTest | Level2)
148 {
149 siginfo_t info;
150
151 pid_t pid = fork();
152 if (pid == 0) {
153 exit(0);
154 }
155
156 int ret = waitid(P_PID, pid, &info, WEXITED);
157 EXPECT_EQ(ret, 0);
158 EXPECT_EQ(info.si_pid, pid);
159 EXPECT_EQ(info.si_status, 0);
160 EXPECT_EQ(info.si_signo, SIGCHLD);
161 EXPECT_EQ(info.si_code, CLD_EXITED);
162 }
163
164 /*
165 * @tc.number : SUB_KERNEL_SYSCALL_WAITID_0500
166 * @tc.name : WaitidChildProcessSuccess_0005
167 * @tc.desc : waitid child process and WSTOPPED success.
168 * @tc.size : MediumTest
169 * @tc.type : Function
170 * @tc.level : Level 2
171 */
172 HWTEST_F(HatsWaitidTest, WaitidChildProcessSuccess_0005, Function | MediumTest | Level2)
173 {
174 siginfo_t info;
175
176 pid_t pid = fork();
177 if (pid == 0) {
178 kill(getpid(), SIGSTOP);
179 wait(nullptr);
180 return;
181 }
182
183 int ret = waitid(P_PID, pid, &info, WSTOPPED | WNOWAIT);
184 EXPECT_EQ(ret, 0);
185 EXPECT_EQ(info.si_pid, pid);
186 EXPECT_EQ(info.si_status, SIGSTOP);
187 EXPECT_EQ(info.si_signo, SIGCHLD);
188 EXPECT_EQ(info.si_code, CLD_STOPPED);
189
190 kill(pid, SIGCONT);
191
192 memset_s(&info, sizeof(info), 0, sizeof(info));
193
194 ret = waitid(P_PID, pid, &info, WCONTINUED);
195 EXPECT_EQ(ret, 0);
196 EXPECT_EQ(info.si_pid, pid);
197 EXPECT_EQ(info.si_status, SIGCONT);
198 EXPECT_EQ(info.si_signo, SIGCHLD);
199 EXPECT_EQ(info.si_code, CLD_CONTINUED);
200 kill(pid, SIGQUIT);
201 }
202
203 /*
204 * @tc.number : SUB_KERNEL_SYSCALL_WAITID_0600
205 * @tc.name : WaitidChildProcessFailed_0006
206 * @tc.desc : waitid child process and WNOHANG failed.
207 * @tc.size : MediumTest
208 * @tc.type : Function
209 * @tc.level : Level 2
210 */
211 HWTEST_F(HatsWaitidTest, WaitidChildProcessFailed_0006, Function | MediumTest | Level2)
212 {
213 siginfo_t info;
214 errno = 0;
215 int ret = waitid(P_ALL, 0, &info, WNOHANG);
216 EXPECT_EQ(ret, -1);
217 EXPECT_EQ(errno, EINVAL);
218 }
219
220