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 #ifndef INCLUDE_PERFETTO_EXT_TRACING_IPC_SERVICE_IPC_HOST_H_ 18 #define INCLUDE_PERFETTO_EXT_TRACING_IPC_SERVICE_IPC_HOST_H_ 19 20 #include <list> 21 #include <memory> 22 23 #include "perfetto/base/export.h" 24 #include "perfetto/ext/base/unix_socket.h" 25 #include "perfetto/ext/tracing/core/basic_types.h" 26 #include "perfetto/ext/tracing/core/tracing_service.h" 27 28 namespace perfetto { 29 namespace base { 30 class TaskRunner; 31 } // namespace base. 32 33 namespace ipc { 34 class Host; 35 } // namespace ipc 36 37 // The argument passed to ServiceIPCHost::Start. Can be either: 38 // 1. a socket name (e.g., "/dev/unix/socket" for AF_UNIX, "127.0.0.1:1234" for 39 // TCP, "vsock://1:1234") 40 // 2. A FD of a pre-bound socket. This handles the case of Android in-tree 41 // builds where init creates the socket and passes the FD in env var 42 // (See perfetto.rc). 43 // 3. A pre-existing ipc::Host object. 44 struct ListenEndpoint { 45 explicit ListenEndpoint(const char* socket_name); 46 explicit ListenEndpoint(std::string socket_name); 47 explicit ListenEndpoint(base::ScopedSocketHandle); 48 explicit ListenEndpoint(std::unique_ptr<ipc::Host>); 49 ~ListenEndpoint(); 50 51 // Allow move but not copy. 52 ListenEndpoint(ListenEndpoint&&) noexcept; 53 ListenEndpoint& operator=(ListenEndpoint&&); 54 ListenEndpoint(const ListenEndpoint&) noexcept = delete; 55 ListenEndpoint& operator=(const ListenEndpoint&) noexcept = delete; 56 57 // Only one of these is ever set. 58 std::string sock_name; 59 base::ScopedSocketHandle sock_handle; 60 std::unique_ptr<ipc::Host> ipc_host; 61 }; 62 63 // Creates an instance of the service (business logic + UNIX socket transport). 64 // Exposed to: 65 // The code in the tracing client that will host the service e.g., traced. 66 // Implemented in: 67 // src/tracing/ipc/service/service_ipc_host_impl.cc 68 class PERFETTO_EXPORT_COMPONENT ServiceIPCHost { 69 public: 70 static std::unique_ptr<ServiceIPCHost> CreateInstance( 71 base::TaskRunner*, 72 TracingService::InitOpts = {}); 73 virtual ~ServiceIPCHost(); 74 75 // Start listening on the Producer & Consumer ports. Returns false in case of 76 // failure (e.g., something else is listening on |socket_name|). 77 virtual bool Start(std::list<ListenEndpoint> producer_sockets, 78 ListenEndpoint consumer_socket) = 0; 79 80 virtual TracingService* service() const = 0; 81 82 // The methods below are for API compatibility with other projects that use 83 // some of the old flavours of Start(), back in the days when we supported 84 // only one socket or fd. 85 86 // Like the above, but takes two file descriptors to already bound sockets. 87 // This is used when building as part of the Android tree, where init opens 88 // and binds the socket beore exec()-ing us. 89 // Note: An internal Google project uses this (b/390202952). 90 bool Start(base::ScopedSocketHandle producer_socket_fd, 91 base::ScopedSocketHandle consumer_socket_fd); 92 93 // Allows callers to supply preconstructed Hosts. 94 bool Start(std::unique_ptr<ipc::Host> producer_host, 95 std::unique_ptr<ipc::Host> consumer_host); 96 97 // Used by tests. producer_socket_name can be a comma-separated list of N 98 // endpoints to listen onto. 99 bool Start(const char* producer_socket_names, 100 const char* consumer_socket_name); 101 102 protected: 103 ServiceIPCHost(); 104 105 private: 106 ServiceIPCHost(const ServiceIPCHost&) = delete; 107 ServiceIPCHost& operator=(const ServiceIPCHost&) = delete; 108 }; 109 110 } // namespace perfetto 111 112 #endif // INCLUDE_PERFETTO_EXT_TRACING_IPC_SERVICE_IPC_HOST_H_ 113