• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_IPC_PROCESS_SKELETON_H
17 #define OHOS_IPC_IPC_PROCESS_SKELETON_H
18 
19 #include <list>
20 #include <map>
21 #include <shared_mutex>
22 
23 #include "invoker_rawdata.h"
24 #include "ipc_object_proxy.h"
25 #include "ipc_object_stub.h"
26 #include "ipc_thread_pool.h"
27 #include "iremote_object.h"
28 #include "nocopyable.h"
29 #include "refbase.h"
30 #include "sys_binder.h"
31 
32 #ifndef CONFIG_IPC_SINGLE
33 #include "comm_auth_info.h"
34 #include "dbinder_callback_stub.h"
35 #include "dbinder_session_object.h"
36 #include "rpc_feature_set.h"
37 #include "ISessionService.h"
38 #include "Session.h"
39 #include "stub_refcount_object.h"
40 
41 using Communication::SoftBus::ISessionService;
42 using Communication::SoftBus::Session;
43 #endif
44 
45 namespace OHOS {
46 #ifdef CONFIG_IPC_SINGLE
47 namespace IPC_SINGLE {
48 #endif
49 
50 #ifndef CONFIG_IPC_SINGLE
51 struct SocketThreadLockInfo {
52     std::mutex mutex;
53     std::condition_variable condition;
54     bool ready = false;
55 };
56 
57 struct ThreadMessageInfo {
58     std::thread::id threadId;
59     uint32_t flags;
60     binder_size_t bufferSize;
61     binder_size_t offsetsSize;
62     binder_uintptr_t offsets;
63     uint32_t socketId;
64     void *buffer;
65 };
66 
67 struct ThreadProcessInfo {
68     uint32_t listenFd;
69     uint32_t packageSize;
70     std::shared_ptr<char> buffer;
71 };
72 #endif
73 
74 class IPCProcessSkeleton : public virtual RefBase {
75 public:
76     enum {
77         LISTEN_THREAD_CREATE_OK, // Invoker family.
78         LISTEN_THREAD_CREATE_FAILED,
79         LISTEN_THREAD_CREATED_ALREADY,
80         LISTEN_THREAD_CREATED_TIMEOUT
81     };
82     ~IPCProcessSkeleton() override;
83 
84     static IPCProcessSkeleton *GetCurrent();
85 
86 #ifndef CONFIG_IPC_SINGLE
87     static uint32_t ConvertChannelID2Int(int64_t databusChannelId);
88     static bool IsHandleMadeByUser(uint32_t handle);
89 #endif
90     bool SetMaxWorkThread(int maxThreadNum);
91 
92     sptr<IRemoteObject> GetRegistryObject();
93 
94     bool SpawnThread(int policy = IPCWorkThread::SPAWN_PASSIVE, int proto = IRemoteObject::IF_PROT_DEFAULT);
95 
96     std::u16string MakeHandleDescriptor(int handle);
97 
98     sptr<IRemoteObject> FindOrNewObject(int handle);
99     bool IsContainsObject(IRemoteObject *object);
100     IRemoteObject *QueryObject(const std::u16string &descriptor);
101     IRemoteObject *QueryObjectInner(const std::u16string &descriptor);
102     bool AttachObject(IRemoteObject *object);
103     bool AttachObjectInner(IRemoteObject *object);
104     bool DetachObject(IRemoteObject *object);
105     bool DetachObjectInner(IRemoteObject *object);
106 
107     bool OnThreadTerminated(const std::string &threadName);
108 
109     bool SetRegistryObject(sptr<IRemoteObject> &object);
110     bool AttachRawData(uint32_t fd, std::shared_ptr<InvokerRawData> rawData);
111     bool DetachRawData(uint32_t fd);
112     std::shared_ptr<InvokerRawData> QueryRawData(uint32_t fd);
113 
114 #ifndef CONFIG_IPC_SINGLE
115     sptr<IRemoteObject> GetSAMgrObject();
116     bool ProxyDetachDBinderSession(uint32_t handle);
117     bool ProxyAttachDBinderSession(uint32_t handle, std::shared_ptr<DBinderSessionObject> object);
118     std::shared_ptr<DBinderSessionObject> ProxyQueryDBinderSession(uint32_t handle);
119     bool QueryProxyBySessionHandle(uint32_t handle, std::vector<uint32_t> &proxyHandle);
120     std::shared_ptr<DBinderSessionObject> QuerySessionByInfo(const std::string &name, const std::string &deviceId);
121 
122     bool DetachThreadLockInfo(const std::thread::id &threadId);
123     bool AttachThreadLockInfo(std::shared_ptr<SocketThreadLockInfo> object, const std::thread::id &threadId);
124     std::shared_ptr<SocketThreadLockInfo> QueryThreadLockInfo(const std::thread::id &threadId);
125     void EraseThreadBySeqNumber(uint64_t seqNumber);
126     bool AddThreadBySeqNumber(uint64_t seqNumber, std::shared_ptr<ThreadMessageInfo> messageInfo);
127     std::shared_ptr<ThreadMessageInfo> QueryThreadBySeqNumber(uint64_t seqNumber);
128     bool AddSendThreadInWait(uint64_t seqNumber, std::shared_ptr<ThreadMessageInfo> messageInfo, int userWaitTime);
129 
130     std::thread::id GetIdleSocketThread();
131     int GetSocketIdleThreadNum() const;
132     int GetSocketTotalThreadNum() const;
133     int PopSocketIdFromThread(const std::thread::id &threadId);
134     void WakeUpSocketIOThread(const std::thread::id &threadID);
135     void WakeUpThreadBySeqNumber(uint64_t seqNumber, uint32_t handle);
136     IRemoteObject *QueryStubByIndex(uint64_t stubIndex);
137     uint64_t AddStubByIndex(IRemoteObject *stubObject);
138     uint64_t GetSeqNumber();
139     uint32_t GetDBinderIdleHandle(uint64_t stubIndex);
140     std::shared_ptr<SocketThreadLockInfo> GetListenThreadLockInfo();
141     std::string GetLocalDeviceID();
142 
143     bool AttachCallbackStub(IPCObjectProxy *ipcProxy, sptr<IPCObjectStub> callbackStub);
144     bool DetachCallbackStub(IPCObjectStub *callbackStub);
145     sptr<IPCObjectStub> QueryCallbackStub(IPCObjectProxy *ipcProxy);
146     IPCObjectProxy *QueryCallbackProxy(IPCObjectStub *callbackStub);
147     bool DetachCallbackStubByProxy(IPCObjectProxy *ipcProxy);
148     uint32_t QueryHandleByDatabusSession(const std::string &name, const std::string &deviceId, uint64_t stubIndex);
149     bool StubDetachDBinderSession(uint32_t handle);
150     std::shared_ptr<DBinderSessionObject> StubQueryDBinderSession(uint32_t handle);
151     bool StubAttachDBinderSession(uint32_t handle, std::shared_ptr<DBinderSessionObject> object);
152     std::string GetDatabusName();
153     bool CreateSoftbusServer(const std::string &name);
154     bool DetachHandleToIndex(uint32_t handle);
155     bool AttachHandleToIndex(uint32_t handle, uint64_t stubIndex);
156     uint64_t QueryHandleToIndex(uint32_t handle);
157     uint64_t QueryHandleToIndex(std::list<uint32_t> &handleList, uint32_t &handle);
158     bool AttachStubRecvRefInfo(IRemoteObject *stub, int pid, const std::string &deviceId);
159     void DetachStubRecvRefInfo(int pid, const std::string &deviceId);
160     bool DetachStubRecvRefInfo(const IRemoteObject *stub, int pid, const std::string &deviceId);
161     void DetachStubRecvRefInfo(const IRemoteObject *stub);
162     std::list<IRemoteObject *> QueryStubRecvRefInfo(int pid, const std::string &deviceId);
163     void DetachStubRefInfo(const int pid, const std::string &deviceId);
164     void DetachStubRefInfo(IRemoteObject *stub, int pid, const std::string &deviceId);
165     bool AttachStubSendRefInfo(IRemoteObject *stub, int pid, const std::string &deviceId);
166     void DetachStubSendRefInfo(int pid, const std::string &deviceId);
167     void DetachStubSendRefInfo(IRemoteObject *stub, int pid, const std::string &deviceId);
168     void DetachStubSendRefInfo(IRemoteObject *stub);
169     bool IncStubRefTimes(IRemoteObject *stub);
170     bool DecStubRefTimes(IRemoteObject *stub);
171     bool DetachStubRefTimes(IRemoteObject *stub);
172 
173     bool AttachCommAuthInfo(IRemoteObject *stub, int pid, int uid, const std::string &deviceId,
174         std::shared_ptr<FeatureSetData> featureSet);
175     void DetachCommAuthInfo(IRemoteObject *stub, int pid, int uid, const std::string &deviceId);
176     void DetachCommAuthInfoByStub(IRemoteObject *stub);
177     std::shared_ptr<FeatureSetData> QueryIsAuth(int pid, int uid, const std::string &deviceId);
178     bool AddDataThreadToIdle(const std::thread::id &threadId);
179     bool DeleteDataThreadFromIdle(const std::thread::id &threadId);
180     std::thread::id GetIdleDataThread();
181     void AddDataInfoToThread(const std::thread::id &threadId, std::shared_ptr<ThreadProcessInfo> processInfo);
182     std::shared_ptr<ThreadProcessInfo> PopDataInfoFromThread(const std::thread::id &threadId);
183     void WakeUpDataThread(const std::thread::id &threadID);
184     void AddDataThreadInWait(const std::thread::id &threadId);
185     bool IsSameRemoteObject(IRemoteObject *stub, int pid, int uid, const std::string &deviceId,
186         const std::shared_ptr<CommAuthInfo> &auth);
187     bool IsSameRemoteObject(int pid, int uid, const std::string &deviceId, const std::shared_ptr<CommAuthInfo> &auth);
188     uint64_t EraseStubIndex(IRemoteObject *stubObject);
189     bool DetachAppInfoToStubIndex(uint32_t pid, uint32_t uid, const std::string &deviceId, uint64_t stubIndex);
190     void DetachAppInfoToStubIndex(uint64_t stubIndex);
191     bool AttachAppInfoToStubIndex(uint32_t pid, uint32_t uid, const std::string &deviceId, uint64_t stubIndex);
192     bool QueryAppInfoToStubIndex(uint32_t pid, uint32_t uid, const std::string &deviceId, uint64_t stubIndex);
193     bool AttachDBinderCallbackStub(sptr<IRemoteObject> rpcProxy, sptr<DBinderCallbackStub> stub);
194     bool DetachDBinderCallbackStubByProxy(sptr<IRemoteObject> rpcProxy);
195     sptr<DBinderCallbackStub> QueryDBinderCallbackStub(sptr<IRemoteObject> rpcProxy);
196     sptr<IRemoteObject> QueryDBinderCallbackProxy(sptr<IRemoteObject> stub);
197 #endif
198 
199 public:
200     static constexpr int DEFAULT_WORK_THREAD_NUM = 16;
201     static constexpr uint32_t DBINDER_HANDLE_BASE = 100000;
202     static constexpr uint32_t DBINDER_HANDLE_RANG = 100;
203 #ifndef CONFIG_IPC_SINGLE
204     std::shared_ptr<SocketThreadLockInfo> listenThreadReady_ = nullptr;
205     static constexpr int TRANS_TIME_INIT_VALUE = 1;
206     static constexpr int SEC_TO_MS = 1000;
207 #endif
208 private:
209     DISALLOW_COPY_AND_MOVE(IPCProcessSkeleton);
210     IPCProcessSkeleton();
211     static IPCProcessSkeleton *instance_;
212     static std::mutex procMutex_;
213     std::recursive_mutex mutex_;
214     std::shared_mutex rawDataMutex_;
215     std::map<std::u16string, wptr<IRemoteObject>> objects_;
216     std::map<IRemoteObject *, bool> isContainStub_;
217     std::map<uint32_t, std::shared_ptr<InvokerRawData>> rawData_;
218     IPCWorkThreadPool *threadPool_ = nullptr;
219     sptr<IRemoteObject> registryObject_ = nullptr;
220     friend void IPCObjectProxy::OnLastStrongRef(const void *objectId);
221 
222 #ifndef CONFIG_IPC_SINGLE
223     std::mutex databusProcMutex_;
224     std::mutex sessionNameMutex_;
225     std::mutex seqNumberMutex_;
226     std::mutex transTimesMutex_;
227     std::mutex stubSendRefMutex_;
228     std::mutex idleDataMutex_;
229     std::mutex dataQueueMutex_;
230     std::mutex findThreadMutex_;
231 
232     std::recursive_mutex proxyToSessionMutex_;
233 
234     std::shared_mutex databusSessionMutex_;
235     std::shared_mutex handleToIndexMutex_;
236     std::shared_mutex threadLockMutex_;
237     std::shared_mutex callbackStubMutex_;
238     std::shared_mutex stubObjectsMutex_;
239     std::shared_mutex stubRecvRefMutex_;
240     std::shared_mutex appInfoToIndexMutex_;
241     std::shared_mutex commAuthMutex_;
242     std::shared_mutex dbinderSentMutex_;
243 
244     std::map<uint64_t, std::shared_ptr<ThreadMessageInfo>> seqNumberToThread_;
245     std::map<uint64_t, IRemoteObject *> stubObjects_;
246     std::map<std::thread::id, std::shared_ptr<SocketThreadLockInfo>> threadLockInfo_;
247     std::map<uint32_t, std::shared_ptr<DBinderSessionObject>> proxyToSession_;
248     std::map<uint32_t, std::shared_ptr<DBinderSessionObject>> dbinderSessionObjects_;
249     std::map<uint32_t, uint64_t> handleToStubIndex_;
250     std::map<IPCObjectProxy *, sptr<IPCObjectStub>> noticeStub_;
251     std::map<IRemoteObject *, uint32_t> transTimes_;
252     std::map<std::thread::id, std::vector<std::shared_ptr<ThreadProcessInfo>>> dataInfoQueue_; // key is threadId
253     std::map<std::string, std::map<uint64_t, bool>> appInfoToStubIndex_;
254     std::map<sptr<IRemoteObject>, sptr<DBinderCallbackStub>> dbinderSentCallback;
255 
256     std::list<std::thread::id> idleDataThreads_;
257     std::list<std::shared_ptr<StubRefCountObject>> stubRecvRefs_;
258     std::list<std::shared_ptr<StubRefCountObject>> stubSendRefs_;
259     std::list<std::shared_ptr<CommAuthInfo>> commAuth_;
260 
261     uint32_t dBinderHandle_ = DBINDER_HANDLE_BASE; /* dbinder handle start at 100000 */
262     uint64_t seqNumber_ = 0;
263     std::string sessionName_ = std::string("");
264     uint64_t randNum_;
265 #endif
266 };
267 #ifdef CONFIG_IPC_SINGLE
268 } // namespace IPC_SINGLE
269 #endif
270 } // namespace OHOS
271 #endif // OHOS_IPC_IPC_PROCESS_SKELETON_H
272