| 32 | } |
| 33 | |
| 34 | void TPipeHandle::Pipe(TPipeHandle& reader, TPipeHandle& writer, EOpenMode mode) { |
| 35 | PIPEHANDLE fds[2]; |
| 36 | #ifdef _win_ |
| 37 | int r = SocketPair(fds, false /* non-overlapped */, mode & CloseOnExec /* cloexec */); |
| 38 | #elif defined(_linux_) |
| 39 | int r = pipe2(fds, mode & CloseOnExec ? O_CLOEXEC : 0); |
| 40 | #else |
| 41 | int r = pipe(fds); |
| 42 | #endif |
| 43 | if (r < 0) { |
| 44 | ythrow TFileError() << "failed to create a pipe"; |
| 45 | } |
| 46 | |
| 47 | #if !defined(_win_) && !defined(_linux_) |
| 48 | // Non-atomic wrt exec |
| 49 | if (mode & CloseOnExec) { |
| 50 | for (int i = 0; i < 2; ++i) { |
| 51 | int flags = fcntl(fds[i], F_GETFD, 0); |
| 52 | if (flags < 0) { |
| 53 | ythrow TFileError() << "failed to get flags"; |
| 54 | } |
| 55 | int r = fcntl(fds[i], F_SETFD, flags | FD_CLOEXEC); |
| 56 | if (r < 0) { |
| 57 | ythrow TFileError() << "failed to set flags"; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | #endif |
| 62 | |
| 63 | TPipeHandle(fds[0]).Swap(reader); |
| 64 | TPipeHandle(fds[1]).Swap(writer); |
| 65 | } |
| 66 | |
| 67 | class TPipe::TImpl: public TAtomicRefCount<TImpl> { |
| 68 | public: |
nothing calls this directly
no test coverage detected