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 = getuser(); /* what srv.c used */
422 repack(&m->tx, &m->tpkt, c->dotu);
426 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
427 err(m, "unknown fid");
431 if(m->tx.newfid == m->tx.fid){
435 m->newfid = fidnew(m->tx.newfid);
436 if(puthash(c->fid, m->tx.newfid, m->newfid) < 0){
437 err(m, "duplicate fid");
445 err(m, "authentication not required");
448 m->afid = fidnew(m->tx.afid);
449 if(puthash(c->fid, m->tx.afid, m->afid) < 0){
450 err(m, "duplicate fid");
456 if(dotu && !c->dotu && (m->tx.perm&(DMSYMLINK|DMDEVICE|DMNAMEDPIPE|DMSOCKET))){
457 err(m, "unsupported file type");
462 if(m->tx.mode&~(OTRUNC|3)){
463 err(m, "bad openfd mode");
478 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
479 err(m, "unknown fid");
483 if(m->tx.type==Twstat && dotu && !c->dotu){
484 if(cvtustat(&m->tx, &m->tpkt, 1) < 0){
485 err(m, "cannot convert stat buffer");
489 if(m->tx.type==Tread && m->fid->isdir && dotu && !c->dotu){
490 if(m->tx.offset = m->fid->coffset)
491 m->tx.offset = m->fid->offset;
493 m->fid->offset = m->fid->coffset;
498 /* have everything - translate and send */
503 m->tx.fid = m->fid->fid;
505 m->tx.newfid = m->newfid->fid;
507 m->tx.afid = m->afid->fid;
509 m->tx.oldtag = m->oldm->tag;
510 /* reference passes to outq */
512 while(c->nmsg >= MAXMSG){
518 if(verbose) fprint(2, "%T fd#%d eof; flushing conn\n", c->fd);
520 /* flush the output queue */
522 while(c->outq != nil)
525 /* flush all outstanding messages */
526 for(i=0; i<NHASH; i++){
527 for(h=c->tag[i]; h; h=hnext){
535 m->tx.oldtag = om->tag;
538 msgincref(m); /* for outq */
540 mm = recvp(c->internal);
542 msgput(m); /* got from recvp */
543 msgput(m); /* got from msgnew */
544 msgput(om); /* got from hash table */
550 /* clunk all outstanding fids */
551 for(i=0; i<NHASH; i++){
552 for(h=c->fid[i]; h; h=hnext){
565 mm = recvp(c->internal);
568 msgput(m); /* got from recvp */
569 msgput(m); /* got from msgnew */
570 fidput(f); /* got from hash table */
578 assert(c->nmsg == 0);
579 assert(c->nfid == 0);
581 chanfree(c->internal);
591 openfdthread(void *v)
604 threadsetname("openfd %s", c->fdfid);
607 if(c->fdmode == OREAD){
609 if(verbose) fprint(2, "%T tread...");
614 m->tx.count = msize - IOHDRSZ;
615 m->tx.fid = fid->fid;
623 if(m->rx.type == Rerror){
624 /* fprint(2, "%T read error: %s\n", m->rx.ename); */
630 if(iowrite(io, c->fd, m->rx.data, m->rx.count) != m->rx.count){
631 /* fprint(2, "%T pipe write error: %r\n"); */
640 if(verbose) fprint(2, "%T twrite...");
644 if((n=ioread(io, c->fd, buf, n)) <= 0){
646 fprint(2, "%T pipe read error: %r\n");
653 m->tx.fid = fid->fid;
663 if(m->rx.type == Rerror){
664 /* fprint(2, "%T write error: %s\n", m->rx.ename); */
672 if(verbose) fprint(2, "%T eof on %d fid %d\n", c->fd, fid->fid);
679 if(verbose) fprint(2, "%T eof on %d fid %d ref %d\n", c->fd, fid->fid, fid->ref);
680 if(--fid->openfd == 0){
686 m->tx.fid = fid->fid;
697 chanfree(c->internal);
710 rerrstr(errs, sizeof errs);
712 /* XXX return here? */
714 if(verbose) fprint(2, "%T xopen pipe %d %d...", p[0], p[1]);
716 /* now we're committed. */
718 /* a new connection for this fid */
719 nc = emalloc(sizeof(Conn));
720 nc->internal = chancreate(sizeof(void*), 0);
726 nc->fdmode = m->tx.mode;
729 /* a thread to tend the pipe */
730 threadcreate(openfdthread, nc, STACK);
732 /* if mode is ORDWR, that openfdthread will write; start a reader */
733 if((m->tx.mode&3) == ORDWR){
734 nc = emalloc(sizeof(Conn));
735 nc->internal = chancreate(sizeof(void*), 0);
740 nc->fd = dup(p[0], -1);
741 threadcreate(openfdthread, nc, STACK);
744 /* steal fid from other connection */
745 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
748 /* rewrite as Ropenfd */
749 m->rx.type = Ropenfd;
751 m->rpkt = erealloc(m->rpkt, n+4);
752 PBIT32(m->rpkt+n, p[1]);
755 m->rpkt[4] = Ropenfd;
761 connoutthread(void *arg)
773 threadsetname("connout %s", c->dir);
774 while((m = recvq(outq)) != nil){
775 err = m->tx.type+1 != m->rx.type;
776 if(!err && m->isopenfd)
783 if(delhash(om->c->tag, om->ctag, om) == 0)
789 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
794 if(verbose) fprint(2, "%T auth error\n");
795 if(delhash(m->c->fid, m->afid->cfid, m->afid) == 0)
801 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
805 if(err || m->rx.nwqid < m->tx.nwname)
806 if(m->tx.fid != m->tx.newfid && m->newfid)
807 if(delhash(m->c->fid, m->newfid->cfid, m->newfid) == 0)
811 if(!err && m->fid->isdir && dotu && !m->c->dotu){
812 m->fid->offset += m->rx.count;
814 m->fid->coffset += m->rx.count;
818 if(!err && dotu && !m->c->dotu)
819 cvtustat(&m->rx, &m->rpkt, 0);
823 m->fid->isdir = (m->rx.qid.type & QTDIR);
826 if(m->rx.type==Rerror && dotu && !c->dotu){
827 ename = estrdup(m->rx.ename);
829 repack(&m->rx, &m->rpkt, c->dotu);
833 if(delhash(m->c->tag, m->ctag, m) == 0)
835 if(verbose > 1) fprint(2, "%T fd#%d <- %F\n", c->fd, &m->rx);
836 rewritehdr(&m->rx, m->rpkt);
837 if(mwrite9p(io, c->fd, m->rpkt) < 0)
838 if(verbose) fprint(2, "%T write error: %r\n");
840 if(c->inputstalled && c->nmsg < MAXMSG)
849 outputthread(void *arg)
856 threadsetname("output");
857 while((m = recvq(outq)) != nil){
858 if(verbose > 1) fprint(2, "%T * <- %F\n", &m->tx);
859 rewritehdr(&m->tx, m->tpkt);
860 if(mwrite9p(io, 1, m->tpkt) < 0)
861 sysfatal("output error: %r");
865 fprint(2, "%T output eof\n");
870 inputthread(void *arg)
877 threadsetname("input");
878 if(verbose) fprint(2, "%T input thread\n");
881 while((pkt = read9ppkt(io, 0)) != nil){
884 fprint(2, "%T short 9P packet from server\n");
888 if(verbose > 2) fprint(2, "%T read %.*H\n", n, pkt);
890 if((m = msgget(tag)) == nil){
891 fprint(2, "%T unexpected 9P response tag %d\n", tag);
895 if((nn = convM2Su(pkt, n, &m->rx, dotu)) != n){
896 fprint(2, "%T bad packet - convM2S %d but %d\n", nn, n);
901 if(verbose > 1) fprint(2, "%T * -> %F%s\n", &m->rx,
902 m->internal ? " (internal)" : "");
906 sendp(m->c->internal, m);
908 sendq(m->c->outq, m);
913 /*fprint(2, "%T input eof\n"); */
918 gethash(Hash **ht, uint n)
922 for(h=ht[n%NHASH]; h; h=h->next)
929 delhash(Hash **ht, uint n, void *v)
933 for(l=&ht[n%NHASH]; h=*l; l=&h->next)
936 if(verbose) fprint(2, "%T delhash %d got %p want %p\n", n, h->v, v);
947 puthash(Hash **ht, uint n, void *v)
953 h = emalloc(sizeof(Hash));
954 h->next = ht[n%NHASH];
971 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
972 if(nfidtab == xafid){
973 fidtab[nfidtab++] = nil;
974 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
976 fidtab[nfidtab] = emalloc(sizeof(Fid));
977 freefid = fidtab[nfidtab];
978 freefid->fid = nfidtab++;
1011 if(verbose > 1) fprint(2, "%T msgincref @0x%lux %p tag %d/%d ref %d=>%d\n",
1012 getcallerpc(&m), m, m->tag, m->ctag, m->ref, m->ref+1);
1022 msgtab = erealloc(msgtab, (nmsgtab+1)*sizeof(msgtab[0]));
1023 msgtab[nmsgtab] = emalloc(sizeof(Msg));
1024 freemsg = msgtab[nmsgtab];
1025 freemsg->tag = nmsgtab++;
1030 if(verbose > 1) fprint(2, "%T msgnew @0x%lux %p tag %d ref %d\n",
1031 getcallerpc(&x), m, m->tag, m->ref);
1037 * Clear data associated with connections, so that
1038 * if all msgs have been msgcleared, the connection
1039 * can be freed. Note that this does *not* free the tpkt
1040 * and rpkt; they are freed in msgput with the msg itself.
1041 * The io write thread might still be holding a ref to msg
1042 * even once the connection has finished with it.
1067 if(m->rx.type == Ropenfd && m->rx.unixfd >= 0){
1068 close(m->rx.unixfd);
1079 if(verbose > 1) fprint(2, "%T msgput 0x%lux %p tag %d/%d ref %d\n",
1080 getcallerpc(&m), m, m->tag, m->ctag, m->ref);
1105 if(n < 0 || n >= nmsgtab)
1110 if(verbose) fprint(2, "%T msgget %d = %p\n", n, m);
1124 sysfatal("out of memory allocating %d", n);
1130 erealloc(void *v, int n)
1135 sysfatal("out of memory reallocating %d", n);
1140 typedef struct Qel Qel;
1161 q = mallocz(sizeof(Queue), 1);
1169 sendq(Queue *q, void *p)
1173 e = emalloc(sizeof(Qel));
1177 werrstr("hungup queue");
1200 while(q->head == nil && !q->hungup)
1215 read9ppkt(Ioproc *io, int fd)
1220 n = ioreadn(io, fd, buf, 4);
1228 nn = ioreadn(io, fd, pkt+4, n-4);
1233 /* would do this if we ever got one of these, but we only generate them
1234 if(pkt[4] == Ropenfd){
1235 newfd = iorecvfd(io, fd);
1236 PBIT32(pkt+n-4, newfd);
1243 mread9p(Ioproc *io, int fd, int dotu)
1249 if((pkt = read9ppkt(io, fd)) == nil)
1255 nn = convM2Su(pkt, n, &m->tx, dotu);
1257 fprint(2, "%T read bad packet from %d\n", fd);
1264 mwrite9p(Ioproc *io, int fd, uchar *pkt)
1269 if(verbose > 2) fprint(2, "%T write %d %d %.*H\n", fd, n, n, pkt);
1270 if(verbose > 1) fprint(2, "%T before iowrite\n");
1271 if(iowrite(io, fd, pkt, n) != n){
1272 fprint(2, "%T write error: %r\n");
1275 if(verbose > 1) fprint(2, "%T after iowrite\n");
1276 if(pkt[4] == Ropenfd){
1277 nfd = GBIT32(pkt+n-4);
1278 if(iosendfd(io, fd, nfd) < 0){
1279 fprint(2, "%T send fd error: %r\n");
1287 restring(uchar *pkt, int pn, char *s)
1291 if(s < (char*)pkt || s >= (char*)pkt+pn)
1296 PBIT16((uchar*)s-1, n);
1300 repack(Fcall *f, uchar **ppkt, int dotu)
1307 nn = sizeS2Mu(f, dotu);
1313 n = convS2Mu(f, pkt, nn, dotu);
1315 sysfatal("convS2M conversion error");
1317 sysfatal("convS2Mu and sizeS2Mu disagree");
1321 rewritehdr(Fcall *f, uchar *pkt)
1326 PBIT16(pkt+5, f->tag);
1330 restring(pkt, n, f->version);
1333 PBIT32(pkt+7, f->afid);
1334 restring(pkt, n, f->uname);
1335 restring(pkt, n, f->aname);
1338 PBIT16(pkt+7, f->oldtag);
1341 restring(pkt, n, f->uname);
1342 restring(pkt, n, f->aname);
1343 PBIT32(pkt+7, f->fid);
1344 PBIT32(pkt+11, f->afid);
1347 PBIT32(pkt+7, f->fid);
1348 PBIT32(pkt+11, f->newfid);
1349 for(i=0; i<f->nwname; i++)
1350 restring(pkt, n, f->wname[i]);
1353 restring(pkt, n, f->name);
1361 PBIT32(pkt+7, f->fid);
1364 PBIT32(pkt+7, f->fid);
1365 PBIT64(pkt+11, f->offset);
1368 restring(pkt, n, f->ename);
1374 _iolisten(va_list *arg)
1378 a = va_arg(*arg, char*);
1379 b = va_arg(*arg, char*);
1380 return listen(a, b);
1384 iolisten(Ioproc *io, char *a, char *b)
1386 return iocall(io, _iolisten, a, b);
1390 _ioaccept(va_list *arg)
1395 fd = va_arg(*arg, int);
1396 dir = va_arg(*arg, char*);
1397 return accept(fd, dir);
1401 ioaccept(Ioproc *io, int fd, char *dir)
1403 return iocall(io, _ioaccept, fd, dir);
1409 static char *mon[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1410 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1414 tm = *localtime(time(0));
1415 return fmtprint(fmt, "%s %2d %02d:%02d:%02d.%03d",
1416 mon[tm.mon], tm.mday, tm.hour, tm.min, tm.sec,
1417 (int)(ns%1000000000)/1000000);
1421 cvtustat(Fcall *f, uchar **fpkt, int tounix)
1428 str = emalloc(f->nstat);
1429 n = convM2Du(f->stat, f->nstat, &dir, str, !tounix);
1435 n = sizeD2Mu(&dir, tounix);
1437 if(convD2Mu(&dir, buf, n, tounix) != n)
1438 sysfatal("convD2Mu conversion error");
1442 repack(f, fpkt, dotu);
1444 f->stat = nil; /* is this okay ??? */
1451 stripudirread(Msg* msg)
1464 for(i = 0; i < rx->count; i += m){
1465 m = BIT16SZ + GBIT16(&rx->data[i]);
1466 if(statchecku((uchar*)&rx->data[i], m, 1) < 0)
1474 buf = emalloc(rx->count);
1477 for(i = 0; i < rx->count; i += m){
1478 m = BIT16SZ + GBIT16(&rx->data[i]);
1479 if(convM2Du((uchar*)&rx->data[i], m, &d, str, 1) != m){
1485 n = convD2M(&d, &buf[nn], rx->count - nn);
1496 rx->data = (char*)buf;
1498 repack(&msg->rx, &msg->rpkt, 0);
1501 rx->data = nil; /* is this okay ??? */