xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_wait.c (revision 3fc36ee018bb836bd1796067cf4ef8683f166ebc)
1 /* $NetBSD: t_wait.c,v 1.4 2016/04/27 21:14:24 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2016 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_wait.c,v 1.4 2016/04/27 21:14:24 christos Exp $");
33 
34 #include <sys/wait.h>
35 #include <sys/resource.h>
36 
37 #include <stdio.h>
38 #include <errno.h>
39 #include <limits.h>
40 #include <pwd.h>
41 #include <signal.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 
45 #include <atf-c.h>
46 
47 #ifdef __FreeBSD__
48 #define	wrusage	__wrusage
49 #endif
50 
51 ATF_TC(wait6_invalid);
52 ATF_TC_HEAD(wait6_invalid, tc)
53 {
54 	atf_tc_set_md_var(tc, "descr",
55 	    "Test that wait6(2) returns EINVAL with 0 options");
56 }
57 
58 ATF_TC_BODY(wait6_invalid, tc)
59 {
60 	siginfo_t si;
61 	struct wrusage wru;
62 	int st;
63 	ATF_REQUIRE(wait6(P_ALL, 0, &st, 0, &wru, &si) == -1
64 	    && errno == EINVAL);
65 }
66 
67 ATF_TC(wait6_noproc);
68 ATF_TC_HEAD(wait6_noproc, tc)
69 {
70 	atf_tc_set_md_var(tc, "descr",
71 	    "Test that wait6(2) returns ECHILD with for no processes");
72 }
73 
74 ATF_TC_BODY(wait6_noproc, tc)
75 {
76 	siginfo_t si;
77 	struct wrusage wru;
78 	int st;
79 	ATF_REQUIRE(wait6(P_ALL, 0, &st, WEXITED, &wru, &si) == -1
80 	    && errno == ECHILD);
81 }
82 
83 ATF_TC(wait6_exited);
84 ATF_TC_HEAD(wait6_exited, tc)
85 {
86 	atf_tc_set_md_var(tc, "descr",
87 	    "Test that wait6(2) handled exiting process and code");
88 }
89 
90 ATF_TC_BODY(wait6_exited, tc)
91 {
92 	siginfo_t si;
93 	struct wrusage wru;
94 	int st;
95 	pid_t pid;
96 
97 	switch (pid = fork()) {
98 	case -1:
99 		ATF_REQUIRE(pid > 0);
100 	case 0:
101 		exit(0x5a5a5a5a);
102 		/*NOTREACHED*/
103 	default:
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);
110 #ifdef __NetBSD__
111 		printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
112 		    (uintmax_t)si.si_utime);
113 #endif
114 		break;
115 	}
116 }
117 
118 ATF_TC(wait6_terminated);
119 ATF_TC_HEAD(wait6_terminated, tc)
120 {
121 	atf_tc_set_md_var(tc, "descr",
122 	    "Test that wait6(2) handled terminated process and code");
123 }
124 
125 ATF_TC_BODY(wait6_terminated, tc)
126 {
127 	siginfo_t si;
128 	struct wrusage wru;
129 	int st;
130 	pid_t pid;
131 
132 	switch (pid = fork()) {
133 	case 0:
134 		sleep(100);
135 		/*FALLTHROUGH*/
136 	case -1:
137 		ATF_REQUIRE(pid > 0);
138 	default:
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);
146 #ifdef __NetBSD__
147 		printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
148 		    (uintmax_t)si.si_utime);
149 #endif
150 		break;
151 	}
152 }
153 
154 ATF_TC(wait6_coredumped);
155 ATF_TC_HEAD(wait6_coredumped, tc)
156 {
157 	atf_tc_set_md_var(tc, "descr",
158 	    "Test that wait6(2) handled coredumped process and code");
159 }
160 
161 ATF_TC_BODY(wait6_coredumped, tc)
162 {
163 	siginfo_t si;
164 	struct wrusage wru;
165 	int st;
166 	pid_t pid;
167 	static const struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
168 
169 	switch (pid = fork()) {
170 	case 0:
171 		ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
172 		*(char *)8 = 0;
173 		/*FALLTHROUGH*/
174 	case -1:
175 		ATF_REQUIRE(pid > 0);
176 	default:
177 		ATF_REQUIRE(wait6(P_PID, pid, &st, WEXITED, &wru, &si) == pid);
178 		ATF_REQUIRE(WIFSIGNALED(st) && WTERMSIG(st) == SIGSEGV
179 		    && WCOREDUMP(st));
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);
184 #ifdef __NetBSD__
185 		printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
186 		    (uintmax_t)si.si_utime);
187 #endif
188 		break;
189 	}
190 }
191 
192 ATF_TC(wait6_stop_and_go);
193 ATF_TC_HEAD(wait6_stop_and_go, tc)
194 {
195 	atf_tc_set_md_var(tc, "descr",
196 	    "Test that wait6(2) handled stopped/continued process and code");
197 }
198 
199 ATF_TC_BODY(wait6_stop_and_go, tc)
200 {
201 	siginfo_t si;
202 	struct wrusage wru;
203 	int st;
204 	pid_t pid;
205 	static const struct rlimit rl = { 0, 0 };
206 
207 	ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
208 	switch (pid = fork()) {
209 	case 0:
210 		sleep(100);
211 		/*FALLTHROUGH*/
212 	case -1:
213 		ATF_REQUIRE(pid > 0);
214 	default:
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);
222 #ifdef __NetBSD__
223 		printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
224 		    (uintmax_t)si.si_utime);
225 #endif
226 
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);
234 #ifdef __NetBSD__
235 		printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
236 		    (uintmax_t)si.si_utime);
237 #endif
238 
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);
246 #ifdef __NetBSD__
247 		printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
248 		    (uintmax_t)si.si_utime);
249 #endif
250 		break;
251 	}
252 }
253 
254 ATF_TP_ADD_TCS(tp)
255 {
256 
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);
263 
264 	return atf_no_error();
265 }
266