harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hinde...@apache.org
Subject svn commit: r620594 - /harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/procimpl.c
Date Mon, 11 Feb 2008 19:48:59 GMT
Author: hindessm
Date: Mon Feb 11 11:48:57 2008
New Revision: 620594

URL: http://svn.apache.org/viewvc?rev=620594&view=rev
Log:
Make sure we close all the file handles we've created rather than leak
them in the exec'd process.

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/procimpl.c

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/procimpl.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/procimpl.c?rev=620594&r1=620593&r2=620594&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/procimpl.c (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/procimpl.c Mon
Feb 11 11:48:57 2008
@@ -25,6 +25,7 @@
 #include <sys/ioctl.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <fcntl.h>
 #if defined(MACOSX)
 #include <crt_externs.h>
 #define environ (*_NSGetEnviron())
@@ -147,6 +148,20 @@
   if (grdpid == -1) goto error;
 
   if (grdpid == 0) {
+    /* Close file descriptors that are not used */
+    close(newFD[0][1]);
+    close(newFD[1][0]);
+    close(newFD[2][0]);
+    close(forkedChildIsRunning[0]);
+    close(execvFailure[0]);
+
+    /* Make sure the others close if the exec succeeds */
+    setCloseOnExec(newFD[0][0]); /* dup2 removes this on the new handle */
+    setCloseOnExec(newFD[1][1]);
+    setCloseOnExec(newFD[2][1]);
+    setCloseOnExec(forkedChildIsRunning[1]);
+    setCloseOnExec(execvFailure[1]);
+
     /* Redirect pipes so grand-child inherits new pipes */
     char dummy = '\0';
     dup2(newFD[0][0], 0);
@@ -302,4 +317,13 @@
 {
   /* The procHandle (Process ID) should not be closed, as it isn't a file descriptor. */
   return 0;
+}
+
+int
+setCloseOnExec(int fd)
+{
+  int flags = fcntl(fd, F_GETFD);
+  if (flags == -1) return -1;
+  flags |= FD_CLOEXEC;
+  return fcntl(fd, F_SETFD, flags);
 }



Mime
View raw message