refactor: use /proc to compute fd_max on linux

This commit is contained in:
Nicolas Duteil 2023-12-17 00:24:03 +01:00
parent 4bb8c35da7
commit 6037f2cdcf

View File

@ -2968,6 +2968,36 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
}
}
#if defined(__linux__)
int pid = getpid();
int max_fd = 0;
char path[32];
struct stat statbuf;
sprintf(path, "/proc/%d/fd", pid);
if (stat(path, &statbuf) == 0) {
if (S_ISDIR(statbuf.st_mode)) {
DIR *dir = opendir(path);
if (dir) {
struct dirent *subdir;
int fd;
for(;;) {
subdir = readdir(dir);
if (!subdir) {
break;
}
fd = atoi(subdir->d_name);
if (fd > max_fd) {
max_fd = fd;
}
}
if (max_fd > 0) {
fd_max = max_fd;
}
closedir(dir);
}
}
}
#endif
for(i = 3; i < fd_max; i++)
close(i);
if (cwd) {