server.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 <thread.h>
  12. #include <sunrpc.h>
  13. /*
  14. * Sun RPC server; for now, no reply cache
  15. */
  16. static void sunRpcProc(void*);
  17. static void sunRpcRequestThread(void*);
  18. static void sunRpcReplyThread(void*);
  19. static void sunRpcForkThread(void*);
  20. static SunProg *sunFindProg(SunSrv*, SunMsg*, SunRpc*, Channel**);
  21. typedef struct Targ Targ;
  22. struct Targ
  23. {
  24. void (*fn)(void*);
  25. void *arg;
  26. };
  27. SunSrv*
  28. sunSrv(void)
  29. {
  30. SunSrv *srv;
  31. srv = emalloc(sizeof(SunSrv));
  32. srv->chatty = 0;
  33. srv->crequest = chancreate(sizeof(SunMsg*), 16);
  34. srv->creply = chancreate(sizeof(SunMsg*), 16);
  35. srv->cthread = chancreate(sizeof(Targ), 4);
  36. proccreate(sunRpcProc, srv, SunStackSize);
  37. return srv;
  38. }
  39. void
  40. sunSrvProg(SunSrv *srv, SunProg *prog, Channel *c)
  41. {
  42. if(srv->nprog%16 == 0){
  43. srv->prog = erealloc(srv->prog, (srv->nprog+16)*sizeof(srv->prog[0]));
  44. srv->cdispatch = erealloc(srv->cdispatch, (srv->nprog+16)*sizeof(srv->cdispatch[0]));
  45. }
  46. srv->prog[srv->nprog] = prog;
  47. srv->cdispatch[srv->nprog] = c;
  48. srv->nprog++;
  49. }
  50. static void
  51. sunRpcProc(void *v)
  52. {
  53. threadcreate(sunRpcReplyThread, v, SunStackSize);
  54. threadcreate(sunRpcRequestThread, v, SunStackSize);
  55. threadcreate(sunRpcForkThread, v, SunStackSize);
  56. }
  57. static void
  58. sunRpcForkThread(void *v)
  59. {
  60. SunSrv *srv = v;
  61. Targ t;
  62. while(recv(srv->cthread, &t) == 1)
  63. threadcreate(t.fn, t.arg, SunStackSize);
  64. }
  65. void
  66. sunSrvThreadCreate(SunSrv *srv, void (*fn)(void*), void *arg)
  67. {
  68. Targ t;
  69. t.fn = fn;
  70. t.arg = arg;
  71. send(srv->cthread, &t);
  72. }
  73. static void
  74. sunRpcRequestThread(void *v)
  75. {
  76. uint8_t *p, *ep;
  77. Channel *c;
  78. SunSrv *srv = v;
  79. SunMsg *m;
  80. SunProg *pg;
  81. SunStatus ok;
  82. while((m = recvp(srv->crequest)) != nil){
  83. /* could look up in cache here? */
  84. if(srv->chatty) fprint(2, "sun msg %p count %d\n", m, m->count);
  85. m->srv = srv;
  86. p = m->data;
  87. ep = p+m->count;
  88. if(sunRpcUnpack(p, ep, &p, &m->rpc) != SunSuccess){
  89. fprint(2, "in: %.*H unpack failed\n", m->count, m->data);
  90. sunMsgDrop(m);
  91. continue;
  92. }
  93. if(srv->chatty)
  94. fprint(2, "in: %B\n", &m->rpc);
  95. if(srv->alwaysReject){
  96. if(srv->chatty)
  97. fprint(2, "\trejecting\n");
  98. sunMsgReplyError(m, SunAuthTooWeak);
  99. continue;
  100. }
  101. if(!m->rpc.iscall){
  102. sunMsgReplyError(m, SunGarbageArgs);
  103. continue;
  104. }
  105. if((pg = sunFindProg(srv, m, &m->rpc, &c)) == nil){
  106. /* sunFindProg sent error */
  107. continue;
  108. }
  109. p = m->rpc.data;
  110. ep = p+m->rpc.ndata;
  111. m->call = nil;
  112. if((ok = sunCallUnpackAlloc(pg, m->rpc.proc<<1, p, ep, &p, &m->call)) != SunSuccess){
  113. sunMsgReplyError(m, ok);
  114. continue;
  115. }
  116. m->call->rpc = m->rpc;
  117. if(srv->chatty)
  118. fprint(2, "\t%C\n", m->call);
  119. m->pg = pg;
  120. sendp(c, m);
  121. }
  122. }
  123. static SunProg*
  124. sunFindProg(SunSrv *srv, SunMsg *m, SunRpc *rpc, Channel **pc)
  125. {
  126. int i, vlo, vhi;
  127. SunProg *pg;
  128. vlo = 0x7fffffff;
  129. vhi = -1;
  130. for(i=0; i<srv->nprog; i++){
  131. pg = srv->prog[i];
  132. if(pg->prog != rpc->prog)
  133. continue;
  134. if(pg->vers == rpc->vers){
  135. *pc = srv->cdispatch[i];
  136. return pg;
  137. }
  138. /* right program, wrong version: record range */
  139. if(pg->vers < vlo)
  140. vlo = pg->vers;
  141. if(pg->vers > vhi)
  142. vhi = pg->vers;
  143. }
  144. if(vhi == -1){
  145. if(srv->chatty)
  146. fprint(2, "\tprogram %ud unavailable\n", rpc->prog);
  147. sunMsgReplyError(m, SunProgUnavail);
  148. }else{
  149. /* putting these in rpc is a botch */
  150. rpc->low = vlo;
  151. rpc->high = vhi;
  152. if(srv->chatty)
  153. fprint(2, "\tversion %ud unavailable; have %d-%d\n", rpc->vers, vlo, vhi);
  154. sunMsgReplyError(m, SunProgMismatch);
  155. }
  156. return nil;
  157. }
  158. static void
  159. sunRpcReplyThread(void *v)
  160. {
  161. SunMsg *m;
  162. SunSrv *srv = v;
  163. while((m = recvp(srv->creply)) != nil){
  164. /* could record in cache here? */
  165. sendp(m->creply, m);
  166. }
  167. }
  168. int
  169. sunMsgReplyError(SunMsg *m, SunStatus error)
  170. {
  171. uint8_t *p, *bp, *ep;
  172. int n;
  173. m->rpc.status = error;
  174. m->rpc.iscall = 0;
  175. m->rpc.verf.flavor = SunAuthNone;
  176. m->rpc.data = nil;
  177. m->rpc.ndata = 0;
  178. if(m->srv->chatty)
  179. fprint(2, "out: %B\n", &m->rpc);
  180. n = sunRpcSize(&m->rpc);
  181. bp = emalloc(n);
  182. ep = bp+n;
  183. p = bp;
  184. if(sunRpcPack(p, ep, &p, &m->rpc) < 0){
  185. fprint(2, "sunRpcPack failed\n");
  186. sunMsgDrop(m);
  187. return 0;
  188. }
  189. if(p != ep){
  190. fprint(2, "sunMsgReplyError: rpc sizes didn't work out\n");
  191. sunMsgDrop(m);
  192. return 0;
  193. }
  194. free(m->data);
  195. m->data = bp;
  196. m->count = n;
  197. sendp(m->srv->creply, m);
  198. return 0;
  199. }
  200. int
  201. sunMsgReply(SunMsg *m, SunCall *c)
  202. {
  203. int n1, n2;
  204. uint8_t *bp, *p, *ep;
  205. c->type = m->call->type+1;
  206. c->rpc.iscall = 0;
  207. c->rpc.prog = m->rpc.prog;
  208. c->rpc.vers = m->rpc.vers;
  209. c->rpc.proc = m->rpc.proc;
  210. c->rpc.xid = m->rpc.xid;
  211. if(m->srv->chatty){
  212. fprint(2, "out: %B\n", &c->rpc);
  213. fprint(2, "\t%C\n", c);
  214. }
  215. n1 = sunRpcSize(&c->rpc);
  216. n2 = sunCallSize(m->pg, c);
  217. bp = emalloc(n1+n2);
  218. ep = bp+n1+n2;
  219. p = bp;
  220. if(sunRpcPack(p, ep, &p, &c->rpc) != SunSuccess){
  221. fprint(2, "sunRpcPack failed\n");
  222. return sunMsgDrop(m);
  223. }
  224. if(sunCallPack(m->pg, p, ep, &p, c) != SunSuccess){
  225. fprint(2, "pg->pack failed\n");
  226. return sunMsgDrop(m);
  227. }
  228. if(p != ep){
  229. fprint(2, "sunMsgReply: sizes didn't work out\n");
  230. return sunMsgDrop(m);
  231. }
  232. free(m->data);
  233. m->data = bp;
  234. m->count = n1+n2;
  235. sendp(m->srv->creply, m);
  236. return 0;
  237. }
  238. int
  239. sunMsgDrop(SunMsg *m)
  240. {
  241. free(m->data);
  242. free(m->call);
  243. memset(m, 0xFB, sizeof *m);
  244. free(m);
  245. return 0;
  246. }