-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclone.c
76 lines (61 loc) · 1.61 KB
/
clone.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#define _GNU_SOURCE
#include <string.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sched.h>
#include <sys/mman.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>
static int childFunc(void *arg)
{
printf("Child: PID=%ld PPID=%ld\n", (long)getpid(), (long)getppid());
while(true){
void * x = malloc(0x10);
free(x);
}
return 0;
}
int guest_clone()
{
const int STACK_SIZE = 1 << 14; /* Stack size for cloned child */
char *stack; /* Start of stack buffer area */
char *stackTop; /* End of stack buffer area */
int flags = 0; /* Flags for cloning child */
int status;
pid_t pid = -1;
int args;
printf("Parent: PID=%ld PPID=%ld\n", (long)getpid(), (long)getppid());
flags |= CLONE_VM;
// flags |= CLONE_FILES;
// flags |= CLONE_FS;
// flags |= CLONE_SIGHAND;
/* Allocate stack for child */
stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
if (stack == MAP_FAILED)
return 111;
stackTop = stack + STACK_SIZE; /* Assume stack grows downward */
printf("child new stack %p in guest_clone\n", stackTop);
if (clone(childFunc, stackTop, flags | SIGCHLD, &args) == -1)
return 112;
/* Parent falls through to here. Wait for child; __WCLONE option is
required for child notifying with signal other than SIGCHLD. */
while(true){
void * x = malloc(0x10);
free(x);
}
pid = waitpid(-1, &status, __WALL);
if (pid == -1)
return 0;
printf(" Child PID=%ld\n", (long)pid);
return 0;
}
int main(){
guest_clone();
return 0;
}