shithub: riscv

Download patch

ref: bf13408df23bb1fe92d6aaf81ff45044d3e1d1f7
parent: 5374d09d35ee47ae73bc17fa363c270c2c141315
author: cinap_lenrek <cinap_lenrek@gmx.de>
date: Sun Oct 21 13:00:12 EDT 2012

rio: various fixes

use notefd in killprocs() insead of postnote() as the process
might'v exited. the notefd stays valid even if the particular
process it was originaly opend on exited. remove the Window.pid
field as its not needed.

dup() the notefd for interruptproc as the window might'v gone
away and closed the notefd file descriptor, resulting in us
writing to the wrong thing.

use snprint() instead of sprint() for safety.

fix bogus debug fprint().

add missing "visible" flushimage() after Reshaped winctl message
got handled. i assumed wsetname()/nameimage() would be enough,
it but does a invisible flush so softscreen doesnt get updated
immidiately.

do not make allocimage() failure in scrtemps() fatal. it wont
draw the window properly, but it gives the user a chance to
delete some windows to recover.

--- a/sys/src/cmd/rio/dat.h
+++ b/sys/src/cmd/rio/dat.h
@@ -172,7 +172,6 @@
 	uchar		mouseopen;
 	uchar		kbdopen;
 	char			*label;
-	int			pid;
 	char			*dir;
 };
 
--- a/sys/src/cmd/rio/fsys.c
+++ b/sys/src/cmd/rio/fsys.c
@@ -96,7 +96,7 @@
 	fd = create(name, OWRITE|ORCLOSE|OCEXEC, 0600);
 	if(fd < 0)
 		error(name);
-	sprint(buf, "%d",srvfd);
+	snprint(buf, sizeof(buf), "%d", srvfd);
 	if(write(fd, buf, strlen(buf)) != strlen(buf))
 		error("srv write");
 	putenv(envname, name);
@@ -150,7 +150,7 @@
 	 */
 	if(cexecpipe(&p0, &wctlfd) < 0)
 		goto Rescue;
-	sprint(srvwctl, "/srv/riowctl.%s.%d", fs->user, pid);
+	snprint(srvwctl, sizeof(srvwctl), "/srv/riowctl.%s.%d", fs->user, pid);
 	post(srvwctl, "wctl", p0);
 	close(p0);
 
@@ -167,7 +167,7 @@
 	/*
 	 * Post srv pipe
 	 */
-	sprint(srvpipe, "/srv/rio.%s.%d", fs->user, pid);
+	snprint(srvpipe, sizeof(srvpipe), "/srv/rio.%s.%d", fs->user, pid);
 	post(srvpipe, "wsys", fs->cfd);
 
 	return fs;
@@ -537,7 +537,7 @@
 	int i, n, o, e, len, j, k, *ids;
 	Dirtab *d, dt;
 	uint clock;
-	char buf[16];
+	char buf[32];
 
 	if((f->qid.type & QTDIR) == 0){
 		sendp(x->c, xfidread);
--- a/sys/src/cmd/rio/rio.c
+++ b/sys/src/cmd/rio/rio.c
@@ -45,7 +45,6 @@
 Channel* initkbd(void);
 
 char		*fontname;
-int		mainpid;
 
 enum
 {
@@ -159,7 +158,6 @@
 		break;
 	}ARGEND
 
-	mainpid = getpid();
 	if(getwd(buf, sizeof buf) == nil)
 		startdir = estrdup(".");
 	else
@@ -333,7 +331,8 @@
 	int i;
 
 	for(i=0; i<nwindow; i++)
-		postnote(PNGROUP, window[i]->pid, "hangup");
+		if(window[i]->notefd >= 0)
+			write(window[i]->notefd, "hangup", 6); 
 }
 
 void
--- a/sys/src/cmd/rio/scrl.c
+++ b/sys/src/cmd/rio/scrl.c
@@ -13,24 +13,25 @@
 static Image *scrtmp;
 
 static
-void
+Image*
 scrtemps(void)
 {
 	int h;
 
-	if(scrtmp)
-		return;
-	h = BIG*Dy(screen->r);
-	scrtmp = allocimage(display, Rect(0, 0, 32, h), screen->chan, 0, DNofill);
-	if(scrtmp == nil)
-		error("scrtemps");
+	if(scrtmp == nil){
+		h = BIG*Dy(screen->r);
+		scrtmp = allocimage(display, Rect(0, 0, 32, h), screen->chan, 0, DNofill);
+	}
+	return scrtmp;
 }
 
 void
 freescrtemps(void)
 {
-	freeimage(scrtmp);
-	scrtmp = nil;
+	if(scrtmp){
+		freeimage(scrtmp);
+		scrtmp = nil;
+	}
 }
 
 static
@@ -68,11 +69,10 @@
 	Rectangle r, r1, r2;
 	Image *b;
 
-	scrtemps();
-	if(w->i == nil)
+	b = scrtemps();
+	if(b == nil || w->i == nil)
 		return;
 	r = w->scrollr;
-	b = scrtmp;
 	r1 = r;
 	r1.min.x = 0;
 	r1.max.x = Dx(r);
--- a/sys/src/cmd/rio/wind.c
+++ b/sys/src/cmd/rio/wind.c
@@ -70,7 +70,7 @@
 	int i, n;
 	char err[ERRMAX];
 	
-	n = sprint(w->name, "window.%d.%d", w->id, w->namecount++);
+	n = snprint(w->name, sizeof(w->name)-2, "window.%d.%d", w->id, w->namecount++);
 	for(i='A'; i<='Z'; i++){
 		if(nameimage(w->i, w->name, 1) > 0)
 			return;
@@ -446,6 +446,7 @@
 
 	notefd = v;
 	write(*notefd, "interrupt", 9);
+	close(*notefd);
 	free(notefd);
 }
 
@@ -672,7 +673,7 @@
 		w->qh = w->nr;
 		wshow(w, w->qh);
 		notefd = emalloc(sizeof(int));
-		*notefd = w->notefd;
+		*notefd = dup(w->notefd, -1);
 		proccreate(interruptproc, notefd, 4096);
 		return;
 	case Kack:	/* ^F: file name completion */
@@ -838,7 +839,7 @@
 			p0--;
 		while(p1<w->nr && w->r[p1]!=' ' && w->r[p1]!='\t' && w->r[p1]!='\n')
 			p1++;
-		sprint(buf, "click=%d", w->q0-p0);
+		snprint(buf, sizeof(buf), "click=%d", w->q0-p0);
 		m->attr = plumbunpackattr(buf);
 	}
 	if(p1-p0 > messagesize-1024){
@@ -1088,6 +1089,7 @@
 		strcpy(buf, w->name);
 		wresize(w, i, m==Moved);
 		proccreate(deletetimeoutproc, estrdup(buf), 4096);
+		flushimage(display, 1);
 		break;
 	case Refresh:
 		if(w->deleted || Dx(w->screenr)<=0 || !rectclip(&r, w->i->r) || w->mouseopen)
@@ -1335,20 +1337,23 @@
 void
 wsetpid(Window *w, int pid, int dolabel)
 {
-	char buf[128];
-	int fd;
+	char buf[64];
+	int ofd;
 
-	w->pid = pid;
-	if(dolabel){
-		sprint(buf, "rc %d", pid);
-		free(w->label);
-		w->label = estrdup(buf);
+	ofd = w->notefd;
+	if(pid <= 0)
+		w->notefd = -1;
+	else {
+		if(dolabel){
+			snprint(buf, sizeof(buf), "rc %d", pid);
+			free(w->label);
+			w->label = estrdup(buf);
+		}
+		snprint(buf, sizeof(buf), "/proc/%d/notepg", pid);
+		w->notefd = open(buf, OWRITE|OCEXEC);
 	}
-	sprint(buf, "/proc/%d/notepg", pid);
-	fd = open(buf, OWRITE|OCEXEC);
-	if(w->notefd > 0)
-		close(w->notefd);
-	w->notefd = fd;
+	if(ofd >= 0)
+		close(ofd);
 }
 
 void
--- a/sys/src/cmd/rio/xfid.c
+++ b/sys/src/cmd/rio/xfid.c
@@ -554,8 +554,8 @@
 		break;
 
 	default:
-		fprint(2, buf, "unknown qid %d in write\n", qid);
-		sprint(buf, "unknown qid in write");
+		fprint(2, "unknown qid %d in write\n", qid);
+		snprint(buf, sizeof(buf), "unknown qid in write");
 		filsysrespond(x->fs, x, &fc, buf);
 		return;
 	}
@@ -892,7 +892,7 @@
 
 	default:
 		fprint(2, "unknown qid %d in read\n", qid);
-		sprint(buf, "unknown qid in read");
+		snprint(buf, sizeof(buf), "unknown qid in read");
 		filsysrespond(x->fs, x, &fc, buf);
 		break;
 	}
--