1 /*
2 * Copyright (c) 2020 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 "bundle_inner_feature.h"
17
18 #include "ability_info_utils.h"
19 #include "appexecfwk_errors.h"
20 #include "bundle_info_utils.h"
21 #include "bundle_inner_interface.h"
22 #include "bundle_manager_service.h"
23 #include "bundle_message_id.h"
24 #include "convert_utils.h"
25 #include "ipc_skeleton.h"
26 #include "bundle_log.h"
27 #include "message.h"
28 #include "ohos_init.h"
29 #include "samgr_lite.h"
30 #include "securec.h"
31 #include "serializer.h"
32 #include "utils.h"
33 #include "want_utils.h"
34
35 namespace OHOS {
36 static BmsInnerImpl g_bmsInnerImpl = {
37 SERVER_IPROXY_IMPL_BEGIN,
38 .Invoke = BundleInnerFeature::Invoke,
39 IPROXY_END
40 };
41
42 BundleInvokeType BundleInnerFeature::BundleMsInvokeFuc[BMS_CMD_END - BMS_INNER_BEGIN] {
43 InstallInnerBundle,
44 UninstallInnerBundle,
45 #ifdef OHOS_DEBUG
46 SetExternalInstallMode,
47 SetInnerDebugMode,
48 SetInnerSignMode,
49 #endif
50 };
51
GetBmsInnerFeatureApi(Feature * feature)52 IUnknown *GetBmsInnerFeatureApi(Feature *feature)
53 {
54 g_bmsInnerImpl.bundleInnerFeature = reinterpret_cast<BundleInnerFeature *>(feature);
55 return GET_IUNKNOWN(g_bmsInnerImpl);
56 }
57
Init()58 static void Init()
59 {
60 SamgrLite *sm = SAMGR_GetInstance();
61 if (sm == nullptr) {
62 return;
63 }
64 sm->RegisterFeature(BMS_SERVICE, reinterpret_cast<Feature *>(BundleInnerFeature::GetInstance()));
65 sm->RegisterFeatureApi(BMS_SERVICE, BMS_INNER_FEATURE,
66 GetBmsInnerFeatureApi(reinterpret_cast<Feature *>(BundleInnerFeature::GetInstance())));
67 HILOG_DEBUG(HILOG_MODULE_APP, "BundleMS inner feature start success");
68 }
69 APP_FEATURE_INIT(Init);
70
BundleInnerFeature()71 BundleInnerFeature::BundleInnerFeature() : identity_()
72 {
73 this->Feature::GetName = BundleInnerFeature::GetFeatureName;
74 this->Feature::OnInitialize = BundleInnerFeature::OnFeatureInitialize;
75 this->Feature::OnStop = BundleInnerFeature::OnFeatureStop;
76 this->Feature::OnMessage = BundleInnerFeature::OnFeatureMessage;
77 }
78
~BundleInnerFeature()79 BundleInnerFeature::~BundleInnerFeature() {}
80
GetFeatureName(Feature * feature)81 const char *BundleInnerFeature::GetFeatureName(Feature *feature)
82 {
83 (void) feature;
84 return BMS_INNER_FEATURE;
85 }
86
OnFeatureInitialize(Feature * feature,Service * parent,Identity identity)87 void BundleInnerFeature::OnFeatureInitialize(Feature *feature, Service *parent, Identity identity)
88 {
89 if (feature == nullptr) {
90 return;
91 }
92 (reinterpret_cast<BundleInnerFeature *>(feature))->identity_ = identity;
93 }
94
OnFeatureStop(Feature * feature,Identity identity)95 void BundleInnerFeature::OnFeatureStop(Feature *feature, Identity identity)
96 {
97 (void) feature;
98 (void) identity;
99 }
100
OnFeatureMessage(Feature * feature,Request * request)101 BOOL BundleInnerFeature::OnFeatureMessage(Feature *feature, Request *request)
102 {
103 if (feature == nullptr || request == nullptr) {
104 return FALSE;
105 }
106 ManagerService::GetInstance().ServiceMsgProcess(request);
107 return TRUE;
108 }
109
InstallInnerBundle(const uint8_t funcId,IpcIo * req,IpcIo * reply)110 uint8_t BundleInnerFeature::InstallInnerBundle(const uint8_t funcId, IpcIo *req, IpcIo *reply)
111 {
112 if ((req == nullptr) || (reply == nullptr)) {
113 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS InstallInnerBundle, request or reply is null");
114 return ERR_APPEXECFWK_OBJECT_NULL;
115 }
116 size_t length = 0;
117 char *reqPath = reinterpret_cast<char *>(ReadString(req, &length));
118 SvcIdentity svc;
119 if (!(ReadRemoteObject(req, &svc))) {
120 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS inner feature deserialize serviceId failed");
121 return ERR_APPEXECFWK_DESERIALIZATION_FAILED;
122 }
123
124 SvcIdentityInfo *info = reinterpret_cast<SvcIdentityInfo *>(AdapterMalloc(sizeof(SvcIdentityInfo)));
125 if (info == nullptr) {
126 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS inner feature malloc service info info failed");
127 return ERR_APPEXECFWK_SYSTEM_INTERNAL_ERROR;
128 }
129 uint8_t svcIdentityInfoRsp = GetSvcIdentityInfo(info, &svc, reqPath, req);
130 if (svcIdentityInfoRsp != ERR_OK) {
131 return svcIdentityInfoRsp;
132 }
133 Request request = {
134 .msgId = BUNDLE_INSTALLED,
135 .len = static_cast<int16>(sizeof(SvcIdentityInfo)),
136 .data = reinterpret_cast<void *>(info),
137 .msgValue = 0
138 };
139 int32 propRet = SAMGR_SendRequest(&(GetInstance()->identity_), &request, nullptr);
140 if (propRet != OHOS_SUCCESS) {
141 AdapterFree(info->path);
142 AdapterFree(info->svc);
143 AdapterFree(info);
144 return ERR_APPEXECFWK_INSTALL_FAILED_SEND_REQUEST_ERROR;
145 }
146 return ERR_OK;
147 }
148
GetSvcIdentityInfo(SvcIdentityInfo * info,const SvcIdentity * svc,const char * reqPath,IpcIo * req)149 uint8_t BundleInnerFeature::GetSvcIdentityInfo(SvcIdentityInfo *info, const SvcIdentity *svc, const char *reqPath,
150 IpcIo *req)
151 {
152 if (info == nullptr) {
153 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS inner feature malloc service info info failed");
154 return ERR_APPEXECFWK_SYSTEM_INTERNAL_ERROR;
155 }
156 info->path = Utils::Strdup(reqPath);
157 if (info->path == nullptr) {
158 AdapterFree(info);
159 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS inner feature malloc path failed");
160 return ERR_APPEXECFWK_SYSTEM_INTERNAL_ERROR;
161 }
162 info->bundleName = nullptr;
163 info->svc = reinterpret_cast<SvcIdentity *>(AdapterMalloc(sizeof(SvcIdentity)));
164 if (info->svc == nullptr) {
165 AdapterFree(info->path);
166 AdapterFree(info);
167 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS inner feature malloc serviceId failed");
168 return ERR_APPEXECFWK_SYSTEM_INTERNAL_ERROR;
169 }
170 *(info->svc) = *svc;
171 ReadInt32(req, &(info->installLocation));
172 info->keepData = false;
173
174 return ERR_OK;
175 }
176
UninstallInnerBundle(const uint8_t funcId,IpcIo * req,IpcIo * reply)177 uint8_t BundleInnerFeature::UninstallInnerBundle(const uint8_t funcId, IpcIo *req, IpcIo *reply)
178 {
179 if ((req == nullptr) || (reply == nullptr)) {
180 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS UninstallInnerBundle, request or reply is null");
181 return ERR_APPEXECFWK_OBJECT_NULL;
182 }
183 size_t length = 0;
184 char *bundleName = reinterpret_cast<char *>(ReadString(req, &length));
185 if (bundleName == nullptr) {
186 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS inner feature deserialize bundle name failed");
187 return ERR_APPEXECFWK_DESERIALIZATION_FAILED;
188 }
189 SvcIdentity svc;
190 if (!(ReadRemoteObject(req, &svc))) {
191 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS inner feature deserialize serviceId failed");
192 return ERR_APPEXECFWK_DESERIALIZATION_FAILED;
193 }
194 SvcIdentityInfo *info = reinterpret_cast<SvcIdentityInfo *>(AdapterMalloc(sizeof(SvcIdentityInfo)));
195 if (info == nullptr) {
196 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS inner feature malloc service info failed");
197 return ERR_APPEXECFWK_SYSTEM_INTERNAL_ERROR;
198 }
199 info->path = nullptr;
200 info->bundleName = Utils::Strdup(bundleName);
201 info->svc = reinterpret_cast<SvcIdentity *>(AdapterMalloc(sizeof(SvcIdentity)));
202 if (info->svc == nullptr) {
203 AdapterFree(info->bundleName);
204 AdapterFree(info);
205 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS inner feature malloc serviceId failed");
206 return ERR_APPEXECFWK_SYSTEM_INTERNAL_ERROR;
207 }
208 *(info->svc) = svc;
209 info->installLocation = 0;
210 ReadBool(req, &(info->keepData));
211 Request request = {
212 .msgId = BUNDLE_UNINSTALLED,
213 .len = static_cast<int16>(sizeof(SvcIdentityInfo)),
214 .data = reinterpret_cast<void *>(info),
215 .msgValue = 0
216 };
217 int32 propRet = SAMGR_SendRequest(&(GetInstance()->identity_), &request, nullptr);
218 if (propRet != OHOS_SUCCESS) {
219 AdapterFree(info->bundleName);
220 AdapterFree(info->svc);
221 AdapterFree(info);
222 return ERR_APPEXECFWK_UNINSTALL_FAILED_SEND_REQUEST_ERROR;
223 }
224 return ERR_OK;
225 }
226
227 #ifdef OHOS_DEBUG
SetExternalInstallMode(const uint8_t funcId,IpcIo * req,IpcIo * reply)228 uint8_t BundleInnerFeature::SetExternalInstallMode(const uint8_t funcId, IpcIo *req, IpcIo *reply)
229 {
230 if ((req == nullptr) || (reply == nullptr)) {
231 return ERR_APPEXECFWK_OBJECT_NULL;
232 }
233 bool mode;
234 ReadBool(req, &mode);
235 uint8_t errorCode = OHOS::ManagerService::GetInstance().SetExternalInstallMode(mode);
236 if (errorCode == OHOS_SUCCESS) {
237 WriteUint8(reply, static_cast<uint8_t>(OHOS_SUCCESS));
238 }
239 return errorCode;
240 }
241
SetInnerDebugMode(const uint8_t funcId,IpcIo * req,IpcIo * reply)242 uint8_t BundleInnerFeature::SetInnerDebugMode(const uint8_t funcId, IpcIo *req, IpcIo *reply)
243 {
244 if ((req == nullptr) || (reply == nullptr)) {
245 return ERR_APPEXECFWK_OBJECT_NULL;
246 }
247 bool mode;
248 ReadBool(req, &mode);
249 uint8_t errorCode = OHOS::ManagerService::GetInstance().SetDebugMode(mode);
250 if (errorCode == OHOS_SUCCESS) {
251 WriteUint8(reply, static_cast<uint8_t>(OHOS_SUCCESS));
252 }
253 return errorCode;
254 }
255
SetInnerSignMode(const uint8_t funcId,IpcIo * req,IpcIo * reply)256 uint8_t BundleInnerFeature::SetInnerSignMode(const uint8_t funcId, IpcIo *req, IpcIo *reply)
257 {
258 if ((req == nullptr) || (reply == nullptr)) {
259 return ERR_APPEXECFWK_OBJECT_NULL;
260 }
261 bool mode;
262 ReadBool(req, &mode);
263 uint8_t errorCode = OHOS::ManagerService::GetInstance().SetSignMode(mode);
264 if (errorCode == OHOS_SUCCESS) {
265 WriteUint8(reply, static_cast<uint8_t>(OHOS_SUCCESS));
266 }
267 return errorCode;
268 }
269 #endif
270
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)271 int32 BundleInnerFeature::Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
272 {
273 if (req == nullptr) {
274 return ERR_APPEXECFWK_OBJECT_NULL;
275 }
276 WriteUint8(reply, static_cast<uint8_t>(funcId));
277 uint8_t ret = OHOS_SUCCESS;
278 #ifdef OHOS_DEBUG
279 if ((funcId >= BMS_INNER_BEGIN) && (funcId < BMS_CMD_END)) {
280 #else
281 if ((funcId >= BMS_INNER_BEGIN) && (funcId <= UNINSTALL)) {
282 #endif
283 ret = BundleMsInvokeFuc[funcId - BMS_INNER_BEGIN](funcId, req, reply);
284 } else {
285 ret = ERR_APPEXECFWK_COMMAND_ERROR;
286 }
287
288 if (ret != OHOS_SUCCESS) {
289 WriteUint8(reply, ret);
290 }
291 return ret;
292 }
293 } // namespace OHOS
294