srvfs.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. static void
  12. usage(void)
  13. {
  14. fprint(2, "usage: %s [-dR] [-p perm] [-P patternfile] [-e exportfs] srvname path\n", argv0);
  15. exits("usage");
  16. }
  17. void
  18. main(int argc, char **argv)
  19. {
  20. char *ename, *arglist[16], **argp;
  21. int n, fd, pipefd[2];
  22. char buf[64];
  23. int perm = 0600;
  24. argp = arglist;
  25. ename = "/bin/exportfs";
  26. *argp++ = "exportfs";
  27. ARGBEGIN{
  28. default:
  29. usage();
  30. case 'd':
  31. *argp++ = "-d";
  32. break;
  33. case 'e':
  34. ename = EARGF(usage());
  35. break;
  36. case 'p':
  37. perm = strtol(EARGF(usage()), 0, 8);
  38. break;
  39. case 'P':
  40. *argp++ = "-P";
  41. *argp++ = EARGF(usage());
  42. break;
  43. case 'R':
  44. *argp++ = "-R";
  45. break;
  46. }ARGEND
  47. *argp = 0;
  48. if(argc != 2)
  49. usage();
  50. if(pipe(pipefd) < 0){
  51. fprint(2, "can't pipe: %r\n");
  52. exits("pipe");
  53. }
  54. switch(rfork(RFPROC|RFNOWAIT|RFNOTEG|RFFDG)){
  55. case -1:
  56. fprint(2, "can't rfork: %r\n");
  57. exits("rfork");
  58. case 0:
  59. dup(pipefd[0], 0);
  60. dup(pipefd[0], 1);
  61. close(pipefd[0]);
  62. close(pipefd[1]);
  63. exec(ename, arglist);
  64. fprint(2, "can't exec exportfs: %r\n");
  65. exits("exec");
  66. default:
  67. break;
  68. }
  69. close(pipefd[0]);
  70. if(fprint(pipefd[1], "%s", argv[1]) < 0){
  71. fprint(2, "can't write pipe: %r\n");
  72. exits("write");
  73. }
  74. n = read(pipefd[1], buf, sizeof buf-1);
  75. if(n < 0){
  76. fprint(2, "can't read pipe: %r\n");
  77. exits("read");
  78. }
  79. buf[n] = 0;
  80. if(n != 2 || strcmp(buf, "OK") != 0){
  81. fprint(2, "not OK (%d): %s\n", n, buf);
  82. exits("OK");
  83. }
  84. if(argv[0][0] == '/')
  85. strecpy(buf, buf+sizeof buf, argv[0]);
  86. else
  87. snprint(buf, sizeof buf, "/srv/%s", argv[0]);
  88. fd = create(buf, OWRITE, perm);
  89. if(fd < 0){
  90. fprint(2, "can't create %s: %r\n", buf);
  91. exits("create");
  92. }
  93. fprint(fd, "%d", pipefd[1]);
  94. close(fd);
  95. close(pipefd[1]);
  96. exits(0);
  97. }