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 #include "work_sched_utils.h"
16
17 #include "errors.h"
18 #include "ohos_account_kits.h"
19 #include "os_account_manager.h"
20 #include "tokenid_kit.h"
21 #include "ipc_skeleton.h"
22 #include "work_sched_hilog.h"
23
24 namespace OHOS {
25 namespace WorkScheduler {
26 const int32_t INVALID_DATA = -1;
27
28 #ifdef WORK_SCHEDULER_TEST
29 #define WEAK_FUNC __attribute__((weak))
30 #else
31 #define WEAK_FUNC
32 #endif
33
GetCurrentAccountId()34 int32_t WorkSchedUtils::GetCurrentAccountId()
35 {
36 std::vector<int32_t> osAccountIds;
37 ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(osAccountIds);
38 if (ret != ERR_OK) {
39 WS_HILOGE("QueryActiveOsAccountIds failed.");
40 return -1;
41 }
42
43 if (osAccountIds.empty()) {
44 WS_HILOGE("osAccountInfos is empty, no accounts.");
45 return -1;
46 }
47
48 auto iter = std::find_if(osAccountIds.cbegin(), osAccountIds.cend(),
49 [](const int32_t &accountId) { return accountId >= 0; });
50 if (iter != osAccountIds.end()) {
51 return *iter;
52 }
53 WS_HILOGE("GetCurrentAccountId failed, no osAccountIds now.");
54 return -1;
55 }
56
IsIdActive(int32_t id)57 bool WorkSchedUtils::IsIdActive(int32_t id)
58 {
59 std::vector<int32_t> osAccountIds;
60 ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(osAccountIds);
61 if (ret != ERR_OK) {
62 WS_HILOGE("QueryActiveOsAccountIds failed.");
63 return false;
64 }
65
66 if (osAccountIds.empty()) {
67 WS_HILOGE("osAccountIds is empty, no accounts.");
68 return false;
69 }
70
71 auto iter = std::find_if(osAccountIds.cbegin(), osAccountIds.cend(),
72 [&id](const int32_t &accountId) { return accountId == id; });
73 if (iter != osAccountIds.end()) {
74 return true;
75 }
76 WS_HILOGD("IsIdActive failed, osAccountIds now.");
77 return false;
78 }
79
GetUserIdByUid(int32_t uid)80 int32_t WorkSchedUtils::GetUserIdByUid(int32_t uid)
81 {
82 if (uid <= INVALID_DATA) {
83 WS_HILOGE("uid is illegal: %{public}d", uid);
84 return INVALID_DATA;
85 }
86 const int32_t baseUserRange = 200000;
87 return uid / baseUserRange;
88 }
89
ConvertFullPath(const std::string & partialPath,std::string & fullPath)90 bool WorkSchedUtils::ConvertFullPath(const std::string& partialPath, std::string& fullPath)
91 {
92 if (partialPath.empty() || partialPath.length() >= PATH_MAX) {
93 return false;
94 }
95 char tmpPath[PATH_MAX] = {0};
96 if (realpath(partialPath.c_str(), tmpPath) == nullptr) {
97 return false;
98 }
99 fullPath = tmpPath;
100 return true;
101 }
102
IsSystemApp()103 bool WorkSchedUtils::IsSystemApp()
104 {
105 uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
106 return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
107 }
108
GetCurrentTimeMs()109 uint64_t WorkSchedUtils::GetCurrentTimeMs()
110 {
111 using namespace std;
112 auto now = chrono::system_clock::now();
113 chrono::milliseconds currentTimeMs = chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch());
114 return currentTimeMs.count();
115 }
116 } // namespace WorkScheduler
117 } // namespace OHOS