5 int post9pservice(int, char*);
8 * Rather than reading /adm/users, which is a lot of work for
9 * a toy program, we assume all groups have the form
11 * meaning that each user is the leader of his own group.
16 OPERM = 0x3, /* mask of all permission types in open mode */
18 Maxsize = 512*1024*1024,
22 typedef struct Fid Fid;
23 typedef struct Ram Ram;
40 long parent; /* index in Ram array */
63 ulong path; /* incremented for each new file */
69 uchar mdata[IOHDRSZ+Maxfdata];
70 uchar rdata[Maxfdata]; /* buffer for data in reply */
71 uchar statbuf[STATMAX];
74 int messagesize = sizeof mdata;
77 uint ramstat(Ram*, uchar*, uint);
80 void *erealloc(void*, ulong);
84 int perm(Fid*, Ram*, int);
86 char *rflush(Fid*), *rversion(Fid*), *rauth(Fid*),
87 *rattach(Fid*), *rwalk(Fid*),
88 *ropen(Fid*), *rcreate(Fid*),
89 *rread(Fid*), *rwrite(Fid*), *rclunk(Fid*),
90 *rremove(Fid*), *rstat(Fid*), *rwstat(Fid*);
92 char *(*fcalls[Tmax])(Fid*);
97 fcalls[Tversion]= rversion;
98 fcalls[Tflush]= rflush;
100 fcalls[Tattach]= rattach;
101 fcalls[Twalk]= rwalk;
102 fcalls[Topen]= ropen;
103 fcalls[Tcreate]= rcreate;
104 fcalls[Tread]= rread;
105 fcalls[Twrite]= rwrite;
106 fcalls[Tclunk]= rclunk;
107 fcalls[Tremove]= rremove;
108 fcalls[Tstat]= rstat;
109 fcalls[Twstat]= rwstat;
112 char Eperm[] = "permission denied";
113 char Enotdir[] = "not a directory";
114 char Enoauth[] = "ramfs: authentication not required";
115 char Enotexist[] = "file does not exist";
116 char Einuse[] = "file in use";
117 char Eexist[] = "file exists";
118 char Eisdir[] = "file is a directory";
119 char Enotowner[] = "not owner";
120 char Eisopen[] = "file already open for I/O";
121 char Excl[] = "exclusive use file already open";
122 char Ename[] = "illegal name";
123 char Eversion[] = "unknown 9P version";
124 char Enotempty[] = "directory not empty";
125 char Ebadfid[] = "bad fid";
131 notifyf(void *a, char *s)
134 if(strncmp(s, "interrupt", 9) == 0)
140 main(int argc, char *argv[])
172 service = EARGF(usage());
179 sysfatal("cannot mount -- not on plan 9");
182 error("pipe failed");
186 if(post9pservice(p[1], service) < 0)
187 sysfatal("post9pservice %s: %r", service);
197 r->perm = DMDIR | 0775;
207 r->name = estrdup(".");
223 r->name = estrdup("file");
226 fmtinstall('F', fcallfmt);
227 switch(rfork(RFFDG|RFPROC|RFNAMEG|RFNOTEG)){
235 close(p[0]); /* don't deadlock if child fails */
246 for(f = fids; f; f = f->next)
249 if(thdr.msize > sizeof mdata)
250 rhdr.msize = sizeof mdata;
252 rhdr.msize = thdr.msize;
253 messagesize = rhdr.msize;
254 if(strncmp(thdr.version, "9P2000", 6) != 0)
256 rhdr.version = "9P2000";
265 return "ramfs: no authentication required";
278 /* no authentication! */
284 rhdr.qid = f->ram->qid;
286 f->user = estrdup(thdr.uname);
289 if(strcmp(user, "none") == 0)
295 clone(Fid *f, Fid **nf)
301 if(f->ram->busy == 0)
303 *nf = newfid(thdr.newfid);
308 (*nf)->user = f->user; /* no ref count; the leakage is minor */
328 if(thdr.newfid != thdr.fid){
332 f = nf; /* walk the new fid */
337 for(i=0; i<thdr.nwname && i<MAXWELEM; i++){
338 if((fram->qid.type & QTDIR) == 0){
347 name = thdr.wname[i];
348 if(strcmp(name, ".") == 0){
351 rhdr.wqid[i] = fram->qid;
354 parent = &ram[fram->parent];
355 if(!perm(f, parent, Pexec)){
359 if(strcmp(name, "..") == 0){
363 for(r=ram; r < &ram[nram]; r++)
364 if(r->busy && r->parent==fram-ram && strcmp(name, r->name)==0){
370 if(i==0 && err == nil)
373 if(nf != nil && (err!=nil || rhdr.nwqid<thdr.nwname)){
374 /* clunk the new fid, which is the one we walked */
375 fprint(2, "f %d zero busy\n", f->fid);
379 if(rhdr.nwqid == thdr.nwname) /* update the fid after a successful walk */
402 if(r->qid.type & QTDIR){
409 /* can't remove root; must be able to write parent */
410 if(r->qid.path==0 || !perm(f, &ram[r->parent], Pwrite))
414 trunc = mode & OTRUNC;
416 if(mode==OWRITE || mode==ORDWR || trunc)
417 if(!perm(f, r, Pwrite))
419 if(mode==OREAD || mode==ORDWR)
420 if(!perm(f, r, Pread))
423 if(!perm(f, r, Pexec))
425 if(trunc && (r->perm&DMAPPEND)==0){
433 rhdr.iounit = messagesize-IOHDRSZ;
450 if(f->ram->busy == 0)
452 parent = f->ram - ram;
453 if((f->ram->qid.type&QTDIR) == 0)
455 /* must be able to write parent */
456 if(!perm(f, f->ram, Pwrite))
460 if(strcmp(name, ".")==0 || strcmp(name, "..")==0)
462 for(r=ram; r<&ram[nram]; r++)
463 if(r->busy && parent==r->parent)
464 if(strcmp((char*)name, r->name)==0)
466 for(r=ram; r->busy; r++)
467 if(r == &ram[Nram-1])
468 return "no free ram resources";
470 r->qid.path = ++path;
473 r->qid.type |= QTDIR;
476 r->name = estrdup(name);
478 r->group = f->ram->group;
479 r->muid = f->ram->muid;
481 prm = (prm&~0777) | (f->ram->perm&prm&0777);
483 prm = (prm&(~0777|0111)) | (f->ram->perm&prm&0666);
490 f->ram->mtime = r->atime;
493 rhdr.iounit = messagesize-IOHDRSZ;
495 if(thdr.mode & ORCLOSE)
511 if(f->ram->busy == 0)
518 if(cnt > messagesize) /* shouldn't happen, anyway */
520 if(f->ram->qid.type & QTDIR){
521 for(r=ram+1; off > 0; r++){
522 if(r->busy && r->parent==f->ram-ram)
523 off -= ramstat(r, statbuf, sizeof statbuf);
524 if(r == &ram[nram-1])
527 for(; r<&ram[nram] && n < cnt; r++){
528 if(!r->busy || r->parent!=f->ram-ram)
530 m = ramstat(r, buf+n, cnt-n);
535 rhdr.data = (char*)rdata;
546 rhdr.data = r->data+off;
564 if(r->perm & DMAPPEND)
567 if(r->qid.type & QTDIR)
569 if(off+cnt >= Maxsize) /* sanity check */
570 return "write too big";
571 if(off+cnt > r->ndata)
572 r->data = erealloc(r->data, off+cnt);
574 memset(r->data+r->ndata, 0, off-r->ndata);
575 if(off+cnt > r->ndata)
577 memmove(r->data+off, thdr.data, cnt);
587 long didx = dr - ram;
590 for(r=ram; r<&ram[nram]; r++)
591 if(r->busy && didx==r->parent)
599 if(r->qid.type & QTDIR && !emptydir(r))
606 memset(&r->qid, 0, sizeof r->qid);
621 e = realremove(f->ram);
622 fprint(2, "clunk fid %d busy=%d\n", f->fid, f->busy);
623 fprint(2, "f %d zero busy\n", f->fid);
637 fprint(2, "f %d zero busy\n", f->fid);
642 if(r->qid.path == 0 || !perm(f, &ram[r->parent], Pwrite))
644 ram[r->parent].mtime = time(0);
645 return realremove(r);
653 if(f->ram->busy == 0)
655 rhdr.nstat = ramstat(f->ram, statbuf, sizeof statbuf);
668 if(f->ram->busy == 0)
670 convM2D(thdr.stat, thdr.nstat, &dir, (char*)statbuf);
674 * To change length, must have write permission on file.
676 if(dir.length!=~0 && dir.length!=r->ndata){
677 if(!perm(f, r, Pwrite))
682 * To change name, must have write permission in parent
683 * and name must be unique.
685 if(dir.name[0]!='\0' && strcmp(dir.name, r->name)!=0){
686 if(!perm(f, &ram[r->parent], Pwrite))
688 for(s=ram; s<&ram[nram]; s++)
689 if(s->busy && s->parent==r->parent)
690 if(strcmp(dir.name, s->name)==0)
695 * To change mode, must be owner or group leader.
696 * Because of lack of users file, leader=>group itself.
698 if(dir.mode!=~0 && r->perm!=dir.mode){
699 if(strcmp(f->user, r->user) != 0)
700 if(strcmp(f->user, r->group) != 0)
705 * To change group, must be owner and member of new group,
706 * or leader of current group and leader of new group.
707 * Second case cannot happen, but we check anyway.
709 if(dir.gid[0]!='\0' && strcmp(r->group, dir.gid)!=0){
710 if(strcmp(f->user, r->user) == 0)
711 // if(strcmp(f->user, dir.gid) == 0)
713 if(strcmp(f->user, r->group) == 0)
714 if(strcmp(f->user, dir.gid) == 0)
722 dir.mode &= ~DMDIR; /* cannot change dir bit */
723 dir.mode |= r->perm&DMDIR;
726 if(dir.name[0] != '\0'){
728 r->name = estrdup(dir.name);
730 if(dir.gid[0] != '\0')
731 r->group = estrdup(dir.gid);
732 if(dir.length!=~0 && dir.length!=r->ndata){
733 r->data = erealloc(r->data, dir.length);
734 if(r->ndata < dir.length)
735 memset(r->data+r->ndata, 0, dir.length-r->ndata);
736 r->ndata = dir.length;
738 ram[r->parent].mtime = time(0);
743 ramstat(Ram *r, uchar *buf, uint nbuf)
751 dir.length = r->ndata;
755 dir.atime = r->atime;
756 dir.mtime = r->mtime;
757 n = convD2M(&dir, buf, nbuf);
769 for(f = fids; f; f = f->next)
771 fprint(2, "got fid %d busy=%d\n", fid, f->busy);
774 else if(!ff && !f->busy)
780 f = emalloc(sizeof *f);
796 snprint(buf, sizeof buf, "/proc/%d/ctl", pid);
797 ctl = open(buf, OWRITE);
799 fprint(2, "can't protect ramfs\n");
801 fprint(ctl, "noswap\n");
802 fprint(ctl, "private\n");
809 * reading from a pipe or a network device
810 * will give an error after a few eof reads.
811 * however, we cannot tell the difference
812 * between a zero-length read and an interrupt
813 * on the processes writing to us,
814 * so we wait for the error.
816 n = read9pmsg(mfd[0], mdata, messagesize);
821 if(convM2S(mdata, n, &thdr) == 0)
825 fprint(2, "ramfs %d:<-%F\n", pid, &thdr);
827 if(!fcalls[thdr.type])
828 err = "bad fcall type";
830 err = (*fcalls[thdr.type])(newfid(thdr.fid));
835 rhdr.type = thdr.type + 1;
840 fprint(2, "ramfs %d:->%F\n", pid, &rhdr);/**/
841 n = convS2M(&rhdr, mdata, messagesize);
843 error("convS2M error on write");
844 if(write(mfd[1], mdata, n) != n)
845 error("mount write");
850 perm(Fid *f, Ram *r, int p)
852 if((p*Pother) & r->perm)
854 if(strcmp(f->user, r->group)==0 && ((p*Pgroup) & r->perm))
856 if(strcmp(f->user, r->user)==0 && ((p*Powner) & r->perm))
864 fprint(2, "%s: %s: %r\n", argv0, s);
875 error("out of memory");
881 erealloc(void *p, ulong n)
885 error("out of memory");
898 error("out of memory");
906 fprint(2, "usage: %s [-is] [-m mountpoint]\n", argv0);