1 78a779a3 2008-07-20 rsc #include <u.h>
2 78a779a3 2008-07-20 rsc #include <libc.h>
3 78a779a3 2008-07-20 rsc #include <auth.h>
4 78a779a3 2008-07-20 rsc #include <fcall.h>
5 78a779a3 2008-07-20 rsc #include "dat.h"
6 78a779a3 2008-07-20 rsc #include "fns.h"
10 78a779a3 2008-07-20 rsc Maxfdata = 8192,
11 78a779a3 2008-07-20 rsc Maxiosize = IOHDRSZ+Maxfdata,
14 78a779a3 2008-07-20 rsc void io(int);
15 78a779a3 2008-07-20 rsc void rversion(void);
16 78a779a3 2008-07-20 rsc void rattach(void);
17 78a779a3 2008-07-20 rsc void rauth(void);
18 78a779a3 2008-07-20 rsc void rclunk(void);
19 78a779a3 2008-07-20 rsc void rcreate(void);
20 78a779a3 2008-07-20 rsc void rflush(void);
21 78a779a3 2008-07-20 rsc void ropen(void);
22 78a779a3 2008-07-20 rsc void rread(void);
23 78a779a3 2008-07-20 rsc void rremove(void);
24 78a779a3 2008-07-20 rsc void rsession(void);
25 78a779a3 2008-07-20 rsc void rstat(void);
26 78a779a3 2008-07-20 rsc void rwalk(void);
27 78a779a3 2008-07-20 rsc void rwrite(void);
28 78a779a3 2008-07-20 rsc void rwstat(void);
30 78a779a3 2008-07-20 rsc static int openflags(int);
31 78a779a3 2008-07-20 rsc static void usage(void);
33 78a779a3 2008-07-20 rsc #define Reqsize (sizeof(Fcall)+Maxfdata)
38 78a779a3 2008-07-20 rsc uchar mdata[Maxiosize];
39 78a779a3 2008-07-20 rsc char fdata[Maxfdata];
40 78a779a3 2008-07-20 rsc uchar statbuf[STATMAX];
44 78a779a3 2008-07-20 rsc extern Xfsub *xsublist[];
45 78a779a3 2008-07-20 rsc extern int nclust;
47 78a779a3 2008-07-20 rsc jmp_buf err_lab[16];
48 78a779a3 2008-07-20 rsc int nerr_lab;
49 78a779a3 2008-07-20 rsc char err_msg[ERRMAX];
52 78a779a3 2008-07-20 rsc int nojoliet;
56 f134aa3c 2008-12-02 trisk void (*fcalls[Tmax])(void);
58 f134aa3c 2008-12-02 trisk static void
59 f134aa3c 2008-12-02 trisk initfcalls(void)
61 f134aa3c 2008-12-02 trisk fcalls[Tversion]= rversion;
62 f134aa3c 2008-12-02 trisk fcalls[Tflush]= rflush;
63 f134aa3c 2008-12-02 trisk fcalls[Tauth]= rauth;
64 f134aa3c 2008-12-02 trisk fcalls[Tattach]= rattach;
65 f134aa3c 2008-12-02 trisk fcalls[Twalk]= rwalk;
66 f134aa3c 2008-12-02 trisk fcalls[Topen]= ropen;
67 f134aa3c 2008-12-02 trisk fcalls[Tcreate]= rcreate;
68 f134aa3c 2008-12-02 trisk fcalls[Tread]= rread;
69 f134aa3c 2008-12-02 trisk fcalls[Twrite]= rwrite;
70 f134aa3c 2008-12-02 trisk fcalls[Tclunk]= rclunk;
71 f134aa3c 2008-12-02 trisk fcalls[Tremove]= rremove;
72 f134aa3c 2008-12-02 trisk fcalls[Tstat]= rstat;
73 f134aa3c 2008-12-02 trisk fcalls[Twstat]= rwstat;
77 78a779a3 2008-07-20 rsc main(int argc, char **argv)
79 78a779a3 2008-07-20 rsc int srvfd, pipefd[2], stdio;
83 f134aa3c 2008-12-02 trisk initfcalls();
91 78a779a3 2008-07-20 rsc nclust = atoi(EARGF(usage()));
92 78a779a3 2008-07-20 rsc if (nclust <= 0)
93 78a779a3 2008-07-20 rsc sysfatal("nclust %d non-positive", nclust);
96 78a779a3 2008-07-20 rsc deffile = EARGF(usage());
108 78a779a3 2008-07-20 rsc nojoliet = 1;
111 78a779a3 2008-07-20 rsc mtpt = EARGF(usage());
117 78a779a3 2008-07-20 rsc switch(argc) {
121 78a779a3 2008-07-20 rsc srvname = argv[0];
127 78a779a3 2008-07-20 rsc iobuf_init();
128 78a779a3 2008-07-20 rsc for(xs=xsublist; *xs; xs++)
129 78a779a3 2008-07-20 rsc (*(*xs)->reset)();
132 78a779a3 2008-07-20 rsc pipefd[0] = 0;
133 78a779a3 2008-07-20 rsc pipefd[1] = 1;
137 78a779a3 2008-07-20 rsc open("/dev/null", OREAD);
138 78a779a3 2008-07-20 rsc open("/dev/null", OWRITE);
139 78a779a3 2008-07-20 rsc if(pipe(pipefd) < 0)
140 78a779a3 2008-07-20 rsc panic(1, "pipe");
142 78a779a3 2008-07-20 rsc if(post9pservice(pipefd[0], srvname, mtpt) < 0)
143 78a779a3 2008-07-20 rsc sysfatal("post9pservice: %r");
144 78a779a3 2008-07-20 rsc close(pipefd[0]);
146 78a779a3 2008-07-20 rsc srvfd = pipefd[1];
148 78a779a3 2008-07-20 rsc switch(rfork(RFNOWAIT|RFNOTEG|RFFDG|RFPROC)){
150 78a779a3 2008-07-20 rsc panic(1, "fork");
162 78a779a3 2008-07-20 rsc io(int srvfd)
165 78a779a3 2008-07-20 rsc Fcall xreq, xrep;
167 78a779a3 2008-07-20 rsc req = &xreq;
168 78a779a3 2008-07-20 rsc rep = &xrep;
169 78a779a3 2008-07-20 rsc pid = getpid();
170 78a779a3 2008-07-20 rsc fmtinstall('F', fcallfmt);
174 78a779a3 2008-07-20 rsc * reading from a pipe or a network device
175 78a779a3 2008-07-20 rsc * will give an error after a few eof reads.
176 78a779a3 2008-07-20 rsc * however, we cannot tell the difference
177 78a779a3 2008-07-20 rsc * between a zero-length read and an interrupt
178 78a779a3 2008-07-20 rsc * on the processes writing to us,
179 78a779a3 2008-07-20 rsc * so we wait for the error.
181 78a779a3 2008-07-20 rsc n = read9pmsg(srvfd, mdata, sizeof mdata);
186 78a779a3 2008-07-20 rsc if(convM2S(mdata, n, req) == 0)
190 78a779a3 2008-07-20 rsc fprint(2, "9660srv %d:<-%F\n", pid, req);
193 78a779a3 2008-07-20 rsc if(!waserror()){
194 78a779a3 2008-07-20 rsc err_msg[0] = 0;
195 78a779a3 2008-07-20 rsc if(req->type >= nelem(fcalls) || !fcalls[req->type])
196 78a779a3 2008-07-20 rsc error("bad fcall type");
197 78a779a3 2008-07-20 rsc (*fcalls[req->type])();
201 78a779a3 2008-07-20 rsc if(err_msg[0]){
202 78a779a3 2008-07-20 rsc rep->type = Rerror;
203 78a779a3 2008-07-20 rsc rep->ename = err_msg;
205 78a779a3 2008-07-20 rsc rep->type = req->type + 1;
206 78a779a3 2008-07-20 rsc rep->fid = req->fid;
208 78a779a3 2008-07-20 rsc rep->tag = req->tag;
211 78a779a3 2008-07-20 rsc fprint(2, "9660srv %d:->%F\n", pid, rep);
212 78a779a3 2008-07-20 rsc n = convS2M(rep, mdata, sizeof mdata);
214 78a779a3 2008-07-20 rsc panic(1, "convS2M error on write");
215 78a779a3 2008-07-20 rsc if(write(srvfd, mdata, n) != n)
216 78a779a3 2008-07-20 rsc panic(1, "mount write");
217 78a779a3 2008-07-20 rsc if(nerr_lab != 0)
218 78a779a3 2008-07-20 rsc panic(0, "err stack %d");
220 78a779a3 2008-07-20 rsc chat("server shut down");
226 78a779a3 2008-07-20 rsc fprint(2, "usage: %s [-v] [-9Jr] [-s] [-f devicefile] [srvname]\n", argv0);
227 78a779a3 2008-07-20 rsc exits("usage");
231 78a779a3 2008-07-20 rsc error(char *p)
233 78a779a3 2008-07-20 rsc strecpy(err_msg, err_msg+sizeof err_msg, p);
234 78a779a3 2008-07-20 rsc nexterror();
238 78a779a3 2008-07-20 rsc nexterror(void)
240 78a779a3 2008-07-20 rsc longjmp(err_lab[--nerr_lab], 1);
244 78a779a3 2008-07-20 rsc ealloc(long n)
248 78a779a3 2008-07-20 rsc p = malloc(n);
250 78a779a3 2008-07-20 rsc error("no memory");
255 78a779a3 2008-07-20 rsc setnames(Dir *d, char *n)
257 78a779a3 2008-07-20 rsc d->name = n;
258 78a779a3 2008-07-20 rsc d->uid = n+Maxname;
259 78a779a3 2008-07-20 rsc d->gid = n+Maxname*2;
260 78a779a3 2008-07-20 rsc d->muid = n+Maxname*3;
262 78a779a3 2008-07-20 rsc d->name[0] = '\0';
263 78a779a3 2008-07-20 rsc d->uid[0] = '\0';
264 78a779a3 2008-07-20 rsc d->gid[0] = '\0';
265 78a779a3 2008-07-20 rsc d->muid[0] = '\0';
269 78a779a3 2008-07-20 rsc rversion(void)
271 78a779a3 2008-07-20 rsc if(req->msize > Maxiosize)
272 78a779a3 2008-07-20 rsc rep->msize = Maxiosize;
274 78a779a3 2008-07-20 rsc rep->msize = req->msize;
275 78a779a3 2008-07-20 rsc rep->version = "9P2000";
281 78a779a3 2008-07-20 rsc error("9660srv: authentication not required");
285 78a779a3 2008-07-20 rsc rflush(void)
290 78a779a3 2008-07-20 rsc rattach(void)
293 78a779a3 2008-07-20 rsc Xfile *root;
296 78a779a3 2008-07-20 rsc chat("attach(fid=%d,uname=\"%s\",aname=\"%s\")...",
297 78a779a3 2008-07-20 rsc req->fid, req->uname, req->aname);
299 78a779a3 2008-07-20 rsc if(waserror()){
300 78a779a3 2008-07-20 rsc xfile(req->fid, Clunk);
301 78a779a3 2008-07-20 rsc nexterror();
303 78a779a3 2008-07-20 rsc root = xfile(req->fid, Clean);
304 78a779a3 2008-07-20 rsc root->qid = (Qid){0, 0, QTDIR};
305 78a779a3 2008-07-20 rsc root->xf = xf = ealloc(sizeof(Xfs));
306 78a779a3 2008-07-20 rsc memset(xf, 0, sizeof(Xfs));
307 78a779a3 2008-07-20 rsc xf->ref = 1;
308 78a779a3 2008-07-20 rsc xf->d = getxdata(req->aname);
310 78a779a3 2008-07-20 rsc for(xs=xsublist; *xs; xs++)
311 78a779a3 2008-07-20 rsc if((*(*xs)->attach)(root) >= 0){
313 78a779a3 2008-07-20 rsc xf->s = *xs;
314 78a779a3 2008-07-20 rsc xf->rootqid = root->qid;
315 78a779a3 2008-07-20 rsc rep->qid = root->qid;
318 78a779a3 2008-07-20 rsc error("unknown format");
322 78a779a3 2008-07-20 rsc doclone(Xfile *of, int newfid)
324 78a779a3 2008-07-20 rsc Xfile *nf, *next;
326 78a779a3 2008-07-20 rsc nf = xfile(newfid, Clean);
327 78a779a3 2008-07-20 rsc if(waserror()){
328 78a779a3 2008-07-20 rsc xfile(newfid, Clunk);
329 78a779a3 2008-07-20 rsc nexterror();
331 78a779a3 2008-07-20 rsc next = nf->next;
333 78a779a3 2008-07-20 rsc nf->next = next;
334 78a779a3 2008-07-20 rsc nf->fid = newfid;
335 78a779a3 2008-07-20 rsc refxfs(nf->xf, 1);
336 78a779a3 2008-07-20 rsc if(nf->len){
337 78a779a3 2008-07-20 rsc nf->ptr = ealloc(nf->len);
338 78a779a3 2008-07-20 rsc memmove(nf->ptr, of->ptr, nf->len);
340 78a779a3 2008-07-20 rsc nf->ptr = of->ptr;
341 78a779a3 2008-07-20 rsc (*of->xf->s->clone)(of, nf);
349 78a779a3 2008-07-20 rsc Xfile *f, *nf;
350 78a779a3 2008-07-20 rsc Isofile *oldptr;
354 78a779a3 2008-07-20 rsc rep->nwqid = 0;
356 78a779a3 2008-07-20 rsc f = xfile(req->fid, Asis);
357 78a779a3 2008-07-20 rsc if(req->fid != req->newfid)
358 78a779a3 2008-07-20 rsc f = nf = doclone(f, req->newfid);
360 78a779a3 2008-07-20 rsc /* save old state in case of error */
361 78a779a3 2008-07-20 rsc oldqid = f->qid;
362 78a779a3 2008-07-20 rsc oldlen = f->len;
363 78a779a3 2008-07-20 rsc oldptr = f->ptr;
365 78a779a3 2008-07-20 rsc oldptr = ealloc(oldlen);
366 78a779a3 2008-07-20 rsc memmove(oldptr, f->ptr, oldlen);
369 78a779a3 2008-07-20 rsc if(waserror()){
370 78a779a3 2008-07-20 rsc if(nf != nil)
371 78a779a3 2008-07-20 rsc xfile(req->newfid, Clunk);
372 78a779a3 2008-07-20 rsc if(rep->nwqid == req->nwname){
374 78a779a3 2008-07-20 rsc free(oldptr);
376 78a779a3 2008-07-20 rsc /* restore previous state */
377 78a779a3 2008-07-20 rsc f->qid = oldqid;
379 78a779a3 2008-07-20 rsc free(f->ptr);
380 78a779a3 2008-07-20 rsc f->ptr = oldptr;
381 78a779a3 2008-07-20 rsc f->len = oldlen;
383 78a779a3 2008-07-20 rsc if(rep->nwqid==req->nwname || rep->nwqid > 0){
384 78a779a3 2008-07-20 rsc err_msg[0] = '\0';
387 78a779a3 2008-07-20 rsc nexterror();
390 78a779a3 2008-07-20 rsc for(rep->nwqid=0; rep->nwqid < req->nwname && rep->nwqid < MAXWELEM; rep->nwqid++){
391 78a779a3 2008-07-20 rsc chat("\twalking %s\n", req->wname[rep->nwqid]);
392 78a779a3 2008-07-20 rsc if(!(f->qid.type & QTDIR)){
393 78a779a3 2008-07-20 rsc chat("\tnot dir: type=%#x\n", f->qid.type);
394 78a779a3 2008-07-20 rsc error("walk in non-directory");
397 78a779a3 2008-07-20 rsc if(strcmp(req->wname[rep->nwqid], "..")==0){
398 78a779a3 2008-07-20 rsc if(f->qid.path != f->xf->rootqid.path)
399 78a779a3 2008-07-20 rsc (*f->xf->s->walkup)(f);
401 78a779a3 2008-07-20 rsc (*f->xf->s->walk)(f, req->wname[rep->nwqid]);
402 78a779a3 2008-07-20 rsc rep->wqid[rep->nwqid] = f->qid;
406 78a779a3 2008-07-20 rsc free(oldptr);
414 78a779a3 2008-07-20 rsc f = xfile(req->fid, Asis);
415 78a779a3 2008-07-20 rsc if(f->flags&Omodes)
416 78a779a3 2008-07-20 rsc error("open on open file");
417 78a779a3 2008-07-20 rsc if(req->mode&ORCLOSE)
418 78a779a3 2008-07-20 rsc error("no removes");
419 78a779a3 2008-07-20 rsc (*f->xf->s->open)(f, req->mode);
420 78a779a3 2008-07-20 rsc f->flags = openflags(req->mode);
421 78a779a3 2008-07-20 rsc rep->qid = f->qid;
422 78a779a3 2008-07-20 rsc rep->iounit = 0;
426 78a779a3 2008-07-20 rsc rcreate(void)
428 78a779a3 2008-07-20 rsc error("no creates");
432 78a779a3 2008-07-20 rsc if(strcmp(req->name, ".") == 0 || strcmp(req->name, "..") == 0)
433 78a779a3 2008-07-20 rsc error("create . or ..");
434 78a779a3 2008-07-20 rsc f = xfile(req->fid, Asis);
435 78a779a3 2008-07-20 rsc if(f->flags&Omodes)
436 78a779a3 2008-07-20 rsc error("create on open file");
437 78a779a3 2008-07-20 rsc if(!(f->qid.path&CHDIR))
438 78a779a3 2008-07-20 rsc error("create in non-directory");
439 78a779a3 2008-07-20 rsc (*f->xf->s->create)(f, req->name, req->perm, req->mode);
440 78a779a3 2008-07-20 rsc chat("f->qid=0x%8.8lux...", f->qid.path);
441 78a779a3 2008-07-20 rsc f->flags = openflags(req->mode);
442 78a779a3 2008-07-20 rsc rep->qid = f->qid;
451 78a779a3 2008-07-20 rsc f=xfile(req->fid, Asis);
452 78a779a3 2008-07-20 rsc if (!(f->flags&Oread))
453 78a779a3 2008-07-20 rsc error("file not opened for reading");
454 78a779a3 2008-07-20 rsc if(f->qid.type & QTDIR)
455 78a779a3 2008-07-20 rsc rep->count = (*f->xf->s->readdir)(f, (uchar*)fdata, req->offset, req->count);
457 78a779a3 2008-07-20 rsc rep->count = (*f->xf->s->read)(f, fdata, req->offset, req->count);
458 78a779a3 2008-07-20 rsc rep->data = fdata;
462 78a779a3 2008-07-20 rsc rwrite(void)
466 78a779a3 2008-07-20 rsc f=xfile(req->fid, Asis);
467 78a779a3 2008-07-20 rsc if(!(f->flags&Owrite))
468 78a779a3 2008-07-20 rsc error("file not opened for writing");
469 78a779a3 2008-07-20 rsc rep->count = (*f->xf->s->write)(f, req->data, req->offset, req->count);
473 78a779a3 2008-07-20 rsc rclunk(void)
477 78a779a3 2008-07-20 rsc if(!waserror()){
478 78a779a3 2008-07-20 rsc f = xfile(req->fid, Asis);
479 78a779a3 2008-07-20 rsc (*f->xf->s->clunk)(f);
482 78a779a3 2008-07-20 rsc xfile(req->fid, Clunk);
486 78a779a3 2008-07-20 rsc rremove(void)
488 78a779a3 2008-07-20 rsc error("no removes");
497 78a779a3 2008-07-20 rsc chat("stat(fid=%d)...", req->fid);
498 78a779a3 2008-07-20 rsc f=xfile(req->fid, Asis);
499 78a779a3 2008-07-20 rsc setnames(&dir, fdata);
500 78a779a3 2008-07-20 rsc (*f->xf->s->stat)(f, &dir);
502 78a779a3 2008-07-20 rsc showdir(2, &dir);
503 78a779a3 2008-07-20 rsc rep->nstat = convD2M(&dir, statbuf, sizeof statbuf);
504 78a779a3 2008-07-20 rsc rep->stat = statbuf;
508 78a779a3 2008-07-20 rsc rwstat(void)
510 78a779a3 2008-07-20 rsc error("no wstat");
514 78a779a3 2008-07-20 rsc openflags(int mode)
516 78a779a3 2008-07-20 rsc int flags = 0;
518 78a779a3 2008-07-20 rsc switch(mode & ~(OTRUNC|OCEXEC|ORCLOSE)){
521 78a779a3 2008-07-20 rsc flags = Oread; break;
522 78a779a3 2008-07-20 rsc case OWRITE:
523 78a779a3 2008-07-20 rsc flags = Owrite; break;
525 78a779a3 2008-07-20 rsc flags = Oread|Owrite; break;
527 78a779a3 2008-07-20 rsc if(mode & ORCLOSE)
528 78a779a3 2008-07-20 rsc flags |= Orclose;
529 78a779a3 2008-07-20 rsc return flags;
533 78a779a3 2008-07-20 rsc showdir(int fd, Dir *s)
535 78a779a3 2008-07-20 rsc char a_time[32], m_time[32];
538 78a779a3 2008-07-20 rsc strcpy(a_time, ctime(s->atime));
539 78a779a3 2008-07-20 rsc if(p=strchr(a_time, '\n')) /* assign = */
541 78a779a3 2008-07-20 rsc strcpy(m_time, ctime(s->mtime));
542 78a779a3 2008-07-20 rsc if(p=strchr(m_time, '\n')) /* assign = */
544 78a779a3 2008-07-20 rsc fprint(fd, "name=\"%s\" qid=(0x%llux,%lud) type=%d dev=%d \
545 78a779a3 2008-07-20 rsc mode=0x%8.8lux=0%luo atime=%s mtime=%s length=%lld uid=\"%s\" gid=\"%s\"...",
546 78a779a3 2008-07-20 rsc s->name, s->qid.path, s->qid.vers, s->type, s->dev,
547 78a779a3 2008-07-20 rsc s->mode, s->mode,
548 78a779a3 2008-07-20 rsc a_time, m_time, s->length, s->uid, s->gid);
551 78a779a3 2008-07-20 rsc #define SIZE 1024
554 78a779a3 2008-07-20 rsc chat(char *fmt, ...)
556 78a779a3 2008-07-20 rsc va_list arg;
559 78a779a3 2008-07-20 rsc va_start(arg, fmt);
560 78a779a3 2008-07-20 rsc vfprint(2, fmt, arg);
561 78a779a3 2008-07-20 rsc va_end(arg);
566 78a779a3 2008-07-20 rsc panic(int rflag, char *fmt, ...)
568 78a779a3 2008-07-20 rsc va_list arg;
569 78a779a3 2008-07-20 rsc char buf[SIZE]; int n;
571 78a779a3 2008-07-20 rsc n = sprint(buf, "%s %d: ", argv0, getpid());
572 78a779a3 2008-07-20 rsc va_start(arg, fmt);
573 78a779a3 2008-07-20 rsc vseprint(buf+n, buf+SIZE, fmt, arg);
574 78a779a3 2008-07-20 rsc va_end(arg);
575 78a779a3 2008-07-20 rsc fprint(2, (rflag ? "%s: %r\n" : "%s\n"), buf);
577 78a779a3 2008-07-20 rsc fprint(2, "abort\n");
580 78a779a3 2008-07-20 rsc exits("panic");