title: Handling errors from execvp()
body: <p>I am a little confused about how to handle errors from <code>execvp()</code>.  My code so far looks like this:</p>
  
  <pre><code>int pid = fork();
  if (pid &lt; 0) {
      // handle error.
  }
  else if (pid == 0) {
      int status = execvp(myCommand,myArgumentVector); // status should be -1 because execvp
                                                       // only returns when an error occurs
      // We only reach this point as a result of failure from execvp
      exit(/* What goes here? */);
  }
  else {
      int status;
      int waitedForPid = waitpid(pid,&amp;status,0);
      //...
  }
  </code></pre>
  
  <p>There are three cases I'm trying to address:</p>
  
  <ol>
  <li><code>myCommand,myArgumentVector</code> are valid and the command executes correctly.</li>
  <li><code>myCommand,myArgumentVector</code> are valid parameters, but something goes wrong in the execution of <code>myCommand</code>.</li>
  <li><code>myCommand,myArgumentVector</code> are invalid parameters (e.g. <code>myCommand</code> cannot be found) and the <code>execvp()</code> call fails.</li>
  </ol>
  
  <p>My primary concern is that the parent process will have all the information it needs in order to handle the child's error correctly, and I'm not entirely sure how to do that.</p>
  
  <p>In the first case, the program presumably ended with an exit status of 0.  This means that if I were to call <code>WIFEXITED(status)</code> in the macro, I should get <code>true</code>.  I think this should work fine.</p>
  
  <p>In the second case, the program presumably ended with an exit status other than 0.  This means that if I were to call <code>WEXITSTATUS(status)</code> I should get the specific exit status of the child invocation of <code>myCommand</code> (please advise if this is incorrect).</p>
  
  <p>The third case is causing me a lot of confusion.  So if <code>execvp()</code> fails then the error is stored in the global variable <code>errno</code>.  But this global variable is only accessible from the child process; the parent as an entirely separate process I don't think can see it.  Does this mean that I should be calling <code>exit(errno)</code>?  Or am I supposed to be doing something else here?  Also, if I call <code>exit(errno)</code> how can I get the value of <code>errno</code> back from <code>status</code> in the parent?</p>
  
  <p>My grasp is still a little tenuous so what I'm looking for is either confirmation or correction in my understanding of how to handle these three cases.</p>