ro.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /* Copyright (c) 2004 Russ Cox */
  10. #include <u.h>
  11. #include <libc.h>
  12. #include <venti.h>
  13. #include <thread.h>
  14. #include <libsec.h>
  15. #ifndef _UNISTD_H_
  16. #endif
  17. VtConn *z;
  18. int verbose;
  19. enum
  20. {
  21. STACK = 8192
  22. };
  23. void
  24. usage(void)
  25. {
  26. fprint(2, "usage: venti/ro [-v] [-a address] [-h address]\n");
  27. threadexitsall("usage");
  28. }
  29. void
  30. readthread(void *v)
  31. {
  32. char err[ERRMAX];
  33. VtReq *r;
  34. uint8_t *buf;
  35. int n;
  36. r = v;
  37. buf = vtmalloc(r->tx.count);
  38. if((n=vtread(z, r->tx.score, r->tx.blocktype, buf, r->tx.count)) < 0){
  39. r->rx.msgtype = VtRerror;
  40. rerrstr(err, sizeof err);
  41. r->rx.error = vtstrdup(err);
  42. free(buf);
  43. }else{
  44. r->rx.data = packetforeign(buf, n, free, buf);
  45. }
  46. if(verbose)
  47. fprint(2, "-> %F\n", &r->rx);
  48. vtrespond(r);
  49. }
  50. void
  51. threadmain(int argc, char **argv)
  52. {
  53. VtReq *r;
  54. VtSrv *srv;
  55. char *address, *ventiaddress;
  56. fmtinstall('F', vtfcallfmt);
  57. fmtinstall('V', vtscorefmt);
  58. address = "tcp!*!venti";
  59. ventiaddress = nil;
  60. ARGBEGIN{
  61. case 'v':
  62. verbose++;
  63. break;
  64. case 'a':
  65. address = EARGF(usage());
  66. break;
  67. case 'h':
  68. ventiaddress = EARGF(usage());
  69. break;
  70. default:
  71. usage();
  72. }ARGEND
  73. if((z = vtdial(ventiaddress)) == nil)
  74. sysfatal("vtdial %s: %r", ventiaddress);
  75. if(vtconnect(z) < 0)
  76. sysfatal("vtconnect: %r");
  77. srv = vtlisten(address);
  78. if(srv == nil)
  79. sysfatal("vtlisten %s: %r", address);
  80. while((r = vtgetreq(srv)) != nil){
  81. r->rx.msgtype = r->tx.msgtype+1;
  82. if(verbose)
  83. fprint(2, "<- %F\n", &r->tx);
  84. switch(r->tx.msgtype){
  85. case VtTping:
  86. break;
  87. case VtTgoodbye:
  88. break;
  89. case VtTread:
  90. threadcreate(readthread, r, 16384);
  91. continue;
  92. case VtTwrite:
  93. r->rx.error = vtstrdup("read-only server");
  94. r->rx.msgtype = VtRerror;
  95. break;
  96. case VtTsync:
  97. break;
  98. }
  99. if(verbose)
  100. fprint(2, "-> %F\n", &r->rx);
  101. vtrespond(r);
  102. }
  103. threadexitsall(nil);
  104. }