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
17 be36ff68 2004-04-29 devnull QLock lk;
18 056fe1ba 2003-11-23 devnull char dir[NETPATHLEN];
19 056fe1ba 2003-11-23 devnull VtSrv *srv;
20 056fe1ba 2003-11-23 devnull VtConn *c;
23 056fe1ba 2003-11-23 devnull struct VtSrv
26 056fe1ba 2003-11-23 devnull int dead;
27 056fe1ba 2003-11-23 devnull char adir[NETPATHLEN];
28 056fe1ba 2003-11-23 devnull Queue *q; /* Queue(VtReq*) */
31 056fe1ba 2003-11-23 devnull static void listenproc(void*);
32 056fe1ba 2003-11-23 devnull static void connproc(void*);
34 be36ff68 2004-04-29 devnull static void
35 be36ff68 2004-04-29 devnull scincref(VtSconn *sc)
37 be36ff68 2004-04-29 devnull qlock(&sc->lk);
38 be36ff68 2004-04-29 devnull sc->ref++;
39 be36ff68 2004-04-29 devnull qunlock(&sc->lk);
42 be36ff68 2004-04-29 devnull static void
43 be36ff68 2004-04-29 devnull scdecref(VtSconn *sc)
45 be36ff68 2004-04-29 devnull qlock(&sc->lk);
46 be36ff68 2004-04-29 devnull if(--sc->ref > 0){
47 be36ff68 2004-04-29 devnull qunlock(&sc->lk);
50 be36ff68 2004-04-29 devnull if(sc->c)
51 be36ff68 2004-04-29 devnull vtfreeconn(sc->c);
52 be36ff68 2004-04-29 devnull vtfree(sc);
56 056fe1ba 2003-11-23 devnull vtlisten(char *addr)
58 056fe1ba 2003-11-23 devnull VtSrv *s;
60 056fe1ba 2003-11-23 devnull s = vtmallocz(sizeof(VtSrv));
61 056fe1ba 2003-11-23 devnull s->afd = announce(addr, s->adir);
62 056fe1ba 2003-11-23 devnull if(s->afd < 0){
64 056fe1ba 2003-11-23 devnull return nil;
66 056fe1ba 2003-11-23 devnull s->q = _vtqalloc();
67 056fe1ba 2003-11-23 devnull proccreate(listenproc, s, STACK);
68 056fe1ba 2003-11-23 devnull return s;
71 056fe1ba 2003-11-23 devnull static void
72 056fe1ba 2003-11-23 devnull listenproc(void *v)
75 056fe1ba 2003-11-23 devnull char dir[NETPATHLEN];
76 056fe1ba 2003-11-23 devnull VtSrv *srv;
77 056fe1ba 2003-11-23 devnull VtSconn *sc;
81 056fe1ba 2003-11-23 devnull ctl = listen(srv->adir, dir);
82 056fe1ba 2003-11-23 devnull if(ctl < 0){
83 056fe1ba 2003-11-23 devnull srv->dead = 1;
86 056fe1ba 2003-11-23 devnull sc = vtmallocz(sizeof(VtSconn));
87 be36ff68 2004-04-29 devnull sc->ref = 1;
88 056fe1ba 2003-11-23 devnull sc->ctl = ctl;
89 056fe1ba 2003-11-23 devnull sc->srv = srv;
90 056fe1ba 2003-11-23 devnull strcpy(sc->dir, dir);
91 056fe1ba 2003-11-23 devnull proccreate(connproc, sc, STACK);
94 056fe1ba 2003-11-23 devnull // hangup
97 056fe1ba 2003-11-23 devnull static void
98 056fe1ba 2003-11-23 devnull connproc(void *v)
100 056fe1ba 2003-11-23 devnull VtSconn *sc;
101 056fe1ba 2003-11-23 devnull VtConn *c;
102 056fe1ba 2003-11-23 devnull Packet *p;
103 056fe1ba 2003-11-23 devnull VtReq *r;
105 a09e80f9 2004-05-23 devnull static int first=1;
107 8baa0cbd 2004-06-09 devnull if(first && chattyventi){
108 a09e80f9 2004-05-23 devnull first=0;
109 a09e80f9 2004-05-23 devnull fmtinstall('F', vtfcallfmt);
111 056fe1ba 2003-11-23 devnull r = nil;
113 be36ff68 2004-04-29 devnull sc->c = nil;
114 8baa0cbd 2004-06-09 devnull if(0) fprint(2, "new call %s on %d\n", sc->dir, sc->ctl);
115 056fe1ba 2003-11-23 devnull fd = accept(sc->ctl, sc->dir);
116 056fe1ba 2003-11-23 devnull close(sc->ctl);
117 056fe1ba 2003-11-23 devnull if(fd < 0){
118 056fe1ba 2003-11-23 devnull fprint(2, "accept %s: %r\n", sc->dir);
119 056fe1ba 2003-11-23 devnull goto out;
122 056fe1ba 2003-11-23 devnull c = vtconn(fd, fd);
123 056fe1ba 2003-11-23 devnull sc->c = c;
124 056fe1ba 2003-11-23 devnull if(vtversion(c) < 0){
125 056fe1ba 2003-11-23 devnull fprint(2, "vtversion %s: %r\n", sc->dir);
126 056fe1ba 2003-11-23 devnull goto out;
128 056fe1ba 2003-11-23 devnull if(vtsrvhello(c) < 0){
129 056fe1ba 2003-11-23 devnull fprint(2, "vtsrvhello %s: %r\n", sc->dir);
130 056fe1ba 2003-11-23 devnull goto out;
133 8baa0cbd 2004-06-09 devnull if(0) fprint(2, "new proc %s\n", sc->dir);
134 056fe1ba 2003-11-23 devnull proccreate(vtsendproc, c, STACK);
135 056fe1ba 2003-11-23 devnull qlock(&c->lk);
136 056fe1ba 2003-11-23 devnull while(!c->writeq)
137 056fe1ba 2003-11-23 devnull rsleep(&c->rpcfork);
138 056fe1ba 2003-11-23 devnull qunlock(&c->lk);
140 056fe1ba 2003-11-23 devnull while((p = vtrecv(c)) != nil){
141 056fe1ba 2003-11-23 devnull r = vtmallocz(sizeof(VtReq));
142 056fe1ba 2003-11-23 devnull if(vtfcallunpack(&r->tx, p) < 0){
143 056fe1ba 2003-11-23 devnull packetfree(p);
144 056fe1ba 2003-11-23 devnull fprint(2, "bad packet on %s: %r\n", sc->dir);
145 056fe1ba 2003-11-23 devnull continue;
147 a09e80f9 2004-05-23 devnull if(chattyventi)
148 a09e80f9 2004-05-23 devnull fprint(2, "%s <- %F\n", argv0, &r->tx);
149 056fe1ba 2003-11-23 devnull packetfree(p);
150 056fe1ba 2003-11-23 devnull if(r->tx.type == VtTgoodbye)
152 056fe1ba 2003-11-23 devnull r->rx.tag = r->tx.tag;
153 056fe1ba 2003-11-23 devnull r->sc = sc;
154 be36ff68 2004-04-29 devnull scincref(sc);
155 056fe1ba 2003-11-23 devnull if(_vtqsend(sc->srv->q, r) < 0){
156 be36ff68 2004-04-29 devnull scdecref(sc);
157 056fe1ba 2003-11-23 devnull fprint(2, "hungup queue\n");
160 056fe1ba 2003-11-23 devnull r = nil;
163 8baa0cbd 2004-06-09 devnull if(0) fprint(2, "eof on %s\n", sc->dir);
167 056fe1ba 2003-11-23 devnull vtfcallclear(&r->tx);
168 056fe1ba 2003-11-23 devnull vtfree(r);
170 8baa0cbd 2004-06-09 devnull if(0) fprint(2, "freed %s\n", sc->dir);
171 be36ff68 2004-04-29 devnull scdecref(sc);
176 056fe1ba 2003-11-23 devnull vtgetreq(VtSrv *srv)
178 056fe1ba 2003-11-23 devnull return _vtqrecv(srv->q);
182 056fe1ba 2003-11-23 devnull vtrespond(VtReq *r)
184 056fe1ba 2003-11-23 devnull Packet *p;
185 056fe1ba 2003-11-23 devnull VtSconn *sc;
187 056fe1ba 2003-11-23 devnull sc = r->sc;
188 056fe1ba 2003-11-23 devnull if(r->rx.tag != r->tx.tag)
189 056fe1ba 2003-11-23 devnull abort();
190 056fe1ba 2003-11-23 devnull if(r->rx.type != r->tx.type+1 && r->rx.type != VtRerror)
191 056fe1ba 2003-11-23 devnull abort();
192 a09e80f9 2004-05-23 devnull if(chattyventi)
193 a09e80f9 2004-05-23 devnull fprint(2, "%s -> %F\n", argv0, &r->rx);
194 056fe1ba 2003-11-23 devnull if((p = vtfcallpack(&r->rx)) == nil){
195 056fe1ba 2003-11-23 devnull fprint(2, "fcallpack on %s: %r\n", sc->dir);
196 056fe1ba 2003-11-23 devnull packetfree(p);
197 056fe1ba 2003-11-23 devnull vtfcallclear(&r->rx);
200 056fe1ba 2003-11-23 devnull vtsend(sc->c, p);
201 be36ff68 2004-04-29 devnull scdecref(sc);
202 056fe1ba 2003-11-23 devnull vtfcallclear(&r->tx);
203 056fe1ba 2003-11-23 devnull vtfcallclear(&r->rx);
204 056fe1ba 2003-11-23 devnull vtfree(r);