6 * Rather than reading /adm/users, which is a lot of work for
7 * a toy program, we assume all groups have the form
9 * meaning that each user is the leader of his own group.
14 OPERM = 0x3, /* mask of all permission types in open mode */
16 Maxsize = 512*1024*1024,
20 typedef struct Fid Fid;
21 typedef struct Ram Ram;
38 long parent; /* index in Ram array */
61 ulong path; /* incremented for each new file */
67 uchar mdata[IOHDRSZ+Maxfdata];
68 uchar rdata[Maxfdata]; /* buffer for data in reply */
69 uchar statbuf[STATMAX];
72 int messagesize = sizeof mdata;
75 uint ramstat(Ram*, uchar*, uint);
78 void *erealloc(void*, ulong);
82 int perm(Fid*, Ram*, int);
84 char *rflush(Fid*), *rversion(Fid*), *rauth(Fid*),
85 *rattach(Fid*), *rwalk(Fid*),
86 *ropen(Fid*), *rcreate(Fid*),
87 *rread(Fid*), *rwrite(Fid*), *rclunk(Fid*),
88 *rremove(Fid*), *rstat(Fid*), *rwstat(Fid*);
90 char *(*fcalls[Tmax])(Fid*);
95 fcalls[Tversion]= rversion;
96 fcalls[Tflush]= rflush;
98 fcalls[Tattach]= rattach;
100 fcalls[Topen]= ropen;
101 fcalls[Tcreate]= rcreate;
102 fcalls[Tread]= rread;
103 fcalls[Twrite]= rwrite;
104 fcalls[Tclunk]= rclunk;
105 fcalls[Tremove]= rremove;
106 fcalls[Tstat]= rstat;
107 fcalls[Twstat]= rwstat;
110 char Eperm[] = "permission denied";
111 char Enotdir[] = "not a directory";
112 char Enoauth[] = "ramfs: authentication not required";
113 char Enotexist[] = "file does not exist";
114 char Einuse[] = "file in use";
115 char Eexist[] = "file exists";
116 char Eisdir[] = "file is a directory";
117 char Enotowner[] = "not owner";
118 char Eisopen[] = "file already open for I/O";
119 char Excl[] = "exclusive use file already open";
120 char Ename[] = "illegal name";
121 char Eversion[] = "unknown 9P version";
122 char Enotempty[] = "directory not empty";
123 char Ebadfid[] = "bad fid";
129 notifyf(void *a, char *s)
132 if(strncmp(s, "interrupt", 9) == 0)
138 main(int argc, char *argv[])
170 service = EARGF(usage());
177 error("pipe failed");
181 if(post9pservice(p[1], service) < 0)
182 sysfatal("post9pservice %s: %r", service);
192 r->perm = DMDIR | 0775;
202 r->name = estrdup(".");
218 r->name = estrdup("file");
221 fmtinstall('F', fcallfmt);
222 switch(rfork(RFFDG|RFPROC|RFNAMEG|RFNOTEG)){
230 close(p[0]); /* don't deadlock if child fails */
241 for(f = fids; f; f = f->next)
244 if(thdr.msize > sizeof mdata)
245 rhdr.msize = sizeof mdata;
247 rhdr.msize = thdr.msize;
248 messagesize = rhdr.msize;
249 if(strncmp(thdr.version, "9P2000", 6) != 0)
251 rhdr.version = "9P2000";
260 return "ramfs: no authentication required";
273 /* no authentication! */
279 rhdr.qid = f->ram->qid;
281 f->user = estrdup(thdr.uname);
284 if(strcmp(user, "none") == 0)
290 xclone(Fid *f, Fid **nf)
296 if(f->ram->busy == 0)
298 *nf = newfid(thdr.newfid);
303 (*nf)->user = f->user; /* no ref count; the leakage is minor */
323 if(thdr.newfid != thdr.fid){
324 err = xclone(f, &nf);
327 f = nf; /* walk the new fid */
332 for(i=0; i<thdr.nwname && i<MAXWELEM; i++){
333 if((fram->qid.type & QTDIR) == 0){
342 name = thdr.wname[i];
343 if(strcmp(name, ".") == 0){
346 rhdr.wqid[i] = fram->qid;
349 parent = &ram[fram->parent];
350 if(!perm(f, parent, Pexec)){
354 if(strcmp(name, "..") == 0){
358 for(r=ram; r < &ram[nram]; r++)
359 if(r->busy && r->parent==fram-ram && strcmp(name, r->name)==0){
365 if(i==0 && err == nil)
368 if(nf != nil && (err!=nil || rhdr.nwqid<thdr.nwname)){
369 /* clunk the new fid, which is the one we walked */
370 fprint(2, "f %d zero busy\n", f->fid);
374 if(rhdr.nwqid == thdr.nwname) /* update the fid after a successful walk */
397 if(r->qid.type & QTDIR){
404 /* can't remove root; must be able to write parent */
405 if(r->qid.path==0 || !perm(f, &ram[r->parent], Pwrite))
409 trunc = mode & OTRUNC;
411 if(mode==OWRITE || mode==ORDWR || trunc)
412 if(!perm(f, r, Pwrite))
414 if(mode==OREAD || mode==ORDWR)
415 if(!perm(f, r, Pread))
418 if(!perm(f, r, Pexec))
420 if(trunc && (r->perm&DMAPPEND)==0){
428 rhdr.iounit = messagesize-IOHDRSZ;
445 if(f->ram->busy == 0)
447 parent = f->ram - ram;
448 if((f->ram->qid.type&QTDIR) == 0)
450 /* must be able to write parent */
451 if(!perm(f, f->ram, Pwrite))
455 if(strcmp(name, ".")==0 || strcmp(name, "..")==0)
457 for(r=ram; r<&ram[nram]; r++)
458 if(r->busy && parent==r->parent)
459 if(strcmp((char*)name, r->name)==0)
461 for(r=ram; r->busy; r++)
462 if(r == &ram[Nram-1])
463 return "no free ram resources";
465 r->qid.path = ++path;
468 r->qid.type |= QTDIR;
471 r->name = estrdup(name);
473 r->group = f->ram->group;
474 r->muid = f->ram->muid;
476 prm = (prm&~0777) | (f->ram->perm&prm&0777);
478 prm = (prm&(~0777|0111)) | (f->ram->perm&prm&0666);
485 f->ram->mtime = r->atime;
488 rhdr.iounit = messagesize-IOHDRSZ;
490 if(thdr.mode & ORCLOSE)
506 if(f->ram->busy == 0)
513 if(cnt > messagesize) /* shouldn't happen, anyway */
515 if(f->ram->qid.type & QTDIR){
516 for(r=ram+1; off > 0; r++){
517 if(r->busy && r->parent==f->ram-ram)
518 off -= ramstat(r, statbuf, sizeof statbuf);
519 if(r == &ram[nram-1])
522 for(; r<&ram[nram] && n < cnt; r++){
523 if(!r->busy || r->parent!=f->ram-ram)
525 m = ramstat(r, buf+n, cnt-n);
530 rhdr.data = (char*)rdata;
541 rhdr.data = r->data+off;
559 if(r->perm & DMAPPEND)
562 if(r->qid.type & QTDIR)
564 if(off+cnt >= Maxsize) /* sanity check */
565 return "write too big";
566 if(off+cnt > r->ndata)
567 r->data = erealloc(r->data, off+cnt);
569 memset(r->data+r->ndata, 0, off-r->ndata);
570 if(off+cnt > r->ndata)
572 memmove(r->data+off, thdr.data, cnt);
582 long didx = dr - ram;
585 for(r=ram; r<&ram[nram]; r++)
586 if(r->busy && didx==r->parent)
594 if(r->qid.type & QTDIR && !emptydir(r))
601 memset(&r->qid, 0, sizeof r->qid);
616 e = realremove(f->ram);
617 fprint(2, "clunk fid %d busy=%d\n", f->fid, f->busy);
618 fprint(2, "f %d zero busy\n", f->fid);
632 fprint(2, "f %d zero busy\n", f->fid);
637 if(r->qid.path == 0 || !perm(f, &ram[r->parent], Pwrite))
639 ram[r->parent].mtime = time(0);
640 return realremove(r);
648 if(f->ram->busy == 0)
650 rhdr.nstat = ramstat(f->ram, statbuf, sizeof statbuf);
663 if(f->ram->busy == 0)
665 convM2D(thdr.stat, thdr.nstat, &dir, (char*)statbuf);
669 * To change length, must have write permission on file.
671 if(dir.length!=~0 && dir.length!=r->ndata){
672 if(!perm(f, r, Pwrite))
677 * To change name, must have write permission in parent
678 * and name must be unique.
680 if(dir.name[0]!='\0' && strcmp(dir.name, r->name)!=0){
681 if(!perm(f, &ram[r->parent], Pwrite))
683 for(s=ram; s<&ram[nram]; s++)
684 if(s->busy && s->parent==r->parent)
685 if(strcmp(dir.name, s->name)==0)
690 * To change mode, must be owner or group leader.
691 * Because of lack of users file, leader=>group itself.
693 if(dir.mode!=~0 && r->perm!=dir.mode){
694 if(strcmp(f->user, r->user) != 0)
695 if(strcmp(f->user, r->group) != 0)
700 * To change group, must be owner and member of new group,
701 * or leader of current group and leader of new group.
702 * Second case cannot happen, but we check anyway.
704 if(dir.gid[0]!='\0' && strcmp(r->group, dir.gid)!=0){
705 if(strcmp(f->user, r->user) == 0)
706 /* if(strcmp(f->user, dir.gid) == 0) */
708 if(strcmp(f->user, r->group) == 0)
709 if(strcmp(f->user, dir.gid) == 0)
717 dir.mode &= ~DMDIR; /* cannot change dir bit */
718 dir.mode |= r->perm&DMDIR;
721 if(dir.name[0] != '\0'){
723 r->name = estrdup(dir.name);
725 if(dir.gid[0] != '\0')
726 r->group = estrdup(dir.gid);
727 if(dir.length!=~0 && dir.length!=r->ndata){
728 r->data = erealloc(r->data, dir.length);
729 if(r->ndata < dir.length)
730 memset(r->data+r->ndata, 0, dir.length-r->ndata);
731 r->ndata = dir.length;
733 ram[r->parent].mtime = time(0);
738 ramstat(Ram *r, uchar *buf, uint nbuf)
746 dir.length = r->ndata;
750 dir.atime = r->atime;
751 dir.mtime = r->mtime;
752 n = convD2M(&dir, buf, nbuf);
764 for(f = fids; f; f = f->next)
767 else if(!ff && !f->busy)
773 f = emalloc(sizeof *f);
789 snprint(buf, sizeof buf, "/proc/%d/ctl", pid);
790 ctl = open(buf, OWRITE);
792 fprint(2, "can't protect ramfs\n");
794 fprint(ctl, "noswap\n");
795 fprint(ctl, "private\n");
802 * reading from a pipe or a network device
803 * will give an error after a few eof reads.
804 * however, we cannot tell the difference
805 * between a zero-length read and an interrupt
806 * on the processes writing to us,
807 * so we wait for the error.
809 n = read9pmsg(mfd[0], mdata, messagesize);
814 if(convM2S(mdata, n, &thdr) == 0)
818 fprint(2, "ramfs %d:<-%F\n", pid, &thdr);
820 if(!fcalls[thdr.type])
821 err = "bad fcall type";
823 err = (*fcalls[thdr.type])(newfid(thdr.fid));
828 rhdr.type = thdr.type + 1;
833 fprint(2, "ramfs %d:->%F\n", pid, &rhdr);/**/
834 n = convS2M(&rhdr, mdata, messagesize);
836 error("convS2M error on write");
837 if(write(mfd[1], mdata, n) != n)
838 error("mount write");
843 perm(Fid *f, Ram *r, int p)
845 if((p*Pother) & r->perm)
847 if(strcmp(f->user, r->group)==0 && ((p*Pgroup) & r->perm))
849 if(strcmp(f->user, r->user)==0 && ((p*Powner) & r->perm))
857 fprint(2, "%s: %s: %r\n", argv0, s);
868 error("out of memory");
874 erealloc(void *p, ulong n)
878 error("out of memory");
891 error("out of memory");
899 fprint(2, "usage: %s [-is] [-m mountpoint]\n", argv0);