| 1172 | } |
| 1173 | |
| 1174 | Result<Pipe> CreatePipe() { |
| 1175 | bool ok; |
| 1176 | int fds[2]; |
| 1177 | Pipe pipe; |
| 1178 | |
| 1179 | #if defined(_WIN32) |
| 1180 | ok = _pipe(fds, 4096, _O_BINARY) >= 0; |
| 1181 | if (ok) { |
| 1182 | pipe = {FileDescriptor(fds[0]), FileDescriptor(fds[1])}; |
| 1183 | } |
| 1184 | #elif defined(__linux__) && defined(__GLIBC__) |
| 1185 | // On Unix, we don't want the file descriptors to survive after an exec() call |
| 1186 | ok = pipe2(fds, O_CLOEXEC) >= 0; |
| 1187 | if (ok) { |
| 1188 | pipe = {FileDescriptor(fds[0]), FileDescriptor(fds[1])}; |
| 1189 | } |
| 1190 | #else |
| 1191 | auto set_cloexec = [](int fd) -> bool { |
| 1192 | int flags = fcntl(fd, F_GETFD); |
| 1193 | if (flags >= 0) { |
| 1194 | flags = fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
| 1195 | } |
| 1196 | return flags >= 0; |
| 1197 | }; |
| 1198 | |
| 1199 | ok = ::pipe(fds) >= 0; |
| 1200 | if (ok) { |
| 1201 | pipe = {FileDescriptor(fds[0]), FileDescriptor(fds[1])}; |
| 1202 | ok &= set_cloexec(fds[0]); |
| 1203 | if (ok) { |
| 1204 | ok &= set_cloexec(fds[1]); |
| 1205 | } |
| 1206 | } |
| 1207 | #endif |
| 1208 | if (!ok) { |
| 1209 | return IOErrorFromErrno(errno, "Error creating pipe"); |
| 1210 | } |
| 1211 | |
| 1212 | return pipe; |
| 1213 | } |
| 1214 | |
| 1215 | Status SetPipeFileDescriptorNonBlocking(int fd) { |
| 1216 | #if defined(_WIN32) |