shithub: riscv

Download patch

ref: 6187e219da80ed8d75c717cd5f7c2f10326c5a81
parent: 0b42409cf44168a945cfa5080f71b61be618beb9
author: cinap_lenrek <cinap_lenrek@gmx.de>
date: Fri Oct 4 17:11:56 EDT 2013

mothra: avoid intermediate rc shell processes, use rfork(RFREND) to isolate rendezvous group

--- a/sys/src/cmd/mothra/getpix.c
+++ b/sys/src/cmd/mothra/getpix.c
@@ -28,7 +28,7 @@
 	Url url;
 	Image *b;
 	int fd, typ;
-	char err[512];
+	char err[512], buf[80], *s;
 	Pix *p;
 
 	ap=t->user;
@@ -54,19 +54,16 @@
 		werrstr("unknown image type");
 		goto Err;
 	}
-	if((fd = pipeline(pixcmd[typ], fd)) < 0)
+	if((fd = pipeline(fd, "exec %s", pixcmd[typ])) < 0)
 		goto Err;
 	if(ap->width>0 || ap->height>0){
-		char buf[80];
-		char *p;
-
-		p = buf;
-		p += sprint(p, "resize");
+		s = buf;
+		s += sprint(s, "exec resize");
 		if(ap->width>0)
-			p += sprint(p, " -x %d", ap->width);
+			s += sprint(s, " -x %d", ap->width);
 		if(ap->height>0)
-			p += sprint(p, " -y %d", ap->height);
-		if((fd = pipeline(buf, fd)) < 0)
+			s += sprint(s, " -y %d", ap->height);
+		if((fd = pipeline(fd, buf)) < 0)
 			goto Err;
 	}
 	b=readimage(display, fd, 1);
--- a/sys/src/cmd/mothra/mothra.c
+++ b/sys/src/cmd/mothra/mothra.c
@@ -747,8 +747,8 @@
 	}
 }
 
-void filter(char *cmd, int fd){
-	switch(rfork(RFFDG|RFPROC|RFMEM|RFNOWAIT|RFNOTEG)){
+void filter(int fd, char *cmd){
+	switch(rfork(RFFDG|RFPROC|RFMEM|RFREND|RFNOWAIT|RFNOTEG)){
 	case -1:
 		message("Can't fork!");
 		break;
@@ -820,17 +820,23 @@
 	free(dir);
 }
 
-int pipeline(char *cmd, int fd)
+int pipeline(int fd, char *fmt, ...)
 {
+	char buf[80], *argv[4];
+	va_list arg;
 	int pfd[2];
 
-	if(pipe(pfd)==-1){
-Err:
+	va_start(arg, fmt);
+	vsnprint(buf, sizeof buf, fmt, arg);
+	va_end(arg);
+
+	if(pipe(pfd) < 0){
+	Err:
 		close(fd);
-		werrstr("pipeline for %s failed: %r", cmd);
+		werrstr("pipeline for %s failed: %r", buf);
 		return -1;
 	}
-	switch(rfork(RFFDG|RFPROC|RFMEM|RFNOWAIT)){
+	switch(rfork(RFPROC|RFMEM|RFFDG|RFREND|RFNOWAIT)){
 	case -1:
 		close(pfd[0]);
 		close(pfd[1]);
@@ -837,7 +843,11 @@
 		goto Err;
 	case 0:
 		dupfds(fd, pfd[1], 2, -1);
-		execl("/bin/rc", "rc", "-c", cmd, 0);
+		argv[0] = "rc";
+		argv[1] = "-c";
+		argv[2] = buf;
+		argv[3] = nil;
+		exec("/bin/rc", argv);
 		_exits(0);
 	}
 	close(fd);
@@ -920,7 +930,7 @@
 			save(fd, cmd);
 			break;
 		case HTML:
-			fd = pipeline("/bin/uhtml", fd);
+			fd = pipeline(fd, "exec uhtml");
 		case PLAIN:
 			n=0; 
 			for(i=wwwtop-1; i>=0 && i!=(wwwtop-NWWW-1); i--){
@@ -969,7 +979,7 @@
 		case PNG:
 		case BMP:
 		case PAGE:
-			filter("page -w", fd);
+			filter(fd, "exec page -w");
 			break;
 		}
 		break;
--- a/sys/src/cmd/mothra/mothra.h
+++ b/sys/src/cmd/mothra/mothra.h
@@ -88,7 +88,7 @@
 ulong countpix(void *p);
 void freepix(void *p);
 void dupfds(int fd, ...);
-int pipeline(char *, int);
+int pipeline(int fd, char *fmt, ...);
 void getfonts(void);
 void *emalloc(int);
 void nstrcpy(char *to, char *from, int len);
--- a/sys/src/cmd/mothra/url.c
+++ b/sys/src/cmd/mothra/url.c
@@ -143,11 +143,11 @@
 	readstr(buf, buf, sizeof(buf));
 
 	if(!cistrcmp(buf, "compress"))
-		fd = pipeline("/bin/uncompress", fd);
+		fd = pipeline(fd, "exec uncompress");
 	else if(!cistrcmp(buf, "gzip"))
-		fd = pipeline("/bin/gunzip", fd);
+		fd = pipeline(fd, "exec gunzip");
 	else if(!cistrcmp(buf, "bzip2"))
-		fd = pipeline("/bin/bunzip2", fd);
+		fd = pipeline(fd, "exec bunzip2");
 
 	return fd;
 }
--