diff options
author | Leah Neukirchen <leah@vuxu.org> | 2019-08-13 14:00:39 +0200 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2019-08-13 14:00:39 +0200 |
commit | 1682f2b487377bff9935e66d2b53239c34a55568 (patch) | |
tree | a5f971ff4862987cfe0a3474ffe334d1cc54cd0b /reap.c | |
parent | 76c42ecb39e57fcb42cd5b3046a6e2b66bea47ec (diff) | |
download | reap-1682f2b487377bff9935e66d2b53239c34a55568.tar.gz reap-1682f2b487377bff9935e66d2b53239c34a55568.tar.xz reap-1682f2b487377bff9935e66d2b53239c34a55568.zip |
better detection of exec errors
Diffstat (limited to 'reap.c')
-rw-r--r-- | reap.c | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/reap.c b/reap.c index fe7b53b..47b8428 100644 --- a/reap.c +++ b/reap.c @@ -6,12 +6,16 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ + +#define _GNU_SOURCE + #include <sys/prctl.h> #include <sys/types.h> #include <sys/wait.h> #include <ctype.h> #include <errno.h> +#include <fcntl.h> #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -106,17 +110,31 @@ main(int argc, char *argv[]) { pid_t pid, desc; + int pipefd[2]; + pipe2(pipefd, O_CLOEXEC); + pid = fork(); if (pid == 0) { // in child + close(pipefd[0]); execvp(argv[optind], argv+optind); - - fprintf(stderr, "reap: exec %s: %s\n", argv[1], strerror(errno)); - exit(111); + char err = errno; + write(pipefd[1], &err, 1); + _exit(111); } else if (pid < 0) { // fork failed fprintf(stderr, "reap: exec %s: %s\n", argv[1], strerror(errno)); exit(111); } + close(pipefd[1]); + + char err = 0; + int n = read(pipefd[0], &err, 1); + + if (n >= 0 && err) { + fprintf(stderr, "reap: exec %s: %s\n", argv[1], strerror(err)); + exit(111); + } + if (verbose) fprintf(stderr, "reap: spawned child %ld\n", (long)pid); |