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 bool SetMaxWorkThread(int maxThreadNum) override; 53 54 void JoinThread(bool initiative) override; 55 56 void JoinProcessThread(bool initiative) override; 57 58 void FreeBuffer(void *data) override; 59 60 void StopWorkThread() override; 61 62 bool SetRegistryObject(sptr<IRemoteObject> &object) override; 63 64 int SendRequest(int handle, uint32_t code, MessageParcel &data, MessageParcel &reply, 65 MessageOption &option) override; 66 67 int SendReply(MessageParcel &reply, uint32_t flags, int32_t result) override; 68 69 bool FlattenObject(Parcel &parcel, const IRemoteObject *object) const override; 70 71 sptr<IRemoteObject> UnflattenObject(Parcel &parcel) override; 72 73 int ReadFileDescriptor(Parcel &parcel) override; 74 75 bool WriteFileDescriptor(Parcel &parcel, int fd, bool takeOwnership) override; 76 77 pid_t GetCallerPid() const override; 78 79 uid_t GetCallerUid() const override; 80 81 uint64_t GetCallerTokenID() const override; 82 83 uint64_t GetFirstCallerTokenID() const override; 84 85 uint64_t GetSelfTokenID() const override; 86 87 uint64_t GetSelfFirstCallerTokenID() const override; 88 89 uint32_t GetStatus() const override; 90 91 bool IsLocalCalling() override; 92 93 void SetStatus(uint32_t status); 94 95 std::string GetLocalDeviceID() override; 96 97 std::string GetCallerDeviceID() const override; 98 99 int FlushCommands(IRemoteObject *object) override; 100 101 std::string ResetCallingIdentity() override; 102 103 bool SetCallingIdentity(std::string &identity) override; 104 105 void ExitCurrentThread(); 106 107 #ifndef CONFIG_IPC_SINGLE 108 int TranslateIRemoteObject(int32_t cmd, const sptr<IRemoteObject> &obj) 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 uint64_t callerTokenID_; 119 uint64_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 bool TranslateDBinderProxy(int handle, MessageParcel &data); 150 151 void GetAccessToken(uint64_t &callerTokenID, uint64_t &firstTokenID); 152 153 private: 154 DISALLOW_COPY_AND_MOVE(BinderInvoker); 155 static constexpr int IPC_DEFAULT_PARCEL_SIZE = 256; 156 static constexpr int IPC_CMD_PROCESS_WARN_TIME = 500; 157 static constexpr int ACCESS_TOKEN_MAX_LEN = 10; 158 Parcel input_; 159 Parcel output_; 160 BinderConnector *binderConnector_; 161 uint32_t status_; 162 static inline InvokerDelegator<BinderInvoker> delegator_ = { IRemoteObject::IF_PROT_BINDER }; 163 }; 164 #ifdef CONFIG_IPC_SINGLE 165 } // namespace IPC_SINGLE 166 #endif 167 } // namespace OHOS 168 #endif // OHOS_IPC_BINDER_INVOKER_H 169