unshare.c 772 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* See LICENSE file for copyright and license details. */
  2. #include <sched.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include "util.h"
  8. static void
  9. usage(void)
  10. {
  11. eprintf("usage: %s [-muinpU] cmd [args...]\n", argv0);
  12. }
  13. int
  14. main(int argc, char *argv[])
  15. {
  16. int flags = 0;
  17. ARGBEGIN {
  18. case 'm':
  19. flags |= CLONE_NEWNS;
  20. break;
  21. case 'u':
  22. flags |= CLONE_NEWUTS;
  23. break;
  24. case 'i':
  25. flags |= CLONE_NEWIPC;
  26. break;
  27. case 'n':
  28. flags |= CLONE_NEWNET;
  29. break;
  30. case 'p':
  31. flags |= CLONE_NEWPID;
  32. break;
  33. case 'U':
  34. flags |= CLONE_NEWUSER;
  35. break;
  36. default:
  37. usage();
  38. } ARGEND;
  39. if (argc < 1)
  40. usage();
  41. if (unshare(flags) < 0)
  42. eprintf("unshare:");
  43. if (execvp(argv[0], argv) < 0)
  44. eprintf("execvp:");
  45. return 0;
  46. }