src/lib/pipeline.c 1.24
1.24 2010/03/26 21:13:29 markd
readability changes suggest by Tim
Index: src/lib/pipeline.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/lib/pipeline.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -b -B -U 4 -r1.23 -r1.24
--- src/lib/pipeline.c 20 Mar 2010 14:22:17 -0000 1.23
+++ src/lib/pipeline.c 26 Mar 2010 21:13:29 -0000 1.24
@@ -28,9 +28,10 @@
char **cmd; /* null-terminated command for this process */
pid_t pid; /* pid for process, -1 if not running */
enum procState state; /* state of process */
int status; /* status from wait */
- int execPipe[2]; /* pipeline to wait on exec to happen */
+ int execPipeParent; /* pipe to wait on for exec */
+ int execPipeChild; /* write side is close-on-exec */
};
struct pipeline
/* Object for a process pipeline and associated open file */
@@ -120,12 +121,11 @@
for (i = 0; i < cmdLen; i++)
proc->cmd[i] = cloneString(cmd[i]);
proc->cmd[cmdLen] = NULL;
proc->state = procStateNew;
-if (pipe(proc->execPipe) != 0)
- errnoAbort("pipe open failed");
-if (fcntl(proc->execPipe[1], F_SETFL, FD_CLOEXEC) != 0)
- errnoAbort("fcntl set cloexec failed");
+proc->execPipeParent = pipeCreate(&proc->execPipeChild);
+if (fcntl(proc->execPipeChild, F_SETFL, FD_CLOEXEC) != 0)
+ errnoAbort("fcntl set FD_cloexec failed");
return proc;
}
static void plProcFree(struct plProc *proc)
@@ -138,10 +138,12 @@
freeMem(proc);
}
static void plProcStateTrans(struct plProc *proc, enum procState newState)
-/* do state transition for process, checking validity */
+/* do state transition for process changing it to a new state */
{
+// States must transition in order. New state must immediately follow the
+// current state.
if (newState != proc->state+1)
errAbort("invalid state transition: %d -> %d", proc->state, newState);
proc->state = newState;
}
@@ -204,9 +206,9 @@
static void plProcMemWrite(struct plProc* proc, int stdoutFd, int stderrFd, void *otherEndBuf, size_t otherEndBufSize)
/* implements child process to write memory buffer to pipeline after
* fork */
{
-safeClose(&proc->execPipe[1]); // don't exec, so explicitly close
+safeClose(&proc->execPipeChild); // memWriter proc doesn't exec, so explicitly close
plProcSetup(proc, STDIN_FILENO, stdoutFd, stderrFd);
ssize_t wrCnt = write(STDOUT_FILENO, otherEndBuf, otherEndBufSize);
if (wrCnt < 0)
@@ -336,20 +338,19 @@
safeClose(&procStdinFd);
if (proc->next != NULL)
safeClose(&procStdoutFd);
-/* child end of execPipe */
-safeClose(&proc->execPipe[1]);
+safeClose(&proc->execPipeChild); // child end of execPipe
return prevStdoutFd;
}
static void waitOnExec(struct plProc *proc)
/* wait on exec to happen on this process */
{
-// execPipe will be close when exec happens
+// execPipeChild will get EOF when exec happens
char buf[1];
-read(proc->execPipe[0], buf, sizeof(buf));
-safeClose(&proc->execPipe[0]);
+read(proc->execPipeParent, buf, sizeof(buf));
+safeClose(&proc->execPipeParent);
}
static void pipelineExec(struct pipeline* pl, int stdinFd, int stdoutFd, int stderrFd,
void *otherEndBuf, size_t otherEndBufSize)