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 switch (code) {
108 case static_cast<uint32_t>(IDeviceInfoIpcCode::COMMAND_GET_UDID):
109 case static_cast<uint32_t>(IDeviceInfoIpcCode::COMMAND_GET_SERIAL_I_D): {
110 if (!CheckPermission("ohos.permission.sec.ACCESS_UDID")) {
111 return SYSPARAM_PERMISSION_DENIED;
112 }
113 break;
114 }
115 case static_cast<uint32_t>(IDeviceInfoIpcCode::COMMAND_GET_DISK_S_N): {
116 if (!CheckPermission("ohos.permission.ACCESS_DISK_PHY_INFO")) {
117 return SYSPARAM_PERMISSION_DENIED;
118 }
119 break;
120 }
121 default: {
122 return SYSPARAM_PERMISSION_DENIED;
123 }
124 }
125 return 0;
126 }
127
CallbackExit(uint32_t code,int32_t result)128 int32_t DeviceInfoService::CallbackExit(uint32_t code, int32_t result)
129 {
130 return 0;
131 }
132
CheckPermission(const std::string & permission)133 bool DeviceInfoService::CheckPermission(const std::string &permission)
134 {
135 #ifdef INIT_SUPPORT_ACCESS_TOKEN
136 AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
137 int32_t result = TypePermissionState::PERMISSION_GRANTED;
138 int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(callerToken);
139 if (tokenType == TOKEN_INVALID) {
140 DINFO_LOGE("AccessToken type:%d, permission:%d denied!", tokenType, callerToken);
141 return false;
142 } else {
143 result = AccessTokenKit::VerifyAccessToken(callerToken, permission);
144 }
145 if (result == TypePermissionState::PERMISSION_DENIED) {
146 DINFO_LOGE("permission:%s denied!", permission.c_str());
147 return false;
148 }
149 return true;
150 #else
151 DINFO_LOGE("CheckPermission is not supported");
152 return false;
153 #endif
154 }
155
OnStart(void)156 void DeviceInfoService::OnStart(void)
157 {
158 int level = GetIntParameter(INIT_DEBUG_LEVEL, (int)INIT_INFO);
159 SetInitLogLevel((InitLogLevel)level);
160 DINFO_LOGI("DeviceInfoService OnStart");
161 bool res = Publish(this);
162 if (!res) {
163 DINFO_LOGE("DeviceInfoService Publish failed");
164 }
165 threadStarted_ = true;
166 std::thread([this] {this->ThreadForUnloadSa();}).detach();
167 }
168
OnStop(void)169 void DeviceInfoService::OnStop(void)
170 {
171 threadStarted_ = false;
172 DINFO_LOGI("DeviceInfoService OnStop");
173 }
174
Dump(int fd,const std::vector<std::u16string> & args)175 int DeviceInfoService::Dump(int fd, const std::vector<std::u16string>& args)
176 {
177 (void)args;
178 DINFO_LOGI("DeviceInfoService Dump");
179 DINFO_CHECK(fd >= 0, return -1, "Invalid fd for dump %d", fd);
180 return dprintf(fd, "%s\n", "No information to dump for this service");
181 }
182
ThreadForUnloadSa(void)183 void DeviceInfoService::ThreadForUnloadSa(void)
184 {
185 while (threadStarted_) {
186 std::this_thread::sleep_for(std::chrono::seconds(DEVICE_INFO_EXIT_TIMEOUT_S / DEVICE_INFO_EXIT_WAITTIMES));
187 if (UnloadDeviceInfoSa() == 1) {
188 break;
189 }
190 }
191 }
192 } // namespace device_info
193 } // namespace OHOS