xref: /freebsd/contrib/netbsd-tests/rump/rumpkern/t_lwproc.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*	$NetBSD: t_lwproc.c,v 1.5 2011/01/02 12:58:17 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 
33 #include <rump/rump.h>
34 #include <rump/rump_syscalls.h>
35 
36 #include <atf-c.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <util.h>
45 
46 #include "../../h_macros.h"
47 
48 ATF_TC(makelwp);
49 ATF_TC_HEAD(makelwp, tc)
50 {
51 
52 	atf_tc_set_md_var(tc, "descr", "tests that lwps can be attached to "
53 	    "processes");
54 }
55 
56 ATF_TC_BODY(makelwp, tc)
57 {
58 	struct lwp *l;
59 	pid_t pid;
60 
61 	rump_init();
62 	RZ(rump_pub_lwproc_newlwp(0));
63 	ATF_REQUIRE_EQ(rump_pub_lwproc_newlwp(37), ESRCH);
64 	l = rump_pub_lwproc_curlwp();
65 
66 	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
67 	ATF_REQUIRE(rump_pub_lwproc_curlwp() != l);
68 	l = rump_pub_lwproc_curlwp();
69 
70 	RZ(rump_pub_lwproc_newlwp(rump_sys_getpid()));
71 	ATF_REQUIRE(rump_pub_lwproc_curlwp() != l);
72 
73 	pid = rump_sys_getpid();
74 	ATF_REQUIRE(pid != -1 && pid != 0);
75 }
76 
77 ATF_TC(proccreds);
78 ATF_TC_HEAD(proccreds, tc)
79 {
80 
81 	atf_tc_set_md_var(tc, "descr", "check that procs have different creds");
82 }
83 
84 ATF_TC_BODY(proccreds, tc)
85 {
86 	struct lwp *l1, *l2;
87 
88 	rump_init();
89 	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
90 	l1 = rump_pub_lwproc_curlwp();
91 
92 	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
93 	l2 = rump_pub_lwproc_curlwp();
94 
95 	RL(rump_sys_setuid(22));
96 	ATF_REQUIRE_EQ(rump_sys_getuid(), 22);
97 
98 	rump_pub_lwproc_switch(l1);
99 	ATF_REQUIRE_EQ(rump_sys_getuid(), 0); /* from parent, proc0 */
100 	RL(rump_sys_setuid(11));
101 	ATF_REQUIRE_EQ(rump_sys_getuid(), 11);
102 
103 	rump_pub_lwproc_switch(l2);
104 	ATF_REQUIRE_EQ(rump_sys_getuid(), 22);
105 	rump_pub_lwproc_newlwp(rump_sys_getpid());
106 	ATF_REQUIRE_EQ(rump_sys_getuid(), 22);
107 }
108 
109 
110 ATF_TC(inherit);
111 ATF_TC_HEAD(inherit, tc)
112 {
113 
114 	atf_tc_set_md_var(tc, "descr", "new processes inherit creds from "
115 	    "parents");
116 }
117 
118 ATF_TC_BODY(inherit, tc)
119 {
120 
121 	rump_init();
122 
123 	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
124 	RL(rump_sys_setuid(66));
125 	ATF_REQUIRE_EQ(rump_sys_getuid(), 66);
126 
127 	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
128 	ATF_REQUIRE_EQ(rump_sys_getuid(), 66);
129 
130 	/* release lwp and proc */
131 	rump_pub_lwproc_releaselwp();
132 	ATF_REQUIRE_EQ(rump_sys_getuid(), 0);
133 }
134 
135 ATF_TC(lwps);
136 ATF_TC_HEAD(lwps, tc)
137 {
138 
139 	atf_tc_set_md_var(tc, "descr", "proc can hold many lwps and is "
140 	    "automatically g/c'd when the last one exits");
141 }
142 
143 #define LOOPS 128
144 ATF_TC_BODY(lwps, tc)
145 {
146 	struct lwp *l[LOOPS];
147 	pid_t mypid;
148 	struct lwp *l_orig;
149 	int i;
150 
151 	rump_init();
152 
153 	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
154 	mypid = rump_sys_getpid();
155 	RL(rump_sys_setuid(375));
156 
157 	l_orig = rump_pub_lwproc_curlwp();
158 	for (i = 0; i < LOOPS; i++) {
159 		mypid = rump_sys_getpid();
160 		ATF_REQUIRE(mypid != -1 && mypid != 0);
161 		RZ(rump_pub_lwproc_newlwp(mypid));
162 		l[i] = rump_pub_lwproc_curlwp();
163 		ATF_REQUIRE_EQ(rump_sys_getuid(), 375);
164 	}
165 
166 	rump_pub_lwproc_switch(l_orig);
167 	rump_pub_lwproc_releaselwp();
168 	for (i = 0; i < LOOPS; i++) {
169 		rump_pub_lwproc_switch(l[i]);
170 		ATF_REQUIRE_EQ(rump_sys_getpid(), mypid);
171 		ATF_REQUIRE_EQ(rump_sys_getuid(), 375);
172 		rump_pub_lwproc_releaselwp();
173 		ATF_REQUIRE_EQ(rump_sys_getpid(), 1);
174 		ATF_REQUIRE_EQ(rump_sys_getuid(), 0);
175 	}
176 
177 	ATF_REQUIRE_EQ(rump_pub_lwproc_newlwp(mypid), ESRCH);
178 }
179 
180 ATF_TC(nolwprelease);
181 ATF_TC_HEAD(nolwprelease, tc)
182 {
183 
184 	atf_tc_set_md_var(tc, "descr", "check that lwp context is required "
185 	    "for lwproc_releaselwp()");
186 }
187 
188 ATF_TC_BODY(nolwprelease, tc)
189 {
190 	int status;
191 
192 	switch (fork()) {
193 	case 0:
194 		rump_init();
195 		rump_pub_lwproc_releaselwp();
196 		atf_tc_fail("survived");
197 		break;
198 	case -1:
199 		atf_tc_fail_errno("fork");
200 		break;
201 	default:
202 		wait(&status);
203 		ATF_REQUIRE(WIFSIGNALED(status));
204 		ATF_REQUIRE_EQ(WTERMSIG(status), SIGABRT);
205 
206 	}
207 }
208 
209 ATF_TC(nolwp);
210 ATF_TC_HEAD(nolwp, tc)
211 {
212 
213 	atf_tc_set_md_var(tc, "descr", "check that curlwp for an implicit "
214 	    "context is NULL");
215 }
216 
217 ATF_TC_BODY(nolwp, tc)
218 {
219 
220 	rump_init();
221 	ATF_REQUIRE_EQ(rump_pub_lwproc_curlwp(), NULL);
222 }
223 
224 ATF_TC(nullswitch);
225 ATF_TC_HEAD(nullswitch, tc)
226 {
227 
228 	atf_tc_set_md_var(tc, "descr", "check that switching to NULL marks "
229 	    "current lwp as not running");
230 }
231 
232 ATF_TC_BODY(nullswitch, tc)
233 {
234 	struct lwp *l;
235 
236 	rump_init();
237 	RZ(rump_pub_lwproc_newlwp(0));
238 	l = rump_pub_lwproc_curlwp();
239 	rump_pub_lwproc_switch(NULL);
240 	/* if remains LP_RUNNING, next call will panic */
241 	rump_pub_lwproc_switch(l);
242 }
243 
244 ATF_TC(rfork);
245 ATF_TC_HEAD(rfork, tc)
246 {
247 
248 	atf_tc_set_md_var(tc, "descr", "check that fork shares fd's");
249 }
250 
251 ATF_TC_BODY(rfork, tc)
252 {
253 	struct stat sb;
254 	struct lwp *l, *l2;
255 	int fd;
256 
257 	RZ(rump_init());
258 
259 	ATF_REQUIRE_EQ(rump_pub_lwproc_rfork(RUMP_RFFDG|RUMP_RFCFDG), EINVAL);
260 
261 	RZ(rump_pub_lwproc_rfork(0));
262 	l = rump_pub_lwproc_curlwp();
263 
264 	RL(fd = rump_sys_open("/file", O_RDWR | O_CREAT, 0777));
265 
266 	/* ok, first check rfork(RUMP_RFCFDG) does *not* preserve fd's */
267 	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
268 	ATF_REQUIRE_ERRNO(EBADF, rump_sys_write(fd, &fd, sizeof(fd)) == -1);
269 
270 	/* then check that rfork(0) does */
271 	rump_pub_lwproc_switch(l);
272 	RZ(rump_pub_lwproc_rfork(0));
273 	ATF_REQUIRE_EQ(rump_sys_write(fd, &fd, sizeof(fd)), sizeof(fd));
274 	RL(rump_sys_fstat(fd, &sb));
275 	l2 = rump_pub_lwproc_curlwp();
276 
277 	/*
278 	 * check that the shared fd table is really shared by
279 	 * closing fd in parent
280 	 */
281 	rump_pub_lwproc_switch(l);
282 	RL(rump_sys_close(fd));
283 	rump_pub_lwproc_switch(l2);
284 	ATF_REQUIRE_ERRNO(EBADF, rump_sys_fstat(fd, &sb) == -1);
285 
286 	/* redo, this time copying the fd table instead of sharing it */
287 	rump_pub_lwproc_releaselwp();
288 	rump_pub_lwproc_switch(l);
289 	RL(fd = rump_sys_open("/file", O_RDWR, 0777));
290 	RZ(rump_pub_lwproc_rfork(RUMP_RFFDG));
291 	ATF_REQUIRE_EQ(rump_sys_write(fd, &fd, sizeof(fd)), sizeof(fd));
292 	RL(rump_sys_fstat(fd, &sb));
293 	l2 = rump_pub_lwproc_curlwp();
294 
295 	/* check that the fd table is copied */
296 	rump_pub_lwproc_switch(l);
297 	RL(rump_sys_close(fd));
298 	rump_pub_lwproc_switch(l2);
299 	RL(rump_sys_fstat(fd, &sb));
300 	ATF_REQUIRE_EQ(sb.st_size, sizeof(fd));
301 }
302 
303 ATF_TP_ADD_TCS(tp)
304 {
305 
306 	ATF_TP_ADD_TC(tp, makelwp);
307 	ATF_TP_ADD_TC(tp, proccreds);
308 	ATF_TP_ADD_TC(tp, inherit);
309 	ATF_TP_ADD_TC(tp, lwps);
310 	ATF_TP_ADD_TC(tp, nolwprelease);
311 	ATF_TP_ADD_TC(tp, nolwp);
312 	ATF_TP_ADD_TC(tp, nullswitch);
313 	ATF_TP_ADD_TC(tp, rfork);
314 
315 	return atf_no_error();
316 }
317