Home | History | Annotate | Line # | Download | only in sys
t_clone.c revision 1.2.2.1
      1  1.2.2.1      yamt /* $NetBSD: t_clone.c,v 1.2.2.1 2012/04/17 00:09:12 yamt Exp $ */
      2      1.1  pgoyette 
      3      1.1  pgoyette /*-
      4      1.1  pgoyette  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5      1.1  pgoyette  * All rights reserved.
      6      1.1  pgoyette  *
      7      1.1  pgoyette  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1  pgoyette  * by Jason R. Thorpe.
      9      1.1  pgoyette  *
     10      1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     11      1.1  pgoyette  * modification, are permitted provided that the following conditions
     12      1.1  pgoyette  * are met:
     13      1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     14      1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     15      1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     18      1.1  pgoyette  *
     19      1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1  pgoyette  */
     31      1.1  pgoyette 
     32      1.1  pgoyette #include <sys/cdefs.h>
     33      1.1  pgoyette __COPYRIGHT("@(#) Copyright (c) 2008\
     34      1.1  pgoyette  The NetBSD Foundation, inc. All rights reserved.");
     35  1.2.2.1      yamt __RCSID("$NetBSD: t_clone.c,v 1.2.2.1 2012/04/17 00:09:12 yamt Exp $");
     36      1.1  pgoyette 
     37      1.1  pgoyette #include <sys/mman.h>
     38      1.1  pgoyette #include <sys/resource.h>
     39      1.1  pgoyette #include <sys/types.h>
     40      1.1  pgoyette #include <sys/wait.h>
     41      1.1  pgoyette 
     42      1.1  pgoyette #include <errno.h>
     43      1.1  pgoyette #include <sched.h>
     44      1.1  pgoyette #include <signal.h>
     45      1.1  pgoyette #include <stdio.h>
     46      1.1  pgoyette #include <stdlib.h>
     47      1.1  pgoyette #include <string.h>
     48      1.1  pgoyette #include <unistd.h>
     49      1.1  pgoyette 
     50      1.1  pgoyette #include <atf-c.h>
     51      1.1  pgoyette 
     52      1.1  pgoyette #define	STACKSIZE	(8 * 1024)
     53      1.1  pgoyette #define	FROBVAL		41973
     54      1.1  pgoyette #define	CHILDEXIT	0xa5
     55      1.1  pgoyette 
     56      1.1  pgoyette static int
     57      1.1  pgoyette dummy(void *arg)
     58      1.1  pgoyette {
     59      1.1  pgoyette 
     60      1.1  pgoyette 	return 0;
     61      1.1  pgoyette }
     62      1.1  pgoyette 
     63      1.1  pgoyette static int
     64      1.1  pgoyette clone_func(void *arg)
     65      1.1  pgoyette {
     66      1.1  pgoyette 	long *frobp = arg, diff;
     67      1.1  pgoyette 
     68      1.1  pgoyette 	printf("child: stack ~= %p, frobme = %p\n", &frobp, frobp);
     69      1.1  pgoyette 	fflush(stdout);
     70      1.1  pgoyette 
     71      1.1  pgoyette 	if (frobp[0] != getppid())
     72      1.1  pgoyette 		return 1;
     73      1.1  pgoyette 
     74      1.1  pgoyette 	if (frobp[0] == getpid())
     75      1.1  pgoyette 		return 2;
     76      1.1  pgoyette 
     77      1.1  pgoyette 	diff = labs(frobp[1] - (long) &frobp);
     78      1.1  pgoyette 
     79      1.1  pgoyette 	if (diff > 1024)
     80      1.1  pgoyette 		return 3;
     81      1.1  pgoyette 
     82      1.1  pgoyette 	frobp[1] = FROBVAL;
     83      1.1  pgoyette 
     84      1.1  pgoyette 	return (CHILDEXIT);
     85      1.1  pgoyette }
     86      1.1  pgoyette 
     87      1.2    jruoho ATF_TC(clone_basic);
     88      1.2    jruoho ATF_TC_HEAD(clone_basic, tc)
     89      1.2    jruoho {
     90      1.2    jruoho 
     91      1.2    jruoho 	atf_tc_set_md_var(tc, "descr", "Checks clone(2)");
     92      1.2    jruoho }
     93      1.2    jruoho 
     94      1.2    jruoho ATF_TC_BODY(clone_basic, tc)
     95      1.1  pgoyette {
     96      1.1  pgoyette 	sigset_t mask;
     97      1.1  pgoyette 	void *allocstack, *stack;
     98      1.1  pgoyette 	pid_t pid;
     99      1.1  pgoyette 	volatile long frobme[2];
    100      1.1  pgoyette 	int stat;
    101      1.1  pgoyette 
    102      1.1  pgoyette 	allocstack = mmap(NULL, STACKSIZE, PROT_READ|PROT_WRITE|PROT_EXEC,
    103      1.1  pgoyette 	    MAP_PRIVATE|MAP_ANON, -1, (off_t) 0);
    104      1.1  pgoyette 
    105      1.1  pgoyette 	ATF_REQUIRE_ERRNO(errno, allocstack != MAP_FAILED);
    106      1.1  pgoyette 
    107  1.2.2.1      yamt 	stack = allocstack;
    108  1.2.2.1      yamt #ifndef __MACHINE_STACK_GROWS_UP
    109  1.2.2.1      yamt 	stack = (char *)stack + STACKSIZE;
    110  1.2.2.1      yamt #endif
    111      1.1  pgoyette 
    112      1.1  pgoyette 	printf("parent: stack = %p, frobme = %p\n", stack, frobme);
    113      1.1  pgoyette 	fflush(stdout);
    114      1.1  pgoyette 
    115      1.1  pgoyette 	frobme[0] = (long)getpid();
    116      1.1  pgoyette 	frobme[1] = (long)stack;
    117      1.1  pgoyette 
    118      1.1  pgoyette 	sigemptyset(&mask);
    119      1.1  pgoyette 	sigaddset(&mask, SIGUSR1);
    120      1.1  pgoyette 
    121      1.1  pgoyette 	ATF_REQUIRE_ERRNO(errno, sigprocmask(SIG_BLOCK, &mask, NULL) != -1);
    122      1.1  pgoyette 
    123      1.1  pgoyette 	switch (pid = __clone(clone_func, stack,
    124      1.1  pgoyette 	    CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|SIGUSR1,
    125      1.1  pgoyette 	    __UNVOLATILE(frobme))) {
    126      1.1  pgoyette 	case 0:
    127      1.1  pgoyette 		atf_tc_fail("clone() returned 0");
    128      1.1  pgoyette 		/*NOTREACHED*/
    129      1.1  pgoyette 	case -1:
    130      1.1  pgoyette 		atf_tc_fail("clone() failed: %s", strerror(errno));
    131      1.1  pgoyette 		/*NOTREACHED*/
    132      1.1  pgoyette 	default:
    133      1.1  pgoyette 		while (waitpid(pid, &stat, __WCLONE) != pid)
    134      1.1  pgoyette 			continue;
    135      1.1  pgoyette 	}
    136      1.1  pgoyette 
    137      1.1  pgoyette 	ATF_REQUIRE_MSG(WIFEXITED(stat) != 0, "child didn't exit");
    138      1.1  pgoyette 
    139      1.1  pgoyette 	printf("parent: childexit = 0x%x, frobme = %ld\n",
    140      1.1  pgoyette 	    WEXITSTATUS(stat), frobme[1]);
    141      1.1  pgoyette 
    142      1.1  pgoyette 	switch (WEXITSTATUS(stat)) {
    143      1.1  pgoyette 	case CHILDEXIT:
    144      1.1  pgoyette 		ATF_REQUIRE_EQ(frobme[1], FROBVAL);
    145      1.1  pgoyette 		break;
    146      1.1  pgoyette 	case 1:
    147      1.1  pgoyette 		atf_tc_fail("child: argument does not contain parent's pid");
    148      1.1  pgoyette 		/*NOTREACHED*/
    149      1.1  pgoyette 	case 2:
    150      1.1  pgoyette 		atf_tc_fail("child: called in parent's pid");
    151      1.1  pgoyette 		/*NOTREACHED*/
    152      1.1  pgoyette 	case 3:
    153      1.1  pgoyette 		atf_tc_fail("child: called with bad stack");
    154      1.1  pgoyette 		/*NOTREACHED*/
    155      1.1  pgoyette 	default:
    156      1.1  pgoyette 		atf_tc_fail("child returned unknown code: %d",
    157      1.1  pgoyette 		    WEXITSTATUS(stat));
    158      1.1  pgoyette 		/*NOTREACHED*/
    159      1.1  pgoyette 	}
    160      1.1  pgoyette 
    161      1.1  pgoyette 	ATF_REQUIRE_ERRNO(errno, munmap(allocstack, STACKSIZE) != -1);
    162      1.1  pgoyette }
    163      1.1  pgoyette 
    164      1.2    jruoho ATF_TC(clone_null_stack);
    165      1.2    jruoho ATF_TC_HEAD(clone_null_stack, tc)
    166      1.1  pgoyette {
    167      1.1  pgoyette 
    168      1.1  pgoyette 	atf_tc_set_md_var(tc, "descr",
    169      1.1  pgoyette 	    "Checks that clone(2) fails when stack pointer is NULL");
    170      1.1  pgoyette }
    171      1.1  pgoyette 
    172      1.2    jruoho ATF_TC_BODY(clone_null_stack, tc)
    173      1.1  pgoyette {
    174      1.1  pgoyette 	int rv;
    175      1.1  pgoyette 
    176      1.1  pgoyette 	rv = __clone(dummy, NULL,
    177      1.1  pgoyette 	    CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|SIGCHLD, NULL);
    178      1.1  pgoyette 
    179      1.1  pgoyette 	ATF_REQUIRE_EQ(rv, -1);
    180      1.1  pgoyette 	ATF_REQUIRE_EQ(errno, EINVAL);
    181      1.1  pgoyette }
    182      1.1  pgoyette 
    183      1.2    jruoho ATF_TC(clone_null_func);
    184      1.2    jruoho ATF_TC_HEAD(clone_null_func, tc)
    185      1.1  pgoyette {
    186      1.1  pgoyette 
    187      1.1  pgoyette 	atf_tc_set_md_var(tc, "descr",
    188      1.1  pgoyette 	    "Checks that clone(2) fails when function pointer is NULL");
    189      1.1  pgoyette }
    190      1.1  pgoyette 
    191      1.2    jruoho ATF_TC_BODY(clone_null_func, tc)
    192      1.1  pgoyette {
    193      1.1  pgoyette 	void *allocstack, *stack;
    194  1.2.2.1      yamt 	int rv;
    195      1.1  pgoyette 
    196      1.1  pgoyette 	allocstack = mmap(NULL, STACKSIZE, PROT_READ|PROT_WRITE|PROT_EXEC,
    197      1.1  pgoyette 	    MAP_PRIVATE|MAP_ANON, -1, (off_t) 0);
    198      1.1  pgoyette 	ATF_REQUIRE_ERRNO(errno, allocstack != MAP_FAILED);
    199  1.2.2.1      yamt 	stack = allocstack;
    200  1.2.2.1      yamt #ifndef __MACHINE_STACK_GROWS_UP
    201  1.2.2.1      yamt 	stack = (char *)stack + STACKSIZE;
    202  1.2.2.1      yamt #endif
    203      1.1  pgoyette 
    204      1.1  pgoyette 	errno = 0;
    205      1.1  pgoyette 	rv = __clone(0, stack,
    206      1.1  pgoyette 	    CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|SIGCHLD, NULL);
    207      1.1  pgoyette 
    208      1.1  pgoyette 	ATF_REQUIRE_EQ(rv, -1);
    209      1.1  pgoyette 	ATF_REQUIRE_EQ(errno, EINVAL);
    210      1.1  pgoyette 
    211      1.1  pgoyette 	ATF_REQUIRE_ERRNO(errno, munmap(allocstack, STACKSIZE) != -1);
    212      1.1  pgoyette }
    213      1.1  pgoyette 
    214      1.2    jruoho ATF_TC(clone_out_of_proc);
    215      1.2    jruoho ATF_TC_HEAD(clone_out_of_proc, tc)
    216      1.1  pgoyette {
    217      1.1  pgoyette 
    218      1.1  pgoyette 	atf_tc_set_md_var(tc, "descr",
    219      1.1  pgoyette 		"Checks that clone(2) fails when running out of processes");
    220      1.1  pgoyette 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
    221      1.1  pgoyette }
    222      1.1  pgoyette 
    223      1.2    jruoho ATF_TC_BODY(clone_out_of_proc, tc)
    224      1.1  pgoyette {
    225      1.1  pgoyette 	struct rlimit rl;
    226      1.1  pgoyette 	int rv;
    227      1.1  pgoyette 
    228      1.1  pgoyette 	ATF_REQUIRE_ERRNO(errno, getrlimit(RLIMIT_NPROC, &rl) != -1);
    229      1.1  pgoyette 
    230      1.1  pgoyette 	rl.rlim_cur = 0;
    231      1.1  pgoyette 	rl.rlim_max = 0;
    232      1.1  pgoyette 
    233      1.1  pgoyette 	ATF_REQUIRE_ERRNO(errno, setrlimit(RLIMIT_NPROC, &rl) != -1);
    234      1.1  pgoyette 
    235      1.1  pgoyette 	errno = 0;
    236      1.1  pgoyette 	rv = __clone(dummy, malloc(10240),
    237      1.1  pgoyette 	    CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|SIGCHLD, (void *)&rl);
    238      1.1  pgoyette 
    239      1.1  pgoyette 	ATF_REQUIRE_EQ(rv, -1);
    240      1.1  pgoyette 	ATF_REQUIRE_EQ(errno, EAGAIN);
    241      1.1  pgoyette }
    242      1.1  pgoyette 
    243      1.1  pgoyette ATF_TP_ADD_TCS(tp)
    244      1.1  pgoyette {
    245      1.1  pgoyette 
    246      1.2    jruoho 	ATF_TP_ADD_TC(tp, clone_basic);
    247      1.2    jruoho 	ATF_TP_ADD_TC(tp, clone_null_stack);
    248      1.2    jruoho 	ATF_TP_ADD_TC(tp, clone_null_func);
    249      1.2    jruoho 	ATF_TP_ADD_TC(tp, clone_out_of_proc);
    250      1.1  pgoyette 
    251      1.1  pgoyette 	return atf_no_error();
    252      1.1  pgoyette }
    253