Skip to content

Commit 7bd8d52

Browse files
adangelsegrey
authored andcommitted
Speed up closing all file descriptors in the child process (Fixes #835)
When _SC_OPEN_MAX (max nr of open files limit per process) is a very big number, then closing all possible file handles can take a while. This change first tries to use the syscall close_range() if available, falling back to use /proc/self/fd to close only open file handles, and lastly falling back to the old way of closing all possible handles one after another. In general, the first or second approach should be available which speeds up the pty spawning. Refs JetBrains/pty4j#147 Copied from JetBrains/pty4j@04685d8 (which is EPL 1.0) Co-authored-by: Sergey Simonchik <[email protected]>
1 parent 0f08a76 commit 7bd8d52

File tree

6 files changed

+63
-8
lines changed

6 files changed

+63
-8
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

core/org.eclipse.cdt.core.native/native_src/unix/exec_pty.c

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,72 @@
2424
#include <libgen.h>
2525
#include <stdlib.h>
2626
#include <termios.h>
27+
#include <sys/syscall.h>
28+
#include <dirent.h>
29+
#include <ctype.h>
2730

2831
/* from pfind.c */
2932
extern char *pfind(const char *name, char *const envp[]);
3033

34+
static int sys_close_range_wrapper(unsigned int from_fd_inclusive) {
35+
// Use fast `close_range` (https://man7.org/linux/man-pages/man2/close_range.2.html) if available.
36+
// Cannot call `close_range` from libc, as it may be unavailable in older libc.
37+
# if defined(__linux__) && defined(SYS_close_range) && defined(CLOSE_RANGE_UNSHARE)
38+
return syscall(SYS_close_range, from_fd_inclusive, ~0U, CLOSE_RANGE_UNSHARE);
39+
# else
40+
errno = ENOSYS;
41+
return -1;
42+
# endif
43+
}
44+
45+
static int close_all_fds_using_parsing(unsigned int from_fd_inclusive) {
46+
// If `opendir` is implemented using a file descriptor, we may close it accidentally.
47+
// Let's close a few lowest file descriptors, in hope that `opendir` will use it.
48+
int lowest_fds_to_close = 2;
49+
for (int i = 0; i < lowest_fds_to_close; i++) {
50+
close(from_fd_inclusive + i);
51+
}
52+
53+
#if defined(__APPLE__)
54+
#define FD_DIR "/dev/fd"
55+
#else
56+
#define FD_DIR "/proc/self/fd"
57+
#endif
58+
59+
DIR *dirp = opendir(FD_DIR);
60+
if (dirp == NULL) return -1;
61+
62+
struct dirent *direntp;
63+
64+
while ((direntp = readdir(dirp)) != NULL) {
65+
if (isdigit(direntp->d_name[0])) {
66+
int fd = strtol(direntp->d_name, NULL, 10);
67+
if (fd >= from_fd_inclusive + lowest_fds_to_close && fd != dirfd(dirp)) {
68+
close(fd);
69+
}
70+
}
71+
}
72+
73+
closedir(dirp);
74+
75+
return 0;
76+
}
77+
78+
static void close_all_fds_fallback(unsigned int from_fd_inclusive) {
79+
int fdlimit = sysconf(_SC_OPEN_MAX);
80+
if (fdlimit == -1) fdlimit = 65535; // arbitrary default, just in case
81+
for (int fd = from_fd_inclusive; fd < fdlimit; fd++) {
82+
close(fd);
83+
}
84+
}
85+
86+
static void close_all_fds() {
87+
unsigned int from_fd = STDERR_FILENO + 1;
88+
if (sys_close_range_wrapper(from_fd) == 0) return;
89+
if (close_all_fds_using_parsing(from_fd) == 0) return;
90+
close_all_fds_fallback(from_fd);
91+
}
92+
3193
pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const char *dirpath, int channels[3],
3294
const char *pts_name, int fdm, int console) {
3395
int pipe2[2];
@@ -107,14 +169,7 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
107169
}
108170

109171
/* Close all the fd's in the child */
110-
{
111-
int fdlimit = sysconf(_SC_OPEN_MAX);
112-
int fd = 3;
113-
114-
while (fd < fdlimit) {
115-
close(fd++);
116-
}
117-
}
172+
close_all_fds();
118173

119174
if (envp && envp[0]) {
120175
execve(full_path, argv, envp);

0 commit comments

Comments
 (0)