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
17 #include "perfetto/ext/base/pipe.h"
18
19 #include "perfetto/base/build_config.h"
20
21 #include <fcntl.h> // For O_BINARY (Windows) and F_SETxx (UNIX)
22
23 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
24 #include <Windows.h>
25 #include <namedpipeapi.h>
26 #else
27 #include <sys/types.h>
28 #include <unistd.h>
29 #endif
30
31 #include "perfetto/base/logging.h"
32
33 namespace perfetto {
34 namespace base {
35
36 Pipe::Pipe() = default;
37 Pipe::Pipe(Pipe&&) noexcept = default;
38 Pipe& Pipe::operator=(Pipe&&) = default;
39
Create(Flags flags)40 Pipe Pipe::Create(Flags flags) {
41 PlatformHandle fds[2];
42 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
43 PERFETTO_CHECK(::CreatePipe(&fds[0], &fds[1], /*lpPipeAttributes=*/nullptr,
44 0 /*default size*/));
45 #else
46 PERFETTO_CHECK(pipe(fds) == 0);
47 PERFETTO_CHECK(fcntl(fds[0], F_SETFD, FD_CLOEXEC) == 0);
48 PERFETTO_CHECK(fcntl(fds[1], F_SETFD, FD_CLOEXEC) == 0);
49 #endif
50 Pipe p;
51 p.rd.reset(fds[0]);
52 p.wr.reset(fds[1]);
53
54 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
55 if (flags == kBothNonBlock || flags == kRdNonBlock) {
56 int cur_flags = fcntl(*p.rd, F_GETFL, 0);
57 PERFETTO_CHECK(cur_flags >= 0);
58 PERFETTO_CHECK(fcntl(*p.rd, F_SETFL, cur_flags | O_NONBLOCK) == 0);
59 }
60
61 if (flags == kBothNonBlock || flags == kWrNonBlock) {
62 int cur_flags = fcntl(*p.wr, F_GETFL, 0);
63 PERFETTO_CHECK(cur_flags >= 0);
64 PERFETTO_CHECK(fcntl(*p.wr, F_SETFL, cur_flags | O_NONBLOCK) == 0);
65 }
66 #else
67 PERFETTO_CHECK(flags == kBothBlock);
68 #endif
69 return p;
70 }
71
72 } // namespace base
73 } // namespace perfetto
74