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;
189 startfsys(int foreground)
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", nil) < 0)
200 sysfatal("post9pservice plumb: %r");
205 proccreate(fsysproc, nil, Stack);
220 buf = malloc(messagesize); /* avoid memset of emalloc */
222 error("malloc failed: %r");
224 n = read9pmsg(srvfd, buf, messagesize);
227 error("i/o error on server channel");
228 threadexitsall("unmounted");
231 * can give false positive (create an extra fsysproc) once in a while,
232 * but no false negatives, so good enough. once we have one extra
233 * we'll never have more.
235 if(readlock.waiting.head == nil) /* no other processes waiting to read; start one */
236 proccreate(fsysproc, nil, Stack);
239 t = emalloc(sizeof(Fcall));
240 if(convM2S(buf, n, t) != n)
241 error("convert error in convM2S");
243 fprint(2, "<= %F\n", t);
244 if(fcall[t->type] == 0)
245 fsysrespond(t, buf, Ebadfcall);
247 if(t->type==Tversion || t->type==Tauth)
251 t = (*fcall[t->type])(t, buf, f);
257 fsysrespond(Fcall *t, uchar *buf, char *err)
267 buf = emalloc(messagesize);
268 n = convS2M(t, buf, messagesize);
270 error("convert error in convS2M");
271 if(write(srvfd, buf, n) != n)
272 error("write error in respond");
274 fprint(2, "=> %F\n", t);
286 fh = &fids[fid&(Nhash-1)];
287 for(f=*fh; f; f=f->next)
290 else if(ff==nil && !f->busy)
297 f = emalloc(sizeof *f);
307 dostat(Dirtab *dir, uchar *buf, uint nbuf, uint clock)
311 d.qid.type = dir->type;
312 d.qid.path = dir->qid;
315 d.length = 0; /* would be nice to do better */
322 return convD2M(&d, buf, nbuf);
326 queuesend(Dirtab *d, Plumbmsg *m)
332 s = emalloc(sizeof(Sendreq));
335 s->fid = emalloc(s->nfid*sizeof(Fid*));
337 /* build array of fids open on this channel */
338 for(f=d->fopen; f!=nil; f=f->nextopen)
342 /* link to end of queue; drainqueue() searches in sender order so this implements a FIFO */
343 for(t=d->sendq; t!=nil; t=t->next)
353 queueread(Dirtab *d, Fcall *t, uchar *buf, Fid *f)
357 r = emalloc(sizeof(Readreq));
366 drainqueue(Dirtab *d)
368 Readreq *r, *nextr, *prevr;
369 Sendreq *s, *nexts, *prevs;
373 for(s=d->sendq; s!=nil; s=nexts){
375 for(i=0; i<s->nfid; i++){
377 for(r=d->readq; r!=nil; r=nextr){
379 if(r->fid == s->fid[i]){
380 /* pack the message if necessary */
382 s->pack = plumbpack(s->msg, &s->npack);
383 /* exchange the stuff... */
384 r->fcall->data = s->pack+r->fid->offset;
385 n = s->npack - r->fid->offset;
386 if(n > messagesize-IOHDRSZ)
387 n = messagesize-IOHDRSZ;
388 if(n > r->fcall->count)
391 fsysrespond(r->fcall, r->buf, nil);
393 if(r->fid->offset >= s->npack){
394 /* message transferred; delete this fid from send queue */
399 /* delete read request from queue */
401 prevr->next = r->next;
411 /* if no fids left, delete this send from queue */
417 prevs->next = s->next;
426 /* can't flush a send because they are always answered synchronously */
428 flushqueue(Dirtab *d, int oldtag)
433 for(r=d->readq; r!=nil; r=r->next){
434 if(oldtag == r->fcall->tag){
435 /* delete read request from queue */
437 prevr->next = r->next;
449 /* remove messages awaiting delivery to now-closing fid */
451 removesenders(Dirtab *d, Fid *fid)
453 Sendreq *s, *nexts, *prevs;
457 for(s=d->sendq; s!=nil; s=nexts){
459 for(i=0; i<s->nfid; i++)
460 if(fid == s->fid[i]){
461 /* delete this fid from send queue */
466 /* if no fids left, delete this send from queue */
472 prevs->next = s->next;
482 hold(Plumbmsg *m, Dirtab *d)
486 h = emalloc(sizeof(Holdq));
488 /* add to end of queue */
492 for(q=d->holdq; q->next!=nil; q=q->next)
503 while(d->holdq != nil){
506 queuesend(d, h->msg);
507 /* no need to drain queue because we know no-one is reading yet */
513 dispose(Fcall *t, uchar *buf, Plumbmsg *m, Ruleset *rs, Exec *e)
520 if(m->dst==nil || m->dst[0]=='\0'){
523 err = startup(rs, e);
526 for(i=NQID; i<ndir; i++)
527 if(strcmp(m->dst, dir[i].name) == 0){
528 if(dir[i].nopen == 0){
529 err = startup(rs, e);
530 if(e!=nil && e->holdforclient)
535 queuesend(&dir[i], m);
542 fsysrespond(t, buf, err);
547 fsysversion(Fcall *t, uchar *buf, Fid *fid)
552 fsysrespond(t, buf, "version: message size too small");
555 if(t->msize < messagesize)
556 messagesize = t->msize;
557 t->msize = messagesize;
558 if(strncmp(t->version, "9P2000", 6) != 0){
559 fsysrespond(t, buf, "unrecognized 9P version");
562 t->version = "9P2000";
563 fsysrespond(t, buf, nil);
568 fsysauth(Fcall *t, uchar *buf, Fid *fid)
571 fsysrespond(t, buf, "plumber: authentication not required");
576 fsysattach(Fcall *t, uchar *buf, Fid *f)
581 if(strcmp(t->uname, user) != 0){
582 fsysrespond(&out, buf, Eperm);
592 memset(&out, 0, sizeof(Fcall));
597 fsysrespond(&out, buf, nil);
602 fsysflush(Fcall *t, uchar *buf, Fid *fid)
608 for(i=NQID; i<ndir; i++)
609 flushqueue(&dir[i], t->oldtag);
611 fsysrespond(t, buf, nil);
616 fsyswalk(Fcall *t, uchar *buf, Fid *f)
628 fsysrespond(t, buf, "clone of an open fid");
633 if(t->fid != t->newfid){
634 nf = newfid(t->newfid);
636 fsysrespond(t, buf, "clone to a busy fid");
652 for(i=0; i<t->nwname; i++){
653 if((q.type & QTDIR) == 0){
657 if(strcmp(t->wname[i], "..") == 0){
664 out.wqid[out.nwqid++] = q;
670 if(strcmp(t->wname[i], d->name) == 0){
683 if(err!=nil || out.nwqid<t->nwname){
686 }else if(out.nwqid == t->nwname){
691 fsysrespond(&out, buf, err);
696 fsysopen(Fcall *t, uchar *buf, Fid *f)
698 int m, clearrules, mode;
701 if(t->mode & OTRUNC){
702 if(f->qid.path != Qrules)
706 /* can't truncate anything, so just disregard */
707 mode = t->mode & ~(OTRUNC|OCEXEC);
708 /* can't execute or remove anything */
709 if(mode==OEXEC || (mode&ORCLOSE))
724 if(((f->dir->perm&~(DMDIR|DMAPPEND))&m) != m)
726 if(f->qid.path==Qrules && (mode==OWRITE || mode==ORDWR)){
728 if(rulesref.ref++ != 0){
730 unlock(&rulesref.lk);
731 fsysrespond(t, buf, Einuse);
734 unlock(&rulesref.lk);
746 f->nextopen = f->dir->fopen;
750 fsysrespond(t, buf, nil);
754 fsysrespond(t, buf, Eperm);
759 fsyscreate(Fcall *t, uchar *buf, Fid *fid)
762 fsysrespond(t, buf, Eperm);
767 fsysreadrules(Fcall *t, uchar *buf)
778 t->data = p+t->offset;
779 if(t->offset+t->count > n)
780 t->count = n-t->offset;
782 fsysrespond(t, buf, nil);
788 fsysread(Fcall *t, uchar *buf, Fid *f)
796 if(f->qid.path != Qdir){
797 if(f->qid.path == Qrules)
798 return fsysreadrules(t, buf);
800 if(f->qid.path < NQID){
801 fsysrespond(t, buf, "internal error: unknown read port");
805 queueread(f->dir, t, buf, f);
811 e = t->offset+t->count;
813 b = malloc(messagesize-IOHDRSZ);
815 fsysrespond(t, buf, Enomem);
820 d++; /* first entry is '.' */
821 for(i=0; d->name!=nil && i<e; i+=len){
822 len = dostat(d, b+n, messagesize-IOHDRSZ-n, clock);
831 fsysrespond(t, buf, nil);
837 fsyswrite(Fcall *t, uchar *buf, Fid *f)
845 switch((int)f->qid.path){
847 fsysrespond(t, buf, Eisdir);
851 fsysrespond(t, buf, writerules(t->data, t->count));
858 /* partial message already assembled */
859 f->writebuf = erealloc(f->writebuf, f->offset + t->count);
860 memmove(f->writebuf+f->offset, t->data, t->count);
862 count = f->offset+t->count;
864 m = plumbunpackpartial(data, count, &n);
870 fsysrespond(t, buf, Ebadmsg);
873 /* can read more... */
875 f->writebuf = emalloc(t->count);
876 memmove(f->writebuf, t->data, t->count);
878 /* else buffer has already been grown */
879 f->offset += t->count;
880 fsysrespond(t, buf, nil);
883 /* release partial buffer */
887 for(i=0; rules[i]; i++)
888 if((e=matchruleset(m, rules[i])) != nil){
889 dispose(t, buf, m, rules[i], e);
893 dispose(t, buf, m, nil, nil);
896 fsysrespond(t, buf, "no matching plumb rule");
899 fsysrespond(t, buf, "internal error: write to unknown file");
904 fsysstat(Fcall *t, uchar *buf, Fid *f)
906 t->stat = emalloc(messagesize-IOHDRSZ);
907 t->nstat = dostat(f->dir, t->stat, messagesize-IOHDRSZ, clock);
908 fsysrespond(t, buf, nil);
915 fsyswstat(Fcall *t, uchar *buf, Fid *fid)
918 fsysrespond(t, buf, Eperm);
923 fsysremove(Fcall *t, uchar *buf, Fid *fid)
926 fsysrespond(t, buf, Eperm);
931 fsysclunk(Fcall *t, uchar *buf, Fid *f)
940 if(d->qid==Qrules && (f->mode==OWRITE || f->mode==ORDWR)){
942 * just to be sure last rule is parsed; error messages will be lost, though,
943 * unless last write ended with a blank line
948 unlock(&rulesref.lk);
951 for(p=d->fopen; p; p=p->nextopen){
954 prev->nextopen = f->nextopen;
956 d->fopen = f->nextopen;
966 if(f->writebuf != nil){
971 fsysrespond(t, buf, nil);