about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2019-08-13 14:00:39 +0200
committerLeah Neukirchen <leah@vuxu.org>2019-08-13 14:00:39 +0200
commit1682f2b487377bff9935e66d2b53239c34a55568 (patch)
treea5f971ff4862987cfe0a3474ffe334d1cc54cd0b
parent76c42ecb39e57fcb42cd5b3046a6e2b66bea47ec (diff)
downloadreap-1682f2b487377bff9935e66d2b53239c34a55568.tar.gz
reap-1682f2b487377bff9935e66d2b53239c34a55568.tar.xz
reap-1682f2b487377bff9935e66d2b53239c34a55568.zip
better detection of exec errors
-rw-r--r--reap.c24
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);