From 70a6e6a9107166e6aead671ce1825f1f533d5faf Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 21 Nov 2024 06:55:53 +0100 Subject: [PATCH] Use closefrom() to close open file descriptors Replace the naive close() loop in js_os_exec with closefrom(). On my system RLIMIT_NOFILE is set to 1 million and the delay from the loop gets noticeable when I spawn many processes. Fixes: https://github.com/quickjs-ng/quickjs/issues/711 --- quickjs-libc.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/quickjs-libc.c b/quickjs-libc.c index 55a0eda6..11bf0ea3 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -3123,8 +3123,6 @@ static JSValue js_os_exec(JSContext *ctx, JSValue this_val, } if (pid == 0) { /* child */ - int fd_max = sysconf(_SC_OPEN_MAX); - /* remap the stdin/stdout/stderr handles if necessary */ for(i = 0; i < 3; i++) { if (std_fds[i] != i) { @@ -3132,9 +3130,13 @@ static JSValue js_os_exec(JSContext *ctx, JSValue this_val, _exit(127); } } - +#if defined(__APPLE__) + int fd_max = sysconf(_SC_OPEN_MAX); for(i = 3; i < fd_max; i++) close(i); +#else + closefrom(3); +#endif if (cwd) { if (chdir(cwd) < 0) _exit(127);