• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "storage_daemon_client.h"
17 
18 #include <chrono>
19 #include <ctime>
20 #include <thread>
21 
22 #include "iremote_object.h"
23 #include "iremote_proxy.h"
24 #include "iservice_registry.h"
25 #include "libfscrypt/fscrypt_utils.h"
26 #include "storage_service_log.h"
27 #include "system_ability_definition.h"
28 
29 namespace {
30 constexpr uint32_t STORAGE_DAEMON_SFIFT = 1;
31 constexpr uint32_t CHECK_SERVICE_TIMES = 1000;
32 constexpr uint32_t SLEEP_TIME_PRE_CHECK = 10; // 10ms
33 constexpr uint32_t STORAGE_SERVICE_FLAG = (1 << STORAGE_DAEMON_SFIFT);
34 constexpr int32_t STORAGE_DAEMON_SAID = OHOS::STORAGE_MANAGER_DAEMON_ID;
35 }
36 
37 namespace OHOS {
38 namespace StorageDaemon {
GetStorageDaemonProxy(void)39 sptr<IStorageDaemon> StorageDaemonClient::GetStorageDaemonProxy(void)
40 {
41     auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42     if (samgr == nullptr) {
43         LOGE("samgr empty error");
44         return nullptr;
45     }
46 
47     sptr<IRemoteObject> object = samgr->GetSystemAbility(OHOS::STORAGE_MANAGER_DAEMON_ID);
48     if (object == nullptr) {
49         LOGE("storage daemon client samgr ablity empty error");
50         return nullptr;
51     }
52 
53     return iface_cast<IStorageDaemon>(object);
54 }
55 
CheckServiceStatus(uint32_t serviceFlags)56 bool StorageDaemonClient::CheckServiceStatus(uint32_t serviceFlags)
57 {
58     LOGI("CheckServiceStatus start");
59 
60     auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
61     if (samgr == nullptr) {
62         LOGW("samgr empty, retry");
63         for (uint32_t i = 0; i < CHECK_SERVICE_TIMES; i++) {
64             samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
65             if (samgr != nullptr) {
66                 break;
67             }
68             LOGI("check samgr %{public}u times", i);
69             std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_PRE_CHECK));
70         }
71         if (samgr == nullptr) {
72             LOGE("samgr empty error");
73             return false;
74         }
75     }
76 
77     bool exist = false;
78     if (serviceFlags & STORAGE_SERVICE_FLAG) {
79         for (uint32_t i = 0; i < CHECK_SERVICE_TIMES; i++) {
80             auto object = samgr->CheckSystemAbility(STORAGE_DAEMON_SAID, exist);
81             if (object != nullptr) {
82                 break;
83             }
84             LOGI("check storage daemon status %{public}u times", i);
85             std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_PRE_CHECK));
86         }
87         if (exist == false) {
88             LOGE("storage daemon service system ability error");
89             return false;
90         }
91     }
92     LOGI("CheckServiceStatus end, success");
93 
94     return true;
95 }
96 
PrepareUserDirs(int32_t userId,uint32_t flags)97 int32_t StorageDaemonClient::PrepareUserDirs(int32_t userId, uint32_t flags)
98 {
99     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
100         LOGE("service check failed");
101         return -EAGAIN;
102     }
103 
104     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
105     if (client == nullptr) {
106         LOGE("get storage daemon service failed");
107         return -EAGAIN;
108     }
109 
110     return client->PrepareUserDirs(userId, flags);
111 }
112 
DestroyUserDirs(int32_t userId,uint32_t flags)113 int32_t StorageDaemonClient::DestroyUserDirs(int32_t userId, uint32_t flags)
114 {
115     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
116         LOGE("service check failed");
117         return -EAGAIN;
118     }
119 
120     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
121     if (client == nullptr) {
122         LOGE("get storage daemon service failed");
123         return -EAGAIN;
124     }
125 
126     return client->DestroyUserDirs(userId, flags);
127 }
128 
StartUser(int32_t userId)129 int32_t StorageDaemonClient::StartUser(int32_t userId)
130 {
131     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
132         LOGE("service check failed");
133         return -EAGAIN;
134     }
135 
136     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
137     if (client == nullptr) {
138         LOGE("get storage daemon service failed");
139         return -EAGAIN;
140     }
141 
142     return client->StartUser(userId);
143 }
144 
StopUser(int32_t userId)145 int32_t StorageDaemonClient::StopUser(int32_t userId)
146 {
147     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
148         LOGE("service check failed");
149         return -EAGAIN;
150     }
151 
152     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
153     if (client == nullptr) {
154         LOGE("get storage daemon service failed");
155         return -EAGAIN;
156     }
157 
158     return client->StopUser(userId);
159 }
160 
PrepareUserSpace(uint32_t userId,const std::string & volumId,uint32_t flags)161 int32_t StorageDaemonClient::PrepareUserSpace(uint32_t userId, const std::string &volumId, uint32_t flags)
162 {
163     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
164         LOGE("service check failed");
165         return -EAGAIN;
166     }
167 
168     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
169     if (client == nullptr) {
170         LOGE("get storage daemon service failed");
171         return -EAGAIN;
172     }
173 
174     return client->PrepareUserDirs(userId, flags);
175 }
176 
DestroyUserSpace(uint32_t userId,const std::string & volumId,uint32_t flags)177 int32_t StorageDaemonClient::DestroyUserSpace(uint32_t userId, const std::string &volumId, uint32_t flags)
178 {
179     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
180         LOGE("service check failed");
181         return -EAGAIN;
182     }
183 
184     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
185     if (client == nullptr) {
186         LOGE("get storage daemon service failed");
187         return -EAGAIN;
188     }
189 
190     return client->DestroyUserDirs(userId, flags);
191 }
192 
InitGlobalKey(void)193 int32_t StorageDaemonClient::InitGlobalKey(void)
194 {
195     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
196         LOGE("service check failed");
197         return -EAGAIN;
198     }
199 
200     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
201     if (client == nullptr) {
202         LOGE("get storage daemon service failed");
203         return -EAGAIN;
204     }
205 
206     return client->InitGlobalKey();
207 }
208 
InitGlobalUserKeys(void)209 int32_t StorageDaemonClient::InitGlobalUserKeys(void)
210 {
211     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
212         LOGE("service check failed");
213         return -EAGAIN;
214     }
215 
216     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
217     if (client == nullptr) {
218         LOGE("get storage daemon service failed");
219         return -EAGAIN;
220     }
221 
222     return client->InitGlobalUserKeys();
223 }
224 
GenerateUserKeys(uint32_t userId,uint32_t flags)225 int32_t StorageDaemonClient::GenerateUserKeys(uint32_t userId, uint32_t flags)
226 {
227     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
228         LOGE("service check failed");
229         return -EAGAIN;
230     }
231 
232     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
233     if (client == nullptr) {
234         LOGE("get storage daemon service failed");
235         return -EAGAIN;
236     }
237 
238     return client->GenerateUserKeys(userId, flags);
239 }
240 
DeleteUserKeys(uint32_t userId)241 int32_t StorageDaemonClient::DeleteUserKeys(uint32_t userId)
242 {
243     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
244         LOGE("service check failed");
245         return -EAGAIN;
246     }
247 
248     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
249     if (client == nullptr) {
250         LOGE("get storage daemon service failed");
251         return -EAGAIN;
252     }
253 
254     return client->DeleteUserKeys(userId);
255 }
256 
UpdateUserAuth(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & oldSecret,const std::vector<uint8_t> & newSecret)257 int32_t StorageDaemonClient::UpdateUserAuth(uint32_t userId,
258                                             const std::vector<uint8_t> &token,
259                                             const std::vector<uint8_t> &oldSecret,
260                                             const std::vector<uint8_t> &newSecret)
261 {
262     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
263         LOGE("service check failed");
264         return -EAGAIN;
265     }
266 
267     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
268     if (client == nullptr) {
269         LOGE("get storage daemon service failed");
270         return -EAGAIN;
271     }
272 
273     return client->UpdateUserAuth(userId, token, oldSecret, newSecret);
274 }
275 
ActiveUserKey(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)276 int32_t StorageDaemonClient::ActiveUserKey(uint32_t userId,
277                                            const std::vector<uint8_t> &token,
278                                            const std::vector<uint8_t> &secret)
279 {
280     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
281         LOGE("service check failed");
282         return -EAGAIN;
283     }
284 
285     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
286     if (client == nullptr) {
287         LOGE("get storage daemon service failed");
288         return -EAGAIN;
289     }
290 
291     return client->ActiveUserKey(userId, token, secret);
292 }
293 
InactiveUserKey(uint32_t userId)294 int32_t StorageDaemonClient::InactiveUserKey(uint32_t userId)
295 {
296     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
297         LOGE("service check failed");
298         return -EAGAIN;
299     }
300 
301     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
302     if (client == nullptr) {
303         LOGE("get storage daemon service failed");
304         return -EAGAIN;
305     }
306 
307     return client->InactiveUserKey(userId);
308 }
309 
UpdateKeyContext(uint32_t userId)310 int32_t StorageDaemonClient::UpdateKeyContext(uint32_t userId)
311 {
312     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
313         LOGE("service check failed");
314         return -EAGAIN;
315     }
316 
317     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
318     if (client == nullptr) {
319         LOGE("get storage daemon service failed");
320         return -EAGAIN;
321     }
322 
323     return client->UpdateKeyContext(userId);
324 }
325 
FscryptEnable(const std::string & fscryptOptions)326 int32_t StorageDaemonClient::FscryptEnable(const std::string &fscryptOptions)
327 {
328     int ret = SetFscryptSysparam(fscryptOptions.c_str());
329     if (ret) {
330         LOGE("Init fscrypt policy failed ret %{public}d", ret);
331         return ret;
332     }
333 
334     return 0;
335 }
336 } // namespace StorageDaemon
337 } // namespace OHOS
338