t_clone.c revision 1.1 1 1.1 pgoyette /* $NetBSD: t_clone.c,v 1.1 2011/01/13 02:40:44 pgoyette 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.1 pgoyette __RCSID("$NetBSD: t_clone.c,v 1.1 2011/01/13 02:40:44 pgoyette 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 /*
57 1.1 pgoyette * return 1 if the stack grows down, 0 if it grows up.
58 1.1 pgoyette */
59 1.1 pgoyette static int
60 1.1 pgoyette stackdir(void *p)
61 1.1 pgoyette {
62 1.1 pgoyette int val;
63 1.1 pgoyette void *q = &val;
64 1.1 pgoyette
65 1.1 pgoyette return p < q ? 0 : 1;
66 1.1 pgoyette }
67 1.1 pgoyette
68 1.1 pgoyette static int
69 1.1 pgoyette dummy(void *arg)
70 1.1 pgoyette {
71 1.1 pgoyette
72 1.1 pgoyette return 0;
73 1.1 pgoyette }
74 1.1 pgoyette
75 1.1 pgoyette ATF_TC(clone);
76 1.1 pgoyette
77 1.1 pgoyette ATF_TC_HEAD(clone, tc)
78 1.1 pgoyette {
79 1.1 pgoyette
80 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Checks clone(2)");
81 1.1 pgoyette }
82 1.1 pgoyette
83 1.1 pgoyette static int
84 1.1 pgoyette clone_func(void *arg)
85 1.1 pgoyette {
86 1.1 pgoyette long *frobp = arg, diff;
87 1.1 pgoyette
88 1.1 pgoyette printf("child: stack ~= %p, frobme = %p\n", &frobp, frobp);
89 1.1 pgoyette fflush(stdout);
90 1.1 pgoyette
91 1.1 pgoyette if (frobp[0] != getppid())
92 1.1 pgoyette return 1;
93 1.1 pgoyette
94 1.1 pgoyette if (frobp[0] == getpid())
95 1.1 pgoyette return 2;
96 1.1 pgoyette
97 1.1 pgoyette diff = labs(frobp[1] - (long) &frobp);
98 1.1 pgoyette
99 1.1 pgoyette if (diff > 1024)
100 1.1 pgoyette return 3;
101 1.1 pgoyette
102 1.1 pgoyette frobp[1] = FROBVAL;
103 1.1 pgoyette
104 1.1 pgoyette return (CHILDEXIT);
105 1.1 pgoyette }
106 1.1 pgoyette
107 1.1 pgoyette ATF_TC_BODY(clone, tc)
108 1.1 pgoyette {
109 1.1 pgoyette sigset_t mask;
110 1.1 pgoyette void *allocstack, *stack;
111 1.1 pgoyette pid_t pid;
112 1.1 pgoyette volatile long frobme[2];
113 1.1 pgoyette int stat;
114 1.1 pgoyette
115 1.1 pgoyette allocstack = mmap(NULL, STACKSIZE, PROT_READ|PROT_WRITE|PROT_EXEC,
116 1.1 pgoyette MAP_PRIVATE|MAP_ANON, -1, (off_t) 0);
117 1.1 pgoyette
118 1.1 pgoyette ATF_REQUIRE_ERRNO(errno, allocstack != MAP_FAILED);
119 1.1 pgoyette
120 1.1 pgoyette stack = (caddr_t) allocstack + STACKSIZE * stackdir(&stat);
121 1.1 pgoyette
122 1.1 pgoyette printf("parent: stack = %p, frobme = %p\n", stack, frobme);
123 1.1 pgoyette fflush(stdout);
124 1.1 pgoyette
125 1.1 pgoyette frobme[0] = (long)getpid();
126 1.1 pgoyette frobme[1] = (long)stack;
127 1.1 pgoyette
128 1.1 pgoyette sigemptyset(&mask);
129 1.1 pgoyette sigaddset(&mask, SIGUSR1);
130 1.1 pgoyette
131 1.1 pgoyette ATF_REQUIRE_ERRNO(errno, sigprocmask(SIG_BLOCK, &mask, NULL) != -1);
132 1.1 pgoyette
133 1.1 pgoyette switch (pid = __clone(clone_func, stack,
134 1.1 pgoyette CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|SIGUSR1,
135 1.1 pgoyette __UNVOLATILE(frobme))) {
136 1.1 pgoyette case 0:
137 1.1 pgoyette atf_tc_fail("clone() returned 0");
138 1.1 pgoyette /*NOTREACHED*/
139 1.1 pgoyette case -1:
140 1.1 pgoyette atf_tc_fail("clone() failed: %s", strerror(errno));
141 1.1 pgoyette /*NOTREACHED*/
142 1.1 pgoyette default:
143 1.1 pgoyette while (waitpid(pid, &stat, __WCLONE) != pid)
144 1.1 pgoyette continue;
145 1.1 pgoyette }
146 1.1 pgoyette
147 1.1 pgoyette ATF_REQUIRE_MSG(WIFEXITED(stat) != 0, "child didn't exit");
148 1.1 pgoyette
149 1.1 pgoyette printf("parent: childexit = 0x%x, frobme = %ld\n",
150 1.1 pgoyette WEXITSTATUS(stat), frobme[1]);
151 1.1 pgoyette
152 1.1 pgoyette switch (WEXITSTATUS(stat)) {
153 1.1 pgoyette case CHILDEXIT:
154 1.1 pgoyette ATF_REQUIRE_EQ(frobme[1], FROBVAL);
155 1.1 pgoyette break;
156 1.1 pgoyette case 1:
157 1.1 pgoyette atf_tc_fail("child: argument does not contain parent's pid");
158 1.1 pgoyette /*NOTREACHED*/
159 1.1 pgoyette case 2:
160 1.1 pgoyette atf_tc_fail("child: called in parent's pid");
161 1.1 pgoyette /*NOTREACHED*/
162 1.1 pgoyette case 3:
163 1.1 pgoyette atf_tc_fail("child: called with bad stack");
164 1.1 pgoyette /*NOTREACHED*/
165 1.1 pgoyette default:
166 1.1 pgoyette atf_tc_fail("child returned unknown code: %d",
167 1.1 pgoyette WEXITSTATUS(stat));
168 1.1 pgoyette /*NOTREACHED*/
169 1.1 pgoyette }
170 1.1 pgoyette
171 1.1 pgoyette ATF_REQUIRE_ERRNO(errno, munmap(allocstack, STACKSIZE) != -1);
172 1.1 pgoyette }
173 1.1 pgoyette
174 1.1 pgoyette ATF_TC(null_stack);
175 1.1 pgoyette
176 1.1 pgoyette ATF_TC_HEAD(null_stack, tc)
177 1.1 pgoyette {
178 1.1 pgoyette
179 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
180 1.1 pgoyette "Checks that clone(2) fails when stack pointer is NULL");
181 1.1 pgoyette }
182 1.1 pgoyette
183 1.1 pgoyette ATF_TC_BODY(null_stack, tc)
184 1.1 pgoyette {
185 1.1 pgoyette int rv;
186 1.1 pgoyette
187 1.1 pgoyette rv = __clone(dummy, NULL,
188 1.1 pgoyette CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|SIGCHLD, NULL);
189 1.1 pgoyette
190 1.1 pgoyette ATF_REQUIRE_EQ(rv, -1);
191 1.1 pgoyette ATF_REQUIRE_EQ(errno, EINVAL);
192 1.1 pgoyette }
193 1.1 pgoyette
194 1.1 pgoyette ATF_TC(null_func);
195 1.1 pgoyette
196 1.1 pgoyette ATF_TC_HEAD(null_func, tc)
197 1.1 pgoyette {
198 1.1 pgoyette
199 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
200 1.1 pgoyette "Checks that clone(2) fails when function pointer is NULL");
201 1.1 pgoyette }
202 1.1 pgoyette
203 1.1 pgoyette ATF_TC_BODY(null_func, tc)
204 1.1 pgoyette {
205 1.1 pgoyette void *allocstack, *stack;
206 1.1 pgoyette int rv, stat;
207 1.1 pgoyette
208 1.1 pgoyette allocstack = mmap(NULL, STACKSIZE, PROT_READ|PROT_WRITE|PROT_EXEC,
209 1.1 pgoyette MAP_PRIVATE|MAP_ANON, -1, (off_t) 0);
210 1.1 pgoyette ATF_REQUIRE_ERRNO(errno, allocstack != MAP_FAILED);
211 1.1 pgoyette stack = (caddr_t) allocstack + STACKSIZE * stackdir(&stat);
212 1.1 pgoyette
213 1.1 pgoyette errno = 0;
214 1.1 pgoyette rv = __clone(0, stack,
215 1.1 pgoyette CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|SIGCHLD, NULL);
216 1.1 pgoyette
217 1.1 pgoyette ATF_REQUIRE_EQ(rv, -1);
218 1.1 pgoyette ATF_REQUIRE_EQ(errno, EINVAL);
219 1.1 pgoyette
220 1.1 pgoyette ATF_REQUIRE_ERRNO(errno, munmap(allocstack, STACKSIZE) != -1);
221 1.1 pgoyette }
222 1.1 pgoyette
223 1.1 pgoyette ATF_TC(out_of_proc);
224 1.1 pgoyette
225 1.1 pgoyette ATF_TC_HEAD(out_of_proc, tc)
226 1.1 pgoyette {
227 1.1 pgoyette
228 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
229 1.1 pgoyette "Checks that clone(2) fails when running out of processes");
230 1.1 pgoyette atf_tc_set_md_var(tc, "require.user", "unprivileged");
231 1.1 pgoyette }
232 1.1 pgoyette
233 1.1 pgoyette ATF_TC_BODY(out_of_proc, tc)
234 1.1 pgoyette {
235 1.1 pgoyette struct rlimit rl;
236 1.1 pgoyette int rv;
237 1.1 pgoyette
238 1.1 pgoyette ATF_REQUIRE_ERRNO(errno, getrlimit(RLIMIT_NPROC, &rl) != -1);
239 1.1 pgoyette
240 1.1 pgoyette rl.rlim_cur = 0;
241 1.1 pgoyette rl.rlim_max = 0;
242 1.1 pgoyette
243 1.1 pgoyette ATF_REQUIRE_ERRNO(errno, setrlimit(RLIMIT_NPROC, &rl) != -1);
244 1.1 pgoyette
245 1.1 pgoyette errno = 0;
246 1.1 pgoyette rv = __clone(dummy, malloc(10240),
247 1.1 pgoyette CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|SIGCHLD, (void *)&rl);
248 1.1 pgoyette
249 1.1 pgoyette ATF_REQUIRE_EQ(rv, -1);
250 1.1 pgoyette ATF_REQUIRE_EQ(errno, EAGAIN);
251 1.1 pgoyette }
252 1.1 pgoyette
253 1.1 pgoyette ATF_TP_ADD_TCS(tp)
254 1.1 pgoyette {
255 1.1 pgoyette
256 1.1 pgoyette ATF_TP_ADD_TC(tp, clone);
257 1.1 pgoyette ATF_TP_ADD_TC(tp, null_stack);
258 1.1 pgoyette ATF_TP_ADD_TC(tp, null_func);
259 1.1 pgoyette ATF_TP_ADD_TC(tp, out_of_proc);
260 1.1 pgoyette
261 1.1 pgoyette return atf_no_error();
262 1.1 pgoyette }
263