ref: 684bd26f2e848ae8e2fff587529e63167659d688
parent: bc91b2709ff5d7d2dfc74a40e650628ea2064e52
author: cinap_lenrek <cinap_lenrek@centraldogma>
date: Wed May 11 03:27:42 EDT 2011
kbdfs: cleanup
--- a/sys/src/cmd/aux/kbdfs.c
+++ b/sys/src/cmd/aux/kbdfs.c
@@ -21,7 +21,7 @@
Rawoff,
Kbdflush,
- STACKSZ = 8*1024,
+ STACK = 8*1024,
};
typedef struct Key Key;
@@ -51,27 +51,27 @@
int type;
} qtab[Nqid] = {
"/",
- DMDIR|0555,
+ DMDIR|0500,
QTDIR,
"kbd",
- 0666,
+ 0600,
0,
"kbin",
- 0222,
+ 0200,
0,
"kbmap",
- 0666,
+ 0600,
0,
"cons",
- 0666,
+ 0600,
0,
"consctl",
- 0666,
+ 0600,
0,
};
@@ -78,6 +78,7 @@
char Eshort[] = "read count too small";
char Ebadarg[] = "invalid argument";
char Eperm[] = "permission denied";
+char Einuse[] = "file in use";
char Enonexist[] = "file does not exist";
char Ebadspec[] = "bad attach specifier";
char Ewalk[] = "walk in non directory";
@@ -91,7 +92,10 @@
int kbdopen;
int consopen;
+int consctlopen;
+int debug;
+
Channel *keychan; /* Key */
Channel *reqchan; /* Req* */
@@ -491,12 +495,12 @@
cook = chancreate(sizeof(Rune), 0);
if(scanfd >= 0)
- proccreate(scanproc, nil, STACKSZ);
+ proccreate(scanproc, nil, STACK);
if(consfd >= 0)
- proccreate(consproc, nil, STACKSZ);
+ proccreate(consproc, nil, STACK);
- threadcreate(keyproc, nil, STACKSZ);
- threadcreate(lineproc, cook, STACKSZ);
+ threadcreate(keyproc, nil, STACK);
+ threadcreate(lineproc, cook, STACK);
raw = 0;
@@ -569,8 +573,7 @@
switch(c){
case Rawoff:
case Rawon:
- raw = (c == Rawon);
- if(raw){
+ if(raw = (c == Rawon)){
while(s = nbrecvp(linechan))
free(s);
r = '\0';
@@ -757,6 +760,28 @@
* 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
fillstat(ulong qid, Dir *d)
{
@@ -763,8 +788,8 @@
struct Qtab *t;
memset(d, 0, sizeof *d);
- d->uid = "kbd";
- d->gid = "kbd";
+ d->uid = getauser();
+ d->gid = getauser();
d->muid = "";
d->qid = (Qid){qid, 0, 0};
d->atime = time(0);
@@ -839,7 +864,7 @@
f = r->fid;
t = qtab + f->qid.path;
- n = need[r->ifcall.mode & 3];
+ n = need[r->ifcall.mode & 3]<<6;
if((n & t->mode) != n)
respond(r, Eperm);
else{
@@ -846,13 +871,18 @@
f->aux = nil;
switch((ulong)f->qid.path){
case Qkbd:
- if(kbdopen++ == 0)
- sendul(ctlchan, Kbdflush);
+ if(kbdopen){
+ respond(r, Einuse);
+ return;
+ }
+ kbdopen++;
+ sendul(ctlchan, Kbdflush);
break;
case Qcons:
+ consopen++;
+ break;
case Qconsctl:
- if(consopen++ == 0)
- sendul(ctlchan, Rawoff);
+ consctlopen++;
break;
}
respond(r, nil);
@@ -994,9 +1024,12 @@
kbdopen--;
break;
case Qcons:
- case Qconsctl:
consopen--;
break;
+ case Qconsctl:
+ if(--consctlopen == 0)
+ sendul(ctlchan, Rawoff);
+ break;
}
}
@@ -1023,6 +1056,9 @@
{
int fd;
+ if(debug)
+ return;
+
if((fd = open("/dev/reboot", OWRITE)) < 0){
fprint(2, "can't open /dev/reboot: %r\n");
return;
@@ -1038,6 +1074,9 @@
Dir *d, nd;
int fd;
+ if(debug)
+ return;
+
snprint(buf, sizeof(buf), "/proc/%d/ctl", getpid());
if((fd = open(buf, OWRITE)) < 0){
fprint(2, "can't open %s: %r\n", buf);
@@ -1065,7 +1104,7 @@
void
usage(void)
{
- fprint(2, "usage: kbdfs [-m mntpnt]\n");
+ fprint(2, "usage: %s [ -dD ] [ -s srv ] [ -m mntpnt ] [ file ]\n", argv0);
exits("usage");
}
@@ -1074,23 +1113,22 @@
{
char *mtpt = "/dev";
char *srv = nil;
- char *cons = nil;
consfd = -1;
echofd = 1;
ARGBEGIN{
+ case 'd':
+ debug++;
+ break;
case 'D':
chatty9p++;
break;
- case 'm':
- mtpt = EARGF(usage());
- break;
case 's':
srv = EARGF(usage());
break;
- case 'c':
- cons = EARGF(usage());
+ case 'm':
+ mtpt = EARGF(usage());
break;
default:
usage();
@@ -1097,17 +1135,17 @@
}ARGEND
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)
- fprint(2, "can't open /dev/leds: %r\n");
+ fprint(2, "%s: warning: can't open /dev/leds: %r\n", argv0);
- if(cons){
- if((consfd = open(cons, ORDWR)) < 0)
- fprint(2, "can't open %s: %r\n", cons);
+ if(*argv){
+ if((consfd = open(*argv, ORDWR)) < 0)
+ fprint(2, "%s: warning: can't open %s: %r\n", argv0, *argv);
else
echofd = consfd;
- }
-
+ }
+
keychan = chancreate(sizeof(Key), 8);
reqchan = chancreate(sizeof(Req*), 0);
ctlchan = chancreate(sizeof(int), 0);
@@ -1115,9 +1153,10 @@
linechan = chancreate(sizeof(char*), 16);
kbdchan = chancreate(sizeof(char*), 16);
- elevate();
+ if(!(keychan && reqchan && ctlchan && rawchan && linechan && kbdchan))
+ sysfatal("allocating chans");
- procrfork(ctlproc, nil, STACKSZ, RFNAMEG|RFNOTEG);
-
+ elevate();
+ procrfork(ctlproc, nil, STACK, RFNAMEG|RFNOTEG);
threadpostmountsrv(&fs, srv, mtpt, MBEFORE);
}