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 default: {
106 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
107 }
108 }
109 return ret;
110 }
111
CheckPermission(MessageParcel & data,const std::string & permission)112 bool DeviceInfoStub::CheckPermission(MessageParcel &data, const std::string &permission)
113 {
114 AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
115 int32_t result = TypePermissionState::PERMISSION_GRANTED;
116 int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(callerToken);
117 if (tokenType == TOKEN_INVALID) {
118 DINFO_LOGE("AccessToken type:%d, permission:%d denied!", tokenType, callerToken);
119 return false;
120 } else {
121 result = AccessTokenKit::VerifyAccessToken(callerToken, permission);
122 }
123 if (result == TypePermissionState::PERMISSION_DENIED) {
124 DINFO_LOGE("permission:%s denied!", permission.c_str());
125 return false;
126 }
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([this] {this->ThreadForUnloadSa();}).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 / DEVICE_INFO_EXIT_WAITTIMES));
171 if (!threadStarted_) {
172 break;
173 }
174 if (UnloadDeviceInfoSa() == 1) {
175 break;
176 }
177 }
178 }
179 } // namespace device_info
180 } // namespace OHOS
181