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;
94 void *gethash(Hash**, uint);
95 int puthash(Hash**, uint, void*);
96 int delhash(Hash**, uint, void*);
97 Msg *mread9p(Ioproc*, int, int);
98 int mwrite9p(Ioproc*, int, uchar*);
99 uchar *read9ppkt(Ioproc*, int);
100 int write9ppkt(int, uchar*);
105 void msgincref(Msg*);
109 void *erealloc(void*, int);
111 int sendq(Queue*, void*);
113 void connthread(void*);
114 void connoutthread(void*);
115 void listenthread(void*);
116 void outputthread(void*);
117 void inputthread(void*);
118 void rewritehdr(Fcall*, uchar*);
119 void repack(Fcall*, uchar**, int);
120 int tlisten(char*, char*);
121 int taccept(int, char*);
122 int iolisten(Ioproc*, char*, char*);
123 int ioaccept(Ioproc*, int, char*);
124 int iorecvfd(Ioproc*, int);
125 int iosendfd(Ioproc*, int, int);
126 void mainproc(void*);
127 int ignorepipe(void*, char*);
129 void dorootstat(void);
130 int stripudirread(Msg*);
131 int cvtustat(Fcall*, uchar**, int);
136 fprint(2, "usage: 9pserve [-lv] [-A aname afid] [-M msize] address\n");
137 fprint(2, "\treads/writes 9P messages on stdin/stdout\n");
138 threadexitsall("usage");
142 extern int _threaddebuglevel;
144 threadmain(int argc, char **argv)
149 x = getenv("verbose9pserve");
152 fprint(2, "verbose9pserve %s => %d\n", x, verbose);
159 xaname = EARGF(usage());
160 xafid = atoi(EARGF(usage()));
164 msize = atoi(EARGF(usage()));
177 if(attached && !versioned){
178 fprint(2, "-A must be used with -M\n");
186 fmtinstall('T', timefmt);
188 if((afd = announce(addr, adir)) < 0)
189 sysfatal("announce %s: %r", addr);
191 if(strncmp(addr, "unix!", 5) == 0)
193 file = smprint("%s.log", addr);
195 sysfatal("smprint log: %r");
196 if((fd = create(file, OWRITE, 0666)) < 0)
197 sysfatal("create %s: %r", file);
202 if(verbose) fprint(2, "%T 9pserve running\n");
203 proccreate(mainproc, nil, STACK);
213 atnotify(ignorepipe, 1);
214 fmtinstall('D', dirfmt);
215 fmtinstall('M', dirmodefmt);
216 fmtinstall('F', fcallfmt);
217 fmtinstall('H', encodefmt);
224 f.version = "9P2000.u";
227 n = convS2M(&f, vbuf, sizeof vbuf);
229 sysfatal("convS2M conversion error");
230 if(verbose > 1) fprint(2, "%T * <- %F\n", &f);
231 nn = write(1, vbuf, n);
233 sysfatal("error writing Tversion: %r\n");
234 n = read9pmsg(0, vbuf, sizeof vbuf);
236 sysfatal("read9pmsg failure");
237 if(convM2S(vbuf, n, &f) != n)
238 sysfatal("convM2S failure");
241 if(verbose > 1) fprint(2, "%T * -> %F\n", &f);
242 dotu = strncmp(f.version, "9P2000.u", 8) == 0;
245 threadcreate(inputthread, nil, STACK);
246 threadcreate(outputthread, nil, STACK);
251 threadcreate(listenthread, nil, STACK);
256 ignorepipe(void *v, char *s)
259 if(strcmp(s, "sys: write on closed pipe") == 0)
261 if(strcmp(s, "sys: tstp") == 0)
263 fprint(2, "9pserve %s: %T note: %s\n", addr, s);
268 listenthread(void *arg)
275 threadsetname("listen %s", adir);
277 c = emalloc(sizeof(Conn));
278 c->fd = iolisten(io, adir, c->dir);
280 if(verbose) fprint(2, "%T listen: %r\n");
285 c->inc = chancreate(sizeof(void*), 0);
286 c->internal = chancreate(sizeof(void*), 0);
289 if(verbose) fprint(2, "%T incoming call on %s\n", c->dir);
290 threadcreate(connthread, c, STACK);
299 n = sizeS2Mu(&m->rx, m->c->dotu);
300 m->rpkt = emalloc(n);
301 nn = convS2Mu(&m->rx, m->rpkt, n, m->c->dotu);
303 sysfatal("convS2Mu conversion error");
305 sysfatal("sizeS2Mu and convS2Mu disagree");
306 sendq(m->c->outq, m);
314 n = sizeS2Mu(&m->tx, m->c->dotu);
315 m->tpkt = emalloc(n);
316 nn = convS2Mu(&m->tx, m->tpkt, n, m->c->dotu);
318 sysfatal("convS2Mu conversion error");
320 sysfatal("sizeS2Mu and convS2Mu disagree");
325 err(Msg *m, char *ename)
329 m->rx.tag = m->tx.tag;
338 t = emalloc(strlen(s)+1);
344 connthread(void *arg)
354 threadsetname("conn %s", c->dir);
356 fd = ioaccept(io, c->fd, c->dir);
358 if(verbose) fprint(2, "%T accept %s: %r\n", c->dir);
363 threadcreate(connoutthread, c, STACK);
364 while((m = mread9p(io, c->fd, c->dotu)) != nil){
365 if(verbose > 1) fprint(2, "%T fd#%d -> %F\n", c->fd, &m->tx);
369 if(verbose > 1) fprint(2, "%T fd#%d: new msg %p\n", c->fd, m);
370 if(puthash(c->tag, m->tx.tag, m) < 0){
371 err(m, "duplicate tag");
377 m->rx.tag = m->tx.tag;
378 m->rx.msize = m->tx.msize;
379 if(m->rx.msize > msize)
381 m->rx.version = "9P2000";
383 if(dotu && strncmp(m->tx.version, "9P2000.u", 8) == 0){
384 m->rx.version = "9P2000.u";
387 m->rx.type = Rversion;
391 if((m->oldm = gethash(c->tag, m->tx.oldtag)) == nil){
392 m->rx.tag = m->tx.tag;
401 if(m->tx.afid != NOFID
402 && (m->afid = gethash(c->fid, m->tx.afid)) == nil){
403 err(m, "unknown fid");
408 m->fid = fidnew(m->tx.fid);
409 if(puthash(c->fid, m->tx.fid, m->fid) < 0){
410 err(m, "duplicate fid");
414 if(attached && m->afid==nil){
415 if(m->tx.aname[0] && strcmp(xaname, m->tx.aname) != 0){
416 err(m, "invalid attach name");
420 m->tx.aname = xaname;
421 m->tx.uname = estrdup(m->tx.uname);
422 repack(&m->tx, &m->tpkt, c->dotu);
428 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
429 err(m, "unknown fid");
433 if(m->tx.newfid == m->tx.fid){
437 m->newfid = fidnew(m->tx.newfid);
438 if(puthash(c->fid, m->tx.newfid, m->newfid) < 0){
439 err(m, "duplicate fid");
447 err(m, "authentication not required");
450 m->afid = fidnew(m->tx.afid);
451 if(puthash(c->fid, m->tx.afid, m->afid) < 0){
452 err(m, "duplicate fid");
458 if(dotu && !c->dotu && (m->tx.perm&(DMSYMLINK|DMDEVICE|DMNAMEDPIPE|DMSOCKET))){
459 err(m, "unsupported file type");
464 if(m->tx.mode&~(OTRUNC|3)){
465 err(m, "bad openfd mode");
480 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
481 err(m, "unknown fid");
485 if(m->tx.type==Twstat && dotu && !c->dotu){
486 if(cvtustat(&m->tx, &m->tpkt, 1) < 0){
487 err(m, "cannot convert stat buffer");
491 if(m->tx.type==Tread && m->fid->isdir && dotu && !c->dotu){
492 if(m->tx.offset = m->fid->coffset)
493 m->tx.offset = m->fid->offset;
495 m->fid->offset = m->fid->coffset;
500 /* have everything - translate and send */
505 m->tx.fid = m->fid->fid;
507 m->tx.newfid = m->newfid->fid;
509 m->tx.afid = m->afid->fid;
511 m->tx.oldtag = m->oldm->tag;
512 /* reference passes to outq */
514 while(c->nmsg >= MAXMSG){
520 if(verbose) fprint(2, "%T fd#%d eof; flushing conn\n", c->fd);
522 /* flush the output queue */
524 while(c->outq != nil)
527 /* flush all outstanding messages */
528 for(i=0; i<NHASH; i++){
529 for(h=c->tag[i]; h; h=hnext){
537 m->tx.oldtag = om->tag;
540 msgincref(m); /* for outq */
542 mm = recvp(c->internal);
544 msgput(m); /* got from recvp */
545 msgput(m); /* got from msgnew */
546 msgput(om); /* got from hash table */
552 /* clunk all outstanding fids */
553 for(i=0; i<NHASH; i++){
554 for(h=c->fid[i]; h; h=hnext){
567 mm = recvp(c->internal);
570 msgput(m); /* got from recvp */
571 msgput(m); /* got from msgnew */
572 fidput(f); /* got from hash table */
580 assert(c->nmsg == 0);
581 assert(c->nfid == 0);
583 chanfree(c->internal);
593 openfdthread(void *v)
606 threadsetname("openfd %s", c->fdfid);
609 if(c->fdmode == OREAD){
611 if(verbose) fprint(2, "%T tread...");
616 m->tx.count = msize - IOHDRSZ;
617 m->tx.fid = fid->fid;
625 if(m->rx.type == Rerror){
626 /* fprint(2, "%T read error: %s\n", m->rx.ename); */
632 if(iowrite(io, c->fd, m->rx.data, m->rx.count) != m->rx.count){
633 /* fprint(2, "%T pipe write error: %r\n"); */
642 if(verbose) fprint(2, "%T twrite...");
646 if((n=ioread(io, c->fd, buf, n)) <= 0){
648 fprint(2, "%T pipe read error: %r\n");
655 m->tx.fid = fid->fid;
665 if(m->rx.type == Rerror){
666 /* fprint(2, "%T write error: %s\n", m->rx.ename); */
674 if(verbose) fprint(2, "%T eof on %d fid %d\n", c->fd, fid->fid);
681 if(verbose) fprint(2, "%T eof on %d fid %d ref %d\n", c->fd, fid->fid, fid->ref);
682 if(--fid->openfd == 0){
688 m->tx.fid = fid->fid;
699 chanfree(c->internal);
712 rerrstr(errs, sizeof errs);
714 /* XXX return here? */
716 if(verbose) fprint(2, "%T xopen pipe %d %d...", p[0], p[1]);
718 /* now we're committed. */
720 /* a new connection for this fid */
721 nc = emalloc(sizeof(Conn));
722 nc->internal = chancreate(sizeof(void*), 0);
728 nc->fdmode = m->tx.mode;
731 /* a thread to tend the pipe */
732 threadcreate(openfdthread, nc, STACK);
734 /* if mode is ORDWR, that openfdthread will write; start a reader */
735 if((m->tx.mode&3) == ORDWR){
736 nc = emalloc(sizeof(Conn));
737 nc->internal = chancreate(sizeof(void*), 0);
742 nc->fd = dup(p[0], -1);
743 threadcreate(openfdthread, nc, STACK);
746 /* steal fid from other connection */
747 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
750 /* rewrite as Ropenfd */
751 m->rx.type = Ropenfd;
753 m->rpkt = erealloc(m->rpkt, n+4);
754 PBIT32(m->rpkt+n, p[1]);
757 m->rpkt[4] = Ropenfd;
763 connoutthread(void *arg)
775 threadsetname("connout %s", c->dir);
776 while((m = recvq(outq)) != nil){
777 err = m->tx.type+1 != m->rx.type;
778 if(!err && m->isopenfd)
785 if(delhash(om->c->tag, om->ctag, om) == 0)
791 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
796 if(verbose) fprint(2, "%T auth error\n");
797 if(delhash(m->c->fid, m->afid->cfid, m->afid) == 0)
803 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
807 if(err || m->rx.nwqid < m->tx.nwname)
808 if(m->tx.fid != m->tx.newfid && m->newfid)
809 if(delhash(m->c->fid, m->newfid->cfid, m->newfid) == 0)
813 if(!err && m->fid->isdir && dotu && !m->c->dotu){
814 m->fid->offset += m->rx.count;
816 m->fid->coffset += m->rx.count;
820 if(!err && dotu && !m->c->dotu)
821 cvtustat(&m->rx, &m->rpkt, 0);
825 m->fid->isdir = (m->rx.qid.type & QTDIR);
828 if(m->rx.type==Rerror && dotu && !c->dotu){
829 ename = estrdup(m->rx.ename);
831 repack(&m->rx, &m->rpkt, c->dotu);
835 if(delhash(m->c->tag, m->ctag, m) == 0)
837 if(verbose > 1) fprint(2, "%T fd#%d <- %F\n", c->fd, &m->rx);
838 rewritehdr(&m->rx, m->rpkt);
839 if(mwrite9p(io, c->fd, m->rpkt) < 0)
840 if(verbose) fprint(2, "%T write error: %r\n");
842 if(c->inputstalled && c->nmsg < MAXMSG)
851 outputthread(void *arg)
858 threadsetname("output");
859 while((m = recvq(outq)) != nil){
860 if(verbose > 1) fprint(2, "%T * <- %F\n", &m->tx);
861 rewritehdr(&m->tx, m->tpkt);
862 if(mwrite9p(io, 1, m->tpkt) < 0)
863 sysfatal("output error: %r");
867 fprint(2, "%T output eof\n");
872 inputthread(void *arg)
879 threadsetname("input");
880 if(verbose) fprint(2, "%T input thread\n");
883 while((pkt = read9ppkt(io, 0)) != nil){
886 fprint(2, "%T short 9P packet from server\n");
890 if(verbose > 2) fprint(2, "%T read %.*H\n", n, pkt);
892 if((m = msgget(tag)) == nil){
893 fprint(2, "%T unexpected 9P response tag %d\n", tag);
897 if((nn = convM2Su(pkt, n, &m->rx, dotu)) != n){
898 fprint(2, "%T bad packet - convM2S %d but %d\n", nn, n);
903 if(verbose > 1) fprint(2, "%T * -> %F%s\n", &m->rx,
904 m->internal ? " (internal)" : "");
908 sendp(m->c->internal, m);
910 sendq(m->c->outq, m);
915 /*fprint(2, "%T input eof\n"); */
920 gethash(Hash **ht, uint n)
924 for(h=ht[n%NHASH]; h; h=h->next)
931 delhash(Hash **ht, uint n, void *v)
935 for(l=&ht[n%NHASH]; h=*l; l=&h->next)
938 if(verbose) fprint(2, "%T delhash %d got %p want %p\n", n, h->v, v);
949 puthash(Hash **ht, uint n, void *v)
955 h = emalloc(sizeof(Hash));
956 h->next = ht[n%NHASH];
973 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
974 if(nfidtab == xafid){
975 fidtab[nfidtab++] = nil;
976 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
978 fidtab[nfidtab] = emalloc(sizeof(Fid));
979 freefid = fidtab[nfidtab];
980 freefid->fid = nfidtab++;
1013 if(verbose > 1) fprint(2, "%T msgincref @0x%lux %p tag %d/%d ref %d=>%d\n",
1014 getcallerpc(&m), m, m->tag, m->ctag, m->ref, m->ref+1);
1024 msgtab = erealloc(msgtab, (nmsgtab+1)*sizeof(msgtab[0]));
1025 msgtab[nmsgtab] = emalloc(sizeof(Msg));
1026 freemsg = msgtab[nmsgtab];
1027 freemsg->tag = nmsgtab++;
1032 if(verbose > 1) fprint(2, "%T msgnew @0x%lux %p tag %d ref %d\n",
1033 getcallerpc(&x), m, m->tag, m->ref);
1039 * Clear data associated with connections, so that
1040 * if all msgs have been msgcleared, the connection
1041 * can be freed. Note that this does *not* free the tpkt
1042 * and rpkt; they are freed in msgput with the msg itself.
1043 * The io write thread might still be holding a ref to msg
1044 * even once the connection has finished with it.
1069 if(m->rx.type == Ropenfd && m->rx.unixfd >= 0){
1070 close(m->rx.unixfd);
1081 if(verbose > 1) fprint(2, "%T msgput 0x%lux %p tag %d/%d ref %d\n",
1082 getcallerpc(&m), m, m->tag, m->ctag, m->ref);
1107 if(n < 0 || n >= nmsgtab)
1112 if(verbose) fprint(2, "%T msgget %d = %p\n", n, m);
1126 sysfatal("out of memory allocating %d", n);
1132 erealloc(void *v, int n)
1137 sysfatal("out of memory reallocating %d", n);
1142 typedef struct Qel Qel;
1163 q = mallocz(sizeof(Queue), 1);
1171 sendq(Queue *q, void *p)
1175 e = emalloc(sizeof(Qel));
1179 werrstr("hungup queue");
1202 while(q->head == nil && !q->hungup)
1217 read9ppkt(Ioproc *io, int fd)
1222 n = ioreadn(io, fd, buf, 4);
1230 nn = ioreadn(io, fd, pkt+4, n-4);
1235 /* would do this if we ever got one of these, but we only generate them
1236 if(pkt[4] == Ropenfd){
1237 newfd = iorecvfd(io, fd);
1238 PBIT32(pkt+n-4, newfd);
1245 mread9p(Ioproc *io, int fd, int dotu)
1251 if((pkt = read9ppkt(io, fd)) == nil)
1257 nn = convM2Su(pkt, n, &m->tx, dotu);
1259 fprint(2, "%T read bad packet from %d\n", fd);
1266 mwrite9p(Ioproc *io, int fd, uchar *pkt)
1271 if(verbose > 2) fprint(2, "%T write %d %d %.*H\n", fd, n, n, pkt);
1272 if(verbose > 1) fprint(2, "%T before iowrite\n");
1273 if(iowrite(io, fd, pkt, n) != n){
1274 fprint(2, "%T write error: %r\n");
1277 if(verbose > 1) fprint(2, "%T after iowrite\n");
1278 if(pkt[4] == Ropenfd){
1279 nfd = GBIT32(pkt+n-4);
1280 if(iosendfd(io, fd, nfd) < 0){
1281 fprint(2, "%T send fd error: %r\n");
1289 restring(uchar *pkt, int pn, char *s)
1293 if(s < (char*)pkt || s >= (char*)pkt+pn)
1298 PBIT16((uchar*)s-1, n);
1302 repack(Fcall *f, uchar **ppkt, int dotu)
1309 nn = sizeS2Mu(f, dotu);
1315 n = convS2Mu(f, pkt, nn, dotu);
1317 sysfatal("convS2M conversion error");
1319 sysfatal("convS2Mu and sizeS2Mu disagree");
1323 rewritehdr(Fcall *f, uchar *pkt)
1328 PBIT16(pkt+5, f->tag);
1332 restring(pkt, n, f->version);
1335 PBIT32(pkt+7, f->afid);
1336 restring(pkt, n, f->uname);
1337 restring(pkt, n, f->aname);
1340 PBIT16(pkt+7, f->oldtag);
1343 restring(pkt, n, f->uname);
1344 restring(pkt, n, f->aname);
1345 PBIT32(pkt+7, f->fid);
1346 PBIT32(pkt+11, f->afid);
1349 PBIT32(pkt+7, f->fid);
1350 PBIT32(pkt+11, f->newfid);
1351 for(i=0; i<f->nwname; i++)
1352 restring(pkt, n, f->wname[i]);
1355 restring(pkt, n, f->name);
1363 PBIT32(pkt+7, f->fid);
1366 PBIT32(pkt+7, f->fid);
1367 PBIT64(pkt+11, f->offset);
1370 restring(pkt, n, f->ename);
1376 _iolisten(va_list *arg)
1380 a = va_arg(*arg, char*);
1381 b = va_arg(*arg, char*);
1382 return listen(a, b);
1386 iolisten(Ioproc *io, char *a, char *b)
1388 return iocall(io, _iolisten, a, b);
1392 _ioaccept(va_list *arg)
1397 fd = va_arg(*arg, int);
1398 dir = va_arg(*arg, char*);
1399 return accept(fd, dir);
1403 ioaccept(Ioproc *io, int fd, char *dir)
1405 return iocall(io, _ioaccept, fd, dir);
1411 static char *mon[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1412 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1416 tm = *localtime(time(0));
1417 return fmtprint(fmt, "%s %2d %02d:%02d:%02d.%03d",
1418 mon[tm.mon], tm.mday, tm.hour, tm.min, tm.sec,
1419 (int)(ns%1000000000)/1000000);
1423 cvtustat(Fcall *f, uchar **fpkt, int tounix)
1430 str = emalloc(f->nstat);
1431 n = convM2Du(f->stat, f->nstat, &dir, str, !tounix);
1437 n = sizeD2Mu(&dir, tounix);
1439 if(convD2Mu(&dir, buf, n, tounix) != n)
1440 sysfatal("convD2Mu conversion error");
1444 repack(f, fpkt, dotu);
1446 f->stat = nil; /* is this okay ??? */
1453 stripudirread(Msg* msg)
1466 for(i = 0; i < rx->count; i += m){
1467 m = BIT16SZ + GBIT16(&rx->data[i]);
1468 if(statchecku((uchar*)&rx->data[i], m, 1) < 0)
1476 buf = emalloc(rx->count);
1479 for(i = 0; i < rx->count; i += m){
1480 m = BIT16SZ + GBIT16(&rx->data[i]);
1481 if(convM2Du((uchar*)&rx->data[i], m, &d, str, 1) != m){
1487 n = convD2M(&d, &buf[nn], rx->count - nn);
1498 rx->data = (char*)buf;
1500 repack(&msg->rx, &msg->rpkt, 0);
1503 rx->data = nil; /* is this okay ??? */