• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "app_log_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     int32_t msgSize = sizeof(AppSpawnMsg) + 1;
36     msg_ = static_cast<AppSpawnMsg *>(malloc(msgSize));
37     if (msg_ == nullptr) {
38         APP_LOGE("failed to malloc!");
39         return false;
40     }
41     if (memset_s(msg_, msgSize, 0, msgSize) != EOK) {
42         APP_LOGE("failed to memset!");
43         return false;
44     }
45     msg_->uid = startMsg.uid;
46     msg_->gid = startMsg.gid;
47     msg_->gidCount = startMsg.gids.size();
48     for (uint32_t i = 0; i < msg_->gidCount; ++i) {
49         msg_->gidTable[i] = startMsg.gids[i];
50     }
51     if (strcpy_s(msg_->processName, sizeof(msg_->processName), startMsg.procName.c_str()) != EOK) {
52         APP_LOGE("failed to transform procName!");
53         return false;
54     }
55     if (strcpy_s(msg_->soPath, sizeof(msg_->soPath), startMsg.soPath.c_str()) != EOK) {
56         APP_LOGE("failed to transform soPath!");
57         return false;
58     }
59 
60     isValid_ = true;
61     DumpMsg();
62     return isValid_;
63 }
64 
VerifyMsg(const AppSpawnStartMsg & startMsg) const65 bool AppSpawnMsgWrapper::VerifyMsg(const AppSpawnStartMsg &startMsg) const
66 {
67     if (startMsg.uid < 0) {
68         APP_LOGE("invalid uid! [%{public}d]", startMsg.uid);
69         return false;
70     }
71 
72     if (startMsg.gid < 0) {
73         APP_LOGE("invalid gid! [%{public}d]", startMsg.gid);
74         return false;
75     }
76 
77     if (startMsg.gids.size() > AppSpawn::ClientSocket::MAX_GIDS) {
78         APP_LOGE("too many app gids!");
79         return false;
80     }
81 
82     for (uint32_t i = 0; i < startMsg.gids.size(); ++i) {
83         if (startMsg.gids[i] < 0) {
84             APP_LOGE("invalid gids array! [%{public}d]", startMsg.gids[i]);
85             return false;
86         }
87     }
88 
89     if (startMsg.procName.empty() || startMsg.procName.size() >= AppSpawn::ClientSocket::LEN_PROC_NAME) {
90         APP_LOGE("invalid procName!");
91         return false;
92     }
93 
94     return true;
95 }
96 
DumpMsg() const97 void AppSpawnMsgWrapper::DumpMsg() const
98 {
99     if (!isValid_) {
100         return;
101     }
102     APP_LOGI("************AppSpawnMsg*************");
103     APP_LOGI("uid: %{public}d", msg_->uid);
104     APP_LOGI("gid: %{public}d", msg_->gid);
105     for (uint32_t i = 0; i < msg_->gidCount; ++i) {
106         APP_LOGI("gidTable[%{public}d]: %{public}d", i, msg_->gidTable[i]);
107     }
108     APP_LOGI("procName: %{public}s", msg_->processName);
109     APP_LOGI("soPath: %{private}s", msg_->soPath);
110     APP_LOGI("************************************");
111 }
112 
FreeMsg()113 void AppSpawnMsgWrapper::FreeMsg()
114 {
115     if (msg_ != nullptr) {
116         free(msg_);
117         msg_ = nullptr;
118         isValid_ = false;
119     }
120 }
121 }  // namespace AppExecFwk
122 }  // namespace OHOS
123