conn.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 <venti.h>
  12. #include "queue.h"
  13. int chattyventi;
  14. VtConn*
  15. vtconn(int infd, int outfd)
  16. {
  17. VtConn *z;
  18. NetConnInfo *nci;
  19. z = vtmallocz(sizeof(VtConn));
  20. z->tagrend.l = &z->lk;
  21. z->rpcfork.l = &z->lk;
  22. z->infd = infd;
  23. z->outfd = outfd;
  24. z->part = packetalloc();
  25. nci = getnetconninfo(nil, infd);
  26. if(nci == nil)
  27. snprint(z->addr, sizeof z->addr, "/dev/fd/%d", infd);
  28. else{
  29. strecpy(z->addr, z->addr+sizeof z->addr, nci->raddr);
  30. freenetconninfo(nci);
  31. }
  32. return z;
  33. }
  34. void
  35. vtfreeconn(VtConn *z)
  36. {
  37. vthangup(z);
  38. qlock(&z->lk);
  39. /*
  40. * Wait for send and recv procs to notice
  41. * the hangup and clear out the queues.
  42. */
  43. while(z->readq || z->writeq){
  44. if(z->readq)
  45. _vtqhangup(z->readq);
  46. if(z->writeq)
  47. _vtqhangup(z->writeq);
  48. rsleep(&z->rpcfork);
  49. }
  50. packetfree(z->part);
  51. vtfree(z->version);
  52. vtfree(z->sid);
  53. qunlock(&z->lk);
  54. vtfree(z);
  55. }