Skip to content

Commit

Permalink
Speed up closing all file descriptors in the child process (Fixes ecl…
Browse files Browse the repository at this point in the history
…ipse-cdt#835)

_This commit adds to eclipse-cdt#840 7bd8d52 to apply the same fix to
another place that does the same operation on all fds._

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 <sergey.simonchik@jetbrains.com>
(cherry picked from commit 3875408)
  • Loading branch information
adangel committed Jun 28, 2024
1 parent e0b3c71 commit b017ae0
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 8 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
75 changes: 67 additions & 8 deletions core/org.eclipse.cdt.core.native/native_src/unix/exec_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,76 @@
#include <string.h>
#include <libgen.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <dirent.h>
#include <ctype.h>

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

static int sys_close_range_wrapper(unsigned int from_fd_inclusive) {
// Use fast `close_range` (https://man7.org/linux/man-pages/man2/close_range.2.html) if available.
// Cannot call `close_range` from libc, as it may be unavailable in older libc.
#if defined(__linux__) && defined(SYS_close_range) && defined(CLOSE_RANGE_UNSHARE)
return syscall(SYS_close_range, from_fd_inclusive, ~0U, CLOSE_RANGE_UNSHARE);
#else
errno = ENOSYS;
return -1;
#endif
}

static int close_all_fds_using_parsing(unsigned int from_fd_inclusive) {
// If `opendir` is implemented using a file descriptor, we may close it accidentally.
// Let's close a few lowest file descriptors, in hope that `opendir` will use it.
int lowest_fds_to_close = 2;
for (int i = 0; i < lowest_fds_to_close; i++) {
close(from_fd_inclusive + i);
}

#if defined(__APPLE__)
#define FD_DIR "/dev/fd"
#else
#define FD_DIR "/proc/self/fd"
#endif

DIR *dirp = opendir(FD_DIR);
if (dirp == NULL)
return -1;

struct dirent *direntp;

while ((direntp = readdir(dirp)) != NULL) {
if (isdigit(direntp->d_name[0])) {
int fd = strtol(direntp->d_name, NULL, 10);
if (fd >= from_fd_inclusive + lowest_fds_to_close && fd != dirfd(dirp)) {
close(fd);
}
}
}

closedir(dirp);

return 0;
}

static void close_all_fds_fallback(unsigned int from_fd_inclusive) {
int fdlimit = sysconf(_SC_OPEN_MAX);
if (fdlimit == -1)
fdlimit = 65535; // arbitrary default, just in case
for (int fd = from_fd_inclusive; fd < fdlimit; fd++) {
close(fd);
}
}

static void close_all_fds() {
unsigned int from_fd = STDERR_FILENO + 1;
if (sys_close_range_wrapper(from_fd) == 0)
return;
if (close_all_fds_using_parsing(from_fd) == 0)
return;
close_all_fds_fallback(from_fd);
}

pid_t exec0(const char *path, char *const argv[], char *const envp[], const char *dirpath, int channels[3]) {
int pipe0[2], pipe1[2], pipe2[2];
pid_t childpid;
Expand Down Expand Up @@ -83,14 +149,7 @@ pid_t exec0(const char *path, char *const argv[], char *const envp[], const char
}

/* Close all the fd's in the child */
{
int fdlimit = sysconf(_SC_OPEN_MAX);
int fd = 3;

while (fd < fdlimit) {
close(fd++);
}
}
close_all_fds();

setpgid(getpid(), getpid());

Expand Down

0 comments on commit b017ae0

Please sign in to comment.