From bf6576a2f3235ed766d9b2a40b5ae145ba170773 Mon Sep 17 00:00:00 2001 From: Steve Gilvarry Date: Thu, 23 Jul 2026 05:41:57 +1000 Subject: [PATCH] fix: emulate pipe2 with pipe + fcntl on platforms without it pipe2() is Linux-only; macOS and the BSDs fail to compile with 'use of undeclared identifier pipe2'. Fall back to pipe() followed by fcntl() to set O_NONBLOCK and FD_CLOEXEC on both ends. Co-Authored-By: Claude Opus 4.8 --- src/net/Pipe.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/net/Pipe.cpp b/src/net/Pipe.cpp index b43dbbb..4ab1a5f 100644 --- a/src/net/Pipe.cpp +++ b/src/net/Pipe.cpp @@ -6,6 +6,10 @@ #include #include #include +#if !defined(WIN32) && !defined(_WIN32) +#include +#include +#endif using namespace xop; @@ -56,10 +60,22 @@ bool Pipe::Create() SocketUtil::SetNonBlock(pipe_fd_[0]); SocketUtil::SetNonBlock(pipe_fd_[1]); -#else /* Not Windows */ +#elif defined(__linux) || defined(__linux__) if (pipe2(pipe_fd_, O_NONBLOCK | O_CLOEXEC) < 0) { return false; } +#else /* macOS/BSD: no pipe2(), emulate with pipe() + fcntl() */ + if (pipe(pipe_fd_) < 0) { + return false; + } + for (int i = 0; i < 2; i++) { + if (fcntl(pipe_fd_[i], F_SETFL, fcntl(pipe_fd_[i], F_GETFL) | O_NONBLOCK) < 0 || + fcntl(pipe_fd_[i], F_SETFD, FD_CLOEXEC) < 0) { + ::close(pipe_fd_[0]); + ::close(pipe_fd_[1]); + return false; + } + } #endif return true; }