about summary refs log tree commit diff
path: root/lib/libsystem.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2021-01-01 23:24:01 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2021-01-01 23:24:01 +0000
commitbf1ccd16d7d2e1c7ef86a6636bb5e96400464622 (patch)
tree7fc7d8c5ed68adc433e0e3445c7b5f67f705752d /lib/libsystem.c
parenteed74003114cd7d56939beb9c81961db3e0351a0 (diff)
downloadnetpbm-mirror-bf1ccd16d7d2e1c7ef86a6636bb5e96400464622.tar.gz
netpbm-mirror-bf1ccd16d7d2e1c7ef86a6636bb5e96400464622.tar.xz
netpbm-mirror-bf1ccd16d7d2e1c7ef86a6636bb5e96400464622.zip
Make standard input feeder process close extraneous inherited file descriptors
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@4020 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/libsystem.c')
-rw-r--r--lib/libsystem.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/lib/libsystem.c b/lib/libsystem.c
index ba48ed85..30b6b2be 100644
--- a/lib/libsystem.c
+++ b/lib/libsystem.c
@@ -40,8 +40,8 @@
 
 
 static void
-closeUninheritableFds(int const stdinFd,
-                      int const stdoutFd) {
+closeUninheritableFds(int const keepFdA,
+                      int const keepFdB) {
 /*----------------------------------------------------------------------------
   Close all the file descriptors that we declare uninheritable -- files Parent
   has open that Child has no business accessing.
@@ -49,14 +49,21 @@ closeUninheritableFds(int const stdinFd,
   Closing an extra file descriptor is essential to allow the file to close
   when Parent closes it.
 
+  It is also essential to prevent the system from messing with the position of
+  the file as the child process exits.  If the file descriptor is backing a
+  stream (FILE *), some process-exit code seeks the file to the current stream
+  position (from the readahead position), but having the file descriptor
+  closed defeats that.
+
   We define uninheritable as less than 64 and not Standard Input, Output,
-  or Error, or 'stdinFd' or 'stdoutFd'.
+  or Error, or 'keepFdA' or 'keepFdB'.
 -----------------------------------------------------------------------------*/
     int fd;
 
     for (fd = 0; fd < 64; ++fd) {
-        if (fd == stdinFd) {
-        } else if (fd == stdoutFd) {
+        if (false) {
+        } else if (fd == keepFdA) {
+        } else if (fd == keepFdB) {
         } else if (fd == STDIN_FILENO) {
         } else if (fd == STDOUT_FILENO) {
         } else if (fd == STDERR_FILENO) {
@@ -148,6 +155,9 @@ createPipeFeeder(void          pipeFeederRtn(int, void *),
     } else if (rc == 0) {
         /* This is the child -- the stdin feeder process */
         close(pipeToFeed[0]);
+
+        closeUninheritableFds(pipeToFeed[1], pipeToFeed[1]);
+
         (*pipeFeederRtn)(pipeToFeed[1], feederParm);
         exit(0);
     } else {