Primiano Tucci | 76499cd | 2018-11-21 10:54:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 17 | #include "perfetto/ext/base/pipe.h" |
Primiano Tucci | 76499cd | 2018-11-21 10:54:04 +0100 | [diff] [blame] | 18 | |
Primiano Tucci | 1366dd0 | 2020-12-02 10:17:10 +0100 | [diff] [blame] | 19 | #include "perfetto/base/build_config.h" |
| 20 | |
Primiano Tucci | d4d08d3 | 2020-12-08 15:35:13 +0100 | [diff] [blame] | 21 | #include <fcntl.h> // For O_BINARY (Windows) and F_SETxx (UNIX) |
| 22 | |
Primiano Tucci | 1366dd0 | 2020-12-02 10:17:10 +0100 | [diff] [blame] | 23 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
| 24 | #include <io.h> |
| 25 | #else |
Primiano Tucci | 76499cd | 2018-11-21 10:54:04 +0100 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
Primiano Tucci | 1366dd0 | 2020-12-02 10:17:10 +0100 | [diff] [blame] | 28 | #endif |
Primiano Tucci | 76499cd | 2018-11-21 10:54:04 +0100 | [diff] [blame] | 29 | |
| 30 | #include "perfetto/base/logging.h" |
| 31 | |
| 32 | namespace perfetto { |
| 33 | namespace base { |
| 34 | |
| 35 | Pipe::Pipe() = default; |
| 36 | Pipe::Pipe(Pipe&&) noexcept = default; |
| 37 | Pipe& Pipe::operator=(Pipe&&) = default; |
| 38 | |
| 39 | Pipe Pipe::Create(Flags flags) { |
| 40 | int fds[2]; |
Primiano Tucci | 1366dd0 | 2020-12-02 10:17:10 +0100 | [diff] [blame] | 41 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
| 42 | PERFETTO_CHECK(_pipe(fds, 256, O_BINARY) == 0); |
| 43 | #else |
Primiano Tucci | 76499cd | 2018-11-21 10:54:04 +0100 | [diff] [blame] | 44 | PERFETTO_CHECK(pipe(fds) == 0); |
Primiano Tucci | 1366dd0 | 2020-12-02 10:17:10 +0100 | [diff] [blame] | 45 | PERFETTO_CHECK(fcntl(fds[0], F_SETFD, FD_CLOEXEC) == 0); |
| 46 | PERFETTO_CHECK(fcntl(fds[1], F_SETFD, FD_CLOEXEC) == 0); |
| 47 | #endif |
Primiano Tucci | 76499cd | 2018-11-21 10:54:04 +0100 | [diff] [blame] | 48 | Pipe p; |
| 49 | p.rd.reset(fds[0]); |
| 50 | p.wr.reset(fds[1]); |
| 51 | |
Primiano Tucci | 1366dd0 | 2020-12-02 10:17:10 +0100 | [diff] [blame] | 52 | #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
Primiano Tucci | 76499cd | 2018-11-21 10:54:04 +0100 | [diff] [blame] | 53 | if (flags == kBothNonBlock || flags == kRdNonBlock) { |
| 54 | int cur_flags = fcntl(*p.rd, F_GETFL, 0); |
| 55 | PERFETTO_CHECK(cur_flags >= 0); |
| 56 | PERFETTO_CHECK(fcntl(*p.rd, F_SETFL, cur_flags | O_NONBLOCK) == 0); |
| 57 | } |
| 58 | |
| 59 | if (flags == kBothNonBlock || flags == kWrNonBlock) { |
| 60 | int cur_flags = fcntl(*p.wr, F_GETFL, 0); |
| 61 | PERFETTO_CHECK(cur_flags >= 0); |
| 62 | PERFETTO_CHECK(fcntl(*p.wr, F_SETFL, cur_flags | O_NONBLOCK) == 0); |
| 63 | } |
Primiano Tucci | 1366dd0 | 2020-12-02 10:17:10 +0100 | [diff] [blame] | 64 | #else |
| 65 | PERFETTO_CHECK(flags == kBothBlock); |
| 66 | #endif |
Primiano Tucci | 76499cd | 2018-11-21 10:54:04 +0100 | [diff] [blame] | 67 | return p; |
| 68 | } |
| 69 | |
| 70 | } // namespace base |
| 71 | } // namespace perfetto |