kbdfs: cleanup

This commit is contained in:
cinap_lenrek 2011-05-11 07:27:42 +00:00
parent bc91b2709f
commit 684bd26f2e

View file

@ -21,7 +21,7 @@ enum {
Rawoff, Rawoff,
Kbdflush, Kbdflush,
STACKSZ = 8*1024, STACK = 8*1024,
}; };
typedef struct Key Key; typedef struct Key Key;
@ -51,33 +51,34 @@ struct Qtab {
int type; int type;
} qtab[Nqid] = { } qtab[Nqid] = {
"/", "/",
DMDIR|0555, DMDIR|0500,
QTDIR, QTDIR,
"kbd", "kbd",
0666, 0600,
0, 0,
"kbin", "kbin",
0222, 0200,
0, 0,
"kbmap", "kbmap",
0666, 0600,
0, 0,
"cons", "cons",
0666, 0600,
0, 0,
"consctl", "consctl",
0666, 0600,
0, 0,
}; };
char Eshort[] = "read count too small"; char Eshort[] = "read count too small";
char Ebadarg[] = "invalid argument"; char Ebadarg[] = "invalid argument";
char Eperm[] = "permission denied"; char Eperm[] = "permission denied";
char Einuse[] = "file in use";
char Enonexist[] = "file does not exist"; char Enonexist[] = "file does not exist";
char Ebadspec[] = "bad attach specifier"; char Ebadspec[] = "bad attach specifier";
char Ewalk[] = "walk in non directory"; char Ewalk[] = "walk in non directory";
@ -91,6 +92,9 @@ int echofd;
int kbdopen; int kbdopen;
int consopen; int consopen;
int consctlopen;
int debug;
Channel *keychan; /* Key */ Channel *keychan; /* Key */
@ -491,12 +495,12 @@ ctlproc(void *)
cook = chancreate(sizeof(Rune), 0); cook = chancreate(sizeof(Rune), 0);
if(scanfd >= 0) if(scanfd >= 0)
proccreate(scanproc, nil, STACKSZ); proccreate(scanproc, nil, STACK);
if(consfd >= 0) if(consfd >= 0)
proccreate(consproc, nil, STACKSZ); proccreate(consproc, nil, STACK);
threadcreate(keyproc, nil, STACKSZ); threadcreate(keyproc, nil, STACK);
threadcreate(lineproc, cook, STACKSZ); threadcreate(lineproc, cook, STACK);
raw = 0; raw = 0;
@ -569,8 +573,7 @@ ctlproc(void *)
switch(c){ switch(c){
case Rawoff: case Rawoff:
case Rawon: case Rawon:
raw = (c == Rawon); if(raw = (c == Rawon)){
if(raw){
while(s = nbrecvp(linechan)) while(s = nbrecvp(linechan))
free(s); free(s);
r = '\0'; r = '\0';
@ -757,14 +760,36 @@ kbmapwrite(Req *req)
* Filesystem * Filesystem
*/ */
static char*
getauser(void)
{
static char user[64];
int fd;
int n;
if(*user)
return user;
if((fd = open("/dev/user", OREAD)) < 0)
strcpy(user, "none");
else {
n = read(fd, user, (sizeof user)-1);
close(fd);
if(n < 0)
strcpy(user, "none");
else
user[n] = 0;
}
return user;
}
static int static int
fillstat(ulong qid, Dir *d) fillstat(ulong qid, Dir *d)
{ {
struct Qtab *t; struct Qtab *t;
memset(d, 0, sizeof *d); memset(d, 0, sizeof *d);
d->uid = "kbd"; d->uid = getauser();
d->gid = "kbd"; d->gid = getauser();
d->muid = ""; d->muid = "";
d->qid = (Qid){qid, 0, 0}; d->qid = (Qid){qid, 0, 0};
d->atime = time(0); d->atime = time(0);
@ -839,20 +864,25 @@ fsopen(Req *r)
f = r->fid; f = r->fid;
t = qtab + f->qid.path; t = qtab + f->qid.path;
n = need[r->ifcall.mode & 3]; n = need[r->ifcall.mode & 3]<<6;
if((n & t->mode) != n) if((n & t->mode) != n)
respond(r, Eperm); respond(r, Eperm);
else{ else{
f->aux = nil; f->aux = nil;
switch((ulong)f->qid.path){ switch((ulong)f->qid.path){
case Qkbd: case Qkbd:
if(kbdopen++ == 0) if(kbdopen){
sendul(ctlchan, Kbdflush); respond(r, Einuse);
return;
}
kbdopen++;
sendul(ctlchan, Kbdflush);
break; break;
case Qcons: case Qcons:
consopen++;
break;
case Qconsctl: case Qconsctl:
if(consopen++ == 0) consctlopen++;
sendul(ctlchan, Rawoff);
break; break;
} }
respond(r, nil); respond(r, nil);
@ -994,9 +1024,12 @@ fsdestroyfid(Fid *f)
kbdopen--; kbdopen--;
break; break;
case Qcons: case Qcons:
case Qconsctl:
consopen--; consopen--;
break; break;
case Qconsctl:
if(--consctlopen == 0)
sendul(ctlchan, Rawoff);
break;
} }
} }
@ -1023,6 +1056,9 @@ reboot(void)
{ {
int fd; int fd;
if(debug)
return;
if((fd = open("/dev/reboot", OWRITE)) < 0){ if((fd = open("/dev/reboot", OWRITE)) < 0){
fprint(2, "can't open /dev/reboot: %r\n"); fprint(2, "can't open /dev/reboot: %r\n");
return; return;
@ -1038,6 +1074,9 @@ elevate(void)
Dir *d, nd; Dir *d, nd;
int fd; int fd;
if(debug)
return;
snprint(buf, sizeof(buf), "/proc/%d/ctl", getpid()); snprint(buf, sizeof(buf), "/proc/%d/ctl", getpid());
if((fd = open(buf, OWRITE)) < 0){ if((fd = open(buf, OWRITE)) < 0){
fprint(2, "can't open %s: %r\n", buf); fprint(2, "can't open %s: %r\n", buf);
@ -1065,7 +1104,7 @@ elevate(void)
void void
usage(void) usage(void)
{ {
fprint(2, "usage: kbdfs [-m mntpnt]\n"); fprint(2, "usage: %s [ -dD ] [ -s srv ] [ -m mntpnt ] [ file ]\n", argv0);
exits("usage"); exits("usage");
} }
@ -1074,36 +1113,35 @@ threadmain(int argc, char** argv)
{ {
char *mtpt = "/dev"; char *mtpt = "/dev";
char *srv = nil; char *srv = nil;
char *cons = nil;
consfd = -1; consfd = -1;
echofd = 1; echofd = 1;
ARGBEGIN{ ARGBEGIN{
case 'd':
debug++;
break;
case 'D': case 'D':
chatty9p++; chatty9p++;
break; break;
case 'm':
mtpt = EARGF(usage());
break;
case 's': case 's':
srv = EARGF(usage()); srv = EARGF(usage());
break; break;
case 'c': case 'm':
cons = EARGF(usage()); mtpt = EARGF(usage());
break; break;
default: default:
usage(); usage();
}ARGEND }ARGEND
if((scanfd = open("/dev/scancode", OREAD)) < 0) if((scanfd = open("/dev/scancode", OREAD)) < 0)
fprint(2, "can't open /dev/scancode: %r\n"); fprint(2, "%s: warning: can't open /dev/scancode: %r\n", argv0);
if((ledsfd = open("/dev/leds", OWRITE)) < 0) if((ledsfd = open("/dev/leds", OWRITE)) < 0)
fprint(2, "can't open /dev/leds: %r\n"); fprint(2, "%s: warning: can't open /dev/leds: %r\n", argv0);
if(cons){ if(*argv){
if((consfd = open(cons, ORDWR)) < 0) if((consfd = open(*argv, ORDWR)) < 0)
fprint(2, "can't open %s: %r\n", cons); fprint(2, "%s: warning: can't open %s: %r\n", argv0, *argv);
else else
echofd = consfd; echofd = consfd;
} }
@ -1115,9 +1153,10 @@ threadmain(int argc, char** argv)
linechan = chancreate(sizeof(char*), 16); linechan = chancreate(sizeof(char*), 16);
kbdchan = chancreate(sizeof(char*), 16); kbdchan = chancreate(sizeof(char*), 16);
if(!(keychan && reqchan && ctlchan && rawchan && linechan && kbdchan))
sysfatal("allocating chans");
elevate(); elevate();
procrfork(ctlproc, nil, STACK, RFNAMEG|RFNOTEG);
procrfork(ctlproc, nil, STACKSZ, RFNAMEG|RFNOTEG);
threadpostmountsrv(&fs, srv, mtpt, MBEFORE); threadpostmountsrv(&fs, srv, mtpt, MBEFORE);
} }