Initial commit.
[hondza-y36pr2.git] / newns / newns.c
blob9491b5f8dc60e8532b9e5b3bf4a7a7dfbef80ee2
1 /******************************************************************************
2 * newns.c
3 * Roughly the same as: http://glandium.org/blog/?p=217
4 *****************************************************************************/
6 #include <sched.h>
7 #include <syscall.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <signal.h>
13 #include <sys/syscall.h>
14 #include <sys/wait.h>
16 #include <getopt.h>
19 void help()
21 fputs("newns: launch a process in a new namespace\n\n" \
22 "Usage: newns [-0npuUi] [command]\n\n" \
23 "Options:\n" \
24 "\t -0\t\treset previous (including default CLONE_NEWNS)\n" \
25 "\t -n\t\t[CLONE_NEWNS] new mount namespace; the default\n" \
26 "\t -p\t\t[CLONE_NEWPID] new PID namespace\n" \
27 "\t -U\t\t[CLONE_NEWUTS] new UTS namespace\n" \
28 "\t -u\t\t[CLONE_NEWUSER] new USER namespace\n" \
29 "\t -i\t\t[CLONE_NEWIPC] new IPC namespace\n\n" \
30 "Default namespace is mount namespace (CLONE_NEWNS); default command is a new shell (/bin/sh)\n\n" \
31 , stderr);
35 int main(int argc, char *argv[])
37 int err, c, flag=CLONE_NEWNS;
39 while( (c = getopt(argc, argv, "0unUiph")) != -1 )
41 switch(c)
43 case '0': flag = 0; break;
44 case 'n': flag |= CLONE_NEWNS; break;
45 case 'u': flag |= CLONE_NEWUSER; break;
46 case 'U': flag |= CLONE_NEWUTS; break;
47 case 'p': flag |= CLONE_NEWPID; break;
48 case 'i': flag |= CLONE_NEWIPC; break;
49 case 'h': help(); exit(0); break;
50 case '?': help(); exit(1); break;
51 } /* switch argument */
52 } /* while getopt() */
54 #if 0
55 err = syscall(SYS_unshare, flag);
56 /* err = unshare(flag); */
57 if(-1 == err)
59 perror("unshare() failed");
60 exit(1);
62 #else
63 err = syscall(SYS_clone, SIGCHLD | flag, NULL);
64 if(-1 == err)
66 perror("clone() failed");
67 exit(1);
69 else if(0 != err)
71 wait(NULL);
72 return 0;
74 #endif
76 if(optind < argc)
77 return execvp(argv[optind], &argv[optind]);
79 return execv("/bin/sh", NULL);