fs.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <fcall.h>
  12. #include <thread.h>
  13. #include <9p.h>
  14. #include "dat.h"
  15. Channel *fschan;
  16. Channel *writechan;
  17. static File *devcons, *devnew;
  18. static void
  19. fsread(Req *r)
  20. {
  21. Fsevent e;
  22. if(r->fid->file == devnew){
  23. if(r->fid->aux==nil){
  24. respond(r, "phase error");
  25. return;
  26. }
  27. readstr(r, r->fid->aux);
  28. respond(r, nil);
  29. return;
  30. }
  31. assert(r->fid->file == devcons);
  32. e.type = 'r';
  33. e.r = r;
  34. send(fschan, &e);
  35. }
  36. static void
  37. fsflush(Req *r)
  38. {
  39. Fsevent e;
  40. e.type = 'f';
  41. e.r = r;
  42. send(fschan, &e);
  43. }
  44. static void
  45. fswrite(Req *r)
  46. {
  47. static Event *e[4];
  48. Event *ep;
  49. int i, j, ei, nb, wid, pid;
  50. Rune rune;
  51. char *s;
  52. char tmp[UTFmax], *t;
  53. static int n, partial;
  54. if(r->fid->file == devnew){
  55. if(r->fid->aux){
  56. respond(r, "already created a window");
  57. return;
  58. }
  59. s = emalloc(r->ifcall.count+1);
  60. memmove(s, r->ifcall.data, r->ifcall.count);
  61. s[r->ifcall.count] = 0;
  62. pid = strtol(s, &t, 0);
  63. if(*t==' ')
  64. t++;
  65. i = newpipewin(pid, t);
  66. free(s);
  67. s = emalloc(32);
  68. sprint(s, "%lud", (uint32_t)i);
  69. r->fid->aux = s;
  70. r->ofcall.count = r->ifcall.count;
  71. respond(r, nil);
  72. return;
  73. }
  74. assert(r->fid->file == devcons);
  75. if(e[0] == nil){
  76. for(i=0; i<nelem(e); i++){
  77. e[i] = emalloc(sizeof(Event));
  78. e[i]->c1 = 'S';
  79. }
  80. }
  81. ep = e[n];
  82. n = (n+1)%nelem(e);
  83. assert(r->ifcall.count <= 8192); /* is this guaranteed by lib9p? */
  84. nb = r->ifcall.count;
  85. memmove(ep->b+partial, r->ifcall.data, nb);
  86. nb += partial;
  87. ep->b[nb] = '\0';
  88. if(strlen(ep->b) < nb){ /* nulls in data */
  89. t = ep->b;
  90. for(i=j=0; i<nb; i++)
  91. if(ep->b[i] != '\0')
  92. t[j++] = ep->b[i];
  93. nb = j;
  94. t[j] = '\0';
  95. }
  96. ei = nb>8192? 8192 : nb;
  97. /* process bytes into runes, transferring terminal partial runes into next buffer */
  98. for(i=j=0; i<ei && fullrune(ep->b+i, ei-i); i+=wid,j++)
  99. wid = chartorune(&rune, ep->b+i);
  100. memmove(tmp, ep->b+i, nb-i);
  101. partial = nb-i;
  102. ep->nb = i;
  103. ep->nr = j;
  104. ep->b[i] = '\0';
  105. if(i != 0){
  106. sendp(win->cevent, ep);
  107. recvp(writechan);
  108. }
  109. partial = nb-i;
  110. memmove(e[n]->b, tmp, partial);
  111. r->ofcall.count = r->ifcall.count;
  112. respond(r, nil);
  113. }
  114. void
  115. fsdestroyfid(Fid *fid)
  116. {
  117. if(fid->aux)
  118. free(fid->aux);
  119. }
  120. Srv fs = {
  121. .read= fsread,
  122. .write= fswrite,
  123. .flush= fsflush,
  124. .destroyfid= fsdestroyfid,
  125. .leavefdsopen= 1,
  126. };
  127. void
  128. mountcons(void)
  129. {
  130. fschan = chancreate(sizeof(Fsevent), 0);
  131. writechan = chancreate(sizeof(void*), 0);
  132. fs.tree = alloctree("win", "win", DMDIR|0555, nil);
  133. devcons = createfile(fs.tree->root, "cons", "win", 0666, nil);
  134. if(devcons == nil)
  135. sysfatal("creating /dev/cons: %r");
  136. devnew = createfile(fs.tree->root, "wnew", "win", 0666, nil);
  137. if(devnew == nil)
  138. sysfatal("creating /dev/wnew: %r");
  139. threadpostmountsrv(&fs, nil, "/dev", MBEFORE);
  140. }