#include "u.h" #include "lib.h" #include "mem.h" #include "dat.h" #include "fns.h" #include "io.h" #include "../port/error.h" #include "devtab.h" struct { IOQ; /* lock to klogputs */ QLock; /* qlock to getc */ }klogq; IOQ lineq; /* lock to getc; interrupt putc's */ IOQ printq; IOQ mouseq; KIOQ kbdq; static Ref ctl; /* number of opens to the control file */ static int raw; /* true if raw has been requested on a ctl file */ char eve[NAMELEN] = "bootes"; char evekey[DESKEYLEN]; /* * init the queues and set the output routine */ void printinit(void) { initq(&printq); printq.puts = 0; initq(&lineq); initq(&kbdq); kbdq.putc = kbdputc; initq(&klogq); initq(&mouseq); mouseq.putc = mouseputc; } /* * Print a string on the console. Convert \n to \r\n for serial * line consoles. Locking of the queues is left up to the screen * or uart code. Multi-line messages to serial consoles may get * interspersed with other messages. */ void putstrn(char *str, int n) { char buf[PRINTSIZE+2]; int m; char *t; /* * if there's an attached bit mapped display, * put the message there. screenputs is defined * as a null macro for systems that have no such * display. */ screenputs(str, n); /* * if there's a serial line being used as a console, * put the message there. Tack a carriage return * before new lines. */ if(printq.puts == 0) return; while(n > 0){ t = memchr(str, '\n', n); if(t){ m = t - str; memmove(buf, str, m); buf[m] = '\r'; buf[m+1] = '\n'; (*printq.puts)(&printq, buf, m+2); str = t + 1; n -= m + 1; } else { (*printq.puts)(&printq, str, n); break; } } } /* * Print a string in the kernel log. Ignore overflow. */ void klogputs(char *str, long n) { int s, m; uchar *nextin; s = splhi(); lock(&klogq); while(n){ m = &klogq.buf[NQ] - klogq.in; if(m > n) m = n; memmove(klogq.in, str, m); n -= m; str += m; nextin = klogq.in + m; if(nextin >= &klogq.buf[NQ]) klogq.in = klogq.buf; else klogq.in = nextin; } unlock(&klogq); splx(s); wakeup(&klogq.r); } int isbrkc(KIOQ *q) { uchar *p; for(p=q->out; p!=q->in; ){ if(raw) return 1; if(*p==0x04 || *p=='\n') return 1; p++; if(p >= q->buf+sizeof(q->buf)) p = q->buf; } return 0; } int sprint(char *s, char *fmt, ...) { return doprint(s, s+PRINTSIZE, fmt, (&fmt+1)) - s; } int print(char *fmt, ...) { char buf[PRINTSIZE]; int n; n = doprint(buf, buf+sizeof(buf), fmt, (&fmt+1)) - buf; putstrn(buf, n); return n; } int kprint(char *fmt, ...) { char buf[PRINTSIZE]; int n; n = doprint(buf, buf+sizeof(buf), fmt, (&fmt+1)) - buf; klogputs(buf, n); return n; } void panic(char *fmt, ...) { char buf[PRINTSIZE]; int n; strcpy(buf, "panic: "); n = doprint(buf+strlen(buf), buf+sizeof(buf), fmt, (&fmt+1)) - buf; buf[n] = '\n'; putstrn(buf, n+1); dumpstack(); if(conf.cntrlp) exit(); else for(;;); } int pprint(char *fmt, ...) { char buf[2*PRINTSIZE]; Chan *c; int n; if(u->p->fgrp == 0) return 0; c = u->p->fgrp->fd[2]; if(c==0 || (c->mode!=OWRITE && c->mode!=ORDWR)) return 0; n = sprint(buf, "%s %d: ", u->p->text, u->p->pid); n = doprint(buf+n, buf+sizeof(buf), fmt, (&fmt+1)) - buf; qlock(&c->wrl); if(waserror()){ qunlock(&c->wrl); return 0; } (*devtab[c->type].write)(c, buf, n, c->offset); c->offset += n; qunlock(&c->wrl); poperror(); return n; } void prflush(void) { while(printq.in != printq.out) ; } void echo(Rune r, char *buf, int n) { static int ctrlt; /* * ^p hack */ if(r==0x10 && conf.cntrlp) panic("^p"); /* * ^t hack BUG */ if(ctrlt == 2){ ctrlt = 0; switch(r){ case 0x14: break; /* pass it on */ case 'm': mntdump(); return; case 'p': procdump(); return; case 'q': dumpqueues(); return; case 'r': exit(); break; } }else if(r == 0x14){ ctrlt++; return; } ctrlt = 0; if(raw) return; if(r == 0x15) putstrn("^U\n", 3); else putstrn(buf, n); } /* * turn '\r' into '\n' before putting it into the queue */ int kbdcr2nl(IOQ *q, int ch) { if(ch == '\r') ch = '\n'; return kbdputc(q, ch); } /* * Put character, possibly a rune, into read queue at interrupt time. * Always called splhi from processor 0. */ int kbdputc(IOQ *q, int ch) { int i, n; char buf[3]; Rune r; USED(q); r = ch; n = runetochar(buf, &r); if(n == 0) return 0; echo(r, buf, n); kbdq.c = r; for(i=0; iHZ){ kbdq.repeat = 2; kbdq.count = 0; return; } if(++kbdq.count&1) kbdputc(&kbdq, kbdq.c); } int consactive(void) { return printq.in != printq.out; } enum{ Qdir, Qcons, Qconsctl, Qcputime, Qlights, Qnoise, Qnull, Qpgrpid, Qpid, Qppid, Qtime, Quser, Qklog, Qmsec, Qclock, Qsysstat, Qswap, Qcrypt, Qkey, Qchal, }; Dirtab consdir[]={ "cons", {Qcons}, 0, 0660, "consctl", {Qconsctl}, 0, 0220, "cputime", {Qcputime}, 6*NUMSIZE, 0444, "time", {Qtime}, NUMSIZE, 0664, "clock", {Qclock}, 2*NUMSIZE, 0444, "msec", {Qmsec}, NUMSIZE, 0444, "lights", {Qlights}, 0, 0220, "noise", {Qnoise}, 0, 0220, "null", {Qnull}, 0, 0666, "pgrpid", {Qpgrpid}, NUMSIZE, 0444, "pid", {Qpid}, NUMSIZE, 0444, "ppid", {Qppid}, NUMSIZE, 0444, "user", {Quser}, 0, 0666, "chal", {Qchal}, 8, 0666, "crypt", {Qcrypt}, 0, 0666, "key", {Qkey}, DESKEYLEN, 0222, "klog", {Qklog}, 0, 0444, "sysstat", {Qsysstat}, 0, 0666, "swap", {Qswap}, 0, 0664, }; #define NCONS (sizeof consdir/sizeof(Dirtab)) ulong boottime; /* seconds since epoch at boot */ long seconds(void) { return boottime + TK2SEC(MACHP(0)->ticks); } int readnum(ulong off, char *buf, ulong n, ulong val, int size) { char tmp[64]; Op op = (Op){ tmp, tmp+sizeof(tmp), &val, size-1, 0, FUNSIGN|FLONG }; numbconv(&op, 10); tmp[size-1] = ' '; if(off >= size) return 0; if(off+n > size) n = size-off; memmove(buf, tmp+off, n); return n; } int readstr(ulong off, char *buf, ulong n, char *str) { int size; size = strlen(str); if(off >= size) return 0; if(off+n > size) n = size-off; memmove(buf, str+off, n); return n; } void consreset(void) { } void consinit(void) { } Chan* consattach(char *spec) { return devattach('c', spec); } Chan* consclone(Chan *c, Chan *nc) { return devclone(c, nc); } int conswalk(Chan *c, char *name) { return devwalk(c, name, consdir, NCONS, devgen); } void consstat(Chan *c, char *dp) { devstat(c, dp, consdir, NCONS, devgen); } Chan* consopen(Chan *c, int omode) { switch(c->qid.path){ case Qconsctl: if(strcmp(u->p->user, eve) != 0) error(Eperm); incref(&ctl); break; } c->aux = 0; return devopen(c, omode, consdir, NCONS, devgen); } void conscreate(Chan *c, char *name, int omode, ulong perm) { USED(c, name, omode, perm); error(Eperm); } void consclose(Chan *c) { /* last close of control file turns off raw */ if(c->qid.path==Qconsctl && (c->flag&COPEN)){ lock(&ctl); if(--ctl.ref == 0) raw = 0; unlock(&ctl); } if(c->qid.path == Qcrypt && c->aux) freeb(c->aux); c->aux = 0; } long consread(Chan *c, void *buf, long n, ulong offset) { int ch, i, j, k, id; ulong l; uchar *out; char *cbuf = buf; char *user, *chal; int userlen; Block *cb; char tmp[6*NUMSIZE], xbuf[512]; Mach *mp; if(n <= 0) return n; switch(c->qid.path & ~CHDIR){ case Qdir: return devdirread(c, buf, n, consdir, NCONS, devgen); case Qcons: qlock(&kbdq); if(waserror()){ qunlock(&kbdq); nexterror(); } while(!cangetc(&lineq)){ sleep(&kbdq.r, isbrkc, &kbdq); do{ lock(&lineq); ch = getc(&kbdq); if(raw){ unlock(&lineq); goto Default; } switch(ch){ case '\b': if(lineq.in != lineq.out){ if(lineq.in == lineq.buf) lineq.in = lineq.buf+sizeof(lineq.buf); lineq.in--; } break; case 0x15: lineq.in = lineq.out; break; Default: default: *lineq.in = ch; if(lineq.in >= lineq.buf+sizeof(lineq.buf)-1) lineq.in = lineq.buf; else lineq.in++; } unlock(&lineq); }while(raw==0 && ch!='\n' && ch!=0x04); } i = 0; while(n > 0){ ch = getc(&lineq); if(ch==-1 || (raw==0 && ch==0x04)) break; i++; *cbuf++ = ch; --n; } poperror(); qunlock(&kbdq); return i; case Qcputime: k = offset; if(k >= sizeof tmp) return 0; if(k+n > sizeof tmp) n = sizeof tmp - k; /* easiest to format in a separate buffer and copy out */ for(i=0; i<6 && NUMSIZE*ip->time[i]; if(i == TReal) l = MACHP(0)->ticks - l; l = TK2MS(l); readnum(0, tmp+NUMSIZE*i, NUMSIZE, l, NUMSIZE); } memmove(buf, tmp+k, n); return n; case Qpgrpid: return readnum(offset, buf, n, u->p->pgrp->pgrpid, NUMSIZE); case Qpid: return readnum(offset, buf, n, u->p->pid, NUMSIZE); case Qppid: return readnum(offset, buf, n, u->p->parentpid, NUMSIZE); case Qtime: return readnum(offset, buf, n, boottime+TK2SEC(MACHP(0)->ticks), 12); case Qclock: k = offset; if(k >= 2*NUMSIZE) return 0; if(k+n > 2*NUMSIZE) n = 2*NUMSIZE - k; readnum(0, tmp, NUMSIZE, MACHP(0)->ticks, NUMSIZE); readnum(0, tmp+NUMSIZE, NUMSIZE, HZ, NUMSIZE); memmove(buf, tmp+k, n); return n; case Qcrypt: cb = c->aux; if(!cb) return 0; if(n > cb->wptr - cb->base) n = cb->wptr - cb->base; memmove(buf, cb->base, n); return n; case Qchal: if(offset != 0 || n != 8) error(Ebadarg); chal = u->p->pgrp->crypt->chal; chal[0] = 0; for(i=1; i<8; i++) chal[i] = nrand(256); memmove(buf, chal, 8); encrypt(evekey, buf, 8); chal[0] = 1; return n; case Quser: return readstr(offset, buf, n, u->p->user); case Qnull: return 0; case Qklog: qlock(&klogq); if(waserror()){ qunlock(&klogq); nexterror(); } while(!cangetc(&klogq)) sleep(&klogq.r, cangetc, &klogq); for(i=0; iticks), NUMSIZE); case Qsysstat: j = 0; xbuf[0] = 0; for(id = 0; id < 32; id++) { if(active.machs & (1<cs, mp->intr, mp->syscall, mp->pfault, mp->tlbfault, mp->tlbpurge, mp->load); } } return readstr(offset, buf, n, xbuf); case Qswap: sprint(xbuf, "%d/%d memory %d/%d swap\n", palloc.user-palloc.freecount, palloc.user, conf.nswap-swapalloc.free, conf.nswap); return readstr(offset, buf, n, xbuf); default: print("consread %lux\n", c->qid); error(Egreg); } } void conslights(char *a, int n) { int l; char line[128]; char *lp; int c; lp = line; while(n--){ *lp++ = c = *a++; if(c=='\n' || n==0 || lp==&line[sizeof(line)-1]) break; } *lp = 0; lights(strtoul(line, 0, 0)); } void consnoise(char *a, int n) { int freq; int duration; char line[128]; char *lp; int c; lp = line; while(n--){ *lp++ = c = *a++; if(c=='\n' || n==0 || lp==&line[sizeof(line)-1]){ *lp = 0; freq = strtoul(line, &lp, 0); while(*lp==' ' || *lp=='\t') lp++; duration = strtoul(lp, &lp, 0); buzz(freq, duration); lp = line; } } } long conswrite(Chan *c, void *va, long n, ulong offset) { char cbuf[64]; char buf[256]; long l, bp; char *a = va; Block *cb; Mach *mp; int id, fd, ch; Chan *swc; switch(c->qid.path){ case Qcons: /* * Can't page fault in putstrn, so copy the data locally. */ l = n; while(l > 0){ bp = l; if(bp > sizeof buf) bp = sizeof buf; memmove(buf, a, bp); putstrn(a, bp); a += bp; l -= bp; } break; case Qconsctl: if(n >= sizeof(buf)) n = sizeof(buf)-1; strncpy(buf, a, n); buf[n] = 0; for(a = buf; a;){ if(strncmp(a, "rawon", 5) == 0){ lock(&lineq); while((ch=getc(&kbdq)) != -1){ *lineq.in++ = ch; if(lineq.in == lineq.buf+sizeof(lineq.buf)) lineq.in = lineq.buf; } unlock(&lineq); lock(&ctl); raw = 1; unlock(&ctl); } else if(strncmp(a, "rawoff", 6) == 0){ lock(&ctl); raw = 0; unlock(&ctl); } if(a = strchr(a, ' ')) a++; } break; case Qtime: if(n<=0 || boottime!=0) /* write once file */ return 0; if(n >= sizeof cbuf) n = sizeof cbuf - 1; memmove(cbuf, a, n); cbuf[n-1] = 0; boottime = strtoul(a, 0, 0); break; case Qcrypt: cb = c->aux; if(!cb){ cb = c->aux = allocb(128); cb->type = 'E'; } if(n < 8){ if(n != 1 || a[0] != 'E' && a[0] != 'D') error(Ebadarg); cb->type = a[0]; return 1; } if(n > cb->lim - cb->base) n = cb->lim - cb->base; memmove(cb->base, a, n); cb->wptr = cb->base + n; if(cb->type == 'E') encrypt(u->p->pgrp->crypt->key, cb->base, n); else decrypt(u->p->pgrp->crypt->key, cb->base, n); break; case Qkey: if(n != DESKEYLEN) error(Ebadarg); memmove(u->p->pgrp->crypt->key, a, DESKEYLEN); if(strcmp(u->p->user, eve) == 0) memmove(evekey, a, DESKEYLEN); break; case Quser: if(offset != 0 || n >= NAMELEN-1) error(Ebadarg); if(n==strlen("none") && strncmp(a, "none", n)==0 || n==strlen(u->p->user) && strncmp(a, u->p->user, n)==0 || strcmp(u->p->user, eve)==0){ memmove(u->p->user, a, n); u->p->user[n] = '\0'; }else error(Eperm); if(!conf.cntrlp && strcmp(eve, "bootes") == 0) memmove(eve, u->p->user, NAMELEN); break; case Qchal: if(offset != 0) error(Ebadarg); if(n != 8+NAMELEN+DESKEYLEN) error(Ebadarg); decrypt(evekey, a, n); if(memcmp(u->p->pgrp->crypt->chal, a, 8) != 0) error(Eperm); strncpy(u->p->user, a+8, NAMELEN); u->p->user[NAMELEN-1] = '\0'; memmove(u->p->pgrp->crypt->key, a+8+NAMELEN, DESKEYLEN); break; case Qnull: break; case Qnoise: consnoise(a, n); break; case Qlights: conslights(a, n); break; case Qsysstat: for(id = 0; id < 32; id++) { if(active.machs & (1<cs = 0; mp->intr = 0; mp->syscall = 0; mp->pfault = 0; mp->tlbfault = 0; mp->tlbpurge = 0; } } break; case Qswap: if(n >= sizeof buf) error(Egreg); memmove(buf, va, n); /* so we can NUL-terminate */ buf[n] = 0; if(strncmp(buf, "start", 5) == 0){ kickpager(); /* start a pager if not already started */ break; } if(conf.cntrlp && strcmp(u->p->user, eve) != 0) error(Eperm); if(buf[0]<'0' || '9'qid.path); error(Egreg); } return n; } void consremove(Chan *c) { USED(c); error(Eperm); } void conswstat(Chan *c, char *dp) { USED(c, dp); error(Eperm); } /* * Rand is huge and not worth it here. Be small. * Borrowed from the white book. */ ulong randn; void srand(char *s) { randn = MACHP(0)->ticks * boottime; while(s && *s) randn = (randn << 1) ^ *s++; } int nrand(int n) { randn = randn*1103515245 + 12345; return (randn>>16) % n; } void setterm(char *f) { char buf[2*NAMELEN]; sprint(buf, f, conffile); ksetenv("terminal", buf); }