1 /*
2 * Copyright (c) 2021-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 #include "app_spawn_client.h"
16
17 #include <unordered_set>
18
19 #include "hitrace_meter.h"
20 #include "hilog_tag_wrapper.h"
21 #include "hilog_tag_wrapper.h"
22 #include "nlohmann/json.hpp"
23 #include "securec.h"
24 #include "time_util.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 using namespace OHOS::AbilityRuntime;
29 namespace {
30 constexpr const char* HSPLIST_BUNDLES = "bundles";
31 constexpr const char* HSPLIST_MODULES = "modules";
32 constexpr const char* HSPLIST_VERSIONS = "versions";
33 constexpr const char* DATAGROUPINFOLIST_DATAGROUPID = "dataGroupId";
34 constexpr const char* DATAGROUPINFOLIST_GID = "gid";
35 constexpr const char* DATAGROUPINFOLIST_DIR = "dir";
36 constexpr const char* JSON_DATA_APP = "/data/app/el2/";
37 constexpr const char* JSON_GROUP = "/group/";
38 constexpr const char* VERSION_PREFIX = "v";
39 constexpr const char* APPSPAWN_CLIENT_USER_NAME = "APP_MANAGER_SERVICE";
40 constexpr int32_t RIGHT_SHIFT_STEP = 1;
41 constexpr int32_t START_FLAG_TEST_NUM = 1;
42 constexpr const char* MAX_CHILD_PROCESS = "MaxChildProcess";
43 constexpr const char* UNINSTALL_BUNDLE_NAME = "uninstallDebugHapMsg";
44 constexpr const char* JITPERMISSIONSLIST_NAME = "name";
45 constexpr const char* JITPERMISSIONSLIST_NAME_VALUE = "JITPermissions";
46 constexpr const char* JITPERMISSIONSLIST_COUNT = "ohos.encaps.count";
47 constexpr const char* JITPERMISSIONSLIST_PERMISSIONS_NAME = "permissions";
48 }
AppSpawnClient(bool isNWebSpawn)49 AppSpawnClient::AppSpawnClient(bool isNWebSpawn)
50 {
51 TAG_LOGD(AAFwkTag::APPMGR, "call");
52 if (isNWebSpawn) {
53 serviceName_ = NWEBSPAWN_SERVER_NAME;
54 }
55 state_ = SpawnConnectionState::STATE_NOT_CONNECT;
56 }
57
AppSpawnClient(const char * serviceName)58 AppSpawnClient::AppSpawnClient(const char* serviceName)
59 {
60 TAG_LOGD(AAFwkTag::APPMGR, "call");
61 std::string serviceName__ = serviceName;
62 if (serviceName__ == APPSPAWN_SERVER_NAME) {
63 serviceName_ = APPSPAWN_SERVER_NAME;
64 } else if (serviceName__ == CJAPPSPAWN_SERVER_NAME) {
65 serviceName_ = CJAPPSPAWN_SERVER_NAME;
66 } else if (serviceName__ == NWEBSPAWN_SERVER_NAME) {
67 serviceName_ = NWEBSPAWN_SERVER_NAME;
68 } else if (serviceName__ == NATIVESPAWN_SERVER_NAME) {
69 serviceName_ = NATIVESPAWN_SERVER_NAME;
70 } else {
71 TAG_LOGE(AAFwkTag::APPMGR, "unknown service name");
72 serviceName_ = NWEBSPAWN_SERVER_NAME;
73 }
74 state_ = SpawnConnectionState::STATE_NOT_CONNECT;
75 }
76
~AppSpawnClient()77 AppSpawnClient::~AppSpawnClient()
78 {
79 CloseConnection();
80 }
81
OpenConnection()82 ErrCode AppSpawnClient::OpenConnection()
83 {
84 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
85 if (state_ == SpawnConnectionState::STATE_CONNECTED) {
86 return 0;
87 }
88 TAG_LOGI(AAFwkTag::APPMGR, "OpenConnection");
89 int64_t startTime = AbilityRuntime::TimeUtil::SystemTimeMillisecond();
90 AppSpawnClientHandle handle = nullptr;
91 ErrCode ret = 0;
92 ret = AppSpawnClientInit(serviceName_.c_str(), &handle);
93 int64_t costTime = AbilityRuntime::TimeUtil::SystemTimeMillisecond() - startTime;
94 if (costTime > MAX_COST_TIME) {
95 TAG_LOGW(AAFwkTag::APPMGR, "appspawnclientInit cost %{public}" PRId64 "ms!", costTime);
96 }
97
98 if (FAILED(ret)) {
99 TAG_LOGE(AAFwkTag::APPMGR, "create appspawn client faild.");
100 state_ = SpawnConnectionState::STATE_CONNECT_FAILED;
101 return ret;
102 }
103 handle_ = handle;
104 state_ = SpawnConnectionState::STATE_CONNECTED;
105
106 return ret;
107 }
108
CloseConnection()109 void AppSpawnClient::CloseConnection()
110 {
111 TAG_LOGD(AAFwkTag::APPMGR, "call");
112 if (state_ == SpawnConnectionState::STATE_CONNECTED) {
113 AppSpawnClientDestroy(handle_);
114 }
115 state_ = SpawnConnectionState::STATE_NOT_CONNECT;
116 }
117
QueryConnectionState() const118 SpawnConnectionState AppSpawnClient::QueryConnectionState() const
119 {
120 return state_;
121 }
122
GetAppSpawnClientHandle() const123 AppSpawnClientHandle AppSpawnClient::GetAppSpawnClientHandle() const
124 {
125 if (state_ == SpawnConnectionState::STATE_CONNECTED) {
126 return handle_;
127 }
128 return nullptr;
129 }
130
DumpDataGroupInfoListToJson(const DataGroupInfoList & dataGroupInfoList)131 static std::string DumpDataGroupInfoListToJson(const DataGroupInfoList &dataGroupInfoList)
132 {
133 nlohmann::json dataGroupInfoListJson;
134 for (auto& dataGroupInfo : dataGroupInfoList) {
135 dataGroupInfoListJson[DATAGROUPINFOLIST_DATAGROUPID].emplace_back(dataGroupInfo.dataGroupId);
136 dataGroupInfoListJson[DATAGROUPINFOLIST_GID].emplace_back(std::to_string(dataGroupInfo.gid));
137 std::string dir = JSON_DATA_APP + std::to_string(dataGroupInfo.userId)
138 + JSON_GROUP + dataGroupInfo.uuid;
139 dataGroupInfoListJson[DATAGROUPINFOLIST_DIR].emplace_back(dir);
140 }
141 return dataGroupInfoListJson.dump();
142 }
143
DumpHspListToJson(const HspList & hspList)144 static std::string DumpHspListToJson(const HspList &hspList)
145 {
146 nlohmann::json hspListJson;
147 for (auto& hsp : hspList) {
148 hspListJson[HSPLIST_BUNDLES].emplace_back(hsp.bundleName);
149 hspListJson[HSPLIST_MODULES].emplace_back(hsp.moduleName);
150 hspListJson[HSPLIST_VERSIONS].emplace_back(VERSION_PREFIX + std::to_string(hsp.versionCode));
151 }
152 return hspListJson.dump();
153 }
154
DumpJITPermissionListToJson(const JITPermissionsList & jitPermissionsList)155 static std::string DumpJITPermissionListToJson(const JITPermissionsList &jitPermissionsList)
156 {
157 nlohmann::json jitPermissionsListJson;
158 jitPermissionsListJson[JITPERMISSIONSLIST_NAME] = JITPERMISSIONSLIST_NAME_VALUE;
159 jitPermissionsListJson[JITPERMISSIONSLIST_COUNT] = jitPermissionsList.size();
160 for (auto& jitPermission : jitPermissionsList) {
161 jitPermissionsListJson[JITPERMISSIONSLIST_PERMISSIONS_NAME].emplace_back(jitPermission);
162 }
163 return jitPermissionsListJson.dump();
164 }
165
DumpAppEnvToJson(const std::map<std::string,std::string> & appEnv)166 static std::string DumpAppEnvToJson(const std::map<std::string, std::string> &appEnv)
167 {
168 nlohmann::json appEnvJson;
169 for (const auto &[envName, envValue] : appEnv) {
170 appEnvJson[envName] = envValue;
171 }
172 return appEnvJson.dump();
173 }
174
SetDacInfo(const AppSpawnStartMsg & startMsg,AppSpawnReqMsgHandle reqHandle)175 int32_t AppSpawnClient::SetDacInfo(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle)
176 {
177 int32_t ret = 0;
178 AppDacInfo appDacInfo = {0};
179 appDacInfo.uid = startMsg.uid;
180 appDacInfo.gid = startMsg.gid;
181 appDacInfo.gidCount = startMsg.gids.size() + startMsg.dataGroupInfoList.size();
182 for (uint32_t i = 0; i < startMsg.gids.size(); i++) {
183 appDacInfo.gidTable[i] = startMsg.gids[i];
184 }
185 for (uint32_t i = startMsg.gids.size(); i < appDacInfo.gidCount; i++) {
186 appDacInfo.gidTable[i] = startMsg.dataGroupInfoList[i - startMsg.gids.size()].gid;
187 }
188 ret = strcpy_s(appDacInfo.userName, sizeof(appDacInfo.userName), APPSPAWN_CLIENT_USER_NAME);
189 if (ret) {
190 TAG_LOGE(AAFwkTag::APPMGR, "failed to set dac userName!");
191 return ret;
192 }
193 return AppSpawnReqMsgSetAppDacInfo(reqHandle, &appDacInfo);
194 }
195
SetMountPermission(const AppSpawnStartMsg & startMsg,AppSpawnReqMsgHandle reqHandle)196 int32_t AppSpawnClient::SetMountPermission(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle)
197 {
198 int32_t ret = 0;
199 std::set<std::string> mountPermissionList = startMsg.permissions;
200 for (std::string permission : mountPermissionList) {
201 ret = AppSpawnClientAddPermission(handle_, reqHandle, permission.c_str());
202 if (ret != 0) {
203 TAG_LOGE(AAFwkTag::APPMGR, "AppSpawnReqMsgAddPermission %{public}s failed", permission.c_str());
204 return ret;
205 }
206 }
207 return ret;
208 }
209
SetStartFlags(const AppSpawnStartMsg & startMsg,AppSpawnReqMsgHandle reqHandle)210 int32_t AppSpawnClient::SetStartFlags(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle)
211 {
212 int32_t ret = 0;
213 uint32_t startFlagTmp = startMsg.flags;
214 int flagIndex = 0;
215 while (startFlagTmp > 0) {
216 if (startFlagTmp & START_FLAG_TEST_NUM) {
217 ret = AppSpawnReqMsgSetAppFlag(reqHandle, static_cast<AppFlagsIndex>(flagIndex));
218 if (ret != 0) {
219 TAG_LOGE(AAFwkTag::APPMGR, "SetFlagIdx %{public}d failed, ret: %{public}d", flagIndex, ret);
220 return ret;
221 }
222 }
223 startFlagTmp = startFlagTmp >> RIGHT_SHIFT_STEP;
224 flagIndex++;
225 }
226 if (startMsg.atomicServiceFlag) {
227 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ATOMIC_SERVICE);
228 if (ret != 0) {
229 TAG_LOGE(AAFwkTag::APPMGR, "SetAtomicServiceFlag failed, ret: %{public}d", ret);
230 return ret;
231 }
232 }
233 if (startMsg.strictMode) {
234 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ISOLATED_SANDBOX);
235 if (ret != 0) {
236 TAG_LOGE(AAFwkTag::APPMGR, "SetStrictMode failed, ret: %{public}d", ret);
237 return ret;
238 }
239 }
240 if (startMsg.isolatedExtension) {
241 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_EXTENSION_SANDBOX);
242 if (ret != 0) {
243 TAG_LOGE(AAFwkTag::APPMGR, "SetAppExtension failed, ret: %{public}d", ret);
244 return ret;
245 }
246 }
247 if (startMsg.flags & APP_FLAGS_CLONE_ENABLE) {
248 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_CLONE_ENABLE);
249 if (ret != 0) {
250 TAG_LOGE(AAFwkTag::APPMGR, "SetCloneFlag failed, ret: %{public}d", ret);
251 return ret;
252 }
253 }
254 ret = SetChildProcessTypeStartFlag(reqHandle, startMsg.childProcessType);
255 if (ret != ERR_OK) {
256 TAG_LOGE(AAFwkTag::APPMGR, "Set childProcessType flag failed, ret: %{public}d", ret);
257 return ret;
258 }
259 ret = SetIsolationModeFlag(startMsg, reqHandle);
260 return ret;
261 }
262
AppspawnSetExtMsg(const AppSpawnStartMsg & startMsg,AppSpawnReqMsgHandle reqHandle)263 int32_t AppSpawnClient::AppspawnSetExtMsg(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle)
264 {
265 int32_t ret = 0;
266 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_RENDER_CMD, startMsg.renderParam.c_str());
267 if (ret) {
268 TAG_LOGE(AAFwkTag::APPMGR, "SetRenderCmd failed, ret: %{public}d", ret);
269 return ret;
270 }
271
272 if (!startMsg.hspList.empty()) {
273 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_HSP_LIST,
274 DumpHspListToJson(startMsg.hspList).c_str());
275 if (ret) {
276 TAG_LOGE(AAFwkTag::APPMGR, "SetExtraHspList failed, ret: %{public}d", ret);
277 return ret;
278 }
279 }
280
281 if (!startMsg.dataGroupInfoList.empty()) {
282 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_DATA_GROUP,
283 DumpDataGroupInfoListToJson(startMsg.dataGroupInfoList).c_str());
284 if (ret) {
285 TAG_LOGE(AAFwkTag::APPMGR, "SetExtraDataGroupInfo failed, ret: %{public}d", ret);
286 return ret;
287 }
288 }
289
290 if (!startMsg.overlayInfo.empty()) {
291 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_OVERLAY, startMsg.overlayInfo.c_str());
292 if (ret) {
293 TAG_LOGE(AAFwkTag::APPMGR, "SetExtraOverlayInfo failed, ret: %{public}d", ret);
294 return ret;
295 }
296 }
297
298 if (!startMsg.appEnv.empty()) {
299 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_APP_ENV, DumpAppEnvToJson(startMsg.appEnv).c_str());
300 if (ret) {
301 TAG_LOGE(AAFwkTag::APPMGR, "SetExtraEnv failed, ret: %{public}d", ret);
302 return ret;
303 }
304 }
305
306 if (!startMsg.atomicAccount.empty()) {
307 ret = AppSpawnReqMsgAddExtInfo(reqHandle, MSG_EXT_NAME_ACCOUNT_ID,
308 reinterpret_cast<const uint8_t*>(startMsg.atomicAccount.c_str()), startMsg.atomicAccount.size());
309 if (ret) {
310 TAG_LOGE(AAFwkTag::APPMGR, "AppSpawnReqMsgAddExtInfo failed, ret: %{public}d", ret);
311 return ret;
312 }
313 }
314
315 return AppspawnSetExtMsgMore(startMsg, reqHandle);
316 }
317
AppspawnSetExtMsgMore(const AppSpawnStartMsg & startMsg,AppSpawnReqMsgHandle reqHandle)318 int32_t AppSpawnClient::AppspawnSetExtMsgMore(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle)
319 {
320 int32_t ret = 0;
321
322 if (!startMsg.provisionType.empty()) {
323 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_PROVISION_TYPE, startMsg.provisionType.c_str());
324 if (ret) {
325 TAG_LOGE(AAFwkTag::APPMGR, "SetExtraProvisionType failed, ret: %{public}d", ret);
326 return ret;
327 }
328 }
329
330 if (!startMsg.processType.empty()) {
331 ret = AppSpawnReqMsgAddExtInfo(reqHandle, MSG_EXT_NAME_PROCESS_TYPE,
332 reinterpret_cast<const uint8_t*>(startMsg.processType.c_str()), startMsg.processType.size());
333 if (ret) {
334 TAG_LOGE(AAFwkTag::APPMGR, "AppSpawnReqMsgAddExtInfo failed, ret: %{public}d", ret);
335 return ret;
336 }
337 }
338
339 std::string maxChildProcessStr = std::to_string(startMsg.maxChildProcess);
340 ret = AppSpawnReqMsgAddExtInfo(reqHandle, MAX_CHILD_PROCESS,
341 reinterpret_cast<const uint8_t*>(maxChildProcessStr.c_str()), maxChildProcessStr.size());
342 if (ret) {
343 TAG_LOGE(AAFwkTag::APPMGR, "Send maxChildProcess failed, ret: %{public}d", ret);
344 return ret;
345 }
346 TAG_LOGD(AAFwkTag::APPMGR, "Send maxChildProcess %{public}s success", maxChildProcessStr.c_str());
347
348 if (!startMsg.extensionSandboxPath.empty()) {
349 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_APP_EXTENSION,
350 startMsg.extensionSandboxPath.c_str());
351 if (ret) {
352 TAG_LOGE(AAFwkTag::APPMGR, "SetExtraExtensionSandboxDirs failed, ret: %{public}d", ret);
353 return ret;
354 }
355 }
356
357 if (!startMsg.fds.empty()) {
358 ret = SetExtMsgFds(reqHandle, startMsg.fds);
359 if (ret != ERR_OK) {
360 TAG_LOGE(AAFwkTag::APPMGR, "SetExtMsgFds failed, ret: %{public}d", ret);
361 return ret;
362 }
363 }
364
365 if (!startMsg.jitPermissionsList.empty()) {
366 std::string jitPermissionsStr = DumpJITPermissionListToJson(startMsg.jitPermissionsList);
367 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_JIT_PERMISSIONS, jitPermissionsStr.c_str());
368 if (ret) {
369 TAG_LOGE(AAFwkTag::APPMGR, "fail, ret: %{public}d", ret);
370 return ret;
371 }
372 TAG_LOGD(AAFwkTag::APPMGR, "Send JIT Permission: %{public}s", jitPermissionsStr.c_str());
373 }
374
375 return ret;
376 }
377
AppspawnCreateDefaultMsg(const AppSpawnStartMsg & startMsg,AppSpawnReqMsgHandle reqHandle)378 int32_t AppSpawnClient::AppspawnCreateDefaultMsg(const AppSpawnStartMsg &startMsg, AppSpawnReqMsgHandle reqHandle)
379 {
380 TAG_LOGD(AAFwkTag::APPMGR, "call");
381 int32_t ret = 0;
382 do {
383 ret = SetDacInfo(startMsg, reqHandle);
384 if (ret) {
385 TAG_LOGE(AAFwkTag::APPMGR, "SetDacInfo failed, ret: %{public}d", ret);
386 break;
387 }
388 ret = AppSpawnReqMsgSetBundleInfo(reqHandle, startMsg.bundleIndex, startMsg.bundleName.c_str());
389 if (ret) {
390 TAG_LOGE(AAFwkTag::APPMGR, "SetBundleInfo failed, ret: %{public}d", ret);
391 break;
392 }
393 ret = AppSpawnReqMsgSetAppInternetPermissionInfo(reqHandle, startMsg.allowInternet,
394 startMsg.setAllowInternet);
395 if (ret) {
396 TAG_LOGE(AAFwkTag::APPMGR, "SetInternetPermissionInfo failed, ret: %{public}d", ret);
397 break;
398 }
399 if (startMsg.ownerId.size()) {
400 ret = AppSpawnReqMsgSetAppOwnerId(reqHandle, startMsg.ownerId.c_str());
401 if (ret) {
402 TAG_LOGE(AAFwkTag::APPMGR, "SetOwnerId %{public}s failed, ret: %{public}d",
403 startMsg.ownerId.c_str(), ret);
404 break;
405 }
406 }
407 ret = AppSpawnReqMsgSetAppAccessToken(reqHandle, startMsg.accessTokenIdEx);
408 if (ret) {
409 TAG_LOGE(AAFwkTag::APPMGR, "ret: %{public}d", ret);
410 break;
411 }
412 ret = AppSpawnReqMsgSetAppDomainInfo(reqHandle, startMsg.hapFlags, startMsg.apl.c_str());
413 if (ret) {
414 TAG_LOGE(AAFwkTag::APPMGR,
415 "SetDomainInfo failed, hapFlags is %{public}d, apl is %{public}s, ret: %{public}d",
416 startMsg.hapFlags, startMsg.apl.c_str(), ret);
417 break;
418 }
419 ret = SetStartFlags(startMsg, reqHandle);
420 if (ret) {
421 TAG_LOGE(AAFwkTag::APPMGR, "SetStartFlags failed, ret: %{public}d", ret);
422 break;
423 }
424 ret = SetMountPermission(startMsg, reqHandle);
425 if (ret) {
426 TAG_LOGE(AAFwkTag::APPMGR, "SetMountPermission failed, ret: %{public}d", ret);
427 break;
428 }
429 if (AppspawnSetExtMsg(startMsg, reqHandle)) {
430 break;
431 }
432 return ret;
433 } while (0);
434
435 TAG_LOGI(AAFwkTag::APPMGR, "AppSpawnReqMsgFree");
436 AppSpawnReqMsgFree(reqHandle);
437
438 return ret;
439 }
440
VerifyMsg(const AppSpawnStartMsg & startMsg)441 bool AppSpawnClient::VerifyMsg(const AppSpawnStartMsg &startMsg)
442 {
443 TAG_LOGD(AAFwkTag::APPMGR, "VerifyMsg");
444 if (startMsg.code == MSG_APP_SPAWN ||
445 startMsg.code == MSG_SPAWN_NATIVE_PROCESS) {
446 if (startMsg.uid < 0) {
447 TAG_LOGE(AAFwkTag::APPMGR, "invalid uid! [%{public}d]", startMsg.uid);
448 return false;
449 }
450
451 if (startMsg.gid < 0) {
452 TAG_LOGE(AAFwkTag::APPMGR, "invalid gid! [%{public}d]", startMsg.gid);
453 return false;
454 }
455
456 if (startMsg.gids.size() > APP_MAX_GIDS) {
457 TAG_LOGE(AAFwkTag::APPMGR, "too many app gids!");
458 return false;
459 }
460
461 for (uint32_t i = 0; i < startMsg.gids.size(); ++i) {
462 if (startMsg.gids[i] < 0) {
463 TAG_LOGE(AAFwkTag::APPMGR, "invalid gids array! [%{public}d]", startMsg.gids[i]);
464 return false;
465 }
466 }
467 if (startMsg.procName.empty() || startMsg.procName.size() >= MAX_PROC_NAME_LEN) {
468 TAG_LOGE(AAFwkTag::APPMGR, "invalid procName!");
469 return false;
470 }
471 } else if (startMsg.code == MSG_GET_RENDER_TERMINATION_STATUS) {
472 if (startMsg.pid < 0) {
473 TAG_LOGE(AAFwkTag::APPMGR, "invalid pid!");
474 return false;
475 }
476 } else {
477 TAG_LOGE(AAFwkTag::APPMGR, "invalid code!");
478 return false;
479 }
480
481 return true;
482 }
483
PreStartNWebSpawnProcess()484 int32_t AppSpawnClient::PreStartNWebSpawnProcess()
485 {
486 TAG_LOGI(AAFwkTag::APPMGR, "PreStartNWebSpawnProcess");
487 return OpenConnection();
488 }
489
StartProcess(const AppSpawnStartMsg & startMsg,pid_t & pid)490 int32_t AppSpawnClient::StartProcess(const AppSpawnStartMsg &startMsg, pid_t &pid)
491 {
492 TAG_LOGD(AAFwkTag::APPMGR, "StartProcess");
493 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
494 if (!VerifyMsg(startMsg)) {
495 return ERR_INVALID_VALUE;
496 }
497
498 int32_t ret = 0;
499 AppSpawnReqMsgHandle reqHandle = nullptr;
500 int64_t startTime = AbilityRuntime::TimeUtil::SystemTimeMillisecond();
501
502 ret = OpenConnection();
503 if (ret != 0) {
504 return ret;
505 }
506
507 ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
508 if (ret != 0) {
509 TAG_LOGE(AAFwkTag::APPMGR, "AppSpawnReqMsgCreate faild.");
510 return ret;
511 }
512
513 ret = AppspawnCreateDefaultMsg(startMsg, reqHandle);
514 if (ret != 0) {
515 return ret; // create msg failed
516 }
517
518 TAG_LOGD(AAFwkTag::APPMGR, "AppspawnSendMsg");
519 AppSpawnResult result = {0};
520 ret = AppSpawnClientSendMsg(handle_, reqHandle, &result);
521
522 int64_t costTime = AbilityRuntime::TimeUtil::SystemTimeMillisecond() - startTime;
523 if (costTime > MAX_COST_TIME) {
524 TAG_LOGW(AAFwkTag::APPMGR, "StartProcess cost %{public}" PRId64 "ms!", costTime);
525 }
526 if (ret != 0) {
527 TAG_LOGE(AAFwkTag::APPMGR, "appspawn send msg faild!");
528 return ret;
529 }
530 if (result.pid <= 0) {
531 TAG_LOGE(AAFwkTag::APPMGR, "pid invalid!");
532 return ERR_APPEXECFWK_INVALID_PID;
533 } else {
534 pid = result.pid;
535 }
536 TAG_LOGI(AAFwkTag::APPMGR, "pid = [%{public}d]", pid);
537 return result.result;
538 }
539
SendAppSpawnUninstallDebugHapMsg(int32_t userId)540 int32_t AppSpawnClient::SendAppSpawnUninstallDebugHapMsg(int32_t userId)
541 {
542 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
543 AppSpawnReqMsgHandle reqHandle = nullptr;
544 int32_t ret = OpenConnection();
545 if (ret != ERR_OK) {
546 TAG_LOGE(AAFwkTag::APPMGR, "OpenConnection failed");
547 return ret;
548 }
549 ret = AppSpawnReqMsgCreate(MSG_UNINSTALL_DEBUG_HAP, UNINSTALL_BUNDLE_NAME, &reqHandle);
550 if (ret != ERR_OK) {
551 TAG_LOGE(AAFwkTag::APPMGR, "AppSpawnReqMsgCreate failed");
552 return ret;
553 }
554 auto msg = std::to_string(userId);
555 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_USERID, msg.c_str());
556 if (ret != ERR_OK) {
557 AppSpawnReqMsgFree(reqHandle);
558 TAG_LOGE(AAFwkTag::APPMGR, "AppSpawnReqMsgAddStringInfo failed");
559 return ret;
560 }
561 AppSpawnResult result = { 0 };
562 ret = AppSpawnClientSendMsg(handle_, reqHandle, &result);
563 if (ret != ERR_OK) {
564 TAG_LOGE(AAFwkTag::APPMGR, "AppSpawnClientSendMsg failed");
565 return ret;
566 }
567 return ret;
568 }
569
GetRenderProcessTerminationStatus(const AppSpawnStartMsg & startMsg,int & status)570 int32_t AppSpawnClient::GetRenderProcessTerminationStatus(const AppSpawnStartMsg &startMsg, int &status)
571 {
572 TAG_LOGI(AAFwkTag::APPMGR, "GetRenderProcessTerminationStatus");
573 int32_t ret = 0;
574 AppSpawnReqMsgHandle reqHandle = nullptr;
575
576 // check parameters
577 if (!VerifyMsg(startMsg)) {
578 return ERR_INVALID_VALUE;
579 }
580
581 ret = OpenConnection();
582 if (ret != 0) {
583 return ret;
584 }
585
586 ret = AppSpawnTerminateMsgCreate(startMsg.pid, &reqHandle);
587 if (ret != 0) {
588 TAG_LOGE(AAFwkTag::APPMGR, "AppSpawnTerminateMsgCreate faild.");
589 return ret;
590 }
591
592 TAG_LOGI(AAFwkTag::APPMGR, "AppspawnSendMsg");
593 AppSpawnResult result = {0};
594 ret = AppSpawnClientSendMsg(handle_, reqHandle, &result);
595 status = result.result;
596 if (ret != 0) {
597 TAG_LOGE(AAFwkTag::APPMGR, "appspawn send msg faild!");
598 return ret;
599 }
600 TAG_LOGI(AAFwkTag::APPMGR, "status = [%{public}d]", status);
601
602 return ret;
603 }
604
SetChildProcessTypeStartFlag(const AppSpawnReqMsgHandle & reqHandle,int32_t childProcessType)605 int32_t AppSpawnClient::SetChildProcessTypeStartFlag(const AppSpawnReqMsgHandle &reqHandle,
606 int32_t childProcessType)
607 {
608 TAG_LOGD(AAFwkTag::APPMGR, "SetChildProcessTypeStartFlag, type:%{public}d", childProcessType);
609 if (childProcessType != CHILD_PROCESS_TYPE_NOT_CHILD) {
610 return AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_CHILDPROCESS);
611 }
612 return ERR_OK;
613 }
614
SetExtMsgFds(const AppSpawnReqMsgHandle & reqHandle,const std::map<std::string,int32_t> & fds)615 int32_t AppSpawnClient::SetExtMsgFds(const AppSpawnReqMsgHandle &reqHandle,
616 const std::map<std::string, int32_t> &fds)
617 {
618 TAG_LOGI(AAFwkTag::APPMGR, "SetExtMsgFds, fds size:%{public}zu", fds.size());
619 int32_t ret = ERR_OK;
620 for (const auto &item : fds) {
621 ret = AppSpawnReqMsgAddFd(reqHandle, item.first.c_str(), item.second);
622 if (ret != ERR_OK) {
623 TAG_LOGE(AAFwkTag::APPMGR, "AppSpawnReqMsgAddFd failed, key:%{public}s, fd:%{public}d, ret:%{public}d",
624 item.first.c_str(), item.second, ret);
625 return ret;
626 }
627 }
628 return ERR_OK;
629 }
630
SetIsolationModeFlag(const AppSpawnStartMsg & startMsg,const AppSpawnReqMsgHandle & reqHandle)631 int32_t AppSpawnClient::SetIsolationModeFlag(const AppSpawnStartMsg &startMsg, const AppSpawnReqMsgHandle &reqHandle)
632 {
633 TAG_LOGD(AAFwkTag::APPMGR, "SetIsolationFlag, isolationMode:%{public}d", startMsg.isolationMode);
634 if (!startMsg.isolationMode) {
635 return ERR_OK;
636 }
637 auto ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ISOLATED_SANDBOX_TYPE);
638 if (ret != 0) {
639 TAG_LOGE(AAFwkTag::APPMGR, "SetIsolationFlag failed, ret: %{public}d", ret);
640 return ret;
641 }
642 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ISOLATED_NETWORK);
643 if (ret != 0) {
644 TAG_LOGE(AAFwkTag::APPMGR, "SetIsolationFlag failed, ret: %{public}d", ret);
645 return ret;
646 }
647 return ERR_OK;
648 }
649 } // namespace AppExecFwk
650 } // namespace OHOS
651