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