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(dotu && !c->dotu && (m->tx.perm&(DMSYMLINK|DMDEVICE|DMNAMEDPIPE|DMSOCKET))){
454 err(m, "unsupported file type");
459 if(m->tx.mode&~(OTRUNC|3)){
460 err(m, "bad openfd mode");
475 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
476 err(m, "unknown fid");
480 if(m->tx.type==Twstat && dotu && !c->dotu){
481 if(cvtustat(&m->tx, &m->tpkt, 1) < 0){
482 err(m, "cannot convert stat buffer");
486 if(m->tx.type==Tread && m->fid->isdir && dotu && !c->dotu){
487 if(m->tx.offset = m->fid->coffset)
488 m->tx.offset = m->fid->offset;
490 m->fid->offset = m->fid->coffset;
495 /* have everything - translate and send */
500 m->tx.fid = m->fid->fid;
502 m->tx.newfid = m->newfid->fid;
504 m->tx.afid = m->afid->fid;
506 m->tx.oldtag = m->oldm->tag;
507 /* reference passes to outq */
509 while(c->nmsg >= MAXMSG){
515 if(verbose) fprint(2, "%T fd#%d eof; flushing conn\n", c->fd);
517 /* flush the output queue */
519 while(c->outq != nil)
522 /* flush all outstanding messages */
523 for(i=0; i<NHASH; i++){
524 for(h=c->tag[i]; h; h=hnext){
532 m->tx.oldtag = om->tag;
535 msgincref(m); /* for outq */
537 mm = recvp(c->internal);
539 msgput(m); /* got from recvp */
540 msgput(m); /* got from msgnew */
541 msgput(om); /* got from hash table */
547 /* clunk all outstanding fids */
548 for(i=0; i<NHASH; i++){
549 for(h=c->fid[i]; h; h=hnext){
562 mm = recvp(c->internal);
565 msgput(m); /* got from recvp */
566 msgput(m); /* got from msgnew */
567 fidput(f); /* got from hash table */
575 assert(c->nmsg == 0);
576 assert(c->nfid == 0);
578 chanfree(c->internal);
588 openfdthread(void *v)
601 threadsetname("openfd %s", c->fdfid);
604 if(c->fdmode == OREAD){
606 if(verbose) fprint(2, "%T tread...");
611 m->tx.count = msize - IOHDRSZ;
612 m->tx.fid = fid->fid;
620 if(m->rx.type == Rerror){
621 /* fprint(2, "%T read error: %s\n", m->rx.ename); */
627 if(iowrite(io, c->fd, m->rx.data, m->rx.count) != m->rx.count){
628 /* fprint(2, "%T pipe write error: %r\n"); */
637 if(verbose) fprint(2, "%T twrite...");
641 if((n=ioread(io, c->fd, buf, n)) <= 0){
643 fprint(2, "%T pipe read error: %r\n");
650 m->tx.fid = fid->fid;
660 if(m->rx.type == Rerror){
661 /* fprint(2, "%T write error: %s\n", m->rx.ename); */
669 if(verbose) fprint(2, "%T eof on %d fid %d\n", c->fd, fid->fid);
676 if(verbose) fprint(2, "%T eof on %d fid %d ref %d\n", c->fd, fid->fid, fid->ref);
677 if(--fid->openfd == 0){
683 m->tx.fid = fid->fid;
694 chanfree(c->internal);
707 rerrstr(errs, sizeof errs);
709 /* XXX return here? */
711 if(verbose) fprint(2, "%T xopen pipe %d %d...", p[0], p[1]);
713 /* now we're committed. */
715 /* a new connection for this fid */
716 nc = emalloc(sizeof(Conn));
717 nc->internal = chancreate(sizeof(void*), 0);
723 nc->fdmode = m->tx.mode;
726 /* a thread to tend the pipe */
727 threadcreate(openfdthread, nc, STACK);
729 /* if mode is ORDWR, that openfdthread will write; start a reader */
730 if((m->tx.mode&3) == ORDWR){
731 nc = emalloc(sizeof(Conn));
732 nc->internal = chancreate(sizeof(void*), 0);
737 nc->fd = dup(p[0], -1);
738 threadcreate(openfdthread, nc, STACK);
741 /* steal fid from other connection */
742 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
745 /* rewrite as Ropenfd */
746 m->rx.type = Ropenfd;
748 m->rpkt = erealloc(m->rpkt, n+4);
749 PBIT32(m->rpkt+n, p[1]);
752 m->rpkt[4] = Ropenfd;
758 connoutthread(void *arg)
770 threadsetname("connout %s", c->dir);
771 while((m = recvq(outq)) != nil){
772 err = m->tx.type+1 != m->rx.type;
773 if(!err && m->isopenfd)
780 if(delhash(om->c->tag, om->ctag, om) == 0)
786 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
791 if(verbose) fprint(2, "%T auth error\n");
792 if(delhash(m->c->fid, m->afid->cfid, m->afid) == 0)
798 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
802 if(err || m->rx.nwqid < m->tx.nwname)
803 if(m->tx.fid != m->tx.newfid && m->newfid)
804 if(delhash(m->c->fid, m->newfid->cfid, m->newfid) == 0)
808 if(!err && m->fid->isdir && dotu && !m->c->dotu){
809 m->fid->offset += m->rx.count;
811 m->fid->coffset += m->rx.count;
815 if(!err && dotu && !m->c->dotu)
816 cvtustat(&m->rx, &m->rpkt, 0);
820 m->fid->isdir = (m->rx.qid.type & QTDIR);
823 if(m->rx.type==Rerror && dotu && !c->dotu){
824 ename = estrdup(m->rx.ename);
826 repack(&m->rx, &m->rpkt, c->dotu);
830 if(delhash(m->c->tag, m->ctag, m) == 0)
832 if(verbose > 1) fprint(2, "%T fd#%d <- %F\n", c->fd, &m->rx);
833 rewritehdr(&m->rx, m->rpkt);
834 if(mwrite9p(io, c->fd, m->rpkt) < 0)
835 if(verbose) fprint(2, "%T write error: %r\n");
837 if(c->inputstalled && c->nmsg < MAXMSG)
846 outputthread(void *arg)
853 threadsetname("output");
854 while((m = recvq(outq)) != nil){
855 if(verbose > 1) fprint(2, "%T * <- %F\n", &m->tx);
856 rewritehdr(&m->tx, m->tpkt);
857 if(mwrite9p(io, 1, m->tpkt) < 0)
858 sysfatal("output error: %r");
862 fprint(2, "%T output eof\n");
867 inputthread(void *arg)
874 threadsetname("input");
875 if(verbose) fprint(2, "%T input thread\n");
878 while((pkt = read9ppkt(io, 0)) != nil){
881 fprint(2, "%T short 9P packet from server\n");
885 if(verbose > 2) fprint(2, "%T read %.*H\n", n, pkt);
887 if((m = msgget(tag)) == nil){
888 fprint(2, "%T unexpected 9P response tag %d\n", tag);
892 if((nn = convM2Su(pkt, n, &m->rx, dotu)) != n){
893 fprint(2, "%T bad packet - convM2S %d but %d\n", nn, n);
898 if(verbose > 1) fprint(2, "%T * -> %F%s\n", &m->rx,
899 m->internal ? " (internal)" : "");
903 sendp(m->c->internal, m);
905 sendq(m->c->outq, m);
910 /*fprint(2, "%T input eof\n"); */
915 gethash(Hash **ht, uint n)
919 for(h=ht[n%NHASH]; h; h=h->next)
926 delhash(Hash **ht, uint n, void *v)
930 for(l=&ht[n%NHASH]; h=*l; l=&h->next)
933 if(verbose) fprint(2, "%T delhash %d got %p want %p\n", n, h->v, v);
944 puthash(Hash **ht, uint n, void *v)
950 h = emalloc(sizeof(Hash));
951 h->next = ht[n%NHASH];
968 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
969 if(nfidtab == xafid){
970 fidtab[nfidtab++] = nil;
971 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
973 fidtab[nfidtab] = emalloc(sizeof(Fid));
974 freefid = fidtab[nfidtab];
975 freefid->fid = nfidtab++;
1008 if(verbose > 1) fprint(2, "%T msgincref @0x%lux %p tag %d/%d ref %d=>%d\n",
1009 getcallerpc(&m), m, m->tag, m->ctag, m->ref, m->ref+1);
1019 msgtab = erealloc(msgtab, (nmsgtab+1)*sizeof(msgtab[0]));
1020 msgtab[nmsgtab] = emalloc(sizeof(Msg));
1021 freemsg = msgtab[nmsgtab];
1022 freemsg->tag = nmsgtab++;
1027 if(verbose > 1) fprint(2, "%T msgnew @0x%lux %p tag %d ref %d\n",
1028 getcallerpc(&x), m, m->tag, m->ref);
1034 * Clear data associated with connections, so that
1035 * if all msgs have been msgcleared, the connection
1036 * can be freed. Note that this does *not* free the tpkt
1037 * and rpkt; they are freed in msgput with the msg itself.
1038 * The io write thread might still be holding a ref to msg
1039 * even once the connection has finished with it.
1064 if(m->rx.type == Ropenfd && m->rx.unixfd >= 0){
1065 close(m->rx.unixfd);
1076 if(verbose > 1) fprint(2, "%T msgput 0x%lux %p tag %d/%d ref %d\n",
1077 getcallerpc(&m), m, m->tag, m->ctag, m->ref);
1102 if(n < 0 || n >= nmsgtab)
1107 if(verbose) fprint(2, "%T msgget %d = %p\n", n, m);
1121 sysfatal("out of memory allocating %d", n);
1127 erealloc(void *v, int n)
1132 sysfatal("out of memory reallocating %d", n);
1137 typedef struct Qel Qel;
1158 q = mallocz(sizeof(Queue), 1);
1166 sendq(Queue *q, void *p)
1170 e = emalloc(sizeof(Qel));
1174 werrstr("hungup queue");
1197 while(q->head == nil && !q->hungup)
1212 read9ppkt(Ioproc *io, int fd)
1217 n = ioreadn(io, fd, buf, 4);
1223 nn = ioreadn(io, fd, pkt+4, n-4);
1228 /* would do this if we ever got one of these, but we only generate them
1229 if(pkt[4] == Ropenfd){
1230 newfd = iorecvfd(io, fd);
1231 PBIT32(pkt+n-4, newfd);
1238 mread9p(Ioproc *io, int fd, int dotu)
1244 if((pkt = read9ppkt(io, fd)) == nil)
1250 nn = convM2Su(pkt, n, &m->tx, dotu);
1252 fprint(2, "%T read bad packet from %d\n", fd);
1259 mwrite9p(Ioproc *io, int fd, uchar *pkt)
1264 if(verbose > 2) fprint(2, "%T write %d %d %.*H\n", fd, n, n, pkt);
1265 if(verbose > 1) fprint(2, "%T before iowrite\n");
1266 if(iowrite(io, fd, pkt, n) != n){
1267 fprint(2, "%T write error: %r\n");
1270 if(verbose > 1) fprint(2, "%T after iowrite\n");
1271 if(pkt[4] == Ropenfd){
1272 nfd = GBIT32(pkt+n-4);
1273 if(iosendfd(io, fd, nfd) < 0){
1274 fprint(2, "%T send fd error: %r\n");
1282 restring(uchar *pkt, int pn, char *s)
1286 if(s < (char*)pkt || s >= (char*)pkt+pn)
1291 PBIT16((uchar*)s-1, n);
1295 repack(Fcall *f, uchar **ppkt, int dotu)
1302 nn = sizeS2Mu(f, dotu);
1308 n = convS2Mu(f, pkt, nn, dotu);
1310 sysfatal("convS2M conversion error");
1312 sysfatal("convS2Mu and sizeS2Mu disagree");
1316 rewritehdr(Fcall *f, uchar *pkt)
1321 PBIT16(pkt+5, f->tag);
1325 restring(pkt, n, f->version);
1328 PBIT32(pkt+7, f->afid);
1329 restring(pkt, n, f->uname);
1330 restring(pkt, n, f->aname);
1333 PBIT16(pkt+7, f->oldtag);
1336 restring(pkt, n, f->uname);
1337 restring(pkt, n, f->aname);
1338 PBIT32(pkt+7, f->fid);
1339 PBIT32(pkt+11, f->afid);
1342 PBIT32(pkt+7, f->fid);
1343 PBIT32(pkt+11, f->newfid);
1344 for(i=0; i<f->nwname; i++)
1345 restring(pkt, n, f->wname[i]);
1348 restring(pkt, n, f->name);
1356 PBIT32(pkt+7, f->fid);
1359 PBIT32(pkt+7, f->fid);
1360 PBIT64(pkt+11, f->offset);
1363 restring(pkt, n, f->ename);
1369 _iolisten(va_list *arg)
1373 a = va_arg(*arg, char*);
1374 b = va_arg(*arg, char*);
1375 return listen(a, b);
1379 iolisten(Ioproc *io, char *a, char *b)
1381 return iocall(io, _iolisten, a, b);
1385 _ioaccept(va_list *arg)
1390 fd = va_arg(*arg, int);
1391 dir = va_arg(*arg, char*);
1392 return accept(fd, dir);
1396 ioaccept(Ioproc *io, int fd, char *dir)
1398 return iocall(io, _ioaccept, fd, dir);
1404 static char *mon[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1405 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1409 tm = *localtime(time(0));
1410 return fmtprint(fmt, "%s %2d %02d:%02d:%02d.%03d",
1411 mon[tm.mon], tm.mday, tm.hour, tm.min, tm.sec,
1412 (int)(ns%1000000000)/1000000);
1416 cvtustat(Fcall *f, uchar **fpkt, int tounix)
1423 str = emalloc(f->nstat);
1424 n = convM2Du(f->stat, f->nstat, &dir, str, !tounix);
1430 n = sizeD2Mu(&dir, tounix);
1432 if(convD2Mu(&dir, buf, n, tounix) != n)
1433 sysfatal("convD2Mu conversion error");
1437 repack(f, fpkt, dotu);
1439 f->stat = nil; /* is this okay ??? */
1446 stripudirread(Msg* msg)
1459 for(i = 0; i < rx->count; i += m){
1460 m = BIT16SZ + GBIT16(&rx->data[i]);
1461 if(statchecku((uchar*)&rx->data[i], m, 1) < 0)
1469 buf = emalloc(rx->count);
1472 for(i = 0; i < rx->count; i += m){
1473 m = BIT16SZ + GBIT16(&rx->data[i]);
1474 if(convM2Du((uchar*)&rx->data[i], m, &d, str, 1) != m){
1480 n = convD2M(&d, &buf[nn], rx->count - nn);
1491 rx->data = (char*)buf;
1493 repack(&msg->rx, &msg->rpkt, 0);
1496 rx->data = nil; /* is this okay ??? */