• 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 "ISessionService.h"
37 #include "Session.h"
38 #include "stub_refcount_object.h"
39 
40 using Communication::SoftBus::ISessionService;
41 using Communication::SoftBus::Session;
42 #endif
43 
44 namespace OHOS {
45 #ifdef CONFIG_IPC_SINGLE
46 namespace IPC_SINGLE {
47 #endif
48 #ifndef CONFIG_IPC_SINGLE
49 struct SocketThreadLockInfo {
50     std::mutex mutex;
51     std::condition_variable condition;
52     bool ready = false;
53 };
54 
55 struct ThreadMessageInfo {
56     uint32_t flags;
57     binder_size_t bufferSize;
58     binder_size_t offsetsSize;
59     binder_uintptr_t offsets;
60     uint32_t socketId;
61     void *buffer;
62     std::mutex mutex;
63     std::condition_variable condition;
64     bool ready;
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     static std::string ConvertToSecureString(const std::string &deviceId);
86 
87 #ifndef CONFIG_IPC_SINGLE
88     static uint32_t ConvertChannelID2Int(int64_t databusChannelId);
89     static bool IsHandleMadeByUser(uint32_t handle);
90 #endif
91     bool SetMaxWorkThread(int maxThreadNum);
92     std::u16string MakeHandleDescriptor(int handle);
93 
94     bool OnThreadTerminated(const std::string &threadName);
95     bool SpawnThread(int policy = IPCWorkThread::SPAWN_PASSIVE, int proto = IRemoteObject::IF_PROT_DEFAULT);
96 
97     sptr<IRemoteObject> FindOrNewObject(int handle);
98     bool IsContainsObject(IRemoteObject *object);
99     sptr<IRemoteObject> QueryObject(const std::u16string &descriptor);
100     bool AttachObject(IRemoteObject *object);
101     bool DetachObject(IRemoteObject *object);
102 
103     sptr<IRemoteObject> GetRegistryObject();
104     bool SetRegistryObject(sptr<IRemoteObject> &object);
105     void BlockUntilThreadAvailable();
106 
107 #ifndef CONFIG_IPC_SINGLE
108     bool AttachRawData(uint32_t fd, std::shared_ptr<InvokerRawData> rawData);
109     bool DetachRawData(uint32_t fd);
110     std::shared_ptr<InvokerRawData> QueryRawData(uint32_t fd);
111 
112     sptr<IRemoteObject> GetSAMgrObject();
113     std::shared_ptr<DBinderSessionObject> ProxyDetachDBinderSession(uint32_t handle, IPCObjectProxy *proxy);
114     bool ProxyAttachDBinderSession(uint32_t handle, std::shared_ptr<DBinderSessionObject> object);
115     std::shared_ptr<DBinderSessionObject> ProxyQueryDBinderSession(uint32_t handle);
116     bool ProxyMoveDBinderSession(uint32_t handle, IPCObjectProxy *proxy);
117     bool QueryProxyBySessionHandle(uint32_t handle, std::vector<uint32_t> &proxyHandle);
118     std::shared_ptr<DBinderSessionObject> QuerySessionByInfo(const std::string &name, const std::string &deviceId);
119 
120     bool DetachThreadLockInfo(const std::thread::id &threadId);
121     bool AttachThreadLockInfo(std::shared_ptr<SocketThreadLockInfo> object, const std::thread::id &threadId);
122     std::shared_ptr<SocketThreadLockInfo> QueryThreadLockInfo(const std::thread::id &threadId);
123     void EraseThreadBySeqNumber(uint64_t seqNumber);
124     bool AddThreadBySeqNumber(uint64_t seqNumber, std::shared_ptr<ThreadMessageInfo> messageInfo);
125     std::shared_ptr<ThreadMessageInfo> QueryThreadBySeqNumber(uint64_t seqNumber);
126     bool AddSendThreadInWait(uint64_t seqNumber, std::shared_ptr<ThreadMessageInfo> messageInfo, int userWaitTime);
127 
128     int GetSocketIdleThreadNum() const;
129     int GetSocketTotalThreadNum() const;
130     int PopSocketIdFromThread(const std::thread::id &threadId);
131     void WakeUpSocketIOThread(const std::thread::id &threadID);
132     void WakeUpThreadBySeqNumber(uint64_t seqNumber, uint32_t handle);
133     IRemoteObject *QueryStubByIndex(uint64_t stubIndex);
134     uint64_t QueryStubIndex(IRemoteObject *stubObject);
135     uint64_t AddStubByIndex(IRemoteObject *stubObject);
136     uint64_t EraseStubIndex(IRemoteObject *stubObject);
137     uint64_t GetSeqNumber();
138     uint32_t GetDBinderIdleHandle(std::shared_ptr<DBinderSessionObject> session);
139     std::string GetLocalDeviceID();
140 
141     bool AttachCallbackStub(IPCObjectProxy *ipcProxy, sptr<IPCObjectStub> callbackStub);
142     sptr<IPCObjectStub> QueryCallbackStub(IPCObjectProxy *ipcProxy);
143     sptr<IPCObjectProxy> QueryCallbackProxy(IPCObjectStub *callbackStub);
144     sptr<IPCObjectStub> DetachCallbackStub(IPCObjectProxy *ipcProxy);
145     uint32_t QueryHandleByDatabusSession(const std::string &name, const std::string &deviceId, uint64_t stubIndex);
146     bool StubDetachDBinderSession(uint32_t handle, uint32_t &tokenId);
147     std::shared_ptr<DBinderSessionObject> StubQueryDBinderSession(uint32_t handle);
148     bool StubAttachDBinderSession(uint32_t handle, std::shared_ptr<DBinderSessionObject> object);
149     std::string GetDatabusName();
150     bool CreateSoftbusServer(const std::string &name);
151 
152     bool AttachCommAuthInfo(IRemoteObject *stub, int pid, int uid, uint32_t tokenId, const std::string &deviceId);
153     bool DetachCommAuthInfo(IRemoteObject *stub, int pid, int uid, uint32_t tokenId, const std::string &deviceId);
154     void DetachCommAuthInfoByStub(IRemoteObject *stub);
155     bool QueryCommAuthInfo(int pid, int uid, uint32_t &tokenId, const std::string &deviceId);
156     bool AddDataThreadToIdle(const std::thread::id &threadId);
157     bool DeleteDataThreadFromIdle(const std::thread::id &threadId);
158     std::thread::id GetIdleDataThread();
159     void AddDataInfoToThread(const std::thread::id &threadId, std::shared_ptr<ThreadProcessInfo> processInfo);
160     std::shared_ptr<ThreadProcessInfo> PopDataInfoFromThread(const std::thread::id &threadId);
161     void WakeUpDataThread(const std::thread::id &threadID);
162     void AddDataThreadInWait(const std::thread::id &threadId);
163     bool IsSameRemoteObject(IRemoteObject *stub, int pid, int uid, uint32_t tokenId, const std::string &deviceId,
164         const std::shared_ptr<CommAuthInfo> &auth);
165     bool IsSameRemoteObject(int pid, int uid, const std::string &deviceId, const std::shared_ptr<CommAuthInfo> &auth);
166     bool DetachAppInfoToStubIndex(uint32_t pid, uint32_t uid, uint32_t tokenId, const std::string &deviceId,
167         uint64_t stubIndex, uint32_t listenFd);
168     std::list<uint64_t> DetachAppInfoToStubIndex(uint32_t pid, uint32_t uid, uint32_t tokenId,
169         const std::string &deviceId, uint32_t listenFd);
170     void DetachAppInfoToStubIndex(uint64_t stubIndex);
171     bool AttachAppInfoToStubIndex(uint32_t pid, uint32_t uid, uint32_t tokenId, const std::string &deviceId,
172         uint64_t stubIndex, uint32_t listenFd);
173     bool AttachAppInfoToStubIndex(uint32_t pid, uint32_t uid, uint32_t tokenId, const std::string &deviceId,
174         uint32_t listenFd);
175     bool QueryAppInfoToStubIndex(uint32_t pid, uint32_t uid, uint32_t tokenId, const std::string &deviceId,
176         uint64_t stubIndex, uint32_t listenFd);
177     std::string UIntToString(uint32_t input);
178     bool AttachDBinderCallbackStub(sptr<IRemoteObject> rpcProxy, sptr<DBinderCallbackStub> stub);
179     bool DetachDBinderCallbackStubByProxy(sptr<IRemoteObject> rpcProxy);
180     void DetachDBinderCallbackStub(DBinderCallbackStub *stub);
181     sptr<DBinderCallbackStub> QueryDBinderCallbackStub(sptr<IRemoteObject> rpcProxy);
182     sptr<IRemoteObject> QueryDBinderCallbackProxy(sptr<IRemoteObject> stub);
183 #endif
184 
185 public:
186     static constexpr int DEFAULT_WORK_THREAD_NUM = 16;
187     static constexpr uint32_t DBINDER_HANDLE_BASE = 100000;
188     static constexpr uint32_t DBINDER_HANDLE_RANG = 100;
189     static constexpr int ENCRYPT_LENGTH = 4;
190 private:
191     DISALLOW_COPY_AND_MOVE(IPCProcessSkeleton);
192     IPCProcessSkeleton();
193     static IPCProcessSkeleton *instance_;
194     static std::mutex procMutex_;
195     std::shared_mutex rawDataMutex_;
196 
197     std::map<uint32_t, std::shared_ptr<InvokerRawData>> rawData_;
198     IPCWorkThreadPool *threadPool_ = nullptr;
199 
200 #ifndef CONFIG_IPC_SINGLE
201     std::mutex databusProcMutex_;
202     std::mutex sessionNameMutex_;
203     std::mutex seqNumberMutex_;
204     std::mutex idleDataMutex_;
205     std::mutex dataQueueMutex_;
206     std::mutex findThreadMutex_;
207 
208     std::recursive_mutex proxyToSessionMutex_;
209 
210     std::shared_mutex databusSessionMutex_;
211     std::shared_mutex threadLockMutex_;
212     std::shared_mutex callbackStubMutex_;
213     std::shared_mutex stubObjectsMutex_;
214     std::shared_mutex appInfoToIndexMutex_;
215     std::shared_mutex commAuthMutex_;
216     std::shared_mutex dbinderSentMutex_;
217 
218     std::map<uint64_t, std::shared_ptr<ThreadMessageInfo>> seqNumberToThread_;
219     std::map<uint64_t, IRemoteObject *> stubObjects_;
220     std::map<std::thread::id, std::shared_ptr<SocketThreadLockInfo>> threadLockInfo_;
221     std::map<uint32_t, std::shared_ptr<DBinderSessionObject>> proxyToSession_;
222     std::map<uint32_t, std::shared_ptr<DBinderSessionObject>> dbinderSessionObjects_;
223     std::map<IPCObjectProxy *, sptr<IPCObjectStub>> noticeStub_;
224     std::map<std::thread::id, std::vector<std::shared_ptr<ThreadProcessInfo>>> dataInfoQueue_; // key is threadId
225     std::map<std::string, std::map<uint64_t, uint32_t>> appInfoToStubIndex_;
226     std::map<sptr<IRemoteObject>, wptr<DBinderCallbackStub>> dbinderSentCallback;
227 
228     std::list<std::thread::id> idleDataThreads_;
229     std::list<std::shared_ptr<CommAuthInfo>> commAuth_;
230 
231     uint32_t dBinderHandle_ = DBINDER_HANDLE_BASE; /* dbinder handle start at 100000 */
232     uint64_t seqNumber_ = 0;
233     std::string sessionName_ = std::string("");
234     uint64_t randNum_;
235 #endif
236 };
237 #ifdef CONFIG_IPC_SINGLE
238 } // namespace IPC_SINGLE
239 #endif
240 } // namespace OHOS
241 #endif // OHOS_IPC_IPC_PROCESS_SKELETON_H
242