newns.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <auth.h>
  12. void
  13. usage(void)
  14. {
  15. fprint(2, "usage: newns [-ad] [-n namespace] [cmd [args...]]\n");
  16. exits("usage");
  17. }
  18. static int
  19. rooted(char *s)
  20. {
  21. if(s[0] == '/')
  22. return 1;
  23. if(s[0] == '.' && s[1] == '/')
  24. return 1;
  25. if(s[0] == '.' && s[1] == '.' && s[2] == '/')
  26. return 1;
  27. return 0;
  28. }
  29. void
  30. main(int argc, char **argv)
  31. {
  32. extern int newnsdebug;
  33. char *defargv[] = { "/bin/rc", "-i", nil };
  34. char *nsfile, err[ERRMAX];
  35. int add;
  36. rfork(RFNAMEG);
  37. add = 0;
  38. nsfile = "/lib/namespace";
  39. ARGBEGIN{
  40. case 'a':
  41. add = 1;
  42. break;
  43. case 'd':
  44. newnsdebug = 1;
  45. break;
  46. case 'n':
  47. nsfile = ARGF();
  48. break;
  49. default:
  50. usage();
  51. break;
  52. }ARGEND
  53. if(argc == 0)
  54. argv = defargv;
  55. if (add)
  56. addns(getuser(), nsfile);
  57. else
  58. newns(getuser(), nsfile);
  59. exec(argv[0], argv);
  60. if(!rooted(argv[0])){
  61. rerrstr(err, sizeof err);
  62. exec(smprint("/bin/%s", argv[0]), argv);
  63. errstr(err, sizeof err);
  64. }
  65. sysfatal("exec: %s: %r", argv[0]);
  66. }