blob: 5c9c3d7df7377dd1e6507a421947d92b9f100b91 [file] [log] [blame]
Primiano Tucci76499cd2018-11-21 10:54:04 +01001/*
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 Tucci2c5488f2019-06-01 03:27:28 +010017#include "perfetto/ext/base/pipe.h"
Primiano Tucci76499cd2018-11-21 10:54:04 +010018
19#include <sys/types.h>
20#include <unistd.h>
21
22#include "perfetto/base/logging.h"
23
24namespace perfetto {
25namespace base {
26
27Pipe::Pipe() = default;
28Pipe::Pipe(Pipe&&) noexcept = default;
29Pipe& Pipe::operator=(Pipe&&) = default;
30
31Pipe Pipe::Create(Flags flags) {
32 int fds[2];
33 PERFETTO_CHECK(pipe(fds) == 0);
34 Pipe p;
35 p.rd.reset(fds[0]);
36 p.wr.reset(fds[1]);
37
38 PERFETTO_CHECK(fcntl(*p.rd, F_SETFD, FD_CLOEXEC) == 0);
39 PERFETTO_CHECK(fcntl(*p.wr, F_SETFD, FD_CLOEXEC) == 0);
40
41 if (flags == kBothNonBlock || flags == kRdNonBlock) {
42 int cur_flags = fcntl(*p.rd, F_GETFL, 0);
43 PERFETTO_CHECK(cur_flags >= 0);
44 PERFETTO_CHECK(fcntl(*p.rd, F_SETFL, cur_flags | O_NONBLOCK) == 0);
45 }
46
47 if (flags == kBothNonBlock || flags == kWrNonBlock) {
48 int cur_flags = fcntl(*p.wr, F_GETFL, 0);
49 PERFETTO_CHECK(cur_flags >= 0);
50 PERFETTO_CHECK(fcntl(*p.wr, F_SETFL, cur_flags | O_NONBLOCK) == 0);
51 }
52 return p;
53}
54
55} // namespace base
56} // namespace perfetto