about summary refs log tree commit diff
path: root/lib/libsystem.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2017-04-23 18:12:45 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2017-04-23 18:12:45 +0000
commited6bb995ac029606bf1f4fec56e91655d824ec24 (patch)
tree92a0fb4a0793543e0e526bf339c5a44b76ce8d5c /lib/libsystem.c
parent79f1c6bf5a89a4e92fbfb8e6930086ea98877c35 (diff)
downloadnetpbm-mirror-ed6bb995ac029606bf1f4fec56e91655d824ec24.tar.gz
netpbm-mirror-ed6bb995ac029606bf1f4fec56e91655d824ec24.tar.xz
netpbm-mirror-ed6bb995ac029606bf1f4fec56e91655d824ec24.zip
Don't close inherited Standard Input
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@2964 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/libsystem.c')
-rw-r--r--lib/libsystem.c24
1 files changed, 18 insertions, 6 deletions
diff --git a/lib/libsystem.c b/lib/libsystem.c
index 4ff473dd..57073dce 100644
--- a/lib/libsystem.c
+++ b/lib/libsystem.c
@@ -436,14 +436,23 @@ pm_system2_vp(const char *    const progName,
     */
     
     int progStdinFd;
+        /* File descriptor that the processor program will get as Standard
+           Input
+        */
+    bool weCreatedStdinFd;
+        /* This program created (opened) file descriptor 'progStdinFd',
+           as opposed to inheriting it.
+        */
     pid_t feederPid;
     pid_t processorPid;
     int termStatus;
 
-    if (stdinFeeder) 
+    if (stdinFeeder) {
         createPipeFeeder(stdinFeeder, feederParm, &progStdinFd, &feederPid);
-    else {
+        weCreatedStdinFd = true;
+    } else {
         progStdinFd = STDIN;
+        weCreatedStdinFd = false;
         feederPid = 0;
     }
 
@@ -456,10 +465,6 @@ pm_system2_vp(const char *    const progName,
         spawnProcessor(progName, argArray, progStdinFd, 
                        &progStdoutFd, &processorPid);
 
-        /* The child process has cloned our 'progStdinFd'; we have no
-           more use for our copy.
-        */
-        close(progStdinFd);
         /* Dispose of the stdout from that child */
         (*stdoutAccepter)(progStdoutFd, accepterParm);
         close(progStdoutFd);
@@ -470,6 +475,13 @@ pm_system2_vp(const char *    const progName,
         spawnProcessor(progName, argArray, progStdinFd, NULL, &processorPid);
     }
 
+    if (weCreatedStdinFd) {
+        /* The child process has cloned our 'progStdinFd'; we have no
+           more use for our copy.
+        */
+        close(progStdinFd);
+    }
+
     waitpid(processorPid, &termStatus, 0);
 
     if (feederPid)