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_BINDER_INVOKER_H 17 #define OHOS_IPC_BINDER_INVOKER_H 18 19 #include <unistd.h> 20 #include <sys/types.h> 21 #include "binder_connector.h" 22 #include "iremote_invoker.h" 23 #include "invoker_factory.h" 24 25 namespace OHOS { 26 #ifdef CONFIG_IPC_SINGLE 27 namespace IPC_SINGLE { 28 #endif 29 30 class BinderInvoker : public IRemoteInvoker { 31 public: 32 class BinderAllocator : public DefaultAllocator { 33 void Dealloc(void *data) override; 34 35 friend BinderInvoker; 36 }; 37 38 BinderInvoker(); 39 40 ~BinderInvoker() = default; 41 42 bool AcquireHandle(int32_t handle) override; 43 44 bool ReleaseHandle(int32_t handle) override; 45 46 bool PingService(int32_t handle) override; 47 48 bool AddDeathRecipient(int32_t handle, void *cookie) override; 49 50 bool RemoveDeathRecipient(int32_t handle, void *cookie) override; 51 52 int GetObjectRefCount(const IRemoteObject *object) override; 53 54 bool SetMaxWorkThread(int maxThreadNum) override; 55 56 void JoinThread(bool initiative) override; 57 58 void JoinProcessThread(bool initiative) override; 59 60 void FreeBuffer(void *data) override; 61 62 void StopWorkThread() override; 63 64 bool SetRegistryObject(sptr<IRemoteObject> &object) override; 65 66 int SendRequest(int handle, uint32_t code, MessageParcel &data, MessageParcel &reply, 67 MessageOption &option) override; 68 69 int SendReply(MessageParcel &reply, uint32_t flags, int32_t result) override; 70 71 bool FlattenObject(Parcel &parcel, const IRemoteObject *object) const override; 72 73 sptr<IRemoteObject> UnflattenObject(Parcel &parcel) override; 74 75 int ReadFileDescriptor(Parcel &parcel) override; 76 77 bool WriteFileDescriptor(Parcel &parcel, int fd, bool takeOwnership) override; 78 79 pid_t GetCallerPid() const override; 80 81 uid_t GetCallerUid() const override; 82 83 uint32_t GetCallerTokenID() const override; 84 85 uint32_t GetFirstTokenID() const override; 86 87 uint32_t GetStatus() const override; 88 89 bool IsLocalCalling() override; 90 91 void SetStatus(uint32_t status); 92 93 std::string GetLocalDeviceID() override; 94 95 std::string GetCallerDeviceID() const override; 96 97 int FlushCommands(IRemoteObject *object) override; 98 99 std::string ResetCallingIdentity() override; 100 101 bool SetCallingIdentity(std::string &identity) override; 102 103 void ExitCurrentThread(); 104 105 #ifndef CONFIG_IPC_SINGLE 106 int TranslateProxy(uint32_t handle, uint32_t flag) override; 107 108 int TranslateStub(binder_uintptr_t cookie, binder_uintptr_t ptr, uint32_t flag, int cmd) override; 109 110 sptr<IRemoteObject> GetSAMgrObject() override; 111 #endif 112 113 protected: 114 bool isMainWorkThread; 115 bool stopWorkThread; 116 pid_t callerPid_; 117 pid_t callerUid_; 118 uint32_t callerTokenID_; 119 uint32_t firstTokenID_; 120 121 private: 122 int TransactWithDriver(bool doRead = true); 123 124 bool WriteTransaction(int cmd, uint32_t flags, int32_t handle, uint32_t code, const MessageParcel &data, 125 const int *status); 126 127 int WaitForCompletion(MessageParcel *reply = nullptr, int32_t *acquireResult = nullptr); 128 129 void OnAttemptAcquire(); 130 131 void OnRemoveRecipientDone(); 132 133 void StartWorkLoop(); 134 135 void OnBinderDied(); 136 137 void OnAcquireObject(uint32_t cmd); 138 139 void OnReleaseObject(uint32_t cmd); 140 141 void OnTransaction(const uint8_t *); 142 143 int HandleCommands(uint32_t cmd); 144 145 int HandleCommandsInner(uint32_t cmd); 146 147 int HandleReply(MessageParcel *reply); 148 149 private: 150 DISALLOW_COPY_AND_MOVE(BinderInvoker); 151 static constexpr int IPC_DEFAULT_PARCEL_SIZE = 256; 152 static constexpr int IPC_CMD_PROCESS_WARN_TIME = 500; 153 Parcel input_; 154 Parcel output_; 155 BinderConnector *binderConnector_; 156 uint32_t status_; 157 static inline InvokerDelegator<BinderInvoker> delegator_ = { IRemoteObject::IF_PROT_BINDER }; 158 }; 159 #ifdef CONFIG_IPC_SINGLE 160 } // namespace IPC_SINGLE 161 #endif 162 } // namespace OHOS 163 #endif // OHOS_IPC_BINDER_INVOKER_H 164