Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The well-known <a href="http://cr.yp.to/docs/selfpipe.html" rel="noreferrer">self-pipe trick</a> can be <a href="http://lkml.org/lkml/2006/7/10/300" rel="noreferrer">adapted</a> for this purpose.</p> <pre><code>#include &lt;errno.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;sys/wait.h&gt; #include &lt;sysexits.h&gt; #include &lt;unistd.h&gt; int main(int argc, char **argv) { int pipefds[2]; int count, err; pid_t child; if (pipe(pipefds)) { perror("pipe"); return EX_OSERR; } if (fcntl(pipefds[1], F_SETFD, fcntl(pipefds[1], F_GETFD) | FD_CLOEXEC)) { perror("fcntl"); return EX_OSERR; } switch (child = fork()) { case -1: perror("fork"); return EX_OSERR; case 0: close(pipefds[0]); execvp(argv[1], argv + 1); write(pipefds[1], &amp;errno, sizeof(int)); _exit(0); default: close(pipefds[1]); while ((count = read(pipefds[0], &amp;err, sizeof(errno))) == -1) if (errno != EAGAIN &amp;&amp; errno != EINTR) break; if (count) { fprintf(stderr, "child's execvp: %s\n", strerror(err)); return EX_UNAVAILABLE; } close(pipefds[0]); puts("waiting for child..."); while (waitpid(child, &amp;err, 0) == -1) if (errno != EINTR) { perror("waitpid"); return EX_SOFTWARE; } if (WIFEXITED(err)) printf("child exited with %d\n", WEXITSTATUS(err)); else if (WIFSIGNALED(err)) printf("child killed by %d\n", WTERMSIG(err)); } return err; } </code></pre> <p>Here's a complete program.</p> <pre> $ ./a.out foo child's execvp: No such file or directory $ (sleep 1 && killall -QUIT sleep &); ./a.out sleep 60 waiting for child... child killed by 3 $ ./a.out true waiting for child... child exited with 0 </pre> <p>How this works:</p> <p>Create a pipe, and make the write endpoint <code>CLOEXEC</code>: it auto-closes when an <code>exec</code> is successfully performed.</p> <p>In the child, try to <code>exec</code>. If it succeeds, we no longer have control, but the pipe is closed. If it fails, write the failure code to the pipe and exit.</p> <p>In the parent, try to read from the other pipe endpoint. If <code>read</code> returns zero, then the pipe was closed and the child must have <code>exec</code> successfully. If <code>read</code> returns data, it's the failure code that our child wrote.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload