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