1 /*
2 * Copyright (c) 2021-2022 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 msg_->bundleIndex = startMsg.bundleIndex;
51 msg_->setAllowInternet = startMsg.setAllowInternet;
52 msg_->allowInternet = startMsg.allowInternet;
53 for (uint32_t i = 0; i < msg_->gidCount; ++i) {
54 msg_->gidTable[i] = startMsg.gids[i];
55 }
56 if (strcpy_s(msg_->processName, sizeof(msg_->processName), startMsg.procName.c_str()) != EOK) {
57 HILOG_ERROR("failed to transform procName!");
58 return false;
59 }
60 if (strcpy_s(msg_->soPath, sizeof(msg_->soPath), startMsg.soPath.c_str()) != EOK) {
61 HILOG_ERROR("failed to transform soPath!");
62 return false;
63 }
64 msg_->accessTokenId = startMsg.accessTokenId;
65 if (strcpy_s(msg_->apl, sizeof(msg_->apl), startMsg.apl.c_str()) != EOK) {
66 HILOG_ERROR("failed to transform apl!");
67 return false;
68 }
69 if (strcpy_s(msg_->bundleName, sizeof(msg_->bundleName), startMsg.bundleName.c_str()) != EOK) {
70 HILOG_ERROR("failed to transform bundleName!");
71 return false;
72 }
73
74 if (strcpy_s(msg_->renderCmd, sizeof(msg_->renderCmd), startMsg.renderParam.c_str()) != EOK) {
75 HILOG_ERROR("failed to transform renderCmd!");
76 return false;
77 }
78 msg_->flags = startMsg.flags;
79 } else if (msg_->code == AppSpawn::ClientSocket::AppOperateCode::GET_RENDER_TERMINATION_STATUS) {
80 msg_->pid = startMsg.pid;
81 } else {
82 HILOG_ERROR("invalid code");
83 return false;
84 }
85
86 isValid_ = true;
87 DumpMsg();
88 return isValid_;
89 }
90
VerifyMsg(const AppSpawnStartMsg & startMsg) const91 bool AppSpawnMsgWrapper::VerifyMsg(const AppSpawnStartMsg &startMsg) const
92 {
93 if (startMsg.code == 0) { // 0: DEFAULT
94 if (startMsg.uid < 0) {
95 HILOG_ERROR("invalid uid! [%{public}d]", startMsg.uid);
96 return false;
97 }
98
99 if (startMsg.gid < 0) {
100 HILOG_ERROR("invalid gid! [%{public}d]", startMsg.gid);
101 return false;
102 }
103
104 if (startMsg.gids.size() > AppSpawn::ClientSocket::MAX_GIDS) {
105 HILOG_ERROR("too many app gids!");
106 return false;
107 }
108
109 for (uint32_t i = 0; i < startMsg.gids.size(); ++i) {
110 if (startMsg.gids[i] < 0) {
111 HILOG_ERROR("invalid gids array! [%{public}d]", startMsg.gids[i]);
112 return false;
113 }
114 }
115
116 if (startMsg.procName.empty() || startMsg.procName.size() >= AppSpawn::ClientSocket::LEN_PROC_NAME) {
117 HILOG_ERROR("invalid procName!");
118 return false;
119 }
120 } else if (startMsg.code == 1) { // 1:GET_RENDER_TERMINATION_STATUS
121 if (startMsg.pid < 0) {
122 HILOG_ERROR("invalid pid!");
123 return false;
124 }
125 } else {
126 HILOG_ERROR("invalid code!");
127 return false;
128 }
129
130 return true;
131 }
132
DumpMsg() const133 void AppSpawnMsgWrapper::DumpMsg() const
134 {
135 if (!isValid_) {
136 return;
137 }
138 HILOG_INFO("************AppSpawnMsg*************");
139 HILOG_INFO("uid: %{public}d, gid: %{public}d, procName: %{public}s", msg_->uid, msg_->gid, msg_->processName);
140 HILOG_INFO("************************************");
141 }
142
FreeMsg()143 void AppSpawnMsgWrapper::FreeMsg()
144 {
145 if (msg_ != nullptr) {
146 free(msg_);
147 msg_ = nullptr;
148 isValid_ = false;
149 }
150 }
151 } // namespace AppExecFwk
152 } // namespace OHOS
153