1 b8c14089 2003-11-23 devnull #include <u.h>
2 b8c14089 2003-11-23 devnull #include <libc.h>
3 b8c14089 2003-11-23 devnull #include <bio.h>
4 b8c14089 2003-11-23 devnull #include <regexp.h>
5 b8c14089 2003-11-23 devnull #include <thread.h>
6 b8c14089 2003-11-23 devnull #include <fcall.h>
7 b8c14089 2003-11-23 devnull #include <plumb.h>
8 b8c14089 2003-11-23 devnull #include "plumber.h"
12 32f69c36 2003-12-11 devnull Stack = 32*1024
15 b8c14089 2003-11-23 devnull typedef struct Dirtab Dirtab;
16 b8c14089 2003-11-23 devnull typedef struct Fid Fid;
17 b8c14089 2003-11-23 devnull typedef struct Holdq Holdq;
18 b8c14089 2003-11-23 devnull typedef struct Readreq Readreq;
19 b8c14089 2003-11-23 devnull typedef struct Sendreq Sendreq;
21 b8c14089 2003-11-23 devnull struct Dirtab
23 b8c14089 2003-11-23 devnull char *name;
24 b8c14089 2003-11-23 devnull uchar type;
25 b8c14089 2003-11-23 devnull uint qid;
26 b8c14089 2003-11-23 devnull uint perm;
27 b8c14089 2003-11-23 devnull int nopen; /* #fids open on this port */
28 b8c14089 2003-11-23 devnull Fid *fopen;
29 b8c14089 2003-11-23 devnull Holdq *holdq;
30 b8c14089 2003-11-23 devnull Readreq *readq;
31 b8c14089 2003-11-23 devnull Sendreq *sendq;
34 b8c14089 2003-11-23 devnull struct Fid
37 b8c14089 2003-11-23 devnull int busy;
38 b8c14089 2003-11-23 devnull int open;
39 b8c14089 2003-11-23 devnull int mode;
41 b8c14089 2003-11-23 devnull Dirtab *dir;
42 b8c14089 2003-11-23 devnull long offset; /* zeroed at beginning of each message, read or write */
43 b8c14089 2003-11-23 devnull char *writebuf; /* partial message written so far; offset tells how much */
44 b8c14089 2003-11-23 devnull Fid *next;
45 b8c14089 2003-11-23 devnull Fid *nextopen;
48 b8c14089 2003-11-23 devnull struct Readreq
50 b8c14089 2003-11-23 devnull Fid *fid;
51 b8c14089 2003-11-23 devnull Fcall *fcall;
52 b8c14089 2003-11-23 devnull uchar *buf;
53 b8c14089 2003-11-23 devnull Readreq *next;
56 b8c14089 2003-11-23 devnull struct Sendreq
58 b8c14089 2003-11-23 devnull int nfid; /* number of fids that should receive this message */
59 b8c14089 2003-11-23 devnull int nleft; /* number left that haven't received it */
60 b8c14089 2003-11-23 devnull Fid **fid; /* fid[nfid] */
61 b8c14089 2003-11-23 devnull Plumbmsg *msg;
62 b8c14089 2003-11-23 devnull char *pack; /* plumbpack()ed message */
63 b8c14089 2003-11-23 devnull int npack; /* length of pack */
64 b8c14089 2003-11-23 devnull Sendreq *next;
67 b8c14089 2003-11-23 devnull struct Holdq
69 b8c14089 2003-11-23 devnull Plumbmsg *msg;
70 b8c14089 2003-11-23 devnull Holdq *next;
73 b8c14089 2003-11-23 devnull struct /* needed because incref() doesn't return value */
77 b8c14089 2003-11-23 devnull } rulesref;
81 b8c14089 2003-11-23 devnull NDIR = 50,
82 b8c14089 2003-11-23 devnull Nhash = 16,
84 b8c14089 2003-11-23 devnull Qdir = 0,
85 b8c14089 2003-11-23 devnull Qrules = 1,
86 b8c14089 2003-11-23 devnull Qsend = 2,
87 b8c14089 2003-11-23 devnull Qport = 3,
88 b8c14089 2003-11-23 devnull NQID = Qport
91 b8c14089 2003-11-23 devnull static Dirtab dir[NDIR] =
93 b8c14089 2003-11-23 devnull { ".", QTDIR, Qdir, 0500|DMDIR },
94 b8c14089 2003-11-23 devnull { "rules", QTFILE, Qrules, 0600 },
95 cbeb0b26 2006-04-01 devnull { "send", QTFILE, Qsend, 0200 }
97 b8c14089 2003-11-23 devnull static int ndir = NQID;
99 b8c14089 2003-11-23 devnull static int srvfd;
100 8ad51794 2004-03-25 devnull #define clock plumbclock /* SunOS name clash */
101 b8c14089 2003-11-23 devnull static int clock;
102 b8c14089 2003-11-23 devnull static Fid *fids[Nhash];
103 b8c14089 2003-11-23 devnull static QLock readlock;
104 b8c14089 2003-11-23 devnull static QLock queue;
105 b8c14089 2003-11-23 devnull static int messagesize = 8192+IOHDRSZ; /* good start */
107 b8c14089 2003-11-23 devnull static void fsysproc(void*);
108 b8c14089 2003-11-23 devnull static void fsysrespond(Fcall*, uchar*, char*);
109 b8c14089 2003-11-23 devnull static Fid* newfid(int);
111 b8c14089 2003-11-23 devnull static Fcall* fsysflush(Fcall*, uchar*, Fid*);
112 b8c14089 2003-11-23 devnull static Fcall* fsysversion(Fcall*, uchar*, Fid*);
113 b8c14089 2003-11-23 devnull static Fcall* fsysauth(Fcall*, uchar*, Fid*);
114 b8c14089 2003-11-23 devnull static Fcall* fsysattach(Fcall*, uchar*, Fid*);
115 b8c14089 2003-11-23 devnull static Fcall* fsyswalk(Fcall*, uchar*, Fid*);
116 b8c14089 2003-11-23 devnull static Fcall* fsysopen(Fcall*, uchar*, Fid*);
117 b8c14089 2003-11-23 devnull static Fcall* fsyscreate(Fcall*, uchar*, Fid*);
118 b8c14089 2003-11-23 devnull static Fcall* fsysread(Fcall*, uchar*, Fid*);
119 b8c14089 2003-11-23 devnull static Fcall* fsyswrite(Fcall*, uchar*, Fid*);
120 b8c14089 2003-11-23 devnull static Fcall* fsysclunk(Fcall*, uchar*, Fid*);
121 b8c14089 2003-11-23 devnull static Fcall* fsysremove(Fcall*, uchar*, Fid*);
122 b8c14089 2003-11-23 devnull static Fcall* fsysstat(Fcall*, uchar*, Fid*);
123 b8c14089 2003-11-23 devnull static Fcall* fsyswstat(Fcall*, uchar*, Fid*);
125 be22ae2d 2004-03-26 devnull Fcall* (*fcall[Tmax])(Fcall*, uchar*, Fid*);
127 be22ae2d 2004-03-26 devnull static void
128 be22ae2d 2004-03-26 devnull initfcall(void)
130 be22ae2d 2004-03-26 devnull fcall[Tflush] = fsysflush;
131 be22ae2d 2004-03-26 devnull fcall[Tversion] = fsysversion;
132 be22ae2d 2004-03-26 devnull fcall[Tauth] = fsysauth;
133 be22ae2d 2004-03-26 devnull fcall[Tattach] = fsysattach;
134 be22ae2d 2004-03-26 devnull fcall[Twalk] = fsyswalk;
135 be22ae2d 2004-03-26 devnull fcall[Topen] = fsysopen;
136 be22ae2d 2004-03-26 devnull fcall[Tcreate] = fsyscreate;
137 be22ae2d 2004-03-26 devnull fcall[Tread] = fsysread;
138 be22ae2d 2004-03-26 devnull fcall[Twrite] = fsyswrite;
139 be22ae2d 2004-03-26 devnull fcall[Tclunk] = fsysclunk;
140 be22ae2d 2004-03-26 devnull fcall[Tremove]= fsysremove;
141 be22ae2d 2004-03-26 devnull fcall[Tstat] = fsysstat;
142 be22ae2d 2004-03-26 devnull fcall[Twstat] = fsyswstat;
145 b8c14089 2003-11-23 devnull char Ebadfcall[] = "bad fcall type";
146 b8c14089 2003-11-23 devnull char Eperm[] = "permission denied";
147 b8c14089 2003-11-23 devnull char Enomem[] = "malloc failed for buffer";
148 b8c14089 2003-11-23 devnull char Enotdir[] = "not a directory";
149 b8c14089 2003-11-23 devnull char Enoexist[] = "plumb file does not exist";
150 b8c14089 2003-11-23 devnull char Eisdir[] = "file is a directory";
151 b8c14089 2003-11-23 devnull char Ebadmsg[] = "bad plumb message format";
152 b8c14089 2003-11-23 devnull char Enosuchport[] ="no such plumb port";
153 b8c14089 2003-11-23 devnull char Enoport[] = "couldn't find destination for message";
154 b8c14089 2003-11-23 devnull char Einuse[] = "file already open";
157 b8c14089 2003-11-23 devnull * Add new port. A no-op if port already exists or is the null string
160 b8c14089 2003-11-23 devnull addport(char *port)
164 b8c14089 2003-11-23 devnull if(port == nil)
166 b8c14089 2003-11-23 devnull for(i=NQID; i<ndir; i++)
167 b8c14089 2003-11-23 devnull if(strcmp(port, dir[i].name) == 0)
169 b8c14089 2003-11-23 devnull if(i == NDIR){
170 b8c14089 2003-11-23 devnull fprint(2, "plumb: too many ports; max %d\n", NDIR);
174 b8c14089 2003-11-23 devnull dir[i].name = estrdup(port);
175 b8c14089 2003-11-23 devnull dir[i].qid = i;
176 b8c14089 2003-11-23 devnull dir[i].perm = 0400;
177 b8c14089 2003-11-23 devnull nports++;
178 b8c14089 2003-11-23 devnull ports = erealloc(ports, nports*sizeof(char*));
179 b8c14089 2003-11-23 devnull ports[nports-1] = dir[i].name;
182 b8c14089 2003-11-23 devnull static ulong
183 b8c14089 2003-11-23 devnull getclock(void)
185 32f69c36 2003-12-11 devnull return time(0);
189 b8c14089 2003-11-23 devnull startfsys(void)
191 32f69c36 2003-12-11 devnull int p[2];
193 b8c14089 2003-11-23 devnull fmtinstall('F', fcallfmt);
194 b8c14089 2003-11-23 devnull clock = getclock();
195 b8c14089 2003-11-23 devnull if(pipe(p) < 0)
196 b8c14089 2003-11-23 devnull error("can't create pipe: %r");
197 b8c14089 2003-11-23 devnull /* 0 will be server end, 1 will be client end */
198 b8c14089 2003-11-23 devnull srvfd = p[0];
199 7db5826d 2008-04-17 rsc if(post9pservice(p[1], "plumb", nil) < 0)
200 32f69c36 2003-12-11 devnull sysfatal("post9pservice plumb: %r");
201 b8c14089 2003-11-23 devnull close(p[1]);
202 f4b26b64 2005-01-04 devnull proccreate(fsysproc, nil, Stack);
205 b8c14089 2003-11-23 devnull static void
206 32f69c36 2003-12-11 devnull fsysproc(void *v)
209 b8c14089 2003-11-23 devnull Fcall *t;
211 b8c14089 2003-11-23 devnull uchar *buf;
213 32f69c36 2003-12-11 devnull USED(v);
214 be22ae2d 2004-03-26 devnull initfcall();
215 b8c14089 2003-11-23 devnull t = nil;
216 b8c14089 2003-11-23 devnull for(;;){
217 b8c14089 2003-11-23 devnull buf = malloc(messagesize); /* avoid memset of emalloc */
218 b8c14089 2003-11-23 devnull if(buf == nil)
219 b8c14089 2003-11-23 devnull error("malloc failed: %r");
220 b8c14089 2003-11-23 devnull qlock(&readlock);
221 f9979097 2004-12-26 devnull n = read9pmsg(srvfd, buf, messagesize);
222 b8c14089 2003-11-23 devnull if(n <= 0){
223 b8c14089 2003-11-23 devnull if(n < 0)
224 b8c14089 2003-11-23 devnull error("i/o error on server channel");
225 b8c14089 2003-11-23 devnull threadexitsall("unmounted");
228 f9979097 2004-12-26 devnull * can give false positive (create an extra fsysproc) once in a while,
229 f9979097 2004-12-26 devnull * but no false negatives, so good enough. once we have one extra
230 f9979097 2004-12-26 devnull * we'll never have more.
232 f9979097 2004-12-26 devnull if(readlock.waiting.head == nil) /* no other processes waiting to read; start one */
233 f9979097 2004-12-26 devnull proccreate(fsysproc, nil, Stack);
234 b8c14089 2003-11-23 devnull qunlock(&readlock);
235 b8c14089 2003-11-23 devnull if(t == nil)
236 b8c14089 2003-11-23 devnull t = emalloc(sizeof(Fcall));
237 b8c14089 2003-11-23 devnull if(convM2S(buf, n, t) != n)
238 b8c14089 2003-11-23 devnull error("convert error in convM2S");
239 32f69c36 2003-12-11 devnull if(debug)
240 b8c14089 2003-11-23 devnull fprint(2, "<= %F\n", t);
241 a8763864 2004-07-09 devnull if(fcall[t->type] == 0)
242 b8c14089 2003-11-23 devnull fsysrespond(t, buf, Ebadfcall);
244 b8c14089 2003-11-23 devnull if(t->type==Tversion || t->type==Tauth)
245 b8c14089 2003-11-23 devnull f = nil;
247 b8c14089 2003-11-23 devnull f = newfid(t->fid);
248 b8c14089 2003-11-23 devnull t = (*fcall[t->type])(t, buf, f);
253 b8c14089 2003-11-23 devnull static void
254 b8c14089 2003-11-23 devnull fsysrespond(Fcall *t, uchar *buf, char *err)
258 b8c14089 2003-11-23 devnull if(err){
259 b8c14089 2003-11-23 devnull t->type = Rerror;
260 b8c14089 2003-11-23 devnull t->ename = err;
262 b8c14089 2003-11-23 devnull t->type++;
263 b8c14089 2003-11-23 devnull if(buf == nil)
264 b8c14089 2003-11-23 devnull buf = emalloc(messagesize);
265 b8c14089 2003-11-23 devnull n = convS2M(t, buf, messagesize);
266 b8c14089 2003-11-23 devnull if(n < 0)
267 b8c14089 2003-11-23 devnull error("convert error in convS2M");
268 b8c14089 2003-11-23 devnull if(write(srvfd, buf, n) != n)
269 b8c14089 2003-11-23 devnull error("write error in respond");
270 32f69c36 2003-12-11 devnull if(debug)
271 b8c14089 2003-11-23 devnull fprint(2, "=> %F\n", t);
272 b8c14089 2003-11-23 devnull free(buf);
277 b8c14089 2003-11-23 devnull newfid(int fid)
279 b8c14089 2003-11-23 devnull Fid *f, *ff, **fh;
281 b8c14089 2003-11-23 devnull qlock(&queue);
282 b8c14089 2003-11-23 devnull ff = nil;
283 b8c14089 2003-11-23 devnull fh = &fids[fid&(Nhash-1)];
284 b8c14089 2003-11-23 devnull for(f=*fh; f; f=f->next)
285 b8c14089 2003-11-23 devnull if(f->fid == fid)
286 b8c14089 2003-11-23 devnull goto Return;
287 b8c14089 2003-11-23 devnull else if(ff==nil && !f->busy)
290 b8c14089 2003-11-23 devnull ff->fid = fid;
292 b8c14089 2003-11-23 devnull goto Return;
294 b8c14089 2003-11-23 devnull f = emalloc(sizeof *f);
295 b8c14089 2003-11-23 devnull f->fid = fid;
296 b8c14089 2003-11-23 devnull f->next = *fh;
297 b8c14089 2003-11-23 devnull *fh = f;
299 b8c14089 2003-11-23 devnull qunlock(&queue);
300 b8c14089 2003-11-23 devnull return f;
303 b8c14089 2003-11-23 devnull static uint
304 b8c14089 2003-11-23 devnull dostat(Dirtab *dir, uchar *buf, uint nbuf, uint clock)
308 b8c14089 2003-11-23 devnull d.qid.type = dir->type;
309 b8c14089 2003-11-23 devnull d.qid.path = dir->qid;
310 b8c14089 2003-11-23 devnull d.qid.vers = 0;
311 b8c14089 2003-11-23 devnull d.mode = dir->perm;
312 b8c14089 2003-11-23 devnull d.length = 0; /* would be nice to do better */
313 b8c14089 2003-11-23 devnull d.name = dir->name;
314 b8c14089 2003-11-23 devnull d.uid = user;
315 b8c14089 2003-11-23 devnull d.gid = user;
316 b8c14089 2003-11-23 devnull d.muid = user;
317 b8c14089 2003-11-23 devnull d.atime = clock;
318 b8c14089 2003-11-23 devnull d.mtime = clock;
319 b8c14089 2003-11-23 devnull return convD2M(&d, buf, nbuf);
322 b8c14089 2003-11-23 devnull static void
323 b8c14089 2003-11-23 devnull queuesend(Dirtab *d, Plumbmsg *m)
325 b8c14089 2003-11-23 devnull Sendreq *s, *t;
329 b8c14089 2003-11-23 devnull s = emalloc(sizeof(Sendreq));
330 b8c14089 2003-11-23 devnull s->nfid = d->nopen;
331 b8c14089 2003-11-23 devnull s->nleft = s->nfid;
332 b8c14089 2003-11-23 devnull s->fid = emalloc(s->nfid*sizeof(Fid*));
334 b8c14089 2003-11-23 devnull /* build array of fids open on this channel */
335 b8c14089 2003-11-23 devnull for(f=d->fopen; f!=nil; f=f->nextopen)
336 b8c14089 2003-11-23 devnull s->fid[i++] = f;
337 b8c14089 2003-11-23 devnull s->msg = m;
338 b8c14089 2003-11-23 devnull s->next = nil;
339 b8c14089 2003-11-23 devnull /* link to end of queue; drainqueue() searches in sender order so this implements a FIFO */
340 b8c14089 2003-11-23 devnull for(t=d->sendq; t!=nil; t=t->next)
341 b8c14089 2003-11-23 devnull if(t->next == nil)
343 b8c14089 2003-11-23 devnull if(t == nil)
344 b8c14089 2003-11-23 devnull d->sendq = s;
346 b8c14089 2003-11-23 devnull t->next = s;
349 b8c14089 2003-11-23 devnull static void
350 b8c14089 2003-11-23 devnull queueread(Dirtab *d, Fcall *t, uchar *buf, Fid *f)
352 b8c14089 2003-11-23 devnull Readreq *r;
354 b8c14089 2003-11-23 devnull r = emalloc(sizeof(Readreq));
355 b8c14089 2003-11-23 devnull r->fcall = t;
356 b8c14089 2003-11-23 devnull r->buf = buf;
357 b8c14089 2003-11-23 devnull r->fid = f;
358 b8c14089 2003-11-23 devnull r->next = d->readq;
359 b8c14089 2003-11-23 devnull d->readq = r;
362 b8c14089 2003-11-23 devnull static void
363 b8c14089 2003-11-23 devnull drainqueue(Dirtab *d)
365 b8c14089 2003-11-23 devnull Readreq *r, *nextr, *prevr;
366 b8c14089 2003-11-23 devnull Sendreq *s, *nexts, *prevs;
367 b8c14089 2003-11-23 devnull int i, n;
369 b8c14089 2003-11-23 devnull prevs = nil;
370 b8c14089 2003-11-23 devnull for(s=d->sendq; s!=nil; s=nexts){
371 b8c14089 2003-11-23 devnull nexts = s->next;
372 b8c14089 2003-11-23 devnull for(i=0; i<s->nfid; i++){
373 b8c14089 2003-11-23 devnull prevr = nil;
374 b8c14089 2003-11-23 devnull for(r=d->readq; r!=nil; r=nextr){
375 b8c14089 2003-11-23 devnull nextr = r->next;
376 b8c14089 2003-11-23 devnull if(r->fid == s->fid[i]){
377 b8c14089 2003-11-23 devnull /* pack the message if necessary */
378 b8c14089 2003-11-23 devnull if(s->pack == nil)
379 b8c14089 2003-11-23 devnull s->pack = plumbpack(s->msg, &s->npack);
380 b8c14089 2003-11-23 devnull /* exchange the stuff... */
381 b8c14089 2003-11-23 devnull r->fcall->data = s->pack+r->fid->offset;
382 b8c14089 2003-11-23 devnull n = s->npack - r->fid->offset;
383 b8c14089 2003-11-23 devnull if(n > messagesize-IOHDRSZ)
384 b8c14089 2003-11-23 devnull n = messagesize-IOHDRSZ;
385 b8c14089 2003-11-23 devnull if(n > r->fcall->count)
386 b8c14089 2003-11-23 devnull n = r->fcall->count;
387 b8c14089 2003-11-23 devnull r->fcall->count = n;
388 b8c14089 2003-11-23 devnull fsysrespond(r->fcall, r->buf, nil);
389 b8c14089 2003-11-23 devnull r->fid->offset += n;
390 b8c14089 2003-11-23 devnull if(r->fid->offset >= s->npack){
391 b8c14089 2003-11-23 devnull /* message transferred; delete this fid from send queue */
392 b8c14089 2003-11-23 devnull r->fid->offset = 0;
393 b8c14089 2003-11-23 devnull s->fid[i] = nil;
394 b8c14089 2003-11-23 devnull s->nleft--;
396 b8c14089 2003-11-23 devnull /* delete read request from queue */
397 b8c14089 2003-11-23 devnull if(prevr)
398 b8c14089 2003-11-23 devnull prevr->next = r->next;
400 b8c14089 2003-11-23 devnull d->readq = r->next;
401 b8c14089 2003-11-23 devnull free(r->fcall);
402 b8c14089 2003-11-23 devnull free(r);
405 b8c14089 2003-11-23 devnull prevr = r;
408 b8c14089 2003-11-23 devnull /* if no fids left, delete this send from queue */
409 b8c14089 2003-11-23 devnull if(s->nleft == 0){
410 b8c14089 2003-11-23 devnull free(s->fid);
411 b8c14089 2003-11-23 devnull plumbfree(s->msg);
412 b8c14089 2003-11-23 devnull free(s->pack);
413 b8c14089 2003-11-23 devnull if(prevs)
414 b8c14089 2003-11-23 devnull prevs->next = s->next;
416 b8c14089 2003-11-23 devnull d->sendq = s->next;
417 b8c14089 2003-11-23 devnull free(s);
419 b8c14089 2003-11-23 devnull prevs = s;
423 b8c14089 2003-11-23 devnull /* can't flush a send because they are always answered synchronously */
424 b8c14089 2003-11-23 devnull static void
425 b8c14089 2003-11-23 devnull flushqueue(Dirtab *d, int oldtag)
427 b8c14089 2003-11-23 devnull Readreq *r, *prevr;
429 b8c14089 2003-11-23 devnull prevr = nil;
430 b8c14089 2003-11-23 devnull for(r=d->readq; r!=nil; r=r->next){
431 b8c14089 2003-11-23 devnull if(oldtag == r->fcall->tag){
432 b8c14089 2003-11-23 devnull /* delete read request from queue */
433 b8c14089 2003-11-23 devnull if(prevr)
434 b8c14089 2003-11-23 devnull prevr->next = r->next;
436 b8c14089 2003-11-23 devnull d->readq = r->next;
437 b8c14089 2003-11-23 devnull free(r->fcall);
438 b8c14089 2003-11-23 devnull free(r->buf);
439 b8c14089 2003-11-23 devnull free(r);
442 b8c14089 2003-11-23 devnull prevr = r;
446 b8c14089 2003-11-23 devnull /* remove messages awaiting delivery to now-closing fid */
447 b8c14089 2003-11-23 devnull static void
448 b8c14089 2003-11-23 devnull removesenders(Dirtab *d, Fid *fid)
450 b8c14089 2003-11-23 devnull Sendreq *s, *nexts, *prevs;
453 b8c14089 2003-11-23 devnull prevs = nil;
454 b8c14089 2003-11-23 devnull for(s=d->sendq; s!=nil; s=nexts){
455 b8c14089 2003-11-23 devnull nexts = s->next;
456 b8c14089 2003-11-23 devnull for(i=0; i<s->nfid; i++)
457 b8c14089 2003-11-23 devnull if(fid == s->fid[i]){
458 b8c14089 2003-11-23 devnull /* delete this fid from send queue */
459 b8c14089 2003-11-23 devnull s->fid[i] = nil;
460 b8c14089 2003-11-23 devnull s->nleft--;
463 b8c14089 2003-11-23 devnull /* if no fids left, delete this send from queue */
464 b8c14089 2003-11-23 devnull if(s->nleft == 0){
465 b8c14089 2003-11-23 devnull free(s->fid);
466 b8c14089 2003-11-23 devnull plumbfree(s->msg);
467 b8c14089 2003-11-23 devnull free(s->pack);
468 b8c14089 2003-11-23 devnull if(prevs)
469 b8c14089 2003-11-23 devnull prevs->next = s->next;
471 b8c14089 2003-11-23 devnull d->sendq = s->next;
472 b8c14089 2003-11-23 devnull free(s);
474 b8c14089 2003-11-23 devnull prevs = s;
478 b8c14089 2003-11-23 devnull static void
479 b8c14089 2003-11-23 devnull hold(Plumbmsg *m, Dirtab *d)
481 b8c14089 2003-11-23 devnull Holdq *h, *q;
483 b8c14089 2003-11-23 devnull h = emalloc(sizeof(Holdq));
484 b8c14089 2003-11-23 devnull h->msg = m;
485 b8c14089 2003-11-23 devnull /* add to end of queue */
486 b8c14089 2003-11-23 devnull if(d->holdq == nil)
487 b8c14089 2003-11-23 devnull d->holdq = h;
489 b8c14089 2003-11-23 devnull for(q=d->holdq; q->next!=nil; q=q->next)
491 b8c14089 2003-11-23 devnull q->next = h;
495 b8c14089 2003-11-23 devnull static void
496 b8c14089 2003-11-23 devnull queueheld(Dirtab *d)
498 b8c14089 2003-11-23 devnull Holdq *h;
500 b8c14089 2003-11-23 devnull while(d->holdq != nil){
501 b8c14089 2003-11-23 devnull h = d->holdq;
502 b8c14089 2003-11-23 devnull d->holdq = h->next;
503 b8c14089 2003-11-23 devnull queuesend(d, h->msg);
504 b8c14089 2003-11-23 devnull /* no need to drain queue because we know no-one is reading yet */
505 b8c14089 2003-11-23 devnull free(h);
509 b8c14089 2003-11-23 devnull static void
510 b8c14089 2003-11-23 devnull dispose(Fcall *t, uchar *buf, Plumbmsg *m, Ruleset *rs, Exec *e)
513 b8c14089 2003-11-23 devnull char *err;
515 b8c14089 2003-11-23 devnull qlock(&queue);
516 b8c14089 2003-11-23 devnull err = nil;
517 b8c14089 2003-11-23 devnull if(m->dst==nil || m->dst[0]=='\0'){
518 b8c14089 2003-11-23 devnull err = Enoport;
519 b8c14089 2003-11-23 devnull if(rs != nil)
520 b8c14089 2003-11-23 devnull err = startup(rs, e);
521 b8c14089 2003-11-23 devnull plumbfree(m);
523 b8c14089 2003-11-23 devnull for(i=NQID; i<ndir; i++)
524 b8c14089 2003-11-23 devnull if(strcmp(m->dst, dir[i].name) == 0){
525 b8c14089 2003-11-23 devnull if(dir[i].nopen == 0){
526 b8c14089 2003-11-23 devnull err = startup(rs, e);
527 b8c14089 2003-11-23 devnull if(e!=nil && e->holdforclient)
528 b8c14089 2003-11-23 devnull hold(m, &dir[i]);
530 b8c14089 2003-11-23 devnull plumbfree(m);
532 b8c14089 2003-11-23 devnull queuesend(&dir[i], m);
533 b8c14089 2003-11-23 devnull drainqueue(&dir[i]);
537 b8c14089 2003-11-23 devnull freeexec(e);
538 b8c14089 2003-11-23 devnull qunlock(&queue);
539 b8c14089 2003-11-23 devnull fsysrespond(t, buf, err);
540 b8c14089 2003-11-23 devnull free(t);
543 b8c14089 2003-11-23 devnull static Fcall*
544 32f69c36 2003-12-11 devnull fsysversion(Fcall *t, uchar *buf, Fid *fid)
546 32f69c36 2003-12-11 devnull USED(fid);
548 b8c14089 2003-11-23 devnull if(t->msize < 256){
549 b8c14089 2003-11-23 devnull fsysrespond(t, buf, "version: message size too small");
550 b8c14089 2003-11-23 devnull return t;
552 b8c14089 2003-11-23 devnull if(t->msize < messagesize)
553 b8c14089 2003-11-23 devnull messagesize = t->msize;
554 b8c14089 2003-11-23 devnull t->msize = messagesize;
555 b8c14089 2003-11-23 devnull if(strncmp(t->version, "9P2000", 6) != 0){
556 b8c14089 2003-11-23 devnull fsysrespond(t, buf, "unrecognized 9P version");
557 b8c14089 2003-11-23 devnull return t;
559 b8c14089 2003-11-23 devnull t->version = "9P2000";
560 b8c14089 2003-11-23 devnull fsysrespond(t, buf, nil);
561 b8c14089 2003-11-23 devnull return t;
564 b8c14089 2003-11-23 devnull static Fcall*
565 32f69c36 2003-12-11 devnull fsysauth(Fcall *t, uchar *buf, Fid *fid)
567 32f69c36 2003-12-11 devnull USED(fid);
568 b8c14089 2003-11-23 devnull fsysrespond(t, buf, "plumber: authentication not required");
569 b8c14089 2003-11-23 devnull return t;
572 b8c14089 2003-11-23 devnull static Fcall*
573 b8c14089 2003-11-23 devnull fsysattach(Fcall *t, uchar *buf, Fid *f)
575 b8c14089 2003-11-23 devnull Fcall out;
577 b8c14089 2003-11-23 devnull if(strcmp(t->uname, user) != 0){
578 b8c14089 2003-11-23 devnull fsysrespond(&out, buf, Eperm);
579 b8c14089 2003-11-23 devnull return t;
581 b8c14089 2003-11-23 devnull f->busy = 1;
582 b8c14089 2003-11-23 devnull f->open = 0;
583 b8c14089 2003-11-23 devnull f->qid.type = QTDIR;
584 b8c14089 2003-11-23 devnull f->qid.path = Qdir;
585 b8c14089 2003-11-23 devnull f->qid.vers = 0;
586 b8c14089 2003-11-23 devnull f->dir = dir;
587 b8c14089 2003-11-23 devnull memset(&out, 0, sizeof(Fcall));
588 b8c14089 2003-11-23 devnull out.type = t->type;
589 b8c14089 2003-11-23 devnull out.tag = t->tag;
590 b8c14089 2003-11-23 devnull out.fid = f->fid;
591 b8c14089 2003-11-23 devnull out.qid = f->qid;
592 b8c14089 2003-11-23 devnull fsysrespond(&out, buf, nil);
593 b8c14089 2003-11-23 devnull return t;
596 b8c14089 2003-11-23 devnull static Fcall*
597 32f69c36 2003-12-11 devnull fsysflush(Fcall *t, uchar *buf, Fid *fid)
601 32f69c36 2003-12-11 devnull USED(fid);
602 b8c14089 2003-11-23 devnull qlock(&queue);
603 b8c14089 2003-11-23 devnull for(i=NQID; i<ndir; i++)
604 b8c14089 2003-11-23 devnull flushqueue(&dir[i], t->oldtag);
605 b8c14089 2003-11-23 devnull qunlock(&queue);
606 b8c14089 2003-11-23 devnull fsysrespond(t, buf, nil);
607 b8c14089 2003-11-23 devnull return t;
610 b8c14089 2003-11-23 devnull static Fcall*
611 b8c14089 2003-11-23 devnull fsyswalk(Fcall *t, uchar *buf, Fid *f)
613 b8c14089 2003-11-23 devnull Fcall out;
614 b8c14089 2003-11-23 devnull Fid *nf;
615 b8c14089 2003-11-23 devnull ulong path;
616 b8c14089 2003-11-23 devnull Dirtab *d, *dir;
619 b8c14089 2003-11-23 devnull uchar type;
620 b8c14089 2003-11-23 devnull char *err;
622 b8c14089 2003-11-23 devnull if(f->open){
623 b8c14089 2003-11-23 devnull fsysrespond(t, buf, "clone of an open fid");
624 b8c14089 2003-11-23 devnull return t;
627 b8c14089 2003-11-23 devnull nf = nil;
628 b8c14089 2003-11-23 devnull if(t->fid != t->newfid){
629 b8c14089 2003-11-23 devnull nf = newfid(t->newfid);
630 b8c14089 2003-11-23 devnull if(nf->busy){
631 b8c14089 2003-11-23 devnull fsysrespond(t, buf, "clone to a busy fid");
632 b8c14089 2003-11-23 devnull return t;
634 b8c14089 2003-11-23 devnull nf->busy = 1;
635 b8c14089 2003-11-23 devnull nf->open = 0;
636 b8c14089 2003-11-23 devnull nf->dir = f->dir;
637 b8c14089 2003-11-23 devnull nf->qid = f->qid;
638 b8c14089 2003-11-23 devnull f = nf; /* walk f */
641 b8c14089 2003-11-23 devnull out.nwqid = 0;
642 b8c14089 2003-11-23 devnull err = nil;
643 b8c14089 2003-11-23 devnull dir = f->dir;
644 b8c14089 2003-11-23 devnull q = f->qid;
646 b8c14089 2003-11-23 devnull if(t->nwname > 0){
647 b8c14089 2003-11-23 devnull for(i=0; i<t->nwname; i++){
648 b8c14089 2003-11-23 devnull if((q.type & QTDIR) == 0){
649 b8c14089 2003-11-23 devnull err = Enotdir;
652 b8c14089 2003-11-23 devnull if(strcmp(t->wname[i], "..") == 0){
653 b8c14089 2003-11-23 devnull type = QTDIR;
654 b8c14089 2003-11-23 devnull path = Qdir;
656 b8c14089 2003-11-23 devnull q.type = type;
657 b8c14089 2003-11-23 devnull q.vers = 0;
658 b8c14089 2003-11-23 devnull q.path = path;
659 b8c14089 2003-11-23 devnull out.wqid[out.nwqid++] = q;
660 b8c14089 2003-11-23 devnull continue;
662 b8c14089 2003-11-23 devnull d = dir;
663 b8c14089 2003-11-23 devnull d++; /* skip '.' */
664 b8c14089 2003-11-23 devnull for(; d->name; d++)
665 b8c14089 2003-11-23 devnull if(strcmp(t->wname[i], d->name) == 0){
666 b8c14089 2003-11-23 devnull type = d->type;
667 b8c14089 2003-11-23 devnull path = d->qid;
668 b8c14089 2003-11-23 devnull dir = d;
669 b8c14089 2003-11-23 devnull goto Accept;
671 b8c14089 2003-11-23 devnull err = Enoexist;
676 b8c14089 2003-11-23 devnull out.type = t->type;
677 b8c14089 2003-11-23 devnull out.tag = t->tag;
678 b8c14089 2003-11-23 devnull if(err!=nil || out.nwqid<t->nwname){
680 b8c14089 2003-11-23 devnull nf->busy = 0;
681 b8c14089 2003-11-23 devnull }else if(out.nwqid == t->nwname){
682 b8c14089 2003-11-23 devnull f->qid = q;
683 b8c14089 2003-11-23 devnull f->dir = dir;
686 b8c14089 2003-11-23 devnull fsysrespond(&out, buf, err);
687 b8c14089 2003-11-23 devnull return t;
690 b8c14089 2003-11-23 devnull static Fcall*
691 b8c14089 2003-11-23 devnull fsysopen(Fcall *t, uchar *buf, Fid *f)
693 b8c14089 2003-11-23 devnull int m, clearrules, mode;
695 b8c14089 2003-11-23 devnull clearrules = 0;
696 b8c14089 2003-11-23 devnull if(t->mode & OTRUNC){
697 b8c14089 2003-11-23 devnull if(f->qid.path != Qrules)
698 b8c14089 2003-11-23 devnull goto Deny;
699 b8c14089 2003-11-23 devnull clearrules = 1;
701 b8c14089 2003-11-23 devnull /* can't truncate anything, so just disregard */
702 b8c14089 2003-11-23 devnull mode = t->mode & ~(OTRUNC|OCEXEC);
703 b8c14089 2003-11-23 devnull /* can't execute or remove anything */
704 b8c14089 2003-11-23 devnull if(mode==OEXEC || (mode&ORCLOSE))
705 b8c14089 2003-11-23 devnull goto Deny;
706 b8c14089 2003-11-23 devnull switch(mode){
707 b8c14089 2003-11-23 devnull default:
708 b8c14089 2003-11-23 devnull goto Deny;
709 b8c14089 2003-11-23 devnull case OREAD:
710 b8c14089 2003-11-23 devnull m = 0400;
712 b8c14089 2003-11-23 devnull case OWRITE:
713 b8c14089 2003-11-23 devnull m = 0200;
715 b8c14089 2003-11-23 devnull case ORDWR:
716 b8c14089 2003-11-23 devnull m = 0600;
719 b8c14089 2003-11-23 devnull if(((f->dir->perm&~(DMDIR|DMAPPEND))&m) != m)
720 b8c14089 2003-11-23 devnull goto Deny;
721 b8c14089 2003-11-23 devnull if(f->qid.path==Qrules && (mode==OWRITE || mode==ORDWR)){
722 32f69c36 2003-12-11 devnull lock(&rulesref.lk);
723 b8c14089 2003-11-23 devnull if(rulesref.ref++ != 0){
724 b8c14089 2003-11-23 devnull rulesref.ref--;
725 32f69c36 2003-12-11 devnull unlock(&rulesref.lk);
726 b8c14089 2003-11-23 devnull fsysrespond(t, buf, Einuse);
727 b8c14089 2003-11-23 devnull return t;
729 32f69c36 2003-12-11 devnull unlock(&rulesref.lk);
731 b8c14089 2003-11-23 devnull if(clearrules){
732 b8c14089 2003-11-23 devnull writerules(nil, 0);
733 b8c14089 2003-11-23 devnull rules[0] = nil;
735 b8c14089 2003-11-23 devnull t->qid = f->qid;
736 b8c14089 2003-11-23 devnull t->iounit = 0;
737 b8c14089 2003-11-23 devnull qlock(&queue);
738 b8c14089 2003-11-23 devnull f->mode = mode;
739 b8c14089 2003-11-23 devnull f->open = 1;
740 b8c14089 2003-11-23 devnull f->dir->nopen++;
741 b8c14089 2003-11-23 devnull f->nextopen = f->dir->fopen;
742 b8c14089 2003-11-23 devnull f->dir->fopen = f;
743 b8c14089 2003-11-23 devnull queueheld(f->dir);
744 b8c14089 2003-11-23 devnull qunlock(&queue);
745 b8c14089 2003-11-23 devnull fsysrespond(t, buf, nil);
746 b8c14089 2003-11-23 devnull return t;
749 b8c14089 2003-11-23 devnull fsysrespond(t, buf, Eperm);
750 b8c14089 2003-11-23 devnull return t;
753 b8c14089 2003-11-23 devnull static Fcall*
754 32f69c36 2003-12-11 devnull fsyscreate(Fcall *t, uchar *buf, Fid *fid)
756 32f69c36 2003-12-11 devnull USED(fid);
757 b8c14089 2003-11-23 devnull fsysrespond(t, buf, Eperm);
758 b8c14089 2003-11-23 devnull return t;
761 b8c14089 2003-11-23 devnull static Fcall*
762 b8c14089 2003-11-23 devnull fsysreadrules(Fcall *t, uchar *buf)
764 b8c14089 2003-11-23 devnull char *p;
767 b8c14089 2003-11-23 devnull p = printrules();
768 b8c14089 2003-11-23 devnull n = strlen(p);
769 b8c14089 2003-11-23 devnull t->data = p;
770 b8c14089 2003-11-23 devnull if(t->offset >= n)
771 b8c14089 2003-11-23 devnull t->count = 0;
773 b8c14089 2003-11-23 devnull t->data = p+t->offset;
774 b8c14089 2003-11-23 devnull if(t->offset+t->count > n)
775 b8c14089 2003-11-23 devnull t->count = n-t->offset;
777 b8c14089 2003-11-23 devnull fsysrespond(t, buf, nil);
778 b8c14089 2003-11-23 devnull free(p);
779 b8c14089 2003-11-23 devnull return t;
782 b8c14089 2003-11-23 devnull static Fcall*
783 b8c14089 2003-11-23 devnull fsysread(Fcall *t, uchar *buf, Fid *f)
785 b8c14089 2003-11-23 devnull uchar *b;
786 b8c14089 2003-11-23 devnull int i, n, o, e;
787 b8c14089 2003-11-23 devnull uint len;
788 b8c14089 2003-11-23 devnull Dirtab *d;
789 b8c14089 2003-11-23 devnull uint clock;
791 b8c14089 2003-11-23 devnull if(f->qid.path != Qdir){
792 b8c14089 2003-11-23 devnull if(f->qid.path == Qrules)
793 b8c14089 2003-11-23 devnull return fsysreadrules(t, buf);
794 b8c14089 2003-11-23 devnull /* read from port */
795 b8c14089 2003-11-23 devnull if(f->qid.path < NQID){
796 b8c14089 2003-11-23 devnull fsysrespond(t, buf, "internal error: unknown read port");
797 b8c14089 2003-11-23 devnull return t;
799 b8c14089 2003-11-23 devnull qlock(&queue);
800 b8c14089 2003-11-23 devnull queueread(f->dir, t, buf, f);
801 b8c14089 2003-11-23 devnull drainqueue(f->dir);
802 b8c14089 2003-11-23 devnull qunlock(&queue);
803 b8c14089 2003-11-23 devnull return nil;
805 b8c14089 2003-11-23 devnull o = t->offset;
806 b8c14089 2003-11-23 devnull e = t->offset+t->count;
807 b8c14089 2003-11-23 devnull clock = getclock();
808 b8c14089 2003-11-23 devnull b = malloc(messagesize-IOHDRSZ);
809 b8c14089 2003-11-23 devnull if(b == nil){
810 b8c14089 2003-11-23 devnull fsysrespond(t, buf, Enomem);
811 b8c14089 2003-11-23 devnull return t;
814 b8c14089 2003-11-23 devnull d = dir;
815 b8c14089 2003-11-23 devnull d++; /* first entry is '.' */
816 b8c14089 2003-11-23 devnull for(i=0; d->name!=nil && i<e; i+=len){
817 b8c14089 2003-11-23 devnull len = dostat(d, b+n, messagesize-IOHDRSZ-n, clock);
818 b8c14089 2003-11-23 devnull if(len <= BIT16SZ)
820 b8c14089 2003-11-23 devnull if(i >= o)
821 b8c14089 2003-11-23 devnull n += len;
824 b8c14089 2003-11-23 devnull t->data = (char*)b;
825 b8c14089 2003-11-23 devnull t->count = n;
826 b8c14089 2003-11-23 devnull fsysrespond(t, buf, nil);
827 b8c14089 2003-11-23 devnull free(b);
828 b8c14089 2003-11-23 devnull return t;
831 b8c14089 2003-11-23 devnull static Fcall*
832 b8c14089 2003-11-23 devnull fsyswrite(Fcall *t, uchar *buf, Fid *f)
834 b8c14089 2003-11-23 devnull Plumbmsg *m;
835 b8c14089 2003-11-23 devnull int i, n;
836 b8c14089 2003-11-23 devnull long count;
837 b8c14089 2003-11-23 devnull char *data;
838 b8c14089 2003-11-23 devnull Exec *e;
840 b8c14089 2003-11-23 devnull switch((int)f->qid.path){
841 b8c14089 2003-11-23 devnull case Qdir:
842 b8c14089 2003-11-23 devnull fsysrespond(t, buf, Eisdir);
843 b8c14089 2003-11-23 devnull return t;
844 b8c14089 2003-11-23 devnull case Qrules:
845 b8c14089 2003-11-23 devnull clock = getclock();
846 b8c14089 2003-11-23 devnull fsysrespond(t, buf, writerules(t->data, t->count));
847 b8c14089 2003-11-23 devnull return t;
848 b8c14089 2003-11-23 devnull case Qsend:
849 b8c14089 2003-11-23 devnull if(f->offset == 0){
850 b8c14089 2003-11-23 devnull data = t->data;
851 b8c14089 2003-11-23 devnull count = t->count;
853 b8c14089 2003-11-23 devnull /* partial message already assembled */
854 b8c14089 2003-11-23 devnull f->writebuf = erealloc(f->writebuf, f->offset + t->count);
855 b8c14089 2003-11-23 devnull memmove(f->writebuf+f->offset, t->data, t->count);
856 b8c14089 2003-11-23 devnull data = f->writebuf;
857 b8c14089 2003-11-23 devnull count = f->offset+t->count;
859 b8c14089 2003-11-23 devnull m = plumbunpackpartial(data, count, &n);
860 b8c14089 2003-11-23 devnull if(m == nil){
861 b8c14089 2003-11-23 devnull if(n == 0){
862 b8c14089 2003-11-23 devnull f->offset = 0;
863 b8c14089 2003-11-23 devnull free(f->writebuf);
864 b8c14089 2003-11-23 devnull f->writebuf = nil;
865 b8c14089 2003-11-23 devnull fsysrespond(t, buf, Ebadmsg);
866 b8c14089 2003-11-23 devnull return t;
868 b8c14089 2003-11-23 devnull /* can read more... */
869 b8c14089 2003-11-23 devnull if(f->offset == 0){
870 b8c14089 2003-11-23 devnull f->writebuf = emalloc(t->count);
871 b8c14089 2003-11-23 devnull memmove(f->writebuf, t->data, t->count);
873 b8c14089 2003-11-23 devnull /* else buffer has already been grown */
874 b8c14089 2003-11-23 devnull f->offset += t->count;
875 b8c14089 2003-11-23 devnull fsysrespond(t, buf, nil);
876 b8c14089 2003-11-23 devnull return t;
878 b8c14089 2003-11-23 devnull /* release partial buffer */
879 b8c14089 2003-11-23 devnull f->offset = 0;
880 b8c14089 2003-11-23 devnull free(f->writebuf);
881 b8c14089 2003-11-23 devnull f->writebuf = nil;
882 b8c14089 2003-11-23 devnull for(i=0; rules[i]; i++)
883 b8c14089 2003-11-23 devnull if((e=matchruleset(m, rules[i])) != nil){
884 b8c14089 2003-11-23 devnull dispose(t, buf, m, rules[i], e);
885 b8c14089 2003-11-23 devnull return nil;
887 b8c14089 2003-11-23 devnull if(m->dst != nil){
888 b8c14089 2003-11-23 devnull dispose(t, buf, m, nil, nil);
889 b8c14089 2003-11-23 devnull return nil;
891 b8c14089 2003-11-23 devnull fsysrespond(t, buf, "no matching plumb rule");
892 b8c14089 2003-11-23 devnull return t;
894 b8c14089 2003-11-23 devnull fsysrespond(t, buf, "internal error: write to unknown file");
895 b8c14089 2003-11-23 devnull return t;
898 b8c14089 2003-11-23 devnull static Fcall*
899 b8c14089 2003-11-23 devnull fsysstat(Fcall *t, uchar *buf, Fid *f)
901 b8c14089 2003-11-23 devnull t->stat = emalloc(messagesize-IOHDRSZ);
902 b8c14089 2003-11-23 devnull t->nstat = dostat(f->dir, t->stat, messagesize-IOHDRSZ, clock);
903 b8c14089 2003-11-23 devnull fsysrespond(t, buf, nil);
904 b8c14089 2003-11-23 devnull free(t->stat);
905 b8c14089 2003-11-23 devnull t->stat = nil;
906 b8c14089 2003-11-23 devnull return t;
909 b8c14089 2003-11-23 devnull static Fcall*
910 32f69c36 2003-12-11 devnull fsyswstat(Fcall *t, uchar *buf, Fid *fid)
912 32f69c36 2003-12-11 devnull USED(fid);
913 b8c14089 2003-11-23 devnull fsysrespond(t, buf, Eperm);
914 b8c14089 2003-11-23 devnull return t;
917 b8c14089 2003-11-23 devnull static Fcall*
918 32f69c36 2003-12-11 devnull fsysremove(Fcall *t, uchar *buf, Fid *fid)
920 32f69c36 2003-12-11 devnull USED(fid);
921 b8c14089 2003-11-23 devnull fsysrespond(t, buf, Eperm);
922 b8c14089 2003-11-23 devnull return t;
925 b8c14089 2003-11-23 devnull static Fcall*
926 b8c14089 2003-11-23 devnull fsysclunk(Fcall *t, uchar *buf, Fid *f)
928 b8c14089 2003-11-23 devnull Fid *prev, *p;
929 b8c14089 2003-11-23 devnull Dirtab *d;
931 b8c14089 2003-11-23 devnull qlock(&queue);
932 b8c14089 2003-11-23 devnull if(f->open){
933 b8c14089 2003-11-23 devnull d = f->dir;
934 b8c14089 2003-11-23 devnull d->nopen--;
935 b8c14089 2003-11-23 devnull if(d->qid==Qrules && (f->mode==OWRITE || f->mode==ORDWR)){
937 b8c14089 2003-11-23 devnull * just to be sure last rule is parsed; error messages will be lost, though,
938 b8c14089 2003-11-23 devnull * unless last write ended with a blank line
940 b8c14089 2003-11-23 devnull writerules(nil, 0);
941 32f69c36 2003-12-11 devnull lock(&rulesref.lk);
942 b8c14089 2003-11-23 devnull rulesref.ref--;
943 32f69c36 2003-12-11 devnull unlock(&rulesref.lk);
945 b8c14089 2003-11-23 devnull prev = nil;
946 b8c14089 2003-11-23 devnull for(p=d->fopen; p; p=p->nextopen){
947 b8c14089 2003-11-23 devnull if(p == f){
948 b8c14089 2003-11-23 devnull if(prev)
949 b8c14089 2003-11-23 devnull prev->nextopen = f->nextopen;
951 b8c14089 2003-11-23 devnull d->fopen = f->nextopen;
952 b8c14089 2003-11-23 devnull removesenders(d, f);
955 b8c14089 2003-11-23 devnull prev = p;
958 b8c14089 2003-11-23 devnull f->busy = 0;
959 b8c14089 2003-11-23 devnull f->open = 0;
960 b8c14089 2003-11-23 devnull f->offset = 0;
961 b8c14089 2003-11-23 devnull if(f->writebuf != nil){
962 b8c14089 2003-11-23 devnull free(f->writebuf);
963 b8c14089 2003-11-23 devnull f->writebuf = nil;
965 b8c14089 2003-11-23 devnull qunlock(&queue);
966 b8c14089 2003-11-23 devnull fsysrespond(t, buf, nil);
967 b8c14089 2003-11-23 devnull return t;