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

This commit is contained in:
cinap_lenrek 2013-10-04 21:11:56 +02:00
parent 0b42409cf4
commit 6187e219da
4 changed files with 31 additions and 24 deletions

View file

@ -28,7 +28,7 @@ void getimage(Rtext *t, Www *w){
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 @@ void getimage(Rtext *t, Www *w){
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);

View file

@ -747,8 +747,8 @@ void dolink(Panel *p, int buttons, Rtext *word){
}
}
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,24 +820,34 @@ dupfds(int fd, ...)
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]);
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 @@ void geturl(char *urlname, int post, int plumb, int map){
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 @@ void geturl(char *urlname, int post, int plumb, int map){
case PNG:
case BMP:
case PAGE:
filter("page -w", fd);
filter(fd, "exec page -w");
break;
}
break;

View file

@ -88,7 +88,7 @@ void getpix(Rtext *, Www *);
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);

View file

@ -143,11 +143,11 @@ urlget(Url *url, int body)
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;
}