1 056fe1ba 2003-11-23 devnull #include <u.h>
2 056fe1ba 2003-11-23 devnull #include <libc.h>
3 056fe1ba 2003-11-23 devnull #include <venti.h>
4 056fe1ba 2003-11-23 devnull #include <thread.h>
5 056fe1ba 2003-11-23 devnull #include "queue.h"
9 056fe1ba 2003-11-23 devnull STACK = 8192,
12 056fe1ba 2003-11-23 devnull typedef struct VtSconn VtSconn;
13 056fe1ba 2003-11-23 devnull struct VtSconn
16 056fe1ba 2003-11-23 devnull char dir[NETPATHLEN];
17 056fe1ba 2003-11-23 devnull VtSrv *srv;
18 056fe1ba 2003-11-23 devnull VtConn *c;
21 056fe1ba 2003-11-23 devnull struct VtSrv
24 056fe1ba 2003-11-23 devnull int dead;
25 056fe1ba 2003-11-23 devnull char adir[NETPATHLEN];
26 056fe1ba 2003-11-23 devnull Queue *q; /* Queue(VtReq*) */
29 056fe1ba 2003-11-23 devnull static void listenproc(void*);
30 056fe1ba 2003-11-23 devnull static void connproc(void*);
33 056fe1ba 2003-11-23 devnull vtlisten(char *addr)
35 056fe1ba 2003-11-23 devnull VtSrv *s;
37 056fe1ba 2003-11-23 devnull s = vtmallocz(sizeof(VtSrv));
38 056fe1ba 2003-11-23 devnull s->afd = announce(addr, s->adir);
39 056fe1ba 2003-11-23 devnull if(s->afd < 0){
41 056fe1ba 2003-11-23 devnull return nil;
43 056fe1ba 2003-11-23 devnull s->q = _vtqalloc();
44 056fe1ba 2003-11-23 devnull proccreate(listenproc, s, STACK);
45 056fe1ba 2003-11-23 devnull return s;
48 056fe1ba 2003-11-23 devnull static void
49 056fe1ba 2003-11-23 devnull listenproc(void *v)
52 056fe1ba 2003-11-23 devnull char dir[NETPATHLEN];
53 056fe1ba 2003-11-23 devnull VtSrv *srv;
54 056fe1ba 2003-11-23 devnull VtSconn *sc;
58 056fe1ba 2003-11-23 devnull ctl = listen(srv->adir, dir);
59 056fe1ba 2003-11-23 devnull if(ctl < 0){
60 056fe1ba 2003-11-23 devnull srv->dead = 1;
63 056fe1ba 2003-11-23 devnull sc = vtmallocz(sizeof(VtSconn));
64 056fe1ba 2003-11-23 devnull sc->ctl = ctl;
65 056fe1ba 2003-11-23 devnull sc->srv = srv;
66 056fe1ba 2003-11-23 devnull strcpy(sc->dir, dir);
67 056fe1ba 2003-11-23 devnull proccreate(connproc, sc, STACK);
70 056fe1ba 2003-11-23 devnull // hangup
73 056fe1ba 2003-11-23 devnull static void
74 056fe1ba 2003-11-23 devnull connproc(void *v)
76 056fe1ba 2003-11-23 devnull VtSconn *sc;
77 056fe1ba 2003-11-23 devnull VtConn *c;
78 056fe1ba 2003-11-23 devnull Packet *p;
79 056fe1ba 2003-11-23 devnull VtReq *r;
85 056fe1ba 2003-11-23 devnull fprint(2, "new call %s on %d\n", sc->dir, sc->ctl);
86 056fe1ba 2003-11-23 devnull fd = accept(sc->ctl, sc->dir);
87 056fe1ba 2003-11-23 devnull close(sc->ctl);
88 056fe1ba 2003-11-23 devnull if(fd < 0){
89 056fe1ba 2003-11-23 devnull fprint(2, "accept %s: %r\n", sc->dir);
90 056fe1ba 2003-11-23 devnull goto out;
93 056fe1ba 2003-11-23 devnull c = vtconn(fd, fd);
94 056fe1ba 2003-11-23 devnull sc->c = c;
95 056fe1ba 2003-11-23 devnull if(vtversion(c) < 0){
96 056fe1ba 2003-11-23 devnull fprint(2, "vtversion %s: %r\n", sc->dir);
97 056fe1ba 2003-11-23 devnull goto out;
99 056fe1ba 2003-11-23 devnull if(vtsrvhello(c) < 0){
100 056fe1ba 2003-11-23 devnull fprint(2, "vtsrvhello %s: %r\n", sc->dir);
101 056fe1ba 2003-11-23 devnull goto out;
104 056fe1ba 2003-11-23 devnull fprint(2, "new proc %s\n", sc->dir);
105 056fe1ba 2003-11-23 devnull proccreate(vtsendproc, c, STACK);
106 056fe1ba 2003-11-23 devnull qlock(&c->lk);
107 056fe1ba 2003-11-23 devnull while(!c->writeq)
108 056fe1ba 2003-11-23 devnull rsleep(&c->rpcfork);
109 056fe1ba 2003-11-23 devnull qunlock(&c->lk);
111 056fe1ba 2003-11-23 devnull while((p = vtrecv(c)) != nil){
112 056fe1ba 2003-11-23 devnull r = vtmallocz(sizeof(VtReq));
113 056fe1ba 2003-11-23 devnull if(vtfcallunpack(&r->tx, p) < 0){
114 056fe1ba 2003-11-23 devnull packetfree(p);
115 056fe1ba 2003-11-23 devnull fprint(2, "bad packet on %s: %r\n", sc->dir);
116 056fe1ba 2003-11-23 devnull continue;
118 056fe1ba 2003-11-23 devnull packetfree(p);
119 056fe1ba 2003-11-23 devnull if(r->tx.type == VtTgoodbye)
121 056fe1ba 2003-11-23 devnull r->rx.tag = r->tx.tag;
122 056fe1ba 2003-11-23 devnull r->sc = sc;
123 056fe1ba 2003-11-23 devnull if(_vtqsend(sc->srv->q, r) < 0){
124 056fe1ba 2003-11-23 devnull fprint(2, "hungup queue\n");
127 056fe1ba 2003-11-23 devnull r = nil;
130 056fe1ba 2003-11-23 devnull fprint(2, "eof on %s\n", sc->dir);
134 056fe1ba 2003-11-23 devnull vtfcallclear(&r->tx);
135 056fe1ba 2003-11-23 devnull vtfree(r);
138 056fe1ba 2003-11-23 devnull vtfreeconn(c);
139 056fe1ba 2003-11-23 devnull fprint(2, "freed %s\n", sc->dir);
140 056fe1ba 2003-11-23 devnull vtfree(sc);
145 056fe1ba 2003-11-23 devnull vtgetreq(VtSrv *srv)
147 056fe1ba 2003-11-23 devnull return _vtqrecv(srv->q);
151 056fe1ba 2003-11-23 devnull vtrespond(VtReq *r)
153 056fe1ba 2003-11-23 devnull Packet *p;
154 056fe1ba 2003-11-23 devnull VtSconn *sc;
156 056fe1ba 2003-11-23 devnull sc = r->sc;
157 056fe1ba 2003-11-23 devnull if(r->rx.tag != r->tx.tag)
158 056fe1ba 2003-11-23 devnull abort();
159 056fe1ba 2003-11-23 devnull if(r->rx.type != r->tx.type+1 && r->rx.type != VtRerror)
160 056fe1ba 2003-11-23 devnull abort();
161 056fe1ba 2003-11-23 devnull if((p = vtfcallpack(&r->rx)) == nil){
162 056fe1ba 2003-11-23 devnull fprint(2, "fcallpack on %s: %r\n", sc->dir);
163 056fe1ba 2003-11-23 devnull packetfree(p);
164 056fe1ba 2003-11-23 devnull vtfcallclear(&r->rx);
167 056fe1ba 2003-11-23 devnull vtsend(sc->c, p);
168 056fe1ba 2003-11-23 devnull vtfcallclear(&r->tx);
169 056fe1ba 2003-11-23 devnull vtfcallclear(&r->rx);
170 056fe1ba 2003-11-23 devnull vtfree(r);