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 #include "app_spawn_msg_wrapper.h"
17
18 #include "securec.h"
19
20 #include "hilog_wrapper.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
~AppSpawnMsgWrapper()24 AppSpawnMsgWrapper::~AppSpawnMsgWrapper()
25 {
26 FreeMsg();
27 }
28
AssembleMsg(const AppSpawnStartMsg & startMsg)29 bool AppSpawnMsgWrapper::AssembleMsg(const AppSpawnStartMsg &startMsg)
30 {
31 if (!VerifyMsg(startMsg)) {
32 return false;
33 }
34 FreeMsg();
35 size_t msgSize = sizeof(AppSpawnMsg) + 1;
36 msg_ = static_cast<AppSpawnMsg *>(malloc(msgSize));
37 if (msg_ == nullptr) {
38 HILOG_ERROR("failed to malloc!");
39 return false;
40 }
41 if (memset_s(msg_, msgSize, 0, msgSize) != EOK) {
42 HILOG_ERROR("failed to memset!");
43 return false;
44 }
45 msg_->code = static_cast<AppSpawn::ClientSocket::AppOperateCode>(startMsg.code);
46 if (msg_->code == AppSpawn::ClientSocket::AppOperateCode::DEFAULT) {
47 msg_->uid = startMsg.uid;
48 msg_->gid = startMsg.gid;
49 msg_->gidCount = startMsg.gids.size();
50 for (uint32_t i = 0; i < msg_->gidCount; ++i) {
51 msg_->gidTable[i] = startMsg.gids[i];
52 }
53 if (strcpy_s(msg_->processName, sizeof(msg_->processName), startMsg.procName.c_str()) != EOK) {
54 HILOG_ERROR("failed to transform procName!");
55 return false;
56 }
57 if (strcpy_s(msg_->soPath, sizeof(msg_->soPath), startMsg.soPath.c_str()) != EOK) {
58 HILOG_ERROR("failed to transform soPath!");
59 return false;
60 }
61 msg_->accessTokenId = startMsg.accessTokenId;
62 if (strcpy_s(msg_->apl, sizeof(msg_->apl), startMsg.apl.c_str()) != EOK) {
63 HILOG_ERROR("failed to transform apl!");
64 return false;
65 }
66 if (strcpy_s(msg_->bundleName, sizeof(msg_->bundleName), startMsg.bundleName.c_str()) != EOK) {
67 HILOG_ERROR("failed to transform bundleName!");
68 return false;
69 }
70
71 if (strcpy_s(msg_->renderCmd, sizeof(msg_->renderCmd), startMsg.renderParam.c_str()) != EOK) {
72 HILOG_ERROR("failed to transform renderCmd!");
73 return false;
74 }
75 msg_->flags = startMsg.coldStart ? AppSpawn::ClientSocket::APPSPAWN_COLD_BOOT : 0;
76 } else if (msg_->code == AppSpawn::ClientSocket::AppOperateCode::GET_RENDER_TERMINATION_STATUS) {
77 msg_->pid = startMsg.pid;
78 } else {
79 HILOG_ERROR("invalid code");
80 return false;
81 }
82
83 isValid_ = true;
84 DumpMsg();
85 return isValid_;
86 }
87
VerifyMsg(const AppSpawnStartMsg & startMsg) const88 bool AppSpawnMsgWrapper::VerifyMsg(const AppSpawnStartMsg &startMsg) const
89 {
90 if (startMsg.code == 0) { // 0: DEFAULT
91 if (startMsg.uid < 0) {
92 HILOG_ERROR("invalid uid! [%{public}d]", startMsg.uid);
93 return false;
94 }
95
96 if (startMsg.gid < 0) {
97 HILOG_ERROR("invalid gid! [%{public}d]", startMsg.gid);
98 return false;
99 }
100
101 if (startMsg.gids.size() > AppSpawn::ClientSocket::MAX_GIDS) {
102 HILOG_ERROR("too many app gids!");
103 return false;
104 }
105
106 for (uint32_t i = 0; i < startMsg.gids.size(); ++i) {
107 if (startMsg.gids[i] < 0) {
108 HILOG_ERROR("invalid gids array! [%{public}d]", startMsg.gids[i]);
109 return false;
110 }
111 }
112
113 if (startMsg.procName.empty() || startMsg.procName.size() >= AppSpawn::ClientSocket::LEN_PROC_NAME) {
114 HILOG_ERROR("invalid procName!");
115 return false;
116 }
117 } else if (startMsg.code == 1) { // 1:GET_RENDER_TERMINATION_STATUS
118 if (startMsg.pid < 0) {
119 HILOG_ERROR("invalid pid!");
120 return false;
121 }
122 } else {
123 HILOG_ERROR("invalid code!");
124 return false;
125 }
126
127 return true;
128 }
129
DumpMsg() const130 void AppSpawnMsgWrapper::DumpMsg() const
131 {
132 if (!isValid_) {
133 return;
134 }
135 HILOG_INFO("************AppSpawnMsg*************");
136 HILOG_INFO("uid: %{public}d", msg_->uid);
137 HILOG_INFO("gid: %{public}d", msg_->gid);
138 for (uint32_t i = 0; i < msg_->gidCount; ++i) {
139 HILOG_INFO("gidTable[%{public}d]: %{public}d", i, msg_->gidTable[i]);
140 }
141 HILOG_INFO("procName: %{public}s", msg_->processName);
142 HILOG_INFO("soPath: %{private}s", msg_->soPath);
143 HILOG_INFO("************************************");
144 }
145
FreeMsg()146 void AppSpawnMsgWrapper::FreeMsg()
147 {
148 if (msg_ != nullptr) {
149 free(msg_);
150 msg_ = nullptr;
151 isValid_ = false;
152 }
153 }
154 } // namespace AppExecFwk
155 } // namespace OHOS
156