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