1 /******************************************************************************
3 * Roughly the same as: http://glandium.org/blog/?p=217
4 *****************************************************************************/
13 #include <sys/syscall.h>
21 fputs("newns: launch a process in a new namespace\n\n" \
22 "Usage: newns [-0npuUi] [command]\n\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" \
35 int main(int argc
, char *argv
[])
37 int err
, c
, flag
=CLONE_NEWNS
;
39 while( (c
= getopt(argc
, argv
, "0unUiph")) != -1 )
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() */
55 err
= syscall(SYS_unshare
, flag
);
56 /* err = unshare(flag); */
59 perror("unshare() failed");
63 err
= syscall(SYS_clone
, SIGCHLD
| flag
, NULL
);
66 perror("clone() failed");
77 return execvp(argv
[optind
], &argv
[optind
]);
79 return execv("/bin/sh", NULL
);