hlfw.ca

drawcpu

Download patch

ref: fc8fc2b553fe537aa105b7b940727324ada55830
parent: 106305c733d2f7b20e3cf8093ba5dc3a1407df5d
author: halfwit <michaelmisch1985@gmail.com>
date: Tue Mar 5 11:48:31 PST 2024

Ensure we log on a higher fd than 0 1 2

--- a/drawcpu.h
+++ b/drawcpu.h
@@ -1,4 +1,4 @@
-extern void runcommand(int, char**);
+extern void runcommand(int, char**, int);
 extern char *getuser(void);
 extern int session(void);
 extern int aanclient(char*, int);
--- a/main.c
+++ b/main.c
@@ -42,11 +42,12 @@
 main(int argc, char **argv)
 {
 	extern ulong kerndate;
+	int ifd, ofd,efd, dfd;
 	kerndate = seconds();
 	eve = getuser();
 	if(eve == nil)
 		eve = "drawcpu";
-
+	
 	sizebug();
 	osinit();
 	procinit0();
@@ -62,11 +63,23 @@
 		panic("bind #e: %r");
 	if(bind("#I", "/net", MBEFORE) < 0)
 		panic("bind #I: %r");
-	if(bind("#U", "/root", MREPL) < 0)
+	if(bind("#U", "/root", MREPL|MCREATE) < 0)
 		panic("bind #U: %r");
     if(bind("/root", "/", MAFTER) < 0)
 		panic("bind /root: %r");
 
+	/* We have to be a bit pedantic about the fds, leave 0, 1, 2 alone */
+	ifd = open("/dev/null", OREAD);
+	ofd = open("/dev/null", OWRITE);
+	efd = open("/devnull", OWRITE);
+	if(argc == 2 && strcmp(argv[1], "-D") == 0)
+		dfd = create("/Users/halfwit/Work/drawcpu/error.log", OWRITE, 0644);
+	else
+		dfd = open("/dev/null", OWRITE);
+	close(ifd);
+	close(ofd);
+	close(efd);
+
 	char *cmd[] = {
 		"drawcpu",
 		"-c"
@@ -73,6 +86,6 @@
 		". <{n=`{read} && ! ~ $#n 0 && read -c $n} >[2=1]"	
 	};
 
-	runcommand(3, cmd);
+	runcommand(3, cmd, dfd);
 	_exit(0);
 }
--- a/rc/drawcpu.c
+++ b/rc/drawcpu.c
@@ -420,9 +420,9 @@
 	int fd;
 	static int tab[] = {OREAD,OWRITE,ORDWR,OREAD|ORCLOSE};
 	fd = open(file, tab[mode&3]);
-	if(fd < 0 && strcmp(file, "/fd/0") == 0)
+	if(fd != 0 && strcmp(file, "/fd/0") == 0)
 		fd = lfdfd(0);
-	if(fd < 0 && strcmp(file, "/fd/1") == 0)
+	if(fd != 1 && strcmp(file, "/fd/1") == 0)
 		fd = lfdfd(1);
 	return fd;
 }
--- a/rc/exec.c
+++ b/rc/exec.c
@@ -225,7 +225,7 @@
  * start interpreting code
  */
 void
-runcommand(int argc, char **argv)
+runcommand(int argc, char **argv, int efd)
 {
 	code bootstrap[20];
 	char num[12];
@@ -242,11 +242,7 @@
 		flag['i'] = 0;
 	else if(flag['i']==0 && argc==1 && Isatty(0)) flag['i'] = flagset;
 	if(flag['m']) rcmain = flag['m'][0];
-
-	/* Open stdout so next desc is stderr */
-	fd = open("/dev/cons", ORDWR);
-	err = openiofd(open("/dev/cons", OWRITE));
-	close(fd);
+	err = openiofd(efd);
 
 	kinit();
 	Trapinit();
--- a/rc/simple.c
+++ b/rc/simple.c
@@ -462,7 +462,7 @@
 	fd = Open(runq->argv->words->word, ORDWR);
 	if(fd < 0)
 		goto Error;
-	if(sysmount(fd, -1, runq->argv->words->next->word, flag, spec) < 0)
+	if(mount(fd, -1, runq->argv->words->next->word, flag, spec) < 0)
 		goto Error;
 	poplist();
 	return;
@@ -492,9 +492,9 @@
 	long n;
 	while((n=Read(f, buf, sizeof buf))>0)
 		if(Write(1, buf, n)!=n)
-			pfmt(err, "write error copying %s: %s", s, strerror(errno));
+			pfmt(err, "write error copying %s: %s\n", s, strerror(errno));
 	if(n < 0)
-		pfmt(err, "error reading %s: %s", s, strerror(errno));
+		pfmt(err, "error reading %s: %s\n", s, strerror(errno));
 }
 
 void
@@ -511,7 +511,7 @@
 	} else for(;a;a = a->next){
 		f = Open(a->word, OREAD);
 		if(f < 0){
-			pfmt(err, "can't open %s: %s", a->word, strerror(errno));
+			pfmt(err, "can't open %s: %s\n", a->word, strerror(errno));
 			break;
 		}
 		cat(f, a->word);