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 cvtustat(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);
224 sysfatal("convS2M conversion error");
225 if(verbose > 1) fprint(2, "%T * <- %F\n", &f);
226 nn = write(1, vbuf, n);
228 sysfatal("error writing Tversion: %r\n");
229 n = read9pmsg(0, vbuf, sizeof vbuf);
230 if(convM2S(vbuf, n, &f) != n)
231 sysfatal("convM2S failure");
234 if(verbose > 1) fprint(2, "%T * -> %F\n", &f);
235 dotu = strncmp(f.version, "9P2000.u", 8) == 0;
238 threadcreate(inputthread, nil, STACK);
239 threadcreate(outputthread, nil, STACK);
244 threadcreate(listenthread, nil, STACK);
249 ignorepipe(void *v, char *s)
252 if(strcmp(s, "sys: write on closed pipe") == 0)
254 if(strcmp(s, "sys: tstp") == 0)
256 fprint(2, "9pserve %s: %T note: %s\n", addr, s);
261 listenthread(void *arg)
268 threadsetname("listen %s", adir);
270 c = emalloc(sizeof(Conn));
271 c->fd = iolisten(io, adir, c->dir);
273 if(verbose) fprint(2, "%T listen: %r\n");
278 c->inc = chancreate(sizeof(void*), 0);
279 c->internal = chancreate(sizeof(void*), 0);
282 if(verbose) fprint(2, "%T incoming call on %s\n", c->dir);
283 threadcreate(connthread, c, STACK);
292 n = sizeS2Mu(&m->rx, m->c->dotu);
293 m->rpkt = emalloc(n);
294 nn = convS2Mu(&m->rx, m->rpkt, n, m->c->dotu);
296 sysfatal("convS2Mu conversion error");
298 sysfatal("sizeS2Mu and convS2Mu disagree");
299 sendq(m->c->outq, m);
307 n = sizeS2Mu(&m->tx, m->c->dotu);
308 m->tpkt = emalloc(n);
309 nn = convS2Mu(&m->tx, m->tpkt, n, m->c->dotu);
311 sysfatal("convS2Mu conversion error");
313 sysfatal("sizeS2Mu and convS2Mu disagree");
318 err(Msg *m, char *ename)
322 m->rx.tag = m->tx.tag;
331 t = emalloc(strlen(s)+1);
337 connthread(void *arg)
347 threadsetname("conn %s", c->dir);
349 fd = ioaccept(io, c->fd, c->dir);
351 if(verbose) fprint(2, "%T accept %s: %r\n", c->dir);
356 threadcreate(connoutthread, c, STACK);
357 while((m = mread9p(io, c->fd, c->dotu)) != nil){
358 if(verbose > 1) fprint(2, "%T fd#%d -> %F\n", c->fd, &m->tx);
362 if(verbose > 1) fprint(2, "%T fd#%d: new msg %p\n", c->fd, m);
363 if(puthash(c->tag, m->tx.tag, m) < 0){
364 err(m, "duplicate tag");
370 m->rx.tag = m->tx.tag;
371 m->rx.msize = m->tx.msize;
372 if(m->rx.msize > msize)
374 m->rx.version = "9P2000";
376 if(dotu && strncmp(m->tx.version, "9P2000.u", 8) == 0){
377 m->rx.version = "9P2000.u";
380 m->rx.type = Rversion;
384 if((m->oldm = gethash(c->tag, m->tx.oldtag)) == nil){
385 m->rx.tag = m->tx.tag;
394 if(m->tx.afid != NOFID
395 && (m->afid = gethash(c->fid, m->tx.afid)) == nil){
396 err(m, "unknown fid");
401 m->fid = fidnew(m->tx.fid);
402 if(puthash(c->fid, m->tx.fid, m->fid) < 0){
403 err(m, "duplicate fid");
407 if(attached && m->afid==nil){
408 if(m->tx.aname[0] && strcmp(xaname, m->tx.aname) != 0){
409 err(m, "invalid attach name");
413 m->tx.aname = xaname;
414 m->tx.uname = estrdup(m->tx.uname);
415 repack(&m->tx, &m->tpkt, c->dotu);
421 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
422 err(m, "unknown fid");
426 if(m->tx.newfid == m->tx.fid){
430 m->newfid = fidnew(m->tx.newfid);
431 if(puthash(c->fid, m->tx.newfid, m->newfid) < 0){
432 err(m, "duplicate fid");
440 err(m, "authentication not required");
443 m->afid = fidnew(m->tx.afid);
444 if(puthash(c->fid, m->tx.afid, m->afid) < 0){
445 err(m, "duplicate fid");
451 if(m->tx.mode&~(OTRUNC|3)){
452 err(m, "bad openfd mode");
467 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
468 err(m, "unknown fid");
472 if(m->tx.type==Twstat && dotu && !c->dotu){
473 if(cvtustat(&m->tx, &m->tpkt, 1) < 0){
474 err(m, "cannot convert stat buffer");
481 /* have everything - translate and send */
486 m->tx.fid = m->fid->fid;
488 m->tx.newfid = m->newfid->fid;
490 m->tx.afid = m->afid->fid;
492 m->tx.oldtag = m->oldm->tag;
493 /* reference passes to outq */
495 while(c->nmsg >= MAXMSG){
501 if(verbose) fprint(2, "%T fd#%d eof; flushing conn\n", c->fd);
503 /* flush the output queue */
505 while(c->outq != nil)
508 /* flush all outstanding messages */
509 for(i=0; i<NHASH; i++){
510 for(h=c->tag[i]; h; h=hnext){
518 m->tx.oldtag = om->tag;
521 msgincref(m); /* for outq */
523 mm = recvp(c->internal);
525 msgput(m); /* got from recvp */
526 msgput(m); /* got from msgnew */
527 msgput(om); /* got from hash table */
533 /* clunk all outstanding fids */
534 for(i=0; i<NHASH; i++){
535 for(h=c->fid[i]; h; h=hnext){
548 mm = recvp(c->internal);
551 msgput(m); /* got from recvp */
552 msgput(m); /* got from msgnew */
553 fidput(f); /* got from hash table */
561 assert(c->nmsg == 0);
562 assert(c->nfid == 0);
564 chanfree(c->internal);
574 openfdthread(void *v)
587 threadsetname("openfd %s", c->fdfid);
590 if(c->fdmode == OREAD){
592 if(verbose) fprint(2, "%T tread...");
597 m->tx.count = msize - IOHDRSZ;
598 m->tx.fid = fid->fid;
606 if(m->rx.type == Rerror){
607 // fprint(2, "%T read error: %s\n", m->rx.ename);
613 if(iowrite(io, c->fd, m->rx.data, m->rx.count) != m->rx.count){
614 // fprint(2, "%T pipe write error: %r\n");
623 if(verbose) fprint(2, "%T twrite...");
627 if((n=ioread(io, c->fd, buf, n)) <= 0){
629 fprint(2, "%T pipe read error: %r\n");
636 m->tx.fid = fid->fid;
646 if(m->rx.type == Rerror){
647 // fprint(2, "%T write error: %s\n", m->rx.ename);
655 if(verbose) fprint(2, "%T eof on %d fid %d\n", c->fd, fid->fid);
662 if(verbose) fprint(2, "%T eof on %d fid %d ref %d\n", c->fd, fid->fid, fid->ref);
663 if(--fid->openfd == 0){
669 m->tx.fid = fid->fid;
680 chanfree(c->internal);
693 rerrstr(errs, sizeof errs);
695 /* XXX return here? */
697 if(verbose) fprint(2, "%T xopen pipe %d %d...", p[0], p[1]);
699 /* now we're committed. */
701 /* a new connection for this fid */
702 nc = emalloc(sizeof(Conn));
703 nc->internal = chancreate(sizeof(void*), 0);
709 nc->fdmode = m->tx.mode;
712 /* a thread to tend the pipe */
713 threadcreate(openfdthread, nc, STACK);
715 /* if mode is ORDWR, that openfdthread will write; start a reader */
716 if((m->tx.mode&3) == ORDWR){
717 nc = emalloc(sizeof(Conn));
718 nc->internal = chancreate(sizeof(void*), 0);
723 nc->fd = dup(p[0], -1);
724 threadcreate(openfdthread, nc, STACK);
727 /* steal fid from other connection */
728 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
731 /* rewrite as Ropenfd */
732 m->rx.type = Ropenfd;
734 m->rpkt = erealloc(m->rpkt, n+4);
735 PBIT32(m->rpkt+n, p[1]);
738 m->rpkt[4] = Ropenfd;
744 connoutthread(void *arg)
756 threadsetname("connout %s", c->dir);
757 while((m = recvq(outq)) != nil){
758 err = m->tx.type+1 != m->rx.type;
759 if(!err && m->isopenfd)
766 if(delhash(om->c->tag, om->ctag, om) == 0)
772 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
777 if(verbose) fprint(2, "%T auth error\n");
778 if(delhash(m->c->fid, m->afid->cfid, m->afid) == 0)
784 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
788 if(err || m->rx.nwqid < m->tx.nwname)
789 if(m->tx.fid != m->tx.newfid && m->newfid)
790 if(delhash(m->c->fid, m->newfid->cfid, m->newfid) == 0)
794 if(!err && m->fid->isdir && dotu && !m->c->dotu)
798 if(!err && dotu && !m->c->dotu)
799 cvtustat(&m->rx, &m->rpkt, 0);
803 m->fid->isdir = (m->rx.qid.type & QTDIR);
806 if(m->rx.type==Rerror && dotu && !c->dotu){
807 ename = estrdup(m->rx.ename);
809 repack(&m->rx, &m->rpkt, c->dotu);
813 if(delhash(m->c->tag, m->ctag, m) == 0)
815 if(verbose > 1) fprint(2, "%T fd#%d <- %F\n", c->fd, &m->rx);
816 rewritehdr(&m->rx, m->rpkt);
817 if(mwrite9p(io, c->fd, m->rpkt) < 0)
818 if(verbose) fprint(2, "%T write error: %r\n");
820 if(c->inputstalled && c->nmsg < MAXMSG)
829 outputthread(void *arg)
836 threadsetname("output");
837 while((m = recvq(outq)) != nil){
838 if(verbose > 1) fprint(2, "%T * <- %F\n", &m->tx);
839 rewritehdr(&m->tx, m->tpkt);
840 if(mwrite9p(io, 1, m->tpkt) < 0)
841 sysfatal("output error: %r");
845 fprint(2, "%T output eof\n");
850 inputthread(void *arg)
857 threadsetname("input");
858 if(verbose) fprint(2, "%T input thread\n");
861 while((pkt = read9ppkt(io, 0)) != nil){
864 fprint(2, "%T short 9P packet from server\n");
868 if(verbose > 2) fprint(2, "%T read %.*H\n", n, pkt);
870 if((m = msgget(tag)) == nil){
871 fprint(2, "%T unexpected 9P response tag %d\n", tag);
875 if((nn = convM2Su(pkt, n, &m->rx, dotu)) != n){
876 fprint(2, "%T bad packet - convM2S %d but %d\n", nn, n);
881 if(verbose > 1) fprint(2, "%T * -> %F%s\n", &m->rx,
882 m->internal ? " (internal)" : "");
886 sendp(m->c->internal, m);
888 sendq(m->c->outq, m);
893 //fprint(2, "%T input eof\n");
898 gethash(Hash **ht, uint n)
902 for(h=ht[n%NHASH]; h; h=h->next)
909 delhash(Hash **ht, uint n, void *v)
913 for(l=&ht[n%NHASH]; h=*l; l=&h->next)
916 if(verbose) fprint(2, "%T delhash %d got %p want %p\n", n, h->v, v);
927 puthash(Hash **ht, uint n, void *v)
933 h = emalloc(sizeof(Hash));
934 h->next = ht[n%NHASH];
951 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
952 if(nfidtab == xafid){
953 fidtab[nfidtab++] = nil;
954 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
956 fidtab[nfidtab] = emalloc(sizeof(Fid));
957 freefid = fidtab[nfidtab];
958 freefid->fid = nfidtab++;
989 if(verbose > 1) fprint(2, "%T msgincref @0x%lux %p tag %d/%d ref %d=>%d\n",
990 getcallerpc(&m), m, m->tag, m->ctag, m->ref, m->ref+1);
1000 msgtab = erealloc(msgtab, (nmsgtab+1)*sizeof(msgtab[0]));
1001 msgtab[nmsgtab] = emalloc(sizeof(Msg));
1002 freemsg = msgtab[nmsgtab];
1003 freemsg->tag = nmsgtab++;
1008 if(verbose > 1) fprint(2, "%T msgnew @0x%lux %p tag %d ref %d\n",
1009 getcallerpc(&x), m, m->tag, m->ref);
1015 * Clear data associated with connections, so that
1016 * if all msgs have been msgcleared, the connection
1017 * can be freed. Note that this does *not* free the tpkt
1018 * and rpkt; they are freed in msgput with the msg itself.
1019 * The io write thread might still be holding a ref to msg
1020 * even once the connection has finished with it.
1045 if(m->rx.type == Ropenfd && m->rx.unixfd >= 0){
1046 close(m->rx.unixfd);
1057 if(verbose > 1) fprint(2, "%T msgput 0x%lux %p tag %d/%d ref %d\n",
1058 getcallerpc(&m), m, m->tag, m->ctag, m->ref);
1083 if(n < 0 || n >= nmsgtab)
1088 if(verbose) fprint(2, "%T msgget %d = %p\n", n, m);
1102 sysfatal("out of memory allocating %d", n);
1108 erealloc(void *v, int n)
1113 sysfatal("out of memory reallocating %d", n);
1118 typedef struct Qel Qel;
1139 q = mallocz(sizeof(Queue), 1);
1147 sendq(Queue *q, void *p)
1151 e = emalloc(sizeof(Qel));
1155 werrstr("hungup queue");
1178 while(q->head == nil && !q->hungup)
1193 read9ppkt(Ioproc *io, int fd)
1198 n = ioreadn(io, fd, buf, 4);
1204 nn = ioreadn(io, fd, pkt+4, n-4);
1209 /* would do this if we ever got one of these, but we only generate them
1210 if(pkt[4] == Ropenfd){
1211 newfd = iorecvfd(io, fd);
1212 PBIT32(pkt+n-4, newfd);
1219 mread9p(Ioproc *io, int fd, int dotu)
1225 if((pkt = read9ppkt(io, fd)) == nil)
1231 nn = convM2Su(pkt, n, &m->tx, dotu);
1233 fprint(2, "%T read bad packet from %d\n", fd);
1240 mwrite9p(Ioproc *io, int fd, uchar *pkt)
1245 if(verbose > 2) fprint(2, "%T write %d %d %.*H\n", fd, n, n, pkt);
1246 if(verbose > 1) fprint(2, "%T before iowrite\n");
1247 if(iowrite(io, fd, pkt, n) != n){
1248 fprint(2, "%T write error: %r\n");
1251 if(verbose > 1) fprint(2, "%T after iowrite\n");
1252 if(pkt[4] == Ropenfd){
1253 nfd = GBIT32(pkt+n-4);
1254 if(iosendfd(io, fd, nfd) < 0){
1255 fprint(2, "%T send fd error: %r\n");
1263 restring(uchar *pkt, int pn, char *s)
1267 if(s < (char*)pkt || s >= (char*)pkt+pn)
1272 PBIT16((uchar*)s-1, n);
1276 repack(Fcall *f, uchar **ppkt, int dotu)
1283 nn = sizeS2Mu(f, dotu);
1289 n = convS2Mu(f, pkt, nn, dotu);
1291 sysfatal("convS2M conversion error");
1293 sysfatal("convS2Mu and sizeS2Mu disagree");
1297 rewritehdr(Fcall *f, uchar *pkt)
1302 PBIT16(pkt+5, f->tag);
1306 restring(pkt, n, f->version);
1309 PBIT32(pkt+7, f->afid);
1310 restring(pkt, n, f->uname);
1311 restring(pkt, n, f->aname);
1314 PBIT16(pkt+7, f->oldtag);
1317 restring(pkt, n, f->uname);
1318 restring(pkt, n, f->aname);
1319 PBIT32(pkt+7, f->fid);
1320 PBIT32(pkt+11, f->afid);
1323 PBIT32(pkt+7, f->fid);
1324 PBIT32(pkt+11, f->newfid);
1325 for(i=0; i<f->nwname; i++)
1326 restring(pkt, n, f->wname[i]);
1329 restring(pkt, n, f->name);
1338 PBIT32(pkt+7, f->fid);
1341 restring(pkt, n, f->ename);
1347 _iolisten(va_list *arg)
1351 a = va_arg(*arg, char*);
1352 b = va_arg(*arg, char*);
1353 return listen(a, b);
1357 iolisten(Ioproc *io, char *a, char *b)
1359 return iocall(io, _iolisten, a, b);
1363 _ioaccept(va_list *arg)
1368 fd = va_arg(*arg, int);
1369 dir = va_arg(*arg, char*);
1370 return accept(fd, dir);
1374 ioaccept(Ioproc *io, int fd, char *dir)
1376 return iocall(io, _ioaccept, fd, dir);
1382 static char *mon[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1383 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1387 tm = *localtime(time(0));
1388 return fmtprint(fmt, "%s %2d %02d:%02d:%02d.%03d",
1389 mon[tm.mon], tm.mday, tm.hour, tm.min, tm.sec,
1390 (int)(ns%1000000000)/1000000);
1394 cvtustat(Fcall *f, uchar **fpkt, int tounix)
1401 str = emalloc(f->nstat);
1402 n = convM2Du(f->stat, f->nstat, &dir, str, !tounix);
1408 n = sizeD2Mu(&dir, tounix);
1410 if(convD2Mu(&dir, buf, n, tounix) != n)
1411 sysfatal("convD2Mu conversion error");
1415 repack(f, fpkt, dotu);
1417 f->stat = nil; /* is this okay ??? */
1424 stripudirread(Msg* msg)
1437 for(i = 0; i < rx->count; i += m){
1438 m = BIT16SZ + GBIT16(&rx->data[i]);
1439 if(statchecku((uchar*)&rx->data[i], m, 1) < 0)
1447 buf = emalloc(rx->count);
1450 for(i = 0; i < rx->count; i += m){
1451 m = BIT16SZ + GBIT16(&rx->data[i]);
1452 if(convM2Du((uchar*)&rx->data[i], m, &d, str, 1) != m){
1458 n = convD2M(&d, &buf[nn], rx->count - nn);
1469 rx->data = (char*)buf;
1471 repack(&msg->rx, &msg->rpkt, 0);
1474 rx->data = nil; /* is this okay ??? */