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