Do not create zombie processes in perfetto_hprof.

We now daemonize after forking to reparent the perfetto_hprof process
to init. ART now waits on the intermediate process, which immediately
exits, before resuming the prevent creating a zombie.

Bug: 148280975
Test: run profile, then `adb shell ps -A | grep Z`
Change-Id: I8d101331375a5542de58bfd0f4e54b8281d0a3a2
diff --git a/perfetto_hprof/perfetto_hprof.cc b/perfetto_hprof/perfetto_hprof.cc
index ce2f7b2db..4f20094 100644
--- a/perfetto_hprof/perfetto_hprof.cc
+++ b/perfetto_hprof/perfetto_hprof.cc
@@ -25,6 +25,7 @@
 #include <signal.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <thread>
 #include <time.h>
 
@@ -363,9 +364,30 @@
   art::ScopedSuspendAll ssa(__FUNCTION__, /* long_suspend=*/ true);
 
   pid_t pid = fork();
-  if (pid != 0) {
+  if (pid == -1) {
+    // Fork error.
+    PLOG(ERROR) << "fork";
     return;
   }
+  if (pid != 0) {
+    // Parent
+    int stat_loc;
+    for (;;) {
+      if (waitpid(pid, &stat_loc, 0) != -1 || errno != EINTR) {
+        break;
+      }
+    }
+    return;
+  }
+
+  // The following code is only executed by the child of the original process.
+  //
+  // Daemon creates a new process that is the grand-child of the original process, and exits.
+  if (daemon(0, 0) == -1) {
+    PLOG(FATAL) << "daemon";
+  }
+
+  // The following code is only executed by the grand-child of the original process.
 
   // Make sure that this is the first thing we do after forking, so if anything
   // below hangs, the fork will go away from the watchdog.