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