• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/pipe.h"
18 
19 #include <sys/types.h>
20 #include <unistd.h>
21 
22 #include "perfetto/base/logging.h"
23 
24 namespace perfetto {
25 namespace base {
26 
27 Pipe::Pipe() = default;
28 Pipe::Pipe(Pipe&&) noexcept = default;
29 Pipe& Pipe::operator=(Pipe&&) = default;
30 
Create(Flags flags)31 Pipe 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
57