1 /* $NetBSD: t_wait.c,v 1.8 2017/01/13 19:28:55 christos Exp $ */
4 * Copyright (c) 2016 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_wait.c,v 1.8 2017/01/13 19:28:55 christos Exp $");
35 #include <sys/resource.h>
48 ATF_TC(wait6_invalid);
49 ATF_TC_HEAD(wait6_invalid, tc)
51 atf_tc_set_md_var(tc, "descr",
52 "Test that wait6(2) returns EINVAL with 0 options");
55 ATF_TC_BODY(wait6_invalid, tc)
60 ATF_REQUIRE(wait6(P_ALL, 0, &st, 0, &wru, &si) == -1
65 ATF_TC_HEAD(wait6_exited, tc)
67 atf_tc_set_md_var(tc, "descr",
68 "Test that wait6(2) handled exiting process and code");
71 ATF_TC_BODY(wait6_exited, tc)
78 switch (pid = fork()) {
85 ATF_REQUIRE(wait6(P_PID, pid, &st, WEXITED, &wru, &si) == pid);
86 ATF_REQUIRE(WIFEXITED(st) && WEXITSTATUS(st) == 0x5a);
87 ATF_REQUIRE(si.si_status = 0x5a5a5a5a);
88 ATF_REQUIRE(si.si_pid == pid);
89 ATF_REQUIRE(si.si_uid == getuid());
90 ATF_REQUIRE(si.si_code == CLD_EXITED);
92 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
93 (uintmax_t)si.si_utime);
99 ATF_TC(wait6_terminated);
100 ATF_TC_HEAD(wait6_terminated, tc)
102 atf_tc_set_md_var(tc, "descr",
103 "Test that wait6(2) handled terminated process and code");
106 ATF_TC_BODY(wait6_terminated, tc)
113 switch (pid = fork()) {
118 ATF_REQUIRE(pid > 0);
120 ATF_REQUIRE(kill(pid, SIGTERM) == 0);
121 ATF_REQUIRE(wait6(P_PID, pid, &st, WEXITED, &wru, &si) == pid);
122 ATF_REQUIRE(WIFSIGNALED(st) && WTERMSIG(st) == SIGTERM);
123 ATF_REQUIRE(si.si_status == SIGTERM);
124 ATF_REQUIRE(si.si_pid == pid);
125 ATF_REQUIRE(si.si_uid == getuid());
126 ATF_REQUIRE(si.si_code == CLD_KILLED);
128 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
129 (uintmax_t)si.si_utime);
135 ATF_TC(wait6_coredumped);
136 ATF_TC_HEAD(wait6_coredumped, tc)
138 atf_tc_set_md_var(tc, "descr",
139 "Test that wait6(2) handled coredumped process and code");
142 ATF_TC_BODY(wait6_coredumped, tc)
148 static const struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
150 switch (pid = fork()) {
152 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
156 ATF_REQUIRE(pid > 0);
158 ATF_REQUIRE(wait6(P_PID, pid, &st, WEXITED, &wru, &si) == pid);
159 ATF_REQUIRE(WIFSIGNALED(st) && WTERMSIG(st) == SIGSEGV
161 ATF_REQUIRE(si.si_status == SIGSEGV);
162 ATF_REQUIRE(si.si_pid == pid);
163 ATF_REQUIRE(si.si_uid == getuid());
164 ATF_REQUIRE(si.si_code == CLD_DUMPED);
166 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
167 (uintmax_t)si.si_utime);
173 ATF_TC(wait6_stop_and_go);
174 ATF_TC_HEAD(wait6_stop_and_go, tc)
176 atf_tc_set_md_var(tc, "descr",
177 "Test that wait6(2) handled stopped/continued process and code");
180 ATF_TC_BODY(wait6_stop_and_go, tc)
186 static const struct rlimit rl = { 0, 0 };
188 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
189 switch (pid = fork()) {
194 ATF_REQUIRE(pid > 0);
196 ATF_REQUIRE(kill(pid, SIGSTOP) == 0);
197 ATF_REQUIRE(wait6(P_PID, pid, &st, WSTOPPED, &wru, &si) == pid);
198 ATF_REQUIRE(!WIFEXITED(st));
199 ATF_REQUIRE(!WIFSIGNALED(st));
200 ATF_REQUIRE(WIFSTOPPED(st) && WSTOPSIG(st) == SIGSTOP);
201 ATF_REQUIRE(!WIFCONTINUED(st));
202 ATF_REQUIRE(si.si_status == SIGSTOP);
203 ATF_REQUIRE(si.si_pid == pid);
204 ATF_REQUIRE(si.si_uid == getuid());
205 ATF_REQUIRE(si.si_code == CLD_STOPPED);
207 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
208 (uintmax_t)si.si_utime);
211 ATF_REQUIRE(kill(pid, SIGCONT) == 0);
212 ATF_REQUIRE(wait6(P_PID, pid, &st, WCONTINUED, &wru, &si) == pid);
213 ATF_REQUIRE(!WIFEXITED(st));
214 ATF_REQUIRE(!WIFSIGNALED(st));
215 ATF_REQUIRE(WIFCONTINUED(st));
216 ATF_REQUIRE(!WIFSTOPPED(st));
217 ATF_REQUIRE(si.si_status == SIGCONT);
218 ATF_REQUIRE(si.si_pid == pid);
219 ATF_REQUIRE(si.si_uid == getuid());
220 ATF_REQUIRE(si.si_code == CLD_CONTINUED);
222 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
223 (uintmax_t)si.si_utime);
226 ATF_REQUIRE(kill(pid, SIGQUIT) == 0);
227 ATF_REQUIRE(wait6(P_PID, pid, &st, WEXITED, &wru, &si) == pid);
228 ATF_REQUIRE(!WIFEXITED(st));
229 ATF_REQUIRE(WIFSIGNALED(st) && WTERMSIG(st) == SIGQUIT);
230 ATF_REQUIRE(!WIFSTOPPED(st));
231 ATF_REQUIRE(!WIFCONTINUED(st));
232 ATF_REQUIRE(si.si_status == SIGQUIT);
233 ATF_REQUIRE(si.si_pid == pid);
234 ATF_REQUIRE(si.si_uid == getuid());
235 ATF_REQUIRE(si.si_code == CLD_KILLED);
237 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
238 (uintmax_t)si.si_utime);
244 ATF_TC(wait6_stopgo_loop);
245 ATF_TC_HEAD(wait6_stopgo_loop, tc)
247 atf_tc_set_md_var(tc, "descr",
248 "Test that wait6(2) handled stopped/continued process loop");
251 ATF_TC_BODY(wait6_stopgo_loop, tc)
257 static const struct rlimit rl = { 0, 0 };
260 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
261 switch (pid = fork()) {
266 ATF_REQUIRE(pid > 0);
269 printf("Before loop of SIGSTOP/SIGCONT sequence %zu times\n", N);
271 ATF_REQUIRE(kill(pid, SIGSTOP) == 0);
272 ATF_REQUIRE(wait6(P_PID, pid, &st, WSTOPPED, &wru, &si) == pid);
273 ATF_REQUIRE(!WIFEXITED(st));
274 ATF_REQUIRE(!WIFSIGNALED(st));
275 ATF_REQUIRE(WIFSTOPPED(st) && WSTOPSIG(st) == SIGSTOP);
276 ATF_REQUIRE(!WIFCONTINUED(st));
277 ATF_REQUIRE(si.si_status == SIGSTOP);
278 ATF_REQUIRE(si.si_pid == pid);
279 ATF_REQUIRE(si.si_uid == getuid());
280 ATF_REQUIRE(si.si_code == CLD_STOPPED);
282 ATF_REQUIRE(kill(pid, SIGCONT) == 0);
283 ATF_REQUIRE(wait6(P_PID, pid, &st, WCONTINUED, &wru, &si) == pid);
284 ATF_REQUIRE(!WIFEXITED(st));
285 ATF_REQUIRE(!WIFSIGNALED(st));
286 ATF_REQUIRE(WIFCONTINUED(st));
287 ATF_REQUIRE(!WIFSTOPPED(st));
288 ATF_REQUIRE(si.si_status == SIGCONT);
289 ATF_REQUIRE(si.si_pid == pid);
290 ATF_REQUIRE(si.si_uid == getuid());
291 ATF_REQUIRE(si.si_code == CLD_CONTINUED);
293 ATF_REQUIRE(kill(pid, SIGQUIT) == 0);
294 ATF_REQUIRE(wait6(P_PID, pid, &st, WEXITED, &wru, &si) == pid);
295 ATF_REQUIRE(!WIFEXITED(st));
296 ATF_REQUIRE(WIFSIGNALED(st) && WTERMSIG(st) == SIGQUIT);
297 ATF_REQUIRE(!WIFSTOPPED(st));
298 ATF_REQUIRE(!WIFCONTINUED(st));
299 ATF_REQUIRE(si.si_status == SIGQUIT);
300 ATF_REQUIRE(si.si_pid == pid);
301 ATF_REQUIRE(si.si_uid == getuid());
302 ATF_REQUIRE(si.si_code == CLD_KILLED);
304 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
305 (uintmax_t)si.si_utime);
312 ATF_TP_ADD_TC(tp, wait6_invalid);
313 ATF_TP_ADD_TC(tp, wait6_exited);
314 ATF_TP_ADD_TC(tp, wait6_terminated);
315 ATF_TP_ADD_TC(tp, wait6_coredumped);
316 ATF_TP_ADD_TC(tp, wait6_stop_and_go);
317 ATF_TP_ADD_TC(tp, wait6_stopgo_loop);
319 return atf_no_error();