about summary refs log tree commit diff
path: root/lib/libsystem.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2006-10-18 17:25:05 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2006-10-18 17:25:05 +0000
commit15313a85f8d8a592dfd2bf68550d903201322a6d (patch)
tree87d6c9438dbf916a69cdb948c5032ded4039c8be /lib/libsystem.c
parent34d905eb05083faa7267ea34550c54e7bd753e98 (diff)
downloadnetpbm-mirror-15313a85f8d8a592dfd2bf68550d903201322a6d.tar.gz
netpbm-mirror-15313a85f8d8a592dfd2bf68550d903201322a6d.tar.xz
netpbm-mirror-15313a85f8d8a592dfd2bf68550d903201322a6d.zip
Don't hang when shell command doesn't take all of Standard Input
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@87 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/libsystem.c')
-rw-r--r--lib/libsystem.c33
1 files changed, 19 insertions, 14 deletions
diff --git a/lib/libsystem.c b/lib/libsystem.c
index e0d62178..59e05a5f 100644
--- a/lib/libsystem.c
+++ b/lib/libsystem.c
@@ -84,21 +84,21 @@ createPipeFeeder(void          pipeFeederRtn(int, void *),
    other end of the pipe as *fdP.
 -----------------------------------------------------------------------------*/
     int pipeToFeed[2];
-    pid_t feederPid;
+    pid_t rc;
 
     pipe(pipeToFeed);
-    feederPid = fork();
-    if (feederPid < 0) {
+    rc = fork();
+    if (rc < 0) {
         pm_error("fork() of stdin feeder failed.  errno=%d (%s)", 
                  errno, strerror(errno));
-    } else if (feederPid == 0) {
+    } else if (rc == 0) {
         /* This is the child -- the stdin feeder process */
         close(pipeToFeed[0]);
         (*pipeFeederRtn)(pipeToFeed[1], feederParm);
         exit(0);
-    }
-    else {
+    } else {
         /* This is the parent */
+        pid_t const feederPid = rc;
         close(pipeToFeed[1]);
         *fdP = pipeToFeed[0];
         *pidP = feederPid;
@@ -120,15 +120,15 @@ spawnProcessor(const char * const shellCommand,
    from which Caller can suck the shell's Standard Output.
 -----------------------------------------------------------------------------*/
     int stdoutpipe[2];
-    pid_t processorpid;
+    pid_t rc;
         
     pipe(stdoutpipe);
 
-    processorpid = fork();
-    if (processorpid < 0) {
+    rc = fork();
+    if (rc < 0) {
         pm_error("fork() of processor process failed.  errno=%d (%s)\n", 
                  errno, strerror(errno));
-    } else if (processorpid == 0) {
+    } else if (rc == 0) {
         /* The second child */
         close(stdoutpipe[0]);
 
@@ -139,6 +139,7 @@ spawnProcessor(const char * const shellCommand,
         pm_error("INTERNAL ERROR: execProgram() returns.");
     } else {
         /* The parent */
+        pid_t const processorpid = rc;
         close(stdoutpipe[1]);
         *stdoutFdP = stdoutpipe[0];
         *pidP = processorpid;
@@ -212,11 +213,11 @@ pm_system(void stdinFeeder(int, void *),
 
        If 'stdoutFeeder' is non-NULL, we create a child process to run
        the shell and create a pipe between the shell's Standard Output
-       and this process, and then this process runs 'stdoutAccepter'
-       to read the data from that pipe.
+       and the current process, and then the current process runs
+       'stdoutAccepter' to read the data from that pipe.
        
-       But if 'stdoutFeeder' is NULL, we just run the shell in this
-       process.
+       But if 'stdoutFeeder' is NULL, we just run the shell in the
+       current process.
 
        So there can be 1, 2, or 3 processes involved depending on 
        parameters.
@@ -242,6 +243,10 @@ pm_system(void stdinFeeder(int, void *),
         spawnProcessor(shellCommand, shellStdinFd, 
                        &shellStdoutFd, &processorPid);
 
+        /* The shell process has cloned our 'shellStdinFd'; we have no
+           more use for our copy.
+        */
+        close(shellStdinFd);
         /* Dispose of the stdout from that shell */
         (*stdoutAccepter)(shellStdoutFd, accepterParm);
         close(shellStdoutFd);