1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_IPC_SERVICES_DBINDER_DBINDER_SERVICE_H 17 #define OHOS_IPC_SERVICES_DBINDER_DBINDER_SERVICE_H 18 19 #include <string> 20 #include <map> 21 #include <mutex> 22 #include <shared_mutex> 23 #include <memory> 24 #include <list> 25 #include <thread> 26 27 #include "dbinder_service_stub.h" 28 #include "ipc_object_proxy.h" 29 #include "ipc_object_stub.h" 30 #include "rpc_system_ability_callback.h" 31 32 namespace OHOS { 33 class DBinderRemoteListener; 34 35 constexpr int DEVICEID_LENGTH = 64; 36 constexpr int SERVICENAME_LENGTH = 200; 37 constexpr int VERSION_NUM = 1; 38 constexpr int ENCRYPT_HEAD_LEN = 28; 39 constexpr int ENCRYPT_LENGTH = 4; 40 41 struct DeviceIdInfo { 42 uint16_t afType; 43 uint16_t reserved; 44 char fromDeviceId[DEVICEID_LENGTH + 1]; 45 char toDeviceId[DEVICEID_LENGTH + 1]; 46 }; 47 48 struct DHandleEntryHead { 49 uint32_t len; 50 uint32_t version; 51 }; 52 53 struct DHandleEntryTxRx { 54 struct DHandleEntryHead head; 55 uint32_t transType; 56 uint32_t dBinderCode; 57 uint16_t fromPort; 58 uint16_t toPort; 59 uint64_t stubIndex; 60 uint32_t seqNumber; 61 binder_uintptr_t binderObject; 62 struct DeviceIdInfo deviceIdInfo; 63 binder_uintptr_t stub; 64 uint16_t serviceNameLength; 65 char serviceName[SERVICENAME_LENGTH + 1]; 66 uint32_t pid; 67 uint32_t uid; 68 }; 69 70 struct SessionInfo { 71 uint32_t type; 72 uint16_t toPort; 73 uint16_t fromPort; 74 uint64_t stubIndex; 75 uint32_t socketFd; 76 std::string serviceName; 77 struct DeviceIdInfo deviceIdInfo; 78 }; 79 80 enum DBinderCode { 81 MESSAGE_AS_INVOKER = 1, 82 MESSAGE_AS_REPLY = 2, 83 MESSAGE_AS_OBITUARY = 3, 84 }; 85 86 enum AfType { 87 IPV4_TYPE = 1, 88 IPV6_TYPE = 2, 89 DATABBUS_TYPE = 3, 90 }; 91 92 struct ThreadLockInfo { 93 std::mutex mutex; 94 std::condition_variable condition; 95 bool ready = false; 96 }; 97 98 class DBinderService : public virtual RefBase { 99 public: 100 DBinderService(); 101 virtual ~DBinderService(); 102 public: 103 static sptr<DBinderService> GetInstance(); 104 bool StartDBinderService(std::shared_ptr<RpcSystemAbilityCallback> &callbackImpl); 105 sptr<DBinderServiceStub> MakeRemoteBinder(const std::u16string &serviceName, 106 const std::string &deviceID, binder_uintptr_t binderObject, uint64_t pid = 0); 107 bool RegisterRemoteProxy(std::u16string serviceName, sptr<IRemoteObject> binderObject); 108 bool RegisterRemoteProxy(std::u16string serviceName, int32_t systemAbilityId); 109 bool OnRemoteMessageTask(const struct DHandleEntryTxRx *message); 110 std::shared_ptr<struct SessionInfo> QuerySessionObject(binder_uintptr_t stub); 111 bool DetachDeathRecipient(sptr<IRemoteObject> object); 112 bool AttachDeathRecipient(sptr<IRemoteObject> object, sptr<IRemoteObject::DeathRecipient> deathRecipient); 113 sptr<IRemoteObject::DeathRecipient> QueryDeathRecipient(sptr<IRemoteObject> object); 114 bool DetachCallbackProxy(sptr<IRemoteObject> object); 115 bool AttachCallbackProxy(sptr<IRemoteObject> object, DBinderServiceStub *dbStub); 116 int32_t NoticeServiceDie(const std::u16string &serviceName, const std::string &deviceID); 117 int32_t NoticeDeviceDie(const std::string &deviceID); 118 bool DetachBusNameObject(IPCObjectProxy *proxy); 119 std::string CreateDatabusName(int uid, int pid); 120 bool DetachProxyObject(binder_uintptr_t binderObject); 121 122 private: 123 static std::shared_ptr<DBinderRemoteListener> GetRemoteListener(); 124 static bool StartRemoteListener(); 125 static void StopRemoteListener(); 126 static std::string ConvertToSecureDeviceID(const std::string &deviceID); 127 std::u16string GetRegisterService(binder_uintptr_t binderObject); 128 bool InvokerRemoteDBinder(const sptr<DBinderServiceStub> stub, uint32_t seqNumber); 129 bool OnRemoteReplyMessage(const struct DHandleEntryTxRx *replyMessage); 130 void MakeSessionByReplyMessage(const struct DHandleEntryTxRx *replyMessage); 131 bool OnRemoteInvokerMessage(const struct DHandleEntryTxRx *message); 132 void WakeupThreadByStub(uint32_t seqNumber); 133 void DetachThreadLockInfo(uint32_t seqNumber); 134 bool AttachThreadLockInfo(uint32_t seqNumber, std::shared_ptr<struct ThreadLockInfo> object); 135 std::shared_ptr<struct ThreadLockInfo> QueryThreadLockInfo(uint32_t seqNumber); 136 bool AttachProxyObject(sptr<IRemoteObject> object, binder_uintptr_t binderObject); 137 sptr<IRemoteObject> QueryProxyObject(binder_uintptr_t binderObject); 138 bool DetachSessionObject(binder_uintptr_t stub); 139 bool AttachSessionObject(std::shared_ptr<struct SessionInfo> object, binder_uintptr_t stub); 140 sptr<IRemoteObject> FindOrNewProxy(binder_uintptr_t binderObject, int32_t systemAbilityId); 141 bool SendEntryToRemote(const sptr<DBinderServiceStub> stub, uint32_t seqNumber); 142 uint16_t AllocFreeSocketPort(); 143 std::string GetLocalDeviceID(); 144 bool CheckBinderObject(const sptr<DBinderServiceStub> &stub, binder_uintptr_t binderObject); 145 bool HasDBinderStub(binder_uintptr_t binderObject); 146 bool IsSameStubObject(const sptr<DBinderServiceStub> &stub, const std::u16string &service, 147 const std::string &device); 148 sptr<DBinderServiceStub> FindDBinderStub(const std::u16string &service, const std::string &device); 149 bool DeleteDBinderStub(const std::u16string &service, const std::string &device); 150 sptr<DBinderServiceStub> FindOrNewDBinderStub(const std::u16string &service, 151 const std::string &device, binder_uintptr_t binderObject); 152 void ProcessCallbackProxy(sptr<DBinderServiceStub> dbStub); 153 bool NoticeCallbackProxy(sptr<DBinderServiceStub> dbStub); 154 std::list<std::u16string> FindServicesByDeviceID(const std::string &deviceID); 155 int32_t NoticeServiceDieInner(const std::u16string &serviceName, const std::string &deviceID); 156 uint32_t GetRemoteTransType(); 157 bool OnRemoteInvokerDataBusMessage(IPCObjectProxy *proxy, struct DHandleEntryTxRx *replyMessage, 158 std::string &remoteDeviceId, int pid, int uid); 159 bool IsDeviceIdIllegal(const std::string &deviceID); 160 bool AttachBusNameObject(IPCObjectProxy *proxy, const std::string &name); 161 std::string QueryBusNameObject(IPCObjectProxy *proxy); 162 std::string GetDatabusNameByProxy(IPCObjectProxy *proxy); 163 uint32_t GetSeqNumber(); 164 bool RegisterRemoteProxyInner(std::u16string serviceName, binder_uintptr_t binder); 165 bool CheckSystemAbilityId(int32_t systemAbilityId); 166 167 private: 168 DISALLOW_COPY_AND_MOVE(DBinderService); 169 static std::mutex instanceMutex_; 170 static constexpr int WAIT_FOR_REPLY_MAX_SEC = 4; 171 static constexpr int RETRY_TIMES = 2; 172 static std::shared_ptr<DBinderRemoteListener> remoteListener_; 173 static bool mainThreadCreated_; 174 static sptr<DBinderService> instance_; 175 176 std::shared_mutex remoteBinderMutex_; 177 std::shared_mutex busNameMutex_; 178 std::shared_mutex proxyMutex_; 179 std::shared_mutex deathRecipientMutex_; 180 std::shared_mutex sessionMutex_; 181 182 std::mutex handleEntryMutex_; 183 std::mutex threadLockMutex_; 184 std::mutex callbackProxyMutex_; 185 std::mutex deathNotificationMutex_; 186 187 uint32_t seqNumber_ = 0; /* indicate make remote binder message sequence number, and can be overflow */ 188 std::list<sptr<DBinderServiceStub>> DBinderStubRegisted_; 189 std::map<std::u16string, binder_uintptr_t> mapRemoteBinderObjects_; 190 std::map<uint32_t, std::shared_ptr<struct ThreadLockInfo>> threadLockInfo_; 191 std::map<int, sptr<IRemoteObject>> proxyObject_; 192 std::map<binder_uintptr_t, std::shared_ptr<struct SessionInfo>> sessionObject_; 193 std::map<sptr<IRemoteObject>, DBinderServiceStub *> noticeProxy_; 194 std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>> deathRecipients_; 195 std::map<IPCObjectProxy *, std::string> busNameObject_; 196 static constexpr int32_t FIRST_SYS_ABILITY_ID = 0x00000001; 197 static constexpr int32_t LAST_SYS_ABILITY_ID = 0x00ffffff; 198 199 std::shared_ptr<RpcSystemAbilityCallback> dbinderCallback_; 200 }; 201 } // namespace OHOS 202 #endif // OHOS_IPC_SERVICES_DBINDER_DBINDER_SERVICE_H 203