From ff87121f963b5f308ef3d47931c3d67e32296380 Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Mon, 27 Jan 2025 13:18:01 +0800 Subject: [PATCH] channel.c: fix for macOS when F_DUPFD_CLOEXEC is undefined --- src/nvim/channel.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git src/nvim/channel.c src/nvim/channel.c index 912d515f84..58f995aa57 100644 --- a/src/nvim/channel.c +++ b/src/nvim/channel.c @@ -546,9 +546,18 @@ uint64_t channel_from_stdio(bool rpc, CallbackReader on_output, const char **err if (embedded_mode) { // Redirect stdout/stdin (the UI channel) to stderr. Use fnctl(F_DUPFD_CLOEXEC) instead of dup() // to prevent child processes from inheriting the file descriptors, which are used by UIs to - // detect when Nvim exits. + // detect when Nvim exits. Fallback to F_DUPFD when F_DUPFD_CLOEXEC is unavailable. +#ifdef F_DUPFD_CLOEXEC stdin_dup_fd = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, STDERR_FILENO + 1); stdout_dup_fd = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, STDERR_FILENO + 1); +#else + stdin_dup_fd = fcntl(STDIN_FILENO, F_DUPFD, STDERR_FILENO + 1); + if (stdin_dup_fd > 0) + fcntl(stdin_dup_fd, F_SETFD, FD_CLOEXEC); + stdout_dup_fd = fcntl(STDOUT_FILENO, F_DUPFD, STDERR_FILENO + 1); + if (stdout_dup_fd > 0) + fcntl(stdout_dup_fd, F_SETFD, FD_CLOEXEC); +#endif dup2(STDERR_FILENO, STDOUT_FILENO); dup2(STDERR_FILENO, STDIN_FILENO); }