actually wait for forked process and return its status -- this is not meant to be a daemon;
authorwenzelm
Sat, 13 Feb 2016 22:52:41 +0100
changeset 62300 f41884b9c4f1
parent 62299 9e95a4afb8c3
child 62301 028e5b1ef9f9
actually wait for forked process and return its status -- this is not meant to be a daemon;
Admin/bash_process/bash_process.c
--- a/Admin/bash_process/bash_process.c	Sat Feb 13 21:22:02 2016 +0100
+++ b/Admin/bash_process/bash_process.c	Sat Feb 13 22:52:41 2016 +0100
@@ -7,9 +7,9 @@
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
 
-
 static void fail(const char *msg)
 {
   fprintf(stderr, "%s\n", msg);
@@ -34,8 +34,24 @@
 
   if (setsid() == -1) {
     pid_t pid = fork();
+    int status;
+
     if (pid == -1) fail("Cannot set session id (failed to fork)");
-    else if (pid != 0) exit(0);
+    else if (pid != 0) {
+      if (waitpid(pid, &status, 0) == -1) {
+        fail("Cannot join forked process");
+      }
+
+      if (WIFEXITED(status)) {
+        exit(WEXITSTATUS(status));
+      }
+      else if (WIFSIGNALED(status)) {
+        exit(128 + WTERMSIG(status));
+      }
+      else {
+        fail("Unknown status of forked process");
+      }
+    }
     else if (setsid() == -1) fail("Cannot set session id (after fork)");
   }