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 "simple_timer_info.h"
17 #include "time_common.h"
18 #include "time_service_stub.h"
19 #include "ntp_update_time.h"
20 #include "ntp_trusted_time.h"
21
22 namespace OHOS {
23 namespace MiscServices {
24 using namespace OHOS::HiviewDFX;
25 namespace {
26 static const uint32_t MAX_EXEMPTION_SIZE = 1000;
27 static const int MAX_PID_LIST_SIZE = 1024;
28 }
29
30
TimeServiceStub()31 TimeServiceStub::TimeServiceStub()
32 {
33 memberFuncMap_ = {
34 { TimeServiceIpcInterfaceCode::SET_TIME,
35 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnSetTime(data, reply); } },
36 { TimeServiceIpcInterfaceCode::SET_TIME_ZONE,
37 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnSetTimeZone(data, reply); } },
38 { TimeServiceIpcInterfaceCode::GET_TIME_ZONE,
39 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetTimeZone(data, reply); } },
40 { TimeServiceIpcInterfaceCode::GET_THREAD_TIME_MILLI,
41 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetThreadTimeMs(data, reply); } },
42 { TimeServiceIpcInterfaceCode::GET_THREAD_TIME_NANO,
43 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetThreadTimeNs(data, reply); } },
44 { TimeServiceIpcInterfaceCode::CREATE_TIMER,
45 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnCreateTimer(data, reply); } },
46 { TimeServiceIpcInterfaceCode::START_TIMER,
47 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnStartTimer(data, reply); } },
48 { TimeServiceIpcInterfaceCode::STOP_TIMER,
49 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnStopTimer(data, reply); } },
50 { TimeServiceIpcInterfaceCode::DESTROY_TIMER,
51 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnDestroyTimer(data, reply); } },
52 { TimeServiceIpcInterfaceCode::PROXY_TIMER,
53 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnTimerProxy(data, reply); } },
54 { TimeServiceIpcInterfaceCode::PID_PROXY_TIMER,
55 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnPidTimerProxy(data, reply); } },
56 { TimeServiceIpcInterfaceCode::ADJUST_TIMER,
57 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnAdjustTimer(data, reply); } },
58 { TimeServiceIpcInterfaceCode::SET_TIMER_EXEMPTION,
59 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t {
60 return OnSetTimerExemption(data, reply); } },
61 { TimeServiceIpcInterfaceCode::RESET_ALL_PROXY,
62 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnAllProxyReset(data, reply); } },
63 { TimeServiceIpcInterfaceCode::GET_NTP_TIME_MILLI,
64 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetNtpTimeMs(data, reply); } },
65 { TimeServiceIpcInterfaceCode::GET_REAL_TIME_MILLI,
66 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetRealTimeMs(data, reply); } },
67 };
68 }
69
~TimeServiceStub()70 TimeServiceStub::~TimeServiceStub()
71 {
72 memberFuncMap_.clear();
73 }
74
OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)75 int32_t TimeServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
76 MessageOption &option)
77 {
78 TIME_HILOGD(TIME_MODULE_SERVICE, " start##code = %{public}u", code);
79 std::u16string descriptor = TimeServiceStub::GetDescriptor();
80 std::u16string remoteDescriptor = data.ReadInterfaceToken();
81 if (descriptor != remoteDescriptor) {
82 TIME_HILOGE(TIME_MODULE_SERVICE, " end##descriptor checked fail");
83 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
84 }
85 pid_t p = IPCSkeleton::GetCallingPid();
86 pid_t p1 = IPCSkeleton::GetCallingUid();
87 TIME_HILOGD(TIME_MODULE_SERVICE, "CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u", p, p1,
88 code);
89 if (code >= static_cast<uint32_t>(TimeServiceIpcInterfaceCode::SET_TIME) &&
90 code <= static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_REAL_TIME_MILLI)) {
91 auto itFunc = memberFuncMap_.find(static_cast<TimeServiceIpcInterfaceCode>(code));
92 if (itFunc != memberFuncMap_.end()) {
93 auto memberFunc = itFunc->second;
94 if (memberFunc != nullptr) {
95 return memberFunc(data, reply);
96 }
97 }
98 }
99 int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
100 TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
101 return ret;
102 }
103
OnSetTime(MessageParcel &data, MessageParcel &reply)104 int32_t TimeServiceStub::OnSetTime(MessageParcel &data, MessageParcel &reply)
105 {
106 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
107 int64_t time = data.ReadInt64();
108 auto apiVersion = data.ReadInt8();
109 if (apiVersion == APIVersion::API_VERSION_9) {
110 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
111 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
112 return E_TIME_NOT_SYSTEM_APP;
113 }
114 }
115 if (!TimePermission::CheckCallingPermission(TimePermission::setTime)) {
116 TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed");
117 return E_TIME_NO_PERMISSION;
118 }
119 int32_t ret = SetTime(time);
120 TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
121 return ret;
122 }
123
OnSetTimeZone(MessageParcel &data, MessageParcel &reply)124 int32_t TimeServiceStub::OnSetTimeZone(MessageParcel &data, MessageParcel &reply)
125 {
126 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
127 std::string timeZoneId = data.ReadString();
128 auto apiVersion = data.ReadInt8();
129 if (apiVersion == APIVersion::API_VERSION_9) {
130 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
131 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
132 return E_TIME_NOT_SYSTEM_APP;
133 }
134 }
135 if (!TimePermission::CheckCallingPermission(TimePermission::setTimeZone)) {
136 TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed");
137 return E_TIME_NO_PERMISSION;
138 }
139 int32_t ret = SetTimeZone(timeZoneId);
140 TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
141 return ret;
142 }
143
OnGetTimeZone(MessageParcel &data, MessageParcel &reply)144 int32_t TimeServiceStub::OnGetTimeZone(MessageParcel &data, MessageParcel &reply)
145 {
146 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
147 std::string timeZoneId;
148 int32_t ret = GetTimeZone(timeZoneId);
149 if (ret != ERR_OK) {
150 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
151 return ret;
152 }
153 reply.WriteString(timeZoneId);
154 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
155 return ret;
156 }
157
OnGetThreadTimeMs(MessageParcel &data, MessageParcel &reply)158 int32_t TimeServiceStub::OnGetThreadTimeMs(MessageParcel &data, MessageParcel &reply)
159 {
160 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
161 int64_t time;
162 int32_t ret = GetThreadTimeMs(time);
163 if (ret != ERR_OK) {
164 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
165 return ret;
166 }
167 reply.WriteInt64(time);
168 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
169 return ret;
170 }
171
OnGetThreadTimeNs(MessageParcel &data, MessageParcel &reply)172 int32_t TimeServiceStub::OnGetThreadTimeNs(MessageParcel &data, MessageParcel &reply)
173 {
174 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
175 int64_t time;
176 int32_t ret = GetThreadTimeNs(time);
177 if (ret != ERR_OK) {
178 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
179 return ret;
180 }
181 reply.WriteInt64(time);
182 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
183 return ret;
184 }
185
OnCreateTimer(MessageParcel &data, MessageParcel &reply)186 int32_t TimeServiceStub::OnCreateTimer(MessageParcel &data, MessageParcel &reply)
187 {
188 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
189 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
190 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
191 return E_TIME_NOT_SYSTEM_APP;
192 }
193 std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent{ nullptr };
194 auto type = data.ReadInt32();
195 auto repeat = data.ReadBool();
196 auto disposable = data.ReadBool();
197 auto interval = data.ReadUint64();
198 if (data.ReadBool()) {
199 wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>(
200 data.ReadParcelable<OHOS::AbilityRuntime::WantAgent::WantAgent>());
201 if (!wantAgent) {
202 TIME_HILOGE(TIME_MODULE_SERVICE, "Input wantagent nullptr");
203 return E_TIME_NULLPTR;
204 }
205 }
206 sptr<IRemoteObject> obj = data.ReadRemoteObject();
207 if (obj == nullptr) {
208 TIME_HILOGE(TIME_MODULE_SERVICE, "Input nullptr");
209 return E_TIME_NULLPTR;
210 }
211 auto timerOptions = std::make_shared<SimpleTimerInfo>();
212 timerOptions->type = type;
213 timerOptions->repeat = repeat;
214 timerOptions->interval = interval;
215 timerOptions->disposable = disposable;
216 timerOptions->wantAgent = wantAgent;
217 uint64_t timerId = data.ReadUint64();
218 auto errCode = CreateTimer(timerOptions, obj, timerId);
219 if (errCode != E_TIME_OK) {
220 TIME_HILOGE(TIME_MODULE_SERVICE, "Create timer failed");
221 return E_TIME_DEAL_FAILED;
222 }
223 if (!reply.WriteUint64(timerId)) {
224 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write timerId");
225 return E_TIME_WRITE_PARCEL_ERROR;
226 }
227 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
228 return ERR_OK;
229 }
230
OnStartTimer(MessageParcel &data, MessageParcel &reply)231 int32_t TimeServiceStub::OnStartTimer(MessageParcel &data, MessageParcel &reply)
232 {
233 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
234 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
235 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
236 return E_TIME_NOT_SYSTEM_APP;
237 }
238 auto timerId = data.ReadUint64();
239 auto triggerTime = data.ReadUint64();
240 if (StartTimer(timerId, triggerTime) != E_TIME_OK) {
241 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to start timer");
242 return E_TIME_DEAL_FAILED;
243 }
244 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
245 return ERR_OK;
246 }
247
OnStopTimer(MessageParcel &data, MessageParcel &reply)248 int32_t TimeServiceStub::OnStopTimer(MessageParcel &data, MessageParcel &reply)
249 {
250 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
251 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
252 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
253 return E_TIME_NOT_SYSTEM_APP;
254 }
255 auto timerId = data.ReadUint64();
256 if (StopTimer(timerId) != E_TIME_OK) {
257 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to stop timer");
258 return E_TIME_DEAL_FAILED;
259 }
260 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
261 return ERR_OK;
262 }
263
OnDestroyTimer(MessageParcel &data, MessageParcel &reply)264 int32_t TimeServiceStub::OnDestroyTimer(MessageParcel &data, MessageParcel &reply)
265 {
266 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
267 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
268 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
269 return E_TIME_NOT_SYSTEM_APP;
270 }
271 auto timerId = data.ReadUint64();
272 if (DestroyTimer(timerId, false) != E_TIME_OK) {
273 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to destory timer");
274 return E_TIME_DEAL_FAILED;
275 }
276 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
277 return ERR_OK;
278 }
279
OnTimerProxy(MessageParcel &data, MessageParcel &reply)280 int32_t TimeServiceStub::OnTimerProxy(MessageParcel &data, MessageParcel &reply)
281 {
282 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
283 auto uid = data.ReadInt32();
284 if (uid == 0) {
285 TIME_HILOGE(TIME_MODULE_SERVICE, "Error param uid.");
286 return E_TIME_READ_PARCEL_ERROR;
287 }
288 auto isProxy = data.ReadBool();
289 auto needRetrigger = data.ReadBool();
290 if (!ProxyTimer(uid, isProxy, needRetrigger)) {
291 return E_TIME_DEAL_FAILED;
292 }
293 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
294 return ERR_OK;
295 }
296
OnPidTimerProxy(MessageParcel &data, MessageParcel &reply)297 int32_t TimeServiceStub::OnPidTimerProxy(MessageParcel &data, MessageParcel &reply)
298 {
299 auto pidListSize = data.ReadInt32();
300 std::set<int> pidList;
301 if (pidListSize == 0 || pidListSize > MAX_PID_LIST_SIZE) {
302 TIME_HILOGE(TIME_MODULE_SERVICE, "Error pid list size.");
303 return E_TIME_READ_PARCEL_ERROR;
304 }
305 for (int i = 0; i < pidListSize; i++) {
306 auto pid = data.ReadInt32();
307 if (pid != 0) {
308 pidList.insert(pid);
309 }
310 }
311 if (pidList.size() == 0) {
312 TIME_HILOGE(TIME_MODULE_SERVICE, "Error pidList.");
313 return E_TIME_READ_PARCEL_ERROR;
314 }
315 auto isProxy = data.ReadBool();
316 auto needRetrigger = data.ReadBool();
317 if (!ProxyTimer(pidList, isProxy, needRetrigger)) {
318 return E_TIME_DEAL_FAILED;
319 }
320 return ERR_OK;
321 }
322
OnAdjustTimer(MessageParcel &data, MessageParcel &reply)323 int32_t TimeServiceStub::OnAdjustTimer(MessageParcel &data, MessageParcel &reply)
324 {
325 TIME_HILOGD(TIME_MODULE_SERVICE, "on timer adjust start.");
326 if (!TimePermission::CheckProxyCallingPermission()) {
327 TIME_HILOGE(TIME_MODULE_SERVICE, "Adjust Timer permission check failed");
328 return E_TIME_NO_PERMISSION;
329 }
330 bool isAdjust = false;
331 uint32_t interval = 0;
332 if (!data.ReadBool(isAdjust)) {
333 return E_TIME_READ_PARCEL_ERROR;
334 }
335 if (!data.ReadUint32(interval)) {
336 return E_TIME_READ_PARCEL_ERROR;
337 }
338 if (isAdjust && interval == 0) {
339 TIME_HILOGE(TIME_MODULE_SERVICE, "invalid parameter: interval");
340 return E_TIME_READ_PARCEL_ERROR;
341 }
342 if (AdjustTimer(isAdjust, interval) != E_TIME_OK) {
343 TIME_HILOGE(TIME_MODULE_SERVICE, "Error adjust timer.");
344 return E_TIME_DEAL_FAILED;
345 }
346 TIME_HILOGD(TIME_MODULE_SERVICE, "on timer adjust end.");
347 return ERR_OK;
348 }
349
OnSetTimerExemption(MessageParcel &data, MessageParcel &reply)350 int32_t TimeServiceStub::OnSetTimerExemption(MessageParcel &data, MessageParcel &reply)
351 {
352 TIME_HILOGD(TIME_MODULE_SERVICE, "set timer exemption start.");
353 if (!TimePermission::CheckProxyCallingPermission()) {
354 TIME_HILOGE(TIME_MODULE_SERVICE, "Set Timer Exemption permission check failed");
355 return E_TIME_NO_PERMISSION;
356 }
357 std::unordered_set<std::string> nameArr;
358 uint32_t size;
359 bool isExemption;
360 if (!data.ReadUint32(size)) {
361 return E_TIME_READ_PARCEL_ERROR;
362 }
363 if (size > MAX_EXEMPTION_SIZE) {
364 return E_TIME_PARAMETERS_INVALID;
365 }
366 for (uint32_t i = 0; i < size; ++i) {
367 std::string name;
368 if (!data.ReadString(name)) {
369 return E_TIME_READ_PARCEL_ERROR;
370 }
371 nameArr.insert(name);
372 }
373
374 if (!data.ReadBool(isExemption)) {
375 return E_TIME_READ_PARCEL_ERROR;
376 }
377 SetTimerExemption(nameArr, isExemption);
378 TIME_HILOGD(TIME_MODULE_SERVICE, "set timer exemption end.");
379 return ERR_OK;
380 }
381
OnAllProxyReset(MessageParcel &data, MessageParcel &reply)382 int32_t TimeServiceStub::OnAllProxyReset(MessageParcel &data, MessageParcel &reply)
383 {
384 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
385 if (!ResetAllProxy()) {
386 return E_TIME_DEAL_FAILED;
387 }
388 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
389 return ERR_OK;
390 }
391
OnGetNtpTimeMs(MessageParcel &data, MessageParcel &reply)392 int32_t TimeServiceStub::OnGetNtpTimeMs(MessageParcel &data, MessageParcel &reply)
393 {
394 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
395 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
396 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
397 return E_TIME_NOT_SYSTEM_APP;
398 }
399 int64_t time = 0;
400 auto ret = GetNtpTimeMs(time);
401 if (ret != E_TIME_OK) {
402 return ret;
403 }
404 if (!reply.WriteUint64(time)) {
405 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write NTP time");
406 return E_TIME_WRITE_PARCEL_ERROR;
407 }
408 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
409 return ERR_OK;
410 }
411
OnGetRealTimeMs(MessageParcel &data, MessageParcel &reply)412 int32_t TimeServiceStub::OnGetRealTimeMs(MessageParcel &data, MessageParcel &reply)
413 {
414 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
415 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
416 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
417 return E_TIME_NOT_SYSTEM_APP;
418 }
419 int64_t time = 0;
420 auto ret = GetRealTimeMs(time);
421 if (ret != E_TIME_OK) {
422 return ret;
423 }
424 if (!reply.WriteUint64(time)) {
425 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write NTP time");
426 return E_TIME_WRITE_PARCEL_ERROR;
427 }
428 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
429 return ERR_OK;
430 }
431 } // namespace MiscServices
432 } // namespace OHOS