t_wait_noproc.c revision 1.5.2.2 1 1.5.2.2 pgoyette /* $NetBSD: t_wait_noproc.c,v 1.5.2.2 2017/01/07 08:56:55 pgoyette Exp $ */
2 1.5.2.2 pgoyette
3 1.5.2.2 pgoyette /*-
4 1.5.2.2 pgoyette * Copyright (c) 2016 The NetBSD Foundation, Inc.
5 1.5.2.2 pgoyette * All rights reserved.
6 1.5.2.2 pgoyette *
7 1.5.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.5.2.2 pgoyette * modification, are permitted provided that the following conditions
9 1.5.2.2 pgoyette * are met:
10 1.5.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.5.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.5.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.5.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.5.2.2 pgoyette * documentation and/or other materials provided with the distribution.
15 1.5.2.2 pgoyette *
16 1.5.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.5.2.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.5.2.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.5.2.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.5.2.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.5.2.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.5.2.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.5.2.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.5.2.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.5.2.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.5.2.2 pgoyette * POSSIBILITY OF SUCH DAMAGE.
27 1.5.2.2 pgoyette */
28 1.5.2.2 pgoyette
29 1.5.2.2 pgoyette #include <sys/cdefs.h>
30 1.5.2.2 pgoyette __RCSID("$NetBSD: t_wait_noproc.c,v 1.5.2.2 2017/01/07 08:56:55 pgoyette Exp $");
31 1.5.2.2 pgoyette
32 1.5.2.2 pgoyette #include <sys/wait.h>
33 1.5.2.2 pgoyette #include <sys/resource.h>
34 1.5.2.2 pgoyette
35 1.5.2.2 pgoyette #include <errno.h>
36 1.5.2.2 pgoyette #include <stdio.h>
37 1.5.2.2 pgoyette
38 1.5.2.2 pgoyette #include <atf-c.h>
39 1.5.2.2 pgoyette
40 1.5.2.2 pgoyette #ifndef TWAIT_OPTION
41 1.5.2.2 pgoyette #define TWAIT_OPTION 0
42 1.5.2.2 pgoyette #endif
43 1.5.2.2 pgoyette
44 1.5.2.2 pgoyette #if TWAIT_OPTION == 0
45 1.5.2.2 pgoyette ATF_TC(wait);
46 1.5.2.2 pgoyette ATF_TC_HEAD(wait, tc)
47 1.5.2.2 pgoyette {
48 1.5.2.2 pgoyette atf_tc_set_md_var(tc, "descr",
49 1.5.2.2 pgoyette "Test that wait(2) returns ECHILD for no child");
50 1.5.2.2 pgoyette }
51 1.5.2.2 pgoyette
52 1.5.2.2 pgoyette ATF_TC_BODY(wait, tc)
53 1.5.2.2 pgoyette {
54 1.5.2.2 pgoyette ATF_REQUIRE_ERRNO(ECHILD, wait(NULL) == -1);
55 1.5.2.2 pgoyette }
56 1.5.2.2 pgoyette #endif
57 1.5.2.2 pgoyette
58 1.5.2.2 pgoyette ATF_TC(waitpid);
59 1.5.2.2 pgoyette ATF_TC_HEAD(waitpid, tc)
60 1.5.2.2 pgoyette {
61 1.5.2.2 pgoyette atf_tc_set_md_var(tc, "descr",
62 1.5.2.2 pgoyette "Test that waitpid(2) returns ECHILD for WAIT_ANY and option %s",
63 1.5.2.2 pgoyette ___STRING(TWAIT_OPTION));
64 1.5.2.2 pgoyette }
65 1.5.2.2 pgoyette
66 1.5.2.2 pgoyette ATF_TC_BODY(waitpid, tc)
67 1.5.2.2 pgoyette {
68 1.5.2.2 pgoyette ATF_REQUIRE_ERRNO(ECHILD, waitpid(WAIT_ANY, NULL, TWAIT_OPTION) == -1);
69 1.5.2.2 pgoyette }
70 1.5.2.2 pgoyette
71 1.5.2.2 pgoyette ATF_TC(waitid);
72 1.5.2.2 pgoyette ATF_TC_HEAD(waitid, tc)
73 1.5.2.2 pgoyette {
74 1.5.2.2 pgoyette atf_tc_set_md_var(tc, "descr",
75 1.5.2.2 pgoyette "Test that waitid(2) returns ECHILD for P_ALL and option %s",
76 1.5.2.2 pgoyette ___STRING(TWAIT_OPTION));
77 1.5.2.2 pgoyette }
78 1.5.2.2 pgoyette
79 1.5.2.2 pgoyette ATF_TC_BODY(waitid, tc)
80 1.5.2.2 pgoyette {
81 1.5.2.2 pgoyette ATF_REQUIRE_ERRNO(ECHILD,
82 1.5.2.2 pgoyette waitid(P_ALL, 0, NULL,
83 1.5.2.2 pgoyette WTRAPPED | WEXITED | TWAIT_OPTION) == -1);
84 1.5.2.2 pgoyette }
85 1.5.2.2 pgoyette
86 1.5.2.2 pgoyette ATF_TC(wait3);
87 1.5.2.2 pgoyette ATF_TC_HEAD(wait3, tc)
88 1.5.2.2 pgoyette {
89 1.5.2.2 pgoyette atf_tc_set_md_var(tc, "descr",
90 1.5.2.2 pgoyette "Test that wait3(2) returns ECHILD for no child");
91 1.5.2.2 pgoyette }
92 1.5.2.2 pgoyette
93 1.5.2.2 pgoyette ATF_TC_BODY(wait3, tc)
94 1.5.2.2 pgoyette {
95 1.5.2.2 pgoyette ATF_REQUIRE_ERRNO(ECHILD, wait3(NULL, TWAIT_OPTION, NULL) == -1);
96 1.5.2.2 pgoyette }
97 1.5.2.2 pgoyette
98 1.5.2.2 pgoyette ATF_TC(wait4);
99 1.5.2.2 pgoyette ATF_TC_HEAD(wait4, tc)
100 1.5.2.2 pgoyette {
101 1.5.2.2 pgoyette atf_tc_set_md_var(tc, "descr",
102 1.5.2.2 pgoyette "Test that wait4(2) returns ECHILD for WAIT_ANY and option %s",
103 1.5.2.2 pgoyette ___STRING(TWAIT_OPTION));
104 1.5.2.2 pgoyette }
105 1.5.2.2 pgoyette
106 1.5.2.2 pgoyette ATF_TC_BODY(wait4, tc)
107 1.5.2.2 pgoyette {
108 1.5.2.2 pgoyette ATF_REQUIRE_ERRNO(ECHILD,
109 1.5.2.2 pgoyette wait4(WAIT_ANY, NULL, TWAIT_OPTION, NULL) == -1);
110 1.5.2.2 pgoyette }
111 1.5.2.2 pgoyette
112 1.5.2.2 pgoyette ATF_TC(wait6);
113 1.5.2.2 pgoyette ATF_TC_HEAD(wait6, tc)
114 1.5.2.2 pgoyette {
115 1.5.2.2 pgoyette atf_tc_set_md_var(tc, "descr",
116 1.5.2.2 pgoyette "Test that wait6(2) returns ECHILD for P_ALL and option %s",
117 1.5.2.2 pgoyette ___STRING(TWAIT_OPTION));
118 1.5.2.2 pgoyette }
119 1.5.2.2 pgoyette
120 1.5.2.2 pgoyette ATF_TC_BODY(wait6, tc)
121 1.5.2.2 pgoyette {
122 1.5.2.2 pgoyette ATF_REQUIRE_ERRNO(ECHILD,
123 1.5.2.2 pgoyette wait6(P_ALL, 0, NULL,
124 1.5.2.2 pgoyette WTRAPPED | WEXITED | TWAIT_OPTION, NULL, NULL) == -1);
125 1.5.2.2 pgoyette }
126 1.5.2.2 pgoyette
127 1.5.2.2 pgoyette /*
128 1.5.2.2 pgoyette * Generator of valid combinations of options
129 1.5.2.2 pgoyette * Usage: i = 0; while ((o = get_options_wait6(i++)) != -1) {}
130 1.5.2.2 pgoyette */
131 1.5.2.2 pgoyette static int
132 1.5.2.2 pgoyette get_options6(size_t pos)
133 1.5.2.2 pgoyette {
134 1.5.2.2 pgoyette int rv = 0;
135 1.5.2.2 pgoyette size_t n;
136 1.5.2.2 pgoyette
137 1.5.2.2 pgoyette /*
138 1.5.2.2 pgoyette * waitid(2) must specify at least one of WEXITED, WUNTRACED,
139 1.5.2.2 pgoyette * WSTOPPED, WTRAPPED or WCONTINUED. Single option WNOWAIT
140 1.5.2.2 pgoyette * isn't valid.
141 1.5.2.2 pgoyette */
142 1.5.2.2 pgoyette
143 1.5.2.2 pgoyette const int matrix[] = {
144 1.5.2.2 pgoyette WNOWAIT, /* First in order to blacklist it easily */
145 1.5.2.2 pgoyette WEXITED,
146 1.5.2.2 pgoyette WUNTRACED,
147 1.5.2.2 pgoyette WSTOPPED, /* SUS compatibility, equal to WUNTRACED */
148 1.5.2.2 pgoyette WTRAPPED,
149 1.5.2.2 pgoyette WCONTINUED
150 1.5.2.2 pgoyette };
151 1.5.2.2 pgoyette
152 1.5.2.2 pgoyette const size_t M = (1 << __arraycount(matrix)) - 1;
153 1.5.2.2 pgoyette
154 1.5.2.2 pgoyette /* Skip empty and sole WNOWAIT option */
155 1.5.2.2 pgoyette pos+=2;
156 1.5.2.2 pgoyette
157 1.5.2.2 pgoyette if (pos > M)
158 1.5.2.2 pgoyette return -1;
159 1.5.2.2 pgoyette
160 1.5.2.2 pgoyette for (n = 0; n < __arraycount(matrix); n++) {
161 1.5.2.2 pgoyette if (pos & __BIT(n))
162 1.5.2.2 pgoyette rv |= matrix[n];
163 1.5.2.2 pgoyette }
164 1.5.2.2 pgoyette
165 1.5.2.2 pgoyette return rv;
166 1.5.2.2 pgoyette }
167 1.5.2.2 pgoyette
168 1.5.2.2 pgoyette /*
169 1.5.2.2 pgoyette * Generator of valid combinations of options
170 1.5.2.2 pgoyette * Usage: i = 0; while ((o = get_options_wait4(i++)) != -1) {}
171 1.5.2.2 pgoyette */
172 1.5.2.2 pgoyette static int
173 1.5.2.2 pgoyette get_options4(size_t pos)
174 1.5.2.2 pgoyette {
175 1.5.2.2 pgoyette int rv = 0;
176 1.5.2.2 pgoyette size_t n;
177 1.5.2.2 pgoyette
178 1.5.2.2 pgoyette const int special[] = {
179 1.5.2.2 pgoyette 0,
180 1.5.2.2 pgoyette WALLSIG,
181 1.5.2.2 pgoyette WALTSIG,
182 1.5.2.2 pgoyette __WALL, /* Linux compatibility, equal to WALLSIG */
183 1.5.2.2 pgoyette __WCLONE /* Linux compatibility, equal to WALTSIG */
184 1.5.2.2 pgoyette };
185 1.5.2.2 pgoyette
186 1.5.2.2 pgoyette const int matrix[] = {
187 1.5.2.2 pgoyette WNOWAIT,
188 1.5.2.2 pgoyette WEXITED,
189 1.5.2.2 pgoyette WUNTRACED,
190 1.5.2.2 pgoyette WSTOPPED, /* SUS compatibility, equal to WUNTRACED */
191 1.5.2.2 pgoyette WTRAPPED,
192 1.5.2.2 pgoyette WCONTINUED
193 1.5.2.2 pgoyette };
194 1.5.2.2 pgoyette
195 1.5.2.2 pgoyette const size_t M = (1 << __arraycount(special)) - 1;
196 1.5.2.2 pgoyette
197 1.5.2.2 pgoyette if (pos < __arraycount(special))
198 1.5.2.2 pgoyette return special[pos];
199 1.5.2.2 pgoyette
200 1.5.2.2 pgoyette pos -= __arraycount(special);
201 1.5.2.2 pgoyette
202 1.5.2.2 pgoyette ++pos; /* Don't start with empty mask */
203 1.5.2.2 pgoyette
204 1.5.2.2 pgoyette if (pos > M)
205 1.5.2.2 pgoyette return -1;
206 1.5.2.2 pgoyette
207 1.5.2.2 pgoyette for (n = 0; n < __arraycount(special); n++) {
208 1.5.2.2 pgoyette if (pos & __BIT(n))
209 1.5.2.2 pgoyette rv |= matrix[n];
210 1.5.2.2 pgoyette }
211 1.5.2.2 pgoyette
212 1.5.2.2 pgoyette return rv;
213 1.5.2.2 pgoyette }
214 1.5.2.2 pgoyette
215 1.5.2.2 pgoyette ATF_TC(waitpid_options);
216 1.5.2.2 pgoyette ATF_TC_HEAD(waitpid_options, tc)
217 1.5.2.2 pgoyette {
218 1.5.2.2 pgoyette atf_tc_set_md_var(tc, "descr",
219 1.5.2.2 pgoyette "Test that waitpid(2) returns ECHILD for WAIT_ANY and valid "
220 1.5.2.2 pgoyette "combination of options with%s WNOHANG",
221 1.5.2.2 pgoyette TWAIT_OPTION == 0 ? "out" : "");
222 1.5.2.2 pgoyette }
223 1.5.2.2 pgoyette
224 1.5.2.2 pgoyette ATF_TC_BODY(waitpid_options, tc)
225 1.5.2.2 pgoyette {
226 1.5.2.2 pgoyette size_t i = 0;
227 1.5.2.2 pgoyette int o;
228 1.5.2.2 pgoyette
229 1.5.2.2 pgoyette while((o = get_options4(i++)) != -1) {
230 1.5.2.2 pgoyette printf("Testing waitpid(2) with options %x\n", o);
231 1.5.2.2 pgoyette
232 1.5.2.2 pgoyette ATF_REQUIRE_ERRNO(ECHILD,
233 1.5.2.2 pgoyette waitpid(WAIT_ANY, NULL, o | TWAIT_OPTION) == -1);
234 1.5.2.2 pgoyette }
235 1.5.2.2 pgoyette }
236 1.5.2.2 pgoyette
237 1.5.2.2 pgoyette ATF_TC(waitid_options);
238 1.5.2.2 pgoyette ATF_TC_HEAD(waitid_options, tc)
239 1.5.2.2 pgoyette {
240 1.5.2.2 pgoyette atf_tc_set_md_var(tc, "descr",
241 1.5.2.2 pgoyette "Test that waitid(2) returns ECHILD for P_ALL and valid "
242 1.5.2.2 pgoyette "combination of options with%s WNOHANG",
243 1.5.2.2 pgoyette TWAIT_OPTION == 0 ? "out" : "");
244 1.5.2.2 pgoyette }
245 1.5.2.2 pgoyette
246 1.5.2.2 pgoyette ATF_TC_BODY(waitid_options, tc)
247 1.5.2.2 pgoyette {
248 1.5.2.2 pgoyette size_t i = 0;
249 1.5.2.2 pgoyette int o;
250 1.5.2.2 pgoyette
251 1.5.2.2 pgoyette while((o = get_options6(i++)) != -1) {
252 1.5.2.2 pgoyette printf("Testing waitid(2) with options %x\n", o);
253 1.5.2.2 pgoyette
254 1.5.2.2 pgoyette ATF_REQUIRE_ERRNO(ECHILD,
255 1.5.2.2 pgoyette waitid(P_ALL, 0, NULL, o | TWAIT_OPTION) == -1);
256 1.5.2.2 pgoyette }
257 1.5.2.2 pgoyette }
258 1.5.2.2 pgoyette
259 1.5.2.2 pgoyette ATF_TC(wait3_options);
260 1.5.2.2 pgoyette ATF_TC_HEAD(wait3_options, tc)
261 1.5.2.2 pgoyette {
262 1.5.2.2 pgoyette atf_tc_set_md_var(tc, "descr",
263 1.5.2.2 pgoyette "Test that wait3(2) returns ECHILD for no child");
264 1.5.2.2 pgoyette }
265 1.5.2.2 pgoyette
266 1.5.2.2 pgoyette ATF_TC_BODY(wait3_options, tc)
267 1.5.2.2 pgoyette {
268 1.5.2.2 pgoyette size_t i = 0;
269 1.5.2.2 pgoyette int o;
270 1.5.2.2 pgoyette
271 1.5.2.2 pgoyette while((o = get_options4(i++)) != -1) {
272 1.5.2.2 pgoyette printf("Testing wait3(2) with options %x\n", o);
273 1.5.2.2 pgoyette
274 1.5.2.2 pgoyette ATF_REQUIRE_ERRNO(ECHILD,
275 1.5.2.2 pgoyette wait3(NULL, o | TWAIT_OPTION, NULL) == -1);
276 1.5.2.2 pgoyette }
277 1.5.2.2 pgoyette }
278 1.5.2.2 pgoyette
279 1.5.2.2 pgoyette ATF_TC(wait4_options);
280 1.5.2.2 pgoyette ATF_TC_HEAD(wait4_options, tc)
281 1.5.2.2 pgoyette {
282 1.5.2.2 pgoyette atf_tc_set_md_var(tc, "descr",
283 1.5.2.2 pgoyette "Test that wait4(2) returns ECHILD for WAIT_ANY and option %s",
284 1.5.2.2 pgoyette ___STRING(TWAIT_OPTION));
285 1.5.2.2 pgoyette }
286 1.5.2.2 pgoyette
287 1.5.2.2 pgoyette ATF_TC_BODY(wait4_options, tc)
288 1.5.2.2 pgoyette {
289 1.5.2.2 pgoyette size_t i = 0;
290 1.5.2.2 pgoyette int o;
291 1.5.2.2 pgoyette
292 1.5.2.2 pgoyette while((o = get_options4(i++)) != -1) {
293 1.5.2.2 pgoyette printf("Testing wait4(2) with options %x\n", o);
294 1.5.2.2 pgoyette
295 1.5.2.2 pgoyette ATF_REQUIRE_ERRNO(ECHILD,
296 1.5.2.2 pgoyette wait4(WAIT_ANY, NULL, o | TWAIT_OPTION, NULL) == -1);
297 1.5.2.2 pgoyette }
298 1.5.2.2 pgoyette }
299 1.5.2.2 pgoyette
300 1.5.2.2 pgoyette ATF_TC(wait6_options);
301 1.5.2.2 pgoyette ATF_TC_HEAD(wait6_options, tc)
302 1.5.2.2 pgoyette {
303 1.5.2.2 pgoyette atf_tc_set_md_var(tc, "descr",
304 1.5.2.2 pgoyette "Test that wait6(2) returns ECHILD for P_ALL and option %s",
305 1.5.2.2 pgoyette ___STRING(TWAIT_OPTION));
306 1.5.2.2 pgoyette }
307 1.5.2.2 pgoyette
308 1.5.2.2 pgoyette ATF_TC_BODY(wait6_options, tc)
309 1.5.2.2 pgoyette {
310 1.5.2.2 pgoyette size_t i = 0;
311 1.5.2.2 pgoyette int o;
312 1.5.2.2 pgoyette
313 1.5.2.2 pgoyette while((o = get_options6(i++)) != -1) {
314 1.5.2.2 pgoyette printf("Testing wait6(2) with options %x\n", o);
315 1.5.2.2 pgoyette
316 1.5.2.2 pgoyette ATF_REQUIRE_ERRNO(ECHILD,
317 1.5.2.2 pgoyette wait6(P_ALL, 0, NULL, o | TWAIT_OPTION, NULL, NULL) == -1);
318 1.5.2.2 pgoyette }
319 1.5.2.2 pgoyette }
320 1.5.2.2 pgoyette
321 1.5.2.2 pgoyette ATF_TP_ADD_TCS(tp)
322 1.5.2.2 pgoyette {
323 1.5.2.2 pgoyette
324 1.5.2.2 pgoyette #if TWAIT_OPTION == 0
325 1.5.2.2 pgoyette ATF_TP_ADD_TC(tp, wait);
326 1.5.2.2 pgoyette #endif
327 1.5.2.2 pgoyette ATF_TP_ADD_TC(tp, waitpid);
328 1.5.2.2 pgoyette ATF_TP_ADD_TC(tp, waitid);
329 1.5.2.2 pgoyette ATF_TP_ADD_TC(tp, wait3);
330 1.5.2.2 pgoyette ATF_TP_ADD_TC(tp, wait4);
331 1.5.2.2 pgoyette ATF_TP_ADD_TC(tp, wait6);
332 1.5.2.2 pgoyette
333 1.5.2.2 pgoyette ATF_TP_ADD_TC(tp, waitpid_options);
334 1.5.2.2 pgoyette ATF_TP_ADD_TC(tp, waitid_options);
335 1.5.2.2 pgoyette ATF_TP_ADD_TC(tp, wait3_options);
336 1.5.2.2 pgoyette ATF_TP_ADD_TC(tp, wait4_options);
337 1.5.2.2 pgoyette ATF_TP_ADD_TC(tp, wait6_options);
338 1.5.2.2 pgoyette
339 1.5.2.2 pgoyette return atf_no_error();
340 1.5.2.2 pgoyette }
341