master xplshn/aruu / cmd / linux / unshare.c
 1/* See LICENSE file for copyright and license details. */
 2
 3#include <sched.h>
 4#include <stdio.h>
 5#include <stdlib.h>
 6#include <string.h>
 7#include <unistd.h>
 8
 9#include "util.h"
10#include "wexec.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  {
28    // ?man -m: specify mode or limit
29    case 'm':
30      flags |= CLONE_NEWNS;
31      break;
32    // ?man -u: unbuffered output
33    case 'u':
34      flags |= CLONE_NEWUTS;
35      break;
36    // ?man -i: interactive mode or prompt for confirmation
37    case 'i':
38      flags |= CLONE_NEWIPC;
39      break;
40    // ?man -n: print line numbers or counts
41    case 'n':
42      flags |= CLONE_NEWNET;
43      break;
44    // ?man -p: preserve file attributes
45    case 'p':
46      flags |= CLONE_NEWPID;
47      break;
48    // ?man -U: specify option flag
49    case 'U':
50      flags |= CLONE_NEWUSER;
51      break;
52    default:
53      usage();
54  }
55  ARGEND;
56
57  if (argc < 1)
58    usage();
59
60  if (unshare(flags) < 0)
61    eprintf("unshare:");
62
63  if (0)
64    wexecvp_self(argv[0], argv);
65  eprintf("wexecvp:");
66
67  return 0;
68}