fork in repl as a tree
authorPhilipp Takacs <philipp@bureaucracy.de>
Wed, 16 Mar 2016 02:06:03 +0000 (03:06 +0100)
committerPhilipp Takacs <philipp@bureaucracy.de>
Thu, 28 Apr 2016 19:52:53 +0000 (21:52 +0200)
This fix the bug in the last improvement (pipe mail through show).
In the first patch repl only waits for one child, now the tree causes
that both childs finished befor repl continues.

uip/repl.c

index 9006dec..8a40543 100644 (file)
@@ -703,7 +703,6 @@ replfilter(FILE *in, FILE *out, char *filter)
 {
        int pid, pid_show, n;
        int mailpipe[2];
-       char *errstr;
 
        if (filter == NULL)
                return;
@@ -714,51 +713,51 @@ replfilter(FILE *in, FILE *out, char *filter)
        rewind(in);
        lseek(fileno(in), (off_t) 0, SEEK_SET);
 
-       if (pipe(mailpipe) == -1) {
-               adios(EX_OSERR, "pipe", "can't create pipe");
-       }
-
-       switch (pid_show = fork()) {
-       case NOTOK:
-               adios(EX_OSERR, "fork", "unable to");
-
-       case OK:
-               dup2(fileno(in), fileno(stdin));
-               dup2(mailpipe[1], fileno(stdout));
-               for (n=3; n<OPEN_MAX; n++) {
-                       close(n);
-               }
-
-               execlp("show", "show", "-file", "-", NULL);
-
-               adios(EX_OSERR, "exec", "unable to");
-       }
-
        switch (pid = fork()) {
        case NOTOK:
                adios(EX_OSERR, "fork", "unable to");
 
        case OK:
-               dup2(mailpipe[0], fileno(stdin));
-               dup2(fileno(out), fileno(stdout));
-               for (n=3; n<OPEN_MAX; n++) {
-                       close(n);
+               if (pipe(mailpipe) == -1) {
+                       adios(EX_OSERR, "pipe", "can't create pipe");
                }
 
-               execlp("mhl", "mhl", "-form", filter, NULL);
-               errstr = strerror(errno);
-               write(2, "unable to exec mhl: ", 20);
-               write(2, errstr, strlen(errstr));
-               write(2, "\n", 1);
-               _exit(EX_OSERR);
+               switch (pid_show = fork()) {
+               case NOTOK:
+                       adios(EX_OSERR, "fork", "unable to");
+               case OK:
+                       dup2(fileno(in), STDIN_FILENO);
+                       dup2(mailpipe[1], STDOUT_FILENO);
+                       close(fileno(in));
+                       close(fileno(out));
+                       close(mailpipe[0]);
+                       close(mailpipe[1]);
+                       for (n=3; n<OPEN_MAX; n++) {
+                               close(n);
+                       }
+                       execlp("show", "show", "-file", "-", NULL);
+                       adios(EX_OSERR, "exec", "unable to");
+                       break;
+               default:
+                       dup2(mailpipe[0], STDIN_FILENO);
+                       dup2(fileno(out), STDOUT_FILENO);
+                       close(fileno(in));
+                       close(fileno(out));
+                       close(mailpipe[0]);
+                       close(mailpipe[1]);
+                       for (n=3; n<OPEN_MAX; n++) {
+                               close(n);
+                       }
+                       execlp("mhl", "mhl", "-form", filter, NULL);
+                       adios(EX_OSERR, "exec", "unable to");
+                       break;
+               }
 
        default:
-               if (pidXwait(-1, "show | mhl"))
+               if (pidXwait(pid, "mhl")) {
                        exit(EX_SOFTWARE);
+               }
                fseek(out, 0L, SEEK_END);
                break;
        }
-
-       close(mailpipe[0]);
-       close(mailpipe[1]);
 }