diff options
author | Max Kellermann <max@musicpd.org> | 2018-08-20 17:02:34 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2018-08-20 17:05:29 +0200 |
commit | 8106929d60d513332fa7e7c9051a377f90bd3460 (patch) | |
tree | 1651a3fb57411ce4c0f49f967c1db01e1923b882 /src/system | |
parent | 795baed3f5c56a9a05c046b7ddd6d93eba39a560 (diff) |
system/FileDescriptor: add pipe2() wrapper
Diffstat (limited to 'src/system')
-rw-r--r-- | src/system/FileDescriptor.cxx | 44 | ||||
-rw-r--r-- | src/system/FileDescriptor.hxx | 5 |
2 files changed, 31 insertions, 18 deletions
diff --git a/src/system/FileDescriptor.cxx b/src/system/FileDescriptor.cxx index 8f7f78e99..db0814469 100644 --- a/src/system/FileDescriptor.cxx +++ b/src/system/FileDescriptor.cxx @@ -115,15 +115,33 @@ FileDescriptor::OpenNonBlocking(const char *pathname) noexcept #endif +#ifdef __linux__ + bool -FileDescriptor::CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept +FileDescriptor::CreatePipe(FileDescriptor &r, FileDescriptor &w, + int flags) noexcept { int fds[2]; + const int result = pipe2(fds, flags); + if (result < 0) + return false; + + r = FileDescriptor(fds[0]); + w = FileDescriptor(fds[1]); + return true; +} +#endif + +bool +FileDescriptor::CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept +{ #ifdef __linux__ - const int flags = O_CLOEXEC; - const int result = pipe2(fds, flags); -#elif defined(_WIN32) + return CreatePipe(r, w, O_CLOEXEC); +#else + int fds[2]; + +#ifdef _WIN32 const int result = _pipe(fds, 512, _O_BINARY); #else const int result = pipe(fds); @@ -135,6 +153,7 @@ FileDescriptor::CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept r = FileDescriptor(fds[0]); w = FileDescriptor(fds[1]); return true; +#endif } #ifndef _WIN32 @@ -143,27 +162,16 @@ bool FileDescriptor::CreatePipeNonBlock(FileDescriptor &r, FileDescriptor &w) noexcept { - int fds[2]; - #ifdef __linux__ - const int flags = O_CLOEXEC|O_NONBLOCK; - const int result = pipe2(fds, flags); + return CreatePipe(r, w, O_CLOEXEC|O_NONBLOCK); #else - const int result = pipe(fds); -#endif - - if (result < 0) + if (!CreatePipe(r, w)) return false; - r = FileDescriptor(fds[0]); - w = FileDescriptor(fds[1]); - -#ifndef __linux__ r.SetNonBlocking(); w.SetNonBlocking(); -#endif - return true; +#endif } void diff --git a/src/system/FileDescriptor.hxx b/src/system/FileDescriptor.hxx index b4294c246..849cde5ec 100644 --- a/src/system/FileDescriptor.hxx +++ b/src/system/FileDescriptor.hxx @@ -129,6 +129,11 @@ public: bool OpenNonBlocking(const char *pathname) noexcept; #endif +#ifdef __linux__ + static bool CreatePipe(FileDescriptor &r, FileDescriptor &w, + int flags) noexcept; +#endif + static bool CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept; #ifdef _WIN32 |