8 void (*_forker)(void(*)(void*), void*, int);
10 static char Ebadattach[] = "unknown specifier in attach";
11 static char Ebadoffset[] = "bad offset";
12 static char Ebadcount[] = "bad count";
13 static char Ebotch[] = "9P protocol botch";
14 static char Ecreatenondir[] = "create in non-directory";
15 static char Edupfid[] = "duplicate fid";
16 static char Eduptag[] = "duplicate tag";
17 static char Eisdir[] = "is a directory";
18 static char Enocreate[] = "create prohibited";
19 static char Enomem[] = "out of memory";
20 static char Enoremove[] = "remove prohibited";
21 static char Enostat[] = "stat prohibited";
22 static char Enotfound[] = "file not found";
23 static char Enowrite[] = "write prohibited";
24 static char Enowstat[] = "wstat prohibited";
25 static char Eperm[] = "permission denied";
26 static char Eunknownfid[] = "unknown fid";
27 static char Ebaddir[] = "bad directory in wstat";
28 static char Ewalknodir[] = "walk in non-directory";
31 setfcallerror(Fcall *f, char *err)
38 changemsize(Srv *srv, int msize)
40 if(srv->rbuf && srv->wbuf && srv->msize == msize)
47 srv->rbuf = emalloc9p(msize);
48 srv->wbuf = emalloc9p(msize);
62 while((n = read9pmsg(s->infd, s->rbuf, s->msize)) == 0)
70 memmove(buf, s->rbuf, n);
73 if(convM2S(buf, n, &f) != n){
78 if((r=allocreq(s->rpool, f.tag)) == nil){ /* duplicate tag: cons up a fake Req */
79 r = emalloc9p(sizeof *r);
90 fprint(2, "<-%d- %F: dup tag\n", s->infd, &f);
98 memset(&r->ofcall, 0, sizeof r->ofcall);
99 r->type = r->ifcall.type;
103 fprint(2, "<-%d- %F: %s\n", s->infd, &r->ifcall, r->error);
105 fprint(2, "<-%d- %F\n", s->infd, &r->ifcall);
120 for(i=0; i<r->ifcall.nwname; i++)
121 if(f = walkfile(f, r->ifcall.wname[i]))
122 r->ofcall.wqid[i] = f->qid;
129 r->newfid->qid = r->newfid->file->qid;
135 walkandclone(Req *r, char *(*walk1)(Fid*, char*, void*), char *(*clone)(Fid*, Fid*, void*), void *arg)
140 if(r->fid == r->newfid && r->ifcall.nwname > 1){
141 respond(r, "lib9p: unused documented feature not implemented");
145 if(r->fid != r->newfid){
146 r->newfid->qid = r->fid->qid;
147 if(clone && (e = clone(r->fid, r->newfid, arg))){
154 for(i=0; i<r->ifcall.nwname; i++){
155 if(e = walk1(r->newfid, r->ifcall.wname[i], arg))
157 r->ofcall.wqid[i] = r->newfid->qid;
168 sversion(Srv *srv, Req *r)
170 if(srv->rref.ref != 2){
174 if(strncmp(r->ifcall.version, "9P", 2) != 0){
175 r->ofcall.version = "unknown";
179 r->ofcall.version = "9P2000";
180 if(r->ifcall.msize < 256){
181 respond(r, "version: message size too small");
184 if(r->ifcall.msize < 1024*1024)
185 r->ofcall.msize = r->ifcall.msize;
187 r->ofcall.msize = 1024*1024;
192 rversion(Req *r, char *error)
195 changemsize(r->srv, r->ofcall.msize);
199 sauth(Srv *srv, Req *r)
203 if((r->afid = allocfid(srv->fpool, r->ifcall.afid)) == nil){
210 snprint(e, sizeof e, "%s: authentication not required", argv0);
215 rauth(Req *r, char *error)
218 closefid(removefid(r->srv->fpool, r->afid->fid));
222 sattach(Srv *srv, Req *r)
224 if((r->fid = allocfid(srv->fpool, r->ifcall.fid)) == nil){
229 if(r->ifcall.afid != NOFID && (r->afid = lookupfid(srv->fpool, r->ifcall.afid)) == nil){
230 respond(r, Eunknownfid);
233 r->fid->uid = estrdup9p(r->ifcall.uname);
235 r->fid->file = srv->tree->root;
236 incref(r->fid->file);
237 r->ofcall.qid = r->fid->file->qid;
238 r->fid->qid = r->ofcall.qid;
247 rattach(Req *r, char *error)
250 closefid(removefid(r->srv->fpool, r->fid->fid));
254 sflush(Srv *srv, Req *r)
256 r->oldreq = lookupreq(srv->rpool, r->ifcall.oldtag);
257 if(r->oldreq == nil || r->oldreq == r)
265 rflush(Req *r, char *error)
269 assert(error == nil);
273 if(or->responded == 0){
274 or->flush = erealloc9p(or->flush, (or->nflush+1)*sizeof(or->flush[0]));
275 or->flush[or->nflush++] = r;
277 return -1; /* delay response until or is responded */
287 oldwalk1(Fid *fid, char *name, void *arg)
294 e = srv->walk1(fid, name, &qid);
302 oldclone(Fid *fid, Fid *newfid, void *arg)
307 if(srv->clone == nil)
309 return srv->clone(fid, newfid);
313 swalk(Srv *srv, Req *r)
315 if((r->fid = lookupfid(srv->fpool, r->ifcall.fid)) == nil){
316 respond(r, Eunknownfid);
319 if(r->fid->omode != -1){
320 respond(r, "cannot clone open fid");
323 if(r->ifcall.nwname && !(r->fid->qid.type&QTDIR)){
324 respond(r, Ewalknodir);
327 if(r->ifcall.fid != r->ifcall.newfid){
328 if((r->newfid = allocfid(srv->fpool, r->ifcall.newfid)) == nil){
332 r->newfid->uid = estrdup9p(r->fid->uid);
334 incref(&r->fid->ref);
340 walkandclone(r, oldwalk1, oldclone, srv);
344 sysfatal("no walk function, no file trees");
347 rwalk(Req *r, char *error)
349 if(error || r->ofcall.nwqid < r->ifcall.nwname){
350 if(r->ifcall.fid != r->ifcall.newfid && r->newfid)
351 closefid(removefid(r->srv->fpool, r->newfid->fid));
352 if (r->ofcall.nwqid==0){
353 if(error==nil && r->ifcall.nwname!=0)
354 r->error = Enotfound;
356 r->error = nil; // No error on partial walks
358 if(r->ofcall.nwqid == 0){
360 r->newfid->qid = r->fid->qid;
362 /* if file trees are in use, filewalk took care of the rest */
363 r->newfid->qid = r->ofcall.wqid[r->ofcall.nwqid-1];
369 dirwritable(Fid *fid)
376 if(f->parent && !hasperm(f->parent, fid->uid, AWRITE)){
386 sopen(Srv *srv, Req *r)
390 if((r->fid = lookupfid(srv->fpool, r->ifcall.fid)) == nil){
391 respond(r, Eunknownfid);
394 if(r->fid->omode != -1){
398 if((r->fid->qid.type&QTDIR) && (r->ifcall.mode&~ORCLOSE) != OREAD){
402 r->ofcall.qid = r->fid->qid;
403 switch(r->ifcall.mode&3){
419 if(r->ifcall.mode&OTRUNC)
421 if((r->fid->qid.type&QTDIR) && p!=AREAD){
426 if(!hasperm(r->fid->file, r->fid->uid, p)){
430 if((r->ifcall.mode&ORCLOSE) && !dirwritable(r->fid)){
434 r->ofcall.qid = r->fid->file->qid;
435 if((r->ofcall.qid.type&QTDIR)
436 && (r->fid->rdir = opendirfile(r->fid->file)) == nil){
437 respond(r, "opendirfile failed");
447 ropen(Req *r, char *error)
453 snprint(errbuf, sizeof errbuf, "fid mode is 0x%ux\n", r->ifcall.mode);
454 write(2, errbuf, strlen(errbuf));
456 r->fid->omode = r->ifcall.mode;
457 r->fid->qid = r->ofcall.qid;
458 if(r->ofcall.qid.type&QTDIR)
459 r->fid->diroffset = 0;
463 screate(Srv *srv, Req *r)
465 if((r->fid = lookupfid(srv->fpool, r->ifcall.fid)) == nil)
466 respond(r, Eunknownfid);
467 else if(r->fid->omode != -1)
469 else if(!(r->fid->qid.type&QTDIR))
470 respond(r, Ecreatenondir);
471 else if(r->fid->file && !hasperm(r->fid->file, r->fid->uid, AWRITE))
476 respond(r, Enocreate);
479 rcreate(Req *r, char *error)
483 r->fid->omode = r->ifcall.mode;
484 r->fid->qid = r->ofcall.qid;
488 sread(Srv *srv, Req *r)
492 if((r->fid = lookupfid(srv->fpool, r->ifcall.fid)) == nil){
493 respond(r, Eunknownfid);
496 if((int)r->ifcall.count < 0){
500 if(r->ifcall.offset < 0
501 || ((r->fid->qid.type&QTDIR) && r->ifcall.offset != 0 && r->ifcall.offset != r->fid->diroffset)){
502 respond(r, Ebadoffset);
506 if(r->ifcall.count > srv->msize - IOHDRSZ)
507 r->ifcall.count = srv->msize - IOHDRSZ;
508 r->rbuf = emalloc9p(r->ifcall.count);
509 r->ofcall.data = r->rbuf;
510 o = r->fid->omode & 3;
511 if(o != OREAD && o != ORDWR && o != OEXEC){
515 if((r->fid->qid.type&QTDIR) && r->fid->file){
516 r->ofcall.count = readdirfile(r->fid->rdir, r->rbuf, r->ifcall.count);
523 respond(r, "no srv->read");
526 rread(Req *r, char *error)
528 if(error==nil && (r->fid->qid.type&QTDIR))
529 r->fid->diroffset += r->ofcall.count;
533 swrite(Srv *srv, Req *r)
538 if((r->fid = lookupfid(srv->fpool, r->ifcall.fid)) == nil){
539 respond(r, Eunknownfid);
542 if((int)r->ifcall.count < 0){
546 if(r->ifcall.offset < 0){
550 if(r->ifcall.count > srv->msize - IOHDRSZ)
551 r->ifcall.count = srv->msize - IOHDRSZ;
552 o = r->fid->omode & 3;
553 if(o != OWRITE && o != ORDWR){
554 snprint(e, sizeof e, "write on fid with open mode 0x%ux", r->fid->omode);
561 respond(r, "no srv->write");
564 rwrite(Req *r, char *error)
569 r->fid->file->qid.vers++;
573 sclunk(Srv *srv, Req *r)
575 if((r->fid = removefid(srv->fpool, r->ifcall.fid)) == nil)
576 respond(r, Eunknownfid);
586 sremove(Srv *srv, Req *r)
588 if((r->fid = removefid(srv->fpool, r->ifcall.fid)) == nil){
589 respond(r, Eunknownfid);
592 if(!dirwritable(r->fid)){
599 respond(r, r->fid->file ? nil : Enoremove);
602 rremove(Req *r, char *error, char *errbuf)
607 if(removefile(r->fid->file) < 0){
608 snprint(errbuf, ERRMAX, "remove %s: %r",
617 sstat(Srv *srv, Req *r)
619 if((r->fid = lookupfid(srv->fpool, r->ifcall.fid)) == nil){
620 respond(r, Eunknownfid);
624 /* should we rlock the file? */
625 r->d = r->fid->file->Dir;
627 r->d.name = estrdup9p(r->d.name);
629 r->d.uid = estrdup9p(r->d.uid);
631 r->d.gid = estrdup9p(r->d.gid);
633 r->d.muid = estrdup9p(r->d.muid);
637 else if(r->fid->file)
643 rstat(Req *r, char *error)
651 if(convD2M(&r->d, tmp, BIT16SZ) != BIT16SZ){
652 r->error = "convD2M(_,_,BIT16SZ) did not return BIT16SZ";
655 n = GBIT16(tmp)+BIT16SZ;
656 statbuf = emalloc9p(n);
658 r->error = "out of memory";
661 r->ofcall.nstat = convD2M(&r->d, statbuf, n);
662 r->ofcall.stat = statbuf; /* freed in closereq */
663 if(r->ofcall.nstat <= BIT16SZ){
664 r->error = "convD2M fails";
671 swstat(Srv *srv, Req *r)
673 if((r->fid = lookupfid(srv->fpool, r->ifcall.fid)) == nil){
674 respond(r, Eunknownfid);
677 if(srv->wstat == nil){
678 respond(r, Enowstat);
681 if(convM2D(r->ifcall.stat, r->ifcall.nstat, &r->d, (char*)r->ifcall.stat) != r->ifcall.nstat){
685 if(r->d.qid.path != ~0 && r->d.qid.path != r->fid->qid.path){
686 respond(r, "wstat -- attempt to change qid.path");
689 if(r->d.qid.vers != ~0 && r->d.qid.vers != r->fid->qid.vers){
690 respond(r, "wstat -- attempt to change qid.vers");
694 if(r->d.mode & ~(DMDIR|DMAPPEND|DMEXCL|DMTMP|0777)){
695 respond(r, "wstat -- unknown bits in mode");
698 if(r->d.qid.type != (uchar)~0 && r->d.qid.type != ((r->d.mode>>24)&0xFF)){
699 respond(r, "wstat -- qid.type/mode mismatch");
702 if(((r->d.mode>>24) ^ r->fid->qid.type) & ~(QTAPPEND|QTEXCL|QTTMP)){
703 respond(r, "wstat -- attempt to change qid.type");
707 if(r->d.qid.type != (uchar)~0 && r->d.qid.type != r->fid->qid.type){
708 respond(r, "wstat -- attempt to change qid.type");
719 static void srvclose(Srv *);
729 while(r = getreq(srv)){
732 respond(r, r->error);
736 switch(r->ifcall.type){
738 respond(r, "unknown message");
740 case Tversion: sversion(srv, r); break;
741 case Tauth: sauth(srv, r); break;
742 case Tattach: sattach(srv, r); break;
743 case Tflush: sflush(srv, r); break;
744 case Twalk: swalk(srv, r); break;
745 case Topen: sopen(srv, r); break;
746 case Tcreate: screate(srv, r); break;
747 case Tread: sread(srv, r); break;
748 case Twrite: swrite(srv, r); break;
749 case Tclunk: sclunk(srv, r); break;
750 case Tremove: sremove(srv, r); break;
751 case Tstat: sstat(srv, r); break;
752 case Twstat: swstat(srv, r); break;
754 qunlock(&srv->slock);
763 if(decref(&srv->rref))
767 fprint(2, "srvclose\n");
774 freefidpool(srv->fpool);
776 freereqpool(srv->rpool);
793 if(decref(&srv->sref) == 0)
794 _forker(srvwork, srv, 0);
795 qunlock(&srv->slock);
801 fmtinstall('D', dirfmt);
802 fmtinstall('F', fcallfmt);
807 if(srv->fpool == nil)
808 srv->fpool = allocfidpool(srv->destroyfid);
809 if(srv->rpool == nil)
810 srv->rpool = allocreqpool(srv->destroyreq);
812 srv->msize = 8192+IOHDRSZ;
814 changemsize(srv, srv->msize);
816 srv->fpool->srv = srv;
817 srv->rpool->srv = srv;
826 respond(Req *r, char *error)
835 assert(r->responded == 0);
838 switch(r->ifcall.type){
840 * Flush is special. If the handler says so, we return
841 * without further processing. Respond will be called
842 * again once it is safe.
845 if(rflush(r, error)<0)
848 case Tversion: rversion(r, error); break;
849 case Tauth: rauth(r, error); break;
850 case Tattach: rattach(r, error); break;
851 case Twalk: rwalk(r, error); break;
852 case Topen: ropen(r, error); break;
853 case Tcreate: rcreate(r, error); break;
854 case Tread: rread(r, error); break;
855 case Twrite: rwrite(r, error); break;
856 case Tclunk: rclunk(r, error); break;
857 case Tremove: rremove(r, error, errbuf); break;
858 case Tstat: rstat(r, error); break;
859 case Twstat: rwstat(r, error); break;
862 r->ofcall.tag = r->ifcall.tag;
863 r->ofcall.type = r->ifcall.type+1;
865 setfcallerror(&r->ofcall, r->error);
868 fprint(2, "-%d-> %F\n", srv->outfd, &r->ofcall);
871 n = convS2M(&r->ofcall, srv->wbuf, srv->msize);
873 fprint(2, "msize = %d n = %d %F\n", srv->msize, n, &r->ofcall);
877 if(r->pool) /* not a fake */
878 closereq(removereq(r->pool, r->ifcall.tag));
879 m = write(srv->outfd, srv->wbuf, n);
881 fprint(2, "lib9p srv: write %d returned %d on fd %d: %r", n, m, srv->outfd);
882 qunlock(&srv->wlock);
884 qlock(&r->lk); /* no one will add flushes now */
888 for(i=0; i<r->nflush; i++)
889 respond(r->flush[i], nil);
907 rerrstr(errbuf, sizeof errbuf);
912 postfd(char *name, int pfd)
917 snprint(buf, sizeof buf, "/srv/%s", name);
919 fprint(2, "postfd %s\n", buf);
920 fd = create(buf, OWRITE|ORCLOSE|OCEXEC, 0600);
923 fprint(2, "create fails: %r\n");
926 if(fprint(fd, "%d", pfd) < 0){
928 fprint(2, "write fails: %r\n");
933 fprint(2, "postfd successful\n");
938 sharefd(char *name, char *desc, int pfd)
943 snprint(buf, sizeof buf, "#σc/%s", name);
944 if((fd = create(buf, OREAD, 0700|DMDIR)) >= 0)
946 snprint(buf, sizeof buf, "#σc/%s/%s", name, desc);
948 fprint(2, "sharefd %s\n", buf);
949 fd = create(buf, OWRITE, 0600);
952 fprint(2, "create fails: %r\n");
955 if(fprint(fd, "%d\n", pfd) < 0){
957 fprint(2, "write fails: %r\n");
963 fprint(2, "sharefd successful\n");