9pvpxy.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * This file is part of the Harvey operating system. It is subject to the
  3. * license terms of the GNU GPL v2 in LICENSE.gpl found in the top-level
  4. * directory of this distribution and at http://www.gnu.org/licenses/gpl-2.0.txt
  5. *
  6. * No part of Harvey operating system, including this file, may be copied,
  7. * modified, propagated, or distributed except according to the terms
  8. * contained in the LICENSE.gpl file.
  9. */
  10. // 9pvpxy.c A 9p proxy over virtqueue. Read 9p messages from standard input, send them to the virtqueue.
  11. // Read responses from the virtqueue, send them to the standard output.
  12. // The operation is single-threaded: write to virtqueue is expected to complete before the response can be read.
  13. #include <u.h>
  14. #include <libc.h>
  15. #include <fcall.h>
  16. #define BUFLEN 256*1024
  17. static uint8_t ibuf[BUFLEN], obuf[BUFLEN];
  18. void
  19. exerr(char *s)
  20. {
  21. char err[200];
  22. err[0] = 0;
  23. errstr(err, 199);
  24. fprint(2, "%s: %s\n", s, err);
  25. exits(err);
  26. }
  27. void
  28. exerr2(char *s, char *err)
  29. {
  30. fprint(2, "%s: %s\n", s, err);
  31. exits(err);
  32. }
  33. void
  34. main(int argc, char *argv[])
  35. {
  36. if(argc == 0) {
  37. fprint(2, "usage: %s file\n", argv[0]);
  38. exits("usage");
  39. }
  40. int fd = open(argv[1], ORDWR);
  41. if(fd < 0)
  42. exerr(argv[0]);
  43. while(1) {
  44. int rc = read9pmsg(0, obuf, BUFLEN);
  45. if(rc < 0)
  46. {
  47. close(fd);
  48. exerr(argv[0]);
  49. }
  50. int rc2 = write(fd, obuf, rc);
  51. if(rc2 < rc)
  52. {
  53. close(fd);
  54. exerr2(argv[0], "short write to vq");
  55. }
  56. rc = read(fd, ibuf, BUFLEN);
  57. if(rc < BUFLEN)
  58. {
  59. close(fd);
  60. exerr(argv[0]);
  61. }
  62. uint32_t ml = GBIT32(ibuf);
  63. rc = write(1, ibuf, ml);
  64. if(rc < 0)
  65. {
  66. close(fd);
  67. exerr2(argv[0], "short write to stdout");
  68. }
  69. }
  70. }