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