Revert "Fix adb hang when subprocess dies early."

This is for http://b/3482112 "adb interactions with device causing test harness failures".

This reverts commit 69c5c4c45bd4f1575ae8bdba13795297be7deb8c.

Change-Id: I630bf2e04d2ecf0223bd2af4e87136754ff880d3
diff --git a/adb/adb.c b/adb/adb.c
index 6e2c0a9..f5e6e0c 100644
--- a/adb/adb.c
+++ b/adb/adb.c
@@ -36,9 +36,6 @@
 #include "usb_vendors.h"
 #endif
 
-#if ADB_TRACE
-ADB_MUTEX_DEFINE( D_lock );
-#endif
 
 int HOST = 0;
 
@@ -594,6 +591,14 @@
     return 0;
 }
 
+#ifdef HAVE_FORKEXEC
+static void sigchld_handler(int n)
+{
+    int status;
+    while(waitpid(-1, &status, WNOHANG) > 0) ;
+}
+#endif
+
 #ifdef HAVE_WIN32_PROC
 static BOOL WINAPI ctrlc_handler(DWORD type)
 {
@@ -636,7 +641,6 @@
 
     fd = unix_open("/dev/null", O_RDONLY);
     dup2(fd, 0);
-    adb_close(fd);
 
     fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
     if(fd < 0) {
@@ -644,8 +648,6 @@
     }
     dup2(fd, 1);
     dup2(fd, 2);
-    adb_close(fd);
-
     fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
 #endif
 }
@@ -790,7 +792,6 @@
         // we use stderr instead of stdout due to stdout's buffering behavior.
         adb_close(fd[0]);
         dup2(fd[1], STDERR_FILENO);
-        dup2(fd[1], STDOUT_FILENO);
         adb_close(fd[1]);
 
         // child process
@@ -847,7 +848,7 @@
 #ifdef HAVE_WIN32_PROC
     SetConsoleCtrlHandler( ctrlc_handler, TRUE );
 #elif defined(HAVE_FORKEXEC)
-    // Let the service subproc creator handle its children.
+    signal(SIGCHLD, sigchld_handler);
     signal(SIGPIPE, SIG_IGN);
 #endif
 
@@ -956,9 +957,7 @@
         // listen on default port
         local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
     }
-    D("adb_main(): pre init_jdwp()\n");
     init_jdwp();
-    D("adb_main(): post init_jdwp()\n");
 #endif
 
     if (is_daemon)
@@ -972,7 +971,6 @@
 #endif
         start_logging();
     }
-    D("Event loop starting\n");
 
     fdevent_loop();
 
@@ -1273,7 +1271,6 @@
 #if ADB_HOST
     adb_trace_init();
     adb_sysdeps_init();
-    D("Handling commandline()\n");
     return adb_commandline(argc - 1, argv + 1);
 #else
     if((argc > 1) && (!strcmp(argv[1],"recovery"))) {
@@ -1282,7 +1279,6 @@
     }
 
     start_device_log();
-    D("Handling main()\n");
     return adb_main(0, DEFAULT_ADB_PORT);
 #endif
 }