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 SRC_IPC_HOST_IMPL_H_ 18 #define SRC_IPC_HOST_IMPL_H_ 19 20 #include <map> 21 #include <set> 22 #include <string> 23 #include <vector> 24 25 #include "perfetto/base/task_runner.h" 26 #include "perfetto/ext/base/scoped_file.h" 27 #include "perfetto/ext/base/sys_types.h" 28 #include "perfetto/ext/base/thread_checker.h" 29 #include "perfetto/ext/base/unix_socket.h" 30 #include "perfetto/ext/ipc/deferred.h" 31 #include "perfetto/ext/ipc/host.h" 32 #include "src/ipc/buffered_frame_deserializer.h" 33 34 namespace perfetto { 35 namespace ipc { 36 37 constexpr uint32_t kDefaultIpcTxTimeoutMs = 10000; 38 39 class HostImpl : public Host, public base::UnixSocket::EventListener { 40 public: 41 HostImpl(const char* socket_name, base::TaskRunner*); 42 HostImpl(base::ScopedSocketHandle, base::TaskRunner*); 43 HostImpl(base::TaskRunner* task_runner); 44 ~HostImpl() override; 45 46 // Host implementation. 47 bool ExposeService(std::unique_ptr<Service>) override; 48 void AdoptConnectedSocket_Fuchsia( 49 base::ScopedSocketHandle, 50 std::function<bool(int)> send_fd_cb) override; 51 void SetSocketSendTimeoutMs(uint32_t timeout_ms) override; 52 53 // base::UnixSocket::EventListener implementation. 54 void OnNewIncomingConnection(base::UnixSocket*, 55 std::unique_ptr<base::UnixSocket>) override; 56 void OnDisconnect(base::UnixSocket*) override; 57 void OnDataAvailable(base::UnixSocket*) override; 58 sock()59 const base::UnixSocket* sock() const { return sock_.get(); } 60 61 private: 62 // Owns the per-client receive buffer (BufferedFrameDeserializer). 63 struct ClientConnection { 64 ~ClientConnection(); 65 ClientID id; 66 std::unique_ptr<base::UnixSocket> sock; 67 BufferedFrameDeserializer frame_deserializer; 68 base::ScopedFile received_fd; 69 std::function<bool(int)> send_fd_cb_fuchsia; 70 // Peer identity set using IPCFrame sent by the client. These 3 fields 71 // should be used only for non-AF_UNIX connections AF_UNIX connections 72 // should only rely on the peer identity obtained from the socket. 73 uid_t uid_override = base::kInvalidUid; 74 pid_t pid_override = base::kInvalidPid; 75 76 // |machine_id| is mapped from machine_id_hint (or socket hostname if 77 // |the client doesn't support machine_id_hint). 78 base::MachineID machine_id = base::kDefaultMachineID; 79 80 pid_t GetLinuxPeerPid() const; 81 uid_t GetPosixPeerUid() const; GetMachineIDClientConnection82 base::MachineID GetMachineID() const { return machine_id; } 83 }; 84 struct ExposedService { 85 ExposedService(ServiceID, const std::string&, std::unique_ptr<Service>); 86 ~ExposedService(); 87 ExposedService(ExposedService&&) noexcept; 88 ExposedService& operator=(ExposedService&&); 89 90 ServiceID id; 91 std::string name; 92 std::unique_ptr<Service> instance; 93 }; 94 95 HostImpl(const HostImpl&) = delete; 96 HostImpl& operator=(const HostImpl&) = delete; 97 98 bool Initialize(const char* socket_name); 99 void OnReceivedFrame(ClientConnection*, const Frame&); 100 void OnBindService(ClientConnection*, const Frame&); 101 void OnInvokeMethod(ClientConnection*, const Frame&); 102 void OnSetPeerIdentity(ClientConnection*, const Frame&); 103 104 void ReplyToMethodInvocation(ClientID, RequestID, AsyncResult<ProtoMessage>); 105 const ExposedService* GetServiceByName(const std::string&); 106 107 static void SendFrame(ClientConnection*, const Frame&, int fd = -1); 108 109 base::TaskRunner* const task_runner_; 110 std::map<ServiceID, ExposedService> services_; 111 std::unique_ptr<base::UnixSocket> sock_; // The listening socket. 112 std::map<ClientID, std::unique_ptr<ClientConnection>> clients_; 113 std::map<base::UnixSocket*, ClientConnection*> clients_by_socket_; 114 ServiceID last_service_id_ = 0; 115 ClientID last_client_id_ = 0; 116 uint32_t socket_tx_timeout_ms_ = kDefaultIpcTxTimeoutMs; 117 PERFETTO_THREAD_CHECKER(thread_checker_) 118 base::WeakPtrFactory<HostImpl> weak_ptr_factory_; // Keep last. 119 }; 120 121 } // namespace ipc 122 } // namespace perfetto 123 124 #endif // SRC_IPC_HOST_IMPL_H_ 125