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_IREMOTE_INVOKER_H 17 #define OHOS_IPC_IREMOTE_INVOKER_H 18 19 #include <unistd.h> 20 #include <sys/types.h> 21 #include "parcel.h" 22 #include "sys_binder.h" 23 #include "iremote_object.h" 24 #include "ipc_file_descriptor.h" 25 26 namespace OHOS { 27 #ifdef CONFIG_IPC_SINGLE 28 namespace IPC_SINGLE { 29 #endif 30 class IRemoteInvoker { 31 public: 32 enum { 33 IDLE_INVOKER, 34 ACTIVE_INVOKER, 35 }; 36 37 struct RemoteObjectSerializedInfo { 38 uint32_t type; 39 size_t size; 40 }; 41 42 virtual ~IRemoteInvoker() = default; 43 virtual bool AcquireHandle(int32_t handle) = 0; 44 45 virtual bool ReleaseHandle(int32_t handle) = 0; 46 47 virtual bool PingService(int32_t handle) = 0; 48 49 virtual int SendReply(MessageParcel &reply, uint32_t flags, int32_t result) = 0; 50 51 virtual int SendRequest(int handle, uint32_t code, MessageParcel &data, MessageParcel &reply, 52 MessageOption &option) = 0; 53 54 virtual bool AddDeathRecipient(int32_t handle, void *cookie) = 0; 55 56 virtual bool RemoveDeathRecipient(int32_t handle, void *cookie) = 0; 57 58 virtual bool SetMaxWorkThread(int maxThreadNum) = 0; 59 60 virtual void JoinThread(bool initiative) = 0; 61 62 virtual void JoinProcessThread(bool initiative) = 0; 63 64 virtual void StopWorkThread() = 0; 65 66 virtual void FreeBuffer(void *data) = 0; 67 68 virtual bool SetRegistryObject(sptr<IRemoteObject> &object) = 0; 69 70 virtual std::string GetCallerSid() const = 0; 71 72 virtual pid_t GetCallerPid() const = 0; 73 74 virtual pid_t GetCallerRealPid() const = 0; 75 76 virtual uid_t GetCallerUid() const = 0; 77 78 virtual uint64_t GetCallerTokenID() const = 0; 79 80 virtual uint64_t GetFirstCallerTokenID() const = 0; 81 82 virtual uint64_t GetSelfTokenID() const = 0; 83 84 virtual uint64_t GetSelfFirstCallerTokenID() const = 0; 85 86 virtual uint32_t GetStatus() = 0; 87 88 virtual bool IsLocalCalling() = 0; 89 90 virtual std::string GetLocalDeviceID() = 0; 91 92 virtual std::string GetCallerDeviceID() const = 0; 93 94 virtual bool FlattenObject(Parcel &parcel, const IRemoteObject *object) const = 0; 95 96 virtual sptr<IRemoteObject> UnflattenObject(Parcel &parcel) = 0; 97 98 virtual int ReadFileDescriptor(Parcel &parcel) = 0; 99 100 virtual bool WriteFileDescriptor(Parcel &parcel, int fd, bool takeOwnership) = 0; 101 102 virtual int FlushCommands(IRemoteObject *object) = 0; 103 104 virtual std::string ResetCallingIdentity() = 0; 105 106 virtual bool SetCallingIdentity(std::string &identity, bool flag) = 0; 107 108 virtual bool TriggerSystemIPCThreadReclaim() = 0; 109 110 virtual bool EnableIPCThreadReclaim(bool enable) = 0; 111 GetRemoteObjectSize(uint32_t objType)112 static inline size_t GetRemoteObjectSize(uint32_t objType) 113 { 114 size_t count = sizeof(remoteObjectInfo_) / sizeof(remoteObjectInfo_[0]); 115 for (size_t idx = 0; idx < count; ++idx) { 116 if (objType == remoteObjectInfo_[idx].type) { 117 return remoteObjectInfo_[idx].size; 118 } 119 } 120 return 0; 121 }; 122 123 #ifndef CONFIG_IPC_SINGLE 124 virtual sptr<IRemoteObject> GetSAMgrObject() = 0; 125 #endif 126 127 private: 128 static constexpr RemoteObjectSerializedInfo remoteObjectInfo_[] = { 129 { BINDER_TYPE_BINDER, sizeof(flat_binder_object) }, 130 { BINDER_TYPE_HANDLE, sizeof(flat_binder_object) }, 131 { BINDER_TYPE_FD, sizeof(binder_fd_object) }, 132 { BINDER_TYPE_PTR, sizeof(binder_buffer_object) }, 133 { BINDER_TYPE_REMOTE_HANDLE, sizeof(flat_binder_object) }, 134 { BINDER_TYPE_FDR, sizeof(binder_fd_object) }, 135 }; 136 }; 137 #ifdef CONFIG_IPC_SINGLE 138 } // namespace IPC_SINGLE 139 #endif 140 } // namespace OHOS 141 #endif // OHOS_IPC_IREMOTE_INVOKER_H 142