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 "ipc/installd_host.h"
17
18 #include "app_log_wrapper.h"
19 #include "appexecfwk_errors.h"
20 #include "bundle_constants.h"
21 #include "parcel_macro.h"
22 #include "string_ex.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
InstalldHost()26 InstalldHost::InstalldHost()
27 {
28 init();
29 APP_LOGI("installd host instance is created");
30 }
31
~InstalldHost()32 InstalldHost::~InstalldHost()
33 {
34 APP_LOGI("installd host instance is destroyed");
35 }
36
init()37 void InstalldHost::init()
38 {
39 funcMap_.emplace(IInstalld::Message::CREATE_BUNDLE_DIR, &InstalldHost::HandleCreateBundleDir);
40 funcMap_.emplace(IInstalld::Message::EXTRACT_MODULE_FILES, &InstalldHost::HandleExtractModuleFiles);
41 funcMap_.emplace(IInstalld::Message::RENAME_MODULE_DIR, &InstalldHost::HandleRenameModuleDir);
42 funcMap_.emplace(IInstalld::Message::CREATE_BUNDLE_DATA_DIR, &InstalldHost::HandleCreateBundleDataDir);
43 funcMap_.emplace(IInstalld::Message::REMOVE_BUNDLE_DATA_DIR, &InstalldHost::HandleRemoveBundleDataDir);
44 funcMap_.emplace(IInstalld::Message::CREATE_MODULE_DATA_DIR, &InstalldHost::HandleCreateModuleDataDir);
45 funcMap_.emplace(IInstalld::Message::REMOVE_MODULE_DATA_DIR, &InstalldHost::HandleRemoveModuleDataDir);
46 funcMap_.emplace(IInstalld::Message::CLEAN_BUNDLE_DATA_DIR, &InstalldHost::HandleCleanBundleDataDir);
47 funcMap_.emplace(IInstalld::Message::SET_DIR_APL, &InstalldHost::HandleSetDirApl);
48 funcMap_.emplace(IInstalld::Message::REMOVE_DIR, &InstalldHost::HandleRemoveDir);
49 funcMap_.emplace(IInstalld::Message::GET_BUNDLE_STATS, &InstalldHost::HandleGetBundleStats);
50 funcMap_.emplace(IInstalld::Message::GET_BUNDLE_CACHE_PATH, &InstalldHost::HandleGetBundleCachePath);
51 }
52
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)53 int InstalldHost::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
54 {
55 APP_LOGD(
56 "installd host receives message from client, code = %{public}d, flags = %{public}d", code, option.GetFlags());
57 std::u16string descripter = InstalldHost::GetDescriptor();
58 std::u16string remoteDescripter = data.ReadInterfaceToken();
59 if (descripter != remoteDescripter) {
60 APP_LOGE("installd host fail to write reply message due to the reply is nullptr");
61 return OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
62 }
63 bool result = true;
64 APP_LOGD("funcMap_ size is %{public}d", static_cast<int32_t>(funcMap_.size()));
65 if (funcMap_.find(code) != funcMap_.end() && funcMap_[code] != nullptr) {
66 result = (this->*funcMap_[code])(data, reply);
67 } else {
68 APP_LOGW("installd host receives unknown code, code = %{public}u", code);
69 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
70 }
71 APP_LOGD("installd host finish to process message from client");
72 return result ? NO_ERROR : OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
73 }
74
HandleCreateBundleDir(MessageParcel & data,MessageParcel & reply)75 bool InstalldHost::HandleCreateBundleDir(MessageParcel &data, MessageParcel &reply)
76 {
77 std::string bundleDir = Str16ToStr8(data.ReadString16());
78 APP_LOGI("bundleName %{public}s", bundleDir.c_str());
79 ErrCode result = CreateBundleDir(bundleDir);
80 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
81 return true;
82 }
83
HandleExtractModuleFiles(MessageParcel & data,MessageParcel & reply)84 bool InstalldHost::HandleExtractModuleFiles(MessageParcel &data, MessageParcel &reply)
85 {
86 std::string srcModulePath = Str16ToStr8(data.ReadString16());
87 std::string targetPath = Str16ToStr8(data.ReadString16());
88 std::string targetSoPath = Str16ToStr8(data.ReadString16());
89 std::string cpuAbi = Str16ToStr8(data.ReadString16());
90 APP_LOGI("extract module %{public}s", targetPath.c_str());
91 ErrCode result = ExtractModuleFiles(srcModulePath, targetPath, targetSoPath, cpuAbi);
92 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
93 return true;
94 }
95
HandleRenameModuleDir(MessageParcel & data,MessageParcel & reply)96 bool InstalldHost::HandleRenameModuleDir(MessageParcel &data, MessageParcel &reply)
97 {
98 std::string oldPath = Str16ToStr8(data.ReadString16());
99 std::string newPath = Str16ToStr8(data.ReadString16());
100 APP_LOGI("rename moduleDir %{public}s", oldPath.c_str());
101 ErrCode result = RenameModuleDir(oldPath, newPath);
102 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
103 return true;
104 }
105
HandleCreateBundleDataDir(MessageParcel & data,MessageParcel & reply)106 bool InstalldHost::HandleCreateBundleDataDir(MessageParcel &data, MessageParcel &reply)
107 {
108 std::string bundleDir = Str16ToStr8(data.ReadString16());
109 int userid = data.ReadInt32();
110 int uid = data.ReadInt32();
111 int gid = data.ReadInt32();
112 std::string apl = Str16ToStr8(data.ReadString16());
113 bool onlyOneUser = data.ReadBool();
114 ErrCode result = CreateBundleDataDir(bundleDir, userid, uid, gid, apl, onlyOneUser);
115 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
116 return true;
117 }
118
HandleCreateModuleDataDir(MessageParcel & data,MessageParcel & reply)119 bool InstalldHost::HandleCreateModuleDataDir(MessageParcel &data, MessageParcel &reply)
120 {
121 std::string bundleDir = Str16ToStr8(data.ReadString16());
122 std::vector<std::string> abilityDirs;
123 if (!data.ReadStringVector(&abilityDirs)) {
124 return false;
125 }
126 int uid = data.ReadInt32();
127 int gid = data.ReadInt32();
128 ErrCode result = CreateModuleDataDir(bundleDir, abilityDirs, uid, gid);
129 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
130 return true;
131 }
132
HandleRemoveBundleDataDir(MessageParcel & data,MessageParcel & reply)133 bool InstalldHost::HandleRemoveBundleDataDir(MessageParcel &data, MessageParcel &reply)
134 {
135 std::string bundleName = Str16ToStr8(data.ReadString16());
136 int userid = data.ReadInt32();
137 ErrCode result = RemoveBundleDataDir(bundleName, userid);
138 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
139 return true;
140 }
141
HandleRemoveModuleDataDir(MessageParcel & data,MessageParcel & reply)142 bool InstalldHost::HandleRemoveModuleDataDir(MessageParcel &data, MessageParcel &reply)
143 {
144 std::string moduleNmae = Str16ToStr8(data.ReadString16());
145 int userid = data.ReadInt32();
146 ErrCode result = RemoveModuleDataDir(moduleNmae, userid);
147 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
148 return true;
149 }
150
HandleRemoveDir(MessageParcel & data,MessageParcel & reply)151 bool InstalldHost::HandleRemoveDir(MessageParcel &data, MessageParcel &reply)
152 {
153 std::string removedDir = Str16ToStr8(data.ReadString16());
154 ErrCode result = RemoveDir(removedDir);
155 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
156 return true;
157 }
158
HandleCleanBundleDataDir(MessageParcel & data,MessageParcel & reply)159 bool InstalldHost::HandleCleanBundleDataDir(MessageParcel &data, MessageParcel &reply)
160 {
161 std::string bundleDir = Str16ToStr8(data.ReadString16());
162 ErrCode result = CleanBundleDataDir(bundleDir);
163 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
164 return true;
165 }
166
HandleGetBundleStats(MessageParcel & data,MessageParcel & reply)167 bool InstalldHost::HandleGetBundleStats(MessageParcel &data, MessageParcel &reply)
168 {
169 std::string bundleName = Str16ToStr8(data.ReadString16());
170 int32_t userId = data.ReadInt32();
171 std::vector<int64_t> bundleStats;
172 ErrCode result = GetBundleStats(bundleName, userId, bundleStats);
173 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
174 if (!reply.WriteInt64Vector(bundleStats)) {
175 APP_LOGE("HandleGetBundleStats write failed");
176 return false;
177 }
178 return true;
179 }
180
HandleSetDirApl(MessageParcel & data,MessageParcel & reply)181 bool InstalldHost::HandleSetDirApl(MessageParcel &data, MessageParcel &reply)
182 {
183 std::string dataDir = Str16ToStr8(data.ReadString16());
184 std::string bundleName = Str16ToStr8(data.ReadString16());
185 std::string apl = Str16ToStr8(data.ReadString16());
186 ErrCode result = SetDirApl(dataDir, bundleName, apl);
187 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
188 return true;
189 }
190
HandleGetBundleCachePath(MessageParcel & data,MessageParcel & reply)191 bool InstalldHost::HandleGetBundleCachePath(MessageParcel &data, MessageParcel &reply)
192 {
193 std::string dir = Str16ToStr8(data.ReadString16());
194 std::vector<std::string> cachePath;
195 ErrCode result = GetBundleCachePath(dir, cachePath);
196 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
197 if (!reply.WriteStringVector(cachePath)) {
198 APP_LOGE("fail to GetBundleCachePath from reply");
199 return false;
200 }
201 return true;
202 }
203 } // namespace AppExecFwk
204 } // namespace OHOS
205