_post.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <fcall.h>
  4. #include <thread.h>
  5. #include <9p.h>
  6. #include <auth.h>
  7. #include "post.h"
  8. Postcrud*
  9. _post1(Srv *s, char *name, char *mtpt, int flag)
  10. {
  11. Postcrud *p;
  12. p = emalloc9p(sizeof *p);
  13. if(!s->nopipe){
  14. if(pipe(p->fd) < 0)
  15. sysfatal("pipe: %r");
  16. s->infd = s->outfd = p->fd[1];
  17. s->srvfd = p->fd[0];
  18. }
  19. if(name)
  20. if(postfd(name, s->srvfd) < 0)
  21. sysfatal("postfd %s: %r", name);
  22. p->s = s;
  23. p->mtpt = mtpt;
  24. p->flag = flag;
  25. return p;
  26. }
  27. void
  28. _post2(void *v)
  29. {
  30. Srv *s;
  31. s = v;
  32. if(!s->leavefdsopen){
  33. rfork(RFNOTEG);
  34. rendezvous((ulong)s, 0);
  35. close(s->srvfd);
  36. }
  37. srv(s);
  38. }
  39. void
  40. _post3(Postcrud *p)
  41. {
  42. /*
  43. * Normally the server is posting as the last thing it does
  44. * before exiting, so the correct thing to do is drop into
  45. * a different fd space and close the 9P server half of the
  46. * pipe before trying to mount the kernel half. This way,
  47. * if the file server dies, we don't have a ref to the 9P server
  48. * half of the pipe. Then killing the other procs will drop
  49. * all the refs on the 9P server half, and the mount will fail.
  50. * Otherwise the mount hangs forever.
  51. *
  52. * Libthread in general and acme win in particular make
  53. * it hard to make this fd bookkeeping work out properly,
  54. * so leaveinfdopen is a flag that win sets to opt out of this
  55. * safety net.
  56. */
  57. if(!p->s->leavefdsopen){
  58. rfork(RFFDG);
  59. rendezvous((ulong)p->s, 0);
  60. close(p->s->infd);
  61. if(p->s->infd != p->s->outfd)
  62. close(p->s->outfd);
  63. }
  64. if(p->mtpt){
  65. if(amount(p->s->srvfd, p->mtpt, p->flag, "") == -1)
  66. sysfatal("mount %s: %r", p->mtpt);
  67. }else
  68. close(p->s->srvfd);
  69. free(p);
  70. }