1 d3df3087 2003-12-06 devnull #include <u.h>
2 d3df3087 2003-12-06 devnull #include <libc.h>
3 d3df3087 2003-12-06 devnull #include <fcall.h>
4 d3df3087 2003-12-06 devnull #include <thread.h>
5 32f69c36 2003-12-11 devnull #include <errno.h>
9 d3df3087 2003-12-06 devnull STACK = 32768,
10 d3df3087 2003-12-06 devnull NHASH = 31,
11 cbeb0b26 2006-04-01 devnull MAXMSG = 64 /* per connection */
14 d3df3087 2003-12-06 devnull typedef struct Hash Hash;
15 d3df3087 2003-12-06 devnull typedef struct Fid Fid;
16 d3df3087 2003-12-06 devnull typedef struct Msg Msg;
17 d3df3087 2003-12-06 devnull typedef struct Conn Conn;
18 d3df3087 2003-12-06 devnull typedef struct Queue Queue;
20 d3df3087 2003-12-06 devnull struct Hash
22 d3df3087 2003-12-06 devnull Hash *next;
27 d3df3087 2003-12-06 devnull struct Fid
31 d3df3087 2003-12-06 devnull int cfid;
32 21e626de 2004-12-28 devnull int openfd;
33 9777187b 2006-02-03 devnull int offset;
34 9777187b 2006-02-03 devnull int coffset;
35 215993f8 2005-09-13 devnull int isdir;
36 d3df3087 2003-12-06 devnull Fid *next;
39 d3df3087 2003-12-06 devnull struct Msg
42 d3df3087 2003-12-06 devnull int internal;
44 d3df3087 2003-12-06 devnull int ctag;
46 32f69c36 2003-12-11 devnull int isopenfd;
47 d3df3087 2003-12-06 devnull Fcall tx;
48 d3df3087 2003-12-06 devnull Fcall rx;
49 d3df3087 2003-12-06 devnull Fid *fid;
50 d3df3087 2003-12-06 devnull Fid *newfid;
51 d3df3087 2003-12-06 devnull Fid *afid;
52 d3df3087 2003-12-06 devnull Msg *oldm;
53 d3df3087 2003-12-06 devnull Msg *next;
54 d3df3087 2003-12-06 devnull uchar *tpkt;
55 d3df3087 2003-12-06 devnull uchar *rpkt;
58 d3df3087 2003-12-06 devnull struct Conn
61 32f69c36 2003-12-11 devnull int fdmode;
62 32f69c36 2003-12-11 devnull Fid *fdfid;
63 d3df3087 2003-12-06 devnull int nmsg;
64 d3df3087 2003-12-06 devnull int nfid;
65 d3df3087 2003-12-06 devnull Channel *inc;
66 d3df3087 2003-12-06 devnull Channel *internal;
67 d3df3087 2003-12-06 devnull int inputstalled;
68 d3df3087 2003-12-06 devnull char dir[40];
69 d3df3087 2003-12-06 devnull Hash *tag[NHASH];
70 d3df3087 2003-12-06 devnull Hash *fid[NHASH];
71 d3df3087 2003-12-06 devnull Queue *outq;
72 d3df3087 2003-12-06 devnull Queue *inq;
73 215993f8 2005-09-13 devnull int dotu;
76 5c84c448 2005-03-21 devnull char *xaname;
77 d3df3087 2003-12-06 devnull char *addr;
79 d3df3087 2003-12-06 devnull char adir[40];
80 d3df3087 2003-12-06 devnull int isunix;
81 d3df3087 2003-12-06 devnull Queue *outq;
82 d3df3087 2003-12-06 devnull Queue *inq;
83 05b7f431 2004-03-02 devnull int verbose = 0;
84 e2a1725d 2005-01-04 devnull int logging = 0;
85 5a8e63b2 2004-02-29 devnull int msize = 8192;
86 34e8254f 2005-07-27 devnull u32int xafid = NOFID;
87 5c84c448 2005-03-21 devnull int attached;
88 5c84c448 2005-03-21 devnull int versioned;
89 215993f8 2005-09-13 devnull int dotu;
91 d3df3087 2003-12-06 devnull void *gethash(Hash**, uint);
92 d3df3087 2003-12-06 devnull int puthash(Hash**, uint, void*);
93 d3df3087 2003-12-06 devnull int delhash(Hash**, uint, void*);
94 215993f8 2005-09-13 devnull Msg *mread9p(Ioproc*, int, int);
95 ceb04770 2003-12-09 devnull int mwrite9p(Ioproc*, int, uchar*);
96 ceb04770 2003-12-09 devnull uchar *read9ppkt(Ioproc*, int);
97 d3df3087 2003-12-06 devnull int write9ppkt(int, uchar*);
98 e2a1725d 2005-01-04 devnull Msg *msgnew(int);
99 d3df3087 2003-12-06 devnull void msgput(Msg*);
100 e2a1725d 2005-01-04 devnull void msgclear(Msg*);
101 d3df3087 2003-12-06 devnull Msg *msgget(int);
102 e2a1725d 2005-01-04 devnull void msgincref(Msg*);
103 d3df3087 2003-12-06 devnull Fid *fidnew(int);
104 d3df3087 2003-12-06 devnull void fidput(Fid*);
105 d3df3087 2003-12-06 devnull void *emalloc(int);
106 d3df3087 2003-12-06 devnull void *erealloc(void*, int);
107 ceb04770 2003-12-09 devnull Queue *qalloc(void);
108 d3df3087 2003-12-06 devnull int sendq(Queue*, void*);
109 d3df3087 2003-12-06 devnull void *recvq(Queue*);
110 d3df3087 2003-12-06 devnull void connthread(void*);
111 ceb04770 2003-12-09 devnull void connoutthread(void*);
112 d3df3087 2003-12-06 devnull void listenthread(void*);
113 ceb04770 2003-12-09 devnull void outputthread(void*);
114 ceb04770 2003-12-09 devnull void inputthread(void*);
115 d3df3087 2003-12-06 devnull void rewritehdr(Fcall*, uchar*);
116 215993f8 2005-09-13 devnull void repack(Fcall*, uchar**, int);
117 d3df3087 2003-12-06 devnull int tlisten(char*, char*);
118 d3df3087 2003-12-06 devnull int taccept(int, char*);
119 ceb04770 2003-12-09 devnull int iolisten(Ioproc*, char*, char*);
120 ceb04770 2003-12-09 devnull int ioaccept(Ioproc*, int, char*);
121 32f69c36 2003-12-11 devnull int iorecvfd(Ioproc*, int);
122 32f69c36 2003-12-11 devnull int iosendfd(Ioproc*, int, int);
123 32f69c36 2003-12-11 devnull void mainproc(void*);
124 32f69c36 2003-12-11 devnull int ignorepipe(void*, char*);
125 e2a1725d 2005-01-04 devnull int timefmt(Fmt*);
126 5c84c448 2005-03-21 devnull void dorootstat(void);
127 215993f8 2005-09-13 devnull int stripudirread(Msg*);
128 16c1defa 2005-11-12 devnull int cvtustat(Fcall*, uchar**, int);
131 d3df3087 2003-12-06 devnull usage(void)
133 5c84c448 2005-03-21 devnull fprint(2, "usage: 9pserve [-lv] [-A aname afid] [-M msize] address\n");
134 d3df3087 2003-12-06 devnull fprint(2, "\treads/writes 9P messages on stdin/stdout\n");
135 38c10d1a 2005-01-17 devnull threadexitsall("usage");
138 ceb04770 2003-12-09 devnull uchar vbuf[128];
139 32f69c36 2003-12-11 devnull extern int _threaddebuglevel;
141 d3df3087 2003-12-06 devnull threadmain(int argc, char **argv)
143 3a6f92ee 2005-03-18 devnull char *file, *x;
146 3a6f92ee 2005-03-18 devnull x = getenv("verbose9pserve");
148 3a6f92ee 2005-03-18 devnull verbose = atoi(x);
149 5c84c448 2005-03-21 devnull fprint(2, "verbose9pserve %s => %d\n", x, verbose);
151 d3df3087 2003-12-06 devnull ARGBEGIN{
152 d3df3087 2003-12-06 devnull default:
153 d3df3087 2003-12-06 devnull usage();
154 5c84c448 2005-03-21 devnull case 'A':
155 5c84c448 2005-03-21 devnull attached = 1;
156 5c84c448 2005-03-21 devnull xaname = EARGF(usage());
157 5c84c448 2005-03-21 devnull xafid = atoi(EARGF(usage()));
159 5c84c448 2005-03-21 devnull case 'M':
160 5c84c448 2005-03-21 devnull versioned = 1;
161 5c84c448 2005-03-21 devnull msize = atoi(EARGF(usage()));
163 ceb04770 2003-12-09 devnull case 'v':
164 ceb04770 2003-12-09 devnull verbose++;
166 d3df3087 2003-12-06 devnull case 'u':
167 5c84c448 2005-03-21 devnull isunix++;
169 e2a1725d 2005-01-04 devnull case 'l':
170 e2a1725d 2005-01-04 devnull logging++;
174 5c84c448 2005-03-21 devnull if(attached && !versioned){
175 5c84c448 2005-03-21 devnull fprint(2, "-A must be used with -M\n");
176 5c84c448 2005-03-21 devnull usage();
179 d3df3087 2003-12-06 devnull if(argc != 1)
180 d3df3087 2003-12-06 devnull usage();
181 ceb04770 2003-12-09 devnull addr = argv[0];
183 e2a1725d 2005-01-04 devnull fmtinstall('T', timefmt);
185 d3df3087 2003-12-06 devnull if((afd = announce(addr, adir)) < 0)
186 d3df3087 2003-12-06 devnull sysfatal("announce %s: %r", addr);
187 e2a1725d 2005-01-04 devnull if(logging){
188 e2a1725d 2005-01-04 devnull if(strncmp(addr, "unix!", 5) == 0)
189 e2a1725d 2005-01-04 devnull addr += 5;
190 e2a1725d 2005-01-04 devnull file = smprint("%s.log", addr);
191 e2a1725d 2005-01-04 devnull if(file == nil)
192 e2a1725d 2005-01-04 devnull sysfatal("smprint log: %r");
193 e2a1725d 2005-01-04 devnull if((fd = create(file, OWRITE, 0666)) < 0)
194 e2a1725d 2005-01-04 devnull sysfatal("create %s: %r", file);
195 e2a1725d 2005-01-04 devnull dup(fd, 2);
196 e2a1725d 2005-01-04 devnull if(fd > 2)
197 e2a1725d 2005-01-04 devnull close(fd);
199 e2a1725d 2005-01-04 devnull if(verbose) fprint(2, "%T 9pserve running\n");
200 e2a1725d 2005-01-04 devnull proccreate(mainproc, nil, STACK);
204 32f69c36 2003-12-11 devnull mainproc(void *v)
206 c91bd328 2004-03-05 devnull int n, nn;
207 32f69c36 2003-12-11 devnull Fcall f;
208 32f69c36 2003-12-11 devnull USED(v);
210 32f69c36 2003-12-11 devnull atnotify(ignorepipe, 1);
211 ceb04770 2003-12-09 devnull fmtinstall('D', dirfmt);
212 ceb04770 2003-12-09 devnull fmtinstall('M', dirmodefmt);
213 ceb04770 2003-12-09 devnull fmtinstall('F', fcallfmt);
214 ceb04770 2003-12-09 devnull fmtinstall('H', encodefmt);
216 ceb04770 2003-12-09 devnull outq = qalloc();
217 ceb04770 2003-12-09 devnull inq = qalloc();
219 5c84c448 2005-03-21 devnull if(!versioned){
220 5c84c448 2005-03-21 devnull f.type = Tversion;
221 215993f8 2005-09-13 devnull f.version = "9P2000.u";
222 5c84c448 2005-03-21 devnull f.msize = msize;
223 5c84c448 2005-03-21 devnull f.tag = NOTAG;
224 5c84c448 2005-03-21 devnull n = convS2M(&f, vbuf, sizeof vbuf);
225 9e2f1d9b 2005-11-21 devnull if(n <= BIT16SZ)
226 9e2f1d9b 2005-11-21 devnull sysfatal("convS2M conversion error");
227 5c84c448 2005-03-21 devnull if(verbose > 1) fprint(2, "%T * <- %F\n", &f);
228 5c84c448 2005-03-21 devnull nn = write(1, vbuf, n);
229 5c84c448 2005-03-21 devnull if(n != nn)
230 5c84c448 2005-03-21 devnull sysfatal("error writing Tversion: %r\n");
231 5c84c448 2005-03-21 devnull n = read9pmsg(0, vbuf, sizeof vbuf);
232 5c84c448 2005-03-21 devnull if(convM2S(vbuf, n, &f) != n)
233 5c84c448 2005-03-21 devnull sysfatal("convM2S failure");
234 5c84c448 2005-03-21 devnull if(f.msize < msize)
235 5c84c448 2005-03-21 devnull msize = f.msize;
236 5c84c448 2005-03-21 devnull if(verbose > 1) fprint(2, "%T * -> %F\n", &f);
237 215993f8 2005-09-13 devnull dotu = strncmp(f.version, "9P2000.u", 8) == 0;
240 32f69c36 2003-12-11 devnull threadcreate(inputthread, nil, STACK);
241 32f69c36 2003-12-11 devnull threadcreate(outputthread, nil, STACK);
243 cbeb0b26 2006-04-01 devnull /* if(rootfid) */
244 cbeb0b26 2006-04-01 devnull /* dorootstat(); */
246 ceb04770 2003-12-09 devnull threadcreate(listenthread, nil, STACK);
247 32f69c36 2003-12-11 devnull threadexits(0);
251 32f69c36 2003-12-11 devnull ignorepipe(void *v, char *s)
253 32f69c36 2003-12-11 devnull USED(v);
254 32f69c36 2003-12-11 devnull if(strcmp(s, "sys: write on closed pipe") == 0)
255 32f69c36 2003-12-11 devnull return 1;
256 c042e61e 2005-03-18 devnull if(strcmp(s, "sys: tstp") == 0)
257 c042e61e 2005-03-18 devnull return 1;
258 c042e61e 2005-03-18 devnull fprint(2, "9pserve %s: %T note: %s\n", addr, s);
259 32f69c36 2003-12-11 devnull return 0;
263 d3df3087 2003-12-06 devnull listenthread(void *arg)
265 d3df3087 2003-12-06 devnull Conn *c;
266 ceb04770 2003-12-09 devnull Ioproc *io;
268 ceb04770 2003-12-09 devnull io = ioproc();
269 d3df3087 2003-12-06 devnull USED(arg);
270 7ffc5208 2004-12-28 devnull threadsetname("listen %s", adir);
271 d3df3087 2003-12-06 devnull for(;;){
272 ceb04770 2003-12-09 devnull c = emalloc(sizeof(Conn));
273 ceb04770 2003-12-09 devnull c->fd = iolisten(io, adir, c->dir);
274 d3df3087 2003-12-06 devnull if(c->fd < 0){
275 e2a1725d 2005-01-04 devnull if(verbose) fprint(2, "%T listen: %r\n");
276 d3df3087 2003-12-06 devnull close(afd);
277 d3df3087 2003-12-06 devnull free(c);
280 32f69c36 2003-12-11 devnull c->inc = chancreate(sizeof(void*), 0);
281 32f69c36 2003-12-11 devnull c->internal = chancreate(sizeof(void*), 0);
282 32f69c36 2003-12-11 devnull c->inq = qalloc();
283 32f69c36 2003-12-11 devnull c->outq = qalloc();
284 e2a1725d 2005-01-04 devnull if(verbose) fprint(2, "%T incoming call on %s\n", c->dir);
285 d3df3087 2003-12-06 devnull threadcreate(connthread, c, STACK);
290 32f69c36 2003-12-11 devnull send9pmsg(Msg *m)
292 d3df3087 2003-12-06 devnull int n, nn;
294 215993f8 2005-09-13 devnull n = sizeS2Mu(&m->rx, m->c->dotu);
295 d3df3087 2003-12-06 devnull m->rpkt = emalloc(n);
296 215993f8 2005-09-13 devnull nn = convS2Mu(&m->rx, m->rpkt, n, m->c->dotu);
297 9e2f1d9b 2005-11-21 devnull if(nn <= BIT16SZ)
298 9e2f1d9b 2005-11-21 devnull sysfatal("convS2Mu conversion error");
299 d3df3087 2003-12-06 devnull if(nn != n)
300 9e2f1d9b 2005-11-21 devnull sysfatal("sizeS2Mu and convS2Mu disagree");
301 d3df3087 2003-12-06 devnull sendq(m->c->outq, m);
305 ceb04770 2003-12-09 devnull sendomsg(Msg *m)
307 ceb04770 2003-12-09 devnull int n, nn;
309 215993f8 2005-09-13 devnull n = sizeS2Mu(&m->tx, m->c->dotu);
310 ceb04770 2003-12-09 devnull m->tpkt = emalloc(n);
311 215993f8 2005-09-13 devnull nn = convS2Mu(&m->tx, m->tpkt, n, m->c->dotu);
312 9e2f1d9b 2005-11-21 devnull if(nn <= BIT16SZ)
313 9e2f1d9b 2005-11-21 devnull sysfatal("convS2Mu conversion error");
314 ceb04770 2003-12-09 devnull if(nn != n)
315 9e2f1d9b 2005-11-21 devnull sysfatal("sizeS2Mu and convS2Mu disagree");
316 ceb04770 2003-12-09 devnull sendq(outq, m);
320 ceb04770 2003-12-09 devnull err(Msg *m, char *ename)
322 ceb04770 2003-12-09 devnull m->rx.type = Rerror;
323 ceb04770 2003-12-09 devnull m->rx.ename = ename;
324 ceb04770 2003-12-09 devnull m->rx.tag = m->tx.tag;
325 32f69c36 2003-12-11 devnull send9pmsg(m);
329 5c84c448 2005-03-21 devnull estrdup(char *s)
331 5c84c448 2005-03-21 devnull char *t;
333 5c84c448 2005-03-21 devnull t = emalloc(strlen(s)+1);
334 5c84c448 2005-03-21 devnull strcpy(t, s);
335 5c84c448 2005-03-21 devnull return t;
339 d3df3087 2003-12-06 devnull connthread(void *arg)
341 d3df3087 2003-12-06 devnull int i, fd;
342 d3df3087 2003-12-06 devnull Conn *c;
343 05b7f431 2004-03-02 devnull Hash *h, *hnext;
344 05b7f431 2004-03-02 devnull Msg *m, *om, *mm;
346 ceb04770 2003-12-09 devnull Ioproc *io;
348 d3df3087 2003-12-06 devnull c = arg;
349 7ffc5208 2004-12-28 devnull threadsetname("conn %s", c->dir);
350 ceb04770 2003-12-09 devnull io = ioproc();
351 ceb04770 2003-12-09 devnull fd = ioaccept(io, c->fd, c->dir);
352 d3df3087 2003-12-06 devnull if(fd < 0){
353 e2a1725d 2005-01-04 devnull if(verbose) fprint(2, "%T accept %s: %r\n", c->dir);
354 d3df3087 2003-12-06 devnull goto out;
356 d3df3087 2003-12-06 devnull close(c->fd);
357 d3df3087 2003-12-06 devnull c->fd = fd;
358 ceb04770 2003-12-09 devnull threadcreate(connoutthread, c, STACK);
359 215993f8 2005-09-13 devnull while((m = mread9p(io, c->fd, c->dotu)) != nil){
360 e2a1725d 2005-01-04 devnull if(verbose > 1) fprint(2, "%T fd#%d -> %F\n", c->fd, &m->tx);
361 d3df3087 2003-12-06 devnull m->c = c;
362 ceb04770 2003-12-09 devnull m->ctag = m->tx.tag;
363 d3df3087 2003-12-06 devnull c->nmsg++;
364 e2a1725d 2005-01-04 devnull if(verbose > 1) fprint(2, "%T fd#%d: new msg %p\n", c->fd, m);
365 d3df3087 2003-12-06 devnull if(puthash(c->tag, m->tx.tag, m) < 0){
366 d3df3087 2003-12-06 devnull err(m, "duplicate tag");
367 d3df3087 2003-12-06 devnull continue;
369 e2a1725d 2005-01-04 devnull msgincref(m);
370 d3df3087 2003-12-06 devnull switch(m->tx.type){
371 ceb04770 2003-12-09 devnull case Tversion:
372 ceb04770 2003-12-09 devnull m->rx.tag = m->tx.tag;
373 ceb04770 2003-12-09 devnull m->rx.msize = m->tx.msize;
374 5a8e63b2 2004-02-29 devnull if(m->rx.msize > msize)
375 5a8e63b2 2004-02-29 devnull m->rx.msize = msize;
376 ceb04770 2003-12-09 devnull m->rx.version = "9P2000";
377 215993f8 2005-09-13 devnull c->dotu = 0;
378 215993f8 2005-09-13 devnull if(dotu && strncmp(m->tx.version, "9P2000.u", 8) == 0){
379 215993f8 2005-09-13 devnull m->rx.version = "9P2000.u";
380 215993f8 2005-09-13 devnull c->dotu = 1;
382 ceb04770 2003-12-09 devnull m->rx.type = Rversion;
383 32f69c36 2003-12-11 devnull send9pmsg(m);
384 ceb04770 2003-12-09 devnull continue;
385 d3df3087 2003-12-06 devnull case Tflush:
386 d3df3087 2003-12-06 devnull if((m->oldm = gethash(c->tag, m->tx.oldtag)) == nil){
387 ceb04770 2003-12-09 devnull m->rx.tag = m->tx.tag;
388 ceb04770 2003-12-09 devnull m->rx.type = Rflush;
389 32f69c36 2003-12-11 devnull send9pmsg(m);
390 d3df3087 2003-12-06 devnull continue;
392 e2a1725d 2005-01-04 devnull msgincref(m->oldm);
394 d3df3087 2003-12-06 devnull case Tattach:
395 ceb04770 2003-12-09 devnull m->afid = nil;
396 ceb04770 2003-12-09 devnull if(m->tx.afid != NOFID
397 ceb04770 2003-12-09 devnull && (m->afid = gethash(c->fid, m->tx.afid)) == nil){
398 ceb04770 2003-12-09 devnull err(m, "unknown fid");
399 ceb04770 2003-12-09 devnull continue;
401 3a6f92ee 2005-03-18 devnull if(m->afid)
402 3a6f92ee 2005-03-18 devnull m->afid->ref++;
403 d3df3087 2003-12-06 devnull m->fid = fidnew(m->tx.fid);
404 d3df3087 2003-12-06 devnull if(puthash(c->fid, m->tx.fid, m->fid) < 0){
405 d3df3087 2003-12-06 devnull err(m, "duplicate fid");
406 d3df3087 2003-12-06 devnull continue;
408 d3df3087 2003-12-06 devnull m->fid->ref++;
409 5c84c448 2005-03-21 devnull if(attached && m->afid==nil){
410 5c84c448 2005-03-21 devnull if(m->tx.aname[0] && strcmp(xaname, m->tx.aname) != 0){
411 5c84c448 2005-03-21 devnull err(m, "invalid attach name");
412 5c84c448 2005-03-21 devnull continue;
414 5c84c448 2005-03-21 devnull m->tx.afid = xafid;
415 5c84c448 2005-03-21 devnull m->tx.aname = xaname;
416 5c84c448 2005-03-21 devnull m->tx.uname = estrdup(m->tx.uname);
417 215993f8 2005-09-13 devnull repack(&m->tx, &m->tpkt, c->dotu);
418 5c84c448 2005-03-21 devnull free(m->tx.uname);
419 5c84c448 2005-03-21 devnull m->tx.uname = "XXX";
422 d3df3087 2003-12-06 devnull case Twalk:
423 d3df3087 2003-12-06 devnull if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
424 d3df3087 2003-12-06 devnull err(m, "unknown fid");
425 d3df3087 2003-12-06 devnull continue;
427 ceb04770 2003-12-09 devnull m->fid->ref++;
428 d3df3087 2003-12-06 devnull if(m->tx.newfid == m->tx.fid){
429 d3df3087 2003-12-06 devnull m->fid->ref++;
430 d3df3087 2003-12-06 devnull m->newfid = m->fid;
432 d3df3087 2003-12-06 devnull m->newfid = fidnew(m->tx.newfid);
433 d3df3087 2003-12-06 devnull if(puthash(c->fid, m->tx.newfid, m->newfid) < 0){
434 d3df3087 2003-12-06 devnull err(m, "duplicate fid");
435 d3df3087 2003-12-06 devnull continue;
437 d3df3087 2003-12-06 devnull m->newfid->ref++;
440 d3df3087 2003-12-06 devnull case Tauth:
441 5c84c448 2005-03-21 devnull if(attached){
442 5c84c448 2005-03-21 devnull err(m, "authentication not required");
443 5c84c448 2005-03-21 devnull continue;
445 ceb04770 2003-12-09 devnull m->afid = fidnew(m->tx.afid);
446 ceb04770 2003-12-09 devnull if(puthash(c->fid, m->tx.afid, m->afid) < 0){
447 d3df3087 2003-12-06 devnull err(m, "duplicate fid");
448 d3df3087 2003-12-06 devnull continue;
450 ceb04770 2003-12-09 devnull m->afid->ref++;
452 b5c7be2f 2006-03-03 devnull case Tcreate:
453 b5c7be2f 2006-03-03 devnull if(dotu && !c->dotu && (m->tx.perm&(DMSYMLINK|DMDEVICE|DMNAMEDPIPE|DMSOCKET))){
454 b5c7be2f 2006-03-03 devnull err(m, "unsupported file type");
455 b5c7be2f 2006-03-03 devnull continue;
457 b5c7be2f 2006-03-03 devnull goto caseTopen;
458 32f69c36 2003-12-11 devnull case Topenfd:
459 49588d5d 2003-12-17 devnull if(m->tx.mode&~(OTRUNC|3)){
460 49588d5d 2003-12-17 devnull err(m, "bad openfd mode");
461 32f69c36 2003-12-11 devnull continue;
463 32f69c36 2003-12-11 devnull m->isopenfd = 1;
464 32f69c36 2003-12-11 devnull m->tx.type = Topen;
465 32f69c36 2003-12-11 devnull m->tpkt[4] = Topen;
466 32f69c36 2003-12-11 devnull /* fall through */
467 b5c7be2f 2006-03-03 devnull caseTopen:
468 d3df3087 2003-12-06 devnull case Topen:
469 d3df3087 2003-12-06 devnull case Tclunk:
470 d3df3087 2003-12-06 devnull case Tread:
471 d3df3087 2003-12-06 devnull case Twrite:
472 ceb04770 2003-12-09 devnull case Tremove:
473 d3df3087 2003-12-06 devnull case Tstat:
474 d3df3087 2003-12-06 devnull case Twstat:
475 d3df3087 2003-12-06 devnull if((m->fid = gethash(c->fid, m->tx.fid)) == nil){
476 d3df3087 2003-12-06 devnull err(m, "unknown fid");
477 d3df3087 2003-12-06 devnull continue;
479 d3df3087 2003-12-06 devnull m->fid->ref++;
480 215993f8 2005-09-13 devnull if(m->tx.type==Twstat && dotu && !c->dotu){
481 16c1defa 2005-11-12 devnull if(cvtustat(&m->tx, &m->tpkt, 1) < 0){
482 215993f8 2005-09-13 devnull err(m, "cannot convert stat buffer");
483 215993f8 2005-09-13 devnull continue;
486 9777187b 2006-02-03 devnull if(m->tx.type==Tread && m->fid->isdir && dotu && !c->dotu){
487 9777187b 2006-02-03 devnull if(m->tx.offset = m->fid->coffset)
488 9777187b 2006-02-03 devnull m->tx.offset = m->fid->offset;
490 9777187b 2006-02-03 devnull m->fid->offset = m->fid->coffset;
495 d3df3087 2003-12-06 devnull /* have everything - translate and send */
496 d3df3087 2003-12-06 devnull m->c = c;
497 d3df3087 2003-12-06 devnull m->ctag = m->tx.tag;
498 d3df3087 2003-12-06 devnull m->tx.tag = m->tag;
499 d3df3087 2003-12-06 devnull if(m->fid)
500 d3df3087 2003-12-06 devnull m->tx.fid = m->fid->fid;
501 d3df3087 2003-12-06 devnull if(m->newfid)
502 d3df3087 2003-12-06 devnull m->tx.newfid = m->newfid->fid;
503 d3df3087 2003-12-06 devnull if(m->afid)
504 d3df3087 2003-12-06 devnull m->tx.afid = m->afid->fid;
505 d3df3087 2003-12-06 devnull if(m->oldm)
506 d3df3087 2003-12-06 devnull m->tx.oldtag = m->oldm->tag;
507 ceb04770 2003-12-09 devnull /* reference passes to outq */
508 d3df3087 2003-12-06 devnull sendq(outq, m);
509 d3df3087 2003-12-06 devnull while(c->nmsg >= MAXMSG){
510 d3df3087 2003-12-06 devnull c->inputstalled = 1;
511 d3df3087 2003-12-06 devnull recvp(c->inc);
515 e2a1725d 2005-01-04 devnull if(verbose) fprint(2, "%T fd#%d eof; flushing conn\n", c->fd);
517 05b7f431 2004-03-02 devnull /* flush the output queue */
518 05b7f431 2004-03-02 devnull sendq(c->outq, nil);
519 05b7f431 2004-03-02 devnull while(c->outq != nil)
520 05b7f431 2004-03-02 devnull yield();
522 d3df3087 2003-12-06 devnull /* flush all outstanding messages */
523 d3df3087 2003-12-06 devnull for(i=0; i<NHASH; i++){
524 05b7f431 2004-03-02 devnull for(h=c->tag[i]; h; h=hnext){
525 d3df3087 2003-12-06 devnull om = h->v;
526 e2a1725d 2005-01-04 devnull m = msgnew(0);
527 d3df3087 2003-12-06 devnull m->internal = 1;
528 d3df3087 2003-12-06 devnull m->c = c;
529 32f69c36 2003-12-11 devnull c->nmsg++;
530 d3df3087 2003-12-06 devnull m->tx.type = Tflush;
531 d3df3087 2003-12-06 devnull m->tx.tag = m->tag;
532 d3df3087 2003-12-06 devnull m->tx.oldtag = om->tag;
533 d3df3087 2003-12-06 devnull m->oldm = om;
534 e2a1725d 2005-01-04 devnull msgincref(om);
535 e2a1725d 2005-01-04 devnull msgincref(m); /* for outq */
536 ceb04770 2003-12-09 devnull sendomsg(m);
537 05b7f431 2004-03-02 devnull mm = recvp(c->internal);
538 05b7f431 2004-03-02 devnull assert(mm == m);
539 32f69c36 2003-12-11 devnull msgput(m); /* got from recvp */
540 32f69c36 2003-12-11 devnull msgput(m); /* got from msgnew */
541 32f69c36 2003-12-11 devnull msgput(om); /* got from hash table */
542 05b7f431 2004-03-02 devnull hnext = h->next;
543 05b7f431 2004-03-02 devnull free(h);
547 d3df3087 2003-12-06 devnull /* clunk all outstanding fids */
548 d3df3087 2003-12-06 devnull for(i=0; i<NHASH; i++){
549 05b7f431 2004-03-02 devnull for(h=c->fid[i]; h; h=hnext){
550 d3df3087 2003-12-06 devnull f = h->v;
551 e2a1725d 2005-01-04 devnull m = msgnew(0);
552 d3df3087 2003-12-06 devnull m->internal = 1;
553 d3df3087 2003-12-06 devnull m->c = c;
554 32f69c36 2003-12-11 devnull c->nmsg++;
555 d3df3087 2003-12-06 devnull m->tx.type = Tclunk;
556 d3df3087 2003-12-06 devnull m->tx.tag = m->tag;
557 d3df3087 2003-12-06 devnull m->tx.fid = f->fid;
558 d3df3087 2003-12-06 devnull m->fid = f;
559 d3df3087 2003-12-06 devnull f->ref++;
560 e2a1725d 2005-01-04 devnull msgincref(m);
561 ceb04770 2003-12-09 devnull sendomsg(m);
562 05b7f431 2004-03-02 devnull mm = recvp(c->internal);
563 05b7f431 2004-03-02 devnull assert(mm == m);
564 e2a1725d 2005-01-04 devnull msgclear(m);
565 32f69c36 2003-12-11 devnull msgput(m); /* got from recvp */
566 32f69c36 2003-12-11 devnull msgput(m); /* got from msgnew */
567 32f69c36 2003-12-11 devnull fidput(f); /* got from hash table */
568 05b7f431 2004-03-02 devnull hnext = h->next;
569 05b7f431 2004-03-02 devnull free(h);
574 e2a1725d 2005-01-04 devnull closeioproc(io);
575 d3df3087 2003-12-06 devnull assert(c->nmsg == 0);
576 d3df3087 2003-12-06 devnull assert(c->nfid == 0);
577 d3df3087 2003-12-06 devnull close(c->fd);
578 32f69c36 2003-12-11 devnull chanfree(c->internal);
579 32f69c36 2003-12-11 devnull c->internal = 0;
580 32f69c36 2003-12-11 devnull chanfree(c->inc);
581 32f69c36 2003-12-11 devnull c->inc = 0;
582 32f69c36 2003-12-11 devnull free(c->inq);
583 32f69c36 2003-12-11 devnull c->inq = 0;
584 d3df3087 2003-12-06 devnull free(c);
587 32f69c36 2003-12-11 devnull static void
588 32f69c36 2003-12-11 devnull openfdthread(void *v)
590 32f69c36 2003-12-11 devnull Conn *c;
591 32f69c36 2003-12-11 devnull Fid *fid;
594 32f69c36 2003-12-11 devnull vlong tot;
595 32f69c36 2003-12-11 devnull Ioproc *io;
596 32f69c36 2003-12-11 devnull char buf[1024];
599 32f69c36 2003-12-11 devnull fid = c->fdfid;
600 32f69c36 2003-12-11 devnull io = ioproc();
601 7ffc5208 2004-12-28 devnull threadsetname("openfd %s", c->fdfid);
602 32f69c36 2003-12-11 devnull tot = 0;
603 05b7f431 2004-03-02 devnull m = nil;
604 32f69c36 2003-12-11 devnull if(c->fdmode == OREAD){
605 32f69c36 2003-12-11 devnull for(;;){
606 e2a1725d 2005-01-04 devnull if(verbose) fprint(2, "%T tread...");
607 e2a1725d 2005-01-04 devnull m = msgnew(0);
608 32f69c36 2003-12-11 devnull m->internal = 1;
609 32f69c36 2003-12-11 devnull m->c = c;
610 32f69c36 2003-12-11 devnull m->tx.type = Tread;
611 5a8e63b2 2004-02-29 devnull m->tx.count = msize - IOHDRSZ;
612 32f69c36 2003-12-11 devnull m->tx.fid = fid->fid;
613 32f69c36 2003-12-11 devnull m->tx.tag = m->tag;
614 32f69c36 2003-12-11 devnull m->tx.offset = tot;
615 32f69c36 2003-12-11 devnull m->fid = fid;
616 32f69c36 2003-12-11 devnull fid->ref++;
617 e2a1725d 2005-01-04 devnull msgincref(m);
618 32f69c36 2003-12-11 devnull sendomsg(m);
619 32f69c36 2003-12-11 devnull recvp(c->internal);
620 49588d5d 2003-12-17 devnull if(m->rx.type == Rerror){
621 cbeb0b26 2006-04-01 devnull /* fprint(2, "%T read error: %s\n", m->rx.ename); */
624 32f69c36 2003-12-11 devnull if(m->rx.count == 0)
626 32f69c36 2003-12-11 devnull tot += m->rx.count;
627 49588d5d 2003-12-17 devnull if(iowrite(io, c->fd, m->rx.data, m->rx.count) != m->rx.count){
628 cbeb0b26 2006-04-01 devnull /* fprint(2, "%T pipe write error: %r\n"); */
631 32f69c36 2003-12-11 devnull msgput(m);
632 32f69c36 2003-12-11 devnull msgput(m);
633 05b7f431 2004-03-02 devnull m = nil;
636 32f69c36 2003-12-11 devnull for(;;){
637 e2a1725d 2005-01-04 devnull if(verbose) fprint(2, "%T twrite...");
638 5a8e63b2 2004-02-29 devnull n = sizeof buf;
639 5a8e63b2 2004-02-29 devnull if(n > msize)
640 5a8e63b2 2004-02-29 devnull n = msize;
641 5a8e63b2 2004-02-29 devnull if((n=ioread(io, c->fd, buf, n)) <= 0){
642 49588d5d 2003-12-17 devnull if(n < 0)
643 e2a1725d 2005-01-04 devnull fprint(2, "%T pipe read error: %r\n");
646 e2a1725d 2005-01-04 devnull m = msgnew(0);
647 32f69c36 2003-12-11 devnull m->internal = 1;
648 32f69c36 2003-12-11 devnull m->c = c;
649 32f69c36 2003-12-11 devnull m->tx.type = Twrite;
650 32f69c36 2003-12-11 devnull m->tx.fid = fid->fid;
651 32f69c36 2003-12-11 devnull m->tx.data = buf;
652 32f69c36 2003-12-11 devnull m->tx.count = n;
653 32f69c36 2003-12-11 devnull m->tx.tag = m->tag;
654 32f69c36 2003-12-11 devnull m->tx.offset = tot;
655 32f69c36 2003-12-11 devnull m->fid = fid;
656 32f69c36 2003-12-11 devnull fid->ref++;
657 e2a1725d 2005-01-04 devnull msgincref(m);
658 32f69c36 2003-12-11 devnull sendomsg(m);
659 32f69c36 2003-12-11 devnull recvp(c->internal);
660 49588d5d 2003-12-17 devnull if(m->rx.type == Rerror){
661 cbeb0b26 2006-04-01 devnull /* fprint(2, "%T write error: %s\n", m->rx.ename); */
663 05b7f431 2004-03-02 devnull tot += n;
664 32f69c36 2003-12-11 devnull msgput(m);
665 32f69c36 2003-12-11 devnull msgput(m);
666 05b7f431 2004-03-02 devnull m = nil;
669 e2a1725d 2005-01-04 devnull if(verbose) fprint(2, "%T eof on %d fid %d\n", c->fd, fid->fid);
670 32f69c36 2003-12-11 devnull close(c->fd);
671 32f69c36 2003-12-11 devnull closeioproc(io);
673 32f69c36 2003-12-11 devnull msgput(m);
674 32f69c36 2003-12-11 devnull msgput(m);
676 e2a1725d 2005-01-04 devnull if(verbose) fprint(2, "%T eof on %d fid %d ref %d\n", c->fd, fid->fid, fid->ref);
677 21e626de 2004-12-28 devnull if(--fid->openfd == 0){
678 e2a1725d 2005-01-04 devnull m = msgnew(0);
679 49588d5d 2003-12-17 devnull m->internal = 1;
680 49588d5d 2003-12-17 devnull m->c = c;
681 49588d5d 2003-12-17 devnull m->tx.type = Tclunk;
682 05b7f431 2004-03-02 devnull m->tx.tag = m->tag;
683 49588d5d 2003-12-17 devnull m->tx.fid = fid->fid;
684 49588d5d 2003-12-17 devnull m->fid = fid;
685 49588d5d 2003-12-17 devnull fid->ref++;
686 e2a1725d 2005-01-04 devnull msgincref(m);
687 49588d5d 2003-12-17 devnull sendomsg(m);
688 49588d5d 2003-12-17 devnull recvp(c->internal);
689 49588d5d 2003-12-17 devnull msgput(m);
690 49588d5d 2003-12-17 devnull msgput(m);
692 32f69c36 2003-12-11 devnull fidput(fid);
693 32f69c36 2003-12-11 devnull c->fdfid = nil;
694 32f69c36 2003-12-11 devnull chanfree(c->internal);
695 32f69c36 2003-12-11 devnull c->internal = 0;
696 32f69c36 2003-12-11 devnull free(c);
700 32f69c36 2003-12-11 devnull xopenfd(Msg *m)
702 32f69c36 2003-12-11 devnull char errs[ERRMAX];
703 32f69c36 2003-12-11 devnull int n, p[2];
704 32f69c36 2003-12-11 devnull Conn *nc;
706 32f69c36 2003-12-11 devnull if(pipe(p) < 0){
707 32f69c36 2003-12-11 devnull rerrstr(errs, sizeof errs);
708 32f69c36 2003-12-11 devnull err(m, errs);
709 21e626de 2004-12-28 devnull /* XXX return here? */
711 e2a1725d 2005-01-04 devnull if(verbose) fprint(2, "%T xopen pipe %d %d...", p[0], p[1]);
713 32f69c36 2003-12-11 devnull /* now we're committed. */
715 32f69c36 2003-12-11 devnull /* a new connection for this fid */
716 32f69c36 2003-12-11 devnull nc = emalloc(sizeof(Conn));
717 32f69c36 2003-12-11 devnull nc->internal = chancreate(sizeof(void*), 0);
719 32f69c36 2003-12-11 devnull /* a ref for us */
720 32f69c36 2003-12-11 devnull nc->fdfid = m->fid;
721 32f69c36 2003-12-11 devnull m->fid->ref++;
722 21e626de 2004-12-28 devnull nc->fdfid->openfd++;
723 32f69c36 2003-12-11 devnull nc->fdmode = m->tx.mode;
724 32f69c36 2003-12-11 devnull nc->fd = p[0];
726 32f69c36 2003-12-11 devnull /* a thread to tend the pipe */
727 32f69c36 2003-12-11 devnull threadcreate(openfdthread, nc, STACK);
729 49588d5d 2003-12-17 devnull /* if mode is ORDWR, that openfdthread will write; start a reader */
730 49588d5d 2003-12-17 devnull if((m->tx.mode&3) == ORDWR){
731 49588d5d 2003-12-17 devnull nc = emalloc(sizeof(Conn));
732 49588d5d 2003-12-17 devnull nc->internal = chancreate(sizeof(void*), 0);
733 49588d5d 2003-12-17 devnull nc->fdfid = m->fid;
734 49588d5d 2003-12-17 devnull m->fid->ref++;
735 21e626de 2004-12-28 devnull nc->fdfid->openfd++;
736 49588d5d 2003-12-17 devnull nc->fdmode = OREAD;
737 49588d5d 2003-12-17 devnull nc->fd = dup(p[0], -1);
738 49588d5d 2003-12-17 devnull threadcreate(openfdthread, nc, STACK);
741 49588d5d 2003-12-17 devnull /* steal fid from other connection */
742 49588d5d 2003-12-17 devnull if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
743 49588d5d 2003-12-17 devnull fidput(m->fid);
745 32f69c36 2003-12-11 devnull /* rewrite as Ropenfd */
746 32f69c36 2003-12-11 devnull m->rx.type = Ropenfd;
747 32f69c36 2003-12-11 devnull n = GBIT32(m->rpkt);
748 32f69c36 2003-12-11 devnull m->rpkt = erealloc(m->rpkt, n+4);
749 32f69c36 2003-12-11 devnull PBIT32(m->rpkt+n, p[1]);
751 32f69c36 2003-12-11 devnull PBIT32(m->rpkt, n);
752 32f69c36 2003-12-11 devnull m->rpkt[4] = Ropenfd;
753 32f69c36 2003-12-11 devnull m->rx.unixfd = p[1];
754 32f69c36 2003-12-11 devnull return 0;
758 d3df3087 2003-12-06 devnull connoutthread(void *arg)
760 215993f8 2005-09-13 devnull char *ename;
761 d3df3087 2003-12-06 devnull int err;
762 d3df3087 2003-12-06 devnull Conn *c;
763 05b7f431 2004-03-02 devnull Queue *outq;
764 d3df3087 2003-12-06 devnull Msg *m, *om;
765 ceb04770 2003-12-09 devnull Ioproc *io;
767 d3df3087 2003-12-06 devnull c = arg;
768 05b7f431 2004-03-02 devnull outq = c->outq;
769 ceb04770 2003-12-09 devnull io = ioproc();
770 7ffc5208 2004-12-28 devnull threadsetname("connout %s", c->dir);
771 05b7f431 2004-03-02 devnull while((m = recvq(outq)) != nil){
772 d3df3087 2003-12-06 devnull err = m->tx.type+1 != m->rx.type;
773 32f69c36 2003-12-11 devnull if(!err && m->isopenfd)
774 32f69c36 2003-12-11 devnull if(xopenfd(m) < 0)
775 32f69c36 2003-12-11 devnull continue;
776 d3df3087 2003-12-06 devnull switch(m->tx.type){
777 d3df3087 2003-12-06 devnull case Tflush:
778 d3df3087 2003-12-06 devnull om = m->oldm;
780 ceb04770 2003-12-09 devnull if(delhash(om->c->tag, om->ctag, om) == 0)
781 ceb04770 2003-12-09 devnull msgput(om);
783 d3df3087 2003-12-06 devnull case Tclunk:
784 ceb04770 2003-12-09 devnull case Tremove:
785 ceb04770 2003-12-09 devnull if(m->fid)
786 ceb04770 2003-12-09 devnull if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
787 ceb04770 2003-12-09 devnull fidput(m->fid);
789 d3df3087 2003-12-06 devnull case Tauth:
790 ceb04770 2003-12-09 devnull if(err && m->afid){
791 80568494 2005-02-11 devnull if(verbose) fprint(2, "%T auth error\n");
792 ceb04770 2003-12-09 devnull if(delhash(m->c->fid, m->afid->cfid, m->afid) == 0)
793 80568494 2005-02-11 devnull fidput(m->afid);
796 d3df3087 2003-12-06 devnull case Tattach:
797 ceb04770 2003-12-09 devnull if(err && m->fid)
798 d3df3087 2003-12-06 devnull if(delhash(m->c->fid, m->fid->cfid, m->fid) == 0)
799 d3df3087 2003-12-06 devnull fidput(m->fid);
801 d3df3087 2003-12-06 devnull case Twalk:
802 5c84c448 2005-03-21 devnull if(err || m->rx.nwqid < m->tx.nwname)
803 5c84c448 2005-03-21 devnull if(m->tx.fid != m->tx.newfid && m->newfid)
804 d3df3087 2003-12-06 devnull if(delhash(m->c->fid, m->newfid->cfid, m->newfid) == 0)
805 d3df3087 2003-12-06 devnull fidput(m->newfid);
807 215993f8 2005-09-13 devnull case Tread:
808 9777187b 2006-02-03 devnull if(!err && m->fid->isdir && dotu && !m->c->dotu){
809 9777187b 2006-02-03 devnull m->fid->offset += m->rx.count;
810 215993f8 2005-09-13 devnull stripudirread(m);
811 9777187b 2006-02-03 devnull m->fid->coffset += m->rx.count;
814 215993f8 2005-09-13 devnull case Tstat:
815 215993f8 2005-09-13 devnull if(!err && dotu && !m->c->dotu)
816 16c1defa 2005-11-12 devnull cvtustat(&m->rx, &m->rpkt, 0);
818 215993f8 2005-09-13 devnull case Topen:
819 215993f8 2005-09-13 devnull case Tcreate:
820 215993f8 2005-09-13 devnull m->fid->isdir = (m->rx.qid.type & QTDIR);
823 215993f8 2005-09-13 devnull if(m->rx.type==Rerror && dotu && !c->dotu){
824 215993f8 2005-09-13 devnull ename = estrdup(m->rx.ename);
825 215993f8 2005-09-13 devnull m->rx.ename = ename;
826 215993f8 2005-09-13 devnull repack(&m->rx, &m->rpkt, c->dotu);
827 215993f8 2005-09-13 devnull free(ename);
828 16c1defa 2005-11-12 devnull m->rx.ename = "XXX";
830 d3df3087 2003-12-06 devnull if(delhash(m->c->tag, m->ctag, m) == 0)
831 d3df3087 2003-12-06 devnull msgput(m);
832 e2a1725d 2005-01-04 devnull if(verbose > 1) fprint(2, "%T fd#%d <- %F\n", c->fd, &m->rx);
833 ceb04770 2003-12-09 devnull rewritehdr(&m->rx, m->rpkt);
834 ceb04770 2003-12-09 devnull if(mwrite9p(io, c->fd, m->rpkt) < 0)
835 e2a1725d 2005-01-04 devnull if(verbose) fprint(2, "%T write error: %r\n");
836 d3df3087 2003-12-06 devnull msgput(m);
837 d3df3087 2003-12-06 devnull if(c->inputstalled && c->nmsg < MAXMSG)
838 d3df3087 2003-12-06 devnull nbsendp(c->inc, 0);
840 ceb04770 2003-12-09 devnull closeioproc(io);
841 05b7f431 2004-03-02 devnull free(outq);
842 05b7f431 2004-03-02 devnull c->outq = nil;
846 d3df3087 2003-12-06 devnull outputthread(void *arg)
849 ceb04770 2003-12-09 devnull Ioproc *io;
851 d3df3087 2003-12-06 devnull USED(arg);
852 ceb04770 2003-12-09 devnull io = ioproc();
853 7ffc5208 2004-12-28 devnull threadsetname("output");
854 d3df3087 2003-12-06 devnull while((m = recvq(outq)) != nil){
855 e2a1725d 2005-01-04 devnull if(verbose > 1) fprint(2, "%T * <- %F\n", &m->tx);
856 ceb04770 2003-12-09 devnull rewritehdr(&m->tx, m->tpkt);
857 ceb04770 2003-12-09 devnull if(mwrite9p(io, 1, m->tpkt) < 0)
858 d3df3087 2003-12-06 devnull sysfatal("output error: %r");
859 d3df3087 2003-12-06 devnull msgput(m);
861 ceb04770 2003-12-09 devnull closeioproc(io);
862 e2a1725d 2005-01-04 devnull fprint(2, "%T output eof\n");
863 32f69c36 2003-12-11 devnull threadexitsall(0);
867 d3df3087 2003-12-06 devnull inputthread(void *arg)
869 d3df3087 2003-12-06 devnull uchar *pkt;
870 d3df3087 2003-12-06 devnull int n, nn, tag;
872 ceb04770 2003-12-09 devnull Ioproc *io;
874 7ffc5208 2004-12-28 devnull threadsetname("input");
875 e2a1725d 2005-01-04 devnull if(verbose) fprint(2, "%T input thread\n");
876 ceb04770 2003-12-09 devnull io = ioproc();
877 ceb04770 2003-12-09 devnull USED(arg);
878 ceb04770 2003-12-09 devnull while((pkt = read9ppkt(io, 0)) != nil){
879 d3df3087 2003-12-06 devnull n = GBIT32(pkt);
880 d3df3087 2003-12-06 devnull if(n < 7){
881 e2a1725d 2005-01-04 devnull fprint(2, "%T short 9P packet from server\n");
882 d3df3087 2003-12-06 devnull free(pkt);
883 d3df3087 2003-12-06 devnull continue;
885 e2a1725d 2005-01-04 devnull if(verbose > 2) fprint(2, "%T read %.*H\n", n, pkt);
886 d3df3087 2003-12-06 devnull tag = GBIT16(pkt+5);
887 d3df3087 2003-12-06 devnull if((m = msgget(tag)) == nil){
888 e2a1725d 2005-01-04 devnull fprint(2, "%T unexpected 9P response tag %d\n", tag);
889 d3df3087 2003-12-06 devnull free(pkt);
890 d3df3087 2003-12-06 devnull continue;
892 215993f8 2005-09-13 devnull if((nn = convM2Su(pkt, n, &m->rx, dotu)) != n){
893 e2a1725d 2005-01-04 devnull fprint(2, "%T bad packet - convM2S %d but %d\n", nn, n);
894 d3df3087 2003-12-06 devnull free(pkt);
895 d3df3087 2003-12-06 devnull msgput(m);
896 d3df3087 2003-12-06 devnull continue;
898 e2a1725d 2005-01-04 devnull if(verbose > 1) fprint(2, "%T * -> %F%s\n", &m->rx,
899 05b7f431 2004-03-02 devnull m->internal ? " (internal)" : "");
900 d3df3087 2003-12-06 devnull m->rpkt = pkt;
901 d3df3087 2003-12-06 devnull m->rx.tag = m->ctag;
902 ceb04770 2003-12-09 devnull if(m->internal)
903 05b7f431 2004-03-02 devnull sendp(m->c->internal, m);
904 05b7f431 2004-03-02 devnull else if(m->c->outq)
905 ceb04770 2003-12-09 devnull sendq(m->c->outq, m);
907 05b7f431 2004-03-02 devnull msgput(m);
909 ceb04770 2003-12-09 devnull closeioproc(io);
910 cbeb0b26 2006-04-01 devnull /*fprint(2, "%T input eof\n"); */
911 32f69c36 2003-12-11 devnull threadexitsall(0);
915 d3df3087 2003-12-06 devnull gethash(Hash **ht, uint n)
917 d3df3087 2003-12-06 devnull Hash *h;
919 d3df3087 2003-12-06 devnull for(h=ht[n%NHASH]; h; h=h->next)
920 d3df3087 2003-12-06 devnull if(h->n == n)
921 d3df3087 2003-12-06 devnull return h->v;
922 d3df3087 2003-12-06 devnull return nil;
926 d3df3087 2003-12-06 devnull delhash(Hash **ht, uint n, void *v)
928 d3df3087 2003-12-06 devnull Hash *h, **l;
930 d3df3087 2003-12-06 devnull for(l=&ht[n%NHASH]; h=*l; l=&h->next)
931 d3df3087 2003-12-06 devnull if(h->n == n){
932 ceb04770 2003-12-09 devnull if(h->v != v){
933 e2a1725d 2005-01-04 devnull if(verbose) fprint(2, "%T delhash %d got %p want %p\n", n, h->v, v);
934 ceb04770 2003-12-09 devnull return -1;
936 d3df3087 2003-12-06 devnull *l = h->next;
937 d3df3087 2003-12-06 devnull free(h);
938 d3df3087 2003-12-06 devnull return 0;
940 d3df3087 2003-12-06 devnull return -1;
944 d3df3087 2003-12-06 devnull puthash(Hash **ht, uint n, void *v)
946 d3df3087 2003-12-06 devnull Hash *h;
948 d3df3087 2003-12-06 devnull if(gethash(ht, n))
949 d3df3087 2003-12-06 devnull return -1;
950 d3df3087 2003-12-06 devnull h = emalloc(sizeof(Hash));
951 d3df3087 2003-12-06 devnull h->next = ht[n%NHASH];
952 d3df3087 2003-12-06 devnull h->n = n;
953 d3df3087 2003-12-06 devnull h->v = v;
954 d3df3087 2003-12-06 devnull ht[n%NHASH] = h;
955 d3df3087 2003-12-06 devnull return 0;
958 d3df3087 2003-12-06 devnull Fid **fidtab;
959 d3df3087 2003-12-06 devnull int nfidtab;
960 d3df3087 2003-12-06 devnull Fid *freefid;
963 d3df3087 2003-12-06 devnull fidnew(int cfid)
967 d3df3087 2003-12-06 devnull if(freefid == nil){
968 ceb04770 2003-12-09 devnull fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
969 5c84c448 2005-03-21 devnull if(nfidtab == xafid){
970 5c84c448 2005-03-21 devnull fidtab[nfidtab++] = nil;
971 5c84c448 2005-03-21 devnull fidtab = erealloc(fidtab, (nfidtab+1)*sizeof(fidtab[0]));
973 d3df3087 2003-12-06 devnull fidtab[nfidtab] = emalloc(sizeof(Fid));
974 ceb04770 2003-12-09 devnull freefid = fidtab[nfidtab];
975 ceb04770 2003-12-09 devnull freefid->fid = nfidtab++;
977 d3df3087 2003-12-06 devnull f = freefid;
978 d3df3087 2003-12-06 devnull freefid = f->next;
979 ceb04770 2003-12-09 devnull f->cfid = cfid;
980 d3df3087 2003-12-06 devnull f->ref = 1;
981 9777187b 2006-02-03 devnull f->offset = 0;
982 9777187b 2006-02-03 devnull f->coffset = 0;
983 215993f8 2005-09-13 devnull f->isdir = -1;
984 d3df3087 2003-12-06 devnull return f;
988 d3df3087 2003-12-06 devnull fidput(Fid *f)
990 ceb04770 2003-12-09 devnull if(f == nil)
992 d3df3087 2003-12-06 devnull assert(f->ref > 0);
993 d3df3087 2003-12-06 devnull if(--f->ref > 0)
995 d3df3087 2003-12-06 devnull f->next = freefid;
996 d3df3087 2003-12-06 devnull f->cfid = -1;
997 d3df3087 2003-12-06 devnull freefid = f;
1000 d3df3087 2003-12-06 devnull Msg **msgtab;
1001 d3df3087 2003-12-06 devnull int nmsgtab;
1002 16c1defa 2005-11-12 devnull int nmsg;
1003 d3df3087 2003-12-06 devnull Msg *freemsg;
1006 e2a1725d 2005-01-04 devnull msgincref(Msg *m)
1008 e2a1725d 2005-01-04 devnull if(verbose > 1) fprint(2, "%T msgincref @0x%lux %p tag %d/%d ref %d=>%d\n",
1009 e2a1725d 2005-01-04 devnull getcallerpc(&m), m, m->tag, m->ctag, m->ref, m->ref+1);
1010 e2a1725d 2005-01-04 devnull m->ref++;
1014 e2a1725d 2005-01-04 devnull msgnew(int x)
1016 d3df3087 2003-12-06 devnull Msg *m;
1018 d3df3087 2003-12-06 devnull if(freemsg == nil){
1019 ceb04770 2003-12-09 devnull msgtab = erealloc(msgtab, (nmsgtab+1)*sizeof(msgtab[0]));
1020 d3df3087 2003-12-06 devnull msgtab[nmsgtab] = emalloc(sizeof(Msg));
1021 ceb04770 2003-12-09 devnull freemsg = msgtab[nmsgtab];
1022 ceb04770 2003-12-09 devnull freemsg->tag = nmsgtab++;
1024 d3df3087 2003-12-06 devnull m = freemsg;
1025 d3df3087 2003-12-06 devnull freemsg = m->next;
1026 d3df3087 2003-12-06 devnull m->ref = 1;
1027 e2a1725d 2005-01-04 devnull if(verbose > 1) fprint(2, "%T msgnew @0x%lux %p tag %d ref %d\n",
1028 e2a1725d 2005-01-04 devnull getcallerpc(&x), m, m->tag, m->ref);
1029 16c1defa 2005-11-12 devnull nmsg++;
1030 d3df3087 2003-12-06 devnull return m;
1034 e2a1725d 2005-01-04 devnull * Clear data associated with connections, so that
1035 e2a1725d 2005-01-04 devnull * if all msgs have been msgcleared, the connection
1036 e2a1725d 2005-01-04 devnull * can be freed. Note that this does *not* free the tpkt
1037 e2a1725d 2005-01-04 devnull * and rpkt; they are freed in msgput with the msg itself.
1038 e2a1725d 2005-01-04 devnull * The io write thread might still be holding a ref to msg
1039 e2a1725d 2005-01-04 devnull * even once the connection has finished with it.
1042 e2a1725d 2005-01-04 devnull msgclear(Msg *m)
1044 e2a1725d 2005-01-04 devnull if(m->c){
1045 e2a1725d 2005-01-04 devnull m->c->nmsg--;
1046 e2a1725d 2005-01-04 devnull m->c = nil;
1048 e2a1725d 2005-01-04 devnull if(m->oldm){
1049 e2a1725d 2005-01-04 devnull msgput(m->oldm);
1050 e2a1725d 2005-01-04 devnull m->oldm = nil;
1052 e2a1725d 2005-01-04 devnull if(m->fid){
1053 e2a1725d 2005-01-04 devnull fidput(m->fid);
1054 e2a1725d 2005-01-04 devnull m->fid = nil;
1056 e2a1725d 2005-01-04 devnull if(m->afid){
1057 e2a1725d 2005-01-04 devnull fidput(m->afid);
1058 e2a1725d 2005-01-04 devnull m->afid = nil;
1060 e2a1725d 2005-01-04 devnull if(m->newfid){
1061 e2a1725d 2005-01-04 devnull fidput(m->newfid);
1062 e2a1725d 2005-01-04 devnull m->newfid = nil;
1064 e2a1725d 2005-01-04 devnull if(m->rx.type == Ropenfd && m->rx.unixfd >= 0){
1065 e2a1725d 2005-01-04 devnull close(m->rx.unixfd);
1066 e2a1725d 2005-01-04 devnull m->rx.unixfd = -1;
1071 d3df3087 2003-12-06 devnull msgput(Msg *m)
1073 05b7f431 2004-03-02 devnull if(m == nil)
1074 05b7f431 2004-03-02 devnull return;
1076 e2a1725d 2005-01-04 devnull if(verbose > 1) fprint(2, "%T msgput 0x%lux %p tag %d/%d ref %d\n",
1077 e2a1725d 2005-01-04 devnull getcallerpc(&m), m, m->tag, m->ctag, m->ref);
1078 d3df3087 2003-12-06 devnull assert(m->ref > 0);
1079 d3df3087 2003-12-06 devnull if(--m->ref > 0)
1080 d3df3087 2003-12-06 devnull return;
1081 16c1defa 2005-11-12 devnull nmsg--;
1082 e2a1725d 2005-01-04 devnull msgclear(m);
1083 e2a1725d 2005-01-04 devnull if(m->tpkt){
1084 e2a1725d 2005-01-04 devnull free(m->tpkt);
1085 e2a1725d 2005-01-04 devnull m->tpkt = nil;
1087 e2a1725d 2005-01-04 devnull if(m->rpkt){
1088 e2a1725d 2005-01-04 devnull free(m->rpkt);
1089 e2a1725d 2005-01-04 devnull m->rpkt = nil;
1091 32f69c36 2003-12-11 devnull m->isopenfd = 0;
1092 32f69c36 2003-12-11 devnull m->internal = 0;
1093 d3df3087 2003-12-06 devnull m->next = freemsg;
1094 d3df3087 2003-12-06 devnull freemsg = m;
1098 ceb04770 2003-12-09 devnull msgget(int n)
1100 ceb04770 2003-12-09 devnull Msg *m;
1102 ceb04770 2003-12-09 devnull if(n < 0 || n >= nmsgtab)
1103 ceb04770 2003-12-09 devnull return nil;
1104 ceb04770 2003-12-09 devnull m = msgtab[n];
1105 ceb04770 2003-12-09 devnull if(m->ref == 0)
1106 ceb04770 2003-12-09 devnull return nil;
1107 e2a1725d 2005-01-04 devnull if(verbose) fprint(2, "%T msgget %d = %p\n", n, m);
1108 e2a1725d 2005-01-04 devnull msgincref(m);
1109 ceb04770 2003-12-09 devnull return m;
1114 d3df3087 2003-12-06 devnull emalloc(int n)
1116 d3df3087 2003-12-06 devnull void *v;
1118 d3df3087 2003-12-06 devnull v = mallocz(n, 1);
1119 ceb04770 2003-12-09 devnull if(v == nil){
1120 ceb04770 2003-12-09 devnull abort();
1121 ceb04770 2003-12-09 devnull sysfatal("out of memory allocating %d", n);
1123 d3df3087 2003-12-06 devnull return v;
1127 d3df3087 2003-12-06 devnull erealloc(void *v, int n)
1129 d3df3087 2003-12-06 devnull v = realloc(v, n);
1130 ceb04770 2003-12-09 devnull if(v == nil){
1131 ceb04770 2003-12-09 devnull abort();
1132 ceb04770 2003-12-09 devnull sysfatal("out of memory reallocating %d", n);
1134 d3df3087 2003-12-06 devnull return v;
1137 d3df3087 2003-12-06 devnull typedef struct Qel Qel;
1138 d3df3087 2003-12-06 devnull struct Qel
1140 d3df3087 2003-12-06 devnull Qel *next;
1141 d3df3087 2003-12-06 devnull void *p;
1144 d3df3087 2003-12-06 devnull struct Queue
1146 d3df3087 2003-12-06 devnull int hungup;
1147 d3df3087 2003-12-06 devnull QLock lk;
1148 d3df3087 2003-12-06 devnull Rendez r;
1149 d3df3087 2003-12-06 devnull Qel *head;
1150 d3df3087 2003-12-06 devnull Qel *tail;
1154 d3df3087 2003-12-06 devnull qalloc(void)
1156 d3df3087 2003-12-06 devnull Queue *q;
1158 d3df3087 2003-12-06 devnull q = mallocz(sizeof(Queue), 1);
1159 d3df3087 2003-12-06 devnull if(q == nil)
1160 d3df3087 2003-12-06 devnull return nil;
1161 d3df3087 2003-12-06 devnull q->r.l = &q->lk;
1162 d3df3087 2003-12-06 devnull return q;
1166 d3df3087 2003-12-06 devnull sendq(Queue *q, void *p)
1168 d3df3087 2003-12-06 devnull Qel *e;
1170 d3df3087 2003-12-06 devnull e = emalloc(sizeof(Qel));
1171 d3df3087 2003-12-06 devnull qlock(&q->lk);
1172 d3df3087 2003-12-06 devnull if(q->hungup){
1173 16c1defa 2005-11-12 devnull free(e);
1174 d3df3087 2003-12-06 devnull werrstr("hungup queue");
1175 d3df3087 2003-12-06 devnull qunlock(&q->lk);
1176 d3df3087 2003-12-06 devnull return -1;
1178 d3df3087 2003-12-06 devnull e->p = p;
1179 d3df3087 2003-12-06 devnull e->next = nil;
1180 d3df3087 2003-12-06 devnull if(q->head == nil)
1181 d3df3087 2003-12-06 devnull q->head = e;
1183 d3df3087 2003-12-06 devnull q->tail->next = e;
1184 d3df3087 2003-12-06 devnull q->tail = e;
1185 d3df3087 2003-12-06 devnull rwakeup(&q->r);
1186 d3df3087 2003-12-06 devnull qunlock(&q->lk);
1187 d3df3087 2003-12-06 devnull return 0;
1191 d3df3087 2003-12-06 devnull recvq(Queue *q)
1193 d3df3087 2003-12-06 devnull void *p;
1194 d3df3087 2003-12-06 devnull Qel *e;
1196 d3df3087 2003-12-06 devnull qlock(&q->lk);
1197 d3df3087 2003-12-06 devnull while(q->head == nil && !q->hungup)
1198 d3df3087 2003-12-06 devnull rsleep(&q->r);
1199 d3df3087 2003-12-06 devnull if(q->hungup){
1200 d3df3087 2003-12-06 devnull qunlock(&q->lk);
1201 d3df3087 2003-12-06 devnull return nil;
1203 d3df3087 2003-12-06 devnull e = q->head;
1204 d3df3087 2003-12-06 devnull q->head = e->next;
1205 d3df3087 2003-12-06 devnull qunlock(&q->lk);
1206 d3df3087 2003-12-06 devnull p = e->p;
1207 d3df3087 2003-12-06 devnull free(e);
1208 d3df3087 2003-12-06 devnull return p;
1212 ceb04770 2003-12-09 devnull read9ppkt(Ioproc *io, int fd)
1214 ceb04770 2003-12-09 devnull uchar buf[4], *pkt;
1215 ceb04770 2003-12-09 devnull int n, nn;
1217 ceb04770 2003-12-09 devnull n = ioreadn(io, fd, buf, 4);
1218 ceb04770 2003-12-09 devnull if(n != 4)
1219 ceb04770 2003-12-09 devnull return nil;
1220 ceb04770 2003-12-09 devnull n = GBIT32(buf);
1221 ceb04770 2003-12-09 devnull pkt = emalloc(n);
1222 ceb04770 2003-12-09 devnull PBIT32(pkt, n);
1223 ceb04770 2003-12-09 devnull nn = ioreadn(io, fd, pkt+4, n-4);
1224 ceb04770 2003-12-09 devnull if(nn != n-4){
1225 ceb04770 2003-12-09 devnull free(pkt);
1226 ceb04770 2003-12-09 devnull return nil;
1228 32f69c36 2003-12-11 devnull /* would do this if we ever got one of these, but we only generate them
1229 32f69c36 2003-12-11 devnull if(pkt[4] == Ropenfd){
1230 32f69c36 2003-12-11 devnull newfd = iorecvfd(io, fd);
1231 32f69c36 2003-12-11 devnull PBIT32(pkt+n-4, newfd);
1234 ceb04770 2003-12-09 devnull return pkt;
1238 215993f8 2005-09-13 devnull mread9p(Ioproc *io, int fd, int dotu)
1240 ceb04770 2003-12-09 devnull int n, nn;
1241 ceb04770 2003-12-09 devnull uchar *pkt;
1242 ceb04770 2003-12-09 devnull Msg *m;
1244 ceb04770 2003-12-09 devnull if((pkt = read9ppkt(io, fd)) == nil)
1245 ceb04770 2003-12-09 devnull return nil;
1247 e2a1725d 2005-01-04 devnull m = msgnew(0);
1248 ceb04770 2003-12-09 devnull m->tpkt = pkt;
1249 ceb04770 2003-12-09 devnull n = GBIT32(pkt);
1250 215993f8 2005-09-13 devnull nn = convM2Su(pkt, n, &m->tx, dotu);
1251 ceb04770 2003-12-09 devnull if(nn != n){
1252 e2a1725d 2005-01-04 devnull fprint(2, "%T read bad packet from %d\n", fd);
1253 ceb04770 2003-12-09 devnull return nil;
1255 ceb04770 2003-12-09 devnull return m;
1259 ceb04770 2003-12-09 devnull mwrite9p(Ioproc *io, int fd, uchar *pkt)
1261 32f69c36 2003-12-11 devnull int n, nfd;
1263 ceb04770 2003-12-09 devnull n = GBIT32(pkt);
1264 e2a1725d 2005-01-04 devnull if(verbose > 2) fprint(2, "%T write %d %d %.*H\n", fd, n, n, pkt);
1265 bb426e77 2005-01-06 devnull if(verbose > 1) fprint(2, "%T before iowrite\n");
1266 ceb04770 2003-12-09 devnull if(iowrite(io, fd, pkt, n) != n){
1267 e2a1725d 2005-01-04 devnull fprint(2, "%T write error: %r\n");
1268 ceb04770 2003-12-09 devnull return -1;
1270 bb426e77 2005-01-06 devnull if(verbose > 1) fprint(2, "%T after iowrite\n");
1271 32f69c36 2003-12-11 devnull if(pkt[4] == Ropenfd){
1272 32f69c36 2003-12-11 devnull nfd = GBIT32(pkt+n-4);
1273 32f69c36 2003-12-11 devnull if(iosendfd(io, fd, nfd) < 0){
1274 e2a1725d 2005-01-04 devnull fprint(2, "%T send fd error: %r\n");
1275 32f69c36 2003-12-11 devnull return -1;
1278 ceb04770 2003-12-09 devnull return 0;
1282 ceb04770 2003-12-09 devnull restring(uchar *pkt, int pn, char *s)
1286 ceb04770 2003-12-09 devnull if(s < (char*)pkt || s >= (char*)pkt+pn)
1287 ceb04770 2003-12-09 devnull return;
1289 ceb04770 2003-12-09 devnull n = strlen(s);
1290 ceb04770 2003-12-09 devnull memmove(s+1, s, n);
1291 ceb04770 2003-12-09 devnull PBIT16((uchar*)s-1, n);
1295 215993f8 2005-09-13 devnull repack(Fcall *f, uchar **ppkt, int dotu)
1297 5c84c448 2005-03-21 devnull uint n, nn;
1298 5c84c448 2005-03-21 devnull uchar *pkt;
1300 5c84c448 2005-03-21 devnull pkt = *ppkt;
1301 5c84c448 2005-03-21 devnull n = GBIT32(pkt);
1302 215993f8 2005-09-13 devnull nn = sizeS2Mu(f, dotu);
1303 5c84c448 2005-03-21 devnull if(nn > n){
1304 5c84c448 2005-03-21 devnull free(pkt);
1305 5c84c448 2005-03-21 devnull pkt = emalloc(nn);
1306 5c84c448 2005-03-21 devnull *ppkt = pkt;
1308 9e2f1d9b 2005-11-21 devnull n = convS2Mu(f, pkt, nn, dotu);
1309 9e2f1d9b 2005-11-21 devnull if(n <= BIT16SZ)
1310 9e2f1d9b 2005-11-21 devnull sysfatal("convS2M conversion error");
1311 9e2f1d9b 2005-11-21 devnull if(n != nn)
1312 9e2f1d9b 2005-11-21 devnull sysfatal("convS2Mu and sizeS2Mu disagree");
1316 ceb04770 2003-12-09 devnull rewritehdr(Fcall *f, uchar *pkt)
1318 ceb04770 2003-12-09 devnull int i, n;
1320 ceb04770 2003-12-09 devnull n = GBIT32(pkt);
1321 ceb04770 2003-12-09 devnull PBIT16(pkt+5, f->tag);
1322 ceb04770 2003-12-09 devnull switch(f->type){
1323 ceb04770 2003-12-09 devnull case Tversion:
1324 ceb04770 2003-12-09 devnull case Rversion:
1325 ceb04770 2003-12-09 devnull restring(pkt, n, f->version);
1327 ceb04770 2003-12-09 devnull case Tauth:
1328 ceb04770 2003-12-09 devnull PBIT32(pkt+7, f->afid);
1329 ceb04770 2003-12-09 devnull restring(pkt, n, f->uname);
1330 ceb04770 2003-12-09 devnull restring(pkt, n, f->aname);
1332 ceb04770 2003-12-09 devnull case Tflush:
1333 ceb04770 2003-12-09 devnull PBIT16(pkt+7, f->oldtag);
1335 ceb04770 2003-12-09 devnull case Tattach:
1336 ceb04770 2003-12-09 devnull restring(pkt, n, f->uname);
1337 ceb04770 2003-12-09 devnull restring(pkt, n, f->aname);
1338 ceb04770 2003-12-09 devnull PBIT32(pkt+7, f->fid);
1339 ceb04770 2003-12-09 devnull PBIT32(pkt+11, f->afid);
1341 ceb04770 2003-12-09 devnull case Twalk:
1342 ceb04770 2003-12-09 devnull PBIT32(pkt+7, f->fid);
1343 ceb04770 2003-12-09 devnull PBIT32(pkt+11, f->newfid);
1344 ceb04770 2003-12-09 devnull for(i=0; i<f->nwname; i++)
1345 ceb04770 2003-12-09 devnull restring(pkt, n, f->wname[i]);
1347 ceb04770 2003-12-09 devnull case Tcreate:
1348 ceb04770 2003-12-09 devnull restring(pkt, n, f->name);
1349 ceb04770 2003-12-09 devnull /* fall through */
1350 ceb04770 2003-12-09 devnull case Topen:
1351 ceb04770 2003-12-09 devnull case Tclunk:
1352 ceb04770 2003-12-09 devnull case Tremove:
1353 ceb04770 2003-12-09 devnull case Tstat:
1354 ceb04770 2003-12-09 devnull case Twstat:
1355 9777187b 2006-02-03 devnull case Twrite:
1356 ceb04770 2003-12-09 devnull PBIT32(pkt+7, f->fid);
1358 9777187b 2006-02-03 devnull case Tread:
1359 9777187b 2006-02-03 devnull PBIT32(pkt+7, f->fid);
1360 9777187b 2006-02-03 devnull PBIT64(pkt+11, f->offset);
1362 ceb04770 2003-12-09 devnull case Rerror:
1363 ceb04770 2003-12-09 devnull restring(pkt, n, f->ename);
1368 32f69c36 2003-12-11 devnull static long
1369 ceb04770 2003-12-09 devnull _iolisten(va_list *arg)
1371 ceb04770 2003-12-09 devnull char *a, *b;
1373 ceb04770 2003-12-09 devnull a = va_arg(*arg, char*);
1374 ceb04770 2003-12-09 devnull b = va_arg(*arg, char*);
1375 ceb04770 2003-12-09 devnull return listen(a, b);
1379 ceb04770 2003-12-09 devnull iolisten(Ioproc *io, char *a, char *b)
1381 ceb04770 2003-12-09 devnull return iocall(io, _iolisten, a, b);
1384 ceb04770 2003-12-09 devnull static long
1385 ceb04770 2003-12-09 devnull _ioaccept(va_list *arg)
1387 ceb04770 2003-12-09 devnull int fd;
1388 ceb04770 2003-12-09 devnull char *dir;
1390 ceb04770 2003-12-09 devnull fd = va_arg(*arg, int);
1391 ceb04770 2003-12-09 devnull dir = va_arg(*arg, char*);
1392 ceb04770 2003-12-09 devnull return accept(fd, dir);
1396 ceb04770 2003-12-09 devnull ioaccept(Ioproc *io, int fd, char *dir)
1398 ceb04770 2003-12-09 devnull return iocall(io, _ioaccept, fd, dir);
1402 e2a1725d 2005-01-04 devnull timefmt(Fmt *fmt)
1404 e2a1725d 2005-01-04 devnull static char *mon[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1405 e2a1725d 2005-01-04 devnull "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1406 bb426e77 2005-01-06 devnull vlong ns;
1408 bb426e77 2005-01-06 devnull ns = nsec();
1409 e2a1725d 2005-01-04 devnull tm = *localtime(time(0));
1410 bb426e77 2005-01-06 devnull return fmtprint(fmt, "%s %2d %02d:%02d:%02d.%03d",
1411 bb426e77 2005-01-06 devnull mon[tm.mon], tm.mday, tm.hour, tm.min, tm.sec,
1412 bb426e77 2005-01-06 devnull (int)(ns%1000000000)/1000000);
1416 16c1defa 2005-11-12 devnull cvtustat(Fcall *f, uchar **fpkt, int tounix)
1419 23e7c657 2005-09-13 devnull uchar *buf;
1420 215993f8 2005-09-13 devnull char *str;
1421 215993f8 2005-09-13 devnull Dir dir;
1423 215993f8 2005-09-13 devnull str = emalloc(f->nstat);
1424 16c1defa 2005-11-12 devnull n = convM2Du(f->stat, f->nstat, &dir, str, !tounix);
1425 16c1defa 2005-11-12 devnull if(n <= BIT16SZ){
1426 16c1defa 2005-11-12 devnull free(str);
1427 215993f8 2005-09-13 devnull return -1;
1430 16c1defa 2005-11-12 devnull n = sizeD2Mu(&dir, tounix);
1431 16c1defa 2005-11-12 devnull buf = emalloc(n);
1432 9e2f1d9b 2005-11-21 devnull if(convD2Mu(&dir, buf, n, tounix) != n)
1433 9e2f1d9b 2005-11-21 devnull sysfatal("convD2Mu conversion error");
1434 215993f8 2005-09-13 devnull f->nstat = n;
1435 215993f8 2005-09-13 devnull f->stat = buf;
1437 215993f8 2005-09-13 devnull repack(f, fpkt, dotu);
1438 215993f8 2005-09-13 devnull free(buf);
1439 16c1defa 2005-11-12 devnull f->stat = nil; /* is this okay ??? */
1440 215993f8 2005-09-13 devnull free(str);
1442 215993f8 2005-09-13 devnull return 0;
1446 215993f8 2005-09-13 devnull stripudirread(Msg* msg)
1448 23e7c657 2005-09-13 devnull char *str;
1449 215993f8 2005-09-13 devnull int i, m, n, nn;
1450 23e7c657 2005-09-13 devnull uchar *buf;
1452 215993f8 2005-09-13 devnull Fcall* rx;
1454 215993f8 2005-09-13 devnull buf = nil;
1455 215993f8 2005-09-13 devnull str = nil;
1456 215993f8 2005-09-13 devnull rx = &msg->rx;
1458 215993f8 2005-09-13 devnull nn = 0;
1459 215993f8 2005-09-13 devnull for(i = 0; i < rx->count; i += m){
1460 215993f8 2005-09-13 devnull m = BIT16SZ + GBIT16(&rx->data[i]);
1461 54705b4a 2005-09-13 devnull if(statchecku((uchar*)&rx->data[i], m, 1) < 0)
1462 215993f8 2005-09-13 devnull return -1;
1463 215993f8 2005-09-13 devnull if(nn < m)
1464 215993f8 2005-09-13 devnull nn = m;
1468 215993f8 2005-09-13 devnull str = emalloc(nn);
1469 215993f8 2005-09-13 devnull buf = emalloc(rx->count);
1471 215993f8 2005-09-13 devnull nn = 0;
1472 215993f8 2005-09-13 devnull for(i = 0; i < rx->count; i += m){
1473 215993f8 2005-09-13 devnull m = BIT16SZ + GBIT16(&rx->data[i]);
1474 54705b4a 2005-09-13 devnull if(convM2Du((uchar*)&rx->data[i], m, &d, str, 1) != m){
1475 215993f8 2005-09-13 devnull free(buf);
1476 215993f8 2005-09-13 devnull free(str);
1477 215993f8 2005-09-13 devnull return -1;
1480 215993f8 2005-09-13 devnull n = convD2M(&d, &buf[nn], rx->count - nn);
1481 215993f8 2005-09-13 devnull if(n <= BIT16SZ){
1482 215993f8 2005-09-13 devnull free(buf);
1483 215993f8 2005-09-13 devnull free(str);
1484 215993f8 2005-09-13 devnull return -1;
1487 215993f8 2005-09-13 devnull nn += n;
1490 215993f8 2005-09-13 devnull rx->count = nn;
1491 54705b4a 2005-09-13 devnull rx->data = (char*)buf;
1493 215993f8 2005-09-13 devnull repack(&msg->rx, &msg->rpkt, 0);
1494 215993f8 2005-09-13 devnull free(str);
1495 215993f8 2005-09-13 devnull free(buf);
1496 16c1defa 2005-11-12 devnull rx->data = nil; /* is this okay ??? */
1498 215993f8 2005-09-13 devnull return 0;