Home | History | Annotate | Download | only in service
      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/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.
     33 std::unique_ptr<ServiceIPCHost> ServiceIPCHost::CreateInstance(
     34     base::TaskRunner* task_runner) {
     35   return std::unique_ptr<ServiceIPCHost>(new ServiceIPCHostImpl(task_runner));
     36 }
     37 
     38 ServiceIPCHostImpl::ServiceIPCHostImpl(base::TaskRunner* task_runner)
     39     : task_runner_(task_runner) {}
     40 
     41 ServiceIPCHostImpl::~ServiceIPCHostImpl() {}
     42 
     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 
     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 
     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_ = Service::CreateInstance(std::move(shm_factory), task_runner_);
     72 
     73   if (!producer_ipc_port_) {
     74     Shutdown();
     75     return false;
     76   }
     77 
     78   if (!producer_ipc_port_) {
     79     Shutdown();
     80     return false;
     81   }
     82 
     83   // TODO(fmayer): add a test that destroyes the ServiceIPCHostImpl soon after
     84   // Start() and checks that no spurious callbacks are issued.
     85   bool producer_service_exposed = producer_ipc_port_->ExposeService(
     86       std::unique_ptr<ipc::Service>(new ProducerIPCService(svc_.get())));
     87   PERFETTO_CHECK(producer_service_exposed);
     88 
     89   bool consumer_service_exposed = consumer_ipc_port_->ExposeService(
     90       std::unique_ptr<ipc::Service>(new ConsumerIPCService(svc_.get())));
     91   PERFETTO_CHECK(consumer_service_exposed);
     92 
     93   return true;
     94 }
     95 
     96 Service* ServiceIPCHostImpl::service_for_testing() const {
     97   return svc_.get();
     98 }
     99 
    100 void ServiceIPCHostImpl::Shutdown() {
    101   // TODO(primiano): add a test that causes the Shutdown() and checks that no
    102   // spurious callbacks are issued.
    103   producer_ipc_port_.reset();
    104   consumer_ipc_port_.reset();
    105   svc_.reset();
    106 }
    107 
    108 // Definitions for the base class ctor/dtor.
    109 ServiceIPCHost::ServiceIPCHost() = default;
    110 ServiceIPCHost::~ServiceIPCHost() = default;
    111 
    112 }  // namespace perfetto
    113