#include #include #include #include enum { OPERM = 0x3, /* mask of all permission types in open mode */ Qdir = 0, Qcons, Qkbdmap, }; typedef struct Fid Fid; struct Fid { short busy; short open; int fid; Qid qid; Fid *next; }; typedef struct Request Request; struct Request { Fcall; Request *next; }; Request *reqq; Request *lastreq; Lock reqlock; typedef struct Dirtab Dirtab; struct Dirtab { char *name; Qid qid; ulong length; long perm; }; Dirtab dirtab[] = { ".", {CHDIR|Qdir}, 0, 0555, "cons", {Qcons}, 0, 0660, "kbdmap", {Qkbdmap}, 0, 0660, nil, }; ulong path; /* incremented for each new file */ Fid *fids; int mfd[2]; char mdata[MAXMSG+MAXFDATA]; char *user; Fcall rhdr; Fcall thdr; int curtag; /* tag for request in the current read */ int readerpid; /* pid of reading process */ Rune kbdmap[128]; Fid * newfid(int); void error(char*); void io(void); void *erealloc(void*, ulong); void *emalloc(ulong); void efree(void*); void usage(void); void reader(void); char *rflush(Fid*), *rnop(Fid*), *rsession(Fid*), *rattach(Fid*), *rclone(Fid*), *rwalk(Fid*), *rclwalk(Fid*), *ropen(Fid*), *rcreate(Fid*), *rread(Fid*), *rwrite(Fid*), *rclunk(Fid*), *rremove(Fid*), *rstat(Fid*), *rwstat(Fid*); char *(*fcalls[])(Fid*) = { [Tflush] rflush, [Tsession] rsession, [Tnop] rnop, [Tattach] rattach, [Tclone] rclone, [Twalk] rwalk, [Tclwalk] rclwalk, [Topen] ropen, [Tcreate] rcreate, [Tread] rread, [Twrite] rwrite, [Tclunk] rclunk, [Tremove] rremove, [Tstat] rstat, [Twstat] rwstat, }; char Eperm[] = "permission denied"; char Enotdir[] = "not a directory"; char Enotexist[] = "file does not exist"; char Eisopen[] = "file already open for I/O"; char Ebadarg[]= "bad argument"; char Eslave[] = "request handled by slave"; int debug; int consfd; void notifyf(void *a, char *s) { USED(a); if(strncmp(s, "interrupt", 9) == 0) noted(NCONT); noted(NDFLT); } void main(int argc, char *argv[]) { char *defmnt; int p[2]; /* get user id */ user = getuser(); if(user == nil || *user == 0) user = "unknown"; consfd = open("/dev/cons", ORDWR); if(consfd < 0) error("can't open /dev/cons"); defmnt = "/dev"; ARGBEGIN{ case 'd': debug = 1; break; default: usage(); }ARGEND if(pipe(p) < 0) error("pipe failed"); mfd[0] = p[0]; mfd[1] = p[0]; notify(notifyf); if(debug) fmtinstall('F', fcallconv); switch(rfork(RFFDG|RFPROC|RFNAMEG|RFNOTEG)){ case -1: error("fork"); case 0: close(p[1]); io(); break; default: close(p[0]); /* don't deadlock if child fails */ if(mount(p[1], defmnt, MBEFORE, "") < 0) error("mount failed"); } exits(0); } char* rnop(Fid *f) { USED(f); return 0; } char* rsession(Fid *unused) { Fid *f; USED(unused); for(f = fids; f; f = f->next) if(f->busy) rclunk(f); memset(thdr.authid, 0, sizeof(thdr.authid)); memset(thdr.authdom, 0, sizeof(thdr.authdom)); memset(thdr.chal, 0, sizeof(thdr.chal)); return 0; } char* rflush(Fid*) { Request **l, *r; lock(&reqlock); /* dequeue it if we can */ for(l = &reqq; *l; l = &r->next){ r = *l; if(r->tag == rhdr.oldtag){ *l = r->next; efree(r); unlock(&reqlock); return 0; } } /* if its the current read, interrupt the reader */ if(rhdr.oldtag == curtag){ curtag = -1; postnote(PNPROC, readerpid, "wakey wakey"); } unlock(&reqlock); return 0; } char* rattach(Fid *f) { /* no authentication! */ f->busy = 1; f->qid = (Qid){CHDIR|Qdir, 0}; thdr.qid = f->qid; return 0; } char* rclone(Fid *f) { Fid *nf; if(f->open) return Eisopen; // if(strcmp(rhdr.uname, user) != 0) // return Eperm; nf = newfid(rhdr.newfid); nf->busy = 1; nf->open = 0; nf->qid = f->qid; thdr.qid = nf->qid; return 0; } char* rwalk(Fid *f) { Dirtab *dt; char *name; if((f->qid.path & CHDIR) == 0) return Enotdir; name = rhdr.name; if(strcmp(name, "..") == 0){ thdr.qid = (Qid){CHDIR|Qdir, 0}; f->qid = thdr.qid; return 0; } for(dt = dirtab+1; dt->name != nil; dt++){ if(strcmp(dt->name, name) == 0){ f->qid = dt->qid; thdr.qid = f->qid; return 0; } } return Enotexist; } char * rclwalk(Fid *f) { Fid *nf; char *err; if((f->qid.path & CHDIR) == 0) return Enotdir; nf = newfid(rhdr.newfid); nf->busy = 1; if(err = rwalk(nf)) rclunk(nf); return err; } char * ropen(Fid *f) { int mode; if(f->open) return Eisopen; mode = rhdr.mode; if(f->qid.path & CHDIR){ if(mode != OREAD) return Eperm; thdr.qid = f->qid; return 0; } thdr.qid = f->qid; f->open = 1; return 0; } char * rcreate(Fid*) { return Eperm; } void dirtabstat(Dirtab *dt, char *buf) { Dir dir; memmove(dir.name, dt->name, NAMELEN); dir.qid = dt->qid; dir.mode = dt->perm; dir.length = dt->length; strncpy(dir.uid, user, NAMELEN); strncpy(dir.gid, user, NAMELEN); dir.atime = 0; dir.mtime = 0; convD2M(&dir, buf); } enum { Linelen = 8, }; char* rread(Fid *f) { char *buf; long off; int i, n, cnt; Dirtab *dt; Request *r; if(f->qid.path & CHDIR){ /* read the directory */ n = 0; thdr.count = 0; off = rhdr.offset; buf = thdr.data; cnt = (rhdr.count/DIRLEN)*DIRLEN; if(off%DIRLEN) return "i/o error"; for(dt = dirtab+1; dt->name != nil && off > 0; dt++) off -= DIRLEN; for(; dt->name != nil && n < cnt; dt++){ dirtabstat(dt, buf+n); n += DIRLEN; } thdr.count = n; return 0; } if(f->qid.path == Qkbdmap){ /* read the keybord map */ n = 0; off = rhdr.offset; buf = thdr.data; cnt = (rhdr.count/Linelen)*Linelen; if(off%8) return "i/o error"; for(i = 0; i < Runeself && off > 0; i++) if(kbdmap[i]) off -= Linelen; for(; i < Runeself && n < cnt; i++) if(kbdmap[i]){ sprint(buf+n, "%2.2ux %4.4ux\n", i, kbdmap[i]); n += Linelen; } thdr.count = n; return 0; } /* pass request to reader */ r = emalloc(sizeof(Request)); memmove(r, &rhdr, sizeof(rhdr)); r->next = nil; lock(&reqlock); if(reqq == nil){ reqq = r; rendezvous(1, 0); /* wakeup reader */ }else lastreq->next = r; lastreq = r; unlock(&reqlock); return Eslave; } void handlenote(void*, char *msg) { if(strstr(msg, "wakey") != nil) noted(NCONT); noted(NDFLT); } void reader(void) { Request *r; int n; Fcall thdr; char buf1[4*1024]; char buf2[12*1024]; char mdata[MAXMSG+MAXFDATA]; char *from, *to, *e; int c; /* fork off a reader */ readerpid = rfork(RFPROC|RFMEM); switch(readerpid){ case -1: exits("out of procs"); case 0: break; default: return; } notify(handlenote); for(;;){ lock(&reqlock); r = reqq; if(r == nil){ unlock(&reqlock); rendezvous(1, 0); continue; } curtag = r->tag; reqq = r->next; unlock(&reqlock); if(r->count > sizeof(buf1)) r->count = sizeof(buf1); n = read(consfd, buf1, r->count); if(n < 0){ if(curtag == -1){ /* we've been interrupted by a flush, forget request */ efree(r); continue; } thdr.type = Rerror; errstr(thdr.ename); } else { from = buf1; to = buf2; for(e = buf1 + n; from < e; from++){ c = *from & 0xff; if(c >= Runeself || kbdmap[c] == 0) *to++ = c; else { n = runetochar(to, &kbdmap[c]); to += n; } } n = to - buf2; thdr.data = buf2; thdr.type = r->type + 1; thdr.fid = r->fid; thdr.count = n; } thdr.tag = r->tag; if(debug) fprint(2, "kbdmap:->%F\n", &thdr); n = convS2M(&thdr, mdata); curtag = -1; /* we're done with this one */ if(write(mfd[1], mdata, n) != n) error("mount write"); efree(r); } } char* rwrite(Fid *f) { int cnt, from, n; char *p; ulong to; static char err[ERRLEN]; cnt = rhdr.count; if(f->qid.path & CHDIR) return "file is a directory"; switch(f->qid.path){ case Qcons: /* write to the real console */ n = write(consfd, rhdr.data, cnt); if(n < 0){ errstr(err); return err; } thdr.count = n; break; case Qkbdmap: p = rhdr.data; /* flush all mappings */ if(strncmp(p, "flush", 5) == 0){ memset(kbdmap, 0, sizeof(kbdmap)); break; } /* pick up keyboard mappings, they are space or * newline separated pairs of "hex-ascii hex-unicode" */ if(cnt >= MAXFDATA) cnt = MAXFDATA - 1; p[cnt] = 0; while(*p){ while(*p == ' ' || *p == '\t' || *p == '\n') p++; from = strtoul(p, &p, 16); while(*p == ' ' || *p == '\t' || *p == '\n') p++; to = strtoul(p, &p, 16); if(from >= Runeself || from < 0 || (to & 0xffff0000) != 0) return "bad from mapping"; kbdmap[from] = to; } break; default: return Ebadarg; } return nil; } char * rclunk(Fid *f) { f->busy = 0; f->open = 0; return 0; } char * rremove(Fid*) { return Eperm; } char * rstat(Fid *f) { Dirtab *dt; for(dt = dirtab; dt->name != nil; dt++) if(f->qid.path == dt->qid.path){ dirtabstat(dt, thdr.stat); return 0; } return Enotexist; } char * rwstat(Fid*) { return Eperm; } Fid * newfid(int fid) { Fid *f, *ff; ff = 0; for(f = fids; f; f = f->next) if(f->fid == fid) return f; else if(!ff && !f->busy) ff = f; if(ff){ ff->fid = fid; return ff; } f = emalloc(sizeof *f); f->fid = fid; f->next = fids; fids = f; return f; } void io(void) { char *err; int n; /* fork off a reading process */ reader(); for(;;){ /* * reading from a pipe or a network device * will give an error after a few eof reads * however, we cannot tell the difference * between a zero-length read and an interrupt * on the processes writing to us, * so we wait for the error */ n = read(mfd[0], mdata, sizeof mdata); if(n == 0) continue; if(n < 0) error("mount read"); if(convM2S(mdata, &rhdr, n) == 0) continue; if(debug) fprint(2, "kbdmap:<-%F\n", &rhdr); thdr.data = mdata + MAXMSG; if(!fcalls[rhdr.type]) err = "bad fcall type"; else err = (*fcalls[rhdr.type])(newfid(rhdr.fid)); if(err){ if(err == Eslave) continue; thdr.type = Rerror; strncpy(thdr.ename, err, ERRLEN); }else{ thdr.type = rhdr.type + 1; thdr.fid = rhdr.fid; } thdr.tag = rhdr.tag; if(debug) fprint(2, "kbdmap:->%F\n", &thdr);/**/ n = convS2M(&thdr, mdata); if(write(mfd[1], mdata, n) != n) error("mount write"); } } void error(char *s) { fprint(2, "%s: %s: %r\n", argv0, s); postnote(PNGROUP, getpid(), "you will be dying now please?"); exits(s); } Lock malloclock; void * emalloc(ulong n) { void *p; lock(&malloclock); p = mallocz(n, 1); unlock(&malloclock); if(!p) error("out of memory"); return p; } void * erealloc(void *p, ulong n) { lock(&malloclock); p = realloc(p, n); unlock(&malloclock); if(!p) error("out of memory"); return p; } void efree(void *p) { lock(&malloclock); free(p); unlock(&malloclock); } void usage(void) { fprint(2, "usage: %s [-d]\n", argv0); exits("usage"); }