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;
91 void *gethash(Hash**, uint);
92 int puthash(Hash**, uint, void*);
93 int delhash(Hash**, uint, void*);
94 Msg *mread9p(Ioproc*, int, int);
95 int mwrite9p(Ioproc*, int, uchar*);
96 uchar *read9ppkt(Ioproc*, int);
97 int write9ppkt(int, uchar*);
102 void msgincref(Msg*);
106 void *erealloc(void*, int);
108 int sendq(Queue*, void*);
110 void connthread(void*);
111 void connoutthread(void*);
112 void listenthread(void*);
113 void outputthread(void*);
114 void inputthread(void*);
115 void rewritehdr(Fcall*, uchar*);
116 void repack(Fcall*, uchar**, int);
117 int tlisten(char*, char*);
118 int taccept(int, char*);
119 int iolisten(Ioproc*, char*, char*);
120 int ioaccept(Ioproc*, int, char*);
121 int iorecvfd(Ioproc*, int);
122 int iosendfd(Ioproc*, int, int);
123 void mainproc(void*);
124 int ignorepipe(void*, char*);
126 void dorootstat(void);
127 int stripudirread(Msg*);
128 int cvtustat(Fcall*, uchar**, int);
133 fprint(2, "usage: 9pserve [-lv] [-A aname afid] [-M msize] address\n");
134 fprint(2, "\treads/writes 9P messages on stdin/stdout\n");
135 threadexitsall("usage");
139 extern int _threaddebuglevel;
141 threadmain(int argc, char **argv)
146 x = getenv("verbose9pserve");
149 fprint(2, "verbose9pserve %s => %d\n", x, verbose);
156 xaname = EARGF(usage());
157 xafid = atoi(EARGF(usage()));
161 msize = atoi(EARGF(usage()));
174 if(attached && !versioned){
175 fprint(2, "-A must be used with -M\n");
183 fmtinstall('T', timefmt);
185 if((afd = announce(addr, adir)) < 0)
186 sysfatal("announce %s: %r", addr);
188 if(strncmp(addr, "unix!", 5) == 0)
190 file = smprint("%s.log", addr);
192 sysfatal("smprint log: %r");
193 if((fd = create(file, OWRITE, 0666)) < 0)
194 sysfatal("create %s: %r", file);
199 if(verbose) fprint(2, "%T 9pserve running\n");
200 proccreate(mainproc, nil, STACK);
210 atnotify(ignorepipe, 1);
211 fmtinstall('D', dirfmt);
212 fmtinstall('M', dirmodefmt);
213 fmtinstall('F', fcallfmt);
214 fmtinstall('H', encodefmt);
221 f.version = "9P2000.u";
224 n = convS2M(&f, vbuf, sizeof vbuf);
226 sysfatal("convS2M conversion error");
227 if(verbose > 1) fprint(2, "%T * <- %F\n", &f);
228 nn = write(1, vbuf, n);
230 sysfatal("error writing Tversion: %r\n");
231 n = read9pmsg(0, vbuf, sizeof vbuf);
232 if(convM2S(vbuf, n, &f) != n)
233 sysfatal("convM2S failure");
236 if(verbose > 1) fprint(2, "%T * -> %F\n", &f);
237 dotu = strncmp(f.version, "9P2000.u", 8) == 0;
240 threadcreate(inputthread, nil, STACK);
241 threadcreate(outputthread, nil, STACK);
246 threadcreate(listenthread, nil, STACK);
251 ignorepipe(void *v, char *s)
254 if(strcmp(s, "sys: write on closed pipe") == 0)
256 if(strcmp(s, "sys: tstp") == 0)
258 fprint(2, "9pserve %s: %T note: %s\n", addr, s);
263 listenthread(void *arg)
270 threadsetname("listen %s", adir);
272 c = emalloc(sizeof(Conn));
273 c->fd = iolisten(io, adir, c->dir);
275 if(verbose) fprint(2, "%T listen: %r\n");
280 c->inc = chancreate(sizeof(void*), 0);
281 c->internal = chancreate(sizeof(void*), 0);
284 if(verbose) fprint(2, "%T incoming call on %s\n", c->dir);
285 threadcreate(connthread, c, STACK);
294 n = sizeS2Mu(&m->rx, m->c->dotu);
295 m->rpkt = emalloc(n);
296 nn = convS2Mu(&m->rx, m->rpkt, n, m->c->dotu);
298 sysfatal("convS2Mu conversion error");
300 sysfatal("sizeS2Mu and convS2Mu disagree");
301 sendq(m->c->outq, m);
309 n = sizeS2Mu(&m->tx, m->c->dotu);
310 m->tpkt = emalloc(n);
311 nn = convS2Mu(&m->tx, m->tpkt, n, m->c->dotu);
313 sysfatal("convS2Mu conversion error");
315 sysfatal("sizeS2Mu and convS2Mu disagree");
320 err(Msg *m, char *ename)
324 m->rx.tag = m->tx.tag;
333 t = emalloc(strlen(s)+1);
339 connthread(void *arg)
349 threadsetname("conn %s", c->dir);
351 fd = ioaccept(io, c->fd, c->dir);
353 if(verbose) fprint(2, "%T accept %s: %r\n", c->dir);
358 threadcreate(connoutthread, c, STACK);
359 while((m = mread9p(io, c->fd, c->dotu)) != nil){
360 if(verbose > 1) fprint(2, "%T fd#%d -> %F\n", c->fd, &m->tx);
364 if(verbose > 1) fprint(2, "%T fd#%d: new msg %p\n", c->fd, m);
365 if(puthash(c->tag, m->tx.tag, m) < 0){
366 err(m, "duplicate tag");
372 m->rx.tag = m->tx.tag;
373 m->rx.msize = m->tx.msize;
374 if(m->rx.msize > msize)
376 m->rx.version = "9P2000";
378 if(dotu && strncmp(m->tx.version, "9P2000.u", 8) == 0){
379 m->rx.version = "9P2000.u";
382 m->rx.type = Rversion;
386 if((m->oldm = gethash(c->tag, m->tx.oldtag)) == nil){
387 m->rx.tag = m->tx.tag;
396 if(m->tx.afid != NOFID
397 && (m->afid = gethash(c->fid, m->tx.afid)) == nil){
398 err(m, "unknown fid");
403 m->fid = fidnew(m->tx.fid);
404 if(puthash(c->fid, m->tx.fid, m->fid) < 0){
405 err(m, "duplicate fid");
409 if(attached && m->afid==nil){
410 if(m->tx.aname[0] && strcmp(xaname, m->tx.aname) != 0){
411 err(m, "invalid attach name");
415 m->tx.aname = xaname;
416 m->tx.uname = estrdup(m->tx.uname);
417 repack(&m->tx, &m->tpkt, c->dotu);
423 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
424 err(m, "unknown fid");
428 if(m->tx.newfid == m->tx.fid){
432 m->newfid = fidnew(m->tx.newfid);
433 if(puthash(c->fid, m->tx.newfid, m->newfid) < 0){
434 err(m, "duplicate fid");
442 err(m, "authentication not required");
445 m->afid = fidnew(m->tx.afid);
446 if(puthash(c->fid, m->tx.afid, m->afid) < 0){
447 err(m, "duplicate fid");
453 if(m->tx.mode&~(OTRUNC|3)){
454 err(m, "bad openfd mode");
469 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
470 err(m, "unknown fid");
474 if(m->tx.type==Twstat && dotu && !c->dotu){
475 if(cvtustat(&m->tx, &m->tpkt, 1) < 0){
476 err(m, "cannot convert stat buffer");
480 if(m->tx.type==Tread && m->fid->isdir && dotu && !c->dotu){
481 if(m->tx.offset = m->fid->coffset)
482 m->tx.offset = m->fid->offset;
484 m->fid->offset = m->fid->coffset;
489 /* have everything - translate and send */
494 m->tx.fid = m->fid->fid;
496 m->tx.newfid = m->newfid->fid;
498 m->tx.afid = m->afid->fid;
500 m->tx.oldtag = m->oldm->tag;
501 /* reference passes to outq */
503 while(c->nmsg >= MAXMSG){
509 if(verbose) fprint(2, "%T fd#%d eof; flushing conn\n", c->fd);
511 /* flush the output queue */
513 while(c->outq != nil)
516 /* flush all outstanding messages */
517 for(i=0; i<NHASH; i++){
518 for(h=c->tag[i]; h; h=hnext){
526 m->tx.oldtag = om->tag;
529 msgincref(m); /* for outq */
531 mm = recvp(c->internal);
533 msgput(m); /* got from recvp */
534 msgput(m); /* got from msgnew */
535 msgput(om); /* got from hash table */
541 /* clunk all outstanding fids */
542 for(i=0; i<NHASH; i++){
543 for(h=c->fid[i]; h; h=hnext){
556 mm = recvp(c->internal);
559 msgput(m); /* got from recvp */
560 msgput(m); /* got from msgnew */
561 fidput(f); /* got from hash table */
569 assert(c->nmsg == 0);
570 assert(c->nfid == 0);
572 chanfree(c->internal);
582 openfdthread(void *v)
595 threadsetname("openfd %s", c->fdfid);
598 if(c->fdmode == OREAD){
600 if(verbose) fprint(2, "%T tread...");
605 m->tx.count = msize - IOHDRSZ;
606 m->tx.fid = fid->fid;
614 if(m->rx.type == Rerror){
615 // fprint(2, "%T read error: %s\n", m->rx.ename);
621 if(iowrite(io, c->fd, m->rx.data, m->rx.count) != m->rx.count){
622 // fprint(2, "%T pipe write error: %r\n");
631 if(verbose) fprint(2, "%T twrite...");
635 if((n=ioread(io, c->fd, buf, n)) <= 0){
637 fprint(2, "%T pipe read error: %r\n");
644 m->tx.fid = fid->fid;
654 if(m->rx.type == Rerror){
655 // fprint(2, "%T write error: %s\n", m->rx.ename);
663 if(verbose) fprint(2, "%T eof on %d fid %d\n", c->fd, fid->fid);
670 if(verbose) fprint(2, "%T eof on %d fid %d ref %d\n", c->fd, fid->fid, fid->ref);
671 if(--fid->openfd == 0){
677 m->tx.fid = fid->fid;
688 chanfree(c->internal);
701 rerrstr(errs, sizeof errs);
703 /* XXX return here? */
705 if(verbose) fprint(2, "%T xopen pipe %d %d...", p[0], p[1]);
707 /* now we're committed. */
709 /* a new connection for this fid */
710 nc = emalloc(sizeof(Conn));
711 nc->internal = chancreate(sizeof(void*), 0);
717 nc->fdmode = m->tx.mode;
720 /* a thread to tend the pipe */
721 threadcreate(openfdthread, nc, STACK);
723 /* if mode is ORDWR, that openfdthread will write; start a reader */
724 if((m->tx.mode&3) == ORDWR){
725 nc = emalloc(sizeof(Conn));
726 nc->internal = chancreate(sizeof(void*), 0);
731 nc->fd = dup(p[0], -1);
732 threadcreate(openfdthread, nc, STACK);
735 /* steal fid from other connection */
736 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
739 /* rewrite as Ropenfd */
740 m->rx.type = Ropenfd;
742 m->rpkt = erealloc(m->rpkt, n+4);
743 PBIT32(m->rpkt+n, p[1]);
746 m->rpkt[4] = Ropenfd;
752 connoutthread(void *arg)
764 threadsetname("connout %s", c->dir);
765 while((m = recvq(outq)) != nil){
766 err = m->tx.type+1 != m->rx.type;
767 if(!err && m->isopenfd)
774 if(delhash(om->c->tag, om->ctag, om) == 0)
780 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
785 if(verbose) fprint(2, "%T auth error\n");
786 if(delhash(m->c->fid, m->afid->cfid, m->afid) == 0)
792 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
796 if(err || m->rx.nwqid < m->tx.nwname)
797 if(m->tx.fid != m->tx.newfid && m->newfid)
798 if(delhash(m->c->fid, m->newfid->cfid, m->newfid) == 0)
802 if(!err && m->fid->isdir && dotu && !m->c->dotu){
803 m->fid->offset += m->rx.count;
805 m->fid->coffset += m->rx.count;
809 if(!err && dotu && !m->c->dotu)
810 cvtustat(&m->rx, &m->rpkt, 0);
814 m->fid->isdir = (m->rx.qid.type & QTDIR);
817 if(m->rx.type==Rerror && dotu && !c->dotu){
818 ename = estrdup(m->rx.ename);
820 repack(&m->rx, &m->rpkt, c->dotu);
824 if(delhash(m->c->tag, m->ctag, m) == 0)
826 if(verbose > 1) fprint(2, "%T fd#%d <- %F\n", c->fd, &m->rx);
827 rewritehdr(&m->rx, m->rpkt);
828 if(mwrite9p(io, c->fd, m->rpkt) < 0)
829 if(verbose) fprint(2, "%T write error: %r\n");
831 if(c->inputstalled && c->nmsg < MAXMSG)
840 outputthread(void *arg)
847 threadsetname("output");
848 while((m = recvq(outq)) != nil){
849 if(verbose > 1) fprint(2, "%T * <- %F\n", &m->tx);
850 rewritehdr(&m->tx, m->tpkt);
851 if(mwrite9p(io, 1, m->tpkt) < 0)
852 sysfatal("output error: %r");
856 fprint(2, "%T output eof\n");
861 inputthread(void *arg)
868 threadsetname("input");
869 if(verbose) fprint(2, "%T input thread\n");
872 while((pkt = read9ppkt(io, 0)) != nil){
875 fprint(2, "%T short 9P packet from server\n");
879 if(verbose > 2) fprint(2, "%T read %.*H\n", n, pkt);
881 if((m = msgget(tag)) == nil){
882 fprint(2, "%T unexpected 9P response tag %d\n", tag);
886 if((nn = convM2Su(pkt, n, &m->rx, dotu)) != n){
887 fprint(2, "%T bad packet - convM2S %d but %d\n", nn, n);
892 if(verbose > 1) fprint(2, "%T * -> %F%s\n", &m->rx,
893 m->internal ? " (internal)" : "");
897 sendp(m->c->internal, m);
899 sendq(m->c->outq, m);
904 //fprint(2, "%T input eof\n");
909 gethash(Hash **ht, uint n)
913 for(h=ht[n%NHASH]; h; h=h->next)
920 delhash(Hash **ht, uint n, void *v)
924 for(l=&ht[n%NHASH]; h=*l; l=&h->next)
927 if(verbose) fprint(2, "%T delhash %d got %p want %p\n", n, h->v, v);
938 puthash(Hash **ht, uint n, void *v)
944 h = emalloc(sizeof(Hash));
945 h->next = ht[n%NHASH];
962 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
963 if(nfidtab == xafid){
964 fidtab[nfidtab++] = nil;
965 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
967 fidtab[nfidtab] = emalloc(sizeof(Fid));
968 freefid = fidtab[nfidtab];
969 freefid->fid = nfidtab++;
1002 if(verbose > 1) fprint(2, "%T msgincref @0x%lux %p tag %d/%d ref %d=>%d\n",
1003 getcallerpc(&m), m, m->tag, m->ctag, m->ref, m->ref+1);
1013 msgtab = erealloc(msgtab, (nmsgtab+1)*sizeof(msgtab[0]));
1014 msgtab[nmsgtab] = emalloc(sizeof(Msg));
1015 freemsg = msgtab[nmsgtab];
1016 freemsg->tag = nmsgtab++;
1021 if(verbose > 1) fprint(2, "%T msgnew @0x%lux %p tag %d ref %d\n",
1022 getcallerpc(&x), m, m->tag, m->ref);
1028 * Clear data associated with connections, so that
1029 * if all msgs have been msgcleared, the connection
1030 * can be freed. Note that this does *not* free the tpkt
1031 * and rpkt; they are freed in msgput with the msg itself.
1032 * The io write thread might still be holding a ref to msg
1033 * even once the connection has finished with it.
1058 if(m->rx.type == Ropenfd && m->rx.unixfd >= 0){
1059 close(m->rx.unixfd);
1070 if(verbose > 1) fprint(2, "%T msgput 0x%lux %p tag %d/%d ref %d\n",
1071 getcallerpc(&m), m, m->tag, m->ctag, m->ref);
1096 if(n < 0 || n >= nmsgtab)
1101 if(verbose) fprint(2, "%T msgget %d = %p\n", n, m);
1115 sysfatal("out of memory allocating %d", n);
1121 erealloc(void *v, int n)
1126 sysfatal("out of memory reallocating %d", n);
1131 typedef struct Qel Qel;
1152 q = mallocz(sizeof(Queue), 1);
1160 sendq(Queue *q, void *p)
1164 e = emalloc(sizeof(Qel));
1168 werrstr("hungup queue");
1191 while(q->head == nil && !q->hungup)
1206 read9ppkt(Ioproc *io, int fd)
1211 n = ioreadn(io, fd, buf, 4);
1217 nn = ioreadn(io, fd, pkt+4, n-4);
1222 /* would do this if we ever got one of these, but we only generate them
1223 if(pkt[4] == Ropenfd){
1224 newfd = iorecvfd(io, fd);
1225 PBIT32(pkt+n-4, newfd);
1232 mread9p(Ioproc *io, int fd, int dotu)
1238 if((pkt = read9ppkt(io, fd)) == nil)
1244 nn = convM2Su(pkt, n, &m->tx, dotu);
1246 fprint(2, "%T read bad packet from %d\n", fd);
1253 mwrite9p(Ioproc *io, int fd, uchar *pkt)
1258 if(verbose > 2) fprint(2, "%T write %d %d %.*H\n", fd, n, n, pkt);
1259 if(verbose > 1) fprint(2, "%T before iowrite\n");
1260 if(iowrite(io, fd, pkt, n) != n){
1261 fprint(2, "%T write error: %r\n");
1264 if(verbose > 1) fprint(2, "%T after iowrite\n");
1265 if(pkt[4] == Ropenfd){
1266 nfd = GBIT32(pkt+n-4);
1267 if(iosendfd(io, fd, nfd) < 0){
1268 fprint(2, "%T send fd error: %r\n");
1276 restring(uchar *pkt, int pn, char *s)
1280 if(s < (char*)pkt || s >= (char*)pkt+pn)
1285 PBIT16((uchar*)s-1, n);
1289 repack(Fcall *f, uchar **ppkt, int dotu)
1296 nn = sizeS2Mu(f, dotu);
1302 n = convS2Mu(f, pkt, nn, dotu);
1304 sysfatal("convS2M conversion error");
1306 sysfatal("convS2Mu and sizeS2Mu disagree");
1310 rewritehdr(Fcall *f, uchar *pkt)
1315 PBIT16(pkt+5, f->tag);
1319 restring(pkt, n, f->version);
1322 PBIT32(pkt+7, f->afid);
1323 restring(pkt, n, f->uname);
1324 restring(pkt, n, f->aname);
1327 PBIT16(pkt+7, f->oldtag);
1330 restring(pkt, n, f->uname);
1331 restring(pkt, n, f->aname);
1332 PBIT32(pkt+7, f->fid);
1333 PBIT32(pkt+11, f->afid);
1336 PBIT32(pkt+7, f->fid);
1337 PBIT32(pkt+11, f->newfid);
1338 for(i=0; i<f->nwname; i++)
1339 restring(pkt, n, f->wname[i]);
1342 restring(pkt, n, f->name);
1350 PBIT32(pkt+7, f->fid);
1353 PBIT32(pkt+7, f->fid);
1354 PBIT64(pkt+11, f->offset);
1357 restring(pkt, n, f->ename);
1363 _iolisten(va_list *arg)
1367 a = va_arg(*arg, char*);
1368 b = va_arg(*arg, char*);
1369 return listen(a, b);
1373 iolisten(Ioproc *io, char *a, char *b)
1375 return iocall(io, _iolisten, a, b);
1379 _ioaccept(va_list *arg)
1384 fd = va_arg(*arg, int);
1385 dir = va_arg(*arg, char*);
1386 return accept(fd, dir);
1390 ioaccept(Ioproc *io, int fd, char *dir)
1392 return iocall(io, _ioaccept, fd, dir);
1398 static char *mon[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1399 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1403 tm = *localtime(time(0));
1404 return fmtprint(fmt, "%s %2d %02d:%02d:%02d.%03d",
1405 mon[tm.mon], tm.mday, tm.hour, tm.min, tm.sec,
1406 (int)(ns%1000000000)/1000000);
1410 cvtustat(Fcall *f, uchar **fpkt, int tounix)
1417 str = emalloc(f->nstat);
1418 n = convM2Du(f->stat, f->nstat, &dir, str, !tounix);
1424 n = sizeD2Mu(&dir, tounix);
1426 if(convD2Mu(&dir, buf, n, tounix) != n)
1427 sysfatal("convD2Mu conversion error");
1431 repack(f, fpkt, dotu);
1433 f->stat = nil; /* is this okay ??? */
1440 stripudirread(Msg* msg)
1453 for(i = 0; i < rx->count; i += m){
1454 m = BIT16SZ + GBIT16(&rx->data[i]);
1455 if(statchecku((uchar*)&rx->data[i], m, 1) < 0)
1463 buf = emalloc(rx->count);
1466 for(i = 0; i < rx->count; i += m){
1467 m = BIT16SZ + GBIT16(&rx->data[i]);
1468 if(convM2Du((uchar*)&rx->data[i], m, &d, str, 1) != m){
1474 n = convD2M(&d, &buf[nn], rx->count - nn);
1485 rx->data = (char*)buf;
1487 repack(&msg->rx, &msg->rpkt, 0);
1490 rx->data = nil; /* is this okay ??? */