summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/sys
diff options
context:
space:
mode:
authorkettenis <>2022-10-26 23:18:02 +0000
committerkettenis <>2022-10-26 23:18:02 +0000
commit58f6589b4358779b62445cadddd2635a06f0c55f (patch)
tree3a2453dc674f007f8f091cb673ed69de57495068 /src/regress/lib/libc/sys
parentc37e55335c8b56e764d4a5cfc3bd92c4ba302ee5 (diff)
downloadopenbsd-58f6589b4358779b62445cadddd2635a06f0c55f.tar.gz
openbsd-58f6589b4358779b62445cadddd2635a06f0c55f.tar.bz2
openbsd-58f6589b4358779b62445cadddd2635a06f0c55f.zip
Enable waitid(2) regress tests and a new test derived from NetBSD's
wait6(2) tests. ok millert@, deraadt@
Diffstat (limited to 'src/regress/lib/libc/sys')
-rw-r--r--src/regress/lib/libc/sys/Makefile3
-rw-r--r--src/regress/lib/libc/sys/t_wait_noproc.c16
-rw-r--r--src/regress/lib/libc/sys/t_waitid.c272
3 files changed, 279 insertions, 12 deletions
diff --git a/src/regress/lib/libc/sys/Makefile b/src/regress/lib/libc/sys/Makefile
index e563279422..2fcba8ca91 100644
--- a/src/regress/lib/libc/sys/Makefile
+++ b/src/regress/lib/libc/sys/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.16 2022/09/11 20:51:44 mbuhl Exp $ 1# $OpenBSD: Makefile,v 1.17 2022/10/26 23:18:01 kettenis Exp $
2 2
3# Copyright (c) 2019 Moritz Buhl <openbsd@moritzbuhl.de> 3# Copyright (c) 2019 Moritz Buhl <openbsd@moritzbuhl.de>
4# Copyright (c) 2019 Alexander Bluhm <bluhm@openbsd.org> 4# Copyright (c) 2019 Alexander Bluhm <bluhm@openbsd.org>
@@ -69,6 +69,7 @@ PROGS += t_stat
69PROGS += t_truncate 69PROGS += t_truncate
70PROGS += t_umask t_unlink 70PROGS += t_umask t_unlink
71PROGS += t_vfork 71PROGS += t_vfork
72PROGS += t_waitid
72PROGS += t_wait_noproc 73PROGS += t_wait_noproc
73PROGS += t_wait_noproc_wnohang 74PROGS += t_wait_noproc_wnohang
74PROGS += t_write 75PROGS += t_write
diff --git a/src/regress/lib/libc/sys/t_wait_noproc.c b/src/regress/lib/libc/sys/t_wait_noproc.c
index ee3454eb70..e5137a3ea9 100644
--- a/src/regress/lib/libc/sys/t_wait_noproc.c
+++ b/src/regress/lib/libc/sys/t_wait_noproc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: t_wait_noproc.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $ */ 1/* $OpenBSD: t_wait_noproc.c,v 1.3 2022/10/26 23:18:01 kettenis Exp $ */
2/* $NetBSD: t_wait_noproc.c,v 1.5 2016/11/09 17:50:19 kamil Exp $ */ 2/* $NetBSD: t_wait_noproc.c,v 1.5 2016/11/09 17:50:19 kamil Exp $ */
3 3
4/*- 4/*-
@@ -68,7 +68,6 @@ ATF_TC_BODY(waitpid, tc)
68 ATF_REQUIRE_ERRNO(ECHILD, waitpid(WAIT_ANY, NULL, TWAIT_OPTION) == -1); 68 ATF_REQUIRE_ERRNO(ECHILD, waitpid(WAIT_ANY, NULL, TWAIT_OPTION) == -1);
69} 69}
70 70
71#ifndef __OpenBSD__
72ATF_TC(waitid); 71ATF_TC(waitid);
73ATF_TC_HEAD(waitid, tc) 72ATF_TC_HEAD(waitid, tc)
74{ 73{
@@ -81,9 +80,8 @@ ATF_TC_BODY(waitid, tc)
81{ 80{
82 ATF_REQUIRE_ERRNO(ECHILD, 81 ATF_REQUIRE_ERRNO(ECHILD,
83 waitid(P_ALL, 0, NULL, 82 waitid(P_ALL, 0, NULL,
84 WTRAPPED | WEXITED | TWAIT_OPTION) == -1); 83 WEXITED | TWAIT_OPTION) == -1);
85} 84}
86#endif
87 85
88ATF_TC(wait3); 86ATF_TC(wait3);
89ATF_TC_HEAD(wait3, tc) 87ATF_TC_HEAD(wait3, tc)
@@ -128,6 +126,7 @@ ATF_TC_BODY(wait4, tc)
128 * WTRAPPED | WEXITED | TWAIT_OPTION, NULL, NULL) == -1); 126 * WTRAPPED | WEXITED | TWAIT_OPTION, NULL, NULL) == -1);
129 * } 127 * }
130 */ 128 */
129#endif
131 130
132/* 131/*
133 * Generator of valid combinations of options 132 * Generator of valid combinations of options
@@ -148,7 +147,9 @@ get_options6(size_t pos)
148 WEXITED, 147 WEXITED,
149 WUNTRACED, 148 WUNTRACED,
150 WSTOPPED, // SUS compatibility, equal to WUNTRACED 149 WSTOPPED, // SUS compatibility, equal to WUNTRACED
150#ifndef __OpenBSD__
151 WTRAPPED, 151 WTRAPPED,
152#endif
152 WCONTINUED 153 WCONTINUED
153 }; 154 };
154 155
@@ -166,7 +167,6 @@ get_options6(size_t pos)
166 167
167 return rv; 168 return rv;
168} 169}
169#endif
170 170
171/* 171/*
172 * Generator of valid combinations of options 172 * Generator of valid combinations of options
@@ -243,7 +243,6 @@ ATF_TC_BODY(waitpid_options, tc)
243 } 243 }
244} 244}
245 245
246#ifndef __OpenBSD__
247ATF_TC(waitid_options); 246ATF_TC(waitid_options);
248ATF_TC_HEAD(waitid_options, tc) 247ATF_TC_HEAD(waitid_options, tc)
249{ 248{
@@ -265,7 +264,6 @@ ATF_TC_BODY(waitid_options, tc)
265 waitid(P_ALL, 0, NULL, o | TWAIT_OPTION) == -1); 264 waitid(P_ALL, 0, NULL, o | TWAIT_OPTION) == -1);
266 } 265 }
267} 266}
268#endif
269 267
270ATF_TC(wait3_options); 268ATF_TC(wait3_options);
271ATF_TC_HEAD(wait3_options, tc) 269ATF_TC_HEAD(wait3_options, tc)
@@ -338,9 +336,7 @@ ATF_TP_ADD_TCS(tp)
338 ATF_TP_ADD_TC(tp, wait); 336 ATF_TP_ADD_TC(tp, wait);
339#endif 337#endif
340 ATF_TP_ADD_TC(tp, waitpid); 338 ATF_TP_ADD_TC(tp, waitpid);
341#ifndef __OpenBSD__
342 ATF_TP_ADD_TC(tp, waitid); 339 ATF_TP_ADD_TC(tp, waitid);
343#endif
344 ATF_TP_ADD_TC(tp, wait3); 340 ATF_TP_ADD_TC(tp, wait3);
345 ATF_TP_ADD_TC(tp, wait4); 341 ATF_TP_ADD_TC(tp, wait4);
346#ifndef __OpenBSD__ 342#ifndef __OpenBSD__
@@ -348,9 +344,7 @@ ATF_TP_ADD_TCS(tp)
348#endif 344#endif
349 345
350 ATF_TP_ADD_TC(tp, waitpid_options); 346 ATF_TP_ADD_TC(tp, waitpid_options);
351#ifndef __OpenBSD__
352 ATF_TP_ADD_TC(tp, waitid_options); 347 ATF_TP_ADD_TC(tp, waitid_options);
353#endif
354 ATF_TP_ADD_TC(tp, wait3_options); 348 ATF_TP_ADD_TC(tp, wait3_options);
355 ATF_TP_ADD_TC(tp, wait4_options); 349 ATF_TP_ADD_TC(tp, wait4_options);
356#ifndef __OpenBSD__ 350#ifndef __OpenBSD__
diff --git a/src/regress/lib/libc/sys/t_waitid.c b/src/regress/lib/libc/sys/t_waitid.c
new file mode 100644
index 0000000000..ecb16fabad
--- /dev/null
+++ b/src/regress/lib/libc/sys/t_waitid.c
@@ -0,0 +1,272 @@
1/* $OpenBSD: t_waitid.c,v 1.1 2022/10/26 23:18:02 kettenis Exp $ */
2/* $NetBSD: t_wait.c,v 1.10 2021/07/17 14:03:35 martin Exp $ */
3
4/*-
5 * Copyright (c) 2016 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Christos Zoulas.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "macros.h"
34
35#include <sys/wait.h>
36#include <sys/resource.h>
37
38#include <errno.h>
39#include <inttypes.h>
40#include <limits.h>
41#include <pwd.h>
42#include <signal.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <unistd.h>
46
47#include "atf-c.h"
48
49ATF_TC(waitid_invalid);
50ATF_TC_HEAD(waitid_invalid, tc)
51{
52 atf_tc_set_md_var(tc, "descr",
53 "Test that waitid(2) returns EINVAL with 0 options");
54}
55
56ATF_TC_BODY(waitid_invalid, tc)
57{
58 siginfo_t si;
59 ATF_REQUIRE(waitid(P_ALL, 0, &si, 0) == -1
60 && errno == EINVAL);
61}
62
63ATF_TC(waitid_exited);
64ATF_TC_HEAD(waitid_exited, tc)
65{
66 atf_tc_set_md_var(tc, "descr",
67 "Test that waitid(2) handled exiting process and code");
68}
69
70ATF_TC_BODY(waitid_exited, tc)
71{
72 siginfo_t si;
73 pid_t pid;
74
75 switch (pid = fork()) {
76 case 0:
77 exit(0x5a5a5a5a);
78 /*NOTREACHED*/
79 case -1:
80 ATF_REQUIRE(pid > 0);
81 __unreachable();
82 default:
83 ATF_REQUIRE(waitid(P_PID, pid, &si, WEXITED) == 0);
84 ATF_REQUIRE(si.si_status == 0x5a5a5a5a);
85 ATF_REQUIRE(si.si_pid == pid);
86 ATF_REQUIRE(si.si_uid == getuid());
87 ATF_REQUIRE(si.si_code == CLD_EXITED);
88 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
89 (uintmax_t)si.si_utime);
90 break;
91 }
92}
93
94ATF_TC(waitid_terminated);
95ATF_TC_HEAD(waitid_terminated, tc)
96{
97 atf_tc_set_md_var(tc, "descr",
98 "Test that waitid(2) handled terminated process and code");
99}
100
101ATF_TC_BODY(waitid_terminated, tc)
102{
103 siginfo_t si;
104 pid_t pid;
105
106 switch (pid = fork()) {
107 case 0:
108 sleep(100);
109 /*FALLTHROUGH*/
110 case -1:
111 ATF_REQUIRE(pid > 0);
112 __unreachable();
113 default:
114 ATF_REQUIRE(kill(pid, SIGTERM) == 0);
115 ATF_REQUIRE(waitid(P_PID, pid, &si, WEXITED) == 0);
116 ATF_REQUIRE(si.si_status == SIGTERM);
117 ATF_REQUIRE(si.si_pid == pid);
118 ATF_REQUIRE(si.si_uid == getuid());
119 ATF_REQUIRE(si.si_code == CLD_KILLED);
120 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
121 (uintmax_t)si.si_utime);
122 break;
123 }
124}
125
126ATF_TC(waitid_coredumped);
127ATF_TC_HEAD(waitid_coredumped, tc)
128{
129 atf_tc_set_md_var(tc, "descr",
130 "Test that waitid(2) handled coredumped process and code");
131}
132
133ATF_TC_BODY(waitid_coredumped, tc)
134{
135 siginfo_t si;
136 pid_t pid;
137 static const struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
138
139 switch (pid = fork()) {
140 case 0:
141 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
142 *(char *)8 = 0;
143 /*FALLTHROUGH*/
144 case -1:
145 ATF_REQUIRE(pid > 0);
146 __unreachable();
147 default:
148 ATF_REQUIRE(waitid(P_PID, pid, &si, WEXITED) == 0);
149 ATF_REQUIRE(si.si_status == SIGSEGV);
150 ATF_REQUIRE(si.si_pid == pid);
151 ATF_REQUIRE(si.si_uid == getuid());
152 ATF_REQUIRE(si.si_code == CLD_DUMPED);
153 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
154 (uintmax_t)si.si_utime);
155 break;
156 }
157}
158
159ATF_TC(waitid_stop_and_go);
160ATF_TC_HEAD(waitid_stop_and_go, tc)
161{
162 atf_tc_set_md_var(tc, "descr",
163 "Test that waitid(2) handled stopped/continued process and code");
164}
165
166ATF_TC_BODY(waitid_stop_and_go, tc)
167{
168 siginfo_t si;
169 pid_t pid;
170 static const struct rlimit rl = { 0, 0 };
171
172 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
173 switch (pid = fork()) {
174 case 0:
175 sleep(100);
176 /*FALLTHROUGH*/
177 case -1:
178 ATF_REQUIRE(pid > 0);
179 __unreachable();
180 default:
181 ATF_REQUIRE(kill(pid, SIGSTOP) == 0);
182 ATF_REQUIRE(waitid(P_PID, pid, &si, WSTOPPED) == 0);
183 ATF_REQUIRE(si.si_status == SIGSTOP);
184 ATF_REQUIRE(si.si_pid == pid);
185 ATF_REQUIRE(si.si_uid == getuid());
186 ATF_REQUIRE(si.si_code == CLD_STOPPED);
187 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
188 (uintmax_t)si.si_utime);
189
190 ATF_REQUIRE(kill(pid, SIGCONT) == 0);
191 ATF_REQUIRE(waitid(P_PID, pid, &si, WCONTINUED) == 0);
192 ATF_REQUIRE(si.si_status == SIGCONT);
193 ATF_REQUIRE(si.si_pid == pid);
194 ATF_REQUIRE(si.si_uid == getuid());
195 ATF_REQUIRE(si.si_code == CLD_CONTINUED);
196 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
197 (uintmax_t)si.si_utime);
198
199 ATF_REQUIRE(kill(pid, SIGQUIT) == 0);
200 ATF_REQUIRE(waitid(P_PID, pid, &si, WEXITED) == 0);
201 ATF_REQUIRE(si.si_status == SIGQUIT);
202 ATF_REQUIRE(si.si_pid == pid);
203 ATF_REQUIRE(si.si_uid == getuid());
204 ATF_REQUIRE(si.si_code == CLD_KILLED);
205 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
206 (uintmax_t)si.si_utime);
207 break;
208 }
209}
210
211ATF_TC(waitid_stopgo_loop);
212ATF_TC_HEAD(waitid_stopgo_loop, tc)
213{
214 atf_tc_set_md_var(tc, "descr",
215 "Test that waitid(2) handled stopped/continued process loop");
216}
217
218ATF_TC_BODY(waitid_stopgo_loop, tc)
219{
220 siginfo_t si;
221 pid_t pid;
222 static const struct rlimit rl = { 0, 0 };
223 size_t N = 100;
224
225 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
226 switch (pid = fork()) {
227 case 0:
228 sleep(100);
229 /*FALLTHROUGH*/
230 case -1:
231 ATF_REQUIRE(pid > 0);
232 __unreachable();
233 }
234
235 printf("Before loop of SIGSTOP/SIGCONT sequence %zu times\n", N);
236 while (N --> 0) {
237 ATF_REQUIRE(kill(pid, SIGSTOP) == 0);
238 ATF_REQUIRE(waitid(P_PID, pid, &si, WSTOPPED) == 0);
239 ATF_REQUIRE(si.si_status == SIGSTOP);
240 ATF_REQUIRE(si.si_pid == pid);
241 ATF_REQUIRE(si.si_uid == getuid());
242 ATF_REQUIRE(si.si_code == CLD_STOPPED);
243
244 ATF_REQUIRE(kill(pid, SIGCONT) == 0);
245 ATF_REQUIRE(waitid(P_PID, pid, &si, WCONTINUED) == 0);
246 ATF_REQUIRE(si.si_status == SIGCONT);
247 ATF_REQUIRE(si.si_pid == pid);
248 ATF_REQUIRE(si.si_uid == getuid());
249 ATF_REQUIRE(si.si_code == CLD_CONTINUED);
250 }
251 ATF_REQUIRE(kill(pid, SIGQUIT) == 0);
252 ATF_REQUIRE(waitid(P_PID, pid, &si, WEXITED) == 0);
253 ATF_REQUIRE(si.si_status == SIGQUIT);
254 ATF_REQUIRE(si.si_pid == pid);
255 ATF_REQUIRE(si.si_uid == getuid());
256 ATF_REQUIRE(si.si_code == CLD_KILLED);
257 printf("user: %ju system: %ju\n", (uintmax_t)si.si_utime,
258 (uintmax_t)si.si_utime);
259}
260
261ATF_TP_ADD_TCS(tp)
262{
263
264 ATF_TP_ADD_TC(tp, waitid_invalid);
265 ATF_TP_ADD_TC(tp, waitid_exited);
266 ATF_TP_ADD_TC(tp, waitid_terminated);
267 ATF_TP_ADD_TC(tp, waitid_coredumped);
268 ATF_TP_ADD_TC(tp, waitid_stop_and_go);
269 ATF_TP_ADD_TC(tp, waitid_stopgo_loop);
270
271 return atf_no_error();
272}