diff --git a/src/system/FileDescriptor.cxx b/src/system/FileDescriptor.cxx index d6d2a3b85..0e2d3cda5 100644 --- a/src/system/FileDescriptor.cxx +++ b/src/system/FileDescriptor.cxx @@ -125,6 +125,33 @@ FileDescriptor::CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept #ifndef _WIN32 +bool +FileDescriptor::CreatePipeNonBlock(FileDescriptor &r, + FileDescriptor &w) noexcept +{ + int fds[2]; + +#ifdef HAVE_PIPE2 + const int flags = O_CLOEXEC|O_NONBLOCK; + const int result = pipe2(fds, flags); +#else + const int result = pipe(fds); +#endif + + if (result < 0) + return false; + + r = FileDescriptor(fds[0]); + w = FileDescriptor(fds[1]); + +#ifndef HAVE_PIPE2 + r.SetNonBlocking(); + w.SetNonBlocking(); +#endif + + return true; +} + void FileDescriptor::SetNonBlocking() noexcept { diff --git a/src/system/FileDescriptor.hxx b/src/system/FileDescriptor.hxx index 4c0e1c3d0..7a5af768c 100644 --- a/src/system/FileDescriptor.hxx +++ b/src/system/FileDescriptor.hxx @@ -115,6 +115,9 @@ public: static bool CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept; #ifndef _WIN32 + static bool CreatePipeNonBlock(FileDescriptor &r, + FileDescriptor &w) noexcept; + /** * Enable non-blocking mode on this file descriptor. */