1 /*
2 * Copyright (C) 2017 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 "src/tracing/ipc/service/service_ipc_host_impl.h"
18
19 #include "perfetto/base/logging.h"
20 #include "perfetto/base/task_runner.h"
21 #include "perfetto/ext/ipc/host.h"
22 #include "perfetto/ext/tracing/core/tracing_service.h"
23 #include "src/tracing/ipc/service/consumer_ipc_service.h"
24 #include "src/tracing/ipc/service/producer_ipc_service.h"
25 #include "src/tracing/ipc/service/relay_ipc_service.h"
26
27 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
28 #include "src/tracing/ipc/shared_memory_windows.h"
29 #else
30 #include "src/tracing/ipc/posix_shared_memory.h"
31 #endif
32
33 namespace perfetto {
34
35 namespace {
36 constexpr uint32_t kProducerSocketTxTimeoutMs = 10;
37 }
38
39 // TODO(fmayer): implement per-uid connection limit (b/69093705).
40
41 // Implements the publicly exposed factory method declared in
42 // include/tracing/posix_ipc/posix_service_host.h.
CreateInstance(base::TaskRunner * task_runner,TracingService::InitOpts init_opts)43 std::unique_ptr<ServiceIPCHost> ServiceIPCHost::CreateInstance(
44 base::TaskRunner* task_runner,
45 TracingService::InitOpts init_opts) {
46 return std::unique_ptr<ServiceIPCHost>(
47 new ServiceIPCHostImpl(task_runner, init_opts));
48 }
49
ServiceIPCHostImpl(base::TaskRunner * task_runner,TracingService::InitOpts init_opts)50 ServiceIPCHostImpl::ServiceIPCHostImpl(base::TaskRunner* task_runner,
51 TracingService::InitOpts init_opts)
52 : task_runner_(task_runner), init_opts_(init_opts) {}
53
~ServiceIPCHostImpl()54 ServiceIPCHostImpl::~ServiceIPCHostImpl() {}
55
Start(const std::vector<std::string> & producer_socket_names,const char * consumer_socket_name)56 bool ServiceIPCHostImpl::Start(
57 const std::vector<std::string>& producer_socket_names,
58 const char* consumer_socket_name) {
59 PERFETTO_CHECK(!svc_); // Check if already started.
60
61 // Initialize the IPC transport.
62 for (const auto& producer_socket_name : producer_socket_names)
63 producer_ipc_ports_.emplace_back(
64 ipc::Host::CreateInstance(producer_socket_name.c_str(), task_runner_));
65 consumer_ipc_port_ =
66 ipc::Host::CreateInstance(consumer_socket_name, task_runner_);
67 return DoStart();
68 }
69
Start(base::ScopedSocketHandle producer_socket_fd,base::ScopedSocketHandle consumer_socket_fd)70 bool ServiceIPCHostImpl::Start(base::ScopedSocketHandle producer_socket_fd,
71 base::ScopedSocketHandle consumer_socket_fd) {
72 PERFETTO_CHECK(!svc_); // Check if already started.
73
74 // Initialize the IPC transport.
75 producer_ipc_ports_.emplace_back(
76 ipc::Host::CreateInstance(std::move(producer_socket_fd), task_runner_));
77 consumer_ipc_port_ =
78 ipc::Host::CreateInstance(std::move(consumer_socket_fd), task_runner_);
79 return DoStart();
80 }
81
Start(std::unique_ptr<ipc::Host> producer_host,std::unique_ptr<ipc::Host> consumer_host)82 bool ServiceIPCHostImpl::Start(std::unique_ptr<ipc::Host> producer_host,
83 std::unique_ptr<ipc::Host> consumer_host) {
84 PERFETTO_CHECK(!svc_); // Check if already started.
85 PERFETTO_DCHECK(producer_host);
86 PERFETTO_DCHECK(consumer_host);
87
88 // Initialize the IPC transport.
89 producer_ipc_ports_.emplace_back(std::move(producer_host));
90 consumer_ipc_port_ = std::move(consumer_host);
91
92 return DoStart();
93 }
94
DoStart()95 bool ServiceIPCHostImpl::DoStart() {
96 // Create and initialize the platform-independent tracing business logic.
97 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
98 std::unique_ptr<SharedMemory::Factory> shm_factory(
99 new SharedMemoryWindows::Factory());
100 #else
101 std::unique_ptr<SharedMemory::Factory> shm_factory(
102 new PosixSharedMemory::Factory());
103 #endif
104 svc_ = TracingService::CreateInstance(std::move(shm_factory), task_runner_,
105 init_opts_);
106
107 if (producer_ipc_ports_.empty() || !consumer_ipc_port_ ||
108 std::any_of(producer_ipc_ports_.begin(), producer_ipc_ports_.end(),
109 [](const std::unique_ptr<ipc::Host>& port) {
110 return port == nullptr;
111 })) {
112 Shutdown();
113 return false;
114 }
115
116 // Lower the timeout for blocking socket sends to producers as we shouldn't
117 // normally exhaust the kernel send buffer unless the producer is
118 // unresponsive. We'll drop the connection if the timeout is hit (see
119 // UnixSocket::Send). Context in b/236813972, b/193234818.
120 // Consumer port continues using the default timeout (10s) as there are
121 // generally fewer consumer processes, and they're better behaved. Also the
122 // consumer port ipcs might exhaust the send buffer under normal operation
123 // due to large messages such as ReadBuffersResponse.
124 for (auto& producer_ipc_port : producer_ipc_ports_)
125 producer_ipc_port->SetSocketSendTimeoutMs(kProducerSocketTxTimeoutMs);
126
127 // TODO(fmayer): add a test that destroyes the ServiceIPCHostImpl soon after
128 // Start() and checks that no spurious callbacks are issued.
129 for (auto& producer_ipc_port : producer_ipc_ports_) {
130 bool producer_service_exposed = producer_ipc_port->ExposeService(
131 std::unique_ptr<ipc::Service>(new ProducerIPCService(svc_.get())));
132 PERFETTO_CHECK(producer_service_exposed);
133
134 if (!init_opts_.enable_relay_endpoint)
135 continue;
136 // Expose a secondary service for sync with remote relay service
137 // if requested.
138 bool relay_service_exposed = producer_ipc_port->ExposeService(
139 std::unique_ptr<ipc::Service>(new RelayIPCService(svc_.get())));
140 PERFETTO_CHECK(relay_service_exposed);
141 }
142
143 bool consumer_service_exposed = consumer_ipc_port_->ExposeService(
144 std::unique_ptr<ipc::Service>(new ConsumerIPCService(svc_.get())));
145 PERFETTO_CHECK(consumer_service_exposed);
146
147 return true;
148 }
149
service() const150 TracingService* ServiceIPCHostImpl::service() const {
151 return svc_.get();
152 }
153
Shutdown()154 void ServiceIPCHostImpl::Shutdown() {
155 // TODO(primiano): add a test that causes the Shutdown() and checks that no
156 // spurious callbacks are issued.
157 producer_ipc_ports_.clear();
158 consumer_ipc_port_.reset();
159 svc_.reset();
160 }
161
162 // Definitions for the base class ctor/dtor.
163 ServiceIPCHost::ServiceIPCHost() = default;
164 ServiceIPCHost::~ServiceIPCHost() = default;
165
166 } // namespace perfetto
167