ref: 6ffcc16c81afebbe735eaed7622659b144edaf07
parent: 50746b16b551b78b0891a2be35be0f8796f2ad6f
author: cinap_lenrek <[email protected]>
date: Mon May 30 00:55:26 EDT 2016
reduce error verbosity, hang when panicing in graphics mode
--- a/cpu.c
+++ b/cpu.c
@@ -13,7 +13,6 @@
#define MaxStr 128
-static void fatal(int, char*, ...);
static void usage(void);
static void writestr(int, char*, char*, int);
static int readstr(int, char*, int);
@@ -37,10 +36,10 @@
char *authserver;
char *secstore;
-void
-exits(char *s)
+static void
+ending(void)
{
- exit(1);
+ panic("ending");
}
void
@@ -98,7 +97,7 @@
int n;
if(fprint(fd, "%7ld\n%s", strlen(script), script) < 0)
- fatal(1, "sending aan script");
+ sysfatal("sending aan script: %r");
n = read(fd, buf, sizeof(buf)-1);
close(fd);
@@ -139,17 +138,17 @@
if(aanfilter){
fd = startaan(host, fd);
if(fd < 0)
- fatal(1, "startaan");
+ sysfatal("startaan: %r");
fd = p9authtls(fd);
}
memset(secstorebuf, 0, sizeof(secstorebuf)); /* forget secstore secrets */
if(fprint(fd, "%7ld\n%s", strlen(script), script) < 0)
- fatal(1, "sending script");
+ sysfatal("sending script: %r");
/* Begin serving the namespace */
exportfs(fd);
- fatal(1, "starting exportfs");
+ sysfatal("starting exportfs: %r");
}
void
@@ -169,9 +168,9 @@
}
writestr(fd, buf, "negotiating authentication method", 0);
if(readstr(fd, buf, sizeof buf) < 0)
- fatal(1, "can't negotiate authentication method: %r");
+ sysfatal("can't negotiate authentication method: %r");
if(*buf)
- fatal(1, "%s", buf);
+ sysfatal("%s", buf);
/* authenticate and encrypt the channel */
fd = p9authssl(fd);
@@ -187,7 +186,7 @@
* of /mnt/term
*/
if(readstr(fd, buf, sizeof(buf)) < 0)
- fatal(1, "waiting for FS: %r");
+ sysfatal("waiting for FS: %r");
if(strncmp("FS", buf, 2) != 0) {
print("remote cpu: %s", buf);
exits(buf);
@@ -194,7 +193,7 @@
}
if(readstr(fd, buf, sizeof(buf)) < 0)
- fatal(1, "waiting for remote export: %r");
+ sysfatal("waiting for remote export: %r");
if(strcmp(buf, "/") != 0){
print("remote cpu: %s", buf);
exits(buf);
@@ -203,7 +202,7 @@
/* Begin serving the gnot namespace */
exportfs(fd);
- fatal(1, "starting exportfs");
+ sysfatal("starting exportfs: %r");
}
void
@@ -267,6 +266,7 @@
panic("bind #i: %r");
if(bind("#m", "/dev", MBEFORE) < 0)
panic("bind #m: %r");
+ atexit(ending);
}
if(bind("/root", "/", MAFTER) < 0)
@@ -273,14 +273,17 @@
panic("bind /root: %r");
if(host == nil)
- host = readcons("cpu", "cpu", 0);
+ if((host = readcons("cpu", "cpu", 0)) == nil)
+ sysfatal("user terminated input");
- if(user == nil)
- user = readcons("user", "glenda", 0);
-
if(authserver == nil)
- authserver = readcons("auth", host, 0);
+ if((authserver = readcons("auth", host, 0)) == nil)
+ sysfatal("user terminated input");
+ if(user == nil)
+ if((user = readcons("user", "glenda", 0)) == nil)
+ sysfatal("user terminated input");
+
if(mountfactotum() < 0){
if(secstore == nil)
secstore = authserver;
@@ -299,30 +302,10 @@
ncpu(host);
- fatal(1, "can't dial %s: %r", host);
+ sysfatal("can't dial %s: %r", host);
}
void
-fatal(int syserr, char *fmt, ...)
-{
- Fmt f;
- char *str;
- va_list arg;
-
- fmtstrinit(&f);
- fmtprint(&f, "cpu: ");
- va_start(arg, fmt);
- fmtvprint(&f, fmt, arg);
- va_end(arg);
- if(syserr)
- fmtprint(&f, ": %r");
- fmtprint(&f, "\n");
- str = fmtstrflush(&f);
- write(2, str, strlen(str));
- exits(str);
-}
-
-void
writestr(int fd, char *str, char *thing, int ignore)
{
int l, n;
@@ -330,7 +313,7 @@
l = strlen(str);
n = write(fd, str, l+1);
if(!ignore && n < 0)
- fatal(1, "writing network: %s", thing);
+ sysfatal("writing network: %s: %r", thing);
}
int
@@ -372,13 +355,13 @@
ai = p9any(fd);
memset(secstorebuf, 0, sizeof(secstorebuf)); /* forget secstore secrets */
if(ai == nil)
- fatal(1, "can't authenticate");
+ sysfatal("can't authenticate: %r");
if(ealgs == nil)
return fd;
if(ai->nsecret < 8){
- fatal(1, "p9authssl: secret too small");
+ sysfatal("p9authssl: secret too small");
return -1;
}
memmove(key+4, ai->secret, 8);
@@ -386,11 +369,11 @@
/* exchange random numbers */
genrandom(key, 4);
if(write(fd, key, 4) != 4){
- fatal(1, "p9authssl: write random: %r");
+ sysfatal("p9authssl: write random: %r");
return -1;
}
if(readn(fd, key+12, 4) != 4){
- fatal(1, "p9authssl: read random: %r");
+ sysfatal("p9authssl: read random: %r");
return -1;
}
@@ -402,7 +385,7 @@
/* set up encryption */
fd = pushssl(fd, ealgs, fromclientsecret, fromserversecret, nil);
if(fd < 0)
- fatal(1, "p9authssl: pushssl: %r");
+ sysfatal("p9authssl: pushssl: %r");
return fd;
}
@@ -418,7 +401,7 @@
ai = p9any(fd);
if(ai == nil)
- fatal(1, "can't authenticate");
+ sysfatal("can't authenticate: %r");
conn = mallocz(sizeof(TLSconn), 1);
conn->pskID = "p9secret";
@@ -427,7 +410,7 @@
fd = tlsClient(fd, conn);
if(fd < 0)
- fatal(1, "tlsClient");
+ sysfatal("tlsClient: %r");
auth_freeAI(ai);
free(conn->sessionID);
@@ -627,7 +610,7 @@
werrstr("");
if(readstr(fd, buf, sizeof buf) < 0)
- fatal(1, "cannot read p9any negotiation");
+ sysfatal("cannot read p9any negotiation: %r");
bbuf = buf;
v2 = 0;
if(strncmp(buf, "v.2 ", 4) == 0){
@@ -640,7 +623,7 @@
if((p = strchr(bbuf, ' ')))
*p++ = 0;
if((dom = strchr(bbuf, '@')) == nil)
- fatal(1, "bad p9any domain");
+ sysfatal("bad p9any domain");
*dom++ = 0;
if(strcmp(bbuf, "p9sk1") == 0 || strcmp(bbuf, "dp9ik") == 0){
proto = bbuf;
@@ -652,21 +635,21 @@
bbuf = p;
}
if(proto == nil)
- fatal(1, "server did not offer p9sk1 or dp9ik");
+ sysfatal("server did not offer p9sk1 or dp9ik");
proto = estrdup(proto);
sprint(buf2, "%s %s", proto, dom);
if(write(fd, buf2, strlen(buf2)+1) != strlen(buf2)+1)
- fatal(1, "cannot write user/domain choice in p9any");
+ sysfatal("cannot write user/domain choice in p9any");
if(v2){
if(readstr(fd, buf, sizeof buf) < 0)
- fatal(1, "cannot read OK in p9any");
+ sysfatal("cannot read OK in p9any: %r");
if(memcmp(buf, "OK\0", 3) != 0)
- fatal(1, "did not get OK in p9any: got %s", buf);
+ sysfatal("did not get OK in p9any: got %s", buf);
}
genrandom(crand, 2*NONCELEN);
genrandom(cchal, CHALLEN);
if(write(fd, cchal, CHALLEN) != CHALLEN)
- fatal(1, "cannot write p9sk1 challenge");
+ sysfatal("cannot write p9sk1 challenge: %r");
n = TICKREQLEN;
if(dp9ik)
@@ -673,11 +656,11 @@
n += PAKYLEN;
if(readn(fd, trbuf, n) != n || convM2TR(trbuf, TICKREQLEN, &tr) <= 0)
- fatal(1, "cannot read ticket request in p9sk1");
+ sysfatal("cannot read ticket request in p9sk1: %r");
if(!findkey(&authkey, user, tr.authdom, proto)){
again: if(!getkey(&authkey, user, tr.authdom, proto))
- fatal(1, "no password");
+ sysfatal("no password");
}
strecpy(tr.hostid, tr.hostid+sizeof tr.hostid, user);
@@ -690,7 +673,7 @@
n = gettickets(&authkey, &tr, nil, tbuf, sizeof(tbuf));
}
if(n <= 0)
- fatal(1, "cannot get auth tickets in p9sk1");
+ sysfatal("cannot get auth tickets in p9sk1: %r");
m = convM2T(tbuf, n, &t, &authkey);
if(m <= 0 || t.num != AuthTc){
@@ -701,7 +684,7 @@
memmove(tbuf, tbuf+m, n);
if(dp9ik && write(fd, y, PAKYLEN) != PAKYLEN)
- fatal(1, "cannot send authpak public key back");
+ sysfatal("cannot send authpak public key back: %r");
auth.num = AuthAc;
memmove(auth.rand, crand, NONCELEN);
@@ -710,11 +693,11 @@
n += m;
if(write(fd, tbuf, n) != n)
- fatal(1, "cannot send ticket and authenticator back");
+ sysfatal("cannot send ticket and authenticator back: %r");
if((n=readn(fd, tbuf, m)) != m || memcmp(tbuf, "cpu:", 4) == 0){
if(n <= 4)
- fatal(1, "cannot read authenticator");
+ sysfatal("cannot read authenticator");
/*
* didn't send back authenticator:
@@ -725,8 +708,7 @@
if(i > 0)
n += i;
buf[n] = 0;
- werrstr("");
- fatal(0, "server says: %s", buf);
+ sysfatal("server says: %s", buf);
}
if(convM2A(tbuf, n, &auth, &t) <= 0
@@ -733,7 +715,7 @@
|| auth.num != AuthAs || tsmemcmp(auth.chal, cchal, CHALLEN) != 0){
print("?you and auth server agree about password.\n");
print("?server is confused.\n");
- fatal(0, "server lies got %llux want %llux", *(vlong*)auth.chal, *(vlong*)cchal);
+ sysfatal("server lies");
}
memmove(crand+NONCELEN, auth.rand, NONCELEN);
--- a/include/user.h
+++ b/include/user.h
@@ -93,4 +93,5 @@
extern void lock(Lock*);
extern void unlock(Lock*);
extern int iprint(char*, ...);
+extern int atexit(void (*)(void));
extern void exits(char*);
--- a/kern/Makefile
+++ b/kern/Makefile
@@ -33,7 +33,6 @@
smalloc.$O\
stub.$O\
sysfile.$O\
- sysproc.$O\
qio.$O\
qlock.$O\
term.$O\
--- a/kern/devcons.c
+++ b/kern/devcons.c
@@ -11,7 +11,6 @@
#undef write
#undef read
-void (*consdebug)(void) = 0;
void (*screenputs)(char*, int) = 0;
Queue* kbdq; /* unprocessed console input */
@@ -38,7 +37,6 @@
char line[1024]; /* current input line */
int count;
- int ctlpoff;
/* a place to save up characters at interrupt time before dumping them in the queue */
Lock lockputc;
@@ -53,7 +51,6 @@
0,
{ 0 },
0,
- 0,
{ 0 },
{ 0 },
kbd.istage,
@@ -102,11 +99,6 @@
setterm(0);
}
-void
-prflush(void)
-{
-}
-
/*
* 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
@@ -166,10 +158,8 @@
kprintoq = nil; /* don't try to write to /dev/kprint */
- if(panicking)
- for(;;);
- panicking = 1;
-
+ if(panicking++)
+ for(;;) osyield();
splhi();
strcpy(buf, "panic: ");
va_start(arg, fmt);
@@ -176,15 +166,11 @@
n = vseprint(buf+strlen(buf), buf+sizeof(buf), fmt, arg) - buf;
va_end(arg);
buf[n] = '\n';
- if(screenputs != 0)
- write(2, buf, n+1);
- if(consdebug)
- (*consdebug)();
spllo();
- prflush();
putstrn(buf, n+1);
- dumpstack();
-
+ while(screenputs != 0)
+ osyield();
+ setterm(0);
exit(1);
}
@@ -248,76 +234,6 @@
static void
echo(char *buf, int n)
{
- static int ctrlt;
- int x;
- char *e, *p;
-
- e = buf+n;
- for(p = buf; p < e; p++){
- switch(*p){
- case 0x10: /* ^P */
- if(cpuserver && !kbd.ctlpoff){
- active.exiting = 1;
- return;
- }
- break;
- case 0x14: /* ^T */
- ctrlt++;
- if(ctrlt > 2)
- ctrlt = 2;
- continue;
- }
-
- if(ctrlt != 2)
- continue;
-
- /* ^T escapes */
- ctrlt = 0;
- switch(*p){
- case 'S':
- x = splhi();
- dumpstack();
- procdump();
- splx(x);
- return;
- case 's':
- dumpstack();
- return;
- case 'x':
- xsummary();
- ixsummary();
- mallocsummary();
- pagersummary();
- return;
- case 'd':
- if(consdebug == 0)
- consdebug = rdb;
- else
- consdebug = 0;
- print("consdebug now 0x%p\n", consdebug);
- return;
- case 'D':
- if(consdebug == 0)
- consdebug = rdb;
- consdebug();
- return;
- case 'p':
- x = spllo();
- procdump();
- splx(x);
- return;
- case 'q':
- scheddump();
- return;
- case 'k':
- killbig();
- return;
- case 'r':
- exit(0);
- return;
- }
- }
-
qproduce(kbdq, buf, n);
if(kbd.raw)
return;
@@ -789,10 +705,6 @@
if(screenputs == 0)
setterm(0);
qunlock(&kbd.lk);
- } else if(strncmp(a, "ctlpon", 6) == 0){
- kbd.ctlpoff = 0;
- } else if(strncmp(a, "ctlpoff", 7) == 0){
- kbd.ctlpoff = 1;
}
if((a = strchr(a, ' ')))
a++;
--- a/kern/fns.h
+++ b/kern/fns.h
@@ -215,7 +215,6 @@
void portclock(Ureg*);
int postnote(Proc*, int, char*, int);
int pprint(char*, ...);
-void prflush(void);
ulong procalarm(ulong);
int proccounter(char *name);
void procctl(Proc*);
--- a/kern/sysproc.c
+++ /dev/null
@@ -1,32 +1,0 @@
-#include "u.h"
-#include "lib.h"
-#include "dat.h"
-#include "fns.h"
-#include "error.h"
-
-long
-sysexits(ulong *arg)
-{
- char *status;
- char *inval = "invalid exit string";
- char buf[ERRMAX];
-
- status = (char*)arg[0];
- if(status){
- if(waserror())
- status = inval;
- else{
- validaddr((ulong)status, 1, 0);
- if(vmemchr(status, 0, ERRMAX) == 0){
- memmove(buf, status, ERRMAX);
- buf[ERRMAX-1] = 0;
- status = buf;
- }
- }
- poperror();
-
- }
- pexit(status, 1);
- return 0; /* not reached */
-}
-
--- a/libc/Makefile
+++ b/libc/Makefile
@@ -3,6 +3,7 @@
LIB=libc.a
OFILES=\
+ atexit.$O\
charstod.$O\
cleanname.$O\
convD2M.$O\
--- /dev/null
+++ b/libc/atexit.c
@@ -1,0 +1,46 @@
+#include <u.h>
+#include <libc.h>
+
+#define NEXIT 33
+
+typedef struct Onex Onex;
+struct Onex{
+ void (*f)(void);
+ int pid;
+};
+
+static Lock onexlock;
+Onex onex[NEXIT];
+
+int
+atexit(void (*f)(void))
+{
+ int i;
+
+ lock(&onexlock);
+ for(i=0; i<NEXIT; i++)
+ if(onex[i].f == 0) {
+ onex[i].pid = getpid();
+ onex[i].f = f;
+ unlock(&onexlock);
+ return 1;
+ }
+ unlock(&onexlock);
+ return 0;
+}
+
+void
+exits(char *s)
+{
+ int i, pid;
+ void (*f)(void);
+
+ pid = getpid();
+ for(i = NEXIT-1; i >= 0; i--)
+ if((f = onex[i].f) && pid == onex[i].pid) {
+ onex[i].f = 0;
+ (*f)();
+ }
+ exit(s && *s);
+}
+
--- a/libc/sysfatal.c
+++ b/libc/sysfatal.c
@@ -11,10 +11,7 @@
fprint(2, "%s: %s\n", argv0, buf);
else
fprint(2, "%s\n", buf);
-#undef write
-write(2, buf, strlen(buf));
-write(2, "\n", 1);
- panic("sysfatal");
+ exits("sysfatal");
}
void (*_sysfatal)(char *fmt, va_list arg) = _sysfatalimpl;