16 typedef struct Dirtab Dirtab;
17 typedef struct Fid Fid;
18 typedef struct Holdq Holdq;
19 typedef struct Readreq Readreq;
20 typedef struct Sendreq Sendreq;
28 int nopen; /* #fids open on this port */
43 long offset; /* zeroed at beginning of each message, read or write */
44 char *writebuf; /* partial message written so far; offset tells how much */
59 int nfid; /* number of fids that should receive this message */
60 int nleft; /* number left that haven't received it */
61 Fid **fid; /* fid[nfid] */
63 char *pack; /* plumbpack()ed message */
64 int npack; /* length of pack */
74 struct /* needed because incref() doesn't return value */
93 static Dirtab dir[NDIR] =
95 { ".", QTDIR, Qdir, 0500|DMDIR },
96 { "rules", QTFILE, Qrules, 0600 },
97 { "send", QTFILE, Qsend, 0200 },
99 static int ndir = NQID;
102 static int srvclosefd; /* rock for end of pipe to close */
105 static Fid *fids[Nhash];
106 static QLock readlock;
108 static char srvfile[128];
109 static int messagesize = 8192+IOHDRSZ; /* good start */
111 static void fsysproc(void*);
112 static void fsysrespond(Fcall*, uchar*, char*);
113 static Fid* newfid(int);
115 static Fcall* fsysflush(Fcall*, uchar*, Fid*);
116 static Fcall* fsysversion(Fcall*, uchar*, Fid*);
117 static Fcall* fsysauth(Fcall*, uchar*, Fid*);
118 static Fcall* fsysattach(Fcall*, uchar*, Fid*);
119 static Fcall* fsyswalk(Fcall*, uchar*, Fid*);
120 static Fcall* fsysopen(Fcall*, uchar*, Fid*);
121 static Fcall* fsyscreate(Fcall*, uchar*, Fid*);
122 static Fcall* fsysread(Fcall*, uchar*, Fid*);
123 static Fcall* fsyswrite(Fcall*, uchar*, Fid*);
124 static Fcall* fsysclunk(Fcall*, uchar*, Fid*);
125 static Fcall* fsysremove(Fcall*, uchar*, Fid*);
126 static Fcall* fsysstat(Fcall*, uchar*, Fid*);
127 static Fcall* fsyswstat(Fcall*, uchar*, Fid*);
129 Fcall* (*fcall[Tmax])(Fcall*, uchar*, Fid*) =
131 [Tflush] = fsysflush,
132 [Tversion] = fsysversion,
134 [Tattach] = fsysattach,
137 [Tcreate] = fsyscreate,
139 [Twrite] = fsyswrite,
140 [Tclunk] = fsysclunk,
141 [Tremove]= fsysremove,
143 [Twstat] = fsyswstat,
146 char Ebadfcall[] = "bad fcall type";
147 char Eperm[] = "permission denied";
148 char Enomem[] = "malloc failed for buffer";
149 char Enotdir[] = "not a directory";
150 char Enoexist[] = "plumb file does not exist";
151 char Eisdir[] = "file is a directory";
152 char Ebadmsg[] = "bad plumb message format";
153 char Enosuchport[] ="no such plumb port";
154 char Enoport[] = "couldn't find destination for message";
155 char Einuse[] = "file already open";
158 * Add new port. A no-op if port already exists or is the null string
167 for(i=NQID; i<ndir; i++)
168 if(strcmp(port, dir[i].name) == 0)
171 fprint(2, "plumb: too many ports; max %d\n", NDIR);
175 dir[i].name = estrdup(port);
179 ports = erealloc(ports, nports*sizeof(char*));
180 ports[nports-1] = dir[i].name;
189 read(clockfd, buf, sizeof buf);
198 fmtinstall('F', fcallfmt);
199 clockfd = open("/dev/time", OREAD|OCEXEC);
202 error("can't create pipe: %r");
203 /* 0 will be server end, 1 will be client end */
206 sprint(srvfile, "/srv/plumb.%s.%d", user, getpid());
207 if(putenv("plumbsrv", srvfile) < 0)
208 error("can't write $plumbsrv: %r");
209 fd = create(srvfile, OWRITE|OCEXEC|ORCLOSE, 0600);
211 error("can't create /srv file: %r");
212 if(fprint(fd, "%d", p[1]) <= 0)
213 error("can't write /srv/file: %r");
214 /* leave fd open; ORCLOSE will take care of it */
216 procrfork(fsysproc, nil, Stack, RFFDG);
219 if(mount(p[1], -1, "/mnt/plumb", MREPL, "") < 0)
220 error("can't mount /mnt/plumb: %r");
236 buf = malloc(messagesize); /* avoid memset of emalloc */
238 error("malloc failed: %r");
240 n = read9pmsg(srvfd, buf, messagesize);
243 error("i/o error on server channel");
244 threadexitsall("unmounted");
246 if(readlock.head == nil) /* no other processes waiting to read; start one */
247 proccreate(fsysproc, nil, Stack);
250 t = emalloc(sizeof(Fcall));
251 if(convM2S(buf, n, t) != n)
252 error("convert error in convM2S");
254 fprint(2, "<= %F\n", t);
255 if(fcall[t->type] == nil)
256 fsysrespond(t, buf, Ebadfcall);
258 if(t->type==Tversion || t->type==Tauth)
262 t = (*fcall[t->type])(t, buf, f);
268 fsysrespond(Fcall *t, uchar *buf, char *err)
278 buf = emalloc(messagesize);
279 n = convS2M(t, buf, messagesize);
281 error("convert error in convS2M");
282 if(write(srvfd, buf, n) != n)
283 error("write error in respond");
285 fprint(2, "=> %F\n", t);
297 fh = &fids[fid&(Nhash-1)];
298 for(f=*fh; f; f=f->next)
301 else if(ff==nil && !f->busy)
308 f = emalloc(sizeof *f);
318 dostat(Dirtab *dir, uchar *buf, uint nbuf, uint clock)
322 d.qid.type = dir->type;
323 d.qid.path = dir->qid;
326 d.length = 0; /* would be nice to do better */
333 return convD2M(&d, buf, nbuf);
337 queuesend(Dirtab *d, Plumbmsg *m)
343 s = emalloc(sizeof(Sendreq));
346 s->fid = emalloc(s->nfid*sizeof(Fid*));
348 /* build array of fids open on this channel */
349 for(f=d->fopen; f!=nil; f=f->nextopen)
353 /* link to end of queue; drainqueue() searches in sender order so this implements a FIFO */
354 for(t=d->sendq; t!=nil; t=t->next)
364 queueread(Dirtab *d, Fcall *t, uchar *buf, Fid *f)
368 r = emalloc(sizeof(Readreq));
377 drainqueue(Dirtab *d)
379 Readreq *r, *nextr, *prevr;
380 Sendreq *s, *nexts, *prevs;
384 for(s=d->sendq; s!=nil; s=nexts){
386 for(i=0; i<s->nfid; i++){
388 for(r=d->readq; r!=nil; r=nextr){
390 if(r->fid == s->fid[i]){
391 /* pack the message if necessary */
393 s->pack = plumbpack(s->msg, &s->npack);
394 /* exchange the stuff... */
395 r->fcall->data = s->pack+r->fid->offset;
396 n = s->npack - r->fid->offset;
397 if(n > messagesize-IOHDRSZ)
398 n = messagesize-IOHDRSZ;
399 if(n > r->fcall->count)
402 fsysrespond(r->fcall, r->buf, nil);
404 if(r->fid->offset >= s->npack){
405 /* message transferred; delete this fid from send queue */
410 /* delete read request from queue */
412 prevr->next = r->next;
422 /* if no fids left, delete this send from queue */
428 prevs->next = s->next;
437 /* can't flush a send because they are always answered synchronously */
439 flushqueue(Dirtab *d, int oldtag)
444 for(r=d->readq; r!=nil; r=r->next){
445 if(oldtag == r->fcall->tag){
446 /* delete read request from queue */
448 prevr->next = r->next;
460 /* remove messages awaiting delivery to now-closing fid */
462 removesenders(Dirtab *d, Fid *fid)
464 Sendreq *s, *nexts, *prevs;
468 for(s=d->sendq; s!=nil; s=nexts){
470 for(i=0; i<s->nfid; i++)
471 if(fid == s->fid[i]){
472 /* delete this fid from send queue */
477 /* if no fids left, delete this send from queue */
483 prevs->next = s->next;
493 hold(Plumbmsg *m, Dirtab *d)
497 h = emalloc(sizeof(Holdq));
499 /* add to end of queue */
503 for(q=d->holdq; q->next!=nil; q=q->next)
514 while(d->holdq != nil){
517 queuesend(d, h->msg);
518 /* no need to drain queue because we know no-one is reading yet */
524 dispose(Fcall *t, uchar *buf, Plumbmsg *m, Ruleset *rs, Exec *e)
531 if(m->dst==nil || m->dst[0]=='\0'){
534 err = startup(rs, e);
537 for(i=NQID; i<ndir; i++)
538 if(strcmp(m->dst, dir[i].name) == 0){
539 if(dir[i].nopen == 0){
540 err = startup(rs, e);
541 if(e!=nil && e->holdforclient)
546 queuesend(&dir[i], m);
553 fsysrespond(t, buf, err);
558 fsysversion(Fcall *t, uchar *buf, Fid*)
561 fsysrespond(t, buf, "version: message size too small");
564 if(t->msize < messagesize)
565 messagesize = t->msize;
566 t->msize = messagesize;
567 if(strncmp(t->version, "9P2000", 6) != 0){
568 fsysrespond(t, buf, "unrecognized 9P version");
571 t->version = "9P2000";
572 fsysrespond(t, buf, nil);
577 fsysauth(Fcall *t, uchar *buf, Fid*)
579 fsysrespond(t, buf, "plumber: authentication not required");
584 fsysattach(Fcall *t, uchar *buf, Fid *f)
588 if(strcmp(t->uname, user) != 0){
589 fsysrespond(&out, buf, Eperm);
598 memset(&out, 0, sizeof(Fcall));
603 fsysrespond(&out, buf, nil);
608 fsysflush(Fcall *t, uchar *buf, Fid*)
613 for(i=NQID; i<ndir; i++)
614 flushqueue(&dir[i], t->oldtag);
616 fsysrespond(t, buf, nil);
621 fsyswalk(Fcall *t, uchar *buf, Fid *f)
633 fsysrespond(t, buf, "clone of an open fid");
638 if(t->fid != t->newfid){
639 nf = newfid(t->newfid);
641 fsysrespond(t, buf, "clone to a busy fid");
657 for(i=0; i<t->nwname; i++){
658 if((q.type & QTDIR) == 0){
662 if(strcmp(t->wname[i], "..") == 0){
669 out.wqid[out.nwqid++] = q;
675 if(strcmp(t->wname[i], d->name) == 0){
688 if(err!=nil || out.nwqid<t->nwname){
691 }else if(out.nwqid == t->nwname){
696 fsysrespond(&out, buf, err);
701 fsysopen(Fcall *t, uchar *buf, Fid *f)
703 int m, clearrules, mode;
706 if(t->mode & OTRUNC){
707 if(f->qid.path != Qrules)
711 /* can't truncate anything, so just disregard */
712 mode = t->mode & ~(OTRUNC|OCEXEC);
713 /* can't execute or remove anything */
714 if(mode==OEXEC || (mode&ORCLOSE))
729 if(((f->dir->perm&~(DMDIR|DMAPPEND))&m) != m)
731 if(f->qid.path==Qrules && (mode==OWRITE || mode==ORDWR)){
733 if(rulesref.ref++ != 0){
736 fsysrespond(t, buf, Einuse);
751 f->nextopen = f->dir->fopen;
755 fsysrespond(t, buf, nil);
759 fsysrespond(t, buf, Eperm);
764 fsyscreate(Fcall *t, uchar *buf, Fid*)
766 fsysrespond(t, buf, Eperm);
771 fsysreadrules(Fcall *t, uchar *buf)
782 t->data = p+t->offset;
783 if(t->offset+t->count > n)
784 t->count = n-t->offset;
786 fsysrespond(t, buf, nil);
792 fsysread(Fcall *t, uchar *buf, Fid *f)
800 if(f->qid.path != Qdir){
801 if(f->qid.path == Qrules)
802 return fsysreadrules(t, buf);
804 if(f->qid.path < NQID){
805 fsysrespond(t, buf, "internal error: unknown read port");
809 queueread(f->dir, t, buf, f);
815 e = t->offset+t->count;
817 b = malloc(messagesize-IOHDRSZ);
819 fsysrespond(t, buf, Enomem);
824 d++; /* first entry is '.' */
825 for(i=0; d->name!=nil && i<e; i+=len){
826 len = dostat(d, b+n, messagesize-IOHDRSZ-n, clock);
835 fsysrespond(t, buf, nil);
841 fsyswrite(Fcall *t, uchar *buf, Fid *f)
849 switch((int)f->qid.path){
851 fsysrespond(t, buf, Eisdir);
855 fsysrespond(t, buf, writerules(t->data, t->count));
862 /* partial message already assembled */
863 f->writebuf = erealloc(f->writebuf, f->offset + t->count);
864 memmove(f->writebuf+f->offset, t->data, t->count);
866 count = f->offset+t->count;
868 m = plumbunpackpartial(data, count, &n);
874 fsysrespond(t, buf, Ebadmsg);
877 /* can read more... */
879 f->writebuf = emalloc(t->count);
880 memmove(f->writebuf, t->data, t->count);
882 /* else buffer has already been grown */
883 f->offset += t->count;
884 fsysrespond(t, buf, nil);
887 /* release partial buffer */
891 for(i=0; rules[i]; i++)
892 if((e=matchruleset(m, rules[i])) != nil){
893 dispose(t, buf, m, rules[i], e);
897 dispose(t, buf, m, nil, nil);
900 fsysrespond(t, buf, "no matching plumb rule");
903 fsysrespond(t, buf, "internal error: write to unknown file");
908 fsysstat(Fcall *t, uchar *buf, Fid *f)
910 t->stat = emalloc(messagesize-IOHDRSZ);
911 t->nstat = dostat(f->dir, t->stat, messagesize-IOHDRSZ, clock);
912 fsysrespond(t, buf, nil);
919 fsyswstat(Fcall *t, uchar *buf, Fid*)
921 fsysrespond(t, buf, Eperm);
926 fsysremove(Fcall *t, uchar *buf, Fid*)
928 fsysrespond(t, buf, Eperm);
933 fsysclunk(Fcall *t, uchar *buf, Fid *f)
942 if(d->qid==Qrules && (f->mode==OWRITE || f->mode==ORDWR)){
944 * just to be sure last rule is parsed; error messages will be lost, though,
945 * unless last write ended with a blank line
953 for(p=d->fopen; p; p=p->nextopen){
956 prev->nextopen = f->nextopen;
958 d->fopen = f->nextopen;
968 if(f->writebuf != nil){
973 fsysrespond(t, buf, nil);