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 #include "ipc_server_stub.h"
17
18 #include "device_manager_ipc_interface_code.h"
19 #include "device_manager_service.h"
20 #include "dm_constants.h"
21 #include "dm_log.h"
22 #include "if_system_ability_manager.h"
23 #include "ipc_cmd_register.h"
24 #include "ipc_skeleton.h"
25 #include "ipc_types.h"
26 #include "iservice_registry.h"
27 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
28 #include "kv_adapter_manager.h"
29 #endif
30 #ifdef SUPPORT_MEMMGR
31 #include "mem_mgr_client.h"
32 #include "mem_mgr_proxy.h"
33 #endif // SUPPORT_MEMMGR
34 #include "string_ex.h"
35 #include "system_ability_definition.h"
36
37 namespace OHOS {
38 namespace DistributedHardware {
39 DM_IMPLEMENT_SINGLE_INSTANCE(IpcServerStub);
40
41 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(&IpcServerStub::GetInstance());
42 constexpr int32_t DM_IPC_THREAD_NUM = 32;
43
IpcServerStub()44 IpcServerStub::IpcServerStub() : SystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID, true)
45 {
46 registerToService_ = false;
47 state_ = ServiceRunningState::STATE_NOT_START;
48 }
49
OnStart()50 void IpcServerStub::OnStart()
51 {
52 LOGI("IpcServerStub::OnStart start");
53 if (state_ == ServiceRunningState::STATE_RUNNING) {
54 LOGI("IpcServerStub has already started.");
55 return;
56 }
57
58 IPCSkeleton::SetMaxWorkThreadNum(DM_IPC_THREAD_NUM);
59
60 LOGI("called:AddAbilityListener begin!");
61 AddSystemAbilityListener(DISTRIBUTED_HARDWARE_SA_ID);
62 #ifdef SUPPORT_MEMMGR
63 AddSystemAbilityListener(MEMORY_MANAGER_SA_ID);
64 #endif // SUPPORT_MEMMGR
65 AddSystemAbilityListener(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN);
66 AddSystemAbilityListener(SCREENLOCK_SERVICE_ID);
67 AddSystemAbilityListener(SOFTBUS_SERVER_SA_ID);
68 LOGI("called:AddAbilityListener end!");
69 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
70 AddSystemAbilityListener(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
71 #endif
72 DeviceManagerService::GetInstance().SubscribePackageCommonEvent();
73 }
74
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)75 void IpcServerStub::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
76 {
77 LOGI("OnAddSystemAbility systemAbilityId:%{public}d added!", systemAbilityId);
78 if (systemAbilityId == SOFTBUS_SERVER_SA_ID) {
79 DeviceManagerService::GetInstance().InitSoftbusListener();
80 if (!Init()) {
81 LOGE("failed to init IpcServerStub");
82 state_ = ServiceRunningState::STATE_NOT_START;
83 return;
84 }
85 state_ = ServiceRunningState::STATE_RUNNING;
86 return;
87 }
88
89 #ifdef SUPPORT_MEMMGR
90 if (systemAbilityId == MEMORY_MANAGER_SA_ID) {
91 int pid = getpid();
92 Memory::MemMgrClient::GetInstance().NotifyProcessStatus(pid, 1, 1, DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
93 return;
94 }
95 #endif // SUPPORT_MEMMGR
96
97 if (systemAbilityId == SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
98 DeviceManagerService::GetInstance().InitAccountInfo();
99 return;
100 }
101
102 if (systemAbilityId == SCREENLOCK_SERVICE_ID) {
103 DeviceManagerService::GetInstance().InitScreenLockEvent();
104 return;
105 }
106
107 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
108 if (systemAbilityId == DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID) {
109 KVAdapterManager::GetInstance().ReInit();
110 return;
111 }
112 #endif
113 }
114
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)115 void IpcServerStub::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
116 {
117 LOGI("OnRemoveSystemAbility systemAbilityId:%{public}d removed!", systemAbilityId);
118 if (systemAbilityId == SOFTBUS_SERVER_SA_ID) {
119 DeviceManagerService::GetInstance().UninitSoftbusListener();
120 } else if (systemAbilityId == DISTRIBUTED_HARDWARE_SA_ID) {
121 DeviceManagerService::GetInstance().LoadHardwareFwkService();
122 }
123 }
124
Init()125 bool IpcServerStub::Init()
126 {
127 LOGI("IpcServerStub::Init ready to init.");
128 DeviceManagerService::GetInstance().InitDMServiceListener();
129 if (!registerToService_) {
130 bool ret = Publish(this);
131 if (!ret) {
132 LOGE("IpcServerStub::Init Publish failed!");
133 return false;
134 }
135 registerToService_ = true;
136 KVAdapterManager::GetInstance().Init();
137 }
138 return true;
139 }
140
OnStop()141 void IpcServerStub::OnStop()
142 {
143 LOGI("IpcServerStub::OnStop ready to stop service.");
144 DeviceManagerService::GetInstance().UninitDMServiceListener();
145 state_ = ServiceRunningState::STATE_NOT_START;
146 registerToService_ = false;
147 #ifdef SUPPORT_MEMMGR
148 int pid = getpid();
149 Memory::MemMgrClient::GetInstance().NotifyProcessStatus(pid, 1, 0, DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
150 #endif // SUPPORT_MEMMGR
151 LOGI("IpcServerStub::OnStop end.");
152 }
153
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)154 int32_t IpcServerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
155 {
156 auto remoteDescriptor = data.ReadInterfaceToken();
157 if (GetDescriptor() != remoteDescriptor) {
158 LOGI("ReadInterfaceToken fail!");
159 return ERR_DM_IPC_READ_FAILED;
160 }
161 int32_t ret = IpcCmdRegister::GetInstance().OnIpcCmd(static_cast<int32_t>(code), data, reply);
162 if (ret == ERR_DM_UNSUPPORTED_IPC_COMMAND) {
163 LOGW("unsupported code: %{public}d", code);
164 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
165 }
166 return ret;
167 }
168
SendCmd(int32_t cmdCode,std::shared_ptr<IpcReq> req,std::shared_ptr<IpcRsp> rsp)169 int32_t IpcServerStub::SendCmd(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp)
170 {
171 MessageParcel data;
172 MessageParcel reply;
173 MessageOption option;
174 if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
175 LOGE("IpcServerStub::SendCmd error: Invalid para, cmdCode: %{public}d", (int32_t)cmdCode);
176 return IPCObjectStub::OnRemoteRequest(cmdCode, data, reply, option);
177 }
178
179 if (IpcCmdRegister::GetInstance().SetRequest(cmdCode, req, data) != DM_OK) {
180 LOGE("set request cmd failed");
181 return ERR_DM_IPC_SEND_REQUEST_FAILED;
182 }
183 int32_t ret = IpcCmdRegister::GetInstance().OnIpcCmd(cmdCode, data, reply);
184 if (ret == ERR_DM_UNSUPPORTED_IPC_COMMAND) {
185 LOGW("unsupported code: %{public}d", cmdCode);
186 return IPCObjectStub::OnRemoteRequest(cmdCode, data, reply, option);
187 }
188 return IpcCmdRegister::GetInstance().ReadResponse(cmdCode, reply, rsp);
189 }
190
QueryServiceState() const191 ServiceRunningState IpcServerStub::QueryServiceState() const
192 {
193 return state_;
194 }
195
RegisterDeviceManagerListener(std::string & pkgName,sptr<IpcRemoteBroker> listener)196 int32_t IpcServerStub::RegisterDeviceManagerListener(std::string &pkgName, sptr<IpcRemoteBroker> listener)
197 {
198 if (pkgName.empty() || listener == nullptr) {
199 LOGE("RegisterDeviceManagerListener error: input parameter invalid.");
200 return ERR_DM_POINT_NULL;
201 }
202
203 LOGI("Register device manager listener for package name: %{public}s", pkgName.c_str());
204 std::lock_guard<std::mutex> autoLock(listenerLock_);
205 auto iter = dmListener_.find(pkgName);
206 if (iter != dmListener_.end()) {
207 LOGI("Listener already exists");
208 auto recipientIter = appRecipient_.find(pkgName);
209 if (recipientIter == appRecipient_.end()) {
210 LOGI("AppRecipient not exists");
211 dmListener_.erase(pkgName);
212 } else {
213 auto listener = iter->second;
214 auto appRecipient = recipientIter->second;
215 listener->AsObject()->RemoveDeathRecipient(appRecipient);
216 appRecipient_.erase(pkgName);
217 dmListener_.erase(pkgName);
218 }
219 }
220
221 sptr<AppDeathRecipient> appRecipient = sptr<AppDeathRecipient>(new AppDeathRecipient());
222 if (!listener->AsObject()->AddDeathRecipient(appRecipient)) {
223 LOGE("AddDeathRecipient Failed");
224 }
225 dmListener_[pkgName] = listener;
226 appRecipient_[pkgName] = appRecipient;
227 LOGI("Register listener complete.");
228 return DM_OK;
229 }
230
UnRegisterDeviceManagerListener(std::string & pkgName)231 int32_t IpcServerStub::UnRegisterDeviceManagerListener(std::string &pkgName)
232 {
233 if (pkgName.empty()) {
234 LOGE("Invalid parameter, pkgName is empty.");
235 return ERR_DM_INPUT_PARA_INVALID;
236 }
237 LOGI("IpcServerStub::UnRegisterDeviceManagerListener In, pkgName: %{public}s", pkgName.c_str());
238 std::lock_guard<std::mutex> autoLock(listenerLock_);
239 auto listenerIter = dmListener_.find(pkgName);
240 if (listenerIter == dmListener_.end()) {
241 LOGI("Listener not exists");
242 return DM_OK;
243 }
244 auto recipientIter = appRecipient_.find(pkgName);
245 if (recipientIter == appRecipient_.end()) {
246 LOGI("AppRecipient not exists");
247 dmListener_.erase(pkgName);
248 return DM_OK;
249 }
250 auto listener = listenerIter->second;
251 auto appRecipient = recipientIter->second;
252 listener->AsObject()->RemoveDeathRecipient(appRecipient);
253 appRecipient_.erase(pkgName);
254 dmListener_.erase(pkgName);
255 return DM_OK;
256 }
257
SendALL(int32_t cmdCode,std::shared_ptr<IpcReq> req,std::shared_ptr<IpcRsp> rsp)258 int32_t IpcServerStub::SendALL(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp)
259 {
260 std::lock_guard<std::mutex> autoLock(listenerLock_);
261 for (const auto &iter : dmListener_) {
262 auto pkgName = iter.first;
263 auto listener = iter.second;
264 req->SetPkgName(pkgName);
265 if (listener == nullptr) {
266 LOGE("IpcServerStub::SendALL, listener is nullptr, pkgName : %{public}s.", pkgName.c_str());
267 continue;
268 }
269 listener->SendCmd(cmdCode, req, rsp);
270 }
271 return DM_OK;
272 }
273
GetAllPkgName()274 std::vector<std::string> IpcServerStub::GetAllPkgName()
275 {
276 std::vector<std::string> PkgNameVec;
277 std::lock_guard<std::mutex> autoLock(listenerLock_);
278 for (const auto &iter : dmListener_) {
279 PkgNameVec.push_back(iter.first);
280 }
281 return PkgNameVec;
282 }
283
GetDmListener(std::string pkgName) const284 const sptr<IpcRemoteBroker> IpcServerStub::GetDmListener(std::string pkgName) const
285 {
286 if (pkgName.empty()) {
287 LOGE("Invalid parameter, pkgName is empty.");
288 return nullptr;
289 }
290 std::lock_guard<std::mutex> autoLock(listenerLock_);
291 auto iter = dmListener_.find(pkgName);
292 if (iter == dmListener_.end()) {
293 return nullptr;
294 }
295 return iter->second;
296 }
297
GetDmListenerPkgName(const wptr<IRemoteObject> & remote) const298 const std::string IpcServerStub::GetDmListenerPkgName(const wptr<IRemoteObject> &remote) const
299 {
300 std::string pkgName = "";
301 std::lock_guard<std::mutex> autoLock(listenerLock_);
302 for (const auto &iter : dmListener_) {
303 if ((iter.second)->AsObject() == remote.promote()) {
304 pkgName = iter.first;
305 break;
306 }
307 }
308 return pkgName;
309 }
310
Dump(int32_t fd,const std::vector<std::u16string> & args)311 int32_t IpcServerStub::Dump(int32_t fd, const std::vector<std::u16string>& args)
312 {
313 LOGI("DistributedHardwareService Dump.");
314 std::vector<std::string> argsStr {};
315 for (auto item : args) {
316 argsStr.emplace_back(Str16ToStr8(item));
317 }
318
319 std::string result("");
320 int ret = DeviceManagerService::GetInstance().DmHiDumper(argsStr, result);
321 if (ret != DM_OK) {
322 LOGE("Dump error, ret = %{public}d", ret);
323 }
324
325 ret = dprintf(fd, "%s\n", result.c_str());
326 if (ret < 0) {
327 LOGE("HiDumper dprintf error");
328 ret = ERR_DM_FAILED;
329 }
330 return ret;
331 }
332
OnRemoteDied(const wptr<IRemoteObject> & remote)333 void AppDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
334 {
335 std::string pkgName = IpcServerStub::GetInstance().GetDmListenerPkgName(remote);
336 LOGI("AppDeathRecipient: OnRemoteDied for %{public}s", pkgName.c_str());
337 IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName);
338 }
339 } // namespace DistributedHardware
340 } // namespace OHOS
341