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 "ipc/storage_daemon_stub.h"
17
18 #include <cinttypes>
19 #include "ipc/storage_daemon_ipc_interface_code.h"
20 #include "storage_service_errno.h"
21 #include "storage_service_log.h"
22 #include "string_ex.h"
23 #include "utils/storage_radar.h"
24
25 namespace OHOS {
26 namespace StorageDaemon {
27 using namespace std;
28 const unsigned int UPDATE_RADAR_STATISTIC_INTERVAL_SECONDS = 300;
29 const unsigned int RADAR_REPORT_STATISTIC_INTERVAL_MINUTES = 1440;
30 const unsigned int USER0ID = 0;
31 const unsigned int USER100ID = 100;
32 const unsigned int RADAR_STATISTIC_THREAD_WAIT_SECONDS = 60;
33
GetUserStatistics(const uint32_t userId)34 std::map<uint32_t, RadarStatisticInfo>::iterator StorageDaemonStub::GetUserStatistics(const uint32_t userId)
35 {
36 auto it = opStatistics_.find(userId);
37 if (it != opStatistics_.end()) {
38 return it;
39 }
40 RadarStatisticInfo radarInfo = { 0 };
41 return opStatistics_.insert(make_pair(userId, radarInfo)).first;
42 }
43
GetTempStatistics(std::map<uint32_t,RadarStatisticInfo> & statistics)44 void StorageDaemonStub::GetTempStatistics(std::map<uint32_t, RadarStatisticInfo> &statistics)
45 {
46 std::lock_guard<std::mutex> lock(mutex_);
47 statistics.insert(opStatistics_.begin(), opStatistics_.end());
48 opStatistics_.clear();
49 }
50
StorageRadarThd(void)51 void StorageDaemonStub::StorageRadarThd(void)
52 {
53 // report radar statistics when restart
54 std::unique_lock<std::mutex> lock(onRadarReportLock_);
55 if (execRadarReportCon_.wait_for(lock, std::chrono::seconds(RADAR_STATISTIC_THREAD_WAIT_SECONDS),
56 [this] { return this->stopRadarReport_.load(); })) {
57 LOGI("Storage statistic radar exit.");
58 return;
59 }
60 lock.unlock();
61 LOGI("Storage statistic thread start.");
62 StorageStatisticRadar::GetInstance().CreateStatisticFile();
63 std::map<uint32_t, RadarStatisticInfo> opStatisticsTemp;
64 StorageStatisticRadar::GetInstance().ReadStatisticFile(opStatisticsTemp);
65 if (!opStatisticsTemp.empty()) {
66 for (auto ele : opStatisticsTemp) {
67 StorageService::StorageRadar::ReportStatistics(ele.first, ele.second);
68 }
69 StorageStatisticRadar::GetInstance().CleanStatisticFile();
70 }
71 lastRadarReportTime_ = std::chrono::system_clock::now();
72 while (!stopRadarReport_.load()) {
73 std::unique_lock<std::mutex> lock(onRadarReportLock_);
74 if (execRadarReportCon_.wait_for(lock, std::chrono::seconds(UPDATE_RADAR_STATISTIC_INTERVAL_SECONDS),
75 [this] { return this->stopRadarReport_.load(); })) {
76 LOGI("Storage statistic radar exit.");
77 return;
78 }
79 std::chrono::time_point<std::chrono::system_clock> nowTime = std::chrono::system_clock::now();
80 int64_t intervalMinutes =
81 std::chrono::duration_cast<std::chrono::minutes>(nowTime - lastRadarReportTime_).count();
82 if ((intervalMinutes > RADAR_REPORT_STATISTIC_INTERVAL_MINUTES) && !opStatistics_.empty()) {
83 LOGI("Storage statistic report, intervalMinutes:%{public}" PRId64, intervalMinutes);
84 opStatisticsTemp.clear();
85 GetTempStatistics(opStatisticsTemp);
86 for (auto ele : opStatisticsTemp) {
87 StorageService::StorageRadar::ReportStatistics(ele.first, ele.second);
88 }
89 lastRadarReportTime_ = std::chrono::system_clock::now();
90 StorageStatisticRadar::GetInstance().CleanStatisticFile();
91 continue;
92 }
93 if (!isNeedUpdateRadarFile_) {
94 LOGI("Storage statistic not need update.");
95 continue;
96 }
97 LOGI("Storage statistic update, intervalMinutes:%{public}" PRId64, intervalMinutes);
98 isNeedUpdateRadarFile_ = false;
99 StorageStatisticRadar::GetInstance().UpdateStatisticFile(opStatistics_);
100 }
101 }
102
StorageDaemonStub()103 StorageDaemonStub::StorageDaemonStub()
104 {
105 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::SHUTDOWN)] =
106 &StorageDaemonStub::HandleShutdown;
107 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::CHECK)] =
108 &StorageDaemonStub::HandleCheck;
109 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT)] =
110 &StorageDaemonStub::HandleMount;
111 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT)] =
112 &StorageDaemonStub::HandleUMount;
113 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::PARTITION)] =
114 &StorageDaemonStub::HandlePartition;
115 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::FORMAT)] =
116 &StorageDaemonStub::HandleFormat;
117 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_VOL_DESC)] =
118 &StorageDaemonStub::HandleSetVolDesc;
119 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::PREPARE_USER_DIRS)] =
120 &StorageDaemonStub::HandlePrepareUserDirs;
121 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::DESTROY_USER_DIRS)] =
122 &StorageDaemonStub::HandleDestroyUserDirs;
123 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::START_USER)] =
124 &StorageDaemonStub::HandleStartUser;
125 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::STOP_USER)] =
126 &StorageDaemonStub::HandleStopUser;
127 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_KEY)] =
128 &StorageDaemonStub::HandleInitGlobalKey;
129 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_USER_KEYS)] =
130 &StorageDaemonStub::HandleInitGlobalUserKeys;
131 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_USER_KEYS)] =
132 &StorageDaemonStub::HandleGenerateUserKeys;
133 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_USER_KEYS)] =
134 &StorageDaemonStub::HandleDeleteUserKeys;
135 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_USER_AUTH)] =
136 &StorageDaemonStub::HandleUpdateUserAuth;
137 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::ACTIVE_USER_KEY)] =
138 &StorageDaemonStub::HandleActiveUserKey;
139 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::INACTIVE_USER_KEY)] =
140 &StorageDaemonStub::HandleInactiveUserKey;
141 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::LOCK_USER_SCREEN)] =
142 &StorageDaemonStub::HandleLockUserScreen;
143 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UNLOCK_USER_SCREEN)] =
144 &StorageDaemonStub::HandleUnlockUserScreen;
145 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::LOCK_SCREEN_STATUS)] =
146 &StorageDaemonStub::HandleGetLockScreenStatus;
147 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_KEY_CONTEXT)] =
148 &StorageDaemonStub::HandleUpdateKeyContext;
149 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_CRYPTO_PATH_AGAIN)] =
150 &StorageDaemonStub::HandleMountCryptoPathAgain;
151 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_SHARE_FILE)] =
152 &StorageDaemonStub::HandleCreateShareFile;
153 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_SHARE_FILE)] =
154 &StorageDaemonStub::HandleDeleteShareFile;
155 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_BUNDLE_QUOTA)] =
156 &StorageDaemonStub::HandleSetBundleQuota;
157 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_SPACE)] =
158 &StorageDaemonStub::HandleGetOccupiedSpace;
159 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_MEM_PARA)] =
160 &StorageDaemonStub::HandleUpdateMemoryPara;
161 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_BUNDLE_STATS_INCREASE)] =
162 &StorageDaemonStub::HandleGetBundleStatsForIncrease;
163 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::GENERATE_APP_KEY)] =
164 &StorageDaemonStub::HandleGenerateAppkey;
165 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_APP_KEY)] =
166 &StorageDaemonStub::HandleDeleteAppkey;
167 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_DFS_DOCS)] =
168 &StorageDaemonStub::HandleMountDfsDocs;
169 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT_DFS_DOCS)] =
170 &StorageDaemonStub::HandleUMountDfsDocs;
171 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_FILE_ENCRYPT_STATUS)] =
172 &StorageDaemonStub::HandleGetFileEncryptStatus;
173 opToInterfaceMap_[static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_USER_NEED_ACTIVE_STATUS)] =
174 &StorageDaemonStub::HandleGetUserNeedActiveStatus;
175 callRadarStatisticReportThread_ = std::thread([this]() { StorageRadarThd(); });
176 }
177
~StorageDaemonStub()178 StorageDaemonStub::~StorageDaemonStub()
179 {
180 std::unique_lock<std::mutex> lock(onRadarReportLock_);
181 stopRadarReport_ = true;
182 execRadarReportCon_.notify_one();
183 lock.unlock();
184 if (callRadarStatisticReportThread_.joinable()) {
185 callRadarStatisticReportThread_.join();
186 }
187 }
188
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)189 int32_t StorageDaemonStub::OnRemoteRequest(uint32_t code,
190 MessageParcel &data,
191 MessageParcel &reply,
192 MessageOption &option)
193 {
194 if (data.ReadInterfaceToken() != GetDescriptor()) {
195 return E_PERMISSION_DENIED;
196 }
197 switch (code) {
198 case static_cast<uint32_t>(StorageDaemonInterfaceCode::SHUTDOWN):
199 case static_cast<uint32_t>(StorageDaemonInterfaceCode::CHECK):
200 case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT):
201 case static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT):
202 case static_cast<uint32_t>(StorageDaemonInterfaceCode::PARTITION):
203 case static_cast<uint32_t>(StorageDaemonInterfaceCode::FORMAT):
204 case static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_VOL_DESC):
205 return OnRemoteRequestForBase(code, data, reply);
206 case static_cast<uint32_t>(StorageDaemonInterfaceCode::PREPARE_USER_DIRS):
207 case static_cast<uint32_t>(StorageDaemonInterfaceCode::DESTROY_USER_DIRS):
208 case static_cast<uint32_t>(StorageDaemonInterfaceCode::START_USER):
209 case static_cast<uint32_t>(StorageDaemonInterfaceCode::STOP_USER):
210 case static_cast<uint32_t>(StorageDaemonInterfaceCode::COMPLETE_ADD_USER):
211 case static_cast<uint32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_KEY):
212 case static_cast<uint32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_USER_KEYS):
213 case static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_USER_KEYS):
214 case static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_USER_KEYS):
215 case static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_USER_AUTH):
216 case static_cast<uint32_t>(StorageDaemonInterfaceCode::ACTIVE_USER_KEY):
217 case static_cast<uint32_t>(StorageDaemonInterfaceCode::INACTIVE_USER_KEY):
218 case static_cast<uint32_t>(StorageDaemonInterfaceCode::LOCK_USER_SCREEN):
219 case static_cast<uint32_t>(StorageDaemonInterfaceCode::UNLOCK_USER_SCREEN):
220 return OnRemoteRequestForUser(code, data, reply);
221 case static_cast<uint32_t>(StorageDaemonInterfaceCode::LOCK_SCREEN_STATUS):
222 case static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_KEY_CONTEXT):
223 case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_CRYPTO_PATH_AGAIN):
224 case static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_SHARE_FILE):
225 case static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_SHARE_FILE):
226 case static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_BUNDLE_QUOTA):
227 case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_SPACE):
228 case static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_MEM_PARA):
229 case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_BUNDLE_STATS_INCREASE):
230 case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_DFS_DOCS):
231 case static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT_DFS_DOCS):
232 case static_cast<uint32_t>(StorageDaemonInterfaceCode::GENERATE_APP_KEY):
233 case static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_APP_KEY):
234 case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_FILE_ENCRYPT_STATUS):
235 case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_USER_NEED_ACTIVE_STATUS):
236 return OnRemoteRequestForApp(code, data, reply);
237 default:
238 LOGE("Cannot response request %d: unknown tranction", code);
239 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
240 }
241 }
242
OnRemoteRequestForBase(uint32_t code,MessageParcel & data,MessageParcel & reply)243 int32_t StorageDaemonStub::OnRemoteRequestForBase(uint32_t code, MessageParcel &data, MessageParcel &reply)
244 {
245 switch (code) {
246 case static_cast<uint32_t>(StorageDaemonInterfaceCode::SHUTDOWN):
247 return HandleShutdown(data, reply);
248 case static_cast<uint32_t>(StorageDaemonInterfaceCode::CHECK):
249 return HandleCheck(data, reply);
250 case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT):
251 return HandleMount(data, reply);
252 case static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT):
253 return HandleUMount(data, reply);
254 case static_cast<uint32_t>(StorageDaemonInterfaceCode::PARTITION):
255 return HandlePartition(data, reply);
256 case static_cast<uint32_t>(StorageDaemonInterfaceCode::FORMAT):
257 return HandleFormat(data, reply);
258 case static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_VOL_DESC):
259 return HandleSetVolDesc(data, reply);
260 default:
261 LOGE("Cannot response request %d: unknown tranction", code);
262 return E_SYS_ERR;
263 }
264 }
OnRemoteRequestForUser(uint32_t code,MessageParcel & data,MessageParcel & reply)265 int32_t StorageDaemonStub::OnRemoteRequestForUser(uint32_t code, MessageParcel &data, MessageParcel &reply)
266 {
267 switch (code) {
268 case static_cast<uint32_t>(StorageDaemonInterfaceCode::PREPARE_USER_DIRS):
269 return HandlePrepareUserDirs(data, reply);
270 case static_cast<uint32_t>(StorageDaemonInterfaceCode::DESTROY_USER_DIRS):
271 return HandleDestroyUserDirs(data, reply);
272 case static_cast<uint32_t>(StorageDaemonInterfaceCode::START_USER):
273 return HandleStartUser(data, reply);
274 case static_cast<uint32_t>(StorageDaemonInterfaceCode::STOP_USER):
275 return HandleStopUser(data, reply);
276 case static_cast<uint32_t>(StorageDaemonInterfaceCode::COMPLETE_ADD_USER):
277 return HandleCompleteAddUser(data, reply);
278 case static_cast<uint32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_KEY):
279 return HandleInitGlobalKey(data, reply);
280 case static_cast<uint32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_USER_KEYS):
281 return HandleInitGlobalUserKeys(data, reply);
282 case static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_USER_KEYS):
283 return HandleGenerateUserKeys(data, reply);
284 case static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_USER_KEYS):
285 return HandleDeleteUserKeys(data, reply);
286 case static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_USER_AUTH):
287 return HandleUpdateUserAuth(data, reply);
288 case static_cast<uint32_t>(StorageDaemonInterfaceCode::ACTIVE_USER_KEY):
289 return HandleActiveUserKey(data, reply);
290 case static_cast<uint32_t>(StorageDaemonInterfaceCode::INACTIVE_USER_KEY):
291 return HandleInactiveUserKey(data, reply);
292 case static_cast<uint32_t>(StorageDaemonInterfaceCode::LOCK_USER_SCREEN):
293 return HandleLockUserScreen(data, reply);
294 case static_cast<uint32_t>(StorageDaemonInterfaceCode::UNLOCK_USER_SCREEN):
295 return HandleUnlockUserScreen(data, reply);
296 default:
297 LOGE("Cannot response request %d: unknown tranction", code);
298 return E_SYS_ERR;
299 }
300 }
OnRemoteRequestForApp(uint32_t code,MessageParcel & data,MessageParcel & reply)301 int32_t StorageDaemonStub::OnRemoteRequestForApp(uint32_t code, MessageParcel &data, MessageParcel &reply)
302 {
303 switch (code) {
304 case static_cast<uint32_t>(StorageDaemonInterfaceCode::LOCK_SCREEN_STATUS):
305 return HandleGetLockScreenStatus(data, reply);
306 case static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_KEY_CONTEXT):
307 return HandleUpdateKeyContext(data, reply);
308 case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_CRYPTO_PATH_AGAIN):
309 return HandleMountCryptoPathAgain(data, reply);
310 case static_cast<uint32_t>(StorageDaemonInterfaceCode::CREATE_SHARE_FILE):
311 return HandleCreateShareFile(data, reply);
312 case static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_SHARE_FILE):
313 return HandleDeleteShareFile(data, reply);
314 case static_cast<uint32_t>(StorageDaemonInterfaceCode::SET_BUNDLE_QUOTA):
315 return HandleSetBundleQuota(data, reply);
316 case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_SPACE):
317 return HandleGetOccupiedSpace(data, reply);
318 case static_cast<uint32_t>(StorageDaemonInterfaceCode::UPDATE_MEM_PARA):
319 return HandleUpdateMemoryPara(data, reply);
320 case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_BUNDLE_STATS_INCREASE):
321 return HandleGetBundleStatsForIncrease(data, reply);
322 case static_cast<uint32_t>(StorageDaemonInterfaceCode::MOUNT_DFS_DOCS):
323 return HandleMountDfsDocs(data, reply);
324 case static_cast<uint32_t>(StorageDaemonInterfaceCode::UMOUNT_DFS_DOCS):
325 return HandleUMountDfsDocs(data, reply);
326 case static_cast<uint32_t>(StorageDaemonInterfaceCode::GENERATE_APP_KEY):
327 return HandleGenerateAppkey(data, reply);
328 case static_cast<uint32_t>(StorageDaemonInterfaceCode::DELETE_APP_KEY):
329 return HandleDeleteAppkey(data, reply);
330 case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_FILE_ENCRYPT_STATUS):
331 return HandleGetFileEncryptStatus(data, reply);
332 case static_cast<uint32_t>(StorageDaemonInterfaceCode::GET_USER_NEED_ACTIVE_STATUS):
333 return HandleGetUserNeedActiveStatus(data, reply);
334 default:
335 LOGE("Cannot response request %d: unknown tranction", code);
336 return E_SYS_ERR;
337 }
338 }
339
HandleShutdown(MessageParcel & data,MessageParcel & reply)340 int32_t StorageDaemonStub::HandleShutdown(MessageParcel &data, MessageParcel &reply)
341 {
342 Shutdown();
343 return E_OK;
344 }
345
HandleMount(MessageParcel & data,MessageParcel & reply)346 int32_t StorageDaemonStub::HandleMount(MessageParcel &data, MessageParcel &reply)
347 {
348 std::string volId = data.ReadString();
349 uint32_t flags = data.ReadUint32();
350
351 int err = Mount(volId, flags);
352 if (!reply.WriteInt32(err)) {
353 return E_WRITE_REPLY_ERR;
354 }
355
356 return E_OK;
357 }
358
HandleUMount(MessageParcel & data,MessageParcel & reply)359 int32_t StorageDaemonStub::HandleUMount(MessageParcel &data, MessageParcel &reply)
360 {
361 std::string volId = data.ReadString();
362
363 int err = UMount(volId);
364 if (!reply.WriteInt32(err)) {
365 return E_WRITE_REPLY_ERR;
366 }
367
368 return E_OK;
369 }
370
HandleCheck(MessageParcel & data,MessageParcel & reply)371 int32_t StorageDaemonStub::HandleCheck(MessageParcel &data, MessageParcel &reply)
372 {
373 std::string volId = data.ReadString();
374
375 int err = Check(volId);
376 if (!reply.WriteInt32(err)) {
377 return E_WRITE_REPLY_ERR;
378 }
379
380 return E_OK;
381 }
382
HandleFormat(MessageParcel & data,MessageParcel & reply)383 int32_t StorageDaemonStub::HandleFormat(MessageParcel &data, MessageParcel &reply)
384 {
385 std::string volId = data.ReadString();
386 std::string fsType = data.ReadString();
387
388 int err = Format(volId, fsType);
389 if (!reply.WriteInt32(err)) {
390 return E_WRITE_REPLY_ERR;
391 }
392
393 return E_OK;
394 }
395
HandlePartition(MessageParcel & data,MessageParcel & reply)396 int32_t StorageDaemonStub::HandlePartition(MessageParcel &data, MessageParcel &reply)
397 {
398 std::string volId = data.ReadString();
399 int32_t type = data.ReadInt32();
400
401 int err = Partition(volId, type);
402 if (!reply.WriteInt32(err)) {
403 return E_WRITE_REPLY_ERR;
404 }
405
406 return E_OK;
407 }
408
HandleSetVolDesc(MessageParcel & data,MessageParcel & reply)409 int32_t StorageDaemonStub::HandleSetVolDesc(MessageParcel &data, MessageParcel &reply)
410 {
411 std::string volId = data.ReadString();
412 std::string description = data.ReadString();
413
414 int err = SetVolumeDescription(volId, description);
415 if (!reply.WriteInt32(err)) {
416 return E_WRITE_REPLY_ERR;
417 }
418
419 return E_OK;
420 }
421
HandlePrepareUserDirs(MessageParcel & data,MessageParcel & reply)422 int32_t StorageDaemonStub::HandlePrepareUserDirs(MessageParcel &data, MessageParcel &reply)
423 {
424 std::lock_guard<std::mutex> lock(mutex_);
425 int32_t userId = data.ReadInt32();
426 uint32_t flags = data.ReadUint32();
427 auto it = GetUserStatistics(userId);
428 isNeedUpdateRadarFile_ = true;
429 int err = PrepareUserDirs(userId, flags);
430 if (err != E_OK) {
431 it->second.userAddFailCount++;
432 } else {
433 it->second.userAddSuccCount++;
434 }
435 if (!reply.WriteInt32(err)) {
436 return E_WRITE_REPLY_ERR;
437 }
438 return E_OK;
439 }
440
HandleDestroyUserDirs(MessageParcel & data,MessageParcel & reply)441 int32_t StorageDaemonStub::HandleDestroyUserDirs(MessageParcel &data, MessageParcel &reply)
442 {
443 std::lock_guard<std::mutex> lock(mutex_);
444 int32_t userId = data.ReadInt32();
445 uint32_t flags = data.ReadUint32();
446 auto it = GetUserStatistics(userId);
447 isNeedUpdateRadarFile_ = true;
448 int err = DestroyUserDirs(userId, flags);
449 if (err != E_OK) {
450 it->second.userRemoveFailCount++;
451 } else {
452 it->second.userRemoveSuccCount++;
453 }
454 if (!reply.WriteInt32(err)) {
455 return E_WRITE_REPLY_ERR;
456 }
457 return E_OK;
458 }
459
HandleStartUser(MessageParcel & data,MessageParcel & reply)460 int32_t StorageDaemonStub::HandleStartUser(MessageParcel &data, MessageParcel &reply)
461 {
462 int32_t userId = data.ReadInt32();
463 auto it = GetUserStatistics(userId);
464 isNeedUpdateRadarFile_ = true;
465 int32_t err = StartUser(userId);
466 if (err != E_OK) {
467 it->second.userStartFailCount++;
468 } else {
469 it->second.userStartSuccCount++;
470 }
471 if (!reply.WriteInt32(err)) {
472 return E_WRITE_REPLY_ERR;
473 }
474 return E_OK;
475 }
476
HandleStopUser(MessageParcel & data,MessageParcel & reply)477 int32_t StorageDaemonStub::HandleStopUser(MessageParcel &data, MessageParcel &reply)
478 {
479 int32_t userId = data.ReadInt32();
480 auto it = GetUserStatistics(userId);
481 isNeedUpdateRadarFile_ = true;
482 int32_t err = StopUser(userId);
483 if (err != E_OK) {
484 it->second.userStopFailCount++;
485 } else {
486 it->second.userStopSuccCount++;
487 }
488 if (!reply.WriteInt32(err)) {
489 return E_WRITE_REPLY_ERR;
490 }
491 return E_OK;
492 }
493
HandleCompleteAddUser(MessageParcel & data,MessageParcel & reply)494 int32_t StorageDaemonStub::HandleCompleteAddUser(MessageParcel &data, MessageParcel &reply)
495 {
496 int32_t userId = data.ReadInt32();
497
498 int32_t err = CompleteAddUser(userId);
499 if (!reply.WriteInt32(err)) {
500 return E_WRITE_REPLY_ERR;
501 }
502
503 return E_OK;
504 }
505
HandleInitGlobalKey(MessageParcel & data,MessageParcel & reply)506 int32_t StorageDaemonStub::HandleInitGlobalKey(MessageParcel &data, MessageParcel &reply)
507 {
508 std::lock_guard<std::mutex> lock(mutex_);
509 auto it = GetUserStatistics(USER0ID);
510 isNeedUpdateRadarFile_ = true;
511 int err = InitGlobalKey();
512 if (err != E_OK) {
513 it->second.keyLoadFailCount++;
514 } else {
515 it->second.keyLoadSuccCount++;
516 }
517 if (!reply.WriteInt32(err)) {
518 return E_WRITE_REPLY_ERR;
519 }
520 return E_OK;
521 }
522
HandleInitGlobalUserKeys(MessageParcel & data,MessageParcel & reply)523 int32_t StorageDaemonStub::HandleInitGlobalUserKeys(MessageParcel &data, MessageParcel &reply)
524 {
525 std::lock_guard<std::mutex> lock(mutex_);
526 auto it = GetUserStatistics(USER100ID);
527 isNeedUpdateRadarFile_ = true;
528 int err = InitGlobalUserKeys();
529 if (err != E_OK) {
530 it->second.keyLoadFailCount++;
531 } else {
532 it->second.keyLoadSuccCount++;
533 }
534 if (!reply.WriteInt32(err)) {
535 return E_WRITE_REPLY_ERR;
536 }
537 return E_OK;
538 }
539
HandleGenerateUserKeys(MessageParcel & data,MessageParcel & reply)540 int32_t StorageDaemonStub::HandleGenerateUserKeys(MessageParcel &data, MessageParcel &reply)
541 {
542 uint32_t userId = data.ReadUint32();
543 uint32_t flags = data.ReadUint32();
544
545 int err = GenerateUserKeys(userId, flags);
546 if (!reply.WriteInt32(err)) {
547 return E_WRITE_REPLY_ERR;
548 }
549
550 return E_OK;
551 }
552
HandleDeleteUserKeys(MessageParcel & data,MessageParcel & reply)553 int32_t StorageDaemonStub::HandleDeleteUserKeys(MessageParcel &data, MessageParcel &reply)
554 {
555 uint32_t userId = data.ReadUint32();
556
557 int err = DeleteUserKeys(userId);
558 if (!reply.WriteInt32(err)) {
559 return E_WRITE_REPLY_ERR;
560 }
561
562 return E_OK;
563 }
564
HandleUpdateUserAuth(MessageParcel & data,MessageParcel & reply)565 int32_t StorageDaemonStub::HandleUpdateUserAuth(MessageParcel &data, MessageParcel &reply)
566 {
567 uint32_t userId = data.ReadUint32();
568 uint64_t secureUid = data.ReadUint64();
569
570 std::vector<uint8_t> token;
571 std::vector<uint8_t> oldSecret;
572 std::vector<uint8_t> newSecret;
573 data.ReadUInt8Vector(&token);
574 data.ReadUInt8Vector(&oldSecret);
575 data.ReadUInt8Vector(&newSecret);
576
577 int err = UpdateUserAuth(userId, secureUid, token, oldSecret, newSecret);
578 if (!reply.WriteInt32(err)) {
579 return E_WRITE_REPLY_ERR;
580 }
581
582 return E_OK;
583 }
584
HandleActiveUserKey(MessageParcel & data,MessageParcel & reply)585 int32_t StorageDaemonStub::HandleActiveUserKey(MessageParcel &data, MessageParcel &reply)
586 {
587 uint32_t userId = data.ReadUint32();
588
589 std::vector<uint8_t> token;
590 std::vector<uint8_t> secret;
591 data.ReadUInt8Vector(&token);
592 data.ReadUInt8Vector(&secret);
593
594 std::lock_guard<std::mutex> lock(mutex_);
595 auto it = GetUserStatistics(userId);
596 isNeedUpdateRadarFile_ = true;
597 int err = ActiveUserKey(userId, token, secret);
598 if ((err == E_OK) || ((err == E_ACTIVE_EL2_FAILED) && token.empty() && secret.empty())) {
599 it->second.keyLoadSuccCount++;
600 } else {
601 it->second.keyLoadFailCount++;
602 }
603 if (!reply.WriteInt32(err)) {
604 return E_WRITE_REPLY_ERR;
605 }
606 return E_OK;
607 }
608
HandleInactiveUserKey(MessageParcel & data,MessageParcel & reply)609 int32_t StorageDaemonStub::HandleInactiveUserKey(MessageParcel &data, MessageParcel &reply)
610 {
611 uint32_t userId = data.ReadUint32();
612
613 std::lock_guard<std::mutex> lock(mutex_);
614 auto it = GetUserStatistics(userId);
615 isNeedUpdateRadarFile_ = true;
616 int err = InactiveUserKey(userId);
617 if (err != E_OK) {
618 it->second.keyUnloadFailCount++;
619 } else {
620 it->second.keyUnloadSuccCount++;
621 }
622 if (!reply.WriteInt32(err)) {
623 return E_WRITE_REPLY_ERR;
624 }
625 return E_OK;
626 }
627
HandleLockUserScreen(MessageParcel & data,MessageParcel & reply)628 int32_t StorageDaemonStub::HandleLockUserScreen(MessageParcel &data, MessageParcel &reply)
629 {
630 uint32_t userId = data.ReadUint32();
631
632 std::lock_guard<std::mutex> lock(mutex_);
633 auto it = GetUserStatistics(userId);
634 isNeedUpdateRadarFile_ = true;
635 int err = LockUserScreen(userId);
636 if (err != E_OK) {
637 it->second.keyUnloadFailCount++;
638 } else {
639 it->second.keyUnloadSuccCount++;
640 }
641 if (!reply.WriteInt32(err)) {
642 return E_WRITE_REPLY_ERR;
643 }
644 return E_OK;
645 }
646
HandleUnlockUserScreen(MessageParcel & data,MessageParcel & reply)647 int32_t StorageDaemonStub::HandleUnlockUserScreen(MessageParcel &data, MessageParcel &reply)
648 {
649 uint32_t userId = data.ReadUint32();
650
651 std::vector<uint8_t> token;
652 std::vector<uint8_t> secret;
653 data.ReadUInt8Vector(&token);
654 data.ReadUInt8Vector(&secret);
655
656 std::lock_guard<std::mutex> lock(mutex_);
657 auto it = GetUserStatistics(userId);
658 isNeedUpdateRadarFile_ = true;
659 int err = UnlockUserScreen(userId, token, secret);
660 if (err != E_OK) {
661 it->second.keyLoadFailCount++;
662 } else {
663 it->second.keyLoadSuccCount++;
664 }
665 if (!reply.WriteInt32(err)) {
666 return E_WRITE_REPLY_ERR;
667 }
668 return E_OK;
669 }
670
HandleGetLockScreenStatus(MessageParcel & data,MessageParcel & reply)671 int32_t StorageDaemonStub::HandleGetLockScreenStatus(MessageParcel &data, MessageParcel &reply)
672 {
673 uint32_t userId = data.ReadUint32();
674 bool lockScreenStatus = false;
675 int err = GetLockScreenStatus(userId, lockScreenStatus);
676 if (!reply.WriteBool(lockScreenStatus)) {
677 return E_WRITE_REPLY_ERR;
678 }
679 if (!reply.WriteInt32(err)) {
680 return E_WRITE_REPLY_ERR;
681 }
682
683 return E_OK;
684 }
685
HandleGenerateAppkey(MessageParcel & data,MessageParcel & reply)686 int32_t StorageDaemonStub::HandleGenerateAppkey(MessageParcel &data, MessageParcel &reply)
687 {
688 uint32_t userId = data.ReadUint32();
689 uint32_t hashId = data.ReadUint32();
690 std::string keyId;
691 int err = GenerateAppkey(userId, hashId, keyId);
692 if (!reply.WriteString(keyId)) {
693 return E_WRITE_REPLY_ERR;
694 }
695 if (!reply.WriteInt32(err)) {
696 return E_WRITE_REPLY_ERR;
697 }
698
699 return E_OK;
700 }
701
HandleDeleteAppkey(MessageParcel & data,MessageParcel & reply)702 int32_t StorageDaemonStub::HandleDeleteAppkey(MessageParcel &data, MessageParcel &reply)
703 {
704 uint32_t userId = data.ReadUint32();
705 std::string keyId = data.ReadString();
706 int err = DeleteAppkey(userId, keyId);
707 if (!reply.WriteInt32(err)) {
708 return E_WRITE_REPLY_ERR;
709 }
710
711 return E_OK;
712 }
713
HandleUpdateKeyContext(MessageParcel & data,MessageParcel & reply)714 int32_t StorageDaemonStub::HandleUpdateKeyContext(MessageParcel &data, MessageParcel &reply)
715 {
716 uint32_t userId = data.ReadUint32();
717 int err = UpdateKeyContext(userId);
718 if (!reply.WriteInt32(err)) {
719 return E_WRITE_REPLY_ERR;
720 }
721
722 return E_OK;
723 }
724
HandleMountCryptoPathAgain(MessageParcel & data,MessageParcel & reply)725 int32_t StorageDaemonStub::HandleMountCryptoPathAgain(MessageParcel &data, MessageParcel &reply)
726 {
727 uint32_t userId = data.ReadUint32();
728 int32_t err = MountCryptoPathAgain(userId);
729 if (!reply.WriteInt32(err)) {
730 return E_WRITE_REPLY_ERR;
731 }
732 return E_OK;
733 }
734
HandleCreateShareFile(MessageParcel & data,MessageParcel & reply)735 int32_t StorageDaemonStub::HandleCreateShareFile(MessageParcel &data, MessageParcel &reply)
736 {
737 std::vector<std::string> uriList;
738 if (!data.ReadStringVector(&uriList)) {
739 return E_WRITE_REPLY_ERR;
740 }
741 uint32_t tokenId = data.ReadUint32();
742 uint32_t flag = data.ReadUint32();
743 std::vector<int32_t> retList = CreateShareFile(uriList, tokenId, flag);
744 if (!reply.WriteInt32Vector(retList)) {
745 return E_WRITE_REPLY_ERR;
746 }
747 return E_OK;
748 }
749
HandleDeleteShareFile(MessageParcel & data,MessageParcel & reply)750 int32_t StorageDaemonStub::HandleDeleteShareFile(MessageParcel &data, MessageParcel &reply)
751 {
752 uint32_t tokenId = data.ReadUint32();
753 std::vector<std::string> uriList;
754 if (!data.ReadStringVector(&uriList)) {
755 return E_WRITE_REPLY_ERR;
756 }
757 int err = DeleteShareFile(tokenId, uriList);
758 if (!reply.WriteInt32(err)) {
759 return E_WRITE_REPLY_ERR;
760 }
761 return E_OK;
762 }
763
HandleSetBundleQuota(MessageParcel & data,MessageParcel & reply)764 int32_t StorageDaemonStub::HandleSetBundleQuota(MessageParcel &data, MessageParcel &reply)
765 {
766 std::string bundleName = data.ReadString();
767 int32_t uid = data.ReadInt32();
768 std::string bundleDataDirPath = data.ReadString();
769 int32_t limitSizeMb = data.ReadInt32();
770 int err = SetBundleQuota(bundleName, uid, bundleDataDirPath, limitSizeMb);
771 if (!reply.WriteInt32(err)) {
772 return E_WRITE_REPLY_ERR;
773 }
774 return E_OK;
775 }
776
HandleGetOccupiedSpace(MessageParcel & data,MessageParcel & reply)777 int32_t StorageDaemonStub::HandleGetOccupiedSpace(MessageParcel &data, MessageParcel &reply)
778 {
779 int32_t idType = data.ReadInt32();
780 int32_t id = data.ReadInt32();
781 int64_t size = 0;
782 int err = GetOccupiedSpace(idType, id, size);
783 if (!reply.WriteInt32(err)) {
784 return E_WRITE_REPLY_ERR;
785 }
786 if (!reply.WriteInt64(size)) {
787 LOGE("StorageManagerStub::HandleGetFree call GetTotalSize failed");
788 return E_WRITE_REPLY_ERR;
789 }
790 return E_OK;
791 }
HandleUpdateMemoryPara(MessageParcel & data,MessageParcel & reply)792 int32_t StorageDaemonStub::HandleUpdateMemoryPara(MessageParcel &data, MessageParcel &reply)
793 {
794 int32_t size = data.ReadInt32();
795 int32_t oldSize = 0;
796 int err = UpdateMemoryPara(size, oldSize);
797 if (!reply.WriteInt32(err)) {
798 return E_WRITE_REPLY_ERR;
799 }
800 if (!reply.WriteInt32(oldSize)) {
801 LOGE("StorageManagerStub::HandleUpdateMemoryPara call Write oldSize failed");
802 return E_WRITE_REPLY_ERR;
803 }
804 return E_OK;
805 }
806
HandleGetBundleStatsForIncrease(MessageParcel & data,MessageParcel & reply)807 int32_t StorageDaemonStub::HandleGetBundleStatsForIncrease(MessageParcel &data, MessageParcel &reply)
808 {
809 uint32_t userId = data.ReadUint32();
810 std::vector<std::string> bundleNames;
811 if (!data.ReadStringVector(&bundleNames)) {
812 return E_WRITE_REPLY_ERR;
813 }
814 std::vector<int64_t> incrementalBackTimes;
815 if (!data.ReadInt64Vector(&incrementalBackTimes)) {
816 return E_WRITE_REPLY_ERR;
817 }
818
819 std::vector<int64_t> pkgFileSizes;
820 std::vector<int64_t> incPkgFileSizes;
821 int32_t err = GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, pkgFileSizes, incPkgFileSizes);
822 if (!reply.WriteInt32(err)) {
823 return E_WRITE_REPLY_ERR;
824 }
825 if (!reply.WriteInt64Vector(pkgFileSizes)) {
826 LOGE("StorageDaemonStub::HandleGetBundleStatsForIncrease call GetBundleStatsForIncrease failed");
827 return E_WRITE_REPLY_ERR;
828 }
829 if (!reply.WriteInt64Vector(incPkgFileSizes)) {
830 LOGE("StorageDaemonStub::HandleGetBundleStatsForIncrease call GetBundleStatsForIncrease failed");
831 return E_WRITE_REPLY_ERR;
832 }
833 return E_OK;
834 }
835
HandleMountDfsDocs(MessageParcel & data,MessageParcel & reply)836 int32_t StorageDaemonStub::HandleMountDfsDocs(MessageParcel &data, MessageParcel &reply)
837 {
838 LOGI("StorageDaemonStub::HandleMountDfsDocs start.");
839 int32_t userId = data.ReadInt32();
840 std::string relativePath = data.ReadString();
841 std::string networkId = data.ReadString();
842 std::string deviceId = data.ReadString();
843
844 int32_t err = MountDfsDocs(userId, relativePath, networkId, deviceId);
845 if (!reply.WriteInt32(err)) {
846 return E_WRITE_REPLY_ERR;
847 }
848 return E_OK;
849 }
850
HandleUMountDfsDocs(MessageParcel & data,MessageParcel & reply)851 int32_t StorageDaemonStub::HandleUMountDfsDocs(MessageParcel &data, MessageParcel &reply)
852 {
853 LOGI("StorageDaemonStub::HandleUMountDfsDocs start.");
854 int32_t userId = data.ReadInt32();
855 std::string relativePath = data.ReadString();
856 std::string networkId = data.ReadString();
857 std::string deviceId = data.ReadString();
858
859 int32_t err = UMountDfsDocs(userId, relativePath, networkId, deviceId);
860 if (!reply.WriteInt32(err)) {
861 return E_WRITE_REPLY_ERR;
862 }
863 return E_OK;
864 }
865
HandleGetFileEncryptStatus(MessageParcel & data,MessageParcel & reply)866 int32_t StorageDaemonStub::HandleGetFileEncryptStatus(MessageParcel &data, MessageParcel &reply)
867 {
868 uint32_t userId = data.ReadUint32();
869 bool needCheckDirMount = data.ReadBool();
870 bool isEncrypted = true;
871 int err = GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount);
872 if (!reply.WriteInt32(err)) {
873 return E_WRITE_REPLY_ERR;
874 }
875 if (!reply.WriteBool(isEncrypted)) {
876 return E_WRITE_REPLY_ERR;
877 }
878 return E_OK;
879 }
880
HandleGetUserNeedActiveStatus(MessageParcel & data,MessageParcel & reply)881 int32_t StorageDaemonStub::HandleGetUserNeedActiveStatus(MessageParcel &data, MessageParcel &reply)
882 {
883 uint32_t userId = data.ReadUint32();
884 bool needActive = false;
885 int err = GetUserNeedActiveStatus(userId, needActive);
886 if (!reply.WriteInt32(err)) {
887 return E_WRITE_REPLY_ERR;
888 }
889 if (!reply.WriteBool(needActive)) {
890 return E_WRITE_REPLY_ERR;
891 }
892 return E_OK;
893 }
894 } // StorageDaemon
895 } // OHOS
896