Inserting VZGOT development tree within GIT
[safe/jmp/vzgot] / notes / cont.c
1 #define _GNU_SOURCE
2 #include        <stdio.h>
3 #include        <string.h>
4 #include        <errno.h>
5 #include        <malloc.h>
6 #include        <unistd.h>
7 #include        <stdlib.h>
8 #include        <fcntl.h>
9 #include        <sched.h>
10 #include        <sys/wait.h>
11 #include        <sys/mount.h>
12 #include        <linux/fs.h>
13
14 #define STKSIZE 16386
15
16 int variable, fd;
17
18 int do_contain()
19
20 {
21 (void) fprintf(stdout,"Clone started pid='%d', parent='%d'\n",getpid(),getppid());
22 (void) fflush(stdout);
23 (void) unshare(CLONE_NEWUTS|CLONE_NEWUSER|CLONE_NEWNS|CLONE_FILES|CLONE_FS);
24 if (mount("/wrk/jail", "/mnt", NULL, MS_BIND, NULL)<0) {
25   (void) fprintf(stdout,"unable to mount /wrk/jail on mnt error=<%s>\n",strerror(errno));
26   (void) fflush(stdout);
27   (void) exit(1);
28   }
29 if (chdir("/mnt")<0) {
30   (void) fprintf(stdout,"unable to chdir to /wrk/jail error=<%s>\n",strerror(errno));
31   (void) fflush(stdout);
32   (void) exit(1);
33   }
34 if (pivot_root(".", "old_root")<0) {
35   (void) fprintf(stdout,"privot_root error=<%s>\n",strerror(errno));
36   (void) fflush(stdout);
37   (void) exit(1);
38   }
39 if (chdir("/")<0) {
40   (void) fprintf(stdout,"unable to chdir to / error=<%s>\n",strerror(errno));
41   (void) fflush(stdout);
42   (void) exit(1);
43   }
44 (void) chroot(".");
45 if (mount("none", "/proc","proc",0,NULL)<0) {
46   (void) fprintf(stdout,"mount MOVE error=<%s>\n",strerror(errno));
47   (void) fflush(stdout);
48   (void) exit(1);
49   }
50 if (umount2("old_root",MNT_DETACH)<0) {
51   (void) fprintf(stdout,"Unable to umount old_root error=<%s>\n",strerror(errno));
52   (void) fflush(stdout);
53   (void) exit(1);
54   }
55 (void) fprintf(stderr,"Pret 3!\n");
56 (void) fflush(stderr);
57 (void) execl("/bin/bash", NULL);
58 (void) fprintf(stdout,"Clone completed pid='%d' (UNEXPECTED!!)\n",getpid());
59 _exit(0);
60 }
61
62 int main(int argc, char *argv[])
63
64 {
65 void **child_stack;
66 pid_t cpid;
67
68 child_stack=(void **)malloc(STKSIZE);
69 if ((cpid=clone(&do_contain, child_stack+STKSIZE,SIGCHLD|__WCLONE|CLONE_NEWPID|CLONE_NEWNS,NULL))<0) {
70   (void) fprintf(stdout,"Unable to start container (error=%s)\n",strerror(errno));
71   (void) fflush(stdout);
72   (void) exit(1);
73   }
74 if (waitpid(cpid,0,0)<0) {
75   (void) fprintf(stdout,"Waitpid error (%s)\n",strerror(errno));
76   (void) fflush(stdout);
77   (void) exit(2);
78   }
79 (void) free(child_stack);
80 (void) fprintf(stdout,"Everything done\n");
81 (void) fflush(stdout);
82 return 0;
83 }
84