Index: trunk/Ohana/src/opihi/pantasks/LocalJob.c
===================================================================
--- trunk/Ohana/src/opihi/pantasks/LocalJob.c	(revision 4693)
+++ trunk/Ohana/src/opihi/pantasks/LocalJob.c	(revision 4697)
@@ -1,3 +1,102 @@
 # include "psched.h"
+
+/* local jobs are forked in the background 
+   we might need to limit the maximum number of local jobs.
+   should we have a queue/stack of pending local jobs, much
+   like controller? */
+
+/* update current state, drain stdout/stderr buffers */
+int CheckLocalJob (Job *job) {
+
+  int Nread;
+
+  // XXX do something useful with exit status?
+  CheckLocalJobStatus (job);
+
+  if ((job[0].state == JOB_EXIT) || (job[0].state == JOB_CRASH)) {
+    EmptyIOBuffer (&job[0].stdout, 10, job[0].stdout_fd);
+    EmptyIOBuffer (&job[0].stderr, 10, job[0].stderr_fd);
+    close (job[0].stdout_fd);
+    close (job[0].stderr_fd);
+  } else {
+    /* read stdout buffer */
+    Nread = ReadtoIOBuffer (&job[0].stdout, job[0].stdout_fd);
+    switch (Nread) {
+      case -2:  /* error in read (programming error?  system level error?) */
+	fprintf (stderr, "serious IO error\n");
+	exit (2);
+      case -1:  /* no data in pipe */
+      case 0:   /* pipe is closed, change child state? **/
+      default:  /* data in pipe */
+	break;
+    }
+  
+    /* read stderr buffer */
+    Nread = ReadtoIOBuffer (&job[0].stderr, job[0].stderr_fd);
+    switch (Nread) {
+      case -2:  /* error in read (programming error?  system level error?) */
+	fprintf (stderr, "serious IO error\n");
+	exit (2);
+      case -1:  /* no data in pipe */
+      case 0:   /* pipe is closed, change child state? **/
+      default:  /* data in pipe */
+	break;
+    }
+  }
+  return (TRUE);
+}
+
+int CheckLocalJobStatus (Job *job) {
+
+  int result, waitstatus;
+
+  /* check local job status */
+  result = waitpid (job[0].pid, &waitstatus, WNOHANG);
+  switch (result) {
+    case -1:  /* error with waitpid */
+      switch (errno) {
+	case ECHILD:
+	  fprintf (stderr, "unknown PID, not a child proc\n");
+	  fprintf (stderr, "did process already exit?  programming error?\n");
+	  job[0].state = JOB_NONE;
+	  job[0].exit_status = 0;
+	  return (FALSE);
+	case EINVAL:
+	  fprintf (stderr, "error EINVAL (waitpid): programming error\n");
+	  exit (1);
+	case EINTR:
+	  fprintf (stderr, "error EINTR (waitpid): programming error\n");
+	  exit (1);
+	default:
+	  fprintf (stderr, "unknown error for waitpid (%d): programming error\n", errno);
+	  exit (1);
+      }
+      break;
+      
+    case 0:  /* process not exited */
+      job[0].state = JOB_BUSY;
+      job[0].exit_status = 0;
+      return (TRUE);
+
+    default:
+      if (result != job[0].pid) {
+	fprintf (stderr, "waitpid error: mis-matched PID (%d vs %d).  programming error\n", result, job[0].pid);
+	exit (1);
+      }
+      if (WIFEXITED(waitstatus)) {
+	job[0].state = JOB_EXIT;
+	job[0].exit_status = WEXITSTATUS(waitstatus);
+      }
+      if (WIFSIGNALED(waitstatus)) {
+	job[0].state = JOB_CRASH;
+	job[0].exit_status = WTERMSIG(waitstatus);
+      }
+      if (WIFSTOPPED(waitstatus)) {
+	fprintf (stderr, "waitpid returns 'stopped': programming error\n");
+	exit (1);
+      }
+  }
+  return (FALSE);
+}
 
 /* this could be written a just a one-way pipe */
@@ -58,99 +157,5 @@
 }
 
-/* update current state, drain stdout/stderr buffers */
-int CheckLocalJob (Job *job) {
-
-  int Nread;
-
-  // XXX do something useful with exit status?
-  CheckLocalJobStatus (job);
-
-  if ((job[0].state == JOB_EXIT) || (job[0].state == JOB_CRASH)) {
-    EmptyIOBuffer (&job[0].stdout, job[0].stdout_fd);
-    EmptyIOBuffer (&job[0].stderr, job[0].stderr_fd);
-  } else {
-    /* read stdout buffer */
-    Nread = ReadtoIOBuffer (&job[0].stdout, job[0].stdout_fd);
-    switch (Nread) {
-      case -2:  /* error in read (programming error?  system level error?) */
-	fprintf (stderr, "serious IO error\n");
-	exit (2);
-      case -1:  /* no data in pipe */
-      case 0:   /* pipe is closed, change child state? **/
-      default:  /* data in pipe */
-	break;
-    }
-  
-    /* read stderr buffer */
-    Nread = ReadtoIOBuffer (&job[0].stderr, job[0].stderr_fd);
-    switch (Nread) {
-      case -2:  /* error in read (programming error?  system level error?) */
-	fprintf (stderr, "serious IO error\n");
-	exit (2);
-      case -1:  /* no data in pipe */
-      case 0:   /* pipe is closed, change child state? **/
-      default:  /* data in pipe */
-	break;
-    }
-  }
-  return (TRUE);
-}
-
-int CheckLocalJobStatus (Job *job) {
-
-  int result, waitstatus;
-
-  /*** if child has exited, does the pipe hang around until it is flushed? ***/
-  /*** who closes the child stdout/stderr fd? */
-
-  /* check local job status */
-  result = waitpid (job[0].pid, &waitstatus, WNOHANG);
-  switch (result) {
-    case -1:  /* error with waitpid */
-      switch (errno) {
-	case ECHILD:
-	  fprintf (stderr, "unknown PID, not a child proc\n");
-	  fprintf (stderr, "did process already exit?  programming error?\n");
-	  job[0].state = JOB_NONE;
-	  job[0].exit_status = 0;
-	  return (FALSE);
-	case EINVAL:
-	  fprintf (stderr, "error EINVAL (waitpid): programming error\n");
-	  exit (1);
-	case EINTR:
-	  fprintf (stderr, "error EINTR (waitpid): programming error\n");
-	  exit (1);
-	default:
-	  fprintf (stderr, "unknown error for waitpid (%d): programming error\n", errno);
-	  exit (1);
-      }
-      break;
-      
-    case 0:  /* process not exited */
-      job[0].state = JOB_BUSY;
-      job[0].exit_status = 0;
-      return (TRUE);
-
-    default:
-      if (result != job[0].pid) {
-	fprintf (stderr, "waitpid error: mis-matched PID (%d vs %d).  programming error\n", result, job[0].pid);
-	exit (1);
-      }
-      if (WIFEXITED(waitstatus)) {
-	job[0].state = JOB_EXIT;
-	job[0].exit_status = WEXITSTATUS(waitstatus);
-      }
-      if (WIFSIGNALED(waitstatus)) {
-	job[0].state = JOB_CRASH;
-	job[0].exit_status = WTERMSIG(waitstatus);
-      }
-      if (WIFSTOPPED(waitstatus)) {
-	fprintf (stderr, "waitpid returns 'stopped': programming error\n");
-	exit (1);
-      }
-  }
-  return (FALSE);
-}
-
+/* should this function close the fd's? */
 int KillLocalJob (Job *job) {
 
