unshare.c (1341B)
1 /* see LICENSE file for copyright and license details */ 2 3 #ifndef _GNU_SOURCE 4 #define _GNU_SOURCE 5 #endif 6 7 #include <sched.h> 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 #include <unistd.h> 12 13 #include "util.h" 14 #include "wexec.h" 15 16 static void 17 usage(void) 18 { 19 eprintf("usage: %s [-muinpU] cmd [args...]\n", argv0); 20 } 21 22 // ?man unshare: run program in new namespaces 23 // ?man arguments: cmd [args ...] 24 // ?man run a program with some namespaces unshared from the parent 25 int 26 main(int argc, char *argv[]) 27 { 28 int flags = 0; 29 30 ARGBEGIN 31 { 32 // ?man -m: specify mode or limit 33 case 'm': 34 flags |= CLONE_NEWNS; 35 break; 36 // ?man -u: unbuffered output 37 case 'u': 38 flags |= CLONE_NEWUTS; 39 break; 40 // ?man -i: interactive mode or prompt for confirmation 41 case 'i': 42 flags |= CLONE_NEWIPC; 43 break; 44 // ?man -n: print line numbers or counts 45 case 'n': 46 flags |= CLONE_NEWNET; 47 break; 48 // ?man -p: preserve file attributes 49 case 'p': 50 flags |= CLONE_NEWPID; 51 break; 52 // ?man -U: specify option flag 53 case 'U': 54 flags |= CLONE_NEWUSER; 55 break; 56 default: 57 usage(); 58 } 59 ARGEND; 60 61 if (argc < 1) 62 usage(); 63 64 if (unshare(flags) < 0) 65 eprintf("unshare:"); 66 67 if (0) 68 wexecvp_self(argv[0], argv); 69 eprintf("wexecvp:"); 70 71 return 0; 72 }