3030#include <unistd.h>
3131#include <string.h>
3232#include <ctype.h>
33+ #include <fcntl.h>
3334#include "sys.h"
3435#include "util.h"
3536#include "error_messages.h"
@@ -48,6 +49,21 @@ static char *skipNonWhitespace(char *p) {
4849 return p ;
4950}
5051
52+ static int
53+ markCloseOnExec (int fd )
54+ {
55+ const int flags = fcntl (fd , F_GETFD );
56+ if (flags < 0 ) {
57+ return -1 ;
58+ }
59+ if ((flags & FD_CLOEXEC ) == 0 ) {
60+ if (fcntl (fd , F_SETFD , flags | FD_CLOEXEC ) < 0 ) {
61+ return -1 ;
62+ }
63+ }
64+ return 0 ;
65+ }
66+
5167#if defined(_AIX )
5268 /* AIX does not understand '/proc/self' - it requires the real process ID */
5369 #define FD_DIR aix_fd_dir
@@ -57,87 +73,72 @@ static char *skipNonWhitespace(char *p) {
5773 #define FD_DIR "/proc/self/fd"
5874#endif
5975
60- // Closes every file descriptor that is listed as a directory
61- // entry in "/proc/self/fd" (or its equivalent). Standard
62- // input/output/error file descriptors will not be closed
63- // by this function. This function returns 0 on failure
64- // and 1 on success.
76+ // Marks all file descriptors found in /proc/self/fd with the
77+ // FD_CLOEXEC flag to ensure they are automatically closed
78+ // upon execution of a new program via exec(). This function
79+ // returns -1 on failure and 0 on success.
6580static int
66- closeDescriptors (void )
81+ markDescriptorsCloseOnExec (void )
6782{
6883 DIR * dp ;
6984 struct dirent * dirp ;
70- /* leave out standard input/output/error descriptors */
71- int from_fd = STDERR_FILENO + 1 ;
72-
73- /* We're trying to close all file descriptors, but opendir() might
74- * itself be implemented using a file descriptor, and we certainly
75- * don't want to close that while it's in use. We assume that if
76- * opendir() is implemented using a file descriptor, then it uses
77- * the lowest numbered file descriptor, just like open(). So
78- * before calling opendir(), we close a couple explicitly, so that
79- * opendir() can then use these lowest numbered closed file
80- * descriptors afresh.
81- *
82- * WARNING: We are not allowed to return with a failure until after
83- * these two closes are done. forkedChildProcess() relies on this. */
84-
85- close (from_fd ); /* for possible use by opendir() */
86- close (from_fd + 1 ); /* another one for good luck */
87- from_fd += 2 ; /* leave out the 2 we just closed, which the opendir() may use */
85+ const int from_fd = STDERR_FILENO ;
8886
8987#if defined(_AIX )
90- /* set FD_DIR for AIX which does not understand '/proc/self' - it
91- * requires the real process ID */
88+ /* AIX does not understand '/proc/self' - it requires the real process ID */
9289 char aix_fd_dir [32 ]; /* the pid has at most 19 digits */
9390 snprintf (aix_fd_dir , 32 , "/proc/%d/fd" , getpid ());
9491#endif
9592
9693 if ((dp = opendir (FD_DIR )) == NULL ) {
9794 ERROR_MESSAGE (("failed to open dir %s while determining"
98- " file descriptors to close for process %d" ,
95+ " file descriptors to mark or close for process %d" ,
9996 FD_DIR , getpid ()));
100- return 0 ; // failure
97+ return -1 ; // failure
10198 }
10299
100+ int dir_fd = dirfd (dp );
101+
103102 while ((dirp = readdir (dp )) != NULL ) {
104103 if (!isdigit (dirp -> d_name [0 ])) {
105104 continue ;
106105 }
107- const long fd = strtol (dirp -> d_name , NULL , 10 );
108- if (fd <= INT_MAX && fd >= from_fd ) {
109- (void )close ((int )fd );
106+ int fd = strtol (dirp -> d_name , NULL , 10 );
107+ if (fd <= INT_MAX && fd > from_fd && fd != dir_fd ) {
108+ if (markCloseOnExec (fd ) == -1 ) {
109+ (void )close ((int )fd );
110+ }
110111 }
111112 }
112113
113114 (void )closedir (dp );
114115
115- return 1 ; // success
116+ return 0 ; // success
116117}
117118
118- // Does necessary housekeeping of a forked child process
119- // (like closing copied file descriptors) before
120- // execing the child process. This function never returns.
119+ // Performs necessary housekeeping in the forked child process,
120+ // such as marking copied file descriptors (except standard input/output/error)
121+ // with FD_CLOEXEC to ensure they are closed during exec().
122+ // This function never returns.
121123static void
122124forkedChildProcess (const char * file , char * const argv [])
123125{
124- /* Close all file descriptors that have been copied over
125- * from the parent process due to fork(). */
126- if (closeDescriptors () == 0 ) { /* failed, close the old way */
126+ /* Mark all file descriptors (except standard input/output/error)
127+ * copied from the parent process with FD_CLOEXEC, so they are
128+ * closed automatically upon exec(). */
129+ if (markDescriptorsCloseOnExec () < 0 ) { /* failed, close the old way */
127130 /* Find max allowed file descriptors for a process
128131 * and assume all were opened for the parent process and
129132 * copied over to this child process. We close them all. */
130133 const rlim_t max_fd = sysconf (_SC_OPEN_MAX );
131134 JDI_ASSERT (max_fd != (rlim_t )- 1 ); // -1 represents error
132135 /* close(), that we subsequently call, takes only int values */
133136 JDI_ASSERT (max_fd <= INT_MAX );
134- /* Leave out standard input/output/error file descriptors. Also,
135- * leave out STDERR_FILENO +1 and +2 since closeDescriptors()
136- * already closed them, even when returning an error. */
137- rlim_t i = STDERR_FILENO + 3 ;
137+ /* leave out standard input/output/error file descriptors */
138+ rlim_t i = STDERR_FILENO + 1 ;
138139 ERROR_MESSAGE (("failed to close file descriptors of"
139140 " child process optimally, falling back to closing"
140- " %d file descriptors sequentially" , (max_fd - i + 1 )));
141+ " %d file descriptors sequentially" , (max_fd - i )));
141142 for (; i < max_fd ; i ++ ) {
142143 (void )close (i );
143144 }
0 commit comments