1 /* $NetBSD: t_clone.c,v 1.3 2011/12/12 20:55:44 joerg Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2008\
34 The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_clone.c,v 1.3 2011/12/12 20:55:44 joerg Exp $");
38 #include <sys/resource.h>
39 #include <sys/types.h>
52 #define STACKSIZE (8 * 1024)
54 #define CHILDEXIT 0xa5
66 long *frobp
= arg
, diff
;
68 printf("child: stack ~= %p, frobme = %p\n", &frobp
, frobp
);
71 if (frobp
[0] != getppid())
74 if (frobp
[0] == getpid())
77 diff
= labs(frobp
[1] - (long) &frobp
);
88 ATF_TC_HEAD(clone_basic
, tc
)
91 atf_tc_set_md_var(tc
, "descr", "Checks clone(2)");
94 ATF_TC_BODY(clone_basic
, tc
)
97 void *allocstack
, *stack
;
99 volatile long frobme
[2];
102 allocstack
= mmap(NULL
, STACKSIZE
, PROT_READ
|PROT_WRITE
|PROT_EXEC
,
103 MAP_PRIVATE
|MAP_ANON
, -1, (off_t
) 0);
105 ATF_REQUIRE_ERRNO(errno
, allocstack
!= MAP_FAILED
);
108 #ifndef __MACHINE_STACK_GROWS_UP
109 stack
= (char *)stack
+ STACKSIZE
;
112 printf("parent: stack = %p, frobme = %p\n", stack
, frobme
);
115 frobme
[0] = (long)getpid();
116 frobme
[1] = (long)stack
;
119 sigaddset(&mask
, SIGUSR1
);
121 ATF_REQUIRE_ERRNO(errno
, sigprocmask(SIG_BLOCK
, &mask
, NULL
) != -1);
123 switch (pid
= __clone(clone_func
, stack
,
124 CLONE_VM
|CLONE_FS
|CLONE_FILES
|CLONE_SIGHAND
|SIGUSR1
,
125 __UNVOLATILE(frobme
))) {
127 atf_tc_fail("clone() returned 0");
130 atf_tc_fail("clone() failed: %s", strerror(errno
));
133 while (waitpid(pid
, &stat
, __WCLONE
) != pid
)
137 ATF_REQUIRE_MSG(WIFEXITED(stat
) != 0, "child didn't exit");
139 printf("parent: childexit = 0x%x, frobme = %ld\n",
140 WEXITSTATUS(stat
), frobme
[1]);
142 switch (WEXITSTATUS(stat
)) {
144 ATF_REQUIRE_EQ(frobme
[1], FROBVAL
);
147 atf_tc_fail("child: argument does not contain parent's pid");
150 atf_tc_fail("child: called in parent's pid");
153 atf_tc_fail("child: called with bad stack");
156 atf_tc_fail("child returned unknown code: %d",
161 ATF_REQUIRE_ERRNO(errno
, munmap(allocstack
, STACKSIZE
) != -1);
164 ATF_TC(clone_null_stack
);
165 ATF_TC_HEAD(clone_null_stack
, tc
)
168 atf_tc_set_md_var(tc
, "descr",
169 "Checks that clone(2) fails when stack pointer is NULL");
172 ATF_TC_BODY(clone_null_stack
, tc
)
176 rv
= __clone(dummy
, NULL
,
177 CLONE_VM
|CLONE_FS
|CLONE_FILES
|CLONE_SIGHAND
|SIGCHLD
, NULL
);
179 ATF_REQUIRE_EQ(rv
, -1);
180 ATF_REQUIRE_EQ(errno
, EINVAL
);
183 ATF_TC(clone_null_func
);
184 ATF_TC_HEAD(clone_null_func
, tc
)
187 atf_tc_set_md_var(tc
, "descr",
188 "Checks that clone(2) fails when function pointer is NULL");
191 ATF_TC_BODY(clone_null_func
, tc
)
193 void *allocstack
, *stack
;
196 allocstack
= mmap(NULL
, STACKSIZE
, PROT_READ
|PROT_WRITE
|PROT_EXEC
,
197 MAP_PRIVATE
|MAP_ANON
, -1, (off_t
) 0);
198 ATF_REQUIRE_ERRNO(errno
, allocstack
!= MAP_FAILED
);
200 #ifndef __MACHINE_STACK_GROWS_UP
201 stack
= (char *)stack
+ STACKSIZE
;
205 rv
= __clone(0, stack
,
206 CLONE_VM
|CLONE_FS
|CLONE_FILES
|CLONE_SIGHAND
|SIGCHLD
, NULL
);
208 ATF_REQUIRE_EQ(rv
, -1);
209 ATF_REQUIRE_EQ(errno
, EINVAL
);
211 ATF_REQUIRE_ERRNO(errno
, munmap(allocstack
, STACKSIZE
) != -1);
214 ATF_TC(clone_out_of_proc
);
215 ATF_TC_HEAD(clone_out_of_proc
, tc
)
218 atf_tc_set_md_var(tc
, "descr",
219 "Checks that clone(2) fails when running out of processes");
220 atf_tc_set_md_var(tc
, "require.user", "unprivileged");
223 ATF_TC_BODY(clone_out_of_proc
, tc
)
228 ATF_REQUIRE_ERRNO(errno
, getrlimit(RLIMIT_NPROC
, &rl
) != -1);
233 ATF_REQUIRE_ERRNO(errno
, setrlimit(RLIMIT_NPROC
, &rl
) != -1);
236 rv
= __clone(dummy
, malloc(10240),
237 CLONE_VM
|CLONE_FS
|CLONE_FILES
|CLONE_SIGHAND
|SIGCHLD
, (void *)&rl
);
239 ATF_REQUIRE_EQ(rv
, -1);
240 ATF_REQUIRE_EQ(errno
, EAGAIN
);
246 ATF_TP_ADD_TC(tp
, clone_basic
);
247 ATF_TP_ADD_TC(tp
, clone_null_stack
);
248 ATF_TP_ADD_TC(tp
, clone_null_func
);
249 ATF_TP_ADD_TC(tp
, clone_out_of_proc
);
251 return atf_no_error();