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