• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 #include "os_account_interface.h"
16 
17 #include<thread>
18 
19 #include "ability_manager_adapter.h"
20 #include "account_log_wrapper.h"
21 #include "bundle_manager_adapter.h"
22 #ifdef HAS_CES_PART
23 #include "common_event_manager.h"
24 #include "common_event_support.h"
25 #endif // HAS_CES_PART
26 #include "datetime_ex.h"
27 #include "hisysevent_adapter.h"
28 #include "hitrace_meter.h"
29 #include "if_system_ability_manager.h"
30 #include "iservice_registry.h"
31 #ifdef HAS_STORAGE_PART
32 #include "istorage_manager.h"
33 #endif
34 #include "os_account_constants.h"
35 #include "os_account_delete_user_idm_callback.h"
36 #include "os_account_stop_user_callback.h"
37 #ifdef HAS_STORAGE_PART
38 #include "storage_manager.h"
39 #include "storage_manager_proxy.h"
40 #endif
41 #include "system_ability_definition.h"
42 #ifdef HAS_USER_IDM_PART
43 #include "user_idm_client.h"
44 #endif // HAS_USER_IDM_PART
45 #ifdef HAS_CES_PART
46 #include "want.h"
47 #endif // HAS_CES_PART
48 
49 namespace OHOS {
50 namespace AccountSA {
SendToAMSAccountStart(OsAccountInfo & osAccountInfo)51 ErrCode OsAccountInterface::SendToAMSAccountStart(OsAccountInfo &osAccountInfo)
52 {
53     ACCOUNT_LOGI("start");
54     StartTrace(HITRACE_TAG_ACCOUNT_MANAGER, "AbilityManagerAdapter StartUser");
55     ErrCode code = AbilityManagerAdapter::GetInstance()->StartUser(osAccountInfo.GetLocalId());
56     if (code != ERR_OK) {
57         ACCOUNT_LOGE("AbilityManagerAdapter StartUser failed! errcode is %{public}d", code);
58         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_ACTIVATE, code,
59             "AbilityManagerAdapter StartUser failed!");
60         FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
61         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_AM_ACCOUNT_START_ERROR;
62     }
63     ACCOUNT_LOGI("end, succeed!");
64     FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
65     return ERR_OK;
66 }
67 
SendToAMSAccountStop(OsAccountInfo & osAccountInfo)68 ErrCode OsAccountInterface::SendToAMSAccountStop(OsAccountInfo &osAccountInfo)
69 {
70     sptr<OsAccountStopUserCallback> osAccountStopUserCallback = new (std::nothrow) OsAccountStopUserCallback();
71     if (osAccountStopUserCallback == nullptr) {
72         ACCOUNT_LOGE("alloc memory for stop user callback failed!");
73         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_STOP,
74             ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR,
75             "malloc for OsAccountStopUserCallback failed!");
76         return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
77     }
78     StartTrace(HITRACE_TAG_ACCOUNT_MANAGER, "AbilityManagerAdapter StopUser");
79     ErrCode code = AbilityManagerAdapter::GetInstance()->StopUser(osAccountInfo.GetLocalId(),
80         osAccountStopUserCallback);
81     if (code != ERR_OK) {
82         ACCOUNT_LOGE("failed to AbilityManagerAdapter stop errcode is %{public}d", code);
83         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_STOP, code,
84             "AbilityManagerService StopUser failed!");
85         FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
86         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_AM_ACCOUNT_START_ERROR;
87     }
88     struct tm startTime = {0};
89     struct tm nowTime = {0};
90     OHOS::GetSystemCurrentTime(&startTime);
91     OHOS::GetSystemCurrentTime(&nowTime);
92     while (OHOS::GetSecondsBetween(startTime, nowTime) < Constants::TIME_WAIT_TIME_OUT &&
93            !osAccountStopUserCallback->isCallBackOk_) {
94         std::this_thread::sleep_for(std::chrono::milliseconds(Constants::WAIT_ONE_TIME));
95         OHOS::GetSystemCurrentTime(&nowTime);
96     }
97     if (!osAccountStopUserCallback->isReturnOk_) {
98         ACCOUNT_LOGE("failed to AbilityManagerService stop in call back");
99         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_STOP, -1,
100             "AbilityManagerService StopUser timeout!");
101         FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
102         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_AM_ACCOUNT_START_ERROR;
103     }
104 
105     FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
106     return ERR_OK;
107 }
108 
SendToBMSAccountCreate(OsAccountInfo & osAccountInfo)109 ErrCode OsAccountInterface::SendToBMSAccountCreate(OsAccountInfo &osAccountInfo)
110 {
111     return BundleManagerAdapter::GetInstance()->CreateNewUser(osAccountInfo.GetLocalId());
112 }
113 
SendToBMSAccountDelete(OsAccountInfo & osAccountInfo)114 ErrCode OsAccountInterface::SendToBMSAccountDelete(OsAccountInfo &osAccountInfo)
115 {
116     return BundleManagerAdapter::GetInstance()->RemoveUser(osAccountInfo.GetLocalId());
117 }
118 
119 #ifdef HAS_USER_IDM_PART
SendToIDMAccountDelete(OsAccountInfo & osAccountInfo)120 ErrCode OsAccountInterface::SendToIDMAccountDelete(OsAccountInfo &osAccountInfo)
121 {
122     std::shared_ptr<OsAccountDeleteUserIdmCallback> callback = std::make_shared<OsAccountDeleteUserIdmCallback>();
123     if (callback == nullptr) {
124         ACCOUNT_LOGE("get idm callback ptr failed! insufficient memory!");
125         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_DELETE,
126             ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR,
127             "malloc for OsAccountDeleteUserIdmCallback failed!");
128         return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
129     }
130     StartTrace(HITRACE_TAG_ACCOUNT_MANAGER, "UserIDMClient EnforceDelUser");
131     int32_t ret = UserIam::UserAuth::UserIdmClient::GetInstance().EraseUser(osAccountInfo.GetLocalId(), callback);
132     if (ret != 0) {
133         ACCOUNT_LOGE("idm enforce delete user failed! error %{public}d", ret);
134         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_DELETE, ret,
135             "UserIDMClient EnforceDelUser failed!");
136         FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
137         return ERR_OK;    // do not return fail
138     }
139 
140     // wait callback
141     struct tm startTime = {0};
142     struct tm nowTime = {0};
143     OHOS::GetSystemCurrentTime(&startTime);
144     OHOS::GetSystemCurrentTime(&nowTime);
145     while (OHOS::GetSecondsBetween(startTime, nowTime) < Constants::TIME_WAIT_TIME_OUT &&
146         !callback->isIdmOnResultCallBack_) {
147         std::this_thread::sleep_for(std::chrono::milliseconds(Constants::WAIT_ONE_TIME));
148         OHOS::GetSystemCurrentTime(&nowTime);
149     }
150     if (!callback->isIdmOnResultCallBack_) {
151         ACCOUNT_LOGE("idm did not call back! timeout!");
152         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_DELETE, -1,
153             "UserIDMClient EnforceDelUser timeout!");
154         FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
155         return ERR_OK;    // do not return fail
156     }
157     ACCOUNT_LOGI("send to idm account delete and get callback succeed!");
158     FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
159     return ERR_OK;
160 }
161 #endif // HAS_USER_IDM_PART
162 
SendToCESAccountCreate(OsAccountInfo & osAccountInfo)163 void OsAccountInterface::SendToCESAccountCreate(OsAccountInfo &osAccountInfo)
164 {
165     int osAccountID = osAccountInfo.GetLocalId();
166 #ifdef HAS_CES_PART
167     StartTrace(HITRACE_TAG_ACCOUNT_MANAGER, "PublishCommonEvent account create");
168     OHOS::AAFwk::Want want;
169     want.SetAction(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED);
170     OHOS::EventFwk::CommonEventData data;
171     data.SetCode(osAccountID);
172     data.SetWant(want);
173     if (!OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data)) {
174         ACCOUNT_LOGE("PublishCommonEvent for create account %{public}d failed!", osAccountID);
175         ReportOsAccountOperationFail(osAccountID, Constants::OPERATION_CREATE, -1, "PublishCommonEvent failed!");
176     } else {
177         ACCOUNT_LOGI("PublishCommonEvent for create account %{public}d succeed!", osAccountID);
178     }
179     FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
180 #else // HAS_CES_PART
181     ACCOUNT_LOGI("No common event part, do not publish for account %{public}d create!", osAccountID);
182 #endif // HAS_CES_PART
183 }
184 
SendToCESAccountDelete(OsAccountInfo & osAccountInfo)185 void OsAccountInterface::SendToCESAccountDelete(OsAccountInfo &osAccountInfo)
186 {
187     int osAccountID = osAccountInfo.GetLocalId();
188 #ifdef HAS_CES_PART
189     StartTrace(HITRACE_TAG_ACCOUNT_MANAGER, "PublishCommonEvent account delete");
190     OHOS::AAFwk::Want want;
191     want.SetAction(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
192     OHOS::EventFwk::CommonEventData data;
193     data.SetCode(osAccountID);
194     data.SetWant(want);
195     if (!OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data)) {
196         ACCOUNT_LOGE("PublishCommonEvent for delete account %{public}d failed!", osAccountID);
197         ReportOsAccountOperationFail(osAccountID, Constants::OPERATION_DELETE, -1, "PublishCommonEvent failed!");
198     } else {
199         ACCOUNT_LOGI("PublishCommonEvent for delete account %{public}d succeed!", osAccountID);
200     }
201     FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
202 #else // HAS_CES_PART
203     ACCOUNT_LOGI("No common event part, do not publish for account %{public}d delete!", osAccountID);
204 #endif // HAS_CES_PART
205 }
206 
PublishCommonEvent(const OsAccountInfo & osAccountInfo,const std::string & commonEvent,const std::string & operation)207 void OsAccountInterface::PublishCommonEvent(
208     const OsAccountInfo &osAccountInfo, const std::string &commonEvent, const std::string &operation)
209 {
210     int osAccountID = osAccountInfo.GetLocalId();
211 #ifdef HAS_CES_PART
212     StartTrace(HITRACE_TAG_ACCOUNT_MANAGER, "PublishCommonEvent account");
213     OHOS::AAFwk::Want want;
214     want.SetAction(commonEvent);
215     OHOS::EventFwk::CommonEventData data;
216     data.SetCode(osAccountID);
217     data.SetWant(want);
218     if (!OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data)) {
219         ACCOUNT_LOGE("PublishCommonEvent %{public}d failed!", osAccountID);
220         ReportOsAccountOperationFail(osAccountID, operation, -1, "PublishCommonEvent failed!");
221     } else {
222         ACCOUNT_LOGI("PublishCommonEvent %{public}d succeed!", osAccountID);
223     }
224     FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
225 #else  // HAS_CES_PART
226     ACCOUNT_LOGI("No common event part, do not publish for account %{public}d!", osAccountID);
227 #endif // HAS_CES_PART
228 }
229 
SendToCESAccountSwitched(OsAccountInfo & osAccountInfo)230 void OsAccountInterface::SendToCESAccountSwitched(OsAccountInfo &osAccountInfo)
231 {
232     int osAccountID = osAccountInfo.GetLocalId();
233 #ifdef HAS_CES_PART
234     StartTrace(HITRACE_TAG_ACCOUNT_MANAGER, "PublishCommonEvent account switch");
235     OHOS::AAFwk::Want want;
236     want.SetAction(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
237     OHOS::EventFwk::CommonEventData data;
238     data.SetCode(osAccountID);
239     data.SetWant(want);
240     if (!OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data)) {
241         ACCOUNT_LOGE("PublishCommonEvent for switched to account %{public}d failed!", osAccountID);
242         ReportOsAccountOperationFail(osAccountID, Constants::OPERATION_SWITCH, -1, "PublishCommonEvent failed!");
243     } else {
244         ACCOUNT_LOGI("PublishCommonEvent for switched to account %{public}d succeed!", osAccountID);
245     }
246     FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
247 #else // HAS_CES_PART
248     ACCOUNT_LOGI("No common event part, do not publish for account %{public}d switched!", osAccountID);
249 #endif // HAS_CES_PART
250 }
251 
SendToStorageAccountCreate(OsAccountInfo & osAccountInfo)252 ErrCode OsAccountInterface::SendToStorageAccountCreate(OsAccountInfo &osAccountInfo)
253 {
254 #ifdef HAS_STORAGE_PART
255     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
256     if (!systemAbilityManager) {
257         ACCOUNT_LOGE("failed to get system ability mgr.");
258         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_CREATE,
259             ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_CREATE_ERROR,
260             "GetSystemAbilityManager for storage failed!");
261         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_CREATE_ERROR;
262     }
263     auto remote = systemAbilityManager->CheckSystemAbility(STORAGE_MANAGER_MANAGER_ID);
264     if (!remote) {
265         ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID service.");
266         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_CREATE,
267             ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_CREATE_ERROR,
268             "CheckSystemAbility for storage failed!");
269         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_CREATE_ERROR;
270     }
271     auto proxy = iface_cast<StorageManager::IStorageManager>(remote);
272     if (!proxy) {
273         ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID proxy.");
274         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_CREATE_ERROR;
275     }
276     StartTrace(HITRACE_TAG_ACCOUNT_MANAGER, "StorageManager PrepareAddUser");
277     int err = proxy->PrepareAddUser(osAccountInfo.GetLocalId(),
278         StorageManager::CRYPTO_FLAG_EL1 | StorageManager::CRYPTO_FLAG_EL2);
279     if (err != 0) {
280         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_CREATE,
281             err, "Storage PrepareAddUser failed!");
282     }
283 
284     FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
285 #endif
286     return ERR_OK;
287 }
288 
SendToStorageAccountRemove(OsAccountInfo & osAccountInfo)289 ErrCode OsAccountInterface::SendToStorageAccountRemove(OsAccountInfo &osAccountInfo)
290 {
291     ACCOUNT_LOGI("start");
292 #ifdef HAS_STORAGE_PART
293     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
294     if (!systemAbilityManager) {
295         ACCOUNT_LOGE("failed to get system ability mgr.");
296         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_DELETE,
297             ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_REMOVE_ERROR,
298             "GetSystemAbilityManager for storage failed!");
299         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_REMOVE_ERROR;
300     }
301     auto remote = systemAbilityManager->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
302     if (!remote) {
303         ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID service.");
304         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_DELETE,
305             ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_REMOVE_ERROR,
306             "GetSystemAbility for storage failed!");
307         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_REMOVE_ERROR;
308     }
309     auto proxy = iface_cast<StorageManager::IStorageManager>(remote);
310     if (!proxy) {
311         ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID proxy.");
312         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_REMOVE_ERROR;
313     }
314 
315     StartTrace(HITRACE_TAG_ACCOUNT_MANAGER, "StorageManager RemoveUser");
316     int err = proxy->RemoveUser(osAccountInfo.GetLocalId(),
317         StorageManager::CRYPTO_FLAG_EL1 | StorageManager::CRYPTO_FLAG_EL2);
318     if (err != 0) {
319         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_DELETE,
320             err, "Storage RemoveUser failed!");
321     }
322 
323     ACCOUNT_LOGI("end, Storage RemoveUser ret %{public}d.", err);
324     FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
325 #endif
326     return ERR_OK;
327 }
328 
SendToStorageAccountStart(OsAccountInfo & osAccountInfo)329 ErrCode OsAccountInterface::SendToStorageAccountStart(OsAccountInfo &osAccountInfo)
330 {
331     ACCOUNT_LOGI("start");
332     bool isUserUnlocked = false;
333 #ifdef HAS_STORAGE_PART
334     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
335     if (!systemAbilityManager) {
336         ACCOUNT_LOGE("failed to get system ability mgr.");
337         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_ACTIVATE,
338             ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_START_ERROR,
339             "GetSystemAbilityManager for storage failed!");
340         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_START_ERROR;
341     }
342     auto remote = systemAbilityManager->CheckSystemAbility(STORAGE_MANAGER_MANAGER_ID);
343     if (!remote) {
344         ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID service.");
345         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_ACTIVATE,
346             ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_START_ERROR, "CheckSystemAbility for storage failed!");
347         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_START_ERROR;
348     }
349     auto proxy = iface_cast<StorageManager::IStorageManager>(remote);
350     if (!proxy) {
351         ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID proxy.");
352         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_START_ERROR;
353     }
354     StartTrace(HITRACE_TAG_ACCOUNT_MANAGER, "StorageManager PrepareStartUser");
355     int localId = osAccountInfo.GetLocalId();
356     std::vector<uint8_t> emptyData;
357     int err = proxy->ActiveUserKey(localId, emptyData, emptyData);
358     if ((err == 0) && !osAccountInfo.GetIsVerified()) {
359         isUserUnlocked = true;
360     }
361     err = proxy->PrepareStartUser(localId);
362     if (err != 0) {
363         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_ACTIVATE,
364             err, "Storage PrepareStartUser failed!");
365     }
366     ACCOUNT_LOGI("end, Storage PrepareStartUser ret %{public}d.", err);
367     FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
368 #else
369     if (!osAccountInfo.GetIsVerified()) {
370         isUserUnlocked = true;
371     }
372 #endif
373     if (isUserUnlocked) {
374         osAccountInfo.SetIsVerified(true);
375         PublishCommonEvent(osAccountInfo, OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED,
376             Constants::OPERATION_UNLOCK);
377     }
378     return ERR_OK;
379 }
380 
SendToStorageAccountStop(OsAccountInfo & osAccountInfo)381 ErrCode OsAccountInterface::SendToStorageAccountStop(OsAccountInfo &osAccountInfo)
382 {
383 #ifdef HAS_STORAGE_PART
384     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
385     if (!systemAbilityManager) {
386         ACCOUNT_LOGE("failed to get system ability mgr.");
387         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_STOP,
388             ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_STOP_ERROR,
389             "GetSystemAbilityManager for storage failed!");
390         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_STOP_ERROR;
391     }
392     auto remote = systemAbilityManager->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
393     if (!remote) {
394         ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID service.");
395         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_STOP,
396             ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_STOP_ERROR,
397             "GetSystemAbility for storage failed!");
398         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_STOP_ERROR;
399     }
400     auto proxy = iface_cast<StorageManager::IStorageManager>(remote);
401     if (!proxy) {
402         ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID proxy.");
403         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_STOP_ERROR;
404     }
405     StartTrace(HITRACE_TAG_ACCOUNT_MANAGER, "StorageManager StopUser");
406     int localId = osAccountInfo.GetLocalId();
407     int err = proxy->StopUser(localId);
408     if (err != 0) {
409         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_STOP,
410             err, "Storage StopUser failed!");
411     }
412     err = proxy->InactiveUserKey(localId);
413     if (err != 0) {
414         ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_STOP,
415             err, "Storage StopUser failed!");
416     }
417 
418     FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
419 #endif
420     return ERR_OK;
421 }
422 }  // namespace AccountSA
423 }  // namespace OHOS
424