11 MAXMSG = 64, /* per connection */
14 typedef struct Hash Hash;
15 typedef struct Fid Fid;
16 typedef struct Msg Msg;
17 typedef struct Conn Conn;
18 typedef struct Queue Queue;
89 void *gethash(Hash**, uint);
90 int puthash(Hash**, uint, void*);
91 int delhash(Hash**, uint, void*);
92 Msg *mread9p(Ioproc*, int, int);
93 int mwrite9p(Ioproc*, int, uchar*);
94 uchar *read9ppkt(Ioproc*, int);
95 int write9ppkt(int, uchar*);
100 void msgincref(Msg*);
104 void *erealloc(void*, int);
106 int sendq(Queue*, void*);
108 void connthread(void*);
109 void connoutthread(void*);
110 void listenthread(void*);
111 void outputthread(void*);
112 void inputthread(void*);
113 void rewritehdr(Fcall*, uchar*);
114 void repack(Fcall*, uchar**, int);
115 int tlisten(char*, char*);
116 int taccept(int, char*);
117 int iolisten(Ioproc*, char*, char*);
118 int ioaccept(Ioproc*, int, char*);
119 int iorecvfd(Ioproc*, int);
120 int iosendfd(Ioproc*, int, int);
121 void mainproc(void*);
122 int ignorepipe(void*, char*);
124 void dorootstat(void);
125 int stripudirread(Msg*);
126 int stripustat(Fcall*, uchar**, int);
131 fprint(2, "usage: 9pserve [-lv] [-A aname afid] [-M msize] address\n");
132 fprint(2, "\treads/writes 9P messages on stdin/stdout\n");
133 threadexitsall("usage");
137 extern int _threaddebuglevel;
139 threadmain(int argc, char **argv)
144 x = getenv("verbose9pserve");
147 fprint(2, "verbose9pserve %s => %d\n", x, verbose);
154 xaname = EARGF(usage());
155 xafid = atoi(EARGF(usage()));
159 msize = atoi(EARGF(usage()));
172 if(attached && !versioned){
173 fprint(2, "-A must be used with -M\n");
181 fmtinstall('T', timefmt);
183 if((afd = announce(addr, adir)) < 0)
184 sysfatal("announce %s: %r", addr);
186 if(strncmp(addr, "unix!", 5) == 0)
188 file = smprint("%s.log", addr);
190 sysfatal("smprint log: %r");
191 if((fd = create(file, OWRITE, 0666)) < 0)
192 sysfatal("create %s: %r", file);
197 if(verbose) fprint(2, "%T 9pserve running\n");
198 proccreate(mainproc, nil, STACK);
208 atnotify(ignorepipe, 1);
209 fmtinstall('D', dirfmt);
210 fmtinstall('M', dirmodefmt);
211 fmtinstall('F', fcallfmt);
212 fmtinstall('H', encodefmt);
219 f.version = "9P2000.u";
222 n = convS2M(&f, vbuf, sizeof vbuf);
223 if(verbose > 1) fprint(2, "%T * <- %F\n", &f);
224 nn = write(1, vbuf, n);
226 sysfatal("error writing Tversion: %r\n");
227 n = read9pmsg(0, vbuf, sizeof vbuf);
228 if(convM2S(vbuf, n, &f) != n)
229 sysfatal("convM2S failure");
232 if(verbose > 1) fprint(2, "%T * -> %F\n", &f);
233 dotu = strncmp(f.version, "9P2000.u", 8) == 0;
236 threadcreate(inputthread, nil, STACK);
237 threadcreate(outputthread, nil, STACK);
242 threadcreate(listenthread, nil, STACK);
247 ignorepipe(void *v, char *s)
250 if(strcmp(s, "sys: write on closed pipe") == 0)
252 if(strcmp(s, "sys: tstp") == 0)
254 fprint(2, "9pserve %s: %T note: %s\n", addr, s);
259 listenthread(void *arg)
266 threadsetname("listen %s", adir);
268 c = emalloc(sizeof(Conn));
269 c->fd = iolisten(io, adir, c->dir);
271 if(verbose) fprint(2, "%T listen: %r\n");
276 c->inc = chancreate(sizeof(void*), 0);
277 c->internal = chancreate(sizeof(void*), 0);
280 if(verbose) fprint(2, "%T incoming call on %s\n", c->dir);
281 threadcreate(connthread, c, STACK);
290 n = sizeS2Mu(&m->rx, m->c->dotu);
291 m->rpkt = emalloc(n);
292 nn = convS2Mu(&m->rx, m->rpkt, n, m->c->dotu);
294 sysfatal("sizeS2M + convS2M disagree");
295 sendq(m->c->outq, m);
303 n = sizeS2Mu(&m->tx, m->c->dotu);
304 m->tpkt = emalloc(n);
305 nn = convS2Mu(&m->tx, m->tpkt, n, m->c->dotu);
307 sysfatal("sizeS2M + convS2M disagree");
312 err(Msg *m, char *ename)
316 m->rx.tag = m->tx.tag;
325 t = emalloc(strlen(s)+1);
331 connthread(void *arg)
341 threadsetname("conn %s", c->dir);
343 fd = ioaccept(io, c->fd, c->dir);
345 if(verbose) fprint(2, "%T accept %s: %r\n", c->dir);
350 threadcreate(connoutthread, c, STACK);
351 while((m = mread9p(io, c->fd, c->dotu)) != nil){
352 if(verbose > 1) fprint(2, "%T fd#%d -> %F\n", c->fd, &m->tx);
356 if(verbose > 1) fprint(2, "%T fd#%d: new msg %p\n", c->fd, m);
357 if(puthash(c->tag, m->tx.tag, m) < 0){
358 err(m, "duplicate tag");
364 m->rx.tag = m->tx.tag;
365 m->rx.msize = m->tx.msize;
366 if(m->rx.msize > msize)
368 m->rx.version = "9P2000";
370 if(dotu && strncmp(m->tx.version, "9P2000.u", 8) == 0){
371 m->rx.version = "9P2000.u";
374 m->rx.type = Rversion;
378 if((m->oldm = gethash(c->tag, m->tx.oldtag)) == nil){
379 m->rx.tag = m->tx.tag;
388 if(m->tx.afid != NOFID
389 && (m->afid = gethash(c->fid, m->tx.afid)) == nil){
390 err(m, "unknown fid");
395 m->fid = fidnew(m->tx.fid);
396 if(puthash(c->fid, m->tx.fid, m->fid) < 0){
397 err(m, "duplicate fid");
401 if(attached && m->afid==nil){
402 if(m->tx.aname[0] && strcmp(xaname, m->tx.aname) != 0){
403 err(m, "invalid attach name");
407 m->tx.aname = xaname;
408 m->tx.uname = estrdup(m->tx.uname);
409 repack(&m->tx, &m->tpkt, c->dotu);
415 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
416 err(m, "unknown fid");
420 if(m->tx.newfid == m->tx.fid){
424 m->newfid = fidnew(m->tx.newfid);
425 if(puthash(c->fid, m->tx.newfid, m->newfid) < 0){
426 err(m, "duplicate fid");
434 err(m, "authentication not required");
437 m->afid = fidnew(m->tx.afid);
438 if(puthash(c->fid, m->tx.afid, m->afid) < 0){
439 err(m, "duplicate fid");
445 if(m->tx.mode&~(OTRUNC|3)){
446 err(m, "bad openfd mode");
461 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
462 err(m, "unknown fid");
466 if(m->tx.type==Twstat && dotu && !c->dotu){
467 if(stripustat(&m->tx, &m->tpkt, 1) < 0){
468 err(m, "cannot convert stat buffer");
475 /* have everything - translate and send */
480 m->tx.fid = m->fid->fid;
482 m->tx.newfid = m->newfid->fid;
484 m->tx.afid = m->afid->fid;
486 m->tx.oldtag = m->oldm->tag;
487 /* reference passes to outq */
489 while(c->nmsg >= MAXMSG){
495 if(verbose) fprint(2, "%T fd#%d eof; flushing conn\n", c->fd);
497 /* flush the output queue */
499 while(c->outq != nil)
502 /* flush all outstanding messages */
503 for(i=0; i<NHASH; i++){
504 for(h=c->tag[i]; h; h=hnext){
512 m->tx.oldtag = om->tag;
515 msgincref(m); /* for outq */
517 mm = recvp(c->internal);
519 msgput(m); /* got from recvp */
520 msgput(m); /* got from msgnew */
521 msgput(om); /* got from hash table */
527 /* clunk all outstanding fids */
528 for(i=0; i<NHASH; i++){
529 for(h=c->fid[i]; h; h=hnext){
542 mm = recvp(c->internal);
545 msgput(m); /* got from recvp */
546 msgput(m); /* got from msgnew */
547 fidput(f); /* got from hash table */
555 assert(c->nmsg == 0);
556 assert(c->nfid == 0);
558 chanfree(c->internal);
568 openfdthread(void *v)
581 threadsetname("openfd %s", c->fdfid);
584 if(c->fdmode == OREAD){
586 if(verbose) fprint(2, "%T tread...");
591 m->tx.count = msize - IOHDRSZ;
592 m->tx.fid = fid->fid;
600 if(m->rx.type == Rerror){
601 // fprint(2, "%T read error: %s\n", m->rx.ename);
607 if(iowrite(io, c->fd, m->rx.data, m->rx.count) != m->rx.count){
608 // fprint(2, "%T pipe write error: %r\n");
617 if(verbose) fprint(2, "%T twrite...");
621 if((n=ioread(io, c->fd, buf, n)) <= 0){
623 fprint(2, "%T pipe read error: %r\n");
630 m->tx.fid = fid->fid;
640 if(m->rx.type == Rerror){
641 // fprint(2, "%T write error: %s\n", m->rx.ename);
649 if(verbose) fprint(2, "%T eof on %d fid %d\n", c->fd, fid->fid);
656 if(verbose) fprint(2, "%T eof on %d fid %d ref %d\n", c->fd, fid->fid, fid->ref);
657 if(--fid->openfd == 0){
663 m->tx.fid = fid->fid;
674 chanfree(c->internal);
687 rerrstr(errs, sizeof errs);
689 /* XXX return here? */
691 if(verbose) fprint(2, "%T xopen pipe %d %d...", p[0], p[1]);
693 /* now we're committed. */
695 /* a new connection for this fid */
696 nc = emalloc(sizeof(Conn));
697 nc->internal = chancreate(sizeof(void*), 0);
703 nc->fdmode = m->tx.mode;
706 /* a thread to tend the pipe */
707 threadcreate(openfdthread, nc, STACK);
709 /* if mode is ORDWR, that openfdthread will write; start a reader */
710 if((m->tx.mode&3) == ORDWR){
711 nc = emalloc(sizeof(Conn));
712 nc->internal = chancreate(sizeof(void*), 0);
717 nc->fd = dup(p[0], -1);
718 threadcreate(openfdthread, nc, STACK);
721 /* steal fid from other connection */
722 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
725 /* rewrite as Ropenfd */
726 m->rx.type = Ropenfd;
728 m->rpkt = erealloc(m->rpkt, n+4);
729 PBIT32(m->rpkt+n, p[1]);
732 m->rpkt[4] = Ropenfd;
738 connoutthread(void *arg)
750 threadsetname("connout %s", c->dir);
751 while((m = recvq(outq)) != nil){
752 err = m->tx.type+1 != m->rx.type;
753 if(!err && m->isopenfd)
760 if(delhash(om->c->tag, om->ctag, om) == 0)
766 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
771 if(verbose) fprint(2, "%T auth error\n");
772 if(delhash(m->c->fid, m->afid->cfid, m->afid) == 0)
778 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
782 if(err || m->rx.nwqid < m->tx.nwname)
783 if(m->tx.fid != m->tx.newfid && m->newfid)
784 if(delhash(m->c->fid, m->newfid->cfid, m->newfid) == 0)
788 if(!err && m->fid->isdir && dotu && !m->c->dotu)
792 if(!err && dotu && !m->c->dotu)
793 stripustat(&m->rx, &m->rpkt, 0);
797 m->fid->isdir = (m->rx.qid.type & QTDIR);
800 if(m->rx.type==Rerror && dotu && !c->dotu){
801 ename = estrdup(m->rx.ename);
803 repack(&m->rx, &m->rpkt, c->dotu);
806 if(delhash(m->c->tag, m->ctag, m) == 0)
808 if(verbose > 1) fprint(2, "%T fd#%d <- %F\n", c->fd, &m->rx);
809 rewritehdr(&m->rx, m->rpkt);
810 if(mwrite9p(io, c->fd, m->rpkt) < 0)
811 if(verbose) fprint(2, "%T write error: %r\n");
813 if(c->inputstalled && c->nmsg < MAXMSG)
822 outputthread(void *arg)
829 threadsetname("output");
830 while((m = recvq(outq)) != nil){
831 if(verbose > 1) fprint(2, "%T * <- %F\n", &m->tx);
832 rewritehdr(&m->tx, m->tpkt);
833 if(mwrite9p(io, 1, m->tpkt) < 0)
834 sysfatal("output error: %r");
838 fprint(2, "%T output eof\n");
843 inputthread(void *arg)
850 threadsetname("input");
851 if(verbose) fprint(2, "%T input thread\n");
854 while((pkt = read9ppkt(io, 0)) != nil){
857 fprint(2, "%T short 9P packet from server\n");
861 if(verbose > 2) fprint(2, "%T read %.*H\n", n, pkt);
863 if((m = msgget(tag)) == nil){
864 fprint(2, "%T unexpected 9P response tag %d\n", tag);
868 if((nn = convM2Su(pkt, n, &m->rx, dotu)) != n){
869 fprint(2, "%T bad packet - convM2S %d but %d\n", nn, n);
874 if(verbose > 1) fprint(2, "%T * -> %F%s\n", &m->rx,
875 m->internal ? " (internal)" : "");
879 sendp(m->c->internal, m);
881 sendq(m->c->outq, m);
886 //fprint(2, "%T input eof\n");
891 gethash(Hash **ht, uint n)
895 for(h=ht[n%NHASH]; h; h=h->next)
902 delhash(Hash **ht, uint n, void *v)
906 for(l=&ht[n%NHASH]; h=*l; l=&h->next)
909 if(verbose) fprint(2, "%T delhash %d got %p want %p\n", n, h->v, v);
920 puthash(Hash **ht, uint n, void *v)
926 h = emalloc(sizeof(Hash));
927 h->next = ht[n%NHASH];
944 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
945 if(nfidtab == xafid){
946 fidtab[nfidtab++] = nil;
947 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
949 fidtab[nfidtab] = emalloc(sizeof(Fid));
950 freefid = fidtab[nfidtab];
951 freefid->fid = nfidtab++;
981 if(verbose > 1) fprint(2, "%T msgincref @0x%lux %p tag %d/%d ref %d=>%d\n",
982 getcallerpc(&m), m, m->tag, m->ctag, m->ref, m->ref+1);
992 msgtab = erealloc(msgtab, (nmsgtab+1)*sizeof(msgtab[0]));
993 msgtab[nmsgtab] = emalloc(sizeof(Msg));
994 freemsg = msgtab[nmsgtab];
995 freemsg->tag = nmsgtab++;
1000 if(verbose > 1) fprint(2, "%T msgnew @0x%lux %p tag %d ref %d\n",
1001 getcallerpc(&x), m, m->tag, m->ref);
1006 * Clear data associated with connections, so that
1007 * if all msgs have been msgcleared, the connection
1008 * can be freed. Note that this does *not* free the tpkt
1009 * and rpkt; they are freed in msgput with the msg itself.
1010 * The io write thread might still be holding a ref to msg
1011 * even once the connection has finished with it.
1036 if(m->rx.type == Ropenfd && m->rx.unixfd >= 0){
1037 close(m->rx.unixfd);
1048 if(verbose > 1) fprint(2, "%T msgput 0x%lux %p tag %d/%d ref %d\n",
1049 getcallerpc(&m), m, m->tag, m->ctag, m->ref);
1073 if(n < 0 || n >= nmsgtab)
1078 if(verbose) fprint(2, "%T msgget %d = %p\n", n, m);
1092 sysfatal("out of memory allocating %d", n);
1098 erealloc(void *v, int n)
1103 sysfatal("out of memory reallocating %d", n);
1108 typedef struct Qel Qel;
1129 q = mallocz(sizeof(Queue), 1);
1137 sendq(Queue *q, void *p)
1141 e = emalloc(sizeof(Qel));
1144 werrstr("hungup queue");
1167 while(q->head == nil && !q->hungup)
1182 read9ppkt(Ioproc *io, int fd)
1187 n = ioreadn(io, fd, buf, 4);
1193 nn = ioreadn(io, fd, pkt+4, n-4);
1198 /* would do this if we ever got one of these, but we only generate them
1199 if(pkt[4] == Ropenfd){
1200 newfd = iorecvfd(io, fd);
1201 PBIT32(pkt+n-4, newfd);
1208 mread9p(Ioproc *io, int fd, int dotu)
1214 if((pkt = read9ppkt(io, fd)) == nil)
1220 nn = convM2Su(pkt, n, &m->tx, dotu);
1222 fprint(2, "%T read bad packet from %d\n", fd);
1229 mwrite9p(Ioproc *io, int fd, uchar *pkt)
1234 if(verbose > 2) fprint(2, "%T write %d %d %.*H\n", fd, n, n, pkt);
1235 if(verbose > 1) fprint(2, "%T before iowrite\n");
1236 if(iowrite(io, fd, pkt, n) != n){
1237 fprint(2, "%T write error: %r\n");
1240 if(verbose > 1) fprint(2, "%T after iowrite\n");
1241 if(pkt[4] == Ropenfd){
1242 nfd = GBIT32(pkt+n-4);
1243 if(iosendfd(io, fd, nfd) < 0){
1244 fprint(2, "%T send fd error: %r\n");
1252 restring(uchar *pkt, int pn, char *s)
1256 if(s < (char*)pkt || s >= (char*)pkt+pn)
1261 PBIT16((uchar*)s-1, n);
1265 repack(Fcall *f, uchar **ppkt, int dotu)
1272 nn = sizeS2Mu(f, dotu);
1278 convS2Mu(f, pkt, nn, dotu);
1282 rewritehdr(Fcall *f, uchar *pkt)
1287 PBIT16(pkt+5, f->tag);
1291 restring(pkt, n, f->version);
1294 PBIT32(pkt+7, f->afid);
1295 restring(pkt, n, f->uname);
1296 restring(pkt, n, f->aname);
1299 PBIT16(pkt+7, f->oldtag);
1302 restring(pkt, n, f->uname);
1303 restring(pkt, n, f->aname);
1304 PBIT32(pkt+7, f->fid);
1305 PBIT32(pkt+11, f->afid);
1308 PBIT32(pkt+7, f->fid);
1309 PBIT32(pkt+11, f->newfid);
1310 for(i=0; i<f->nwname; i++)
1311 restring(pkt, n, f->wname[i]);
1314 restring(pkt, n, f->name);
1323 PBIT32(pkt+7, f->fid);
1326 restring(pkt, n, f->ename);
1332 _iolisten(va_list *arg)
1336 a = va_arg(*arg, char*);
1337 b = va_arg(*arg, char*);
1338 return listen(a, b);
1342 iolisten(Ioproc *io, char *a, char *b)
1344 return iocall(io, _iolisten, a, b);
1348 _ioaccept(va_list *arg)
1353 fd = va_arg(*arg, int);
1354 dir = va_arg(*arg, char*);
1355 return accept(fd, dir);
1359 ioaccept(Ioproc *io, int fd, char *dir)
1361 return iocall(io, _ioaccept, fd, dir);
1367 static char *mon[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1368 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1372 tm = *localtime(time(0));
1373 return fmtprint(fmt, "%s %2d %02d:%02d:%02d.%03d",
1374 mon[tm.mon], tm.mday, tm.hour, tm.min, tm.sec,
1375 (int)(ns%1000000000)/1000000);
1379 stripustat(Fcall *f, uchar **fpkt, int s2u)
1386 str = emalloc(f->nstat);
1387 n = convM2Du(f->stat, f->nstat, &dir, str, s2u);
1390 n = sizeD2Mu(&dir, !s2u);
1393 n = convD2Mu(&dir, buf, n, !s2u);
1399 repack(f, fpkt, dotu);
1407 stripudirread(Msg* msg)
1420 for(i = 0; i < rx->count; i += m){
1421 m = BIT16SZ + GBIT16(&rx->data[i]);
1422 if(statchecku((uchar*)&rx->data[i], m, 1) < 0)
1430 buf = emalloc(rx->count);
1433 for(i = 0; i < rx->count; i += m){
1434 m = BIT16SZ + GBIT16(&rx->data[i]);
1435 if(convM2Du((uchar*)&rx->data[i], m, &d, str, 1) != m){
1441 n = convD2M(&d, &buf[nn], rx->count - nn);
1452 rx->data = (char*)buf;
1454 repack(&msg->rx, &msg->rpkt, 0);