7 #define err err9pserve /* Darwin x86 */
13 MAXMSG = 64, /* per connection */
14 MAXMSGSIZE = 4*1024*1024
17 typedef struct Hash Hash;
18 typedef struct Fid Fid;
19 typedef struct Msg Msg;
20 typedef struct Conn Conn;
21 typedef struct Queue Queue;
97 void *gethash(Hash**, uint);
98 int puthash(Hash**, uint, void*);
99 int delhash(Hash**, uint, void*);
100 Msg *mread9p(Ioproc*, int, int);
101 int mwrite9p(Ioproc*, int, uchar*);
102 uchar *read9ppkt(Ioproc*, int);
103 int write9ppkt(int, uchar*);
108 void msgincref(Msg*);
112 void *erealloc(void*, int);
114 int sendq(Queue*, void*);
116 void connthread(void*);
117 void connoutthread(void*);
118 void listenthread(void*);
119 void outputthread(void*);
120 void inputthread(void*);
121 void rewritehdr(Fcall*, uchar*);
122 void repack(Fcall*, uchar**, int);
123 int tlisten(char*, char*);
124 int taccept(int, char*);
125 int iolisten(Ioproc*, char*, char*);
126 int ioaccept(Ioproc*, int, char*);
127 int iorecvfd(Ioproc*, int);
128 int iosendfd(Ioproc*, int, int);
129 void mainproc(void*);
130 int ignorepipe(void*, char*);
132 void dorootstat(void);
133 int stripudirread(Msg*);
134 int cvtustat(Fcall*, uchar**, int);
139 fprint(2, "usage: 9pserve [-lnv] [-A aname afid] [-c addr] [-M msize] address\n");
140 fprint(2, "\treads/writes 9P messages on stdin/stdout\n");
141 threadexitsall("usage");
145 extern int _threaddebuglevel;
147 threadmain(int argc, char **argv)
149 char *file, *x, *addr;
153 x = getenv("verbose9pserve");
156 fprint(2, "verbose9pserve %s => %d\n", x, verbose);
163 xaname = EARGF(usage());
164 xafid = atoi(EARGF(usage()));
168 msize = atoi(EARGF(usage()));
171 addr = netmkaddr(EARGF(usage()), "net", "9fs");
172 if((fd = dial(addr, nil, nil, nil)) < 0)
173 sysfatal("dial %s: %r", addr);
193 if(attached && !versioned){
194 fprint(2, "-A must be used with -M\n");
202 fmtinstall('T', timefmt);
204 if((afd = announce(addr, adir)) < 0)
205 sysfatal("announce %s: %r", addr);
207 if(strncmp(addr, "unix!", 5) == 0)
209 file = smprint("%s.log", addr);
211 sysfatal("smprint log: %r");
212 if((fd = create(file, OWRITE, 0666)) < 0)
213 sysfatal("create %s: %r", file);
218 if(verbose) fprint(2, "%T 9pserve running\n");
219 proccreate(mainproc, nil, STACK);
229 atnotify(ignorepipe, 1);
230 fmtinstall('D', dirfmt);
231 fmtinstall('M', dirmodefmt);
232 fmtinstall('F', fcallfmt);
233 fmtinstall('H', encodefmt);
240 f.version = "9P2000.u";
243 n = convS2M(&f, vbuf, sizeof vbuf);
245 sysfatal("convS2M conversion error");
246 if(verbose > 1) fprint(2, "%T * <- %F\n", &f);
247 nn = write(1, vbuf, n);
249 sysfatal("error writing Tversion: %r\n");
250 n = read9pmsg(0, vbuf, sizeof vbuf);
252 sysfatal("read9pmsg failure");
253 if(convM2S(vbuf, n, &f) != n)
254 sysfatal("convM2S failure");
257 if(verbose > 1) fprint(2, "%T * -> %F\n", &f);
258 dotu = strncmp(f.version, "9P2000.u", 8) == 0;
261 threadcreate(inputthread, nil, STACK);
262 threadcreate(outputthread, nil, STACK);
267 threadcreate(listenthread, nil, STACK);
272 ignorepipe(void *v, char *s)
275 if(strcmp(s, "sys: write on closed pipe") == 0)
277 if(strcmp(s, "sys: tstp") == 0)
279 if(strcmp(s, "sys: window size change") == 0)
281 fprint(2, "9pserve %s: %T note: %s\n", addr, s);
286 listenthread(void *arg)
293 threadsetname("listen %s", adir);
295 c = emalloc(sizeof(Conn));
296 c->fd = iolisten(io, adir, c->dir);
298 if(verbose) fprint(2, "%T listen: %r\n");
303 c->inc = chancreate(sizeof(void*), 0);
304 c->internal = chancreate(sizeof(void*), 0);
307 c->outqdead = chancreate(sizeof(void*), 0);
308 if(verbose) fprint(2, "%T incoming call on %s\n", c->dir);
309 threadcreate(connthread, c, STACK);
318 n = sizeS2Mu(&m->rx, m->c->dotu);
319 m->rpkt = emalloc(n);
320 nn = convS2Mu(&m->rx, m->rpkt, n, m->c->dotu);
322 sysfatal("convS2Mu conversion error");
324 sysfatal("sizeS2Mu and convS2Mu disagree");
325 sendq(m->c->outq, m);
333 n = sizeS2Mu(&m->tx, m->c->dotu);
334 m->tpkt = emalloc(n);
335 nn = convS2Mu(&m->tx, m->tpkt, n, m->c->dotu);
337 sysfatal("convS2Mu conversion error");
339 sysfatal("sizeS2Mu and convS2Mu disagree");
344 err(Msg *m, char *ename)
348 m->rx.tag = m->tx.tag;
357 t = emalloc(strlen(s)+1);
363 connthread(void *arg)
368 Msg *m, *om, *mm, sync;
373 threadsetname("conn %s", c->dir);
375 fd = ioaccept(io, c->fd, c->dir);
377 if(verbose) fprint(2, "%T accept %s: %r\n", c->dir);
382 threadcreate(connoutthread, c, STACK);
383 while((m = mread9p(io, c->fd, c->dotu)) != nil){
384 if(verbose > 1) fprint(2, "%T fd#%d -> %F\n", c->fd, &m->tx);
388 if(verbose > 1) fprint(2, "%T fd#%d: new msg %p\n", c->fd, m);
389 if(puthash(c->tag, m->tx.tag, m) < 0){
390 err(m, "duplicate tag");
396 m->rx.tag = m->tx.tag;
397 m->rx.msize = m->tx.msize;
398 if(m->rx.msize > msize)
400 m->rx.version = "9P2000";
402 if(dotu && strncmp(m->tx.version, "9P2000.u", 8) == 0){
403 m->rx.version = "9P2000.u";
406 m->rx.type = Rversion;
410 if((m->oldm = gethash(c->tag, m->tx.oldtag)) == nil){
411 m->rx.tag = m->tx.tag;
420 if(m->tx.afid != NOFID
421 && (m->afid = gethash(c->fid, m->tx.afid)) == nil){
422 err(m, "unknown fid");
427 m->fid = fidnew(m->tx.fid);
428 if(puthash(c->fid, m->tx.fid, m->fid) < 0){
429 err(m, "duplicate fid");
433 if(attached && m->afid==nil){
434 if(m->tx.aname[0] && strcmp(xaname, m->tx.aname) != 0){
435 err(m, "invalid attach name");
439 m->tx.aname = xaname;
440 m->tx.uname = getuser(); /* what srv.c used */
441 repack(&m->tx, &m->tpkt, c->dotu);
445 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
446 err(m, "unknown fid");
450 if(m->tx.newfid == m->tx.fid){
454 m->newfid = fidnew(m->tx.newfid);
455 if(puthash(c->fid, m->tx.newfid, m->newfid) < 0){
456 err(m, "duplicate fid");
464 err(m, "authentication not required");
468 err(m, "authentication rejected");
471 m->afid = fidnew(m->tx.afid);
472 if(puthash(c->fid, m->tx.afid, m->afid) < 0){
473 err(m, "duplicate fid");
479 if(dotu && !c->dotu && (m->tx.perm&(DMSYMLINK|DMDEVICE|DMNAMEDPIPE|DMSOCKET))){
480 err(m, "unsupported file type");
485 if(m->tx.mode&~(OTRUNC|3)){
486 err(m, "bad openfd mode");
501 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
502 err(m, "unknown fid");
506 if(m->tx.type==Twstat && dotu && !c->dotu){
507 if(cvtustat(&m->tx, &m->tpkt, 1) < 0){
508 err(m, "cannot convert stat buffer");
512 if(m->tx.type==Tread && m->fid->isdir && dotu && !c->dotu){
513 if(m->tx.offset = m->fid->coffset)
514 m->tx.offset = m->fid->offset;
516 m->fid->offset = m->fid->coffset;
521 /* have everything - translate and send */
526 m->tx.fid = m->fid->fid;
528 m->tx.newfid = m->newfid->fid;
530 m->tx.afid = m->afid->fid;
532 m->tx.oldtag = m->oldm->tag;
533 /* reference passes to outq */
535 while(c->nmsg >= MAXMSG){
541 if(verbose) fprint(2, "%T fd#%d eof; flushing conn\n", c->fd);
543 /* flush all outstanding messages */
544 for(i=0; i<NHASH; i++){
545 while((h = c->tag[i]) != nil){
547 msgincref(om); /* for us */
554 m->tx.oldtag = om->tag;
557 msgincref(m); /* for outq */
559 mm = recvp(c->internal);
561 msgput(m); /* got from recvp */
562 msgput(m); /* got from msgnew */
563 if(delhash(c->tag, om->ctag, om) == 0)
564 msgput(om); /* got from hash table */
565 msgput(om); /* got from msgincref */
570 * outputthread has written all its messages
571 * to the remote connection (because we've gotten all the replies!),
572 * but it might not have gotten a chance to msgput
573 * the very last one. sync up to make sure.
575 memset(&sync, 0, sizeof sync);
581 /* everything is quiet; can close the local output queue. */
585 /* should be no messages left anywhere. */
586 assert(c->nmsg == 0);
588 /* clunk all outstanding fids */
589 for(i=0; i<NHASH; i++){
590 for(h=c->fid[i]; h; h=hnext){
603 mm = recvp(c->internal);
606 msgput(m); /* got from recvp */
607 msgput(m); /* got from msgnew */
608 fidput(f); /* got from hash table */
616 assert(c->nmsg == 0);
617 assert(c->nfid == 0);
619 chanfree(c->internal);
629 openfdthread(void *v)
642 threadsetname("openfd %s", c->fdfid);
645 if(c->fdmode == OREAD){
647 if(verbose) fprint(2, "%T tread...");
652 m->tx.count = msize - IOHDRSZ;
653 m->tx.fid = fid->fid;
661 if(m->rx.type == Rerror){
662 /* fprint(2, "%T read error: %s\n", m->rx.ename); */
668 if(iowrite(io, c->fd, m->rx.data, m->rx.count) != m->rx.count){
669 /* fprint(2, "%T pipe write error: %r\n"); */
678 if(verbose) fprint(2, "%T twrite...");
682 if((n=ioread(io, c->fd, buf, n)) <= 0){
684 fprint(2, "%T pipe read error: %r\n");
691 m->tx.fid = fid->fid;
701 if(m->rx.type == Rerror){
702 /* fprint(2, "%T write error: %s\n", m->rx.ename); */
710 if(verbose) fprint(2, "%T eof on %d fid %d\n", c->fd, fid->fid);
717 if(verbose) fprint(2, "%T eof on %d fid %d ref %d\n", c->fd, fid->fid, fid->ref);
718 if(--fid->openfd == 0){
724 m->tx.fid = fid->fid;
735 chanfree(c->internal);
748 rerrstr(errs, sizeof errs);
750 /* XXX return here? */
752 if(verbose) fprint(2, "%T xopen pipe %d %d...", p[0], p[1]);
754 /* now we're committed. */
756 /* a new connection for this fid */
757 nc = emalloc(sizeof(Conn));
758 nc->internal = chancreate(sizeof(void*), 0);
764 nc->fdmode = m->tx.mode;
767 /* a thread to tend the pipe */
768 threadcreate(openfdthread, nc, STACK);
770 /* if mode is ORDWR, that openfdthread will write; start a reader */
771 if((m->tx.mode&3) == ORDWR){
772 nc = emalloc(sizeof(Conn));
773 nc->internal = chancreate(sizeof(void*), 0);
778 nc->fd = dup(p[0], -1);
779 threadcreate(openfdthread, nc, STACK);
782 /* steal fid from other connection */
783 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
786 /* rewrite as Ropenfd */
787 m->rx.type = Ropenfd;
789 m->rpkt = erealloc(m->rpkt, n+4);
790 PBIT32(m->rpkt+n, p[1]);
793 m->rpkt[4] = Ropenfd;
799 connoutthread(void *arg)
809 threadsetname("connout %s", c->dir);
810 while((m = recvq(c->outq)) != nil){
811 err = m->tx.type+1 != m->rx.type;
812 if(!err && m->isopenfd)
819 if(delhash(om->c->tag, om->ctag, om) == 0)
825 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
830 if(verbose) fprint(2, "%T auth error\n");
831 if(delhash(m->c->fid, m->afid->cfid, m->afid) == 0)
837 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
841 if(err || m->rx.nwqid < m->tx.nwname)
842 if(m->tx.fid != m->tx.newfid && m->newfid)
843 if(delhash(m->c->fid, m->newfid->cfid, m->newfid) == 0)
847 if(!err && m->fid->isdir && dotu && !m->c->dotu){
848 m->fid->offset += m->rx.count;
850 m->fid->coffset += m->rx.count;
854 if(!err && dotu && !m->c->dotu)
855 cvtustat(&m->rx, &m->rpkt, 0);
859 m->fid->isdir = (m->rx.qid.type & QTDIR);
862 if(m->rx.type==Rerror && dotu && !c->dotu){
863 ename = estrdup(m->rx.ename);
865 repack(&m->rx, &m->rpkt, c->dotu);
869 if(delhash(m->c->tag, m->ctag, m) == 0)
871 if(verbose > 1) fprint(2, "%T fd#%d <- %F\n", c->fd, &m->rx);
872 rewritehdr(&m->rx, m->rpkt);
873 if(mwrite9p(io, c->fd, m->rpkt) < 0)
874 if(verbose) fprint(2, "%T write error: %r\n");
876 if(c->inputstalled && c->nmsg < MAXMSG)
882 sendp(c->outqdead, nil);
886 outputthread(void *arg)
893 threadsetname("output");
894 while((m = recvq(outq)) != nil){
896 sendp(m->c->outqdead, nil);
899 if(verbose > 1) fprint(2, "%T * <- %F\n", &m->tx);
900 rewritehdr(&m->tx, m->tpkt);
901 if(mwrite9p(io, 1, m->tpkt) < 0)
902 sysfatal("output error: %r");
906 fprint(2, "%T output eof\n");
911 inputthread(void *arg)
918 threadsetname("input");
919 if(verbose) fprint(2, "%T input thread\n");
922 while((pkt = read9ppkt(io, 0)) != nil){
925 fprint(2, "%T short 9P packet from server\n");
929 if(verbose > 2) fprint(2, "%T read %.*H\n", n, pkt);
931 if((m = msgget(tag)) == nil){
932 fprint(2, "%T unexpected 9P response tag %d\n", tag);
936 if((nn = convM2Su(pkt, n, &m->rx, dotu)) != n){
937 fprint(2, "%T bad packet - convM2S %d but %d\n", nn, n);
942 if(verbose > 1) fprint(2, "%T * -> %F%s\n", &m->rx,
943 m->internal ? " (internal)" : "");
947 sendp(m->c->internal, m);
949 sendq(m->c->outq, m);
954 /*fprint(2, "%T input eof\n"); */
959 gethash(Hash **ht, uint n)
963 for(h=ht[n%NHASH]; h; h=h->next)
970 delhash(Hash **ht, uint n, void *v)
974 for(l=&ht[n%NHASH]; h=*l; l=&h->next)
977 if(verbose) fprint(2, "%T delhash %d got %p want %p\n", n, h->v, v);
988 puthash(Hash **ht, uint n, void *v)
994 h = emalloc(sizeof(Hash));
995 h->next = ht[n%NHASH];
1012 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
1013 if(nfidtab == xafid){
1014 fidtab[nfidtab++] = nil;
1015 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
1017 fidtab[nfidtab] = emalloc(sizeof(Fid));
1018 freefid = fidtab[nfidtab];
1019 freefid->fid = nfidtab++;
1052 if(verbose > 1) fprint(2, "%T msgincref @0x%lux %p tag %d/%d ref %d=>%d\n",
1053 getcallerpc(&m), m, m->tag, m->ctag, m->ref, m->ref+1);
1063 msgtab = erealloc(msgtab, (nmsgtab+1)*sizeof(msgtab[0]));
1064 msgtab[nmsgtab] = emalloc(sizeof(Msg));
1065 freemsg = msgtab[nmsgtab];
1066 freemsg->tag = nmsgtab++;
1071 if(verbose > 1) fprint(2, "%T msgnew @0x%lux %p tag %d ref %d\n",
1072 getcallerpc(&x), m, m->tag, m->ref);
1078 * Clear data associated with connections, so that
1079 * if all msgs have been msgcleared, the connection
1080 * can be freed. Note that this does *not* free the tpkt
1081 * and rpkt; they are freed in msgput with the msg itself.
1082 * The io write thread might still be holding a ref to msg
1083 * even once the connection has finished with it.
1108 if(m->rx.type == Ropenfd && m->rx.unixfd >= 0){
1109 close(m->rx.unixfd);
1120 if(verbose > 1) fprint(2, "%T msgput 0x%lux %p tag %d/%d ref %d\n",
1121 getcallerpc(&m), m, m->tag, m->ctag, m->ref);
1146 if(n < 0 || n >= nmsgtab)
1151 if(verbose) fprint(2, "%T msgget %d = %p\n", n, m);
1165 sysfatal("out of memory allocating %d", n);
1171 erealloc(void *v, int n)
1176 sysfatal("out of memory reallocating %d", n);
1181 typedef struct Qel Qel;
1201 q = mallocz(sizeof(Queue), 1);
1209 sendq(Queue *q, void *p)
1213 e = emalloc(sizeof(Qel));
1234 while(q->head == nil)
1245 read9ppkt(Ioproc *io, int fd)
1250 n = ioreadn(io, fd, buf, 4);
1258 nn = ioreadn(io, fd, pkt+4, n-4);
1263 /* would do this if we ever got one of these, but we only generate them
1264 if(pkt[4] == Ropenfd){
1265 newfd = iorecvfd(io, fd);
1266 PBIT32(pkt+n-4, newfd);
1273 mread9p(Ioproc *io, int fd, int dotu)
1279 if((pkt = read9ppkt(io, fd)) == nil)
1285 nn = convM2Su(pkt, n, &m->tx, dotu);
1287 fprint(2, "%T read bad packet from %d\n", fd);
1294 mwrite9p(Ioproc *io, int fd, uchar *pkt)
1299 if(verbose > 2) fprint(2, "%T write %d %d %.*H\n", fd, n, n, pkt);
1300 if(verbose > 1) fprint(2, "%T before iowrite\n");
1301 if(iowrite(io, fd, pkt, n) != n){
1302 fprint(2, "%T write error: %r\n");
1305 if(verbose > 1) fprint(2, "%T after iowrite\n");
1306 if(pkt[4] == Ropenfd){
1307 nfd = GBIT32(pkt+n-4);
1308 if(iosendfd(io, fd, nfd) < 0){
1309 fprint(2, "%T send fd error: %r\n");
1317 restring(uchar *pkt, int pn, char *s)
1321 if(s < (char*)pkt || s >= (char*)pkt+pn)
1326 PBIT16((uchar*)s-1, n);
1330 repack(Fcall *f, uchar **ppkt, int dotu)
1337 nn = sizeS2Mu(f, dotu);
1343 n = convS2Mu(f, pkt, nn, dotu);
1345 sysfatal("convS2M conversion error");
1347 sysfatal("convS2Mu and sizeS2Mu disagree");
1351 rewritehdr(Fcall *f, uchar *pkt)
1356 PBIT16(pkt+5, f->tag);
1360 restring(pkt, n, f->version);
1363 PBIT32(pkt+7, f->afid);
1364 restring(pkt, n, f->uname);
1365 restring(pkt, n, f->aname);
1368 PBIT16(pkt+7, f->oldtag);
1371 restring(pkt, n, f->uname);
1372 restring(pkt, n, f->aname);
1373 PBIT32(pkt+7, f->fid);
1374 PBIT32(pkt+11, f->afid);
1377 PBIT32(pkt+7, f->fid);
1378 PBIT32(pkt+11, f->newfid);
1379 for(i=0; i<f->nwname; i++)
1380 restring(pkt, n, f->wname[i]);
1383 restring(pkt, n, f->name);
1391 PBIT32(pkt+7, f->fid);
1394 PBIT32(pkt+7, f->fid);
1395 PBIT64(pkt+11, f->offset);
1398 restring(pkt, n, f->ename);
1404 _iolisten(va_list *arg)
1408 a = va_arg(*arg, char*);
1409 b = va_arg(*arg, char*);
1410 return listen(a, b);
1414 iolisten(Ioproc *io, char *a, char *b)
1416 return iocall(io, _iolisten, a, b);
1420 _ioaccept(va_list *arg)
1425 fd = va_arg(*arg, int);
1426 dir = va_arg(*arg, char*);
1427 return accept(fd, dir);
1431 ioaccept(Ioproc *io, int fd, char *dir)
1433 return iocall(io, _ioaccept, fd, dir);
1439 static char *mon[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1440 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1444 tm = *localtime(time(0));
1445 return fmtprint(fmt, "%s %2d %02d:%02d:%02d.%03d",
1446 mon[tm.mon], tm.mday, tm.hour, tm.min, tm.sec,
1447 (int)(ns%1000000000)/1000000);
1451 cvtustat(Fcall *f, uchar **fpkt, int tounix)
1458 str = emalloc(f->nstat);
1459 n = convM2Du(f->stat, f->nstat, &dir, str, !tounix);
1465 n = sizeD2Mu(&dir, tounix);
1467 if(convD2Mu(&dir, buf, n, tounix) != n)
1468 sysfatal("convD2Mu conversion error");
1472 repack(f, fpkt, dotu);
1474 f->stat = nil; /* is this okay ??? */
1481 stripudirread(Msg* msg)
1494 for(i = 0; i < rx->count; i += m){
1495 m = BIT16SZ + GBIT16(&rx->data[i]);
1496 if(statchecku((uchar*)&rx->data[i], m, 1) < 0)
1504 buf = emalloc(rx->count);
1507 for(i = 0; i < rx->count; i += m){
1508 m = BIT16SZ + GBIT16(&rx->data[i]);
1509 if(convM2Du((uchar*)&rx->data[i], m, &d, str, 1) != m){
1515 n = convD2M(&d, &buf[nn], rx->count - nn);
1526 rx->data = (char*)buf;
1528 repack(&msg->rx, &msg->rpkt, 0);
1531 rx->data = nil; /* is this okay ??? */