1 /*
2 * Copyright (c) 2025 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 "device_info_service.h"
17
18 #include <thread>
19
20 #ifdef INIT_SUPPORT_ACCESS_TOKEN
21 #include "accesstoken_kit.h"
22 #endif
23 #include "device_info_kits.h"
24 #include "ipc_skeleton.h"
25 #include "if_system_ability_manager.h"
26 #include "iservice_registry.h"
27 #include "system_ability_definition.h"
28 #include "param_comm.h"
29 #include "parameter.h"
30 #include "sysparam_errno.h"
31 #include "init_utils.h"
32
33 namespace OHOS {
34 #ifdef INIT_SUPPORT_ACCESS_TOKEN
35 using namespace Security;
36 using namespace Security::AccessToken;
37 #endif
38
39 namespace device_info {
40
41 REGISTER_SYSTEM_ABILITY_BY_ID(DeviceInfoService, SYSPARAM_DEVICE_SERVICE_ID, true)
42
43 static std::mutex g_lock;
44 static struct timespec g_lastTime;
45
46 #ifndef STARTUP_INIT_TEST
47 static const int DEVICE_INFO_EXIT_TIMEOUT_S = 60;
48 #else
49 static const int DEVICE_INFO_EXIT_TIMEOUT_S = 3;
50 #endif
51 static const int DEVICE_INFO_EXIT_WAITTIMES = 12;
52
UnloadDeviceInfoSa(void)53 static int UnloadDeviceInfoSa(void)
54 {
55 {
56 std::unique_lock<std::mutex> lock(g_lock);
57 struct timespec currTimer = {0};
58 (void)clock_gettime(CLOCK_MONOTONIC, &currTimer);
59 if (IntervalTime(&g_lastTime, &currTimer) < DEVICE_INFO_EXIT_TIMEOUT_S) {
60 return 0;
61 }
62 }
63 DINFO_LOGI("DeviceInfoService::UnloadDeviceInfoSa");
64 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
65 DINFO_CHECK(sam != nullptr, return 0, "GetSystemAbilityManager return null");
66
67 int32_t ret = sam->UnloadSystemAbility(SYSPARAM_DEVICE_SERVICE_ID);
68 DINFO_CHECK(ret == ERR_OK, return 0, "UnLoadSystemAbility deviceinfo sa failed");
69 return 1;
70 }
71
GetUdid(std::string & result)72 int32_t DeviceInfoService::GetUdid(std::string& result)
73 {
74 int ret = ERR_FAIL;
75 char localDeviceInfo[UDID_LEN] = {0};
76 ret = GetDevUdid_(localDeviceInfo, UDID_LEN);
77 DINFO_CHECK(ret == 0, return ret, "Failed to get dev udid");
78 result = std::string(localDeviceInfo);
79 return ret;
80 }
81
GetSerialID(std::string & result)82 int32_t DeviceInfoService::GetSerialID(std::string& result)
83 {
84 int ret = ERR_FAIL;
85 const char *serialNumber = GetSerial_();
86 DINFO_CHECK(serialNumber != nullptr, return ret, "Failed to get serialNumber");
87 result = std::string(serialNumber);
88 return 0;
89 }
90
GetDiskSN(std::string & result)91 int32_t DeviceInfoService::GetDiskSN(std::string& result)
92 {
93 int ret = ERR_FAIL;
94 char diskSN[DISK_SN_LEN] = {0};
95 ret = GetDiskSN_(diskSN, DISK_SN_LEN);
96 DINFO_CHECK(ret == 0, return ret, "Failed to get disk SN");
97 result = std::string(diskSN);
98 return ret;
99 }
100
CallbackEnter(uint32_t code)101 int32_t DeviceInfoService::CallbackEnter(uint32_t code)
102 {
103 {
104 std::unique_lock<std::mutex> lock(g_lock);
105 (void)clock_gettime(CLOCK_MONOTONIC, &g_lastTime);
106 }
107 if (!CheckPermission("ohos.permission.sec.ACCESS_UDID")) {
108 return SYSPARAM_PERMISSION_DENIED;
109 }
110 return 0;
111 }
112
CallbackExit(uint32_t code,int32_t result)113 int32_t DeviceInfoService::CallbackExit(uint32_t code, int32_t result)
114 {
115 return 0;
116 }
117
CheckPermission(const std::string & permission)118 bool DeviceInfoService::CheckPermission(const std::string &permission)
119 {
120 #ifdef INIT_SUPPORT_ACCESS_TOKEN
121 AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
122 int32_t result = TypePermissionState::PERMISSION_GRANTED;
123 int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(callerToken);
124 if (tokenType == TOKEN_INVALID) {
125 DINFO_LOGE("AccessToken type:%d, permission:%d denied!", tokenType, callerToken);
126 return false;
127 } else {
128 result = AccessTokenKit::VerifyAccessToken(callerToken, permission);
129 }
130 if (result == TypePermissionState::PERMISSION_DENIED) {
131 DINFO_LOGE("permission:%s denied!", permission.c_str());
132 return false;
133 }
134 return true;
135 #else
136 DINFO_LOGE("CheckPermission is not supported");
137 return false;
138 #endif
139 }
140
OnStart(void)141 void DeviceInfoService::OnStart(void)
142 {
143 int level = GetIntParameter(INIT_DEBUG_LEVEL, (int)INIT_INFO);
144 SetInitLogLevel((InitLogLevel)level);
145 DINFO_LOGI("DeviceInfoService OnStart");
146 bool res = Publish(this);
147 if (!res) {
148 DINFO_LOGE("DeviceInfoService Publish failed");
149 }
150 threadStarted_ = true;
151 std::thread([this] {this->ThreadForUnloadSa();}).detach();
152 }
153
OnStop(void)154 void DeviceInfoService::OnStop(void)
155 {
156 threadStarted_ = false;
157 DINFO_LOGI("DeviceInfoService OnStop");
158 }
159
Dump(int fd,const std::vector<std::u16string> & args)160 int DeviceInfoService::Dump(int fd, const std::vector<std::u16string>& args)
161 {
162 (void)args;
163 DINFO_LOGI("DeviceInfoService Dump");
164 DINFO_CHECK(fd >= 0, return -1, "Invalid fd for dump %d", fd);
165 return dprintf(fd, "%s\n", "No information to dump for this service");
166 }
167
ThreadForUnloadSa(void)168 void DeviceInfoService::ThreadForUnloadSa(void)
169 {
170 while (threadStarted_) {
171 std::this_thread::sleep_for(std::chrono::seconds(DEVICE_INFO_EXIT_TIMEOUT_S / DEVICE_INFO_EXIT_WAITTIMES));
172 if (UnloadDeviceInfoSa() == 1) {
173 break;
174 }
175 }
176 }
177 } // namespace device_info
178 } // namespace OHOS