15 typedef struct Dirtab Dirtab;
16 typedef struct Fid Fid;
17 typedef struct Holdq Holdq;
18 typedef struct Readreq Readreq;
19 typedef struct Sendreq Sendreq;
27 int nopen; /* #fids open on this port */
42 long offset; /* zeroed at beginning of each message, read or write */
43 char *writebuf; /* partial message written so far; offset tells how much */
58 int nfid; /* number of fids that should receive this message */
59 int nleft; /* number left that haven't received it */
60 Fid **fid; /* fid[nfid] */
62 char *pack; /* plumbpack()ed message */
63 int npack; /* length of pack */
73 struct /* needed because incref() doesn't return value */
91 static Dirtab dir[NDIR] =
93 { ".", QTDIR, Qdir, 0500|DMDIR },
94 { "rules", QTFILE, Qrules, 0600 },
95 { "send", QTFILE, Qsend, 0200 },
97 static int ndir = NQID;
100 #define clock plumbclock /* SunOS name clash */
102 static Fid *fids[Nhash];
103 static QLock readlock;
105 static int messagesize = 8192+IOHDRSZ; /* good start */
107 static void fsysproc(void*);
108 static void fsysrespond(Fcall*, uchar*, char*);
109 static Fid* newfid(int);
111 static Fcall* fsysflush(Fcall*, uchar*, Fid*);
112 static Fcall* fsysversion(Fcall*, uchar*, Fid*);
113 static Fcall* fsysauth(Fcall*, uchar*, Fid*);
114 static Fcall* fsysattach(Fcall*, uchar*, Fid*);
115 static Fcall* fsyswalk(Fcall*, uchar*, Fid*);
116 static Fcall* fsysopen(Fcall*, uchar*, Fid*);
117 static Fcall* fsyscreate(Fcall*, uchar*, Fid*);
118 static Fcall* fsysread(Fcall*, uchar*, Fid*);
119 static Fcall* fsyswrite(Fcall*, uchar*, Fid*);
120 static Fcall* fsysclunk(Fcall*, uchar*, Fid*);
121 static Fcall* fsysremove(Fcall*, uchar*, Fid*);
122 static Fcall* fsysstat(Fcall*, uchar*, Fid*);
123 static Fcall* fsyswstat(Fcall*, uchar*, Fid*);
125 Fcall* (*fcall[Tmax])(Fcall*, uchar*, Fid*);
130 fcall[Tflush] = fsysflush;
131 fcall[Tversion] = fsysversion;
132 fcall[Tauth] = fsysauth;
133 fcall[Tattach] = fsysattach;
134 fcall[Twalk] = fsyswalk;
135 fcall[Topen] = fsysopen;
136 fcall[Tcreate] = fsyscreate;
137 fcall[Tread] = fsysread;
138 fcall[Twrite] = fsyswrite;
139 fcall[Tclunk] = fsysclunk;
140 fcall[Tremove]= fsysremove;
141 fcall[Tstat] = fsysstat;
142 fcall[Twstat] = fsyswstat;
145 char Ebadfcall[] = "bad fcall type";
146 char Eperm[] = "permission denied";
147 char Enomem[] = "malloc failed for buffer";
148 char Enotdir[] = "not a directory";
149 char Enoexist[] = "plumb file does not exist";
150 char Eisdir[] = "file is a directory";
151 char Ebadmsg[] = "bad plumb message format";
152 char Enosuchport[] ="no such plumb port";
153 char Enoport[] = "couldn't find destination for message";
154 char Einuse[] = "file already open";
157 * Add new port. A no-op if port already exists or is the null string
166 for(i=NQID; i<ndir; i++)
167 if(strcmp(port, dir[i].name) == 0)
170 fprint(2, "plumb: too many ports; max %d\n", NDIR);
174 dir[i].name = estrdup(port);
178 ports = erealloc(ports, nports*sizeof(char*));
179 ports[nports-1] = dir[i].name;
193 fmtinstall('F', fcallfmt);
196 error("can't create pipe: %r");
197 /* 0 will be server end, 1 will be client end */
199 if(post9pservice(p[1], "plumb") < 0)
200 sysfatal("post9pservice plumb: %r");
202 threadcreate(fsysproc, nil, Stack);
217 buf = malloc(messagesize); /* avoid memset of emalloc */
219 error("malloc failed: %r");
221 n = threadread9pmsg(srvfd, buf, messagesize);
224 error("i/o error on server channel");
225 threadexitsall("unmounted");
227 if(readlock.head == nil) /* no other processes waiting to read; start one */
228 threadcreate(fsysproc, nil, Stack);
231 t = emalloc(sizeof(Fcall));
232 if(convM2S(buf, n, t) != n)
233 error("convert error in convM2S");
235 fprint(2, "<= %F\n", t);
236 if(fcall[t->type] == nil)
237 fsysrespond(t, buf, Ebadfcall);
239 if(t->type==Tversion || t->type==Tauth)
243 t = (*fcall[t->type])(t, buf, f);
249 fsysrespond(Fcall *t, uchar *buf, char *err)
259 buf = emalloc(messagesize);
260 n = convS2M(t, buf, messagesize);
262 error("convert error in convS2M");
263 if(write(srvfd, buf, n) != n)
264 error("write error in respond");
266 fprint(2, "=> %F\n", t);
278 fh = &fids[fid&(Nhash-1)];
279 for(f=*fh; f; f=f->next)
282 else if(ff==nil && !f->busy)
289 f = emalloc(sizeof *f);
299 dostat(Dirtab *dir, uchar *buf, uint nbuf, uint clock)
303 d.qid.type = dir->type;
304 d.qid.path = dir->qid;
307 d.length = 0; /* would be nice to do better */
314 return convD2M(&d, buf, nbuf);
318 queuesend(Dirtab *d, Plumbmsg *m)
324 s = emalloc(sizeof(Sendreq));
327 s->fid = emalloc(s->nfid*sizeof(Fid*));
329 /* build array of fids open on this channel */
330 for(f=d->fopen; f!=nil; f=f->nextopen)
334 /* link to end of queue; drainqueue() searches in sender order so this implements a FIFO */
335 for(t=d->sendq; t!=nil; t=t->next)
345 queueread(Dirtab *d, Fcall *t, uchar *buf, Fid *f)
349 r = emalloc(sizeof(Readreq));
358 drainqueue(Dirtab *d)
360 Readreq *r, *nextr, *prevr;
361 Sendreq *s, *nexts, *prevs;
365 for(s=d->sendq; s!=nil; s=nexts){
367 for(i=0; i<s->nfid; i++){
369 for(r=d->readq; r!=nil; r=nextr){
371 if(r->fid == s->fid[i]){
372 /* pack the message if necessary */
374 s->pack = plumbpack(s->msg, &s->npack);
375 /* exchange the stuff... */
376 r->fcall->data = s->pack+r->fid->offset;
377 n = s->npack - r->fid->offset;
378 if(n > messagesize-IOHDRSZ)
379 n = messagesize-IOHDRSZ;
380 if(n > r->fcall->count)
383 fsysrespond(r->fcall, r->buf, nil);
385 if(r->fid->offset >= s->npack){
386 /* message transferred; delete this fid from send queue */
391 /* delete read request from queue */
393 prevr->next = r->next;
403 /* if no fids left, delete this send from queue */
409 prevs->next = s->next;
418 /* can't flush a send because they are always answered synchronously */
420 flushqueue(Dirtab *d, int oldtag)
425 for(r=d->readq; r!=nil; r=r->next){
426 if(oldtag == r->fcall->tag){
427 /* delete read request from queue */
429 prevr->next = r->next;
441 /* remove messages awaiting delivery to now-closing fid */
443 removesenders(Dirtab *d, Fid *fid)
445 Sendreq *s, *nexts, *prevs;
449 for(s=d->sendq; s!=nil; s=nexts){
451 for(i=0; i<s->nfid; i++)
452 if(fid == s->fid[i]){
453 /* delete this fid from send queue */
458 /* if no fids left, delete this send from queue */
464 prevs->next = s->next;
474 hold(Plumbmsg *m, Dirtab *d)
478 h = emalloc(sizeof(Holdq));
480 /* add to end of queue */
484 for(q=d->holdq; q->next!=nil; q=q->next)
495 while(d->holdq != nil){
498 queuesend(d, h->msg);
499 /* no need to drain queue because we know no-one is reading yet */
505 dispose(Fcall *t, uchar *buf, Plumbmsg *m, Ruleset *rs, Exec *e)
512 if(m->dst==nil || m->dst[0]=='\0'){
515 err = startup(rs, e);
518 for(i=NQID; i<ndir; i++)
519 if(strcmp(m->dst, dir[i].name) == 0){
520 if(dir[i].nopen == 0){
521 err = startup(rs, e);
522 if(e!=nil && e->holdforclient)
527 queuesend(&dir[i], m);
534 fsysrespond(t, buf, err);
539 fsysversion(Fcall *t, uchar *buf, Fid *fid)
544 fsysrespond(t, buf, "version: message size too small");
547 if(t->msize < messagesize)
548 messagesize = t->msize;
549 t->msize = messagesize;
550 if(strncmp(t->version, "9P2000", 6) != 0){
551 fsysrespond(t, buf, "unrecognized 9P version");
554 t->version = "9P2000";
555 fsysrespond(t, buf, nil);
560 fsysauth(Fcall *t, uchar *buf, Fid *fid)
563 fsysrespond(t, buf, "plumber: authentication not required");
568 fsysattach(Fcall *t, uchar *buf, Fid *f)
572 if(strcmp(t->uname, user) != 0){
573 fsysrespond(&out, buf, Eperm);
582 memset(&out, 0, sizeof(Fcall));
587 fsysrespond(&out, buf, nil);
592 fsysflush(Fcall *t, uchar *buf, Fid *fid)
598 for(i=NQID; i<ndir; i++)
599 flushqueue(&dir[i], t->oldtag);
601 fsysrespond(t, buf, nil);
606 fsyswalk(Fcall *t, uchar *buf, Fid *f)
618 fsysrespond(t, buf, "clone of an open fid");
623 if(t->fid != t->newfid){
624 nf = newfid(t->newfid);
626 fsysrespond(t, buf, "clone to a busy fid");
642 for(i=0; i<t->nwname; i++){
643 if((q.type & QTDIR) == 0){
647 if(strcmp(t->wname[i], "..") == 0){
654 out.wqid[out.nwqid++] = q;
660 if(strcmp(t->wname[i], d->name) == 0){
673 if(err!=nil || out.nwqid<t->nwname){
676 }else if(out.nwqid == t->nwname){
681 fsysrespond(&out, buf, err);
686 fsysopen(Fcall *t, uchar *buf, Fid *f)
688 int m, clearrules, mode;
691 if(t->mode & OTRUNC){
692 if(f->qid.path != Qrules)
696 /* can't truncate anything, so just disregard */
697 mode = t->mode & ~(OTRUNC|OCEXEC);
698 /* can't execute or remove anything */
699 if(mode==OEXEC || (mode&ORCLOSE))
714 if(((f->dir->perm&~(DMDIR|DMAPPEND))&m) != m)
716 if(f->qid.path==Qrules && (mode==OWRITE || mode==ORDWR)){
718 if(rulesref.ref++ != 0){
720 unlock(&rulesref.lk);
721 fsysrespond(t, buf, Einuse);
724 unlock(&rulesref.lk);
736 f->nextopen = f->dir->fopen;
740 fsysrespond(t, buf, nil);
744 fsysrespond(t, buf, Eperm);
749 fsyscreate(Fcall *t, uchar *buf, Fid *fid)
752 fsysrespond(t, buf, Eperm);
757 fsysreadrules(Fcall *t, uchar *buf)
768 t->data = p+t->offset;
769 if(t->offset+t->count > n)
770 t->count = n-t->offset;
772 fsysrespond(t, buf, nil);
778 fsysread(Fcall *t, uchar *buf, Fid *f)
786 if(f->qid.path != Qdir){
787 if(f->qid.path == Qrules)
788 return fsysreadrules(t, buf);
790 if(f->qid.path < NQID){
791 fsysrespond(t, buf, "internal error: unknown read port");
795 queueread(f->dir, t, buf, f);
801 e = t->offset+t->count;
803 b = malloc(messagesize-IOHDRSZ);
805 fsysrespond(t, buf, Enomem);
810 d++; /* first entry is '.' */
811 for(i=0; d->name!=nil && i<e; i+=len){
812 len = dostat(d, b+n, messagesize-IOHDRSZ-n, clock);
821 fsysrespond(t, buf, nil);
827 fsyswrite(Fcall *t, uchar *buf, Fid *f)
835 switch((int)f->qid.path){
837 fsysrespond(t, buf, Eisdir);
841 fsysrespond(t, buf, writerules(t->data, t->count));
848 /* partial message already assembled */
849 f->writebuf = erealloc(f->writebuf, f->offset + t->count);
850 memmove(f->writebuf+f->offset, t->data, t->count);
852 count = f->offset+t->count;
854 m = plumbunpackpartial(data, count, &n);
860 fsysrespond(t, buf, Ebadmsg);
863 /* can read more... */
865 f->writebuf = emalloc(t->count);
866 memmove(f->writebuf, t->data, t->count);
868 /* else buffer has already been grown */
869 f->offset += t->count;
870 fsysrespond(t, buf, nil);
873 /* release partial buffer */
877 for(i=0; rules[i]; i++)
878 if((e=matchruleset(m, rules[i])) != nil){
879 dispose(t, buf, m, rules[i], e);
883 dispose(t, buf, m, nil, nil);
886 fsysrespond(t, buf, "no matching plumb rule");
889 fsysrespond(t, buf, "internal error: write to unknown file");
894 fsysstat(Fcall *t, uchar *buf, Fid *f)
896 t->stat = emalloc(messagesize-IOHDRSZ);
897 t->nstat = dostat(f->dir, t->stat, messagesize-IOHDRSZ, clock);
898 fsysrespond(t, buf, nil);
905 fsyswstat(Fcall *t, uchar *buf, Fid *fid)
908 fsysrespond(t, buf, Eperm);
913 fsysremove(Fcall *t, uchar *buf, Fid *fid)
916 fsysrespond(t, buf, Eperm);
921 fsysclunk(Fcall *t, uchar *buf, Fid *f)
930 if(d->qid==Qrules && (f->mode==OWRITE || f->mode==ORDWR)){
932 * just to be sure last rule is parsed; error messages will be lost, though,
933 * unless last write ended with a blank line
938 unlock(&rulesref.lk);
941 for(p=d->fopen; p; p=p->nextopen){
944 prev->nextopen = f->nextopen;
946 d->fopen = f->nextopen;
956 if(f->writebuf != nil){
961 fsysrespond(t, buf, nil);