• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ipc/host.h"
22 #include "perfetto/tracing/core/tracing_service.h"
23 #include "src/tracing/ipc/posix_shared_memory.h"
24 #include "src/tracing/ipc/service/consumer_ipc_service.h"
25 #include "src/tracing/ipc/service/producer_ipc_service.h"
26 
27 namespace perfetto {
28 
29 // TODO(fmayer): implement per-uid connection limit (b/69093705).
30 
31 // Implements the publicly exposed factory method declared in
32 // include/tracing/posix_ipc/posix_service_host.h.
CreateInstance(base::TaskRunner * task_runner)33 std::unique_ptr<ServiceIPCHost> ServiceIPCHost::CreateInstance(
34     base::TaskRunner* task_runner) {
35   return std::unique_ptr<ServiceIPCHost>(new ServiceIPCHostImpl(task_runner));
36 }
37 
ServiceIPCHostImpl(base::TaskRunner * task_runner)38 ServiceIPCHostImpl::ServiceIPCHostImpl(base::TaskRunner* task_runner)
39     : task_runner_(task_runner) {}
40 
~ServiceIPCHostImpl()41 ServiceIPCHostImpl::~ServiceIPCHostImpl() {}
42 
Start(const char * producer_socket_name,const char * consumer_socket_name)43 bool ServiceIPCHostImpl::Start(const char* producer_socket_name,
44                                const char* consumer_socket_name) {
45   PERFETTO_CHECK(!svc_);  // Check if already started.
46 
47   // Initialize the IPC transport.
48   producer_ipc_port_ =
49       ipc::Host::CreateInstance(producer_socket_name, task_runner_);
50   consumer_ipc_port_ =
51       ipc::Host::CreateInstance(consumer_socket_name, task_runner_);
52   return DoStart();
53 }
54 
Start(base::ScopedFile producer_socket_fd,base::ScopedFile consumer_socket_fd)55 bool ServiceIPCHostImpl::Start(base::ScopedFile producer_socket_fd,
56                                base::ScopedFile consumer_socket_fd) {
57   PERFETTO_CHECK(!svc_);  // Check if already started.
58 
59   // Initialize the IPC transport.
60   producer_ipc_port_ =
61       ipc::Host::CreateInstance(std::move(producer_socket_fd), task_runner_);
62   consumer_ipc_port_ =
63       ipc::Host::CreateInstance(std::move(consumer_socket_fd), task_runner_);
64   return DoStart();
65 }
66 
DoStart()67 bool ServiceIPCHostImpl::DoStart() {
68   // Create and initialize the platform-independent tracing business logic.
69   std::unique_ptr<SharedMemory::Factory> shm_factory(
70       new PosixSharedMemory::Factory());
71   svc_ = TracingService::CreateInstance(std::move(shm_factory), task_runner_);
72 
73   if (!producer_ipc_port_ || !consumer_ipc_port_) {
74     Shutdown();
75     return false;
76   }
77 
78   // TODO(fmayer): add a test that destroyes the ServiceIPCHostImpl soon after
79   // Start() and checks that no spurious callbacks are issued.
80   bool producer_service_exposed = producer_ipc_port_->ExposeService(
81       std::unique_ptr<ipc::Service>(new ProducerIPCService(svc_.get())));
82   PERFETTO_CHECK(producer_service_exposed);
83 
84   bool consumer_service_exposed = consumer_ipc_port_->ExposeService(
85       std::unique_ptr<ipc::Service>(new ConsumerIPCService(svc_.get())));
86   PERFETTO_CHECK(consumer_service_exposed);
87 
88   return true;
89 }
90 
service() const91 TracingService* ServiceIPCHostImpl::service() const {
92   return svc_.get();
93 }
94 
Shutdown()95 void ServiceIPCHostImpl::Shutdown() {
96   // TODO(primiano): add a test that causes the Shutdown() and checks that no
97   // spurious callbacks are issued.
98   producer_ipc_port_.reset();
99   consumer_ipc_port_.reset();
100   svc_.reset();
101 }
102 
103 // Definitions for the base class ctor/dtor.
104 ServiceIPCHost::ServiceIPCHost() = default;
105 ServiceIPCHost::~ServiceIPCHost() = default;
106 
107 }  // namespace perfetto
108