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;
95 void *gethash(Hash**, uint);
96 int puthash(Hash**, uint, void*);
97 int delhash(Hash**, uint, void*);
98 Msg *mread9p(Ioproc*, int);
99 int mwrite9p(Ioproc*, int, uchar*);
100 uchar *read9ppkt(Ioproc*, int);
101 int write9ppkt(int, uchar*);
106 void msgincref(Msg*);
110 void *erealloc(void*, int);
112 int sendq(Queue*, void*);
114 void connthread(void*);
115 void connoutthread(void*);
116 void listenthread(void*);
117 void outputthread(void*);
118 void inputthread(void*);
119 void rewritehdr(Fcall*, uchar*);
120 void repack(Fcall*, uchar**);
121 int tlisten(char*, char*);
122 int taccept(int, char*);
123 int iolisten(Ioproc*, char*, char*);
124 int ioaccept(Ioproc*, int, char*);
125 int iorecvfd(Ioproc*, int);
126 int iosendfd(Ioproc*, int, int);
127 void mainproc(void*);
128 int ignorepipe(void*, char*);
130 void dorootstat(void);
135 fprint(2, "usage: 9pserve [-lnv] [-A aname afid] [-c addr] [-M msize] address\n");
136 fprint(2, "\treads/writes 9P messages on stdin/stdout\n");
137 threadexitsall("usage");
141 threadmaybackground(void)
147 extern int _threaddebuglevel;
149 threadmain(int argc, char **argv)
151 char *file, *x, *addr;
155 x = getenv("verbose9pserve");
158 fprint(2, "verbose9pserve %s => %d\n", x, verbose);
165 xaname = EARGF(usage());
166 xafid = atoi(EARGF(usage()));
170 msize = atoi(EARGF(usage()));
173 addr = netmkaddr(EARGF(usage()), "net", "9fs");
174 if((fd = dial(addr, nil, nil, nil)) < 0)
175 sysfatal("dial %s: %r", addr);
195 if(attached && !versioned){
196 fprint(2, "-A must be used with -M\n");
204 fmtinstall('T', timefmt);
206 if((afd = announce(addr, adir)) < 0)
207 sysfatal("announce %s: %r", addr);
209 if(strncmp(addr, "unix!", 5) == 0)
211 file = smprint("%s.log", addr);
213 sysfatal("smprint log: %r");
214 if((fd = create(file, OWRITE, 0666)) < 0)
215 sysfatal("create %s: %r", file);
220 if(verbose) fprint(2, "%T 9pserve running\n");
221 proccreate(mainproc, nil, STACK);
231 atnotify(ignorepipe, 1);
232 fmtinstall('D', dirfmt);
233 fmtinstall('M', dirmodefmt);
234 fmtinstall('F', fcallfmt);
235 fmtinstall('H', encodefmt);
242 f.version = "9P2000";
245 n = convS2M(&f, vbuf, sizeof vbuf);
247 sysfatal("convS2M conversion error");
248 if(verbose > 1) fprint(2, "%T * <- %F\n", &f);
249 nn = write(1, vbuf, n);
251 sysfatal("error writing Tversion: %r\n");
252 n = read9pmsg(0, vbuf, sizeof vbuf);
254 sysfatal("read9pmsg failure");
255 if(convM2S(vbuf, n, &f) != n)
256 sysfatal("convM2S failure");
259 if(verbose > 1) fprint(2, "%T * -> %F\n", &f);
262 threadcreate(inputthread, nil, STACK);
263 threadcreate(outputthread, nil, STACK);
268 threadcreate(listenthread, nil, STACK);
273 ignorepipe(void *v, char *s)
276 if(strcmp(s, "sys: write on closed pipe") == 0)
278 if(strcmp(s, "sys: tstp") == 0)
280 if(strcmp(s, "sys: window size change") == 0)
282 fprint(2, "9pserve %s: %T note: %s\n", addr, s);
287 listenthread(void *arg)
294 threadsetname("listen %s", adir);
296 c = emalloc(sizeof(Conn));
297 c->fd = iolisten(io, adir, c->dir);
299 if(verbose) fprint(2, "%T listen: %r\n");
304 c->inc = chancreate(sizeof(void*), 0);
305 c->internal = chancreate(sizeof(void*), 0);
308 c->outqdead = chancreate(sizeof(void*), 0);
309 if(verbose) fprint(2, "%T incoming call on %s\n", c->dir);
310 threadcreate(connthread, c, STACK);
320 m->rpkt = emalloc(n);
321 nn = convS2M(&m->rx, m->rpkt, n);
323 sysfatal("convS2M conversion error");
325 sysfatal("sizeS2M and convS2M disagree");
326 sendq(m->c->outq, m);
335 m->tpkt = emalloc(n);
336 nn = convS2M(&m->tx, m->tpkt, n);
338 sysfatal("convS2M conversion error");
340 sysfatal("sizeS2M and convS2M disagree");
345 err(Msg *m, char *ename)
349 m->rx.tag = m->tx.tag;
358 t = emalloc(strlen(s)+1);
364 connthread(void *arg)
369 Msg *m, *om, *mm, sync;
374 threadsetname("conn %s", c->dir);
376 fd = ioaccept(io, c->fd, c->dir);
378 if(verbose) fprint(2, "%T accept %s: %r\n", c->dir);
383 threadcreate(connoutthread, c, STACK);
384 while((m = mread9p(io, c->fd)) != nil){
385 if(verbose > 1) fprint(2, "%T fd#%d -> %F\n", c->fd, &m->tx);
389 if(verbose > 1) fprint(2, "%T fd#%d: new msg %p\n", c->fd, m);
390 if(puthash(c->tag, m->tx.tag, m) < 0){
391 err(m, "duplicate tag");
397 m->rx.tag = m->tx.tag;
398 m->rx.msize = m->tx.msize;
399 if(m->rx.msize > msize)
401 m->rx.version = "9P2000";
402 m->rx.type = Rversion;
406 if((m->oldm = gethash(c->tag, m->tx.oldtag)) == nil){
407 m->rx.tag = m->tx.tag;
416 if(m->tx.afid != NOFID
417 && (m->afid = gethash(c->fid, m->tx.afid)) == nil){
418 err(m, "unknown fid");
423 m->fid = fidnew(m->tx.fid);
424 if(puthash(c->fid, m->tx.fid, m->fid) < 0){
425 err(m, "duplicate fid");
429 if(attached && m->afid==nil){
430 if(m->tx.aname[0] && strcmp(xaname, m->tx.aname) != 0){
431 err(m, "invalid attach name");
435 m->tx.aname = xaname;
436 m->tx.uname = getuser(); /* what srv.c used */
437 repack(&m->tx, &m->tpkt);
441 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
442 err(m, "unknown fid");
446 if(m->tx.newfid == m->tx.fid){
450 m->newfid = fidnew(m->tx.newfid);
451 if(puthash(c->fid, m->tx.newfid, m->newfid) < 0){
452 err(m, "duplicate fid");
460 err(m, "authentication not required");
464 err(m, "authentication rejected");
467 m->afid = fidnew(m->tx.afid);
468 if(puthash(c->fid, m->tx.afid, m->afid) < 0){
469 err(m, "duplicate fid");
475 if(m->tx.perm&(DMSYMLINK|DMDEVICE|DMNAMEDPIPE|DMSOCKET)){
476 err(m, "unsupported file type");
481 if(m->tx.mode&~(OTRUNC|3)){
482 err(m, "bad openfd mode");
497 if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
498 err(m, "unknown fid");
505 /* have everything - translate and send */
510 m->tx.fid = m->fid->fid;
512 m->tx.newfid = m->newfid->fid;
514 m->tx.afid = m->afid->fid;
516 m->tx.oldtag = m->oldm->tag;
517 /* reference passes to outq */
519 while(c->nmsg >= MAXMSG){
525 if(verbose) fprint(2, "%T fd#%d eof; flushing conn\n", c->fd);
527 /* flush all outstanding messages */
528 for(i=0; i<NHASH; i++){
529 while((h = c->tag[i]) != nil){
531 msgincref(om); /* for us */
538 m->tx.oldtag = om->tag;
541 msgincref(m); /* for outq */
543 mm = recvp(c->internal);
545 msgput(m); /* got from recvp */
546 msgput(m); /* got from msgnew */
547 if(delhash(c->tag, om->ctag, om) == 0)
548 msgput(om); /* got from hash table */
549 msgput(om); /* got from msgincref */
554 * outputthread has written all its messages
555 * to the remote connection (because we've gotten all the replies!),
556 * but it might not have gotten a chance to msgput
557 * the very last one. sync up to make sure.
559 memset(&sync, 0, sizeof sync);
565 /* everything is quiet; can close the local output queue. */
569 /* should be no messages left anywhere. */
570 assert(c->nmsg == 0);
572 /* clunk all outstanding fids */
573 for(i=0; i<NHASH; i++){
574 for(h=c->fid[i]; h; h=hnext){
587 mm = recvp(c->internal);
590 msgput(m); /* got from recvp */
591 msgput(m); /* got from msgnew */
592 fidput(f); /* got from hash table */
600 assert(c->nmsg == 0);
601 assert(c->nfid == 0);
603 chanfree(c->internal);
613 openfdthread(void *v)
626 threadsetname("openfd %s", c->fdfid);
629 if(c->fdmode == OREAD){
631 if(verbose) fprint(2, "%T tread...");
636 m->tx.count = msize - IOHDRSZ;
637 m->tx.fid = fid->fid;
645 if(m->rx.type == Rerror){
646 /* fprint(2, "%T read error: %s\n", m->rx.ename); */
652 if(iowrite(io, c->fd, m->rx.data, m->rx.count) != m->rx.count){
653 /* fprint(2, "%T pipe write error: %r\n"); */
662 if(verbose) fprint(2, "%T twrite...");
666 if((n=ioread(io, c->fd, buf, n)) <= 0){
668 fprint(2, "%T pipe read error: %r\n");
675 m->tx.fid = fid->fid;
685 if(m->rx.type == Rerror){
686 /* fprint(2, "%T write error: %s\n", m->rx.ename); */
694 if(verbose) fprint(2, "%T eof on %d fid %d\n", c->fd, fid->fid);
701 if(verbose) fprint(2, "%T eof on %d fid %d ref %d\n", c->fd, fid->fid, fid->ref);
702 if(--fid->openfd == 0){
708 m->tx.fid = fid->fid;
719 chanfree(c->internal);
732 rerrstr(errs, sizeof errs);
734 /* XXX return here? */
736 if(verbose) fprint(2, "%T xopen pipe %d %d...", p[0], p[1]);
738 /* now we're committed. */
740 /* a new connection for this fid */
741 nc = emalloc(sizeof(Conn));
742 nc->internal = chancreate(sizeof(void*), 0);
748 nc->fdmode = m->tx.mode;
751 /* a thread to tend the pipe */
752 threadcreate(openfdthread, nc, STACK);
754 /* if mode is ORDWR, that openfdthread will write; start a reader */
755 if((m->tx.mode&3) == ORDWR){
756 nc = emalloc(sizeof(Conn));
757 nc->internal = chancreate(sizeof(void*), 0);
762 nc->fd = dup(p[0], -1);
763 threadcreate(openfdthread, nc, STACK);
766 /* steal fid from other connection */
767 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
770 /* rewrite as Ropenfd */
771 m->rx.type = Ropenfd;
773 m->rpkt = erealloc(m->rpkt, n+4);
774 PBIT32(m->rpkt+n, p[1]);
777 m->rpkt[4] = Ropenfd;
783 connoutthread(void *arg)
792 threadsetname("connout %s", c->dir);
793 while((m = recvq(c->outq)) != nil){
794 err = m->tx.type+1 != m->rx.type;
795 if(!err && m->isopenfd)
802 if(delhash(om->c->tag, om->ctag, om) == 0)
808 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
813 if(verbose) fprint(2, "%T auth error\n");
814 if(delhash(m->c->fid, m->afid->cfid, m->afid) == 0)
820 if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
824 if(err || m->rx.nwqid < m->tx.nwname)
825 if(m->tx.fid != m->tx.newfid && m->newfid)
826 if(delhash(m->c->fid, m->newfid->cfid, m->newfid) == 0)
835 m->fid->isdir = (m->rx.qid.type & QTDIR);
838 if(delhash(m->c->tag, m->ctag, m) == 0)
840 if(verbose > 1) fprint(2, "%T fd#%d <- %F\n", c->fd, &m->rx);
841 rewritehdr(&m->rx, m->rpkt);
842 if(mwrite9p(io, c->fd, m->rpkt) < 0)
843 if(verbose) fprint(2, "%T write error: %r\n");
845 if(c->inputstalled && c->nmsg < MAXMSG)
851 sendp(c->outqdead, nil);
855 outputthread(void *arg)
862 threadsetname("output");
863 while((m = recvq(outq)) != nil){
865 sendp(m->c->outqdead, nil);
868 if(verbose > 1) fprint(2, "%T * <- %F\n", &m->tx);
869 rewritehdr(&m->tx, m->tpkt);
870 if(mwrite9p(io, 1, m->tpkt) < 0)
871 sysfatal("output error: %r");
875 fprint(2, "%T output eof\n");
880 inputthread(void *arg)
887 threadsetname("input");
888 if(verbose) fprint(2, "%T input thread\n");
891 while((pkt = read9ppkt(io, 0)) != nil){
894 fprint(2, "%T short 9P packet from server\n");
898 if(verbose > 2) fprint(2, "%T read %.*H\n", n, pkt);
900 if((m = msgget(tag)) == nil){
901 fprint(2, "%T unexpected 9P response tag %d\n", tag);
905 if((nn = convM2S(pkt, n, &m->rx)) != n){
906 fprint(2, "%T bad packet - convM2S %d but %d\n", nn, n);
911 if(verbose > 1) fprint(2, "%T * -> %F%s\n", &m->rx,
912 m->internal ? " (internal)" : "");
916 sendp(m->c->internal, m);
918 sendq(m->c->outq, m);
923 /*fprint(2, "%T input eof\n"); */
928 gethash(Hash **ht, uint n)
932 for(h=ht[n%NHASH]; h; h=h->next)
939 delhash(Hash **ht, uint n, void *v)
943 for(l=&ht[n%NHASH]; h=*l; l=&h->next)
946 if(verbose) fprint(2, "%T delhash %d got %p want %p\n", n, h->v, v);
957 puthash(Hash **ht, uint n, void *v)
963 h = emalloc(sizeof(Hash));
964 h->next = ht[n%NHASH];
981 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
982 if(nfidtab == xafid){
983 fidtab[nfidtab++] = nil;
984 fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
986 fidtab[nfidtab] = emalloc(sizeof(Fid));
987 freefid = fidtab[nfidtab];
988 freefid->fid = nfidtab++;
1021 if(verbose > 1) fprint(2, "%T msgincref @0x%lux %p tag %d/%d ref %d=>%d\n",
1022 getcallerpc(&m), m, m->tag, m->ctag, m->ref, m->ref+1);
1032 msgtab = erealloc(msgtab, (nmsgtab+1)*sizeof(msgtab[0]));
1033 msgtab[nmsgtab] = emalloc(sizeof(Msg));
1034 freemsg = msgtab[nmsgtab];
1035 freemsg->tag = nmsgtab++;
1040 if(verbose > 1) fprint(2, "%T msgnew @0x%lux %p tag %d ref %d\n",
1041 getcallerpc(&x), m, m->tag, m->ref);
1047 * Clear data associated with connections, so that
1048 * if all msgs have been msgcleared, the connection
1049 * can be freed. Note that this does *not* free the tpkt
1050 * and rpkt; they are freed in msgput with the msg itself.
1051 * The io write thread might still be holding a ref to msg
1052 * even once the connection has finished with it.
1077 if(m->rx.type == Ropenfd && m->rx.unixfd >= 0){
1078 close(m->rx.unixfd);
1089 if(verbose > 1) fprint(2, "%T msgput 0x%lux %p tag %d/%d ref %d\n",
1090 getcallerpc(&m), m, m->tag, m->ctag, m->ref);
1115 if(n < 0 || n >= nmsgtab)
1120 if(verbose) fprint(2, "%T msgget %d = %p\n", n, m);
1134 sysfatal("out of memory allocating %d", n);
1140 erealloc(void *v, int n)
1145 sysfatal("out of memory reallocating %d", n);
1150 typedef struct Qel Qel;
1170 q = mallocz(sizeof(Queue), 1);
1178 sendq(Queue *q, void *p)
1182 e = emalloc(sizeof(Qel));
1203 while(q->head == nil)
1214 read9ppkt(Ioproc *io, int fd)
1219 n = ioreadn(io, fd, buf, 4);
1227 nn = ioreadn(io, fd, pkt+4, n-4);
1232 /* would do this if we ever got one of these, but we only generate them
1233 if(pkt[4] == Ropenfd){
1234 newfd = iorecvfd(io, fd);
1235 PBIT32(pkt+n-4, newfd);
1242 mread9p(Ioproc *io, int fd)
1248 if((pkt = read9ppkt(io, fd)) == nil)
1254 nn = convM2S(pkt, n, &m->tx);
1256 fprint(2, "%T read bad packet from %d\n", fd);
1265 mwrite9p(Ioproc *io, int fd, uchar *pkt)
1270 if(verbose > 2) fprint(2, "%T write %d %d %.*H\n", fd, n, n, pkt);
1271 if(verbose > 1) fprint(2, "%T before iowrite\n");
1272 if(iowrite(io, fd, pkt, n) != n){
1273 fprint(2, "%T write error: %r\n");
1276 if(verbose > 1) fprint(2, "%T after iowrite\n");
1277 if(pkt[4] == Ropenfd){
1278 nfd = GBIT32(pkt+n-4);
1279 if(iosendfd(io, fd, nfd) < 0){
1280 fprint(2, "%T send fd error: %r\n");
1288 restring(uchar *pkt, int pn, char *s)
1292 if(s < (char*)pkt || s >= (char*)pkt+pn)
1297 PBIT16((uchar*)s-1, n);
1301 repack(Fcall *f, uchar **ppkt)
1314 n = convS2M(f, pkt, nn);
1316 sysfatal("convS2M conversion error");
1318 sysfatal("convS2M and sizeS2M disagree");
1322 rewritehdr(Fcall *f, uchar *pkt)
1327 PBIT16(pkt+5, f->tag);
1331 restring(pkt, n, f->version);
1334 PBIT32(pkt+7, f->afid);
1335 restring(pkt, n, f->uname);
1336 restring(pkt, n, f->aname);
1339 PBIT16(pkt+7, f->oldtag);
1342 restring(pkt, n, f->uname);
1343 restring(pkt, n, f->aname);
1344 PBIT32(pkt+7, f->fid);
1345 PBIT32(pkt+11, f->afid);
1348 PBIT32(pkt+7, f->fid);
1349 PBIT32(pkt+11, f->newfid);
1350 for(i=0; i<f->nwname; i++)
1351 restring(pkt, n, f->wname[i]);
1354 restring(pkt, n, f->name);
1362 PBIT32(pkt+7, f->fid);
1365 PBIT32(pkt+7, f->fid);
1366 PBIT64(pkt+11, f->offset);
1369 restring(pkt, n, f->ename);
1375 _iolisten(va_list *arg)
1379 a = va_arg(*arg, char*);
1380 b = va_arg(*arg, char*);
1381 return listen(a, b);
1385 iolisten(Ioproc *io, char *a, char *b)
1387 return iocall(io, _iolisten, a, b);
1391 _ioaccept(va_list *arg)
1396 fd = va_arg(*arg, int);
1397 dir = va_arg(*arg, char*);
1398 return accept(fd, dir);
1402 ioaccept(Ioproc *io, int fd, char *dir)
1404 return iocall(io, _ioaccept, fd, dir);
1410 static char *mon[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1411 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1415 tm = *localtime(time(0));
1416 return fmtprint(fmt, "%s %2d %02d:%02d:%02d.%03d",
1417 mon[tm.mon], tm.mday, tm.hour, tm.min, tm.sec,
1418 (int)(ns%1000000000)/1000000);