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