summaryrefslogtreecommitdiff
path: root/sys/src/cmd/mothra/mothra.c
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@gmx.de>2013-10-04 16:08:19 +0200
committercinap_lenrek <cinap_lenrek@gmx.de>2013-10-04 16:08:19 +0200
commitd1602327475bad9da4795ddf9326507161b08b7a (patch)
tree34e7c4c28f80509d70b3b4ef38d2867e71975331 /sys/src/cmd/mothra/mothra.c
parent8c1d4604db54021b774b17eb7440685b4638163b (diff)
mothra: run page in own note group, close unneeded filedescriptors after forking
running page in own note group avoids killing it when we exit mothra. copypaste dupfds() function from page and use it to close unneeded file descriptors.
Diffstat (limited to 'sys/src/cmd/mothra/mothra.c')
-rw-r--r--sys/src/cmd/mothra/mothra.c43
1 files changed, 32 insertions, 11 deletions
diff --git a/sys/src/cmd/mothra/mothra.c b/sys/src/cmd/mothra/mothra.c
index 29e9219fd..6ea1b850e 100644
--- a/sys/src/cmd/mothra/mothra.c
+++ b/sys/src/cmd/mothra/mothra.c
@@ -748,14 +748,12 @@ void dolink(Panel *p, int buttons, Rtext *word){
}
void filter(char *cmd, int fd){
- switch(rfork(RFFDG|RFPROC|RFMEM|RFNOWAIT)){
+ switch(rfork(RFFDG|RFPROC|RFMEM|RFNOWAIT|RFNOTEG)){
case -1:
message("Can't fork!");
break;
case 0:
- close(0);
- dup(fd, 0);
- close(fd);
+ dupfds(fd, 1, 2, -1);
execl("/bin/rc", "rc", "-c", cmd, 0);
_exits(0);
}
@@ -796,6 +794,32 @@ void freetext(Rtext *t){
plrtfree(tt);
}
+void
+dupfds(int fd, ...)
+{
+ int mfd, n, i;
+ va_list arg;
+ Dir *dir;
+
+ va_start(arg, fd);
+ for(mfd = 0; fd >= 0; fd = va_arg(arg, int), mfd++)
+ if(fd != mfd)
+ if(dup(fd, mfd) < 0)
+ sysfatal("dup: %r");
+ va_end(arg);
+ if((fd = open("/fd", OREAD)) < 0)
+ sysfatal("open: %r");
+ n = dirreadall(fd, &dir);
+ for(i=0; i<n; i++){
+ if(strstr(dir[i].name, "ctl"))
+ continue;
+ fd = atoi(dir[i].name);
+ if(fd >= mfd)
+ close(fd);
+ }
+ free(dir);
+}
+
int pipeline(char *cmd, int fd)
{
int pfd[2];
@@ -806,22 +830,19 @@ Err:
werrstr("pipeline for %s failed: %r", cmd);
return -1;
}
- switch(fork()){
+ switch(rfork(RFFDG|RFPROC|RFMEM|RFNOWAIT)){
case -1:
close(pfd[0]);
close(pfd[1]);
goto Err;
case 0:
- dup(fd, 0);
- dup(pfd[0], 1);
- close(pfd[0]);
- close(pfd[1]);
+ dupfds(fd, pfd[1], 2, -1);
execl("/bin/rc", "rc", "-c", cmd, 0);
_exits(0);
}
- close(pfd[0]);
close(fd);
- return pfd[1];
+ close(pfd[1]);
+ return pfd[0];
}
char*