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