• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define _GNU_SOURCE
2 #include <assert.h>
3 #include <errno.h>
4 #include <sched.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/mman.h>
8 #include <sys/wait.h>
9 #include <unistd.h>
10 // Based on a test by Steven Stewart-Gallus, see 342040
fork_routine(void * arg)11 int fork_routine(void *arg)
12 {
13         write(1, "fork_routine\n", 13);
14 	_Exit(EXIT_SUCCESS);
15 }
16 
main(void)17 int main(void)
18 {
19 	long page_size = sysconf(_SC_PAGE_SIZE);
20 	assert(page_size != -1);
21 
22 	/* We need an extra page for signals */
23 	long stack_size = sysconf(_SC_THREAD_STACK_MIN) + page_size;
24 	assert(stack_size != -1);
25 
26 	size_t stack_and_guard_size = page_size + stack_size + page_size;
27 	void *child_stack = mmap(
28 	    NULL, stack_and_guard_size, PROT_READ | PROT_WRITE,
29 	    MAP_PRIVATE | MAP_ANONYMOUS | MAP_GROWSDOWN, -1, 0);
30 	if (NULL == child_stack) {
31 		perror("mmap");
32 		return EXIT_FAILURE;
33 	}
34 
35 	/* Guard pages are shared between the stacks */
36 	if (-1 == mprotect((char *)child_stack, page_size, PROT_NONE)) {
37 		perror("mprotect");
38 		return EXIT_FAILURE;
39 	}
40 
41 	if (-1 == mprotect((char *)child_stack + page_size + stack_size,
42 	                   page_size, PROT_NONE)) {
43 		perror("mprotect");
44 		return EXIT_FAILURE;
45 	}
46 
47 	void *stack_start = (char *)child_stack + page_size + stack_size;
48         if (0)
49            printf("stack_start %p page_size %d stack_size %d\n",
50                   stack_start, (int)page_size, (int)stack_size);
51         write(1, "parent before clone\n", 20);
52 	pid_t child =
53 	    clone(fork_routine, stack_start,
54 	          SIGCHLD | CLONE_VFORK | CLONE_VM, NULL);
55         write(1, "parent after clone\n", 19);
56 	if (-1 == child) {
57 		perror("clone");
58 		return EXIT_FAILURE;
59 	}
60 
61 	for (;;) {
62 		int xx;
63 		switch (waitpid(child, &xx, 0)) {
64 		case -1:
65 			switch (errno) {
66 			case EINTR:
67 				continue;
68 			default:
69 				perror("waitpid");
70 				return EXIT_FAILURE;
71 			}
72 
73 		default:
74 			return EXIT_SUCCESS;
75 		}
76 	}
77 }
78