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 2277c5d7 2004-03-21 devnull fprint(2, "listen for venti\n");
59 056fe1ba 2003-11-23 devnull ctl = listen(srv->adir, dir);
60 056fe1ba 2003-11-23 devnull if(ctl < 0){
61 056fe1ba 2003-11-23 devnull srv->dead = 1;
64 2277c5d7 2004-03-21 devnull fprint(2, "got one\n");
65 056fe1ba 2003-11-23 devnull sc = vtmallocz(sizeof(VtSconn));
66 056fe1ba 2003-11-23 devnull sc->ctl = ctl;
67 056fe1ba 2003-11-23 devnull sc->srv = srv;
68 056fe1ba 2003-11-23 devnull strcpy(sc->dir, dir);
69 056fe1ba 2003-11-23 devnull proccreate(connproc, sc, STACK);
72 056fe1ba 2003-11-23 devnull // hangup
75 056fe1ba 2003-11-23 devnull static void
76 056fe1ba 2003-11-23 devnull connproc(void *v)
78 056fe1ba 2003-11-23 devnull VtSconn *sc;
79 056fe1ba 2003-11-23 devnull VtConn *c;
80 056fe1ba 2003-11-23 devnull Packet *p;
81 056fe1ba 2003-11-23 devnull VtReq *r;
87 056fe1ba 2003-11-23 devnull fprint(2, "new call %s on %d\n", sc->dir, sc->ctl);
88 056fe1ba 2003-11-23 devnull fd = accept(sc->ctl, sc->dir);
89 056fe1ba 2003-11-23 devnull close(sc->ctl);
90 056fe1ba 2003-11-23 devnull if(fd < 0){
91 056fe1ba 2003-11-23 devnull fprint(2, "accept %s: %r\n", sc->dir);
92 056fe1ba 2003-11-23 devnull goto out;
95 056fe1ba 2003-11-23 devnull c = vtconn(fd, fd);
96 056fe1ba 2003-11-23 devnull sc->c = c;
97 056fe1ba 2003-11-23 devnull if(vtversion(c) < 0){
98 056fe1ba 2003-11-23 devnull fprint(2, "vtversion %s: %r\n", sc->dir);
99 056fe1ba 2003-11-23 devnull goto out;
101 056fe1ba 2003-11-23 devnull if(vtsrvhello(c) < 0){
102 056fe1ba 2003-11-23 devnull fprint(2, "vtsrvhello %s: %r\n", sc->dir);
103 056fe1ba 2003-11-23 devnull goto out;
106 056fe1ba 2003-11-23 devnull fprint(2, "new proc %s\n", sc->dir);
107 056fe1ba 2003-11-23 devnull proccreate(vtsendproc, c, STACK);
108 056fe1ba 2003-11-23 devnull qlock(&c->lk);
109 056fe1ba 2003-11-23 devnull while(!c->writeq)
110 056fe1ba 2003-11-23 devnull rsleep(&c->rpcfork);
111 056fe1ba 2003-11-23 devnull qunlock(&c->lk);
113 056fe1ba 2003-11-23 devnull while((p = vtrecv(c)) != nil){
114 056fe1ba 2003-11-23 devnull r = vtmallocz(sizeof(VtReq));
115 056fe1ba 2003-11-23 devnull if(vtfcallunpack(&r->tx, p) < 0){
116 056fe1ba 2003-11-23 devnull packetfree(p);
117 056fe1ba 2003-11-23 devnull fprint(2, "bad packet on %s: %r\n", sc->dir);
118 056fe1ba 2003-11-23 devnull continue;
120 056fe1ba 2003-11-23 devnull packetfree(p);
121 056fe1ba 2003-11-23 devnull if(r->tx.type == VtTgoodbye)
123 056fe1ba 2003-11-23 devnull r->rx.tag = r->tx.tag;
124 056fe1ba 2003-11-23 devnull r->sc = sc;
125 056fe1ba 2003-11-23 devnull if(_vtqsend(sc->srv->q, r) < 0){
126 056fe1ba 2003-11-23 devnull fprint(2, "hungup queue\n");
129 056fe1ba 2003-11-23 devnull r = nil;
132 056fe1ba 2003-11-23 devnull fprint(2, "eof on %s\n", sc->dir);
136 056fe1ba 2003-11-23 devnull vtfcallclear(&r->tx);
137 056fe1ba 2003-11-23 devnull vtfree(r);
140 056fe1ba 2003-11-23 devnull vtfreeconn(c);
141 056fe1ba 2003-11-23 devnull fprint(2, "freed %s\n", sc->dir);
142 056fe1ba 2003-11-23 devnull vtfree(sc);
147 056fe1ba 2003-11-23 devnull vtgetreq(VtSrv *srv)
149 056fe1ba 2003-11-23 devnull return _vtqrecv(srv->q);
153 056fe1ba 2003-11-23 devnull vtrespond(VtReq *r)
155 056fe1ba 2003-11-23 devnull Packet *p;
156 056fe1ba 2003-11-23 devnull VtSconn *sc;
158 056fe1ba 2003-11-23 devnull sc = r->sc;
159 056fe1ba 2003-11-23 devnull if(r->rx.tag != r->tx.tag)
160 056fe1ba 2003-11-23 devnull abort();
161 056fe1ba 2003-11-23 devnull if(r->rx.type != r->tx.type+1 && r->rx.type != VtRerror)
162 056fe1ba 2003-11-23 devnull abort();
163 056fe1ba 2003-11-23 devnull if((p = vtfcallpack(&r->rx)) == nil){
164 056fe1ba 2003-11-23 devnull fprint(2, "fcallpack on %s: %r\n", sc->dir);
165 056fe1ba 2003-11-23 devnull packetfree(p);
166 056fe1ba 2003-11-23 devnull vtfcallclear(&r->rx);
169 056fe1ba 2003-11-23 devnull vtsend(sc->c, p);
170 056fe1ba 2003-11-23 devnull vtfcallclear(&r->tx);
171 056fe1ba 2003-11-23 devnull vtfcallclear(&r->rx);
172 056fe1ba 2003-11-23 devnull vtfree(r);