<sys/socket.h>: turn off MSG_NOSIGNAL
[minix3.git] / tests / rump / rumpkern / t_lwproc.c
blobdcdfdb6f30a4c1aa06bb393869fffba79ab9d4a8
1 /* $NetBSD: t_lwproc.c,v 1.5 2011/01/02 12:58:17 pooka Exp $ */
3 /*
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
30 #include <sys/types.h>
31 #include <sys/wait.h>
33 #include <rump/rump.h>
34 #include <rump/rump_syscalls.h>
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>
46 #include "../../h_macros.h"
48 ATF_TC(makelwp);
49 ATF_TC_HEAD(makelwp, tc)
52 atf_tc_set_md_var(tc, "descr", "tests that lwps can be attached to "
53 "processes");
56 ATF_TC_BODY(makelwp, tc)
58 struct lwp *l;
59 pid_t pid;
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();
66 RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
67 ATF_REQUIRE(rump_pub_lwproc_curlwp() != l);
68 l = rump_pub_lwproc_curlwp();
70 RZ(rump_pub_lwproc_newlwp(rump_sys_getpid()));
71 ATF_REQUIRE(rump_pub_lwproc_curlwp() != l);
73 pid = rump_sys_getpid();
74 ATF_REQUIRE(pid != -1 && pid != 0);
77 ATF_TC(proccreds);
78 ATF_TC_HEAD(proccreds, tc)
81 atf_tc_set_md_var(tc, "descr", "check that procs have different creds");
84 ATF_TC_BODY(proccreds, tc)
86 struct lwp *l1, *l2;
88 rump_init();
89 RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
90 l1 = rump_pub_lwproc_curlwp();
92 RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
93 l2 = rump_pub_lwproc_curlwp();
95 RL(rump_sys_setuid(22));
96 ATF_REQUIRE_EQ(rump_sys_getuid(), 22);
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);
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);
110 ATF_TC(inherit);
111 ATF_TC_HEAD(inherit, tc)
114 atf_tc_set_md_var(tc, "descr", "new processes inherit creds from "
115 "parents");
118 ATF_TC_BODY(inherit, tc)
121 rump_init();
123 RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
124 RL(rump_sys_setuid(66));
125 ATF_REQUIRE_EQ(rump_sys_getuid(), 66);
127 RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
128 ATF_REQUIRE_EQ(rump_sys_getuid(), 66);
130 /* release lwp and proc */
131 rump_pub_lwproc_releaselwp();
132 ATF_REQUIRE_EQ(rump_sys_getuid(), 0);
135 ATF_TC(lwps);
136 ATF_TC_HEAD(lwps, tc)
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");
143 #define LOOPS 128
144 ATF_TC_BODY(lwps, tc)
146 struct lwp *l[LOOPS];
147 pid_t mypid;
148 struct lwp *l_orig;
149 int i;
151 rump_init();
153 RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
154 mypid = rump_sys_getpid();
155 RL(rump_sys_setuid(375));
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);
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);
177 ATF_REQUIRE_EQ(rump_pub_lwproc_newlwp(mypid), ESRCH);
180 ATF_TC(nolwprelease);
181 ATF_TC_HEAD(nolwprelease, tc)
184 atf_tc_set_md_var(tc, "descr", "check that lwp context is required "
185 "for lwproc_releaselwp()");
188 ATF_TC_BODY(nolwprelease, tc)
190 int status;
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);
209 ATF_TC(nolwp);
210 ATF_TC_HEAD(nolwp, tc)
213 atf_tc_set_md_var(tc, "descr", "check that curlwp for an implicit "
214 "context is NULL");
217 ATF_TC_BODY(nolwp, tc)
220 rump_init();
221 ATF_REQUIRE_EQ(rump_pub_lwproc_curlwp(), NULL);
224 ATF_TC(nullswitch);
225 ATF_TC_HEAD(nullswitch, tc)
228 atf_tc_set_md_var(tc, "descr", "check that switching to NULL marks "
229 "current lwp as not running");
232 ATF_TC_BODY(nullswitch, tc)
234 struct lwp *l;
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);
244 ATF_TC(rfork);
245 ATF_TC_HEAD(rfork, tc)
248 atf_tc_set_md_var(tc, "descr", "check that fork shares fd's");
251 ATF_TC_BODY(rfork, tc)
253 struct stat sb;
254 struct lwp *l, *l2;
255 int fd;
257 RZ(rump_init());
259 ATF_REQUIRE_EQ(rump_pub_lwproc_rfork(RUMP_RFFDG|RUMP_RFCFDG), EINVAL);
261 RZ(rump_pub_lwproc_rfork(0));
262 l = rump_pub_lwproc_curlwp();
264 RL(fd = rump_sys_open("/file", O_RDWR | O_CREAT, 0777));
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);
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();
278 * check that the shared fd table is really shared by
279 * closing fd in parent
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);
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();
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));
303 ATF_TP_ADD_TCS(tp)
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);
315 return atf_no_error();