7 * Rather than reading /adm/users, which is a lot of work for
8 * a toy program, we assume all groups have the form
10 * meaning that each user is the leader of his own group.
15 OPERM = 0x3, /* mask of all permission types in open mode */
17 Maxsize = 768*1024*1024,
19 Maxulong= (1ULL << 32) - 1,
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*);
108 char *(*fcalls[])(Fid*) = {
124 char Eperm[] = "permission denied";
125 char Enotdir[] = "not a directory";
126 char Enoauth[] = "ramfs: authentication not required";
127 char Enotexist[] = "file does not exist";
128 char Einuse[] = "file in use";
129 char Eexist[] = "file exists";
130 char Eisdir[] = "file is a directory";
131 char Enotowner[] = "not owner";
132 char Eisopen[] = "file already open for I/O";
133 char Excl[] = "exclusive use file already open";
134 char Ename[] = "illegal name";
135 char Eversion[] = "unknown 9P version";
136 char Enotempty[] = "directory not empty";
141 static int memlim = 1;
144 notifyf(void *a, char *s)
147 if(strncmp(s, "interrupt", 9) == 0)
153 main(int argc, char *argv[])
156 char *defmnt, *service;
173 defmnt = EARGF(usage());
182 memlim = 0; /* unlimited memory consumption */
189 service = EARGF(usage());
192 mountflags |= MBEFORE;
195 mountflags |= MCREATE;
198 mountflags |= MAFTER;
204 mountflags = MREPL | MCREATE;
207 error("pipe failed");
213 snprint(buf, sizeof buf, "#s/%s", service);
214 fd = create(buf, OWRITE|ORCLOSE, 0666);
216 error("create failed");
217 sprint(buf, "%d", p[1]);
218 if(write(fd, buf, strlen(buf)) < 0)
219 error("writing service file");
230 r->perm = DMDIR | 0775;
240 r->name = estrdup(".");
243 fmtinstall('F', fcallfmt);
244 fmtinstall('M', dirmodefmt);
246 switch(rfork(RFFDG|RFPROC|RFNAMEG|RFNOTEG)){
254 close(p[0]); /* don't deadlock if child fails */
255 if(defmnt && mount(p[1], -1, defmnt, mountflags, "") < 0)
256 error("mount failed");
266 for(f = fids; f; f = f->next)
269 if(thdr.msize > sizeof mdata)
270 rhdr.msize = sizeof mdata;
272 rhdr.msize = thdr.msize;
273 messagesize = rhdr.msize;
274 if(strncmp(thdr.version, "9P2000", 6) != 0)
276 rhdr.version = "9P2000";
283 return "ramfs: no authentication required";
296 /* no authentication! */
300 rhdr.qid = f->ram->qid;
302 f->user = estrdup(thdr.uname);
305 if(strcmp(user, "none") == 0)
311 clone(Fid *f, Fid **nf)
315 if(f->ram->busy == 0)
317 *nf = newfid(thdr.newfid);
322 (*nf)->user = f->user; /* no ref count; the leakage is minor */
340 if(thdr.newfid != thdr.fid){
344 f = nf; /* walk the new fid */
349 for(i=0; i<thdr.nwname && i<MAXWELEM; i++){
350 if((fram->qid.type & QTDIR) == 0){
359 name = thdr.wname[i];
360 if(strcmp(name, ".") == 0){
363 rhdr.wqid[i] = fram->qid;
366 parent = &ram[fram->parent];
367 if(!perm(f, parent, Pexec)){
371 if(strcmp(name, "..") == 0){
375 for(r=ram; r < &ram[nram]; r++)
376 if(r->busy && r->parent==fram-ram && strcmp(name, r->name)==0){
382 if(i==0 && err == nil)
385 if(nf != nil && (err!=nil || rhdr.nwqid<thdr.nwname)){
386 /* clunk the new fid, which is the one we walked */
391 err = nil; /* didn't get everything in 9P2000 right! */
392 if(rhdr.nwqid == thdr.nwname) /* update the fid after a successful walk */
412 if(r->qid.type & QTDIR){
419 /* can't remove root; must be able to write parent */
420 if(r->qid.path==0 || !perm(f, &ram[r->parent], Pwrite))
424 trunc = mode & OTRUNC;
426 if(mode==OWRITE || mode==ORDWR || trunc)
427 if(!perm(f, r, Pwrite))
429 if(mode==OREAD || mode==ORDWR)
430 if(!perm(f, r, Pread))
433 if(!perm(f, r, Pexec))
435 if(trunc && (r->perm&DMAPPEND)==0){
443 rhdr.iounit = messagesize-IOHDRSZ;
458 if(f->ram->busy == 0)
460 parent = f->ram - ram;
461 if((f->ram->qid.type&QTDIR) == 0)
463 /* must be able to write parent */
464 if(!perm(f, f->ram, Pwrite))
468 if(strcmp(name, ".")==0 || strcmp(name, "..")==0)
470 for(r=ram; r<&ram[nram]; r++)
471 if(r->busy && parent==r->parent)
472 if(strcmp((char*)name, r->name)==0)
474 for(r=ram; r->busy; r++)
475 if(r == &ram[Nram-1])
476 return "no free ram resources";
478 r->qid.path = ++path;
481 r->qid.type |= QTDIR;
484 r->name = estrdup(name);
486 r->group = f->ram->group;
487 r->muid = f->ram->muid;
489 prm = (prm&~0777) | (f->ram->perm&prm&0777);
491 prm = (prm&(~0777|0111)) | (f->ram->perm&prm&0666);
498 f->ram->mtime = r->atime;
501 rhdr.iounit = messagesize-IOHDRSZ;
503 if(thdr.mode & ORCLOSE)
517 if(f->ram->busy == 0)
521 rhdr.data = (char*)rdata;
523 return "negative seek offset";
527 if(cnt > messagesize) /* shouldn't happen, anyway */
530 return "negative read count";
531 if(f->ram->qid.type & QTDIR){
532 for(r=ram+1; off > 0; r++){
533 if(r->busy && r->parent==f->ram-ram)
534 off -= ramstat(r, statbuf, sizeof statbuf);
535 if(r == &ram[nram-1])
538 for(; r<&ram[nram] && n < cnt; r++){
539 if(!r->busy || r->parent!=f->ram-ram)
541 m = ramstat(r, buf+n, cnt-n);
546 rhdr.data = (char*)rdata;
557 rhdr.data = r->data+off;
574 return "negative seek offset";
576 if(r->perm & DMAPPEND)
580 return "negative write count";
581 if(r->qid.type & QTDIR)
583 if(memlim && off+cnt >= Maxsize) /* sanity check */
584 return "write too big";
585 if(off+cnt > r->ndata)
586 r->data = erealloc(r->data, off+cnt);
588 memset(r->data+r->ndata, 0, off-r->ndata);
589 if(off+cnt > r->ndata)
591 memmove(r->data+off, thdr.data, cnt);
601 long didx = dr - ram;
604 for(r=ram; r<&ram[nram]; r++)
605 if(r->busy && didx==r->parent)
613 if(r->qid.type & QTDIR && !emptydir(r))
620 memset(&r->qid, 0, sizeof r->qid);
635 e = realremove(f->ram);
653 if(r->qid.path == 0 || !perm(f, &ram[r->parent], Pwrite))
655 ram[r->parent].mtime = time(0);
656 return realremove(r);
662 if(f->ram->busy == 0)
664 rhdr.nstat = ramstat(f->ram, statbuf, sizeof statbuf);
675 if(f->ram->busy == 0)
677 convM2D(thdr.stat, thdr.nstat, &dir, (char*)statbuf);
681 * To change length, must have write permission on file.
683 if(dir.length!=~0 && dir.length!=r->ndata){
684 if(!perm(f, r, Pwrite))
689 * To change name, must have write permission in parent
690 * and name must be unique.
692 if(dir.name[0]!='\0' && strcmp(dir.name, r->name)!=0){
693 if(!perm(f, &ram[r->parent], Pwrite))
695 for(s=ram; s<&ram[nram]; s++)
696 if(s->busy && s->parent==r->parent)
697 if(strcmp(dir.name, s->name)==0)
702 * To change mode, must be owner or group leader.
703 * Because of lack of users file, leader=>group itself.
705 if(dir.mode!=~0 && r->perm!=dir.mode){
706 if(strcmp(f->user, r->user) != 0)
707 if(strcmp(f->user, r->group) != 0)
712 * To change group, must be owner and member of new group,
713 * or leader of current group and leader of new group.
714 * Second case cannot happen, but we check anyway.
716 if(dir.gid[0]!='\0' && strcmp(r->group, dir.gid)!=0){
717 if(strcmp(f->user, r->user) == 0)
718 // if(strcmp(f->user, dir.gid) == 0)
720 if(strcmp(f->user, r->group) == 0)
721 if(strcmp(f->user, dir.gid) == 0)
729 dir.mode &= ~DMDIR; /* cannot change dir bit */
730 dir.mode |= r->perm&DMDIR;
733 if(dir.name[0] != '\0'){
735 r->name = estrdup(dir.name);
737 if(dir.gid[0] != '\0')
738 r->group = estrdup(dir.gid);
739 if(dir.length!=~0 && dir.length!=r->ndata){
740 r->data = erealloc(r->data, dir.length);
741 if(r->ndata < dir.length)
742 memset(r->data+r->ndata, 0, dir.length-r->ndata);
743 r->ndata = dir.length;
745 ram[r->parent].mtime = time(0);
750 ramstat(Ram *r, uchar *buf, uint nbuf)
758 dir.length = r->ndata;
762 dir.atime = r->atime;
763 dir.mtime = r->mtime;
764 n = convD2M(&dir, buf, nbuf);
776 for(f = fids; f; f = f->next)
779 else if(!ff && !f->busy)
785 f = emalloc(sizeof *f);
802 snprint(buf, sizeof buf, "/proc/%d/ctl", pid);
803 ctl = open(buf, OWRITE);
805 fprint(2, "can't protect ramfs\n");
807 fprint(ctl, "noswap\n");
808 fprint(ctl, "private\n");
815 * reading from a pipe or a network device
816 * will give an error after a few eof reads.
817 * however, we cannot tell the difference
818 * between a zero-length read and an interrupt
819 * on the processes writing to us,
820 * so we wait for the error.
822 n = read9pmsg(mfd[0], mdata, messagesize);
824 rerrstr(buf, sizeof buf);
825 if(buf[0]=='\0' || strstr(buf, "hungup"))
831 if(convM2S(mdata, n, &thdr) == 0)
835 fprint(2, "ramfs %d:<-%F\n", pid, &thdr);
837 if(thdr.type<0 || thdr.type>=nelem(fcalls) || !fcalls[thdr.type])
838 err = "bad fcall type";
839 else if(((fid=newfid(thdr.fid))==nil || !fid->ram) && needfid[thdr.type])
840 err = "fid not in use";
842 err = (*fcalls[thdr.type])(fid);
847 rhdr.type = thdr.type + 1;
852 fprint(2, "ramfs %d:->%F\n", pid, &rhdr);/**/
853 n = convS2M(&rhdr, mdata, messagesize);
855 error("convS2M error on write");
856 if(write(mfd[1], mdata, n) != n)
857 error("mount write");
862 perm(Fid *f, Ram *r, int p)
864 if((p*Pother) & r->perm)
866 if(strcmp(f->user, r->group)==0 && ((p*Pgroup) & r->perm))
868 if(strcmp(f->user, r->user)==0 && ((p*Powner) & r->perm))
876 fprint(2, "%s: %s: %r\n", argv0, s);
887 error("out of memory");
893 erealloc(void *p, ulong n)
897 error("out of memory");
910 error("out of memory");
918 fprint(2, "usage: %s [-Dipsubac] [-m mountpoint] [-S srvname]\n", argv0);