Skip to content

Commit f40c481

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]> cherry picked and squashed from commits: - 7bd8d52 - 24d9bd1 - 3875408
1 parent 1fca711 commit f40c481

File tree

9 files changed

+136
-18
lines changed

9 files changed

+136
-18
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
552 Bytes
Binary file not shown.

core/org.eclipse.cdt.core.native/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.cdt.core.native;singleton:=true
5-
Bundle-Version: 6.3.400.qualifier
5+
Bundle-Version: 6.3.401.qualifier
66
Bundle-Activator: org.eclipse.cdt.internal.core.natives.CNativePlugin
77
Bundle-Vendor: %providerName
88
Bundle-Localization: plugin

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

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,76 @@
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)
61+
return -1;
62+
63+
struct dirent *direntp;
64+
65+
while ((direntp = readdir(dirp)) != NULL) {
66+
if (isdigit(direntp->d_name[0])) {
67+
int fd = strtol(direntp->d_name, NULL, 10);
68+
if (fd >= from_fd_inclusive + lowest_fds_to_close && fd != dirfd(dirp)) {
69+
close(fd);
70+
}
71+
}
72+
}
73+
74+
closedir(dirp);
75+
76+
return 0;
77+
}
78+
79+
static void close_all_fds_fallback(unsigned int from_fd_inclusive) {
80+
int fdlimit = sysconf(_SC_OPEN_MAX);
81+
if (fdlimit == -1)
82+
fdlimit = 65535; // arbitrary default, just in case
83+
for (int fd = from_fd_inclusive; fd < fdlimit; fd++) {
84+
close(fd);
85+
}
86+
}
87+
88+
static void close_all_fds() {
89+
unsigned int from_fd = STDERR_FILENO + 1;
90+
if (sys_close_range_wrapper(from_fd) == 0)
91+
return;
92+
if (close_all_fds_using_parsing(from_fd) == 0)
93+
return;
94+
close_all_fds_fallback(from_fd);
95+
}
96+
3197
pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const char *dirpath, int channels[3],
3298
const char *pts_name, int fdm, int console) {
3399
int pipe2[2];
@@ -107,14 +173,7 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
107173
}
108174

109175
/* 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-
}
176+
close_all_fds();
118177

119178
if (envp && envp[0]) {
120179
execve(full_path, argv, envp);

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

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,76 @@
2121
#include <string.h>
2222
#include <libgen.h>
2323
#include <stdlib.h>
24+
#include <sys/syscall.h>
25+
#include <dirent.h>
26+
#include <ctype.h>
2427

2528
/* from pfind.c */
2629
extern char *pfind(const char *name, char *const envp[]);
2730

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

85151
/* Close all the fd's in the child */
86-
{
87-
int fdlimit = sysconf(_SC_OPEN_MAX);
88-
int fd = 3;
89-
90-
while (fd < fdlimit) {
91-
close(fd++);
92-
}
93-
}
152+
close_all_fds();
94153

95154
setpgid(getpid(), getpid());
96155

core/org.eclipse.cdt.core.native/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<relativePath>../../pom.xml</relativePath>
2424
</parent>
2525

26-
<version>6.3.400-SNAPSHOT</version>
26+
<version>6.3.401-SNAPSHOT</version>
2727
<artifactId>org.eclipse.cdt.core.native</artifactId>
2828
<packaging>eclipse-plugin</packaging>
2929

0 commit comments

Comments
 (0)