Index: trunk/Ohana/src/opihi/pcontrol/rconnect.c
===================================================================
--- trunk/Ohana/src/opihi/pcontrol/rconnect.c	(revision 4689)
+++ trunk/Ohana/src/opihi/pcontrol/rconnect.c	(revision 4705)
@@ -10,4 +10,5 @@
 
   int i, stdin_fd[2], stdout_fd[2], stderr_fd[2], status;
+  int result, waitstatus;
   pid_t pid;
   char *p;
@@ -69,7 +70,6 @@
 
   /* send handshake command */
-  sprintf (command, "echo CONNECTED\n");
-  status = write (stdin_fd[1], command, strlen(command));
-  if ((status == -1) && (errno == EPIPE)) goto pipe_error;
+  status = write_fmt (stdin_fd[1], "echo CONNECTED\n");
+  if ((status == -1) && (errno == EPIPE)) goto connect_error;
 
   /* try to get evidence connection is alive - wait upto a few seconds */
@@ -81,7 +81,7 @@
     usleep (20000);
   }
+  if (status == 0) goto connect_error;
+  if (status == -1) goto connect_error;
   if (VerboseMode()) fprintf (stderr, "%d cycles to connect\n", i);
-  if (status == 0) goto pipe_error;
-  if (status == -1) goto io_error;
   FreeIOBuffer (&buffer);
 
@@ -98,7 +98,32 @@
   goto close_pipes;
 
-io_error:
-  if (VerboseMode()) fprintf (stderr, "timeout while connecting\n");
-  goto close_pipes;
+connect_error:
+  if (VerboseMode()) fprintf (stderr, "error while connecting\n");
+
+  /* harvest the child process: kill & wait (< 100 ms) for exit */
+  kill (pid, SIGKILL);
+  result = waitpid (pid, &waitstatus, WNOHANG);
+  for (i = 0; (i < 10) && (result == 0); i++) {
+    usleep (10000);  /* 10 ms is min */
+    result = waitpid (pid, &waitstatus, WNOHANG);
+  }
+
+  if ((result == -1) && (errno != ECHILD)) {
+    fprintf (stderr, "unexpected error from waitpid (%d): programming error\n", errno);
+    exit (1);
+  }
+  if (result == 0) {
+    if (VerboseMode()) fprintf (stderr, "child did not exit??");
+  }
+  if (result > 0) {
+    if (result != pid) {
+      fprintf (stderr, "waitpid error: mis-matched PID (%d vs %d).  programming error\n", result, pid);
+      exit (1);
+    }
+    if (WIFSTOPPED(waitstatus)) {
+      fprintf (stderr, "waitpid returns 'stopped': programming error\n");
+      exit (1);
+    }
+  }
 
 close_pipes:
