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; }