title: bi-directional communication using socketpair: hangs reading output from child process
body: <p>I'm trying to use a socketpair to have a parent process provide input to a child process that execs a different program (e.g., grep) and then read the resulting output. The program hangs in the while loop that reads the output from the program that the child execs.. The child dupes stdin and stdout on to its end of the socketpair and the parent and the child both close their unused end of the pair.</p>
  
  <p>Interestingly, if the child execs a program that I wrote (OK, I ripped it off from Stevens Advanced Programming in the Unix Environment) everything works as expected. However, if the child execs grep (or some other standard program) the parent invariably hangs in trying to read the output. I can't tell if the input is not reaching grep or if the grep cannot determine the end of the input or if the output is somehow being lost.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;cstdio&gt;
  #include &lt;cerrno&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  void 
  sigpipe_handler(int sig, siginfo_t *siginfo, void * context) {
    cout &lt;&lt; "caught SIGPIPE\n";
    pid_t pid;
  
    if (errno == EPIPE) {
      throw "SIGPIPE caught";
    }
  }
  
  int main(int argc, char** argv) {
  
    struct sigaction sa;
    memset(&amp;sa, '\0', sizeof(struct sigaction));
    sa.sa_sigaction = sigpipe_handler;
    sa.sa_flags = SA_SIGINFO | SA_RESTART;
    sigaction(SIGPIPE, &amp;sa, NULL);
  
    int sp[2];
    socketpair(PF_UNIX, SOCK_STREAM, AF_UNIX, sp);
  
    pid_t childPid = fork();
  
    if (childPid == 0) {
      close(sp[0]);
      if (dup2(sp[1], STDIN_FILENO) != STDIN_FILENO) throw "dup2 error to stdin";
      if (dup2(sp[1], STDOUT_FILENO) != STDOUT_FILENO) throw "dup2 error to stdout";
  
      execl("/bin/grep", "grep", "-n", "namespace", (char*)NULL);
    } else {
      close(sp[1]);
      char line[80];
      int n;
      try {
        while (fgets(line, 80, stdin) != NULL) {
   n = strlen(line);
   if (write(sp[0], line, n) != n) {
     throw "write error to pipe";
   }
  
   if ((n=read(sp[0], line, 80)) &lt; 0) {  // hangs here
     throw "read error from pipe";
   }
   if (n ==0) {
     throw "child closed pipe";
     break;
   }
   line[n] = 0;
   if (fputs(line, stdout) == EOF) {
     throw "puts error";
   }
   if (ferror(stdin)) {
     throw "fgets error on stdin";
   }
   exit(0);
        }
      } catch (const char* e) {
        cout &lt;&lt; e &lt;&lt; endl;
      }
  
      int status;
      waitpid(childPid, &amp;status, 0);
    }
  }
  </code></pre>