1 /* $NetBSD: t_wait.c,v 1.4 2016/04/27 21:14:24 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.4 2016/04/27 21:14:24 christos Exp $");
35 #include <sys/resource.h>
48 #define wrusage __wrusage
51 ATF_TC(wait6_invalid);
52 ATF_TC_HEAD(wait6_invalid, tc)
54 atf_tc_set_md_var(tc, "descr",
55 "Test that wait6(2) returns EINVAL with 0 options");
58 ATF_TC_BODY(wait6_invalid, tc)
63 ATF_REQUIRE(wait6(P_ALL, 0, &st, 0, &wru, &si) == -1
68 ATF_TC_HEAD(wait6_noproc, tc)
70 atf_tc_set_md_var(tc, "descr",
71 "Test that wait6(2) returns ECHILD with for no processes");
74 ATF_TC_BODY(wait6_noproc, tc)
79 ATF_REQUIRE(wait6(P_ALL, 0, &st, WEXITED, &wru, &si) == -1
84 ATF_TC_HEAD(wait6_exited, tc)
86 atf_tc_set_md_var(tc, "descr",
87 "Test that wait6(2) handled exiting process and code");
90 ATF_TC_BODY(wait6_exited, tc)
97 switch (pid = fork()) {
104 ATF_REQUIRE(wait6(P_PID, pid, &st, WEXITED, &wru, &si) == pid);
105 ATF_REQUIRE(WIFEXITED(st) && WEXITSTATUS(st) == 0x5a);
106 ATF_REQUIRE(si.si_status = 0x5a5a5a5a);
107 ATF_REQUIRE(si.si_pid == pid);
108 ATF_REQUIRE(si.si_uid == getuid());
109 ATF_REQUIRE(si.si_code == CLD_EXITED);
111 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
112 (uintmax_t)si.si_utime);
118 ATF_TC(wait6_terminated);
119 ATF_TC_HEAD(wait6_terminated, tc)
121 atf_tc_set_md_var(tc, "descr",
122 "Test that wait6(2) handled terminated process and code");
125 ATF_TC_BODY(wait6_terminated, tc)
132 switch (pid = fork()) {
137 ATF_REQUIRE(pid > 0);
139 ATF_REQUIRE(kill(pid, SIGTERM) == 0);
140 ATF_REQUIRE(wait6(P_PID, pid, &st, WEXITED, &wru, &si) == pid);
141 ATF_REQUIRE(WIFSIGNALED(st) && WTERMSIG(st) == SIGTERM);
142 ATF_REQUIRE(si.si_status == SIGTERM);
143 ATF_REQUIRE(si.si_pid == pid);
144 ATF_REQUIRE(si.si_uid == getuid());
145 ATF_REQUIRE(si.si_code == CLD_KILLED);
147 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
148 (uintmax_t)si.si_utime);
154 ATF_TC(wait6_coredumped);
155 ATF_TC_HEAD(wait6_coredumped, tc)
157 atf_tc_set_md_var(tc, "descr",
158 "Test that wait6(2) handled coredumped process and code");
161 ATF_TC_BODY(wait6_coredumped, tc)
167 static const struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
169 switch (pid = fork()) {
171 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
175 ATF_REQUIRE(pid > 0);
177 ATF_REQUIRE(wait6(P_PID, pid, &st, WEXITED, &wru, &si) == pid);
178 ATF_REQUIRE(WIFSIGNALED(st) && WTERMSIG(st) == SIGSEGV
180 ATF_REQUIRE(si.si_status == SIGSEGV);
181 ATF_REQUIRE(si.si_pid == pid);
182 ATF_REQUIRE(si.si_uid == getuid());
183 ATF_REQUIRE(si.si_code == CLD_DUMPED);
185 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
186 (uintmax_t)si.si_utime);
192 ATF_TC(wait6_stop_and_go);
193 ATF_TC_HEAD(wait6_stop_and_go, tc)
195 atf_tc_set_md_var(tc, "descr",
196 "Test that wait6(2) handled stopped/continued process and code");
199 ATF_TC_BODY(wait6_stop_and_go, tc)
205 static const struct rlimit rl = { 0, 0 };
207 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
208 switch (pid = fork()) {
213 ATF_REQUIRE(pid > 0);
215 ATF_REQUIRE(kill(pid, SIGSTOP) == 0);
216 ATF_REQUIRE(wait6(P_PID, pid, &st, WSTOPPED, &wru, &si) == pid);
217 ATF_REQUIRE(WIFSTOPPED(st) && WSTOPSIG(st) == SIGSTOP);
218 ATF_REQUIRE(si.si_status == SIGSTOP);
219 ATF_REQUIRE(si.si_pid == pid);
220 ATF_REQUIRE(si.si_uid == getuid());
221 ATF_REQUIRE(si.si_code == CLD_STOPPED);
223 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
224 (uintmax_t)si.si_utime);
227 ATF_REQUIRE(kill(pid, SIGCONT) == 0);
228 ATF_REQUIRE(wait6(P_PID, pid, &st, WCONTINUED, &wru, &si) == pid);
229 ATF_REQUIRE(WIFCONTINUED(st));
230 ATF_REQUIRE(si.si_status == SIGCONT);
231 ATF_REQUIRE(si.si_pid == pid);
232 ATF_REQUIRE(si.si_uid == getuid());
233 ATF_REQUIRE(si.si_code == CLD_CONTINUED);
235 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
236 (uintmax_t)si.si_utime);
239 ATF_REQUIRE(kill(pid, SIGQUIT) == 0);
240 ATF_REQUIRE(wait6(P_PID, pid, &st, WEXITED, &wru, &si) == pid);
241 ATF_REQUIRE(WIFSIGNALED(st) && WTERMSIG(st) == SIGQUIT);
242 ATF_REQUIRE(si.si_status == SIGQUIT);
243 ATF_REQUIRE(si.si_pid == pid);
244 ATF_REQUIRE(si.si_uid == getuid());
245 ATF_REQUIRE(si.si_code == CLD_KILLED);
247 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
248 (uintmax_t)si.si_utime);
257 ATF_TP_ADD_TC(tp, wait6_invalid);
258 ATF_TP_ADD_TC(tp, wait6_noproc);
259 ATF_TP_ADD_TC(tp, wait6_exited);
260 ATF_TP_ADD_TC(tp, wait6_terminated);
261 ATF_TP_ADD_TC(tp, wait6_coredumped);
262 ATF_TP_ADD_TC(tp, wait6_stop_and_go);
264 return atf_no_error();