1 /* 2 * Copyright (C) 2022-2024 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_PROCESS_SKELETON_H 17 #define OHOS_IPC_PROCESS_SKELETON_H 18 19 #include <atomic> 20 #include <map> 21 #include <mutex> 22 #include <shared_mutex> 23 #include <unordered_map> 24 25 #include "iremote_object.h" 26 #include "sys_binder.h" 27 28 namespace OHOS { 29 30 static constexpr size_t INT_STRING_MAX_LEN = 10; 31 32 struct InvokerProcInfo { 33 pid_t pid; 34 pid_t realPid; 35 pid_t uid; 36 uint64_t tokenId; 37 uint64_t firstTokenId; 38 std::string sid; 39 uint32_t invoker; 40 }; 41 42 class ProcessSkeleton { 43 public: 44 static std::string ConvertToSecureDesc(const std::string &str); 45 static bool IsPrint(int err, std::atomic<int> &lastErr, std::atomic<int> &lastErrCnt); 46 static uint32_t ConvertAddr(const void *ptr); 47 static ProcessSkeleton* GetInstance(); 48 static bool FlattenDBinderData(Parcel &parcel, const dbinder_negotiation_data *&dbinderData); 49 static bool UnFlattenDBinderData(Parcel &parcel, dbinder_negotiation_data *&dbinderData); 50 static bool GetSubStr(const std::string &str, std::string &substr, size_t offset, size_t length); 51 static bool IsNumStr(const std::string &str); 52 static bool StrToUint64(const std::string &str, uint64_t &value); 53 static bool StrToInt32(const std::string &str, int32_t &value); 54 55 bool SetIPCProxyLimit(uint64_t num, std::function<void(uint64_t num)> callback); 56 sptr<IRemoteObject> GetRegistryObject(); 57 void SetRegistryObject(sptr<IRemoteObject> &object); 58 void SetSamgrFlag(bool flag); 59 bool GetSamgrFlag(); 60 61 bool IsContainsObject(IRemoteObject *object); 62 sptr<IRemoteObject> QueryObject(const std::u16string &descriptor, bool lockFlag); 63 bool AttachObject(IRemoteObject *object, const std::u16string &descriptor, bool lockFlag); 64 bool DetachObject(IRemoteObject *object, const std::u16string &descriptor); 65 bool LockObjectMutex(); 66 bool UnlockObjectMutex(); 67 bool AttachValidObject(IRemoteObject *object, const std::u16string &desc); 68 bool DetachValidObject(IRemoteObject *object); 69 bool IsValidObject(IRemoteObject *object, std::u16string &desc); 70 bool AttachInvokerProcInfo(bool isLocal, InvokerProcInfo &invokeInfo); 71 bool QueryInvokerProcInfo(bool isLocal, InvokerProcInfo &invokeInfo); 72 bool DetachInvokerProcInfo(bool isLocal); 73 74 bool GetThreadStopFlag(); 75 void IncreaseThreadCount(); 76 void DecreaseThreadCount(); 77 void NotifyChildThreadStop(); 78 79 private: 80 DISALLOW_COPY_AND_MOVE(ProcessSkeleton); 81 ProcessSkeleton() = default; 82 ~ProcessSkeleton(); 83 84 class DestroyInstance { 85 public: ~DestroyInstance()86 ~DestroyInstance() 87 { 88 if (instance_ != nullptr) { 89 delete instance_; 90 instance_ = nullptr; 91 } 92 } 93 }; 94 95 static ProcessSkeleton* instance_; 96 static std::mutex mutex_; 97 static DestroyInstance destroyInstance_; 98 static std::atomic<bool> exitFlag_; 99 100 std::shared_mutex objMutex_; 101 sptr<IRemoteObject> registryObject_ = nullptr; 102 bool isSamgr_ = false; 103 104 std::unordered_map<std::u16string, wptr<IRemoteObject>> objects_; 105 std::unordered_map<IRemoteObject *, bool> isContainStub_; 106 107 std::shared_mutex validObjectMutex_; 108 std::unordered_map<IRemoteObject *, std::u16string> validObjectRecord_; 109 uint64_t ipcProxyLimitNum_ = 20000; // default maximun ipc proxy number 110 std::atomic<uint64_t> proxyObjectCountNum_ = 0; 111 std::function<void(uint64_t num)> ipcProxyCallback_{ nullptr }; 112 113 std::shared_mutex invokerProcMutex_; 114 std::unordered_map<std::string, InvokerProcInfo> invokerProcInfo_; 115 116 static constexpr size_t MAIN_THREAD_MAX_WAIT_TIME = 1; 117 std::atomic_bool stopThreadFlag_ = false; 118 std::mutex threadCountMutex_; 119 std::condition_variable threadCountCon_; 120 std::atomic_size_t runningChildThreadNum_ = 0; 121 }; 122 } // namespace OHOS 123 #endif // OHOS_IPC_PROCESS_SKELETON_H 124