srvfs.c 1.7 KB

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